3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
7 # Thomas Nagy, 2006-2018 (ita)
10 C# support. A simple example::
15 bld(features='cs', source='main.cs', gen='foo')
17 Note that the configuration may compile C# snippets::
21 public class Test { public static int Main(string[] args) { return 0; } }
24 conf.check(features='cs', fragment=FRAG, compile_filename='test.cs', gen='test.exe',
25 bintype='exe', csflags=['-pkg:gtk-sharp-2.0'], msg='Checking for Gtksharp support')
28 from waflib import Utils, Task, Options, Errors
29 from waflib.TaskGen import before_method, after_method, feature
30 from waflib.Tools import ccroot
31 from waflib.Configure import conf
33 ccroot.USELIB_VARS['cs'] = set(['CSFLAGS', 'ASSEMBLIES', 'RESOURCES'])
34 ccroot.lib_patterns['csshlib'] = ['%s']
37 @before_method('process_source')
40 Create a C# task bound to the attribute *cs_task*. There can be only one C# task by task generator.
44 for x in self.to_nodes(self.source):
45 if x.name.endswith('.cs'):
49 self.source = no_nodes
51 bintype = getattr(self, 'bintype', self.gen.endswith('.dll') and 'library' or 'exe')
52 self.cs_task = tsk = self.create_task('mcs', cs_nodes, self.path.find_or_declare(self.gen))
53 tsk.env.CSTYPE = '/target:%s' % bintype
54 tsk.env.OUT = '/out:%s' % tsk.outputs[0].abspath()
55 self.env.append_value('CSFLAGS', '/platform:%s' % getattr(self, 'platform', 'anycpu'))
57 inst_to = getattr(self, 'install_path', bintype=='exe' and '${BINDIR}' or '${LIBDIR}')
59 # note: we are making a copy, so the files added to cs_task.outputs won't be installed automatically
60 mod = getattr(self, 'chmod', bintype=='exe' and Utils.O755 or Utils.O644)
61 self.install_task = self.add_install_files(install_to=inst_to, install_from=self.cs_task.outputs[:], chmod=mod)
64 @after_method('apply_cs')
67 C# applications honor the **use** keyword::
70 bld(features='cs', source='My.cs', bintype='library', gen='my.dll', name='mylib')
71 bld(features='cs', source='Hi.cs', includes='.', bintype='exe', gen='hi.exe', use='mylib', name='hi')
73 names = self.to_list(getattr(self, 'use', []))
74 get = self.bld.get_tgen_by_name
78 except Errors.WafError:
79 self.env.append_value('CSFLAGS', '/reference:%s' % x)
83 tsk = getattr(y, 'cs_task', None) or getattr(y, 'link_task', None)
85 self.bld.fatal('cs task has no link task for use %r' % self)
86 self.cs_task.dep_nodes.extend(tsk.outputs) # dependency
87 self.cs_task.set_run_after(tsk) # order (redundant, the order is inferred from the nodes inputs/outputs)
88 self.env.append_value('CSFLAGS', '/reference:%s' % tsk.outputs[0].abspath())
91 @after_method('apply_cs', 'use_cs')
94 The C# targets may create .mdb or .pdb files::
97 bld(features='cs', source='My.cs', bintype='library', gen='my.dll', csdebug='full')
98 # csdebug is a value in (True, 'full', 'pdbonly')
100 csdebug = getattr(self, 'csdebug', self.env.CSDEBUG)
104 node = self.cs_task.outputs[0]
105 if self.env.CS_NAME == 'mono':
106 out = node.parent.find_or_declare(node.name + '.mdb')
108 out = node.change_ext('.pdb')
109 self.cs_task.outputs.append(out)
111 if getattr(self, 'install_task', None):
112 self.pdb_install_task = self.add_install_files(
113 install_to=self.install_task.install_to, install_from=out)
115 if csdebug == 'pdbonly':
116 val = ['/debug+', '/debug:pdbonly']
117 elif csdebug == 'full':
118 val = ['/debug+', '/debug:full']
121 self.env.append_value('CSFLAGS', val)
124 @after_method('debug_cs')
127 The C# targets may create .xml documentation files::
130 bld(features='cs', source='My.cs', bintype='library', gen='my.dll', csdoc=True)
131 # csdoc is a boolean value
133 csdoc = getattr(self, 'csdoc', self.env.CSDOC)
137 node = self.cs_task.outputs[0]
138 out = node.change_ext('.xml')
139 self.cs_task.outputs.append(out)
141 if getattr(self, 'install_task', None):
142 self.doc_install_task = self.add_install_files(
143 install_to=self.install_task.install_to, install_from=out)
145 self.env.append_value('CSFLAGS', '/doc:%s' % out.abspath())
147 class mcs(Task.Task):
152 run_str = '${MCS} ${CSTYPE} ${CSFLAGS} ${ASS_ST:ASSEMBLIES} ${RES_ST:RESOURCES} ${OUT} ${SRC}'
154 def split_argfile(self, cmd):
158 # csc doesn't want /noconfig in @file
159 if x.lower() == '/noconfig':
162 infile.append(self.quote_flag(x))
163 return (inline, infile)
167 Find a C# compiler, set the variable MCS for the compiler and CS_NAME (mono or csc)
169 csc = getattr(Options.options, 'cscbinary', None)
172 conf.find_program(['csc', 'mcs', 'gmcs'], var='MCS')
173 conf.env.ASS_ST = '/r:%s'
174 conf.env.RES_ST = '/resource:%s'
176 conf.env.CS_NAME = 'csc'
177 if str(conf.env.MCS).lower().find('mcs') > -1:
178 conf.env.CS_NAME = 'mono'
182 Add a command-line option for the configuration::
184 $ waf configure --with-csc-binary=/foo/bar/mcs
186 opt.add_option('--with-csc-binary', type='string', dest='cscbinary')
188 class fake_csshlib(Task.Task):
190 Task used for reading a foreign .net assembly and adding the dependency on it
195 def runnable_status(self):
199 def read_csshlib(self, name, paths=[]):
201 Read a foreign .net assembly for the *use* system::
204 bld.read_csshlib('ManagedLibrary.dll', paths=[bld.env.mylibrarypath])
205 bld(features='cs', source='Hi.cs', bintype='exe', gen='hi.exe', use='ManagedLibrary.dll')
207 :param name: Name of the library
209 :param paths: Folders in which the library may be found
210 :type paths: list of string
211 :return: A task generator having the feature *fake_lib* which will call :py:func:`waflib.Tools.ccroot.process_lib`
212 :rtype: :py:class:`waflib.TaskGen.task_gen`
214 return self(name=name, features='fake_lib', lib_paths=paths, lib_type='csshlib')