toolchain.configure 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. # PGO
  6. # ==============================================================
  7. option(env='MOZ_PGO', help='Build with profile guided optimizations')
  8. set_config('MOZ_PGO', depends('MOZ_PGO')(lambda x: bool(x)))
  9. add_old_configure_assignment('MOZ_PGO', depends('MOZ_PGO')(lambda x: bool(x)))
  10. # yasm detection
  11. # ==============================================================
  12. yasm = check_prog('YASM', ['yasm'], allow_missing=True)
  13. @depends_if(yasm)
  14. @checking('yasm version')
  15. def yasm_version(yasm):
  16. version = check_cmd_output(
  17. yasm, '--version',
  18. onerror=lambda: die('Failed to get yasm version.')
  19. ).splitlines()[0].split()[1]
  20. return Version(version)
  21. # Until we move all the yasm consumers out of old-configure.
  22. # bug 1257904
  23. add_old_configure_assignment('_YASM_MAJOR_VERSION',
  24. delayed_getattr(yasm_version, 'major'))
  25. add_old_configure_assignment('_YASM_MINOR_VERSION',
  26. delayed_getattr(yasm_version, 'minor'))
  27. @depends(yasm, target)
  28. def yasm_asflags(yasm, target):
  29. if yasm:
  30. asflags = {
  31. ('OSX', 'x86'): '-f macho32',
  32. ('OSX', 'x86_64'): '-f macho64',
  33. ('WINNT', 'x86'): '-f win32',
  34. ('WINNT', 'x86_64'): '-f x64',
  35. }.get((target.os, target.cpu), None)
  36. if asflags is None:
  37. # We're assuming every x86 platform we support that's
  38. # not Windows or Mac is ELF.
  39. if target.cpu == 'x86':
  40. asflags = '-f elf32'
  41. elif target.cpu == 'x86_64':
  42. asflags = '-f elf64'
  43. if asflags:
  44. asflags += ' -rnasm -pnasm'
  45. return asflags
  46. set_config('YASM_ASFLAGS', yasm_asflags)
  47. @depends(yasm_asflags)
  48. def have_yasm(value):
  49. if value:
  50. return True
  51. set_config('HAVE_YASM', have_yasm)
  52. # Until the YASM variable is not necessary in old-configure.
  53. add_old_configure_assignment('YASM', have_yasm)
  54. # Android NDK
  55. # ==============================================================
  56. @depends('--disable-compile-environment', build_project, gonkdir, '--help')
  57. def compiling_android(compile_env, build_project, gonkdir, _):
  58. return compile_env and (gonkdir or build_project in ('mobile/android', 'js'))
  59. include('android-ndk.configure', when=compiling_android)
  60. # MacOS deployment target version
  61. # ==============================================================
  62. # This needs to happen before any compilation test is done.
  63. option('--enable-macos-target', env='MACOSX_DEPLOYMENT_TARGET', nargs=1,
  64. default='10.7', help='Set the minimum MacOS version needed at runtime')
  65. @depends('--enable-macos-target', target)
  66. @imports(_from='os', _import='environ')
  67. def macos_target(value, target):
  68. if value and target.os == 'OSX':
  69. # Ensure every compiler process we spawn uses this value.
  70. environ['MACOSX_DEPLOYMENT_TARGET'] = value[0]
  71. return value[0]
  72. if value and value.origin != 'default':
  73. die('--enable-macos-target cannot be used when targeting %s',
  74. target.os)
  75. set_config('MACOSX_DEPLOYMENT_TARGET', macos_target)
  76. add_old_configure_assignment('MACOSX_DEPLOYMENT_TARGET', macos_target)
  77. # Compiler wrappers
  78. # ==============================================================
  79. # Normally, we'd use js_option and automatically have those variables
  80. # propagated to js/src, but things are complicated by possible additional
  81. # wrappers in CC/CXX, and by other subconfigures that do not handle those
  82. # options and do need CC/CXX altered.
  83. option('--with-compiler-wrapper', env='COMPILER_WRAPPER', nargs=1,
  84. help='Enable compiling with wrappers such as distcc and ccache')
  85. option('--with-ccache', env='CCACHE', nargs='?',
  86. help='Enable compiling with ccache')
  87. @depends_if('--with-ccache')
  88. def ccache(value):
  89. if len(value):
  90. return value
  91. # If --with-ccache was given without an explicit value, we default to
  92. # 'ccache'.
  93. return 'ccache'
  94. ccache = check_prog('CCACHE', progs=(), input=ccache)
  95. @depends_if(ccache)
  96. def using_ccache(ccache):
  97. return True
  98. set_config('MOZ_USING_CCACHE', using_ccache)
  99. @depends('--with-compiler-wrapper', ccache)
  100. @imports(_from='mozbuild.shellutil', _import='split', _as='shell_split')
  101. def compiler_wrapper(wrapper, ccache):
  102. if wrapper:
  103. raw_wrapper = wrapper[0]
  104. wrapper = shell_split(raw_wrapper)
  105. wrapper_program = find_program(wrapper[0])
  106. if not wrapper_program:
  107. die('Cannot find `%s` from the given compiler wrapper `%s`',
  108. wrapper[0], raw_wrapper)
  109. wrapper[0] = wrapper_program
  110. if ccache:
  111. if wrapper:
  112. return tuple([ccache] + wrapper)
  113. else:
  114. return (ccache,)
  115. elif wrapper:
  116. return tuple(wrapper)
  117. add_old_configure_assignment('COMPILER_WRAPPER', compiler_wrapper)
  118. @depends_if(compiler_wrapper)
  119. def using_compiler_wrapper(compiler_wrapper):
  120. return True
  121. set_config('MOZ_USING_COMPILER_WRAPPER', using_compiler_wrapper)
  122. # Cross-compilation related things.
  123. # ==============================================================
  124. js_option('--with-toolchain-prefix', env='TOOLCHAIN_PREFIX', nargs=1,
  125. help='Prefix for the target toolchain')
  126. @depends('--with-toolchain-prefix', target, host, cross_compiling)
  127. def toolchain_prefix(value, target, host, cross_compiling):
  128. if value:
  129. return tuple(value)
  130. if cross_compiling:
  131. return ('%s-' % target.toolchain, '%s-' % target.alias)
  132. @depends(toolchain_prefix, target)
  133. def first_toolchain_prefix(toolchain_prefix, target):
  134. # Pass TOOLCHAIN_PREFIX down to the build system if it was given from the
  135. # command line/environment (in which case there's only one value in the tuple),
  136. # or when cross-compiling for Android.
  137. if toolchain_prefix and (target.os == 'Android' or len(toolchain_prefix) == 1):
  138. return toolchain_prefix[0]
  139. set_config('TOOLCHAIN_PREFIX', first_toolchain_prefix)
  140. add_old_configure_assignment('TOOLCHAIN_PREFIX', first_toolchain_prefix)
  141. # Compilers
  142. # ==============================================================
  143. include('compilers-util.configure')
  144. def try_preprocess(compiler, language, source):
  145. return try_invoke_compiler(compiler, language, source, ['-E'])
  146. @imports(_from='mozbuild.configure.constants', _import='CompilerType')
  147. @imports(_from='mozbuild.configure.constants',
  148. _import='CPU_preprocessor_checks')
  149. @imports(_from='mozbuild.configure.constants',
  150. _import='kernel_preprocessor_checks')
  151. @imports(_from='textwrap', _import='dedent')
  152. def get_compiler_info(compiler, language):
  153. '''Returns information about the given `compiler` (command line in the
  154. form of a list or tuple), in the given `language`.
  155. The returned information includes:
  156. - the compiler type (msvc, clang-cl, clang or gcc)
  157. - the compiler version
  158. - the compiler supported language
  159. - the compiler supported language version
  160. '''
  161. # Note: MSVC doesn't expose __STDC_VERSION__. It does expose __STDC__,
  162. # but only when given the -Za option, which disables compiler
  163. # extensions.
  164. # Note: We'd normally do a version check for clang, but versions of clang
  165. # in Xcode have a completely different versioning scheme despite exposing
  166. # the version with the same defines.
  167. # So instead, we make things such that the version is missing when the
  168. # clang used is below the minimum supported version (currently clang 3.6).
  169. # We then only include the version information when the C++ compiler
  170. # matches the feature check, so that an unsupported version of clang would
  171. # have no version number.
  172. check = dedent('''\
  173. #if defined(_MSC_VER)
  174. #if defined(__clang__)
  175. %COMPILER "clang-cl"
  176. %VERSION _MSC_FULL_VER
  177. #else
  178. %COMPILER "msvc"
  179. %VERSION _MSC_FULL_VER
  180. #endif
  181. #elif defined(__clang__)
  182. %COMPILER "clang"
  183. # if !__cplusplus || __has_feature(cxx_alignof)
  184. %VERSION __clang_major__.__clang_minor__.__clang_patchlevel__
  185. # endif
  186. #elif defined(__GNUC__)
  187. %COMPILER "gcc"
  188. %VERSION __GNUC__.__GNUC_MINOR__.__GNUC_PATCHLEVEL__
  189. #endif
  190. #if __cplusplus
  191. %cplusplus __cplusplus
  192. #elif __STDC_VERSION__
  193. %STDC_VERSION __STDC_VERSION__
  194. #elif __STDC__
  195. %STDC_VERSION 198900L
  196. #endif
  197. ''')
  198. # While we're doing some preprocessing, we might as well do some more
  199. # preprocessor-based tests at the same time, to check the toolchain
  200. # matches what we want.
  201. for name, preprocessor_checks in (
  202. ('CPU', CPU_preprocessor_checks),
  203. ('KERNEL', kernel_preprocessor_checks),
  204. ):
  205. for n, (value, condition) in enumerate(preprocessor_checks.iteritems()):
  206. check += dedent('''\
  207. #%(if)s %(condition)s
  208. %%%(name)s "%(value)s"
  209. ''' % {
  210. 'if': 'elif' if n else 'if',
  211. 'condition': condition,
  212. 'name': name,
  213. 'value': value,
  214. })
  215. check += '#endif\n'
  216. # Also check for endianness. The advantage of living in modern times is
  217. # that all the modern compilers we support now have __BYTE_ORDER__ defined
  218. # by the preprocessor, except MSVC, which only supports little endian.
  219. check += dedent('''\
  220. #if _MSC_VER || __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  221. %ENDIANNESS "little"
  222. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  223. %ENDIANNESS "big"
  224. #endif
  225. ''')
  226. result = try_preprocess(compiler, language, check)
  227. if not result:
  228. raise FatalCheckError(
  229. 'Unknown compiler or compiler not supported.')
  230. # Metadata emitted by preprocessors such as GCC with LANG=ja_JP.utf-8 may
  231. # have non-ASCII characters. Treat the output as bytearray.
  232. data = {}
  233. for line in result.splitlines():
  234. if line.startswith(b'%'):
  235. k, _, v = line.partition(' ')
  236. k = k.lstrip('%')
  237. data[k] = v.replace(' ', '').lstrip('"').rstrip('"')
  238. log.debug('%s = %s', k, data[k])
  239. try:
  240. type = CompilerType(data['COMPILER'])
  241. except:
  242. raise FatalCheckError(
  243. 'Unknown compiler or compiler not supported.')
  244. cplusplus = int(data.get('cplusplus', '0L').rstrip('L'))
  245. stdc_version = int(data.get('STDC_VERSION', '0L').rstrip('L'))
  246. version = data.get('VERSION')
  247. if version and type in ('msvc', 'clang-cl'):
  248. msc_ver = version
  249. version = msc_ver[0:2]
  250. if len(msc_ver) > 2:
  251. version += '.' + msc_ver[2:4]
  252. if len(msc_ver) > 4:
  253. version += '.' + msc_ver[4:]
  254. if version:
  255. version = Version(version)
  256. return namespace(
  257. type=type,
  258. version=version,
  259. cpu=data.get('CPU'),
  260. kernel=data.get('KERNEL'),
  261. endianness=data.get('ENDIANNESS'),
  262. language='C++' if cplusplus else 'C',
  263. language_version=cplusplus if cplusplus else stdc_version,
  264. )
  265. @imports(_from='mozbuild.shellutil', _import='quote')
  266. def check_compiler(compiler, language, target):
  267. info = get_compiler_info(compiler, language)
  268. flags = []
  269. def append_flag(flag):
  270. if flag not in flags:
  271. if info.type == 'clang-cl':
  272. flags.append('-Xclang')
  273. flags.append(flag)
  274. # Check language standards
  275. # --------------------------------------------------------------------
  276. if language != info.language:
  277. raise FatalCheckError(
  278. '`%s` is not a %s compiler.' % (quote(*compiler), language))
  279. # Note: We do a strict version check because there sometimes are backwards
  280. # incompatible changes in the standard, and not all code that compiles as
  281. # C99 compiles as e.g. C11 (as of writing, this is true of libnestegg, for
  282. # example)
  283. if info.language == 'C' and info.language_version != 199901:
  284. if info.type in ('clang-cl', 'clang', 'gcc'):
  285. append_flag('-std=gnu99')
  286. # Note: MSVC, while supporting C++11, still reports 199711L for __cplusplus.
  287. # Note: this is a strict version check because we used to always add
  288. # -std=gnu++11.
  289. if info.language == 'C++':
  290. if info.type in ('clang', 'gcc') and info.language_version != 201103:
  291. append_flag('-std=gnu++11')
  292. # MSVC 2015 headers include C++14 features, but don't guard them
  293. # with appropriate checks.
  294. if info.type == 'clang-cl' and info.language_version != 201402:
  295. append_flag('-std=c++14')
  296. # We force clang-cl to emulate Visual C++ 2015 Update 3 with fallback to
  297. # cl.exe.
  298. if info.type == 'clang-cl' and info.version != '19.00.24213':
  299. # Those flags are direct clang-cl flags that don't need -Xclang, add
  300. # them directly.
  301. flags.append('-fms-compatibility-version=19.00.24213')
  302. flags.append('-fallback')
  303. # Check compiler target
  304. # --------------------------------------------------------------------
  305. if not info.cpu or info.cpu != target.cpu:
  306. if info.type == 'clang':
  307. append_flag('--target=%s' % target.toolchain)
  308. elif info.type == 'gcc':
  309. same_arch_different_bits = (
  310. ('x86', 'x86_64'),
  311. ('ppc', 'ppc64'),
  312. ('sparc', 'sparc64'),
  313. )
  314. if (target.cpu, info.cpu) in same_arch_different_bits:
  315. append_flag('-m32')
  316. elif (info.cpu, target.cpu) in same_arch_different_bits:
  317. append_flag('-m64')
  318. if not info.kernel or info.kernel != target.kernel:
  319. if info.type == 'clang':
  320. append_flag('--target=%s' % target.toolchain)
  321. if not info.endianness or info.endianness != target.endianness:
  322. if info.type == 'clang':
  323. append_flag('--target=%s' % target.toolchain)
  324. return namespace(
  325. type=info.type,
  326. version=info.version,
  327. target_cpu=info.cpu,
  328. target_kernel=info.kernel,
  329. target_endianness=info.endianness,
  330. flags=flags,
  331. )
  332. @imports(_from='collections', _import='defaultdict')
  333. @imports(_from='__builtin__', _import='sorted')
  334. def get_vc_paths(base):
  335. vc = defaultdict(lambda: defaultdict(dict))
  336. subkey = r'Microsoft\VisualStudio\VC\*\*\*\Compiler'
  337. for v, h, t, p in get_registry_values(base + '\\' + subkey):
  338. vc[v][h][t] = p
  339. if not vc:
  340. return
  341. version, data = sorted(vc.iteritems(), key=lambda x: Version(x[0]))[-1]
  342. return data
  343. @depends(host)
  344. @imports('platform')
  345. def vc_compiler_path(host):
  346. if host.kernel != 'WINNT':
  347. return
  348. vc_host = {
  349. 'x86': 'x86',
  350. 'AMD64': 'x64',
  351. }.get(platform.machine())
  352. if vc_host is None:
  353. return
  354. vc_target = {
  355. 'x86': 'x86',
  356. 'x86_64': 'x64',
  357. 'arm': 'arm',
  358. }.get(host.cpu)
  359. if vc_target is None:
  360. return
  361. base_key = r'HKEY_LOCAL_MACHINE\SOFTWARE'
  362. data = get_vc_paths(base_key)
  363. if not data:
  364. data = get_vc_paths(base_key + r'\Wow6432Node')
  365. if not data:
  366. return
  367. path = data.get(vc_host, {}).get(vc_target)
  368. if not path and vc_host == 'x64':
  369. vc_host = 'x86'
  370. path = data.get(vc_host, {}).get(vc_target)
  371. if not path:
  372. return
  373. path = os.path.dirname(path)
  374. if vc_host != vc_target:
  375. other_path = data.get(vc_host, {}).get(vc_host)
  376. if other_path:
  377. return (path, os.path.dirname(other_path))
  378. return (path,)
  379. @depends(vc_compiler_path)
  380. @imports('os')
  381. def toolchain_search_path(vc_compiler_path):
  382. if vc_compiler_path:
  383. result = [os.environ.get('PATH')]
  384. result.extend(vc_compiler_path)
  385. # We're going to alter PATH for good in windows.configure, but we also
  386. # need to do it for the valid_compiler() check below.
  387. os.environ['PATH'] = os.pathsep.join(result)
  388. return result
  389. @template
  390. def default_c_compilers(host_or_target):
  391. '''Template defining the set of default C compilers for the host and
  392. target platforms.
  393. `host_or_target` is either `host` or `target` (the @depends functions
  394. from init.configure.
  395. '''
  396. assert host_or_target in (host, target)
  397. @depends(host_or_target, target, toolchain_prefix)
  398. def default_c_compilers(host_or_target, target, toolchain_prefix):
  399. gcc = ('gcc',)
  400. if toolchain_prefix and host_or_target is target:
  401. gcc = tuple('%sgcc' % p for p in toolchain_prefix) + gcc
  402. if host_or_target.kernel == 'WINNT':
  403. return ('cl', 'clang-cl') + gcc + ('clang',)
  404. if host_or_target.kernel == 'Darwin':
  405. return ('clang',)
  406. return gcc + ('clang',)
  407. return default_c_compilers
  408. @template
  409. def default_cxx_compilers(c_compiler):
  410. '''Template defining the set of default C++ compilers for the host and
  411. target platforms.
  412. `c_compiler` is the @depends function returning a Compiler instance for
  413. the desired platform.
  414. Because the build system expects the C and C++ compilers to be from the
  415. same compiler suite, we derive the default C++ compilers from the C
  416. compiler that was found if none was provided.
  417. '''
  418. @depends(c_compiler)
  419. def default_cxx_compilers(c_compiler):
  420. dir = os.path.dirname(c_compiler.compiler)
  421. file = os.path.basename(c_compiler.compiler)
  422. if c_compiler.type == 'gcc':
  423. return (os.path.join(dir, file.replace('gcc', 'g++')),)
  424. if c_compiler.type == 'clang':
  425. return (os.path.join(dir, file.replace('clang', 'clang++')),)
  426. return (c_compiler.compiler,)
  427. return default_cxx_compilers
  428. @template
  429. def compiler(language, host_or_target, c_compiler=None, other_compiler=None,
  430. other_c_compiler=None):
  431. '''Template handling the generic base checks for the compiler for the
  432. given `language` on the given platform (`host_or_target`).
  433. `host_or_target` is either `host` or `target` (the @depends functions
  434. from init.configure.
  435. When the language is 'C++', `c_compiler` is the result of the `compiler`
  436. template for the language 'C' for the same `host_or_target`.
  437. When `host_or_target` is `host`, `other_compiler` is the result of the
  438. `compiler` template for the same `language` for `target`.
  439. When `host_or_target` is `host` and the language is 'C++',
  440. `other_c_compiler` is the result of the `compiler` template for the
  441. language 'C' for `target`.
  442. '''
  443. assert host_or_target in (host, target)
  444. assert language in ('C', 'C++')
  445. assert language == 'C' or c_compiler
  446. assert host_or_target == target or other_compiler
  447. assert language == 'C' or host_or_target == target or other_c_compiler
  448. host_or_target_str = {
  449. host: 'host',
  450. target: 'target',
  451. }[host_or_target]
  452. var = {
  453. ('C', target): 'CC',
  454. ('C++', target): 'CXX',
  455. ('C', host): 'HOST_CC',
  456. ('C++', host): 'HOST_CXX',
  457. }[language, host_or_target]
  458. default_compilers = {
  459. 'C': lambda: default_c_compilers(host_or_target),
  460. 'C++': lambda: default_cxx_compilers(c_compiler),
  461. }[language]()
  462. what='the %s %s compiler' % (host_or_target_str, language)
  463. option(env=var, nargs=1, help='Path to %s' % what)
  464. # Handle the compiler given by the user through one of the CC/CXX/HOST_CC/
  465. # HOST_CXX variables.
  466. @depends_if(var)
  467. @imports(_from='itertools', _import='takewhile')
  468. @imports(_from='mozbuild.shellutil', _import='split', _as='shell_split')
  469. def provided_compiler(cmd):
  470. # Historically, the compiler variables have contained more than the
  471. # path to the compiler itself. So for backwards compatibility, try to
  472. # find what is what in there, assuming the first dash-prefixed item is
  473. # a compiler option, the item before that is the compiler, and anything
  474. # before that is a compiler wrapper.
  475. cmd = shell_split(cmd[0])
  476. without_flags = list(takewhile(lambda x: not x.startswith('-'), cmd))
  477. return namespace(
  478. wrapper=without_flags[:-1],
  479. compiler=without_flags[-1],
  480. flags=cmd[len(without_flags):],
  481. )
  482. # Derive the host compiler from the corresponding target compiler when no
  483. # explicit compiler was given and we're not cross compiling. For the C++
  484. # compiler, though, prefer to derive from the host C compiler when it
  485. # doesn't match the target C compiler.
  486. # As a special case, since clang supports all kinds of targets in the same
  487. # executable, when cross compiling with clang, default to the same compiler
  488. # as the target compiler, resetting flags.
  489. if host_or_target == host:
  490. args = (c_compiler, other_c_compiler) if other_c_compiler else ()
  491. @depends(provided_compiler, other_compiler, cross_compiling, *args)
  492. def provided_compiler(value, other_compiler, cross_compiling, *args):
  493. if value:
  494. return value
  495. c_compiler, other_c_compiler = args if args else (None, None)
  496. if not cross_compiling and c_compiler == other_c_compiler:
  497. return other_compiler
  498. if cross_compiling and other_compiler.type == 'clang':
  499. return namespace(**{
  500. k: [] if k == 'flags' else v
  501. for k, v in other_compiler.__dict__.iteritems()
  502. })
  503. # Normally, we'd use `var` instead of `_var`, but the interaction with
  504. # old-configure complicates things, and for now, we a) can't take the plain
  505. # result from check_prog as CC/CXX/HOST_CC/HOST_CXX and b) have to let
  506. # old-configure AC_SUBST it (because it's autoconf doing it, not us)
  507. compiler = check_prog('_%s' % var, what=what, progs=default_compilers,
  508. input=delayed_getattr(provided_compiler, 'compiler'),
  509. paths=toolchain_search_path)
  510. @depends(compiler, provided_compiler, compiler_wrapper, host_or_target)
  511. @checking('whether %s can be used' % what, lambda x: bool(x))
  512. @imports(_from='mozbuild.shellutil', _import='quote')
  513. def valid_compiler(compiler, provided_compiler, compiler_wrapper,
  514. host_or_target):
  515. wrapper = list(compiler_wrapper or ())
  516. if provided_compiler:
  517. provided_wrapper = list(provided_compiler.wrapper)
  518. # When doing a subconfigure, the compiler is set by old-configure
  519. # and it contains the wrappers from --with-compiler-wrapper and
  520. # --with-ccache.
  521. if provided_wrapper[:len(wrapper)] == wrapper:
  522. provided_wrapper = provided_wrapper[len(wrapper):]
  523. wrapper.extend(provided_wrapper)
  524. flags = provided_compiler.flags
  525. else:
  526. flags = []
  527. # Ideally, we'd always use the absolute path, but unfortunately, on
  528. # Windows, the compiler is very often in a directory containing spaces.
  529. # Unfortunately, due to the way autoconf does its compiler tests with
  530. # eval, that doesn't work out. So in that case, check that the
  531. # compiler can still be found in $PATH, and use the file name instead
  532. # of the full path.
  533. if quote(compiler) != compiler:
  534. full_path = os.path.abspath(compiler)
  535. compiler = os.path.basename(compiler)
  536. found_compiler = find_program(compiler)
  537. if not found_compiler:
  538. die('%s is not in your $PATH'
  539. % quote(os.path.dirname(full_path)))
  540. if os.path.normcase(find_program(compiler)) != os.path.normcase(
  541. full_path):
  542. die('Found `%s` before `%s` in your $PATH. '
  543. 'Please reorder your $PATH.',
  544. quote(os.path.dirname(found_compiler)),
  545. quote(os.path.dirname(full_path)))
  546. info = check_compiler(wrapper + [compiler] + flags, language,
  547. host_or_target)
  548. # Check that the additional flags we got are enough to not require any
  549. # more flags.
  550. if info.flags:
  551. flags += info.flags
  552. info = check_compiler(wrapper + [compiler] + flags, language,
  553. host_or_target)
  554. if not info.target_cpu or info.target_cpu != host_or_target.cpu:
  555. raise FatalCheckError(
  556. '%s %s compiler target CPU (%s) does not match --%s CPU (%s)'
  557. % (host_or_target_str.capitalize(), language,
  558. info.target_cpu or 'unknown', host_or_target_str,
  559. host_or_target.raw_cpu))
  560. if not info.target_kernel or (info.target_kernel !=
  561. host_or_target.kernel):
  562. raise FatalCheckError(
  563. '%s %s compiler target kernel (%s) does not match --%s kernel (%s)'
  564. % (host_or_target_str.capitalize(), language,
  565. info.target_kernel or 'unknown', host_or_target_str,
  566. host_or_target.kernel))
  567. if not info.target_endianness or (info.target_endianness !=
  568. host_or_target.endianness):
  569. raise FatalCheckError(
  570. '%s %s compiler target endianness (%s) does not match --%s '
  571. 'endianness (%s)'
  572. % (host_or_target_str.capitalize(), language,
  573. info.target_endianness or 'unknown', host_or_target_str,
  574. host_or_target.endianness))
  575. if info.flags:
  576. raise FatalCheckError(
  577. 'Unknown compiler or compiler not supported.')
  578. # Compiler version checks
  579. # ===================================================
  580. # Check the compiler version here instead of in `compiler_version` so
  581. # that the `checking` message doesn't pretend the compiler can be used
  582. # to then bail out one line later.
  583. if info.type == 'gcc' and info.version < '4.9.0':
  584. raise FatalCheckError(
  585. 'Only GCC 4.9 or newer is supported (found version %s).'
  586. % info.version)
  587. # If you want to bump the version check here search for
  588. # __cpp_static_assert above, and see the associated comment.
  589. if info.type == 'clang' and not info.version:
  590. raise FatalCheckError(
  591. 'Only clang/llvm 3.6 or newer is supported.')
  592. if info.type == 'msvc':
  593. if info.version < '19.00.24213':
  594. raise FatalCheckError(
  595. 'This version (%s) of the MSVC compiler is not '
  596. 'supported.\n'
  597. 'You must install Visual C++ 2015 Update 3 or newer in '
  598. 'order to build.\n'
  599. 'See https://developer.mozilla.org/en/'
  600. 'Windows_Build_Prerequisites' % info.version)
  601. return namespace(
  602. wrapper=wrapper,
  603. compiler=compiler,
  604. flags=flags,
  605. type=info.type,
  606. version=info.version,
  607. language=language,
  608. )
  609. @depends(valid_compiler)
  610. @checking('%s version' % what)
  611. def compiler_version(compiler):
  612. return compiler.version
  613. if language == 'C++':
  614. @depends(valid_compiler, c_compiler)
  615. def valid_compiler(compiler, c_compiler):
  616. if compiler.type != c_compiler.type:
  617. die('The %s C compiler is %s, while the %s C++ compiler is '
  618. '%s. Need to use the same compiler suite.',
  619. host_or_target_str, c_compiler.type,
  620. host_or_target_str, compiler.type)
  621. if compiler.version != c_compiler.version:
  622. die('The %s C compiler is version %s, while the %s C++ '
  623. 'compiler is version %s. Need to use the same compiler '
  624. 'version.',
  625. host_or_target_str, c_compiler.version,
  626. host_or_target_str, compiler.version)
  627. return compiler
  628. # Set CC/CXX/HOST_CC/HOST_CXX for old-configure, which needs the wrapper
  629. # and the flags that were part of the user input for those variables to
  630. # be provided.
  631. add_old_configure_assignment(var, depends_if(valid_compiler)(
  632. lambda x: list(x.wrapper) + [x.compiler] + list(x.flags)))
  633. # Set CC_TYPE/CC_VERSION/HOST_CC_TYPE/HOST_CC_VERSION to allow
  634. # old-configure to do some of its still existing checks.
  635. if language == 'C':
  636. set_config(
  637. '%s_TYPE' % var, delayed_getattr(valid_compiler, 'type'))
  638. add_old_configure_assignment(
  639. '%s_TYPE' % var, delayed_getattr(valid_compiler, 'type'))
  640. add_old_configure_assignment(
  641. '%s_VERSION' % var, delayed_getattr(valid_compiler, 'version'))
  642. valid_compiler = compiler_class(valid_compiler)
  643. def compiler_error():
  644. raise FatalCheckError('Failed compiling a simple %s source with %s'
  645. % (language, what))
  646. valid_compiler.try_compile(check_msg='%s works' % what,
  647. onerror=compiler_error)
  648. # Set CPP/CXXCPP for both the build system and old-configure. We don't
  649. # need to check this works for preprocessing, because we already relied
  650. # on $CC -E/$CXX -E doing preprocessing work to validate the compiler
  651. # in the first place.
  652. if host_or_target == target:
  653. pp_var = {
  654. 'C': 'CPP',
  655. 'C++': 'CXXCPP',
  656. }[language]
  657. preprocessor = depends_if(valid_compiler)(
  658. lambda x: list(x.wrapper) + [x.compiler, '-E'] + list(x.flags))
  659. set_config(pp_var, preprocessor)
  660. add_old_configure_assignment(pp_var, preprocessor)
  661. return valid_compiler
  662. c_compiler = compiler('C', target)
  663. cxx_compiler = compiler('C++', target, c_compiler=c_compiler)
  664. host_c_compiler = compiler('C', host, other_compiler=c_compiler)
  665. host_cxx_compiler = compiler('C++', host, c_compiler=host_c_compiler,
  666. other_compiler=cxx_compiler,
  667. other_c_compiler=c_compiler)
  668. # Generic compiler-based conditions.
  669. non_msvc_compiler = depends(c_compiler)(lambda info: info.type != 'msvc')
  670. building_with_gcc = depends(c_compiler)(lambda info: info.type == 'gcc')
  671. include('compile-checks.configure')
  672. @depends(have_64_bit,
  673. try_compile(body='static_assert(sizeof(void *) == 8, "")',
  674. check_msg='for 64-bit OS'))
  675. def check_have_64_bit(have_64_bit, compiler_have_64_bit):
  676. if have_64_bit != compiler_have_64_bit:
  677. configure_error('The target compiler does not agree with configure '
  678. 'about the target bitness.')
  679. @depends(c_compiler)
  680. def default_debug_flags(compiler_info):
  681. # Debug info is ON by default.
  682. if compiler_info.type in ('msvc', 'clang-cl'):
  683. return '-Zi'
  684. return '-g'
  685. option(env='MOZ_DEBUG_FLAGS',
  686. nargs=1,
  687. help='Debug compiler flags')
  688. imply_option('--enable-debug-symbols',
  689. depends_if('--enable-debug')(lambda v: v))
  690. js_option('--enable-debug-symbols',
  691. nargs='?',
  692. default=True,
  693. help='Enable debug symbols using the given compiler flags')
  694. set_config('MOZ_DEBUG_SYMBOLS',
  695. depends_if('--enable-debug-symbols')(lambda _: True))
  696. @depends('MOZ_DEBUG_FLAGS', '--enable-debug-symbols', default_debug_flags)
  697. def debug_flags(env_debug_flags, enable_debug_flags, default_debug_flags):
  698. # If MOZ_DEBUG_FLAGS is set, and --enable-debug-symbols is set to a value,
  699. # --enable-debug-symbols takes precedence. Note, the value of
  700. # --enable-debug-symbols may be implied by --enable-debug.
  701. if len(enable_debug_flags):
  702. return enable_debug_flags[0]
  703. if env_debug_flags:
  704. return env_debug_flags[0]
  705. return default_debug_flags
  706. set_config('MOZ_DEBUG_FLAGS', debug_flags)
  707. add_old_configure_assignment('MOZ_DEBUG_FLAGS', debug_flags)
  708. # Some standard library headers (notably bionic on Android) declare standard
  709. # functions (e.g. getchar()) and also #define macros for those standard
  710. # functions. libc++ deals with this by doing something like the following
  711. # (explanatory comments added):
  712. #
  713. # #ifdef FUNC
  714. # // Capture the definition of FUNC.
  715. # inline _LIBCPP_INLINE_VISIBILITY int __libcpp_FUNC(...) { return FUNC(...); }
  716. # #undef FUNC
  717. # // Use a real inline definition.
  718. # inline _LIBCPP_INLINE_VISIBILITY int FUNC(...) { return _libcpp_FUNC(...); }
  719. # #endif
  720. #
  721. # _LIBCPP_INLINE_VISIBILITY is typically defined as:
  722. #
  723. # __attribute__((__visibility__("hidden"), __always_inline__))
  724. #
  725. # Unfortunately, this interacts badly with our system header wrappers, as the:
  726. #
  727. # #pragma GCC visibility push(default)
  728. #
  729. # that they do prior to including the actual system header is treated by the
  730. # compiler as an explicit declaration of visibility on every function declared
  731. # in the header. Therefore, when the libc++ code above is encountered, it is
  732. # as though the compiler has effectively seen:
  733. #
  734. # int FUNC(...) __attribute__((__visibility__("default")));
  735. # int FUNC(...) __attribute__((__visibility__("hidden")));
  736. #
  737. # and the compiler complains about the mismatched visibility declarations.
  738. #
  739. # However, libc++ will only define _LIBCPP_INLINE_VISIBILITY if there is no
  740. # existing definition. We can therefore define it to the empty string (since
  741. # we are properly managing visibility ourselves) and avoid this whole mess.
  742. # Note that we don't need to do this with gcc, as libc++ detects gcc and
  743. # effectively does the same thing we are doing here.
  744. @depends(c_compiler, target)
  745. def libcxx_inline_visibility(c_compiler, target):
  746. if c_compiler.type == 'clang' and target.os == 'Android':
  747. return ''
  748. set_define('_LIBCPP_INLINE_VISIBILITY', libcxx_inline_visibility)
  749. set_define('_LIBCPP_INLINE_VISIBILITY_EXCEPT_GCC49', libcxx_inline_visibility)
  750. @depends(c_compiler, target, check_build_environment)
  751. def visibility_flags(c_compiler, target, env):
  752. if target.os != 'WINNT':
  753. if target.kernel == 'Darwin':
  754. return ('-fvisibility=hidden', '-fvisibility-inlines-hidden')
  755. return ('-I%s/system_wrappers' % os.path.join(env.dist),
  756. '-include',
  757. '%s/config/gcc_hidden.h' % env.topsrcdir)
  758. @depends(target, visibility_flags)
  759. def wrap_system_includes(target, visibility_flags):
  760. if visibility_flags and target.kernel != 'Darwin':
  761. return True
  762. set_define('HAVE_VISIBILITY_HIDDEN_ATTRIBUTE',
  763. depends(visibility_flags)(lambda v: bool(v) or None))
  764. set_define('HAVE_VISIBILITY_ATTRIBUTE',
  765. depends(visibility_flags)(lambda v: bool(v) or None))
  766. set_config('WRAP_SYSTEM_INCLUDES', wrap_system_includes)
  767. set_config('VISIBILITY_FLAGS', visibility_flags)
  768. include('windows.configure')