3 # Matthias Jahn jahn dôt matthias ât freenet dôt de, 2007 (pmarat)
6 Try to detect a C compiler from the list of supported compilers (gcc, msvc, etc)::
11 cnf.load('compiler_c')
13 bld.program(source='main.c', target='app')
15 The compilers are associated to platforms in :py:attr:`waflib.Tools.compiler_c.c_compiler`. To register
16 a new C compiler named *cfoo* (assuming the tool ``waflib/extras/cfoo.py`` exists), use::
18 from waflib.Tools.compiler_c import c_compiler
19 c_compiler['win32'] = ['cfoo', 'msvc', 'gcc']
22 opt.load('compiler_c')
24 cnf.load('compiler_c')
26 bld.program(source='main.c', target='app')
28 Not all compilers need to have a specific tool. For example, the clang compilers can be detected by the gcc tools when using::
30 $ CC=clang waf configure
34 from waflib.Tools import ccroot
35 from waflib import Utils
36 from waflib.Logs import debug
39 'win32': ['msvc', 'gcc', 'clang'],
40 'cygwin': ['gcc', 'clang'],
41 'darwin': ['clang', 'gcc'],
42 'aix': ['xlc', 'gcc', 'clang'],
43 'linux': ['gcc', 'clang', 'icc'],
44 'sunos': ['suncc', 'gcc'],
45 'irix': ['gcc', 'irixcc'],
48 'gnu': ['gcc', 'clang'],
49 'java': ['gcc', 'msvc', 'clang', 'icc'],
50 'gnukfreebsd': ['gcc', 'clang'],
51 'default': ['clang', 'gcc'],
54 Dict mapping platform names to Waf tools finding specific C compilers::
56 from waflib.Tools.compiler_c import c_compiler
57 c_compiler['linux'] = ['gcc', 'icc', 'suncc']
60 def default_compilers():
61 build_platform = Utils.unversioned_sys_platform()
62 possible_compiler_list = c_compiler.get(build_platform, c_compiler['default'])
63 return ' '.join(possible_compiler_list)
67 Detects a suitable C compiler
69 :raises: :py:class:`waflib.Errors.ConfigurationError` when no suitable compiler is found
72 test_for_compiler = conf.options.check_c_compiler or default_compilers()
73 except AttributeError:
74 conf.fatal("Add options(opt): opt.load('compiler_c')")
76 for compiler in re.split('[ ,]+', test_for_compiler):
78 conf.start_msg('Checking for %r (C compiler)' % compiler)
81 except conf.errors.ConfigurationError as e:
84 debug('compiler_c: %r', e)
87 conf.end_msg(conf.env.get_flat('CC'))
88 conf.env.COMPILER_CC = compiler
94 conf.fatal('could not configure a C compiler!')
98 This is how to provide compiler preferences on the command-line::
100 $ waf configure --check-c-compiler=gcc
102 test_for_compiler = default_compilers()
103 opt.load_special_tools('c_*.py', ban=['c_dumbpreproc.py'])
104 cc_compiler_opts = opt.add_option_group('Configuration options')
105 cc_compiler_opts.add_option('--check-c-compiler', default=None,
106 help='list of C compilers to try [%s]' % test_for_compiler,
107 dest="check_c_compiler")
109 for x in test_for_compiler.split():