我的项目使用SCons来管理构建过程.我想支持多个编译器,所以我决定使用,AddOption这样用户就可以在命令行中指定使用哪个编译器(默认情况下是当前编译器所用的编译器).
AddOption('--compiler', dest = 'compiler', type = 'string', action = 'store', default = DefaultEnvironment()['CXX'], help = 'Name of the compiler to use.')
Run Code Online (Sandbox Code Playgroud)
我希望能够为各种编译器提供内置的编译器设置(包括特定编译器的最大警告级别等).这是我目前首次尝试解决方案的方式:
if is_compiler('g++'):
from build_scripts.gcc.std import cxx_std
from build_scripts.gcc.warnings import warnings, warnings_debug, warnings_optimized
from build_scripts.gcc.optimizations import optimizations, preprocessor_optimizations, linker_optimizations
elif is_compiler('clang++'):
from build_scripts.clang.std import cxx_std
from build_scripts.clang.warnings import warnings, warnings_debug, warnings_optimized
from build_scripts.clang.optimizations import optimizations, preprocessor_optimizations, linker_optimizations
Run Code Online (Sandbox Code Playgroud)
但是,我不确定该is_compiler()功能是什么样的.我的第一个想法是直接比较编译器名称(例如'clang ++')与用户传入的内容.但是,当我尝试使用时,这立即失败了scons --compiler=~/data/llvm-3.1-obj/Release+Asserts/bin/clang++.
所以我觉得我会变得更聪明并且使用这个功能
cxx = GetOption('compiler')
def is_compiler (compiler):
return cxx[-len(compiler):] == compiler
Run Code Online (Sandbox Code Playgroud)
这只会查看编译器字符串的结尾,以便忽略目录.不幸的是,'clang ++'以'g ++'结尾,所以我的编译器被认为是g ++而不是clang ++.
我的下一个想法是进行向后搜索并查找第一次出现的路径分隔符('\'或'/'),但后来我意识到这对于拥有多个编译器版本的人来说不起作用.用'g ++ - 4.7'编译的人不会注册为g ++.
那么,是否有一些简单的方法来确定请求了哪个编译器?
目前,由于c ++ 11的支持,只支持g ++和clang ++(并且只支持它们最近发布的版本),所以只适用于这两种的解决方案现在已经足够好了.但是,我的最终目标是至少支持g ++,clang ++,icc和msvc ++(一旦它们支持所需的c ++ 11特性),所以更喜欢更通用的解决方案.