How to pass arbitrary compiler CFLAGS with Scons from the command line without custom code?

送分小仙女□ 提交于 2019-12-11 15:45:48

问题


Is there a way to write something like:

scons CFLAGS='-fsanitize=address -fsanitize=thread'

which would just work with a minimal script:

env = Environment()
env.Program(target='main.out', source=['main.c'])

without changing that script?

I know how to do it by modifying the script with AddOption + env.Append(CCFLAGS but I'm wondering it it is possible without changing the code to explicitly support it.


回答1:


This is not possible by design (without explicitly changing the build scripts). From the answer to #1 of the most-frequently-asked questions in our FAQ:

SCons does not automatically propagate the external environment used to execute 'scons' to the commands used to build target files. This is so that builds will be guaranteed repeatable regardless of the environment variables set at the time scons is invoked. This also means that if the compiler or other commands that you want to use to build your target files are not in standard system locations, SCons will not find them unless you explicitly set the PATH to include those locations.




回答2:


I ended up going with:

env = Environment()
env.Append(CCFLAGS='-Werror')
env.Append(CCFLAGS=ARGUMENTS.get('CCFLAGS', ''))
env.Program(target='main.out', source=['main.c'])

which can be used as:

scons CCFLAGS='-Wall -pedantic'

and will compile as:

gcc -o main.o -c -Werror -Wall -pedantic main.c

You likely want to keep the env.Append(CCFLAGS=ARGUMENTS.get('CCFLAGS', '')) line as the very last change made to CCFLAGS, since this will allow overriding the defaults on the command line: GCC tends to just use the last value seen as the actual one.

TODO: how to make it work with Variables? This would be nicer since we can get more error checking and the help message generation:

variables = Variables(None, ARGUMENTS)
variables.Add('CCFLAGS', 'my help', '')
env = Environment(variables)
env.Append(CCFLAGS='$CCFLAGS')
env.Append(CCFLAGS=['-Werror'])
env.Program(
    source=['main.c'],
    target='main.out',
)
Help(variables.GenerateHelpText(env))

but this fails due to bad quoting:

gcc -o main.o -c "-Wall -pedantic" -Werror main.c


来源:https://stackoverflow.com/questions/51929011/how-to-pass-arbitrary-compiler-cflags-with-scons-from-the-command-line-without-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!