-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgcc.py
322 lines (275 loc) · 12.8 KB
/
gcc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env python
"""Ninja toolchain abstraction for GCC compiler suite"""
import os
import toolchain
class GCCToolchain(toolchain.Toolchain):
def initialize(self, project, archs, configs, includepaths, dependlibs, libpaths, variables, subninja):
#Local variable defaults
self.toolchain = ''
self.includepaths = []
self.libpaths = libpaths
self.ccompiler = os.environ.get('CC') or 'gcc'
self.cxxcompiler = os.environ.get('CXX') or 'g++'
self.archiver = os.environ.get('AR') or 'ar'
self.linker = os.environ.get('CC') or 'gcc'
self.cxxlinker = os.environ.get('CXX') or 'g++'
#Command definitions
self.cccmd = '$toolchain$cc -MMD -MT $out -MF $out.d $includepaths $moreincludepaths $cflags $carchflags $cconfigflags $cmoreflags $cenvflags -c $in -o $out'
self.cxxcmd = '$toolchain$cxx -MMD -MT $out -MF $out.d $includepaths $moreincludepaths $cxxflags $carchflags $cconfigflags $cmoreflags $cxxenvflags -c $in -o $out'
self.ccdeps = 'gcc'
self.ccdepfile = '$out.d'
self.arcmd = self.rmcmd('$out') + ' && $toolchain$ar crsD $ararchflags $arflags $arenvflags $out $in'
self.linkcmd = '$toolchain$link $libpaths $configlibpaths $linkflags $linkarchflags $linkconfigflags $linkenvflags -o $out $in $libs $archlibs $oslibs'
#Base flags
self.cflags = ['-D' + project.upper() + '_COMPILE=1',
'-funit-at-a-time', '-fstrict-aliasing',
'-fno-math-errno','-ffinite-math-only', '-funsafe-math-optimizations',
'-fno-trapping-math', '-ffast-math']
self.cwarnflags = ['-Wextra', '-Wall', '-Werror']
self.cmoreflags = []
self.mflags = []
self.arflags = []
self.linkflags = []
self.oslibs = []
self.initialize_subninja(subninja)
self.initialize_archs(archs)
self.initialize_configs(configs)
self.initialize_project(project)
self.initialize_toolchain()
self.initialize_depends(dependlibs)
self.parse_default_variables(variables)
self.read_build_prefs()
if self.target.is_linux() or self.target.is_bsd() or self.target.is_raspberrypi() or self.target.is_sunos():
self.cflags += ['-D_GNU_SOURCE=1']
self.linkflags += ['-pthread']
if self.target.is_linux() or self.target.is_raspberrypi():
self.oslibs += ['dl']
if self.target.is_raspberrypi():
self.linkflags += ['-latomic']
if self.target.is_bsd():
self.oslibs += ['execinfo']
if self.target.is_haiku():
self.cflags += ['-D_GNU_SOURCE=1']
self.linkflags += ['-lpthread']
self.includepaths = self.prefix_includepaths((includepaths or []) + ['.'])
if self.is_monolithic():
self.cflags += ['-DBUILD_MONOLITHIC=1']
if self.use_coverage():
self.cflags += ['--coverage']
self.linkflags += ['--coverage']
if not 'nowarning' in variables or not variables['nowarning']:
self.cflags += self.cwarnflags
self.cxxflags = list(self.cflags)
self.cflags += ['-std=c11']
if self.target.is_macos() or self.target.is_ios():
self.cxxflags += ['-std=c++14', '-stdlib=libc++']
else:
self.cxxflags += ['-std=gnu++14']
#Overrides
self.objext = '.o'
#Builders
self.builders['c'] = self.builder_cc
self.builders['cc'] = self.builder_cxx
self.builders['cpp'] = self.builder_cxx
self.builders['lib'] = self.builder_lib
self.builders['multilib'] = self.builder_multicopy
self.builders['sharedlib'] = self.builder_sharedlib
self.builders['multisharedlib'] = self.builder_multicopy
self.builders['bin'] = self.builder_bin
self.builders['multibin'] = self.builder_multicopy
#Setup target platform
self.build_target_toolchain(self.target)
def name(self):
return 'gcc'
def parse_prefs(self, prefs):
super(GCCToolchain, self).parse_prefs(prefs)
if 'gcc' in prefs:
gccprefs = prefs['gcc']
if 'toolchain' in gccprefs:
self.toolchain = gccprefs['toolchain']
if os.path.split(self.toolchain)[1] != 'bin':
self.toolchain = os.path.join(self.toolchain, 'bin')
def write_variables(self, writer):
super(GCCToolchain, self).write_variables(writer)
writer.variable('toolchain', self.toolchain)
writer.variable('cc', self.ccompiler)
writer.variable('cxx', self.cxxcompiler)
writer.variable('ar', self.archiver)
writer.variable('link', self.linker)
writer.variable('includepaths', self.make_includepaths(self.includepaths))
writer.variable('moreincludepaths', '')
writer.variable('cflags', self.cflags)
writer.variable('cxxflags', self.cxxflags)
writer.variable('carchflags', '')
writer.variable('cconfigflags', '')
writer.variable('cmoreflags', self.cmoreflags)
writer.variable('cenvflags', (os.environ.get('CFLAGS') or '').split())
writer.variable('cxxenvflags', (os.environ.get('CXXFLAGS') or '').split())
writer.variable('arflags', self.arflags)
writer.variable('ararchflags', '')
writer.variable('arconfigflags', '')
writer.variable('arenvflags', (os.environ.get('ARFLAGS') or '').split())
writer.variable('linkflags', self.linkflags)
writer.variable('linkarchflags', '')
writer.variable('linkconfigflags', '')
writer.variable('linkenvflags', (os.environ.get('LDFLAGS') or '').split())
writer.variable('libs', '')
writer.variable('libpaths', self.make_libpaths(self.libpaths))
writer.variable('configlibpaths', '')
writer.variable('archlibs', '')
writer.variable('oslibs', self.make_libs(self.oslibs))
writer.newline()
def write_rules(self, writer):
super(GCCToolchain, self).write_rules(writer)
writer.rule('cc', command = self.cccmd, depfile = self.ccdepfile, deps = self.ccdeps, description = 'CC $in')
writer.rule('cxx', command = self.cxxcmd, depfile = self.ccdepfile, deps = self.ccdeps, description = 'CXX $in')
writer.rule('ar', command = self.arcmd, description = 'LIB $out')
writer.rule('link', command = self.linkcmd, description = 'LINK $out')
writer.rule('so', command = self.linkcmd, description = 'SO $out')
writer.newline()
def build_target_toolchain(self, target):
if target.is_windows():
self.build_windows_toolchain()
if self.toolchain != '' and not self.toolchain.endswith('/') and not self.toolchain.endswith('\\'):
self.toolchain += os.sep
def build_windows_toolchain(self):
self.cflags += ['-U__STRICT_ANSI__']
self.oslibs = ['kernel32', 'user32', 'shell32', 'advapi32']
def make_includepath(self, path):
if os.path.isabs(path) or self.subninja == '':
return self.path_escape(path)
if path == '.':
return self.path_escape(self.subninja)
return self.path_escape(os.path.join(self.subninja, path))
def make_includepaths(self, includepaths):
if not includepaths is None:
return ['-I' + self.make_includepath(path) for path in list(includepaths)]
return []
def make_libpath(self, path):
return self.path_escape(path)
def make_libpaths(self, libpaths):
if not libpaths is None:
return ['-L' + self.make_libpath(path) for path in libpaths]
return []
def make_targetarchflags(self, arch, targettype):
flags = []
if arch == 'x86':
flags += ['-m32']
elif arch == 'x86-64':
flags += ['-m64']
return flags
def make_carchflags(self, arch, targettype):
flags = []
if targettype == 'sharedlib':
flags += ['-DBUILD_DYNAMIC_LINK=1']
if self.target.is_linux() or self.target.is_bsd() or self.target.is_sunos():
flags += ['-fPIC']
flags += self.make_targetarchflags(arch, targettype)
return flags
def make_cconfigflags(self, config, targettype):
flags = []
if config == 'debug':
flags += ['-DBUILD_DEBUG=1', '-g']
elif config == 'release':
flags += ['-DBUILD_RELEASE=1', '-O3', '-g', '-funroll-loops']
elif config == 'profile':
flags += ['-DBUILD_PROFILE=1', '-O3', '-g', '-funroll-loops']
elif config == 'deploy':
flags += ['-DBUILD_DEPLOY=1', '-O3', '-g', '-funroll-loops']
return flags
def make_ararchflags(self, arch, targettype):
flags = []
return flags
def make_arconfigflags(self, config, targettype):
flags = []
return flags
def make_linkarchflags(self, arch, targettype):
flags = []
flags += self.make_targetarchflags(arch, targettype)
return flags
def make_linkconfigflags(self, config, targettype):
flags = []
if self.target.is_windows():
if targettype == 'sharedlib':
flags += ['-Xlinker', '/DLL']
elif targettype == 'bin':
flags += ['-Xlinker', '/SUBSYSTEM:CONSOLE']
elif self.target.is_macos() or self.target.is_ios():
if targettype == 'sharedlib' or targettype == 'multisharedlib':
flags += ['-dynamiclib']
else:
if targettype == 'sharedlib':
flags += ['-shared']
return flags
def make_libs(self, libs):
if libs != None:
return ['-l' + lib for lib in libs]
return []
def make_configlibpaths(self, config, arch, extralibpaths):
libpaths = [
self.libpath,
os.path.join(self.libpath, arch),
os.path.join(self.libpath, config),
os.path.join(self.libpath, config, arch)
]
if extralibpaths != None:
libpaths += [os.path.join(libpath, self.libpath) for libpath in extralibpaths]
libpaths += [os.path.join(libpath, self.libpath, arch) for libpath in extralibpaths]
libpaths += [os.path.join(libpath, self.libpath, config) for libpath in extralibpaths]
libpaths += [os.path.join(libpath, self.libpath, config, arch) for libpath in extralibpaths]
return self.make_libpaths(libpaths)
def cc_variables(self, config, arch, targettype, variables):
localvariables = []
if 'includepaths' in variables:
moreincludepaths = self.make_includepaths(variables['includepaths'])
if not moreincludepaths == []:
localvariables += [('moreincludepaths', moreincludepaths)]
carchflags = self.make_carchflags(arch, targettype)
if carchflags != []:
localvariables += [('carchflags', carchflags)]
cconfigflags = self.make_cconfigflags(config, targettype)
if cconfigflags != []:
localvariables += [('cconfigflags', cconfigflags)]
if 'defines' in variables:
localvariables += [('cmoreflags', ['-D' + define for define in variables['defines']])]
return localvariables
def ar_variables(self, config, arch, targettype, variables):
localvariables = []
ararchflags = self.make_ararchflags(arch, targettype)
if ararchflags != []:
localvariables += [('ararchflags', ararchflags)]
arconfigflags = self.make_arconfigflags(config, targettype)
if arconfigflags != []:
localvariables += [('arconfigflags', arconfigflags)]
return localvariables
def link_variables(self, config, arch, targettype, variables):
localvariables = []
linkarchflags = self.make_linkarchflags(arch, targettype)
if linkarchflags != []:
localvariables += [('linkarchflags', linkarchflags)]
linkconfigflags = self.make_linkconfigflags(config, targettype)
if linkconfigflags != []:
localvariables += [('linkconfigflags', linkconfigflags)]
if 'libs' in variables:
libvar = self.make_libs(variables['libs'])
if libvar != []:
localvariables += [('libs', libvar)]
libpaths = []
if 'libpaths' in variables:
libpaths = variables['libpaths']
localvariables += [('configlibpaths', self.make_configlibpaths(config, arch, libpaths))]
if 'runtime' in variables and variables['runtime'] == 'c++':
localvariables += [('link', self.cxxlinker)]
return localvariables
def builder_cc(self, writer, config, arch, targettype, infile, outfile, variables):
return writer.build(outfile, 'cc', infile, implicit = self.implicit_deps(config, variables), variables = self.cc_variables(config, arch, targettype, variables))
def builder_cxx(self, writer, config, arch, targettype, infile, outfile, variables):
return writer.build(outfile, 'cxx', infile, implicit = self.implicit_deps(config, variables), variables = self.cc_variables(config, arch, targettype, variables))
def builder_lib(self, writer, config, arch, targettype, infiles, outfile, variables):
return writer.build(outfile, 'ar', infiles, implicit = self.implicit_deps(config, variables), variables = self.ar_variables(config, arch, targettype, variables))
def builder_sharedlib(self, writer, config, arch, targettype, infiles, outfile, variables):
return writer.build(outfile, 'so', infiles, implicit = self.implicit_deps(config, variables), variables = self.link_variables(config, arch, targettype, variables))
def builder_bin(self, writer, config, arch, targettype, infiles, outfile, variables):
return writer.build(outfile, 'link', infiles, implicit = self.implicit_deps(config, variables), variables = self.link_variables(config, arch, targettype, variables))
def create(host, target, toolchain):
return GCCToolchain(host, target, toolchain)