Setting compiler specific flags in scons

2019-08-08 05:06发布

问题:

When writing a scons script that will be used on different platform and compiler it's sometimes desired to add compiler specific flags. Fx in pseudo code

if using_gcc:
    env.Append( CCFLAGS=["-g"] )
elif using_msvc:
    env.Append( CCFLAGS=["/Zi"] )

At an earlier occation I just copied the appropriate(?) .py files under Tools and modified these and placed under site_tools, but that seems like a hackish solution.

Is there a better way to achieve this result? It would of course be nice if it could be added under site_scons to take global effect (without having to resort to hacking the tools files themselves).

Note: The reason I find it desirable to do this under site_scons is that it could be done a part of a common build system that is shared among multiple projects. Naturally this means that one have to share this build system among all participants in the project too.

回答1:

I utilize SCons to build on OSX, Winders, and Linux, with the same scripts, and usually handle platform dependent flags like this.

import platform
env = Environment()
env['SYSTEM'] = platform.system().lower()

if env['SYSTEM'] in ['linux', 'darwin']:
    env.Append( CCFLAGS=["-g"] )
if env['SYSTEM'] == 'windows':
    env.Append( CCFLAGS=["/Zi"] )

I add the system as an environment variable because it usually comes up several times, and it's handy to just have it available everywhere.

Note that this logic usually gets much more complicated when you mix in toolchains, build modes, and other fun options.

I try not to use site_scons, user environment variables, or any dot-files to modify the build environment. I find it much more useful to have the entire environment specification defined in one place, and guaranteed (as best as possible) to be the same for all users on all platforms.