3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
7 # Matthias Jahn jahn dôt matthias ât freenet dôt de, 2007 (pmarat)
10 Try to detect a C compiler from the list of supported compilers (gcc, msvc, etc)::
13 opt.load('compiler_c')
15 cnf.load('compiler_c')
17 bld.program(source='main.c', target='app')
19 The compilers are associated to platforms in :py:attr:`waflib.Tools.compiler_c.c_compiler`. To register
20 a new C compiler named *cfoo* (assuming the tool ``waflib/extras/cfoo.py`` exists), use::
22 from waflib.Tools.compiler_c import c_compiler
23 c_compiler['win32'] = ['cfoo', 'msvc', 'gcc']
26 opt.load('compiler_c')
28 cnf.load('compiler_c')
30 bld.program(source='main.c', target='app')
32 Not all compilers need to have a specific tool. For example, the clang compilers can be detected by the gcc tools when using::
34 $ CC=clang waf configure
38 from waflib.Tools import ccroot
39 from waflib import Utils
40 from waflib.Logs import debug
43 'win32': ['msvc', 'gcc', 'clang'],
45 'darwin': ['clang', 'gcc'],
46 'aix': ['xlc', 'gcc', 'clang'],
47 'linux': ['gcc', 'clang', 'icc'],
48 'sunos': ['suncc', 'gcc'],
49 'irix': ['gcc', 'irixcc'],
52 'gnu': ['gcc', 'clang'],
53 'java': ['gcc', 'msvc', 'clang', 'icc'],
54 'default':['clang', 'gcc'],
57 Dict mapping platform names to Waf tools finding specific C compilers::
59 from waflib.Tools.compiler_c import c_compiler
60 c_compiler['linux'] = ['gcc', 'icc', 'suncc']
63 def default_compilers():
64 build_platform = Utils.unversioned_sys_platform()
65 possible_compiler_list = c_compiler.get(build_platform, c_compiler['default'])
66 return ' '.join(possible_compiler_list)
70 Detects a suitable C compiler
72 :raises: :py:class:`waflib.Errors.ConfigurationError` when no suitable compiler is found
75 test_for_compiler = conf.options.check_c_compiler or default_compilers()
76 except AttributeError:
77 conf.fatal("Add options(opt): opt.load('compiler_c')")
79 for compiler in re.split('[ ,]+', test_for_compiler):
81 conf.start_msg('Checking for %r (C compiler)' % compiler)
84 except conf.errors.ConfigurationError ,e:
87 debug('compiler_c: %r', e)
90 conf.end_msg(conf.env.get_flat('CC'))
91 conf.env.COMPILER_CC = compiler
97 conf.fatal('could not configure a C compiler!')
101 This is how to provide compiler preferences on the command-line::
103 $ waf configure --check-c-compiler=gcc
105 test_for_compiler = default_compilers()
106 opt.load_special_tools('c_*.py', ban=['c_dumbpreproc.py'])
107 cc_compiler_opts = opt.add_option_group('Configuration options')
108 cc_compiler_opts.add_option('--check-c-compiler', default=None,
109 help='list of C compilers to try [%s]' % test_for_compiler,
110 dest="check_c_compiler")
112 for x in test_for_compiler.split():