Boost logo

Boost-Build :

Subject: Re: [Boost-build] Target OS preprocessor macro
From: Vladimir Prus (ghost_at_[hidden])
Date: 2012-09-05 01:31:33


On 05.09.2012 01:15, mareq wrote:
> I would like to conditionally execute parts of my code, depending on the
> target OS. That means, that I need to define preprocessor macro (e.g.
> BOOST_BUILD_TARGET_OS_LINUX), based on the value of <target-os> feature.
>
> However, the only way how to do this, which is known to me by now is to
> generate header file, containing appropriate #define directive and include
> that header file into the sources. This is not very straightforward way, to
> say the least.
>
> Therefore, my question: What is the simplest way to add <define> property
> into targets' property-set, which value (i.e. name of the preprocessor macro
> being defined) is dependent on <target-os> property of the target in
> question (e.g. <target-os>linux -> <define>BOOST_BUILD_TARGET_OS_LINUX,
> <target-os>windows -> <define>BOOST_BUILD_TARGET_OS_WINDOWS, etc.).

Hi,

the simplest way is to have this in your project requirements:

        <target-os>linux:<define>BOOST_BUILD_TARGET_OS_LINUX
        <target-os>windows:<define>BOOST_BUILD_TARGET_OS_WINDOWS

for every OS you need to support. It is possible to make this generic using indirect requirements:

        import feature ;
        rule add-os-define ( properties * )
         {
                local os = [ feature.get target-os : $(properties) ] ;
                return <define>BOOST_BUILD_TARGET_OS_$(os:U) ;
        }

        exe main : main.cpp : <conditional>@add-os-define ;

Hope this helps,
Volodya


Boost-Build list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk