b23b77f91f29311b9c42a82fab65a7d6b4028298
[bbaumbach/samba-autobuild/.git] / third_party / waf / waflib / Tools / cs.py
1 #! /usr/bin/env python
2 # encoding: utf-8
3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
4
5 #!/usr/bin/env python
6 # encoding: utf-8
7 # Thomas Nagy, 2006-2018 (ita)
8
9 """
10 C# support. A simple example::
11
12         def configure(conf):
13                 conf.load('cs')
14         def build(bld):
15                 bld(features='cs', source='main.cs', gen='foo')
16
17 Note that the configuration may compile C# snippets::
18
19         FRAG = '''
20         namespace Moo {
21                 public class Test { public static int Main(string[] args) { return 0; } }
22         }'''
23         def configure(conf):
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')
26 """
27
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
32
33 ccroot.USELIB_VARS['cs'] = set(['CSFLAGS', 'ASSEMBLIES', 'RESOURCES'])
34 ccroot.lib_patterns['csshlib'] = ['%s']
35
36 @feature('cs')
37 @before_method('process_source')
38 def apply_cs(self):
39         """
40         Create a C# task bound to the attribute *cs_task*. There can be only one C# task by task generator.
41         """
42         cs_nodes = []
43         no_nodes = []
44         for x in self.to_nodes(self.source):
45                 if x.name.endswith('.cs'):
46                         cs_nodes.append(x)
47                 else:
48                         no_nodes.append(x)
49         self.source = no_nodes
50
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'))
56
57         inst_to = getattr(self, 'install_path', bintype=='exe' and '${BINDIR}' or '${LIBDIR}')
58         if inst_to:
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)
62
63 @feature('cs')
64 @after_method('apply_cs')
65 def use_cs(self):
66         """
67         C# applications honor the **use** keyword::
68
69                 def build(bld):
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')
72         """
73         names = self.to_list(getattr(self, 'use', []))
74         get = self.bld.get_tgen_by_name
75         for x in names:
76                 try:
77                         y = get(x)
78                 except Errors.WafError:
79                         self.env.append_value('CSFLAGS', '/reference:%s' % x)
80                         continue
81                 y.post()
82
83                 tsk = getattr(y, 'cs_task', None) or getattr(y, 'link_task', None)
84                 if not tsk:
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())
89
90 @feature('cs')
91 @after_method('apply_cs', 'use_cs')
92 def debug_cs(self):
93         """
94         The C# targets may create .mdb or .pdb files::
95
96                 def build(bld):
97                         bld(features='cs', source='My.cs', bintype='library', gen='my.dll', csdebug='full')
98                         # csdebug is a value in (True, 'full', 'pdbonly')
99         """
100         csdebug = getattr(self, 'csdebug', self.env.CSDEBUG)
101         if not csdebug:
102                 return
103
104         node = self.cs_task.outputs[0]
105         if self.env.CS_NAME == 'mono':
106                 out = node.parent.find_or_declare(node.name + '.mdb')
107         else:
108                 out = node.change_ext('.pdb')
109         self.cs_task.outputs.append(out)
110
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)
114
115         if csdebug == 'pdbonly':
116                 val = ['/debug+', '/debug:pdbonly']
117         elif csdebug == 'full':
118                 val = ['/debug+', '/debug:full']
119         else:
120                 val = ['/debug-']
121         self.env.append_value('CSFLAGS', val)
122
123 @feature('cs')
124 @after_method('debug_cs')
125 def doc_cs(self):
126         """
127         The C# targets may create .xml documentation files::
128
129                 def build(bld):
130                         bld(features='cs', source='My.cs', bintype='library', gen='my.dll', csdoc=True)
131                         # csdoc is a boolean value
132         """
133         csdoc = getattr(self, 'csdoc', self.env.CSDOC)
134         if not csdoc:
135                 return
136
137         node = self.cs_task.outputs[0]
138         out = node.change_ext('.xml')
139         self.cs_task.outputs.append(out)
140
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)
144
145         self.env.append_value('CSFLAGS', '/doc:%s' % out.abspath())
146
147 class mcs(Task.Task):
148         """
149         Compile C# files
150         """
151         color   = 'YELLOW'
152         run_str = '${MCS} ${CSTYPE} ${CSFLAGS} ${ASS_ST:ASSEMBLIES} ${RES_ST:RESOURCES} ${OUT} ${SRC}'
153
154         def split_argfile(self, cmd):
155                 inline = [cmd[0]]
156                 infile = []
157                 for x in cmd[1:]:
158                         # csc doesn't want /noconfig in @file
159                         if x.lower() == '/noconfig':
160                                 inline.append(x)
161                         else:
162                                 infile.append(self.quote_flag(x))
163                 return (inline, infile)
164
165 def configure(conf):
166         """
167         Find a C# compiler, set the variable MCS for the compiler and CS_NAME (mono or csc)
168         """
169         csc = getattr(Options.options, 'cscbinary', None)
170         if csc:
171                 conf.env.MCS = csc
172         conf.find_program(['csc', 'mcs', 'gmcs'], var='MCS')
173         conf.env.ASS_ST = '/r:%s'
174         conf.env.RES_ST = '/resource:%s'
175
176         conf.env.CS_NAME = 'csc'
177         if str(conf.env.MCS).lower().find('mcs') > -1:
178                 conf.env.CS_NAME = 'mono'
179
180 def options(opt):
181         """
182         Add a command-line option for the configuration::
183
184                 $ waf configure --with-csc-binary=/foo/bar/mcs
185         """
186         opt.add_option('--with-csc-binary', type='string', dest='cscbinary')
187
188 class fake_csshlib(Task.Task):
189         """
190         Task used for reading a foreign .net assembly and adding the dependency on it
191         """
192         color   = 'YELLOW'
193         inst_to = None
194
195         def runnable_status(self):
196                 return Task.SKIP_ME
197
198 @conf
199 def read_csshlib(self, name, paths=[]):
200         """
201         Read a foreign .net assembly for the *use* system::
202
203                 def build(bld):
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')
206
207         :param name: Name of the library
208         :type name: string
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`
213         """
214         return self(name=name, features='fake_lib', lib_paths=paths, lib_type='csshlib')
215