了解bjam的目标,以及如何指定新的目标?

时间:2012-09-11 23:55:01

标签: boost makefile boost-python bjam boost-build

我在理解如何使用bjam指定和调用目标时遇到问题。通过这个,我的意思是我想为bjam提供命令行目标来构建(实际上来自Makefile),这些目标对应于构建过程的不同方面,而不是仅仅运行整个事物。 / p>

例如,现在当我键入'bjam'时,它会关闭并构建一个python扩展,运行一个单元测试文件,并创建一个单独的'main'可执行文件。我有自定义规则执行每个步骤,我的Jamfile只是按顺序列出它们:

project-name = example ;

sources =
  $(project-name).cpp
  $(project-name)_ext.cpp
  ;

build-ext $(project-name) : $(sources) ;

build-main $(project-name) ;

在我的Jamroot(上一个目录)中我定义了这些规则,这里是不完整的文件:

# A rule to simplify declaration of extension tests:
rule run-test ( test-name : sources + )
{
    import testing ;
    testing.make-test run-pyd : $(sources) : : $(test-name) ;
}

# A rule to further simply declaration of extension tests:
rule run-ext-test ( project-name )
{
  run-test $(project-name) : $(project-name)_ext test_$(project-name)_ext.py ;
}

# A rule to simplify copying of the extension and Boost.Python libraries to the current directory
rule convenient-copy ( project-name )
{
  install convenient_copy
    : $(project-name)_ext
    : <install-dependencies>on <install-type>SHARED_LIB <install-type>PYTHON_EXTENSION
      <location>.
    ;
}

rule build-ext ( project-name : sources + )
{
  python-extension $(project-name)_ext : $(sources) : ;

  # copy the extension and Boost.Python libraries to the current directory
  convenient-copy $(project-name) ;

  # run extension tests
  run-ext-test $(project-name) ;
}

rule build-main ( project-name : other-sources * )
{
  obj $(project-name).o : $(project-name).cpp ;
  exe main_$(project-name) : main_$(project-name).cpp $(project-name).o $(other-sources) ;
  install main : main_$(project-name) : <location>. ;
}

但是我注意到以下对bjam的调用没有做我希望他们做的事情:

$ bjam build-main
notice: could not find main target build-main
notice: assuming it is a name of file to create.
don't know how to make <e>build-main
...found 1 target...
...can't find 1 target...

$ bjam main_example
...patience...
...patience...
...found 1597 targets...
...updating 3 targets...
gcc.compile.c++ bin/gcc-4.6/debug/main_example.o
gcc.compile.c++ bin/gcc-4.6/debug/example.o
gcc.link bin/gcc-4.6/debug/main_example
...updated 3 targets...

^^^但是没有运行安装规则,因此不会将二进制文件复制到Jamfile目录。

奇怪的是,有一些目标可以做事,但并不总是我所期望的:

$ bjam main
...patience...
...patience...
...found 1598 targets...
...updating 3 targets...
gcc.compile.c++ bin/gcc-4.6/debug/main_example.o
gcc.compile.c++ bin/gcc-4.6/debug/example.o
gcc.link main_example
...updated 3 targets...

这确实在Jamfile目录中创建了二进制文件。

main目标来自哪里?我没有定义它......

另一个奇怪的人:

$ bjam example_ext
...patience...
...patience...
...found 2834 targets...
...updating 3 targets...
gcc.compile.c++ bin/gcc-4.6/debug/example.o
gcc.compile.c++ bin/gcc-4.6/debug/example_ext.o
gcc.link.dll bin/gcc-4.6/debug/example_ext.so
...updated 3 targets...

^^^创建了example_ext.so,但没有将其复制到Jamfile位置。

$ bjam example_ext.so
notice: could not find main target example_ext.so
notice: assuming it is a name of file to create.
...patience...
...patience...
...found 2836 targets...
...updating 4 targets...
gcc.compile.c++ bin/gcc-4.6/debug/example.o
gcc.compile.c++ bin/gcc-4.6/debug/example_ext.o
gcc.link.dll bin/gcc-4.6/debug/example_ext.so
common.copy example_ext.so
...updated 4 targets...

