third_party/waf: upgrade to waf 2.0.8
[samba.git] / third_party / waf / waflib / Tools / compiler_c.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 # Matthias Jahn jahn dôt matthias ât freenet dôt de, 2007 (pmarat)
4
5 """
6 Try to detect a C compiler from the list of supported compilers (gcc, msvc, etc)::
7
8         def options(opt):
9                 opt.load('compiler_c')
10         def configure(cnf):
11                 cnf.load('compiler_c')
12         def build(bld):
13                 bld.program(source='main.c', target='app')
14
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::
17
18         from waflib.Tools.compiler_c import c_compiler
19         c_compiler['win32'] = ['cfoo', 'msvc', 'gcc']
20
21         def options(opt):
22                 opt.load('compiler_c')
23         def configure(cnf):
24                 cnf.load('compiler_c')
25         def build(bld):
26                 bld.program(source='main.c', target='app')
27
28 Not all compilers need to have a specific tool. For example, the clang compilers can be detected by the gcc tools when using::
29
30         $ CC=clang waf configure
31 """
32
33 import re
34 from waflib.Tools import ccroot
35 from waflib import Utils
36 from waflib.Logs import debug
37
38 c_compiler = {
39 'win32':  ['msvc', 'gcc', 'clang'],
40 'cygwin': ['gcc'],
41 'darwin': ['clang', 'gcc'],
42 'aix':    ['xlc', 'gcc', 'clang'],
43 'linux':  ['gcc', 'clang', 'icc'],
44 'sunos':  ['suncc', 'gcc'],
45 'irix':   ['gcc', 'irixcc'],
46 'hpux':   ['gcc'],
47 'osf1V':  ['gcc'],
48 'gnu':    ['gcc', 'clang'],
49 'java':   ['gcc', 'msvc', 'clang', 'icc'],
50 'default':['clang', 'gcc'],
51 }
52 """
53 Dict mapping platform names to Waf tools finding specific C compilers::
54
55         from waflib.Tools.compiler_c import c_compiler
56         c_compiler['linux'] = ['gcc', 'icc', 'suncc']
57 """
58
59 def default_compilers():
60         build_platform = Utils.unversioned_sys_platform()
61         possible_compiler_list = c_compiler.get(build_platform, c_compiler['default'])
62         return ' '.join(possible_compiler_list)
63
64 def configure(conf):
65         """
66         Detects a suitable C compiler
67
68         :raises: :py:class:`waflib.Errors.ConfigurationError` when no suitable compiler is found
69         """
70         try:
71                 test_for_compiler = conf.options.check_c_compiler or default_compilers()
72         except AttributeError:
73                 conf.fatal("Add options(opt): opt.load('compiler_c')")
74
75         for compiler in re.split('[ ,]+', test_for_compiler):
76                 conf.env.stash()
77                 conf.start_msg('Checking for %r (C compiler)' % compiler)
78                 try:
79                         conf.load(compiler)
80                 except conf.errors.ConfigurationError as e:
81                         conf.env.revert()
82                         conf.end_msg(False)
83                         debug('compiler_c: %r', e)
84                 else:
85                         if conf.env.CC:
86                                 conf.end_msg(conf.env.get_flat('CC'))
87                                 conf.env.COMPILER_CC = compiler
88                                 conf.env.commit()
89                                 break
90                         conf.env.revert()
91                         conf.end_msg(False)
92         else:
93                 conf.fatal('could not configure a C compiler!')
94
95 def options(opt):
96         """
97         This is how to provide compiler preferences on the command-line::
98
99                 $ waf configure --check-c-compiler=gcc
100         """
101         test_for_compiler = default_compilers()
102         opt.load_special_tools('c_*.py', ban=['c_dumbpreproc.py'])
103         cc_compiler_opts = opt.add_option_group('Configuration options')
104         cc_compiler_opts.add_option('--check-c-compiler', default=None,
105                 help='list of C compilers to try [%s]' % test_for_compiler,
106                 dest="check_c_compiler")
107
108         for x in test_for_compiler.split():
109                 opt.load('%s' % x)
110