cs.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2006-2018 (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, Errors
  20. from waflib.TaskGen import before_method, after_method, feature
  21. from waflib.Tools import ccroot
  22. from waflib.Configure import conf
  23. ccroot.USELIB_VARS['cs'] = set(['CSFLAGS', 'ASSEMBLIES', 'RESOURCES'])
  24. ccroot.lib_patterns['csshlib'] = ['%s']
  25. @feature('cs')
  26. @before_method('process_source')
  27. def apply_cs(self):
  28. """
  29. Create a C# task bound to the attribute *cs_task*. There can be only one C# task by task generator.
  30. """
  31. cs_nodes = []
  32. no_nodes = []
  33. for x in self.to_nodes(self.source):
  34. if x.name.endswith('.cs'):
  35. cs_nodes.append(x)
  36. else:
  37. no_nodes.append(x)
  38. self.source = no_nodes
  39. bintype = getattr(self, 'bintype', self.gen.endswith('.dll') and 'library' or 'exe')
  40. self.cs_task = tsk = self.create_task('mcs', cs_nodes, self.path.find_or_declare(self.gen))
  41. tsk.env.CSTYPE = '/target:%s' % bintype
  42. tsk.env.OUT = '/out:%s' % tsk.outputs[0].abspath()
  43. self.env.append_value('CSFLAGS', '/platform:%s' % getattr(self, 'platform', 'anycpu'))
  44. inst_to = getattr(self, 'install_path', bintype=='exe' and '${BINDIR}' or '${LIBDIR}')
  45. if inst_to:
  46. # note: we are making a copy, so the files added to cs_task.outputs won't be installed automatically
  47. mod = getattr(self, 'chmod', bintype=='exe' and Utils.O755 or Utils.O644)
  48. self.install_task = self.add_install_files(install_to=inst_to, install_from=self.cs_task.outputs[:], chmod=mod)
  49. @feature('cs')
  50. @after_method('apply_cs')
  51. def use_cs(self):
  52. """
  53. C# applications honor the **use** keyword::
  54. def build(bld):
  55. bld(features='cs', source='My.cs', bintype='library', gen='my.dll', name='mylib')
  56. bld(features='cs', source='Hi.cs', includes='.', bintype='exe', gen='hi.exe', use='mylib', name='hi')
  57. """
  58. names = self.to_list(getattr(self, 'use', []))
  59. get = self.bld.get_tgen_by_name
  60. for x in names:
  61. try:
  62. y = get(x)
  63. except Errors.WafError:
  64. self.env.append_value('CSFLAGS', '/reference:%s' % x)
  65. continue
  66. y.post()
  67. tsk = getattr(y, 'cs_task', None) or getattr(y, 'link_task', None)
  68. if not tsk:
  69. self.bld.fatal('cs task has no link task for use %r' % self)
  70. self.cs_task.dep_nodes.extend(tsk.outputs) # dependency
  71. self.cs_task.set_run_after(tsk) # order (redundant, the order is inferred from the nodes inputs/outputs)
  72. self.env.append_value('CSFLAGS', '/reference:%s' % tsk.outputs[0].abspath())
  73. @feature('cs')
  74. @after_method('apply_cs', 'use_cs')
  75. def debug_cs(self):
  76. """
  77. The C# targets may create .mdb or .pdb files::
  78. def build(bld):
  79. bld(features='cs', source='My.cs', bintype='library', gen='my.dll', csdebug='full')
  80. # csdebug is a value in (True, 'full', 'pdbonly')
  81. """
  82. csdebug = getattr(self, 'csdebug', self.env.CSDEBUG)
  83. if not csdebug:
  84. return
  85. node = self.cs_task.outputs[0]
  86. if self.env.CS_NAME == 'mono':
  87. out = node.parent.find_or_declare(node.name + '.mdb')
  88. else:
  89. out = node.change_ext('.pdb')
  90. self.cs_task.outputs.append(out)
  91. if getattr(self, 'install_task', None):
  92. self.pdb_install_task = self.add_install_files(
  93. install_to=self.install_task.install_to, install_from=out)
  94. if csdebug == 'pdbonly':
  95. val = ['/debug+', '/debug:pdbonly']
  96. elif csdebug == 'full':
  97. val = ['/debug+', '/debug:full']
  98. else:
  99. val = ['/debug-']
  100. self.env.append_value('CSFLAGS', val)
  101. @feature('cs')
  102. @after_method('debug_cs')
  103. def doc_cs(self):
  104. """
  105. The C# targets may create .xml documentation files::
  106. def build(bld):
  107. bld(features='cs', source='My.cs', bintype='library', gen='my.dll', csdoc=True)
  108. # csdoc is a boolean value
  109. """
  110. csdoc = getattr(self, 'csdoc', self.env.CSDOC)
  111. if not csdoc:
  112. return
  113. node = self.cs_task.outputs[0]
  114. out = node.change_ext('.xml')
  115. self.cs_task.outputs.append(out)
  116. if getattr(self, 'install_task', None):
  117. self.doc_install_task = self.add_install_files(
  118. install_to=self.install_task.install_to, install_from=out)
  119. self.env.append_value('CSFLAGS', '/doc:%s' % out.abspath())
  120. class mcs(Task.Task):
  121. """
  122. Compile C# files
  123. """
  124. color = 'YELLOW'
  125. run_str = '${MCS} ${CSTYPE} ${CSFLAGS} ${ASS_ST:ASSEMBLIES} ${RES_ST:RESOURCES} ${OUT} ${SRC}'
  126. def split_argfile(self, cmd):
  127. inline = [cmd[0]]
  128. infile = []
  129. for x in cmd[1:]:
  130. # csc doesn't want /noconfig in @file
  131. if x.lower() == '/noconfig':
  132. inline.append(x)
  133. else:
  134. infile.append(self.quote_flag(x))
  135. return (inline, infile)
  136. def configure(conf):
  137. """
  138. Find a C# compiler, set the variable MCS for the compiler and CS_NAME (mono or csc)
  139. """
  140. csc = getattr(Options.options, 'cscbinary', None)
  141. if csc:
  142. conf.env.MCS = csc
  143. conf.find_program(['csc', 'mcs', 'gmcs'], var='MCS')
  144. conf.env.ASS_ST = '/r:%s'
  145. conf.env.RES_ST = '/resource:%s'
  146. conf.env.CS_NAME = 'csc'
  147. if str(conf.env.MCS).lower().find('mcs') > -1:
  148. conf.env.CS_NAME = 'mono'
  149. def options(opt):
  150. """
  151. Add a command-line option for the configuration::
  152. $ waf configure --with-csc-binary=/foo/bar/mcs
  153. """
  154. opt.add_option('--with-csc-binary', type='string', dest='cscbinary')
  155. class fake_csshlib(Task.Task):
  156. """
  157. Task used for reading a foreign .net assembly and adding the dependency on it
  158. """
  159. color = 'YELLOW'
  160. inst_to = None
  161. def runnable_status(self):
  162. return Task.SKIP_ME
  163. @conf
  164. def read_csshlib(self, name, paths=[]):
  165. """
  166. Read a foreign .net assembly for the *use* system::
  167. def build(bld):
  168. bld.read_csshlib('ManagedLibrary.dll', paths=[bld.env.mylibrarypath])
  169. bld(features='cs', source='Hi.cs', bintype='exe', gen='hi.exe', use='ManagedLibrary.dll')
  170. :param name: Name of the library
  171. :type name: string
  172. :param paths: Folders in which the library may be found
  173. :type paths: list of string
  174. :return: A task generator having the feature *fake_lib* which will call :py:func:`waflib.Tools.ccroot.process_lib`
  175. :rtype: :py:class:`waflib.TaskGen.task_gen`
  176. """
  177. return self(name=name, features='fake_lib', lib_paths=paths, lib_type='csshlib')