1. 程式人生 > >bcp,只為boost庫瘦身

bcp,只為boost庫瘦身

Boost簡介

Boost,是一組經過同行評審的(peer-reviewed)可移植的C++程式碼庫。採用Boost Software License進行開源,其作者很多都來自C++標準委員會。Boost庫已成為C++的TR1標準之一,同時實現了很多C++11的功能(號稱C++11的準標準庫)。

Boost提供了很多高階的演算法,資料結構等元件,應用非常廣泛,如mongodb就採用了很多boost元件。陳碩的muduo庫,也採用了boost的很多庫……。其中,應用最廣泛的,如array,call_traits等。

雖然,Boost很好,但不是每一個元件都是最佳實踐(編碼風格等),不是每一個庫都很好用,不是每一個都達到產品級的質量要求。以下是

Google C++ Style Guide3.245版本,針對Boost庫的使用規範:

Boost

Use only approved libraries from the Boost library collection.

Definition:

The Boost library collection is a popular collection of peer-reviewed, free, open-source C++ libraries.

Pros:

Boost code is generally very high-quality, is widely portable, and fills many important gaps in the C++ standard library, such as type traits, better binders, and better smart pointers. It also provides an implementation of the TR1 extension to the standard library.

Cons:

Some Boost libraries encourage coding practices which can hamper readability, such as metaprogramming and other advanced template techniques, and an excessively "functional" style of programming.

Decision:

In order to maintain a high level of readability for all contributors who might read and maintain code, we only allow an approved subset of Boost features. Currently, the following libraries are permitted:

