1

build.jam:

project : usage-requirements <include>. ;

alias PUB : : : : <use>/ProjectA//PUB
                  <use>/ProjectB//PUB ;

lib LIB : [ glob *.c : feature.c ]
        : <link>static
          <use>/ProjectA//PUB
          <use>/ProjectB//PUB ;

I would like to add a target that will add feature.c to the sources and define USE_FEATURE. I've tried a few different things, but none seem to work as I want.

alias LIB_WITH_FEAT : LIB feature.c : <define>USE_FEATURE ;

alias LIB_WITH_FEAT : LIB : <source>feature.c <define>USE_FEATURE ;

does not add feature.c or USE_FEATURE to the build. But gives no errors or warnings. It just builds LIB.

lib LIB_WITH_FEAT : feature.c LIB : <define>USE_FEATURE ;

gives "warn: Unable to construct LIB_WITH_FEAT". Although if it worked, I don't think it'd be what I wanted as it would try to build LIB separately and LIB needs the USE_FEATURE to work properly with feature.c.

Gabriel Devillers
  • 3,155
  • 2
  • 30
  • 53
Jason Brown
  • 174
  • 9
  • Your question is terribly unclear.. What do you want to add feature.c to? – GrafikRobot Feb 11 '15 at 21:50
  • Sorry, You can build target LIB to build the library, but from some projects I want to build the library with Feature X. The implementation of Feature X is in feature.c and the calls in the main library are wrapped with USE_FEATURE guards. I would like to add a target that will build the library like the LIB target but also include the feature.c file in the sources and define USE_FEATURE. – Jason Brown Feb 12 '15 at 14:31
  • Does my answer below do what you want? – GrafikRobot Feb 19 '15 at 16:59
  • I haven't had a chance to test yet, but it does look like what I want. Once I verify that it works as I need, I will accept your answer. – Jason Brown Feb 20 '15 at 17:34

1 Answers1

2

Your key problem is that <define> is a free feature. And as such doesn't cause differentiation in the variant of what to build. To do what you want you need to create a new feature that describes what build option you are selecting (see feature documentation). For example:

import feature : feature ;
feature with-feat : no yes : optional propagated ;

You can then define whatever you want on your targets based on that feature's value. For example to define symbols or add sources:

lib LIB
    : [ glob *.c : feature.c ]
    : <link>static
      <use>/ProjectA//PUB
      <use>/ProjectB//PUB
      <with-feat>yes:<define>USE_FEATURE
      <with-feat>yes:<source>feature.c
    ;

Or you can use the conditional rule shorthand if you have many extra requirements to add:

lib LIB
    : [ glob *.c : feature.c ]
    : <link>static
      <use>/ProjectA//PUB
      <use>/ProjectB//PUB
      [ conditional <with-feat>yes :
        <define>USE_FEATURE
        <source>feature.c ]
    ;

To select a particular variation of that LIB from another target you specify the feature requirement in the target reference:

exe my-feat-exe : source.cpp LIB/<with-feat>yes ;
GrafikRobot
  • 3,020
  • 1
  • 20
  • 21