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.

704 lines
22KB

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2005-2010 (ita)
  4. """
  5. Classes and methods shared by tools providing support for C-like language such
  6. as C/C++/D/Assembly/Go (this support module is almost never used alone).
  7. """
  8. import os, re
  9. from waflib import Task, Utils, Node, Errors
  10. from waflib.TaskGen import after_method, before_method, feature, taskgen_method, extension
  11. from waflib.Tools import c_aliases, c_preproc, c_config, c_osx, c_tests
  12. from waflib.Configure import conf
  13. SYSTEM_LIB_PATHS = ['/usr/lib64', '/usr/lib', '/usr/local/lib64', '/usr/local/lib']
  14. USELIB_VARS = Utils.defaultdict(set)
  15. """
  16. Mapping for features to :py:class:`waflib.ConfigSet.ConfigSet` variables. See :py:func:`waflib.Tools.ccroot.propagate_uselib_vars`.
  17. """
  18. USELIB_VARS['c'] = set(['INCLUDES', 'FRAMEWORKPATH', 'DEFINES', 'CPPFLAGS', 'CCDEPS', 'CFLAGS', 'ARCH'])
  19. USELIB_VARS['cxx'] = set(['INCLUDES', 'FRAMEWORKPATH', 'DEFINES', 'CPPFLAGS', 'CXXDEPS', 'CXXFLAGS', 'ARCH'])
  20. USELIB_VARS['d'] = set(['INCLUDES', 'DFLAGS'])
  21. USELIB_VARS['includes'] = set(['INCLUDES', 'FRAMEWORKPATH', 'ARCH'])
  22. USELIB_VARS['cprogram'] = USELIB_VARS['cxxprogram'] = set(['LIB', 'STLIB', 'LIBPATH', 'STLIBPATH', 'LINKFLAGS', 'RPATH', 'LINKDEPS', 'FRAMEWORK', 'FRAMEWORKPATH', 'ARCH'])
  23. USELIB_VARS['cshlib'] = USELIB_VARS['cxxshlib'] = set(['LIB', 'STLIB', 'LIBPATH', 'STLIBPATH', 'LINKFLAGS', 'RPATH', 'LINKDEPS', 'FRAMEWORK', 'FRAMEWORKPATH', 'ARCH'])
  24. USELIB_VARS['cstlib'] = USELIB_VARS['cxxstlib'] = set(['ARFLAGS', 'LINKDEPS'])
  25. USELIB_VARS['dprogram'] = set(['LIB', 'STLIB', 'LIBPATH', 'STLIBPATH', 'LINKFLAGS', 'RPATH', 'LINKDEPS'])
  26. USELIB_VARS['dshlib'] = set(['LIB', 'STLIB', 'LIBPATH', 'STLIBPATH', 'LINKFLAGS', 'RPATH', 'LINKDEPS'])
  27. USELIB_VARS['dstlib'] = set(['ARFLAGS', 'LINKDEPS'])
  28. USELIB_VARS['asm'] = set(['ASFLAGS'])
  29. # =================================================================================================
  30. @taskgen_method
  31. def create_compiled_task(self, name, node):
  32. """
  33. Create the compilation task: c, cxx, asm, etc. The output node is created automatically (object file with a typical **.o** extension).
  34. The task is appended to the list *compiled_tasks* which is then used by :py:func:`waflib.Tools.ccroot.apply_link`
  35. :param name: name of the task class
  36. :type name: string
  37. :param node: the file to compile
  38. :type node: :py:class:`waflib.Node.Node`
  39. :return: The task created
  40. :rtype: :py:class:`waflib.Task.Task`
  41. """
  42. out = '%s.%d.o' % (node.name, self.idx)
  43. task = self.create_task(name, node, node.parent.find_or_declare(out))
  44. try:
  45. self.compiled_tasks.append(task)
  46. except AttributeError:
  47. self.compiled_tasks = [task]
  48. return task
  49. @taskgen_method
  50. def to_incnodes(self, inlst):
  51. """
  52. Task generator method provided to convert a list of string/nodes into a list of includes folders.
  53. The paths are assumed to be relative to the task generator path, except if they begin by **#**
  54. in which case they are searched from the top-level directory (``bld.srcnode``).
  55. The folders are simply assumed to be existing.
  56. The node objects in the list are returned in the output list. The strings are converted
  57. into node objects if possible. The node is searched from the source directory, and if a match is found,
  58. the equivalent build directory is created and added to the returned list too. When a folder cannot be found, it is ignored.
  59. :param inlst: list of folders
  60. :type inlst: space-delimited string or a list of string/nodes
  61. :rtype: list of :py:class:`waflib.Node.Node`
  62. :return: list of include folders as nodes
  63. """
  64. lst = []
  65. seen = set([])
  66. for x in self.to_list(inlst):
  67. if x in seen or not x:
  68. continue
  69. seen.add(x)
  70. # with a real lot of targets, it is sometimes interesting to cache the results below
  71. if isinstance(x, Node.Node):
  72. lst.append(x)
  73. else:
  74. if os.path.isabs(x):
  75. lst.append(self.bld.root.make_node(x) or x)
  76. else:
  77. if x[0] == '#':
  78. p = self.bld.bldnode.make_node(x[1:])
  79. v = self.bld.srcnode.make_node(x[1:])
  80. else:
  81. p = self.path.get_bld().make_node(x)
  82. v = self.path.make_node(x)
  83. if p.is_child_of(self.bld.bldnode):
  84. p.mkdir()
  85. lst.append(p)
  86. lst.append(v)
  87. return lst
  88. @feature('c', 'cxx', 'd', 'asm', 'fc', 'includes')
  89. @after_method('propagate_uselib_vars', 'process_source')
  90. def apply_incpaths(self):
  91. """
  92. Task generator method that processes the attribute *includes*::
  93. tg = bld(features='includes', includes='.')
  94. The folders only need to be relative to the current directory, the equivalent build directory is
  95. added automatically (for headers created in the build directory). This enable using a build directory
  96. or not (``top == out``).
  97. This method will add a list of nodes read by :py:func:`waflib.Tools.ccroot.to_incnodes` in ``tg.env.INCPATHS``,
  98. and the list of include paths in ``tg.env.INCLUDES``.
  99. """
  100. lst = self.to_incnodes(self.to_list(getattr(self, 'includes', [])) + self.env['INCLUDES'])
  101. self.includes_nodes = lst
  102. self.env['INCPATHS'] = [x.abspath() for x in lst]
  103. class link_task(Task.Task):
  104. """
  105. Base class for all link tasks. A task generator is supposed to have at most one link task bound in the attribute *link_task*. See :py:func:`waflib.Tools.ccroot.apply_link`.
  106. .. inheritance-diagram:: waflib.Tools.ccroot.stlink_task waflib.Tools.c.cprogram waflib.Tools.c.cshlib waflib.Tools.cxx.cxxstlib waflib.Tools.cxx.cxxprogram waflib.Tools.cxx.cxxshlib waflib.Tools.d.dprogram waflib.Tools.d.dshlib waflib.Tools.d.dstlib waflib.Tools.ccroot.fake_shlib waflib.Tools.ccroot.fake_stlib waflib.Tools.asm.asmprogram waflib.Tools.asm.asmshlib waflib.Tools.asm.asmstlib
  107. """
  108. color = 'YELLOW'
  109. inst_to = None
  110. """Default installation path for the link task outputs, or None to disable"""
  111. chmod = Utils.O755
  112. """Default installation mode for the link task outputs"""
  113. def add_target(self, target):
  114. """
  115. Process the *target* attribute to add the platform-specific prefix/suffix such as *.so* or *.exe*.
  116. The settings are retrieved from ``env.clsname_PATTERN``
  117. """
  118. if isinstance(target, str):
  119. pattern = self.env[self.__class__.__name__ + '_PATTERN']
  120. if not pattern:
  121. pattern = '%s'
  122. folder, name = os.path.split(target)
  123. if self.__class__.__name__.find('shlib') > 0 and getattr(self.generator, 'vnum', None):
  124. nums = self.generator.vnum.split('.')
  125. if self.env.DEST_BINFMT == 'pe':
  126. # include the version in the dll file name,
  127. # the import lib file name stays unversionned.
  128. name = name + '-' + nums[0]
  129. elif self.env.DEST_OS == 'openbsd':
  130. pattern = '%s.%s' % (pattern, nums[0])
  131. if len(nums) >= 2:
  132. pattern += '.%s' % nums[1]
  133. tmp = folder + os.sep + pattern % name
  134. target = self.generator.path.find_or_declare(tmp)
  135. self.set_outputs(target)
  136. class stlink_task(link_task):
  137. """
  138. Base for static link tasks, which use *ar* most of the time.
  139. The target is always removed before being written.
  140. """
  141. run_str = '${AR} ${ARFLAGS} ${AR_TGT_F}${TGT} ${AR_SRC_F}${SRC}'
  142. chmod = Utils.O644
  143. """Default installation mode for the static libraries"""
  144. def rm_tgt(cls):
  145. old = cls.run
  146. def wrap(self):
  147. try: os.remove(self.outputs[0].abspath())
  148. except OSError: pass
  149. return old(self)
  150. setattr(cls, 'run', wrap)
  151. rm_tgt(stlink_task)
  152. @feature('c', 'cxx', 'd', 'fc', 'asm')
  153. @after_method('process_source')
  154. def apply_link(self):
  155. """
  156. Collect the tasks stored in ``compiled_tasks`` (created by :py:func:`waflib.Tools.ccroot.create_compiled_task`), and
  157. use the outputs for a new instance of :py:class:`waflib.Tools.ccroot.link_task`. The class to use is the first link task
  158. matching a name from the attribute *features*, for example::
  159. def build(bld):
  160. tg = bld(features='cxx cxxprogram cprogram', source='main.c', target='app')
  161. will create the task ``tg.link_task`` as a new instance of :py:class:`waflib.Tools.cxx.cxxprogram`
  162. """
  163. for x in self.features:
  164. if x == 'cprogram' and 'cxx' in self.features: # limited compat
  165. x = 'cxxprogram'
  166. elif x == 'cshlib' and 'cxx' in self.features:
  167. x = 'cxxshlib'
  168. if x in Task.classes:
  169. if issubclass(Task.classes[x], link_task):
  170. link = x
  171. break
  172. else:
  173. return
  174. objs = [t.outputs[0] for t in getattr(self, 'compiled_tasks', [])]
  175. self.link_task = self.create_task(link, objs)
  176. self.link_task.add_target(self.target)
  177. # remember that the install paths are given by the task generators
  178. try:
  179. inst_to = self.install_path
  180. except AttributeError:
  181. inst_to = self.link_task.__class__.inst_to
  182. if inst_to:
  183. # install a copy of the node list we have at this moment (implib not added)
  184. self.install_task = self.bld.install_files(inst_to, self.link_task.outputs[:], env=self.env, chmod=self.link_task.chmod, task=self.link_task)
  185. @taskgen_method
  186. def use_rec(self, name, **kw):
  187. """
  188. Processes the ``use`` keyword recursively. This method is kind of private and only meant to be used from ``process_use``
  189. """
  190. if name in self.tmp_use_not or name in self.tmp_use_seen:
  191. return
  192. try:
  193. y = self.bld.get_tgen_by_name(name)
  194. except Errors.WafError:
  195. self.uselib.append(name)
  196. self.tmp_use_not.add(name)
  197. return
  198. self.tmp_use_seen.append(name)
  199. y.post()
  200. # bind temporary attributes on the task generator
  201. y.tmp_use_objects = objects = kw.get('objects', True)
  202. y.tmp_use_stlib = stlib = kw.get('stlib', True)
  203. try:
  204. link_task = y.link_task
  205. except AttributeError:
  206. y.tmp_use_var = ''
  207. else:
  208. objects = False
  209. if not isinstance(link_task, stlink_task):
  210. stlib = False
  211. y.tmp_use_var = 'LIB'
  212. else:
  213. y.tmp_use_var = 'STLIB'
  214. p = self.tmp_use_prec
  215. for x in self.to_list(getattr(y, 'use', [])):
  216. if self.env["STLIB_" + x]:
  217. continue
  218. try:
  219. p[x].append(name)
  220. except KeyError:
  221. p[x] = [name]
  222. self.use_rec(x, objects=objects, stlib=stlib)
  223. @feature('c', 'cxx', 'd', 'use', 'fc')
  224. @before_method('apply_incpaths', 'propagate_uselib_vars')
  225. @after_method('apply_link', 'process_source')
  226. def process_use(self):
  227. """
  228. Process the ``use`` attribute which contains a list of task generator names::
  229. def build(bld):
  230. bld.shlib(source='a.c', target='lib1')
  231. bld.program(source='main.c', target='app', use='lib1')
  232. See :py:func:`waflib.Tools.ccroot.use_rec`.
  233. """
  234. use_not = self.tmp_use_not = set([])
  235. self.tmp_use_seen = [] # we would like an ordered set
  236. use_prec = self.tmp_use_prec = {}
  237. self.uselib = self.to_list(getattr(self, 'uselib', []))
  238. self.includes = self.to_list(getattr(self, 'includes', []))
  239. names = self.to_list(getattr(self, 'use', []))
  240. for x in names:
  241. self.use_rec(x)
  242. for x in use_not:
  243. if x in use_prec:
  244. del use_prec[x]
  245. # topological sort
  246. out = []
  247. tmp = []
  248. for x in self.tmp_use_seen:
  249. for k in use_prec.values():
  250. if x in k:
  251. break
  252. else:
  253. tmp.append(x)
  254. while tmp:
  255. e = tmp.pop()
  256. out.append(e)
  257. try:
  258. nlst = use_prec[e]
  259. except KeyError:
  260. pass
  261. else:
  262. del use_prec[e]
  263. for x in nlst:
  264. for y in use_prec:
  265. if x in use_prec[y]:
  266. break
  267. else:
  268. tmp.append(x)
  269. if use_prec:
  270. raise Errors.WafError('Cycle detected in the use processing %r' % use_prec)
  271. out.reverse()
  272. link_task = getattr(self, 'link_task', None)
  273. for x in out:
  274. y = self.bld.get_tgen_by_name(x)
  275. var = y.tmp_use_var
  276. if var and link_task:
  277. if var == 'LIB' or y.tmp_use_stlib or x in names:
  278. self.env.append_value(var, [y.target[y.target.rfind(os.sep) + 1:]])
  279. self.link_task.dep_nodes.extend(y.link_task.outputs)
  280. tmp_path = y.link_task.outputs[0].parent.path_from(self.bld.bldnode)
  281. self.env.append_unique(var + 'PATH', [tmp_path])
  282. else:
  283. if y.tmp_use_objects:
  284. self.add_objects_from_tgen(y)
  285. if getattr(y, 'export_includes', None):
  286. self.includes.extend(y.to_incnodes(y.export_includes))
  287. if getattr(y, 'export_defines', None):
  288. self.env.append_value('DEFINES', self.to_list(y.export_defines))
  289. # and finally, add the use variables (no recursion needed)
  290. for x in names:
  291. try:
  292. y = self.bld.get_tgen_by_name(x)
  293. except Errors.WafError:
  294. if not self.env['STLIB_' + x] and not x in self.uselib:
  295. self.uselib.append(x)
  296. else:
  297. for k in self.to_list(getattr(y, 'use', [])):
  298. if not self.env['STLIB_' + k] and not k in self.uselib:
  299. self.uselib.append(k)
  300. @taskgen_method
  301. def accept_node_to_link(self, node):
  302. """
  303. PRIVATE INTERNAL USE ONLY
  304. """
  305. return not node.name.endswith('.pdb')
  306. @taskgen_method
  307. def add_objects_from_tgen(self, tg):
  308. """
  309. Add the objects from the depending compiled tasks as link task inputs.
  310. Some objects are filtered: for instance, .pdb files are added
  311. to the compiled tasks but not to the link tasks (to avoid errors)
  312. PRIVATE INTERNAL USE ONLY
  313. """
  314. try:
  315. link_task = self.link_task
  316. except AttributeError:
  317. pass
  318. else:
  319. for tsk in getattr(tg, 'compiled_tasks', []):
  320. for x in tsk.outputs:
  321. if self.accept_node_to_link(x):
  322. link_task.inputs.append(x)
  323. @taskgen_method
  324. def get_uselib_vars(self):
  325. """
  326. :return: the *uselib* variables associated to the *features* attribute (see :py:attr:`waflib.Tools.ccroot.USELIB_VARS`)
  327. :rtype: list of string
  328. """
  329. _vars = set([])
  330. for x in self.features:
  331. if x in USELIB_VARS:
  332. _vars |= USELIB_VARS[x]
  333. return _vars
  334. @feature('c', 'cxx', 'd', 'fc', 'javac', 'cs', 'uselib', 'asm')
  335. @after_method('process_use')
  336. def propagate_uselib_vars(self):
  337. """
  338. Process uselib variables for adding flags. For example, the following target::
  339. def build(bld):
  340. bld.env.AFLAGS_aaa = ['bar']
  341. from waflib.Tools.ccroot import USELIB_VARS
  342. USELIB_VARS['aaa'] = set('AFLAGS')
  343. tg = bld(features='aaa', aflags='test')
  344. The *aflags* attribute will be processed and this method will set::
  345. tg.env.AFLAGS = ['bar', 'test']
  346. """
  347. _vars = self.get_uselib_vars()
  348. env = self.env
  349. app = env.append_value
  350. feature_uselib = self.features + self.to_list(getattr(self, 'uselib', []))
  351. for var in _vars:
  352. y = var.lower()
  353. val = getattr(self, y, [])
  354. if val:
  355. app(var, self.to_list(val))
  356. for x in feature_uselib:
  357. val = env['%s_%s' % (var, x)]
  358. if val:
  359. app(var, val)
  360. # ============ the code above must not know anything about import libs ==========
  361. @feature('cshlib', 'cxxshlib', 'fcshlib')
  362. @after_method('apply_link')
  363. def apply_implib(self):
  364. """
  365. Handle dlls and their import libs on Windows-like systems.
  366. A ``.dll.a`` file called *import library* is generated.
  367. It must be installed as it is required for linking the library.
  368. """
  369. if not self.env.DEST_BINFMT == 'pe':
  370. return
  371. dll = self.link_task.outputs[0]
  372. if isinstance(self.target, Node.Node):
  373. name = self.target.name
  374. else:
  375. name = os.path.split(self.target)[1]
  376. implib = self.env['implib_PATTERN'] % name
  377. implib = dll.parent.find_or_declare(implib)
  378. self.env.append_value('LINKFLAGS', self.env['IMPLIB_ST'] % implib.bldpath())
  379. self.link_task.outputs.append(implib)
  380. if getattr(self, 'defs', None) and self.env.DEST_BINFMT == 'pe':
  381. node = self.path.find_resource(self.defs)
  382. if not node:
  383. raise Errors.WafError('invalid def file %r' % self.defs)
  384. if 'msvc' in (self.env.CC_NAME, self.env.CXX_NAME):
  385. self.env.append_value('LINKFLAGS', '/def:%s' % node.path_from(self.bld.bldnode))
  386. self.link_task.dep_nodes.append(node)
  387. else:
  388. #gcc for windows takes *.def file a an input without any special flag
  389. self.link_task.inputs.append(node)
  390. # where to put the import library
  391. if getattr(self, 'install_task', None):
  392. try:
  393. # user has given a specific installation path for the import library
  394. inst_to = self.install_path_implib
  395. except AttributeError:
  396. try:
  397. # user has given an installation path for the main library, put the import library in it
  398. inst_to = self.install_path
  399. except AttributeError:
  400. # else, put the library in BINDIR and the import library in LIBDIR
  401. inst_to = '${IMPLIBDIR}'
  402. self.install_task.dest = '${BINDIR}'
  403. if not self.env.IMPLIBDIR:
  404. self.env.IMPLIBDIR = self.env.LIBDIR
  405. self.implib_install_task = self.bld.install_files(inst_to, implib, env=self.env, chmod=self.link_task.chmod, task=self.link_task)
  406. # ============ the code above must not know anything about vnum processing on unix platforms =========
  407. re_vnum = re.compile('^([1-9]\\d*|0)([.]([1-9]\\d*|0)){0,2}?$')
  408. @feature('cshlib', 'cxxshlib', 'dshlib', 'fcshlib', 'vnum')
  409. @after_method('apply_link', 'propagate_uselib_vars')
  410. def apply_vnum(self):
  411. """
  412. Enforce version numbering on shared libraries. The valid version numbers must have either zero or two dots::
  413. def build(bld):
  414. bld.shlib(source='a.c', target='foo', vnum='14.15.16')
  415. In this example, ``libfoo.so`` is installed as ``libfoo.so.1.2.3``, and the following symbolic links are created:
  416. * ``libfoo.so → libfoo.so.1.2.3``
  417. * ``libfoo.so.1 → libfoo.so.1.2.3``
  418. """
  419. if not getattr(self, 'vnum', '') or os.name != 'posix' or self.env.DEST_BINFMT not in ('elf', 'mac-o'):
  420. return
  421. link = self.link_task
  422. if not re_vnum.match(self.vnum):
  423. raise Errors.WafError('Invalid vnum %r for target %r' % (self.vnum, getattr(self, 'name', self)))
  424. nums = self.vnum.split('.')
  425. node = link.outputs[0]
  426. libname = node.name
  427. if libname.endswith('.dylib'):
  428. name3 = libname.replace('.dylib', '.%s.dylib' % self.vnum)
  429. name2 = libname.replace('.dylib', '.%s.dylib' % nums[0])
  430. else:
  431. name3 = libname + '.' + self.vnum
  432. name2 = libname + '.' + nums[0]
  433. # add the so name for the ld linker - to disable, just unset env.SONAME_ST
  434. if self.env.SONAME_ST:
  435. v = self.env.SONAME_ST % name2
  436. self.env.append_value('LINKFLAGS', v.split())
  437. # the following task is just to enable execution from the build dir :-/
  438. if self.env.DEST_OS != 'openbsd':
  439. outs = [node.parent.find_or_declare(name3)]
  440. if name2 != name3:
  441. outs.append(node.parent.find_or_declare(name2))
  442. self.create_task('vnum', node, outs)
  443. if getattr(self, 'install_task', None):
  444. self.install_task.hasrun = Task.SKIP_ME
  445. bld = self.bld
  446. path = self.install_task.dest
  447. if self.env.DEST_OS == 'openbsd':
  448. libname = self.link_task.outputs[0].name
  449. t1 = bld.install_as('%s%s%s' % (path, os.sep, libname), node, env=self.env, chmod=self.link_task.chmod)
  450. self.vnum_install_task = (t1,)
  451. else:
  452. t1 = bld.install_as(path + os.sep + name3, node, env=self.env, chmod=self.link_task.chmod)
  453. t3 = bld.symlink_as(path + os.sep + libname, name3)
  454. if name2 != name3:
  455. t2 = bld.symlink_as(path + os.sep + name2, name3)
  456. self.vnum_install_task = (t1, t2, t3)
  457. else:
  458. self.vnum_install_task = (t1, t3)
  459. if '-dynamiclib' in self.env['LINKFLAGS']:
  460. # this requires after(propagate_uselib_vars)
  461. try:
  462. inst_to = self.install_path
  463. except AttributeError:
  464. inst_to = self.link_task.__class__.inst_to
  465. if inst_to:
  466. p = Utils.subst_vars(inst_to, self.env)
  467. path = os.path.join(p, self.link_task.outputs[0].name)
  468. self.env.append_value('LINKFLAGS', ['-install_name', path])
  469. class vnum(Task.Task):
  470. """
  471. Create the symbolic links for a versioned shared library. Instances are created by :py:func:`waflib.Tools.ccroot.apply_vnum`
  472. """
  473. color = 'CYAN'
  474. quient = True
  475. ext_in = ['.bin']
  476. def keyword(self):
  477. return 'Symlinking'
  478. def run(self):
  479. for x in self.outputs:
  480. path = x.abspath()
  481. try:
  482. os.remove(path)
  483. except OSError:
  484. pass
  485. try:
  486. os.symlink(self.inputs[0].name, path)
  487. except OSError:
  488. return 1
  489. class fake_shlib(link_task):
  490. """
  491. Task used for reading a system library and adding the dependency on it
  492. """
  493. def runnable_status(self):
  494. for t in self.run_after:
  495. if not t.hasrun:
  496. return Task.ASK_LATER
  497. for x in self.outputs:
  498. x.sig = Utils.h_file(x.abspath())
  499. return Task.SKIP_ME
  500. class fake_stlib(stlink_task):
  501. """
  502. Task used for reading a system library and adding the dependency on it
  503. """
  504. def runnable_status(self):
  505. for t in self.run_after:
  506. if not t.hasrun:
  507. return Task.ASK_LATER
  508. for x in self.outputs:
  509. x.sig = Utils.h_file(x.abspath())
  510. return Task.SKIP_ME
  511. @conf
  512. def read_shlib(self, name, paths=[], export_includes=[], export_defines=[]):
  513. """
  514. Read a system shared library, enabling its use as a local library. Will trigger a rebuild if the file changes::
  515. def build(bld):
  516. bld.read_shlib('m')
  517. bld.program(source='main.c', use='m')
  518. """
  519. return self(name=name, features='fake_lib', lib_paths=paths, lib_type='shlib', export_includes=export_includes, export_defines=export_defines)
  520. @conf
  521. def read_stlib(self, name, paths=[], export_includes=[], export_defines=[]):
  522. """
  523. Read a system static library, enabling a use as a local library. Will trigger a rebuild if the file changes.
  524. """
  525. return self(name=name, features='fake_lib', lib_paths=paths, lib_type='stlib', export_includes=export_includes, export_defines=export_defines)
  526. lib_patterns = {
  527. 'shlib' : ['lib%s.so', '%s.so', 'lib%s.dylib', 'lib%s.dll', '%s.dll'],
  528. 'stlib' : ['lib%s.a', '%s.a', 'lib%s.dll', '%s.dll', 'lib%s.lib', '%s.lib'],
  529. }
  530. @feature('fake_lib')
  531. def process_lib(self):
  532. """
  533. Find the location of a foreign library. Used by :py:class:`waflib.Tools.ccroot.read_shlib` and :py:class:`waflib.Tools.ccroot.read_stlib`.
  534. """
  535. node = None
  536. names = [x % self.name for x in lib_patterns[self.lib_type]]
  537. for x in self.lib_paths + [self.path] + SYSTEM_LIB_PATHS:
  538. if not isinstance(x, Node.Node):
  539. x = self.bld.root.find_node(x) or self.path.find_node(x)
  540. if not x:
  541. continue
  542. for y in names:
  543. node = x.find_node(y)
  544. if node:
  545. node.sig = Utils.h_file(node.abspath())
  546. break
  547. else:
  548. continue
  549. break
  550. else:
  551. raise Errors.WafError('could not find library %r' % self.name)
  552. self.link_task = self.create_task('fake_%s' % self.lib_type, [], [node])
  553. self.target = self.name
  554. class fake_o(Task.Task):
  555. def runnable_status(self):
  556. return Task.SKIP_ME
  557. @extension('.o', '.obj')
  558. def add_those_o_files(self, node):
  559. tsk = self.create_task('fake_o', [], node)
  560. try:
  561. self.compiled_tasks.append(tsk)
  562. except AttributeError:
  563. self.compiled_tasks = [tsk]
  564. @feature('fake_obj')
  565. @before_method('process_source')
  566. def process_objs(self):
  567. """
  568. Puts object files in the task generator outputs
  569. """
  570. for node in self.to_nodes(self.source):
  571. self.add_those_o_files(node)
  572. self.source = []
  573. @conf
  574. def read_object(self, obj):
  575. """
  576. Read an object file, enabling injection in libs/programs. Will trigger a rebuild if the file changes.
  577. :param obj: object file path, as string or Node
  578. """
  579. if not isinstance(obj, self.path.__class__):
  580. obj = self.path.find_resource(obj)
  581. return self(features='fake_obj', source=obj, name=obj.name)
  582. @feature('cxxprogram', 'cprogram')
  583. @after_method('apply_link', 'process_use')
  584. def set_full_paths_hpux(self):
  585. """
  586. On hp-ux, extend the libpaths and static library paths to absolute paths
  587. """
  588. if self.env.DEST_OS != 'hp-ux':
  589. return
  590. base = self.bld.bldnode.abspath()
  591. for var in ['LIBPATH', 'STLIBPATH']:
  592. lst = []
  593. for x in self.env[var]:
  594. if x.startswith('/'):
  595. lst.append(x)
  596. else:
  597. lst.append(os.path.normpath(os.path.join(base, x)))
  598. self.env[var] = lst