问题描述
我想在waf
中配置几个构建配置(意味着不同的C编译器).我设法那样做,但是对我来说似乎有点臭.
I want to have several build configurations (meaning different C compilers) configured in waf
. I managed to do it that way, but it looks a bit smelly to me.
当前如何操作:我制作了不同的环境,并在加载下一个编译器之前重置c_compiler
列表,最后将其重置为所有编译器.
How do I do it currently: I make different envs, and reset the c_compiler
list before loading the next compiler, and at the end I reset it to all compilers.
def configure(cnf):
_os = Utils.unversioned_sys_platform()
restore_c_compilers = c_compiler[_os]
# load gcc
c_compiler[_os] = ['gcc']
conf.setenv('gcc')
conf.load('compiler_c')
# load clang
conf.setenv('clang')
c_compiler[_os] = ['clang']
conf.load('compiler_c')
c_compiler[_os] = restore_c_compilers # reset compiler list
有没有一种更好的方法?
Is there a better way to do it?
SO上存在此问题(如何将多个编译器与waf(Python)),但没有合适的答案.
There is this question on SO (How to use multiple compilers with waf (Python)) but with no suitable answer.
推荐答案
好吧,在这种情况下使用waf的方法是变体"(参见waf书第7.2.2节).由于gcc通常是默认的编译器,因此我为每个其他编译器创建了一个变体,以及一组相应的命令和环境.实践中:
Well, the way to go with waf in this case is "variants" (Cf. the waf book §7.2.2). As gcc is usually the default compiler, I create a variant for each other compiler, and a corresponding set of commands and environments. In practise:
def options(opt):
opt.load('compiler_c')
def configure(conf):
# here we are in default variant/env
# we load the default compiler, probably gcc
conf.load('compiler_c')
# config for clang variant
conf.setenv('clang')
conf.env.CC = ['clang']
conf.load('compiler_c')
# config for icc variant
conf.setenv('icc')
conf.env.CC = ['icc']
conf.load('compiler_c')
# back to default config
conf.setenv('')
def build(bld):
bld.program(source = 'main.c', target = 'myexe')
# this create variants commands and build directories
from waflib.Build import BuildContext, \
CleanContext, InstallContext, UninstallContext
for variant in ['clang', 'icc']:
for context in [BuildContext, CleanContext, InstallContext, UninstallContext]:
name = context.__name__.replace('Context','').lower()
class tmp(context):
cmd = name + '_' + variant
variant = variant
获得的内容:额外的命令build_icc
,build_clang
,clean_icc
,clean_clang
,...每个变量的目录,即build/icc
和build/clang
,当然,您的exe将使用相应的编译器.
What you get: extra commands build_icc
, build_clang
, clean_icc
, clean_clang
, ... A directory for each variant, namely build/icc
and build/clang
and of course your exe build with the corresponding compiler.
要测试:
waf build -v # use gcc, or the default compiler
waf build_icc -v # use icc
waf build_clang -v # use clang
您将获得:
build/
├── c4che
│ ├── build.config.py
│ ├── _cache.py
│ ├── clang_cache.py
│ └── icc_cache.py
├── clang
│ ├── main.c.1.o
│ └── myexe
├── icc
│ ├── main.c.1.o
│ └── myexe
├── config.log
├── main.c.1.o
└── myexe
请注意,默认变体位于根构建目录中.其缓存文件为c4che/_cache.py
.每个变体都有一个目录和一个名为ater的缓存.
Notice the default variant is in the root build directory. Its cache file is c4che/_cache.py
. Each variant has a directory and a cache named ater its name.
这篇关于在WAF中指定不同的编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!