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.

223 lines
6.5KB

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2006-2010 (ita)
  4. """
  5. C# support. A simple example::
  6. def configure(conf):
  7. conf.load('cs')
  8. def build(bld):
  9. bld(features='cs', source='main.cs', gen='foo')
  10. Note that the configuration may compile C# snippets::
  11. FRAG = '''
  12. namespace Moo {
  13. public class Test { public static int Main(string[] args) { return 0; } }
  14. }'''
  15. def configure(conf):
  16. conf.check(features='cs', fragment=FRAG, compile_filename='test.cs', gen='test.exe',
  17. bintype='exe', csflags=['-pkg:gtk-sharp-2.0'], msg='Checking for Gtksharp support')
  18. """
  19. from waflib import Utils, Task, Options, Logs, Errors
  20. from waflib.TaskGen import before_method, after_method, feature
  21. from waflib.Tools import ccroot
  22. from waflib.Configure import conf
  23. import os, tempfile
  24. ccroot.USELIB_VARS['cs'] = set(['CSFLAGS', 'ASSEMBLIES', 'RESOURCES'])
  25. ccroot.lib_patterns['csshlib'] = ['%s']
  26. @feature('cs')
  27. @before_method('process_source')
  28. def apply_cs(self):
  29. """
  30. Create a C# task bound to the attribute *cs_task*. There can be only one C# task by task generator.
  31. """
  32. cs_nodes = []
  33. no_nodes = []
  34. for x in self.to_nodes(self.source):
  35. if x.name.endswith('.cs'):
  36. cs_nodes.append(x)
  37. else:
  38. no_nodes.append(x)
  39. self.source = no_nodes
  40. bintype = getattr(self, 'bintype', self.gen.endswith('.dll') and 'library' or 'exe')
  41. self.cs_task = tsk = self.create_task('mcs', cs_nodes, self.path.find_or_declare(self.gen))
  42. tsk.env.CSTYPE = '/target:%s' % bintype
  43. tsk.env.OUT = '/out:%s' % tsk.outputs[0].abspath()
  44. self.env.append_value('CSFLAGS', '/platform:%s' % getattr(self, 'platform', 'anycpu'))
  45. inst_to = getattr(self, 'install_path', bintype=='exe' and '${BINDIR}' or '${LIBDIR}')
  46. if inst_to:
  47. # note: we are making a copy, so the files added to cs_task.outputs won't be installed automatically
  48. mod = getattr(self, 'chmod', bintype=='exe' and Utils.O755 or Utils.O644)
  49. self.install_task = self.bld.install_files(inst_to, self.cs_task.outputs[:], env=self.env, chmod=mod)
  50. @feature('cs')
  51. @after_method('apply_cs')
  52. def use_cs(self):
  53. """
  54. C# applications honor the **use** keyword::
  55. def build(bld):
  56. bld(features='cs', source='My.cs', bintype='library', gen='my.dll', name='mylib')
  57. bld(features='cs', source='Hi.cs', includes='.', bintype='exe', gen='hi.exe', use='mylib', name='hi')
  58. """
  59. names = self.to_list(getattr(self, 'use', []))
  60. get = self.bld.get_tgen_by_name
  61. for x in names:
  62. try:
  63. y = get(x)
  64. except Errors.WafError:
  65. self.env.append_value('CSFLAGS', '/reference:%s' % x)
  66. continue
  67. y.post()
  68. tsk = getattr(y, 'cs_task', None) or getattr(y, 'link_task', None)
  69. if not tsk:
  70. self.bld.fatal('cs task has no link task for use %r' % self)
  71. self.cs_task.dep_nodes.extend(tsk.outputs) # dependency
  72. self.cs_task.set_run_after(tsk) # order (redundant, the order is infered from the nodes inputs/outputs)
  73. self.env.append_value('CSFLAGS', '/reference:%s' % tsk.outputs[0].abspath())
  74. @feature('cs')
  75. @after_method('apply_cs', 'use_cs')
  76. def debug_cs(self):
  77. """
  78. The C# targets may create .mdb or .pdb files::
  79. def build(bld):
  80. bld(features='cs', source='My.cs', bintype='library', gen='my.dll', csdebug='full')
  81. # csdebug is a value in (True, 'full', 'pdbonly')
  82. """
  83. csdebug = getattr(self, 'csdebug', self.env.CSDEBUG)
  84. if not csdebug:
  85. return
  86. node = self.cs_task.outputs[0]
  87. if self.env.CS_NAME == 'mono':
  88. out = node.parent.find_or_declare(node.name + '.mdb')
  89. else:
  90. out = node.change_ext('.pdb')
  91. self.cs_task.outputs.append(out)
  92. try:
  93. self.install_task.source.append(out)
  94. except AttributeError:
  95. pass
  96. if csdebug == 'pdbonly':
  97. val = ['/debug+', '/debug:pdbonly']
  98. elif csdebug == 'full':
  99. val = ['/debug+', '/debug:full']
  100. else:
  101. val = ['/debug-']
  102. self.env.append_value('CSFLAGS', val)
  103. class mcs(Task.Task):
  104. """
  105. Compile C# files
  106. """
  107. color = 'YELLOW'
  108. run_str = '${MCS} ${CSTYPE} ${CSFLAGS} ${ASS_ST:ASSEMBLIES} ${RES_ST:RESOURCES} ${OUT} ${SRC}'
  109. def exec_command(self, cmd, **kw):
  110. bld = self.generator.bld
  111. try:
  112. if not kw.get('cwd', None):
  113. kw['cwd'] = bld.cwd
  114. except AttributeError:
  115. bld.cwd = kw['cwd'] = bld.variant_dir
  116. try:
  117. tmp = None
  118. if isinstance(cmd, list) and len(' '.join(cmd)) >= 8192:
  119. program = cmd[0] #unquoted program name, otherwise exec_command will fail
  120. cmd = [self.quote_response_command(x) for x in cmd]
  121. (fd, tmp) = tempfile.mkstemp()
  122. os.write(fd, '\r\n'.join(i.replace('\\', '\\\\') for i in cmd[1:]).encode())
  123. os.close(fd)
  124. cmd = [program, '@' + tmp]
  125. # no return here, that's on purpose
  126. ret = self.generator.bld.exec_command(cmd, **kw)
  127. finally:
  128. if tmp:
  129. try:
  130. os.remove(tmp)
  131. except OSError:
  132. pass # anti-virus and indexers can keep the files open -_-
  133. return ret
  134. def quote_response_command(self, flag):
  135. # /noconfig is not allowed when using response files
  136. if flag.lower() == '/noconfig':
  137. return ''
  138. if flag.find(' ') > -1:
  139. for x in ('/r:', '/reference:', '/resource:', '/lib:', '/out:'):
  140. if flag.startswith(x):
  141. flag = '%s"%s"' % (x, '","'.join(flag[len(x):].split(',')))
  142. break
  143. else:
  144. flag = '"%s"' % flag
  145. return flag
  146. def configure(conf):
  147. """
  148. Find a C# compiler, set the variable MCS for the compiler and CS_NAME (mono or csc)
  149. """
  150. csc = getattr(Options.options, 'cscbinary', None)
  151. if csc:
  152. conf.env.MCS = csc
  153. conf.find_program(['csc', 'mcs', 'gmcs'], var='MCS')
  154. conf.env.ASS_ST = '/r:%s'
  155. conf.env.RES_ST = '/resource:%s'
  156. conf.env.CS_NAME = 'csc'
  157. if str(conf.env.MCS).lower().find('mcs') > -1:
  158. conf.env.CS_NAME = 'mono'
  159. def options(opt):
  160. """
  161. Add a command-line option for the configuration::
  162. $ waf configure --with-csc-binary=/foo/bar/mcs
  163. """
  164. opt.add_option('--with-csc-binary', type='string', dest='cscbinary')
  165. class fake_csshlib(Task.Task):
  166. """
  167. Task used for reading a foreign .net assembly and adding the dependency on it
  168. """
  169. color = 'YELLOW'
  170. inst_to = None
  171. def runnable_status(self):
  172. for x in self.outputs:
  173. x.sig = Utils.h_file(x.abspath())
  174. return Task.SKIP_ME
  175. @conf
  176. def read_csshlib(self, name, paths=[]):
  177. """
  178. Read a foreign .net assembly for the *use* system::
  179. def build(bld):
  180. bld.read_csshlib('ManagedLibrary.dll', paths=[bld.env.mylibrarypath])
  181. bld(features='cs', source='Hi.cs', bintype='exe', gen='hi.exe', use='ManagedLibrary.dll')
  182. :param name: Name of the library
  183. :type name: string
  184. :param paths: Folders in which the library may be found
  185. :type paths: list of string
  186. :return: A task generator having the feature *fake_lib* which will call :py:func:`waflib.Tools.ccroot.process_lib`
  187. :rtype: :py:class:`waflib.TaskGen.task_gen`
  188. """
  189. return self(name=name, features='fake_lib', lib_paths=paths, lib_type='csshlib')