compiler_c.py 3.08 KB
Newer Older
Thomas Nagy's avatar
Thomas Nagy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/env python
# encoding: utf-8
# Matthias Jahn jahn dôt matthias ât freenet dôt de, 2007 (pmarat)

"""
Try to detect a C compiler from the list of supported compilers (gcc, msvc, etc)::

	def options(opt):
		opt.load('compiler_c')
	def configure(cnf):
		cnf.load('compiler_c')
	def build(bld):
		bld.program(source='main.c', target='app')

The compilers are associated to platforms in :py:attr:`waflib.Tools.compiler_c.c_compiler`. To register
a new C compiler named *cfoo* (assuming the tool ``waflib/extras/cfoo.py`` exists), use::

18 19 20
	from waflib.Tools.compiler_c import c_compiler
	c_compiler['win32'] = ['cfoo', 'msvc', 'gcc']

Thomas Nagy's avatar
Thomas Nagy committed
21 22 23 24 25 26 27 28 29 30 31 32
	def options(opt):
		opt.load('compiler_c')
	def configure(cnf):
		cnf.load('compiler_c')
	def build(bld):
		bld.program(source='main.c', target='app')

Not all compilers need to have a specific tool. For example, the clang compilers can be detected by the gcc tools when using::

	$ CC=clang waf configure
"""

33
import re
Thomas Nagy's avatar
Thomas Nagy committed
34
from waflib.Tools import ccroot
35
from waflib import Utils
Thomas Nagy's avatar
Thomas Nagy committed
36 37 38
from waflib.Logs import debug

c_compiler = {
39
'win32':  ['msvc', 'gcc', 'clang'],
swaldhoer's avatar
swaldhoer committed
40
'cygwin': ['gcc', 'clang'],
41 42 43
'darwin': ['clang', 'gcc'],
'aix':    ['xlc', 'gcc', 'clang'],
'linux':  ['gcc', 'clang', 'icc'],
Thomas Nagy's avatar
Thomas Nagy committed
44 45 46
'sunos':  ['suncc', 'gcc'],
'irix':   ['gcc', 'irixcc'],
'hpux':   ['gcc'],
Thomas Nagy's avatar
Thomas Nagy committed
47
'osf1V':  ['gcc'],
48 49
'gnu':    ['gcc', 'clang'],
'java':   ['gcc', 'msvc', 'clang', 'icc'],
50
'default':['clang', 'gcc'],
Thomas Nagy's avatar
Thomas Nagy committed
51 52
}
"""
ita1024's avatar
ita1024 committed
53
Dict mapping platform names to Waf tools finding specific C compilers::
Thomas Nagy's avatar
Thomas Nagy committed
54 55 56 57 58

	from waflib.Tools.compiler_c import c_compiler
	c_compiler['linux'] = ['gcc', 'icc', 'suncc']
"""

59 60 61 62 63
def default_compilers():
	build_platform = Utils.unversioned_sys_platform()
	possible_compiler_list = c_compiler.get(build_platform, c_compiler['default'])
	return ' '.join(possible_compiler_list)

Thomas Nagy's avatar
Thomas Nagy committed
64 65
def configure(conf):
	"""
ita1024's avatar
ita1024 committed
66 67 68
	Detects a suitable C compiler

	:raises: :py:class:`waflib.Errors.ConfigurationError` when no suitable compiler is found
Thomas Nagy's avatar
Thomas Nagy committed
69
	"""
ita1024's avatar
ita1024 committed
70 71 72 73
	try:
		test_for_compiler = conf.options.check_c_compiler or default_compilers()
	except AttributeError:
		conf.fatal("Add options(opt): opt.load('compiler_c')")
74 75

	for compiler in re.split('[ ,]+', test_for_compiler):
Thomas Nagy's avatar
Thomas Nagy committed
76
		conf.env.stash()
77
		conf.start_msg('Checking for %r (C compiler)' % compiler)
Thomas Nagy's avatar
Thomas Nagy committed
78 79 80 81 82
		try:
			conf.load(compiler)
		except conf.errors.ConfigurationError as e:
			conf.env.revert()
			conf.end_msg(False)
ita1024's avatar
ita1024 committed
83
			debug('compiler_c: %r', e)
Thomas Nagy's avatar
Thomas Nagy committed
84
		else:
ita1024's avatar
ita1024 committed
85
			if conf.env.CC:
Thomas Nagy's avatar
Thomas Nagy committed
86
				conf.end_msg(conf.env.get_flat('CC'))
ita1024's avatar
ita1024 committed
87
				conf.env.COMPILER_CC = compiler
88
				conf.env.commit()
Thomas Nagy's avatar
Thomas Nagy committed
89
				break
90
			conf.env.revert()
Thomas Nagy's avatar
Thomas Nagy committed
91 92
			conf.end_msg(False)
	else:
93
		conf.fatal('could not configure a C compiler!')
Thomas Nagy's avatar
Thomas Nagy committed
94 95 96

def options(opt):
	"""
ita1024's avatar
ita1024 committed
97
	This is how to provide compiler preferences on the command-line::
Thomas Nagy's avatar
Thomas Nagy committed
98 99 100

		$ waf configure --check-c-compiler=gcc
	"""
101
	test_for_compiler = default_compilers()
Thomas Nagy's avatar
Thomas Nagy committed
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,
Thomas Nagy's avatar
Thomas Nagy committed
106
		dest="check_c_compiler")
107

Thomas Nagy's avatar
Thomas Nagy committed
108 109 110
	for x in test_for_compiler.split():
		opt.load('%s' % x)