jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1226 lines
36KB

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2005-2010 (ita)
  4. """
  5. C/C++/D configuration helpers
  6. """
  7. import os, re, shlex, sys
  8. from waflib import Build, Utils, Task, Options, Logs, Errors, Runner
  9. from waflib.TaskGen import after_method, feature
  10. from waflib.Configure import conf
  11. WAF_CONFIG_H = 'config.h'
  12. """default name for the config.h file"""
  13. DEFKEYS = 'define_key'
  14. INCKEYS = 'include_key'
  15. cfg_ver = {
  16. 'atleast-version': '>=',
  17. 'exact-version': '==',
  18. 'max-version': '<=',
  19. }
  20. SNIP_FUNCTION = '''
  21. int main(int argc, char **argv) {
  22. void (*p)();
  23. (void)argc; (void)argv;
  24. p=(void(*)())(%s);
  25. return !p;
  26. }
  27. '''
  28. """Code template for checking for functions"""
  29. SNIP_TYPE = '''
  30. int main(int argc, char **argv) {
  31. (void)argc; (void)argv;
  32. if ((%(type_name)s *) 0) return 0;
  33. if (sizeof (%(type_name)s)) return 0;
  34. return 1;
  35. }
  36. '''
  37. """Code template for checking for types"""
  38. SNIP_EMPTY_PROGRAM = '''
  39. int main(int argc, char **argv) {
  40. (void)argc; (void)argv;
  41. return 0;
  42. }
  43. '''
  44. SNIP_FIELD = '''
  45. int main(int argc, char **argv) {
  46. char *off;
  47. (void)argc; (void)argv;
  48. off = (char*) &((%(type_name)s*)0)->%(field_name)s;
  49. return (size_t) off < sizeof(%(type_name)s);
  50. }
  51. '''
  52. MACRO_TO_DESTOS = {
  53. '__linux__' : 'linux',
  54. '__GNU__' : 'gnu', # hurd
  55. '__FreeBSD__' : 'freebsd',
  56. '__NetBSD__' : 'netbsd',
  57. '__OpenBSD__' : 'openbsd',
  58. '__sun' : 'sunos',
  59. '__hpux' : 'hpux',
  60. '__sgi' : 'irix',
  61. '_AIX' : 'aix',
  62. '__CYGWIN__' : 'cygwin',
  63. '__MSYS__' : 'cygwin',
  64. '_UWIN' : 'uwin',
  65. '_WIN64' : 'win32',
  66. '_WIN32' : 'win32',
  67. # Note about darwin: this is also tested with 'defined __APPLE__ && defined __MACH__' somewhere below in this file.
  68. '__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__' : 'darwin',
  69. '__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__' : 'darwin', # iphone
  70. '__QNX__' : 'qnx',
  71. '__native_client__' : 'nacl' # google native client platform
  72. }
  73. MACRO_TO_DEST_CPU = {
  74. '__x86_64__' : 'x86_64',
  75. '__amd64__' : 'x86_64',
  76. '__i386__' : 'x86',
  77. '__ia64__' : 'ia',
  78. '__mips__' : 'mips',
  79. '__sparc__' : 'sparc',
  80. '__alpha__' : 'alpha',
  81. '__aarch64__' : 'aarch64',
  82. '__thumb__' : 'thumb',
  83. '__arm__' : 'arm',
  84. '__hppa__' : 'hppa',
  85. '__powerpc__' : 'powerpc',
  86. '__ppc__' : 'powerpc',
  87. '__convex__' : 'convex',
  88. '__m68k__' : 'm68k',
  89. '__s390x__' : 's390x',
  90. '__s390__' : 's390',
  91. '__sh__' : 'sh',
  92. }
  93. @conf
  94. def parse_flags(self, line, uselib_store, env=None, force_static=False, posix=None):
  95. """
  96. Parse the flags from the input lines, and add them to the relevant use variables::
  97. def configure(conf):
  98. conf.parse_flags('-O3', 'FOO')
  99. # conf.env.CXXFLAGS_FOO = ['-O3']
  100. # conf.env.CFLAGS_FOO = ['-O3']
  101. :param line: flags
  102. :type line: string
  103. :param uselib_store: where to add the flags
  104. :type uselib_store: string
  105. :param env: config set or conf.env by default
  106. :type env: :py:class:`waflib.ConfigSet.ConfigSet`
  107. """
  108. assert(isinstance(line, str))
  109. env = env or self.env
  110. # Issue 811 and 1371
  111. if posix is None:
  112. posix = True
  113. if '\\' in line:
  114. posix = ('\\ ' in line) or ('\\\\' in line)
  115. lex = shlex.shlex(line, posix=posix)
  116. lex.whitespace_split = True
  117. lex.commenters = ''
  118. lst = list(lex)
  119. # append_unique is not always possible
  120. # for example, apple flags may require both -arch i386 and -arch ppc
  121. app = env.append_value
  122. appu = env.append_unique
  123. uselib = uselib_store
  124. static = False
  125. while lst:
  126. x = lst.pop(0)
  127. st = x[:2]
  128. ot = x[2:]
  129. if st == '-I' or st == '/I':
  130. if not ot: ot = lst.pop(0)
  131. appu('INCLUDES_' + uselib, [ot])
  132. elif st == '-i':
  133. tmp = [x, lst.pop(0)]
  134. app('CFLAGS', tmp)
  135. app('CXXFLAGS', tmp)
  136. elif st == '-D' or (env.CXX_NAME == 'msvc' and st == '/D'): # not perfect but..
  137. if not ot: ot = lst.pop(0)
  138. app('DEFINES_' + uselib, [ot])
  139. elif st == '-l':
  140. if not ot: ot = lst.pop(0)
  141. prefix = (force_static or static) and 'STLIB_' or 'LIB_'
  142. appu(prefix + uselib, [ot])
  143. elif st == '-L':
  144. if not ot: ot = lst.pop(0)
  145. prefix = (force_static or static) and 'STLIBPATH_' or 'LIBPATH_'
  146. appu(prefix + uselib, [ot])
  147. elif x.startswith('/LIBPATH:'):
  148. prefix = (force_static or static) and 'STLIBPATH_' or 'LIBPATH_'
  149. appu(prefix + uselib, [x.replace('/LIBPATH:', '')])
  150. elif x == '-pthread' or x.startswith('+') or x.startswith('-std'):
  151. app('CFLAGS_' + uselib, [x])
  152. app('CXXFLAGS_' + uselib, [x])
  153. app('LINKFLAGS_' + uselib, [x])
  154. elif x == '-framework':
  155. appu('FRAMEWORK_' + uselib, [lst.pop(0)])
  156. elif x.startswith('-F'):
  157. appu('FRAMEWORKPATH_' + uselib, [x[2:]])
  158. elif x == '-Wl,-rpath' or x == '-Wl,-R':
  159. app('RPATH_' + uselib, lst.pop(0).lstrip('-Wl,'))
  160. elif x.startswith('-Wl,-R,'):
  161. app('RPATH_' + uselib, x[7:])
  162. elif x.startswith('-Wl,-R'):
  163. app('RPATH_' + uselib, x[6:])
  164. elif x.startswith('-Wl,-rpath,'):
  165. app('RPATH_' + uselib, x[11:])
  166. elif x == '-Wl,-Bstatic' or x == '-Bstatic':
  167. static = True
  168. elif x == '-Wl,-Bdynamic' or x == '-Bdynamic':
  169. static = False
  170. elif x.startswith('-Wl'):
  171. app('LINKFLAGS_' + uselib, [x])
  172. elif x.startswith('-m') or x.startswith('-f') or x.startswith('-dynamic'):
  173. app('CFLAGS_' + uselib, [x])
  174. app('CXXFLAGS_' + uselib, [x])
  175. elif x.startswith('-bundle'):
  176. app('LINKFLAGS_' + uselib, [x])
  177. elif x.startswith('-undefined') or x.startswith('-Xlinker'):
  178. arg = lst.pop(0)
  179. app('LINKFLAGS_' + uselib, [x, arg])
  180. elif x.startswith('-arch') or x.startswith('-isysroot'):
  181. tmp = [x, lst.pop(0)]
  182. app('CFLAGS_' + uselib, tmp)
  183. app('CXXFLAGS_' + uselib, tmp)
  184. app('LINKFLAGS_' + uselib, tmp)
  185. elif x.endswith('.a') or x.endswith('.so') or x.endswith('.dylib') or x.endswith('.lib'):
  186. appu('LINKFLAGS_' + uselib, [x]) # not cool, #762
  187. @conf
  188. def validate_cfg(self, kw):
  189. """
  190. Search for the program *pkg-config* if missing, and validate the parameters to pass to
  191. :py:func:`waflib.Tools.c_config.exec_cfg`.
  192. :param path: the **-config program to use** (default is *pkg-config*)
  193. :type path: list of string
  194. :param msg: message to display to describe the test executed
  195. :type msg: string
  196. :param okmsg: message to display when the test is successful
  197. :type okmsg: string
  198. :param errmsg: message to display in case of error
  199. :type errmsg: string
  200. """
  201. if not 'path' in kw:
  202. if not self.env.PKGCONFIG:
  203. self.find_program('pkg-config', var='PKGCONFIG')
  204. kw['path'] = self.env.PKGCONFIG
  205. # pkg-config version
  206. if 'atleast_pkgconfig_version' in kw:
  207. if not 'msg' in kw:
  208. kw['msg'] = 'Checking for pkg-config version >= %r' % kw['atleast_pkgconfig_version']
  209. return
  210. if not 'okmsg' in kw:
  211. kw['okmsg'] = 'yes'
  212. if not 'errmsg' in kw:
  213. kw['errmsg'] = 'not found'
  214. if 'modversion' in kw:
  215. if not 'msg' in kw:
  216. kw['msg'] = 'Checking for %r version' % kw['modversion']
  217. return
  218. # checking for the version of a module, for the moment, one thing at a time
  219. for x in cfg_ver.keys():
  220. y = x.replace('-', '_')
  221. if y in kw:
  222. if not 'package' in kw:
  223. raise ValueError('%s requires a package' % x)
  224. if not 'msg' in kw:
  225. kw['msg'] = 'Checking for %r %s %s' % (kw['package'], cfg_ver[x], kw[y])
  226. return
  227. if not 'define_name' in kw:
  228. pkgname = kw.get('uselib_store', kw['package'].upper())
  229. kw['define_name'] = self.have_define(pkgname)
  230. if not 'uselib_store' in kw:
  231. self.undefine(kw['define_name'])
  232. if not 'msg' in kw:
  233. kw['msg'] = 'Checking for %r' % (kw['package'] or kw['path'])
  234. @conf
  235. def exec_cfg(self, kw):
  236. """
  237. Execute the program *pkg-config*:
  238. * if atleast_pkgconfig_version is given, check that pkg-config has the version n and return
  239. * if modversion is given, then return the module version
  240. * else, execute the *-config* program with the *args* and *variables* given, and set the flags on the *conf.env.FLAGS_name* variable
  241. :param atleast_pkgconfig_version: minimum pkg-config version to use (disable other tests)
  242. :type atleast_pkgconfig_version: string
  243. :param package: package name, for example *gtk+-2.0*
  244. :type package: string
  245. :param uselib_store: if the test is successful, define HAVE\_*name*. It is also used to define *conf.env.FLAGS_name* variables.
  246. :type uselib_store: string
  247. :param modversion: if provided, return the version of the given module and define *name*\_VERSION
  248. :type modversion: string
  249. :param args: arguments to give to *package* when retrieving flags
  250. :type args: list of string
  251. :param variables: return the values of particular variables
  252. :type variables: list of string
  253. :param define_variable: additional variables to define (also in conf.env.PKG_CONFIG_DEFINES)
  254. :type define_variable: dict(string: string)
  255. """
  256. path = Utils.to_list(kw['path'])
  257. def define_it():
  258. pkgname = kw.get('uselib_store', kw['package'].upper())
  259. if kw.get('global_define'):
  260. # compatibility
  261. self.define(self.have_define(kw['package']), 1, False)
  262. else:
  263. self.env.append_unique('DEFINES_%s' % pkgname, "%s=1" % self.have_define(pkgname))
  264. self.env[self.have_define(pkgname)] = 1
  265. # pkg-config version
  266. if 'atleast_pkgconfig_version' in kw:
  267. cmd = path + ['--atleast-pkgconfig-version=%s' % kw['atleast_pkgconfig_version']]
  268. self.cmd_and_log(cmd)
  269. if not 'okmsg' in kw:
  270. kw['okmsg'] = 'yes'
  271. return
  272. # checking for the version of a module
  273. for x in cfg_ver:
  274. y = x.replace('-', '_')
  275. if y in kw:
  276. self.cmd_and_log(path + ['--%s=%s' % (x, kw[y]), kw['package']])
  277. if not 'okmsg' in kw:
  278. kw['okmsg'] = 'yes'
  279. define_it()
  280. break
  281. # retrieving the version of a module
  282. if 'modversion' in kw:
  283. version = self.cmd_and_log(path + ['--modversion', kw['modversion']]).strip()
  284. self.define('%s_VERSION' % Utils.quote_define_name(kw.get('uselib_store', kw['modversion'])), version)
  285. return version
  286. lst = [] + path
  287. defi = kw.get('define_variable', None)
  288. if not defi:
  289. defi = self.env.PKG_CONFIG_DEFINES or {}
  290. for key, val in defi.items():
  291. lst.append('--define-variable=%s=%s' % (key, val))
  292. static = kw.get('force_static', False)
  293. if 'args' in kw:
  294. args = Utils.to_list(kw['args'])
  295. if '--static' in args or '--static-libs' in args:
  296. static = True
  297. lst += args
  298. # tools like pkgconf expect the package argument after the -- ones -_-
  299. lst.extend(Utils.to_list(kw['package']))
  300. # retrieving variables of a module
  301. if 'variables' in kw:
  302. env = kw.get('env', self.env)
  303. uselib = kw.get('uselib_store', kw['package'].upper())
  304. vars = Utils.to_list(kw['variables'])
  305. for v in vars:
  306. val = self.cmd_and_log(lst + ['--variable=' + v]).strip()
  307. var = '%s_%s' % (uselib, v)
  308. env[var] = val
  309. if not 'okmsg' in kw:
  310. kw['okmsg'] = 'yes'
  311. return
  312. # so we assume the command-line will output flags to be parsed afterwards
  313. ret = self.cmd_and_log(lst)
  314. if not 'okmsg' in kw:
  315. kw['okmsg'] = 'yes'
  316. define_it()
  317. self.parse_flags(ret, kw.get('uselib_store', kw['package'].upper()), kw.get('env', self.env), force_static=static, posix=kw.get('posix', None))
  318. return ret
  319. @conf
  320. def check_cfg(self, *k, **kw):
  321. """
  322. Check for configuration flags using a **-config**-like program (pkg-config, sdl-config, etc).
  323. Encapsulate the calls to :py:func:`waflib.Tools.c_config.validate_cfg` and :py:func:`waflib.Tools.c_config.exec_cfg`
  324. A few examples::
  325. def configure(conf):
  326. conf.load('compiler_c')
  327. conf.check_cfg(package='glib-2.0', args='--libs --cflags')
  328. conf.check_cfg(package='glib-2.0', uselib_store='GLIB', atleast_version='2.10.0',
  329. args='--cflags --libs')
  330. conf.check_cfg(package='pango')
  331. conf.check_cfg(package='pango', uselib_store='MYPANGO', args=['--cflags', '--libs'])
  332. conf.check_cfg(package='pango',
  333. args=['pango >= 0.1.0', 'pango < 9.9.9', '--cflags', '--libs'],
  334. msg="Checking for 'pango 0.1.0'")
  335. conf.check_cfg(path='sdl-config', args='--cflags --libs', package='', uselib_store='SDL')
  336. conf.check_cfg(path='mpicc', args='--showme:compile --showme:link',
  337. package='', uselib_store='OPEN_MPI', mandatory=False)
  338. # variables
  339. conf.check_cfg(package='gtk+-2.0', variables=['includedir', 'prefix'], uselib_store='FOO')
  340. print(conf.env.FOO_includedir)
  341. """
  342. if k:
  343. lst = k[0].split()
  344. kw['package'] = lst[0]
  345. kw['args'] = ' '.join(lst[1:])
  346. self.validate_cfg(kw)
  347. if 'msg' in kw:
  348. self.start_msg(kw['msg'], **kw)
  349. ret = None
  350. try:
  351. ret = self.exec_cfg(kw)
  352. except self.errors.WafError:
  353. if 'errmsg' in kw:
  354. self.end_msg(kw['errmsg'], 'YELLOW', **kw)
  355. if Logs.verbose > 1:
  356. raise
  357. else:
  358. self.fatal('The configuration failed')
  359. else:
  360. if not ret:
  361. ret = True
  362. kw['success'] = ret
  363. if 'okmsg' in kw:
  364. self.end_msg(self.ret_msg(kw['okmsg'], kw), **kw)
  365. return ret
  366. def build_fun(bld):
  367. if bld.kw['compile_filename']:
  368. node = bld.srcnode.make_node(bld.kw['compile_filename'])
  369. node.write(bld.kw['code'])
  370. o = bld(features=bld.kw['features'], source=bld.kw['compile_filename'], target='testprog')
  371. for k, v in bld.kw.items():
  372. setattr(o, k, v)
  373. if not bld.kw.get('quiet', None):
  374. bld.conf.to_log("==>\n%s\n<==" % bld.kw['code'])
  375. @conf
  376. def validate_c(self, kw):
  377. """
  378. pre-check the parameters that will be given to :py:func:`waflib.Configure.run_build`
  379. :param compiler: c or cxx (tries to guess what is best)
  380. :type compiler: string
  381. :param type: cprogram, cshlib, cstlib - not required if *features are given directly*
  382. :type type: binary to create
  383. :param feature: desired features for the task generator that will execute the test, for example ``cxx cxxstlib``
  384. :type feature: list of string
  385. :param fragment: provide a piece of code for the test (default is to let the system create one)
  386. :type fragment: string
  387. :param uselib_store: define variables after the test is executed (IMPORTANT!)
  388. :type uselib_store: string
  389. :param use: parameters to use for building (just like the normal *use* keyword)
  390. :type use: list of string
  391. :param define_name: define to set when the check is over
  392. :type define_name: string
  393. :param execute: execute the resulting binary
  394. :type execute: bool
  395. :param define_ret: if execute is set to True, use the execution output in both the define and the return value
  396. :type define_ret: bool
  397. :param header_name: check for a particular header
  398. :type header_name: string
  399. :param auto_add_header_name: if header_name was set, add the headers in env.INCKEYS so the next tests will include these headers
  400. :type auto_add_header_name: bool
  401. """
  402. if not 'build_fun' in kw:
  403. kw['build_fun'] = build_fun
  404. if not 'env' in kw:
  405. kw['env'] = self.env.derive()
  406. env = kw['env']
  407. if not 'compiler' in kw and not 'features' in kw:
  408. kw['compiler'] = 'c'
  409. if env['CXX_NAME'] and Task.classes.get('cxx', None):
  410. kw['compiler'] = 'cxx'
  411. if not self.env['CXX']:
  412. self.fatal('a c++ compiler is required')
  413. else:
  414. if not self.env['CC']:
  415. self.fatal('a c compiler is required')
  416. if not 'compile_mode' in kw:
  417. kw['compile_mode'] = 'c'
  418. if 'cxx' in Utils.to_list(kw.get('features',[])) or kw.get('compiler', '') == 'cxx':
  419. kw['compile_mode'] = 'cxx'
  420. if not 'type' in kw:
  421. kw['type'] = 'cprogram'
  422. if not 'features' in kw:
  423. kw['features'] = [kw['compile_mode'], kw['type']] # "cprogram c"
  424. else:
  425. kw['features'] = Utils.to_list(kw['features'])
  426. if not 'compile_filename' in kw:
  427. kw['compile_filename'] = 'test.c' + ((kw['compile_mode'] == 'cxx') and 'pp' or '')
  428. def to_header(dct):
  429. if 'header_name' in dct:
  430. dct = Utils.to_list(dct['header_name'])
  431. return ''.join(['#include <%s>\n' % x for x in dct])
  432. return ''
  433. #OSX
  434. if 'framework_name' in kw:
  435. fwkname = kw['framework_name']
  436. if not 'uselib_store' in kw:
  437. kw['uselib_store'] = fwkname.upper()
  438. if not kw.get('no_header', False):
  439. if not 'header_name' in kw:
  440. kw['header_name'] = []
  441. fwk = '%s/%s.h' % (fwkname, fwkname)
  442. if kw.get('remove_dot_h', None):
  443. fwk = fwk[:-2]
  444. kw['header_name'] = Utils.to_list(kw['header_name']) + [fwk]
  445. kw['msg'] = 'Checking for framework %s' % fwkname
  446. kw['framework'] = fwkname
  447. #kw['frameworkpath'] = set it yourself
  448. if 'function_name' in kw:
  449. fu = kw['function_name']
  450. if not 'msg' in kw:
  451. kw['msg'] = 'Checking for function %s' % fu
  452. kw['code'] = to_header(kw) + SNIP_FUNCTION % fu
  453. if not 'uselib_store' in kw:
  454. kw['uselib_store'] = fu.upper()
  455. if not 'define_name' in kw:
  456. kw['define_name'] = self.have_define(fu)
  457. elif 'type_name' in kw:
  458. tu = kw['type_name']
  459. if not 'header_name' in kw:
  460. kw['header_name'] = 'stdint.h'
  461. if 'field_name' in kw:
  462. field = kw['field_name']
  463. kw['code'] = to_header(kw) + SNIP_FIELD % {'type_name' : tu, 'field_name' : field}
  464. if not 'msg' in kw:
  465. kw['msg'] = 'Checking for field %s in %s' % (field, tu)
  466. if not 'define_name' in kw:
  467. kw['define_name'] = self.have_define((tu + '_' + field).upper())
  468. else:
  469. kw['code'] = to_header(kw) + SNIP_TYPE % {'type_name' : tu}
  470. if not 'msg' in kw:
  471. kw['msg'] = 'Checking for type %s' % tu
  472. if not 'define_name' in kw:
  473. kw['define_name'] = self.have_define(tu.upper())
  474. elif 'header_name' in kw:
  475. if not 'msg' in kw:
  476. kw['msg'] = 'Checking for header %s' % kw['header_name']
  477. l = Utils.to_list(kw['header_name'])
  478. assert len(l)>0, 'list of headers in header_name is empty'
  479. kw['code'] = to_header(kw) + SNIP_EMPTY_PROGRAM
  480. if not 'uselib_store' in kw:
  481. kw['uselib_store'] = l[0].upper()
  482. if not 'define_name' in kw:
  483. kw['define_name'] = self.have_define(l[0])
  484. if 'lib' in kw:
  485. if not 'msg' in kw:
  486. kw['msg'] = 'Checking for library %s' % kw['lib']
  487. if not 'uselib_store' in kw:
  488. kw['uselib_store'] = kw['lib'].upper()
  489. if 'stlib' in kw:
  490. if not 'msg' in kw:
  491. kw['msg'] = 'Checking for static library %s' % kw['stlib']
  492. if not 'uselib_store' in kw:
  493. kw['uselib_store'] = kw['stlib'].upper()
  494. if 'fragment' in kw:
  495. # an additional code fragment may be provided to replace the predefined code
  496. # in custom headers
  497. kw['code'] = kw['fragment']
  498. if not 'msg' in kw:
  499. kw['msg'] = 'Checking for code snippet'
  500. if not 'errmsg' in kw:
  501. kw['errmsg'] = 'no'
  502. for (flagsname,flagstype) in (('cxxflags','compiler'), ('cflags','compiler'), ('linkflags','linker')):
  503. if flagsname in kw:
  504. if not 'msg' in kw:
  505. kw['msg'] = 'Checking for %s flags %s' % (flagstype, kw[flagsname])
  506. if not 'errmsg' in kw:
  507. kw['errmsg'] = 'no'
  508. if not 'execute' in kw:
  509. kw['execute'] = False
  510. if kw['execute']:
  511. kw['features'].append('test_exec')
  512. kw['chmod'] = 493
  513. if not 'errmsg' in kw:
  514. kw['errmsg'] = 'not found'
  515. if not 'okmsg' in kw:
  516. kw['okmsg'] = 'yes'
  517. if not 'code' in kw:
  518. kw['code'] = SNIP_EMPTY_PROGRAM
  519. # if there are headers to append automatically to the next tests
  520. if self.env[INCKEYS]:
  521. kw['code'] = '\n'.join(['#include <%s>' % x for x in self.env[INCKEYS]]) + '\n' + kw['code']
  522. if not kw.get('success'): kw['success'] = None
  523. if 'define_name' in kw:
  524. self.undefine(kw['define_name'])
  525. if not 'msg' in kw:
  526. self.fatal('missing "msg" in conf.check(...)')
  527. @conf
  528. def post_check(self, *k, **kw):
  529. "Set the variables after a test executed in :py:func:`waflib.Tools.c_config.check` was run successfully"
  530. is_success = 0
  531. if kw['execute']:
  532. if kw['success'] is not None:
  533. if kw.get('define_ret', False):
  534. is_success = kw['success']
  535. else:
  536. is_success = (kw['success'] == 0)
  537. else:
  538. is_success = (kw['success'] == 0)
  539. if 'define_name' in kw:
  540. # TODO simplify?
  541. if 'header_name' in kw or 'function_name' in kw or 'type_name' in kw or 'fragment' in kw:
  542. if kw['execute'] and kw.get('define_ret', None) and isinstance(is_success, str):
  543. self.define(kw['define_name'], is_success, quote=kw.get('quote', 1))
  544. else:
  545. self.define_cond(kw['define_name'], is_success)
  546. else:
  547. self.define_cond(kw['define_name'], is_success)
  548. if 'header_name' in kw:
  549. if kw.get('auto_add_header_name', False):
  550. self.env.append_value(INCKEYS, Utils.to_list(kw['header_name']))
  551. if is_success and 'uselib_store' in kw:
  552. from waflib.Tools import ccroot
  553. # TODO see get_uselib_vars from ccroot.py
  554. _vars = set([])
  555. for x in kw['features']:
  556. if x in ccroot.USELIB_VARS:
  557. _vars |= ccroot.USELIB_VARS[x]
  558. for k in _vars:
  559. lk = k.lower()
  560. if lk in kw:
  561. val = kw[lk]
  562. # remove trailing slash
  563. if isinstance(val, str):
  564. val = val.rstrip(os.path.sep)
  565. self.env.append_unique(k + '_' + kw['uselib_store'], Utils.to_list(val))
  566. return is_success
  567. @conf
  568. def check(self, *k, **kw):
  569. """
  570. Perform a configuration test by calling :py:func:`waflib.Configure.run_build`.
  571. For the complete list of parameters, see :py:func:`waflib.Tools.c_config.validate_c`.
  572. To force a specific compiler, pass "compiler='c'" or "compiler='cxx'" in the arguments
  573. Besides build targets, complete builds can be given though a build function. All files will
  574. be written to a temporary directory::
  575. def build(bld):
  576. lib_node = bld.srcnode.make_node('libdir/liblc1.c')
  577. lib_node.parent.mkdir()
  578. lib_node.write('#include <stdio.h>\\nint lib_func(void) { FILE *f = fopen("foo", "r");}\\n', 'w')
  579. bld(features='c cshlib', source=[lib_node], linkflags=conf.env.EXTRA_LDFLAGS, target='liblc')
  580. conf.check(build_fun=build, msg=msg)
  581. """
  582. self.validate_c(kw)
  583. self.start_msg(kw['msg'], **kw)
  584. ret = None
  585. try:
  586. ret = self.run_build(*k, **kw)
  587. except self.errors.ConfigurationError:
  588. self.end_msg(kw['errmsg'], 'YELLOW', **kw)
  589. if Logs.verbose > 1:
  590. raise
  591. else:
  592. self.fatal('The configuration failed')
  593. else:
  594. kw['success'] = ret
  595. ret = self.post_check(*k, **kw)
  596. if not ret:
  597. self.end_msg(kw['errmsg'], 'YELLOW', **kw)
  598. self.fatal('The configuration failed %r' % ret)
  599. else:
  600. self.end_msg(self.ret_msg(kw['okmsg'], kw), **kw)
  601. return ret
  602. class test_exec(Task.Task):
  603. """
  604. A task for executing a programs after they are built. See :py:func:`waflib.Tools.c_config.test_exec_fun`.
  605. """
  606. color = 'PINK'
  607. def run(self):
  608. if getattr(self.generator, 'rpath', None):
  609. if getattr(self.generator, 'define_ret', False):
  610. self.generator.bld.retval = self.generator.bld.cmd_and_log([self.inputs[0].abspath()])
  611. else:
  612. self.generator.bld.retval = self.generator.bld.exec_command([self.inputs[0].abspath()])
  613. else:
  614. env = self.env.env or {}
  615. env.update(dict(os.environ))
  616. for var in ('LD_LIBRARY_PATH', 'DYLD_LIBRARY_PATH', 'PATH'):
  617. env[var] = self.inputs[0].parent.abspath() + os.path.pathsep + env.get(var, '')
  618. if getattr(self.generator, 'define_ret', False):
  619. self.generator.bld.retval = self.generator.bld.cmd_and_log([self.inputs[0].abspath()], env=env)
  620. else:
  621. self.generator.bld.retval = self.generator.bld.exec_command([self.inputs[0].abspath()], env=env)
  622. @feature('test_exec')
  623. @after_method('apply_link')
  624. def test_exec_fun(self):
  625. """
  626. The feature **test_exec** is used to create a task that will to execute the binary
  627. created (link task output) during the build. The exit status will be set
  628. on the build context, so only one program may have the feature *test_exec*.
  629. This is used by configuration tests::
  630. def configure(conf):
  631. conf.check(execute=True)
  632. """
  633. self.create_task('test_exec', self.link_task.outputs[0])
  634. @conf
  635. def check_cxx(self, *k, **kw):
  636. # DO NOT USE
  637. kw['compiler'] = 'cxx'
  638. return self.check(*k, **kw)
  639. @conf
  640. def check_cc(self, *k, **kw):
  641. # DO NOT USE
  642. kw['compiler'] = 'c'
  643. return self.check(*k, **kw)
  644. @conf
  645. def define(self, key, val, quote=True):
  646. """
  647. Store a single define and its state into conf.env.DEFINES. If the value is True, False or None it is cast to 1 or 0.
  648. :param key: define name
  649. :type key: string
  650. :param val: value
  651. :type val: int or string
  652. :param quote: enclose strings in quotes (yes by default)
  653. :type quote: bool
  654. """
  655. assert key and isinstance(key, str)
  656. if val is True:
  657. val = 1
  658. elif val in (False, None):
  659. val = 0
  660. if isinstance(val, int) or isinstance(val, float):
  661. s = '%s=%s'
  662. else:
  663. s = quote and '%s="%s"' or '%s=%s'
  664. app = s % (key, str(val))
  665. ban = key + '='
  666. lst = self.env['DEFINES']
  667. for x in lst:
  668. if x.startswith(ban):
  669. lst[lst.index(x)] = app
  670. break
  671. else:
  672. self.env.append_value('DEFINES', app)
  673. self.env.append_unique(DEFKEYS, key)
  674. @conf
  675. def undefine(self, key):
  676. """
  677. Remove a define from conf.env.DEFINES
  678. :param key: define name
  679. :type key: string
  680. """
  681. assert key and isinstance(key, str)
  682. ban = key + '='
  683. lst = [x for x in self.env['DEFINES'] if not x.startswith(ban)]
  684. self.env['DEFINES'] = lst
  685. self.env.append_unique(DEFKEYS, key)
  686. @conf
  687. def define_cond(self, key, val):
  688. """
  689. Conditionally define a name::
  690. def configure(conf):
  691. conf.define_cond('A', True)
  692. # equivalent to:
  693. # if val: conf.define('A', 1)
  694. # else: conf.undefine('A')
  695. :param key: define name
  696. :type key: string
  697. :param val: value
  698. :type val: int or string
  699. """
  700. assert key and isinstance(key, str)
  701. if val:
  702. self.define(key, 1)
  703. else:
  704. self.undefine(key)
  705. @conf
  706. def is_defined(self, key):
  707. """
  708. :param key: define name
  709. :type key: string
  710. :return: True if the define is set
  711. :rtype: bool
  712. """
  713. assert key and isinstance(key, str)
  714. ban = key + '='
  715. for x in self.env['DEFINES']:
  716. if x.startswith(ban):
  717. return True
  718. return False
  719. @conf
  720. def get_define(self, key):
  721. """
  722. :param key: define name
  723. :type key: string
  724. :return: the value of a previously stored define or None if it is not set
  725. """
  726. assert key and isinstance(key, str)
  727. ban = key + '='
  728. for x in self.env['DEFINES']:
  729. if x.startswith(ban):
  730. return x[len(ban):]
  731. return None
  732. @conf
  733. def have_define(self, key):
  734. """
  735. :param key: define name
  736. :type key: string
  737. :return: the input key prefixed by *HAVE_* and substitute any invalid characters.
  738. :rtype: string
  739. """
  740. return (self.env.HAVE_PAT or 'HAVE_%s') % Utils.quote_define_name(key)
  741. @conf
  742. def write_config_header(self, configfile='', guard='', top=False, defines=True, headers=False, remove=True, define_prefix=''):
  743. """
  744. Write a configuration header containing defines and includes::
  745. def configure(cnf):
  746. cnf.define('A', 1)
  747. cnf.write_config_header('config.h')
  748. This function only adds include guards (if necessary), consult
  749. :py:func:`waflib.Tools.c_config.get_config_header` for details on the body.
  750. :param configfile: path to the file to create (relative or absolute)
  751. :type configfile: string
  752. :param guard: include guard name to add, by default it is computed from the file name
  753. :type guard: string
  754. :param top: write the configuration header from the build directory (default is from the current path)
  755. :type top: bool
  756. :param defines: add the defines (yes by default)
  757. :type defines: bool
  758. :param headers: add #include in the file
  759. :type headers: bool
  760. :param remove: remove the defines after they are added (yes by default, works like in autoconf)
  761. :type remove: bool
  762. :type define_prefix: string
  763. :param define_prefix: prefix all the defines in the file with a particular prefix
  764. """
  765. if not configfile: configfile = WAF_CONFIG_H
  766. waf_guard = guard or 'W_%s_WAF' % Utils.quote_define_name(configfile)
  767. node = top and self.bldnode or self.path.get_bld()
  768. node = node.make_node(configfile)
  769. node.parent.mkdir()
  770. lst = ['/* WARNING! All changes made to this file will be lost! */\n']
  771. lst.append('#ifndef %s\n#define %s\n' % (waf_guard, waf_guard))
  772. lst.append(self.get_config_header(defines, headers, define_prefix=define_prefix))
  773. lst.append('\n#endif /* %s */\n' % waf_guard)
  774. node.write('\n'.join(lst))
  775. # config files must not be removed on "waf clean"
  776. self.env.append_unique(Build.CFG_FILES, [node.abspath()])
  777. if remove:
  778. for key in self.env[DEFKEYS]:
  779. self.undefine(key)
  780. self.env[DEFKEYS] = []
  781. @conf
  782. def get_config_header(self, defines=True, headers=False, define_prefix=''):
  783. """
  784. Create the contents of a ``config.h`` file from the defines and includes
  785. set in conf.env.define_key / conf.env.include_key. No include guards are added.
  786. A prelude will be added from the variable env.WAF_CONFIG_H_PRELUDE if provided. This
  787. can be used to insert complex macros or include guards::
  788. def configure(conf):
  789. conf.env.WAF_CONFIG_H_PRELUDE = '#include <unistd.h>\\n'
  790. conf.write_config_header('config.h')
  791. :param defines: write the defines values
  792. :type defines: bool
  793. :param headers: write include entries for each element in self.env.INCKEYS
  794. :type headers: bool
  795. :type define_prefix: string
  796. :param define_prefix: prefix all the defines with a particular prefix
  797. :return: the contents of a ``config.h`` file
  798. :rtype: string
  799. """
  800. lst = []
  801. if self.env.WAF_CONFIG_H_PRELUDE:
  802. lst.append(self.env.WAF_CONFIG_H_PRELUDE)
  803. if headers:
  804. for x in self.env[INCKEYS]:
  805. lst.append('#include <%s>' % x)
  806. if defines:
  807. tbl = {}
  808. for k in self.env['DEFINES']:
  809. a, _, b = k.partition('=')
  810. tbl[a] = b
  811. for k in self.env[DEFKEYS]:
  812. try:
  813. txt = '#define %s%s %s' % (define_prefix, k, tbl[k])
  814. except KeyError:
  815. txt = '/* #undef %s%s */' % (define_prefix, k)
  816. lst.append(txt)
  817. return "\n".join(lst)
  818. @conf
  819. def cc_add_flags(conf):
  820. """
  821. Add CFLAGS / CPPFLAGS from os.environ to conf.env
  822. """
  823. conf.add_os_flags('CPPFLAGS', dup=False)
  824. conf.add_os_flags('CFLAGS', dup=False)
  825. @conf
  826. def cxx_add_flags(conf):
  827. """
  828. Add CXXFLAGS / CPPFLAGS from os.environ to conf.env
  829. """
  830. conf.add_os_flags('CPPFLAGS', dup=False)
  831. conf.add_os_flags('CXXFLAGS', dup=False)
  832. @conf
  833. def link_add_flags(conf):
  834. """
  835. Add LINKFLAGS / LDFLAGS from os.environ to conf.env
  836. """
  837. conf.add_os_flags('LINKFLAGS', dup=False)
  838. conf.add_os_flags('LDFLAGS', dup=False)
  839. @conf
  840. def cc_load_tools(conf):
  841. """
  842. Load the c tool
  843. """
  844. if not conf.env.DEST_OS:
  845. conf.env.DEST_OS = Utils.unversioned_sys_platform()
  846. conf.load('c')
  847. @conf
  848. def cxx_load_tools(conf):
  849. """
  850. Load the cxx tool
  851. """
  852. if not conf.env.DEST_OS:
  853. conf.env.DEST_OS = Utils.unversioned_sys_platform()
  854. conf.load('cxx')
  855. @conf
  856. def get_cc_version(conf, cc, gcc=False, icc=False, clang=False):
  857. """
  858. Run the preprocessor to determine the compiler version
  859. The variables CC_VERSION, DEST_OS, DEST_BINFMT and DEST_CPU will be set in *conf.env*
  860. """
  861. cmd = cc + ['-dM', '-E', '-']
  862. env = conf.env.env or None
  863. try:
  864. out, err = conf.cmd_and_log(cmd, output=0, input='\n'.encode(), env=env)
  865. except Exception:
  866. conf.fatal('Could not determine the compiler version %r' % cmd)
  867. if gcc:
  868. if out.find('__INTEL_COMPILER') >= 0:
  869. conf.fatal('The intel compiler pretends to be gcc')
  870. if out.find('__GNUC__') < 0 and out.find('__clang__') < 0:
  871. conf.fatal('Could not determine the compiler type')
  872. if icc and out.find('__INTEL_COMPILER') < 0:
  873. conf.fatal('Not icc/icpc')
  874. if clang and out.find('__clang__') < 0:
  875. conf.fatal('Not clang/clang++')
  876. if not clang and out.find('__clang__') >= 0:
  877. conf.fatal('Could not find gcc/g++ (only Clang), if renamed try eg: CC=gcc48 CXX=g++48 waf configure')
  878. k = {}
  879. if icc or gcc or clang:
  880. out = out.splitlines()
  881. for line in out:
  882. lst = shlex.split(line)
  883. if len(lst)>2:
  884. key = lst[1]
  885. val = lst[2]
  886. k[key] = val
  887. def isD(var):
  888. return var in k
  889. # Some documentation is available at http://predef.sourceforge.net
  890. # The names given to DEST_OS must match what Utils.unversioned_sys_platform() returns.
  891. if not conf.env.DEST_OS:
  892. conf.env.DEST_OS = ''
  893. for i in MACRO_TO_DESTOS:
  894. if isD(i):
  895. conf.env.DEST_OS = MACRO_TO_DESTOS[i]
  896. break
  897. else:
  898. if isD('__APPLE__') and isD('__MACH__'):
  899. conf.env.DEST_OS = 'darwin'
  900. elif isD('__unix__'): # unix must be tested last as it's a generic fallback
  901. conf.env.DEST_OS = 'generic'
  902. if isD('__ELF__'):
  903. conf.env.DEST_BINFMT = 'elf'
  904. elif isD('__WINNT__') or isD('__CYGWIN__') or isD('_WIN32'):
  905. conf.env.DEST_BINFMT = 'pe'
  906. conf.env.LIBDIR = conf.env.BINDIR
  907. elif isD('__APPLE__'):
  908. conf.env.DEST_BINFMT = 'mac-o'
  909. if not conf.env.DEST_BINFMT:
  910. # Infer the binary format from the os name.
  911. conf.env.DEST_BINFMT = Utils.destos_to_binfmt(conf.env.DEST_OS)
  912. for i in MACRO_TO_DEST_CPU:
  913. if isD(i):
  914. conf.env.DEST_CPU = MACRO_TO_DEST_CPU[i]
  915. break
  916. Logs.debug('ccroot: dest platform: ' + ' '.join([conf.env[x] or '?' for x in ('DEST_OS', 'DEST_BINFMT', 'DEST_CPU')]))
  917. if icc:
  918. ver = k['__INTEL_COMPILER']
  919. conf.env['CC_VERSION'] = (ver[:-2], ver[-2], ver[-1])
  920. else:
  921. if isD('__clang__') and isD('__clang_major__'):
  922. conf.env['CC_VERSION'] = (k['__clang_major__'], k['__clang_minor__'], k['__clang_patchlevel__'])
  923. else:
  924. # older clang versions and gcc
  925. conf.env['CC_VERSION'] = (k['__GNUC__'], k['__GNUC_MINOR__'], k.get('__GNUC_PATCHLEVEL__', '0'))
  926. return k
  927. @conf
  928. def get_xlc_version(conf, cc):
  929. """Get the compiler version"""
  930. cmd = cc + ['-qversion']
  931. try:
  932. out, err = conf.cmd_and_log(cmd, output=0)
  933. except Errors.WafError:
  934. conf.fatal('Could not find xlc %r' % cmd)
  935. # the intention is to catch the 8.0 in "IBM XL C/C++ Enterprise Edition V8.0 for AIX..."
  936. for v in (r"IBM XL C/C\+\+.* V(?P<major>\d*)\.(?P<minor>\d*)",):
  937. version_re = re.compile(v, re.I).search
  938. match = version_re(out or err)
  939. if match:
  940. k = match.groupdict()
  941. conf.env['CC_VERSION'] = (k['major'], k['minor'])
  942. break
  943. else:
  944. conf.fatal('Could not determine the XLC version.')
  945. @conf
  946. def get_suncc_version(conf, cc):
  947. """Get the compiler version"""
  948. cmd = cc + ['-V']
  949. try:
  950. out, err = conf.cmd_and_log(cmd, output=0)
  951. except Errors.WafError as e:
  952. # Older versions of the compiler exit with non-zero status when reporting their version
  953. if not (hasattr(e, 'returncode') and hasattr(e, 'stdout') and hasattr(e, 'stderr')):
  954. conf.fatal('Could not find suncc %r' % cmd)
  955. out = e.stdout
  956. err = e.stderr
  957. version = (out or err)
  958. version = version.splitlines()[0]
  959. version_re = re.compile(r'cc:\s+sun\s+(c\+\+|c)\s+(?P<major>\d*)\.(?P<minor>\d*)', re.I).search
  960. match = version_re(version)
  961. if match:
  962. k = match.groupdict()
  963. conf.env['CC_VERSION'] = (k['major'], k['minor'])
  964. else:
  965. conf.fatal('Could not determine the suncc version.')
  966. # ============ the --as-needed flag should added during the configuration, not at runtime =========
  967. @conf
  968. def add_as_needed(self):
  969. """
  970. Add ``--as-needed`` to the *LINKFLAGS*
  971. On some platforms, it is a default flag. In some cases (e.g., in NS-3) it is necessary to explicitly disable this feature with `-Wl,--no-as-needed` flag.
  972. """
  973. if self.env.DEST_BINFMT == 'elf' and 'gcc' in (self.env.CXX_NAME, self.env.CC_NAME):
  974. self.env.append_unique('LINKFLAGS', '-Wl,--as-needed')
  975. # ============ parallel configuration
  976. class cfgtask(Task.TaskBase):
  977. """
  978. A task that executes configuration tests
  979. make sure that the checks write to conf.env in a thread-safe manner
  980. for the moment it only executes conf.check
  981. """
  982. def display(self):
  983. return ''
  984. def runnable_status(self):
  985. return Task.RUN_ME
  986. def uid(self):
  987. return Utils.SIG_NIL
  988. def run(self):
  989. conf = self.conf
  990. bld = Build.BuildContext(top_dir=conf.srcnode.abspath(), out_dir=conf.bldnode.abspath())
  991. bld.env = conf.env
  992. bld.init_dirs()
  993. bld.in_msg = 1 # suppress top-level start_msg
  994. bld.logger = self.logger
  995. try:
  996. bld.check(**self.args)
  997. except Exception:
  998. return 1
  999. @conf
  1000. def multicheck(self, *k, **kw):
  1001. """
  1002. Use tuples to perform parallel configuration tests
  1003. """
  1004. self.start_msg(kw.get('msg', 'Executing %d configuration tests' % len(k)), **kw)
  1005. class par(object):
  1006. def __init__(self):
  1007. self.keep = False
  1008. self.returned_tasks = []
  1009. self.task_sigs = {}
  1010. self.progress_bar = 0
  1011. def total(self):
  1012. return len(tasks)
  1013. def to_log(self, *k, **kw):
  1014. return
  1015. bld = par()
  1016. tasks = []
  1017. for dct in k:
  1018. x = cfgtask(bld=bld)
  1019. tasks.append(x)
  1020. x.args = dct
  1021. x.bld = bld
  1022. x.conf = self
  1023. x.args = dct
  1024. # bind a logger that will keep the info in memory
  1025. x.logger = Logs.make_mem_logger(str(id(x)), self.logger)
  1026. def it():
  1027. yield tasks
  1028. while 1:
  1029. yield []
  1030. p = Runner.Parallel(bld, Options.options.jobs)
  1031. p.biter = it()
  1032. p.start()
  1033. # flush the logs in order into the config.log
  1034. for x in tasks:
  1035. x.logger.memhandler.flush()
  1036. if p.error:
  1037. for x in p.error:
  1038. if getattr(x, 'err_msg', None):
  1039. self.to_log(x.err_msg)
  1040. self.end_msg('fail', color='RED')
  1041. raise Errors.WafError('There is an error in the library, read config.log for more information')
  1042. for x in tasks:
  1043. if x.hasrun != Task.SUCCESS:
  1044. self.end_msg(kw.get('errmsg', 'no'), color='YELLOW', **kw)
  1045. self.fatal(kw.get('fatalmsg', None) or 'One of the tests has failed, read config.log for more information')
  1046. self.end_msg('ok', **kw)