Call Traits from boost/call_traits.hpp
Compressed Pair from boost/compressed_pair.hpp
Pointer Container from boost/ptr_container except serialization and wrappers for containers not in the C++03  standard (ptr_circular_buffer.hpp and ptr_unordered*)
Array from boost/array.hpp
The Boost Graph Library (BGL) from boost/graph, except serialization (adj_list_serialize.hpp) and parallel/distributed algorithms and data structures (boost/graph/parallel/* and boost/graph/distributed/*).
Property Map from boost/property_map, except parallel/distributed property maps (boost/property_map/parallel/*).
The part of Iterator that deals with defining iterators: boost/iterator/iterator_adaptor.hpp, boost/iterator/iterator_facade.hpp, and boost/function_output_iterator.hpp
The part of Polygon that deals with Voronoi diagram construction and doesn't depend on the rest of Polygon: boost/polygon/voronoi_builder.hpp, boost/polygon/voronoi_diagram.hpp, and boost/polygon/voronoi_geometry_type.hpp

We are actively considering adding other Boost features to the list, so this rule may be relaxed in the future.

另外,一個關鍵性的問題就是Boost標準發行版本實在太大了。解壓之後大概400多M,如果有完整編譯,需要1.2G左右的磁碟空間。雖然,有一小部分元件或實用功能不需要(Boost幾乎就是template的樂高積木)編譯,但這依然是阻礙很多人使用的一個原因。還好,Boost為了方便開發人員,組織和釋出屬於自己的獨立元件,而提供了一個bcp工具(估計很多人都不知道)。當然,任何人都可以通過bcp來裁剪boost,以滿足自己的需求(mongodb使用的,就是經過裁剪的boost庫)。bcp才是本文的主角,下面言歸正傳。

bcp實用工具

bcp是一個專門用於提取Boost子集元件的工具。對於想要從Boost獨立釋出個人的庫程式碼的Boost開發者,以及想根據自己的需求裁剪/定製Boost的使用者來說,這是一個非常有用的工具。

bcp同時可以報告你的程式碼依賴Boost元件以及這些元件使用的licences。

bcp語法

bcp --list [options] module-list

列出module-list及其所有依賴的檔案列表。

bcp [options] module-list output-path

拷貝module-list依賴的所有檔案到output-path目錄。

bcp --report [options] module-list html-file

輸出module-list的所有依賴到html報告中。其中依賴的檔案,包括:licenses、原始碼和測試程式程式碼等。 命令選項,包括:

--boost=path

設定boost程式碼樹本地路徑。如果沒有指定此選項,預設會將當前路徑作為Boost程式碼樹的跟目錄

--scan

將module列表(module list)作為非boost檔案,通過掃描確定對Boost的依賴,不會拷貝或列出模組列表自身相關的檔案。

--svn

僅僅拷貝處於svn版本管理之下的檔案。

--unix-lines

確保所有被拷貝的檔案都採用unix風格的行編輯模式(line endings)。

module-list: 

沒有指定--scan選項時,用於指定待拷貝的boost檔案或庫名列表。這個列表可以是:

一個工具的名稱:如`build`,將找到`tools/build`;
一個庫的名稱:如`regex`;
一個頭檔案的title:如`scoped_ptr`,將找到`boost/scoped_ptr.hpp`;
一個頭檔案的檔名:如`scoped_ptr.hpp`,將找到`boost/scoped_ptr.hpp`;
一個檔名:如`boost/regex.hpp`

當指定了--scan選項時,是一個非Boost(non-boost)的檔案列表,用於掃描其對boost的依賴,這些檔案將不會被拷貝或羅列。

output-path

目標檔案的輸出目錄。

bcp使用示例

bcp scoped_ptr /foo

拷貝boost/scoped_ptr.hpp及其依賴的原始碼到/foo目錄。

bcp boost/regex.hpp /foo

拷貝boost/regex.hpp和所有依賴的程式碼,包括regex原始碼(libs/regex/src)和構建檔案(libs/regex/build)到/foo目錄。但是,不會拷貝regex文件,test,或示/樣例程式碼。

bcp regex /foo

拷貝所有regex庫(libs/regex),包括依賴的程式碼(比如,regex測試程式依賴的boost.test原始碼)到/foo

bcp regex config build /foo

拷貝所有regex庫(libs/regex),附加config庫(libs/config)和構建系統(tools/build)以及所有的依賴程式碼到/foo。(這個非常有用

bcp --scan --boost=/boost foo.cpp bar.cpp boost

掃描非boost檔案foo.cpp和bar.cpp對boost的依賴程式碼。同時,將其拷貝到子目錄boost下。(在不明確依賴關係的情況下,這個模式非常有用

bcp --report regex.hpp boost-regex-report.html

為Boost模組regex.hpp建立一個名為boost-regex-report.html的HTML報告檔案。此報告包含,license資訊,作者明細和檔案依賴。

bcp應用演示

編譯boost及bcp

1、下載 boost_1_53_0程式碼到~/boost目錄,並進行本地編譯

[email protected] boost_1_53_0> cd ~/boost
[email protected] boost> tar xzvf boost_1_53_0.tar.gz
[email protected] boost_1_53_0> cd boost_1_53_0
[email protected] boost_1_53_0> ./bootstrap.sh
[email protected] boost_1_53_0> ./b2
[email protected] boost_1_53_0> ./bjam --toolset=gcc stage

[email protected] boost_1_53_0> cd tools/bcp
[email protected] bcp> ../../bjam --toolset=gcc stage

2、定製boost/timer.hpp對應的依賴程式碼

[email protected] bcp> cd ../..
[email protected] boost_1_53_0> mkdir -p foo
[email protected] boost_1_53_0> dist/bin/bcp boost/timer.hpp foo
 Copying file: boost/config.hpp
 Copying file: boost/config/abi/borland_prefix.hpp
 Copying file: boost/config/abi/borland_suffix.hpp
 Copying file: boost/config/abi/msvc_prefix.hpp
 Copying file: boost/config/abi/msvc_suffix.hpp
 Copying file: boost/config/abi_prefix.hpp
 Copying file: boost/config/abi_suffix.hpp
 Copying file: boost/config/auto_link.hpp
 Copying file: boost/config/compiler/borland.hpp
 Copying file: boost/config/compiler/clang.hpp
 Copying file: boost/config/compiler/codegear.hpp
 Copying file: boost/config/compiler/comeau.hpp
 Copying file: boost/config/compiler/common_edg.hpp
 Copying file: boost/config/compiler/compaq_cxx.hpp
 Copying file: boost/config/compiler/cray.hpp
 Copying file: boost/config/compiler/digitalmars.hpp
 Copying file: boost/config/compiler/gcc.hpp
 Copying file: boost/config/compiler/gcc_xml.hpp
 Copying file: boost/config/compiler/greenhills.hpp
 Copying file: boost/config/compiler/hp_acc.hpp
 Copying file: boost/config/compiler/intel.hpp
 Copying file: boost/config/compiler/kai.hpp
 Copying file: boost/config/compiler/metrowerks.hpp
 Copying file: boost/config/compiler/mpw.hpp
 Copying file: boost/config/compiler/nvcc.hpp
 Copying file: boost/config/compiler/pathscale.hpp
 Copying file: boost/config/compiler/pgi.hpp
 Copying file: boost/config/compiler/sgi_mipspro.hpp
 Copying file: boost/config/compiler/sunpro_cc.hpp
 Copying file: boost/config/compiler/vacpp.hpp
 Copying file: boost/config/compiler/visualc.hpp
 Copying file: boost/config/no_tr1/cmath.hpp
 Copying file: boost/config/no_tr1/complex.hpp
 Copying file: boost/config/no_tr1/functional.hpp
 Copying file: boost/config/no_tr1/memory.hpp
 Copying file: boost/config/no_tr1/utility.hpp
 Copying file: boost/config/platform/aix.hpp
 Copying file: boost/config/platform/amigaos.hpp
 Copying file: boost/config/platform/beos.hpp
 Copying file: boost/config/platform/bsd.hpp
 Copying file: boost/config/platform/cray.hpp
 Copying file: boost/config/platform/cygwin.hpp
 Copying file: boost/config/platform/hpux.hpp
 Copying file: boost/config/platform/irix.hpp
 Copying file: boost/config/platform/linux.hpp
 Copying file: boost/config/platform/macos.hpp
 Copying file: boost/config/platform/qnxnto.hpp
 Copying file: boost/config/platform/solaris.hpp
 Copying file: boost/config/platform/symbian.hpp
 Copying file: boost/config/platform/vms.hpp
 Copying file: boost/config/platform/vxworks.hpp
 Copying file: boost/config/platform/win32.hpp
 Copying file: boost/config/posix_features.hpp
 Copying file: boost/config/requires_threads.hpp
 Copying file: boost/config/select_compiler_config.hpp
 Copying file: boost/config/select_platform_config.hpp
 Copying file: boost/config/select_stdlib_config.hpp
 Copying file: boost/config/stdlib/dinkumware.hpp
 Copying file: boost/config/stdlib/libcomo.hpp
 Copying file: boost/config/stdlib/libcpp.hpp
 Copying file: boost/config/stdlib/libstdcpp3.hpp
 Copying file: boost/config/stdlib/modena.hpp
 Copying file: boost/config/stdlib/msl.hpp
 Copying file: boost/config/stdlib/roguewave.hpp
 Copying file: boost/config/stdlib/sgi.hpp
 Copying file: boost/config/stdlib/stlport.hpp
 Copying file: boost/config/stdlib/vacpp.hpp
 Copying file: boost/config/suffix.hpp
 Copying file: boost/config/user.hpp
 Copying file: boost/config/warning_disable.hpp
 Copying file: boost/detail/endian.hpp
 Copying file: boost/detail/limits.hpp
 Copying file: boost/limits.hpp
 Copying file: boost/non_type.hpp
 Copying file: boost/timer.hpp
 Copying file: boost/type.hpp
 Copying file: boost/version.hpp
 no errors detected

擴充套件閱讀

祝大家玩的開心