^^^创建了.so文件并将其复制,但没有调用方便复制来引入libboost_python.so文件。

我真的不明白这里发生了什么。 bjam文档确实给我带来了严重的问题。它详细描述了目标,但是在规则的上下文中,而不是在从命令行调用bjam的上下文中。我确实遇到过一些关于伪目标和'生成'的提及,但它似乎太复杂了,我觉得应该是一个简单的用例。还提到了一种“绑定”机制,但文档中提到=$(BINDRULE[1])=这对我来说毫无意义。

我也遇到过别名,NOTFILEexplicit,但我不确定自己是否走在正确的轨道上,并且无法做出任何结论。

有没有关于如何在bjam中创建自定义目标的好例子?或者我只是试图以非预期的方式使用bjam?

1 个答案:

答案 0 :(得分:4)

没有参数的bjam调用会构建所有内容,因为您没有任何标记为explicit的目标,除非明确请求,否则可以用来阻止某些目标的构建。

bjam build-main失败,因为build-main不是要生成的目标或文件的名称;它是可以使用不同参数调用的规则(函数)的名称,每个调用声明不同的目标集。

bjam main_example生成声明的目标:

exe main_$(project-name) : main_$(project-name).cpp $(project-name).o $(other-sources) ;

自然不包含install目标,在下一行宣布。

bjam main构建main_example并安装它,因为main是其install目标的名称,声明为:install main : main_$(project-name) : <location>. ;

请注意,如果您在jamfile中多次调用build-main,则每个bjam调用都会以error: No best alternative for ./main退出,因此最好将安装目标的名称重命名为{{1}防止名称冲突。然后install_main_$(project-name)将构建并安装bjam install_main_example

main_example生成声明的目标:

bjam example_ext

并且不会像使用python-extension $(project-name)_ext : $(sources) : ; 那样重新安装。

bjam main_example的工作原理bjam example_ext.so确实是一个正在创建的文件名(当然在给定的平台下),因此所有导致名为example_ext.so的文件的目标都将由那个调用。现在这就是为什么example_ext.so指示要安装的所有文件都是convenient-copy调用安装的原因。在这里,我想澄清一些事情:“没有调用方便复制”是不太准确的术语。 bjam example_ext.so是规则的名称,而不是目标,并且使用上面编写的代码,无论bjam调用参数如何,都将始终调用该规则。调用时所做的就是声明一个名为convenient-copy的目标(注意下划线),这反过来导致(隐式)声明一些文件目标(用于安装),例如: example_ext.so,libboost_python.so以及convenient_copy匹配的其他依赖项。当<install-type>SHARED_LIB <install-type>PYTHON_EXTENSION规则被调用时,它实际上并没有构建任何东西,只是声明了一些目标。实际构建的内容将在稍后阶段决定,决定取决于bjam调用参数。

convenient-copy将构建example_ext.so并将其与其依赖项一起正确安装,但它遇到bjam convenient_copy安装目标所遇到的同样问题:如果您调用main它将会中断不止一次。将名称从convenient-copy更改为convenient_copy可以解决问题,然后您可以使用install_$(project-name)_ext调用安装。

最后,如果您希望目标不在没有参数的bjam调用的基础上构建,则将该目标标记为显式,例如。

bjam install_example_ext
除非明确请求,否则

将禁止main_example的安装。要阻止main_example的构建,请为explicit install_main_$(project-name) ; explicit添加main_$(project-name)

$(project-name).o

或全部:

explicit main_$(project-name) ;
explicit $(project-name).o ;

请注意,explicit install_main_$(project-name) main_$(project-name) $(project-name).o ; 声明main_$(project-name)explicit未声明为install_main_$(project-name)$(project-name).o声明为explicitmain_$(project-name)未声明为explicit没有意义,就像请求构建目标一样,即使这些依赖项都是{{1}},也会请求构建其所有依赖项目标。