Added support for building with clang.
1 # vim: set ts=2 sw=2 tw=99 noet ft=python:
4 from ambuild.command import SymlinkCommand
8 self.compiler = Cpp.Compiler()
12 self.sdkInfo['ep1'] = {'sdk': 'HL2SDK', 'ext': '14.ep1', 'def': '1',
13 'name': 'EPISODEONE', 'platform': ['windows', 'linux']}
14 self.sdkInfo['ep2'] = {'sdk': 'HL2SDKOB', 'ext': '16.ep2', 'def': '3',
15 'name': 'ORANGEBOX', 'platform': ['windows', 'linux']}
16 self.sdkInfo['ep2v'] = {'sdk': 'HL2SDKOBVALVE', 'ext': '16.ep2v', 'def': '5',
17 'name': 'ORANGEBOXVALVE', 'platform': ['windows', 'linux', 'darwin']}
18 self.sdkInfo['l4d'] = {'sdk': 'HL2SDKL4D', 'ext': '16.l4d', 'def': '6',
19 'name': 'LEFT4DEAD', 'platform': ['windows', 'linux', 'darwin']}
20 self.sdkInfo['l4d2'] = {'sdk': 'HL2SDKL4D2', 'ext': '16.l4d2', 'def': '7',
21 'name': 'LEFT4DEAD2', 'platform': ['windows', 'linux', 'darwin']}
22 self.sdkInfo['darkm'] = {'sdk': 'HL2SDK-DARKM', 'ext': '16.darkm', 'def': '2',
23 'name': 'DARKMESSIAH', 'platform': ['windows']}
24 self.sdkInfo['swarm'] = {'sdk': 'HL2SDK-SWARM', 'ext': '16.swarm', 'def': '8',
25 'name': 'ALIENSWARM', 'platform': ['windows']}
26 self.sdkInfo['bgt'] = {'sdk': 'HL2SDK-BGT', 'ext': '16.bgt', 'def': '4',
27 'name': 'BLOODYGOODTIME', 'platform': ['windows']}
28 self.sdkInfo['csgo'] = {'sdk': 'HL2SDKCSGO', 'ext': '16.csgo', 'def': '12',
29 'name': 'CSGO', 'platform': ['windows', 'linux']}
31 if AMBuild.mode == 'config':
33 self.compiler.DetectAll(AMBuild)
36 envvars = { 'MMSOURCE19': 'mmsource-1.9' }
37 if AMBuild.target['platform'] != 'darwin':
38 envvars['HL2SDK'] = 'hl2sdk'
39 envvars['HL2SDKOB'] = 'hl2sdk-ob'
40 envvars['HL2SDKCSGO'] = 'hl2sdk-csgo'
42 envvars['HL2SDKOBVALVE'] = 'hl2sdk-ob-valve'
43 envvars['HL2SDKL4D'] = 'hl2sdk-l4d'
44 envvars['HL2SDKL4D2'] = 'hl2sdk-l4d2'
46 #Dark Messiah is Windows-only
47 if AMBuild.target['platform'] == 'windows':
48 envvars['HL2SDK-DARKM'] = 'hl2sdk-darkm'
49 envvars['HL2SDK-SWARM'] = 'hl2sdk-swarm'
50 envvars['HL2SDK-BGT'] = 'hl2sdk-bgt'
52 #Must have a path for each envvar (file a bug if you don't like this)
56 if not os.path.isdir(path):
57 raise Exception('Path for {0} was not found: {1}'.format(i, path))
61 while head != None and head != oldhead:
62 path = os.path.join(head, envvars[i])
63 if os.path.isdir(path):
66 head, tail = os.path.split(head)
67 if head == None or head == oldhead:
68 raise Exception('Could not find a valid path for {0}'.format(i))
69 AMBuild.cache.CacheVariable(i, path)
72 cxx = self.compiler.cxx
73 if isinstance(cxx, Cpp.CompatGCC):
74 if isinstance(cxx, Cpp.GCC):
76 elif isinstance(cxx, Cpp.Clang):
78 self.compiler.AddToListVar('CDEFINES', 'stricmp=strcasecmp')
79 self.compiler.AddToListVar('CDEFINES', '_stricmp=strcasecmp')
80 self.compiler.AddToListVar('CDEFINES', '_snprintf=snprintf')
81 self.compiler.AddToListVar('CDEFINES', '_vsnprintf=vsnprintf')
82 self.compiler.AddToListVar('CFLAGS', '-pipe')
83 self.compiler.AddToListVar('CFLAGS', '-fno-strict-aliasing')
84 if (self.vendor == 'gcc' and cxx.majorVersion >= 4) or self.vendor == 'clang':
85 self.compiler.AddToListVar('CFLAGS', '-fvisibility=hidden')
86 self.compiler.AddToListVar('CXXFLAGS', '-fvisibility-inlines-hidden')
87 self.compiler.AddToListVar('CFLAGS', '-Wall')
88 self.compiler.AddToListVar('CFLAGS', '-Werror')
89 self.compiler.AddToListVar('CFLAGS', '-Wno-uninitialized')
90 self.compiler.AddToListVar('CFLAGS', '-Wno-unused')
91 self.compiler.AddToListVar('CFLAGS', '-Wno-switch')
92 self.compiler.AddToListVar('CFLAGS', '-msse')
93 self.compiler.AddToListVar('CFLAGS', '-m32')
94 self.compiler.AddToListVar('POSTLINKFLAGS', '-m32')
95 self.compiler.AddToListVar('CXXFLAGS', '-fno-exceptions')
96 self.compiler.AddToListVar('CXXFLAGS', '-fno-rtti')
97 self.compiler.AddToListVar('CXXFLAGS', '-fno-threadsafe-statics')
98 self.compiler.AddToListVar('CXXFLAGS', '-Wno-non-virtual-dtor')
99 self.compiler.AddToListVar('CXXFLAGS', '-Wno-overloaded-virtual')
100 if (self.vendor == 'gcc' and cxx.majorVersion >= 4 and cxx.minorVersion >= 7) or \
101 (self.vendor == 'clang' and cxx.majorVersion >= 3):
102 self.compiler.AddToListVar('CXXFLAGS', '-Wno-delete-non-virtual-dtor')
103 self.compiler.AddToListVar('CDEFINES', 'HAVE_STDINT_H')
104 if self.vendor == 'gcc':
105 self.compiler.AddToListVar('CFLAGS', '-mfpmath=sse')
106 elif isinstance(cxx, Cpp.MSVC):
108 if AMBuild.options.debug == '1':
109 self.compiler.AddToListVar('CFLAGS', '/MTd')
110 self.compiler.AddToListVar('POSTLINKFLAGS', '/NODEFAULTLIB:libcmt')
112 self.compiler.AddToListVar('CFLAGS', '/MT')
113 self.compiler.AddToListVar('CDEFINES', '_CRT_SECURE_NO_DEPRECATE')
114 self.compiler.AddToListVar('CDEFINES', '_CRT_SECURE_NO_WARNINGS')
115 self.compiler.AddToListVar('CDEFINES', '_CRT_NONSTDC_NO_DEPRECATE')
116 self.compiler.AddToListVar('CXXFLAGS', '/EHsc')
117 self.compiler.AddToListVar('CXXFLAGS', '/GR-')
118 self.compiler.AddToListVar('CFLAGS', '/W3')
119 self.compiler.AddToListVar('CFLAGS', '/nologo')
120 self.compiler.AddToListVar('CFLAGS', '/Zi')
121 self.compiler.AddToListVar('CXXFLAGS', '/TP')
122 self.compiler.AddToListVar('POSTLINKFLAGS', '/DEBUG')
123 self.compiler.AddToListVar('POSTLINKFLAGS', '/MACHINE:X86')
124 self.compiler.AddToListVar('POSTLINKFLAGS', '/SUBSYSTEM:WINDOWS')
125 self.compiler.AddToListVar('POSTLINKFLAGS', 'kernel32.lib')
126 self.compiler.AddToListVar('POSTLINKFLAGS', 'user32.lib')
127 self.compiler.AddToListVar('POSTLINKFLAGS', 'gdi32.lib')
128 self.compiler.AddToListVar('POSTLINKFLAGS', 'winspool.lib')
129 self.compiler.AddToListVar('POSTLINKFLAGS', 'comdlg32.lib')
130 self.compiler.AddToListVar('POSTLINKFLAGS', 'advapi32.lib')
131 self.compiler.AddToListVar('POSTLINKFLAGS', 'shell32.lib')
132 self.compiler.AddToListVar('POSTLINKFLAGS', 'ole32.lib')
133 self.compiler.AddToListVar('POSTLINKFLAGS', 'oleaut32.lib')
134 self.compiler.AddToListVar('POSTLINKFLAGS', 'uuid.lib')
135 self.compiler.AddToListVar('POSTLINKFLAGS', 'odbc32.lib')
136 self.compiler.AddToListVar('POSTLINKFLAGS', 'odbccp32.lib')
139 if AMBuild.options.opt == '1':
140 self.compiler.AddToListVar('CDEFINES', 'NDEBUG')
141 if self.vendor == 'gcc' or self.vendor == 'clang':
142 self.compiler.AddToListVar('CFLAGS', '-O3')
143 elif self.vendor == 'msvc':
144 self.compiler.AddToListVar('CFLAGS', '/Ox')
145 self.compiler.AddToListVar('POSTLINKFLAGS', '/OPT:ICF')
146 self.compiler.AddToListVar('POSTLINKFLAGS', '/OPT:REF')
149 if AMBuild.options.debug == '1':
150 self.compiler.AddToListVar('CDEFINES', 'DEBUG')
151 self.compiler.AddToListVar('CDEFINES', '_DEBUG')
152 if self.vendor == 'gcc' or self.vendor == 'clang':
153 self.compiler.AddToListVar('CFLAGS', '-g3')
154 elif self.vendor == 'msvc':
155 self.compiler.AddToListVar('CFLAGS', '/Od')
156 self.compiler.AddToListVar('CFLAGS', '/RTC1')
159 if AMBuild.target['platform'] == 'linux':
160 self.compiler.AddToListVar('CDEFINES', '_LINUX')
161 if self.vendor == 'gcc':
162 self.compiler.AddToListVar('POSTLINKFLAGS', '-static-libgcc')
163 if self.vendor == 'clang':
164 self.compiler.AddToListVar('POSTLINKFLAGS', '-lgcc_eh')
165 elif AMBuild.target['platform'] == 'darwin':
166 self.compiler.AddToListVar('POSTLINKFLAGS', '-mmacosx-version-min=10.5')
167 self.compiler.AddToListVar('POSTLINKFLAGS', ['-arch', 'i386'])
168 self.compiler.AddToListVar('POSTLINKFLAGS', '-lstdc++')
170 # For OS X dylib versioning
172 productFile = open(os.path.join(AMBuild.sourceFolder, 'product.version'), 'r')
173 productContents = productFile.read()
175 m = re.match('(\d+)\.(\d+)\.(\d+).*', productContents)
177 self.version = '1.0.0'
179 major, minor, release = m.groups()
180 self.version = '{0}.{1}.{2}'.format(major, minor, release)
181 AMBuild.cache.CacheVariable('version', self.version)
182 elif AMBuild.target['platform'] == 'windows':
183 self.compiler.AddToListVar('CDEFINES', 'WIN32')
184 self.compiler.AddToListVar('CDEFINES', '_WINDOWS')
187 self.compiler.AddToListVar('CDEFINES', 'STRIPPER_GENERATED_BUILD')
188 self.compiler.AddToListVar('CINCLUDES',
189 os.path.join(AMBuild.outputFolder, 'includes'))
190 self.compiler.ToConfig(AMBuild, 'compiler')
191 AMBuild.cache.CacheVariable('vendor', self.vendor)
193 AMBuild.cache.CacheVariable('targetMap', self.targetMap)
195 self.compiler.FromConfig(AMBuild, 'compiler')
196 self.targetMap = AMBuild.cache['targetMap']
198 if AMBuild.target['platform'] == 'windows':
199 self.compiler.AddToListVar('RCINCLUDES', os.path.join(AMBuild.sourceFolder, 'public'))
200 self.compiler.AddToListVar('RCINCLUDES',
201 os.path.join(AMBuild.outputFolder, 'includes'))
202 self.mmsPath = AMBuild.cache['MMSOURCE19']
204 def DefaultCompiler(self):
205 compiler = self.compiler.Clone()
206 compiler['CXXINCLUDES'].append(os.path.join(self.mmsPath, 'core', 'sourcehook'))
209 def JobMatters(self, jobname):
210 file = sys._getframe().f_code.co_filename
211 if AMBuild.mode == 'config':
212 self.targetMap[jobname] = file
214 if len(AMBuild.args) == 0:
216 if not jobname in AMBuild.args:
219 def AutoVersion(self, folder, binary):
220 if AMBuild.target['platform'] == 'windows':
221 env = {'RCDEFINES': ['BINARY_NAME="' + binary.binaryFile + '"', 'STRIPPER_GENERATED_BUILD']}
222 binary.AddResourceFile(os.path.join(folder, 'version.rc' ), env)
223 elif AMBuild.target['platform'] == 'darwin' and isinstance(binary, Cpp.LibraryBuilder):
224 binary.compiler['POSTLINKFLAGS'].extend(['-compatibility_version', '1.0.0'])
225 binary.compiler['POSTLINKFLAGS'].extend(['-current_version', AMBuild.cache['version']])
229 def PreSetupHL2Job(self, job, builder, sdk):
230 info = self.sdkInfo[sdk]
231 sdkPath = AMBuild.cache[info['sdk']]
232 if AMBuild.target['platform'] == 'linux':
234 staticLibs = os.path.join(sdkPath, 'linux_sdk')
236 staticLibs = os.path.join(sdkPath, 'lib', 'linux')
237 workFolder = os.path.join(AMBuild.outputFolder, job.workFolder)
238 if sdk in ['ep2v', 'l4d', 'l4d2', 'csgo']:
239 libs = ['tier1_i486.a', 'mathlib_i486.a', 'libvstdlib.so', 'libtier0.so']
241 libs.append('interfaces_i486.a')
243 link = os.path.join(workFolder, lib)
244 target = os.path.join(staticLibs, lib)
248 job.AddCommand(SymlinkCommand(link, target))
250 libs = ['tier1_i486.a', 'mathlib_i486.a', 'vstdlib_i486.so', 'tier0_i486.so']
252 link = os.path.join(workFolder, lib)
253 target = os.path.join(staticLibs, lib)
257 job.AddCommand(SymlinkCommand(link, target))
258 elif AMBuild.target['platform'] == 'darwin':
259 staticLibs = os.path.join(sdkPath, 'lib', 'mac')
260 workFolder = os.path.join(AMBuild.outputFolder, job.workFolder)
261 libs = ['tier1_i486.a', 'mathlib_i486.a', 'libvstdlib.dylib', 'libtier0.dylib']
263 libs.append('interfaces_i486.a')
265 link = os.path.join(workFolder, lib)
266 target = os.path.join(staticLibs, lib)
270 job.AddCommand(SymlinkCommand(link, target))
271 elif AMBuild.target['platform'] == 'windows':
272 libs = ['tier0', 'tier1', 'vstdlib', 'mathlib']
273 if sdk in ['swarm', 'csgo']:
274 libs.append('interfaces')
276 libPath = os.path.join(sdkPath, 'lib', 'public', lib) + '.lib'
277 builder.RebuildIfNewer(libPath)
278 builder['POSTLINKFLAGS'].append(libPath)
280 def PostSetupHL2Job(self, job, builder, sdk):
281 if AMBuild.target['platform'] in ['linux', 'darwin']:
282 builder.AddObjectFiles(['tier1_i486.a', 'mathlib_i486.a'])
284 builder.AddObjectFiles(['interfaces_i486.a'])
286 def DefaultHL2Compiler(self, path, sdk, noLink = False, oldMms = '-legacy'):
287 compiler = self.DefaultCompiler()
293 compiler['CXXINCLUDES'].append(os.path.join(self.mmsPath, mms))
294 compiler['CXXINCLUDES'].append(os.path.join(self.mmsPath, mms, 'sourcehook'))
297 compiler['CDEFINES'].extend(['SE_' + info[i]['name'] + '=' + info[i]['def'] for i in info])
299 paths = [['public'], ['public', 'engine'], ['public', 'mathlib'], ['public', 'vstdlib'],
300 ['public', 'tier0'], ['public', 'tier1']]
301 if sdk == 'ep1' or sdk == 'darkm':
302 paths.append(['public', 'dlls'])
303 paths.append(['game_shared'])
305 paths.append(['public', 'game', 'server'])
306 paths.append(['game', 'shared'])
307 paths.append(['common'])
309 info = self.sdkInfo[sdk]
310 sdkPath = AMBuild.cache[info['sdk']]
312 compiler['CDEFINES'].append('SOURCE_ENGINE=' + info['def'])
314 if sdk in ['swarm','csgo']:
315 if AMBuild.target['platform'] == 'windows':
316 compiler['CDEFINES'].extend(['COMPILER_MSVC', 'COMPILER_MSVC32'])
318 compiler['CDEFINES'].extend(['COMPILER_GCC', 'POSIX'])
321 if AMBuild.target['platform'] == 'linux':
322 staticLibs = os.path.join(sdkPath, 'linux_sdk')
324 if AMBuild.target['platform'] == 'linux':
325 staticLibs = os.path.join(sdkPath, 'lib', 'linux')
326 elif AMBuild.target['platform'] == 'darwin':
327 staticLibs = os.path.join(sdkPath, 'lib', 'mac')
330 compiler['CXXINCLUDES'].append(os.path.join(sdkPath, *i))
333 if AMBuild.target['platform'] == 'linux':
334 compiler['POSTLINKFLAGS'][0:0] = ['-lm']
335 if sdk in ['ep2v', 'l4d', 'l4d2', 'csgo']:
336 compiler['POSTLINKFLAGS'][0:0] = ['libtier0.so']
337 compiler['POSTLINKFLAGS'][0:0] = ['libvstdlib.so']
339 compiler['POSTLINKFLAGS'][0:0] = ['tier0_i486.so']
340 compiler['POSTLINKFLAGS'][0:0] = ['vstdlib_i486.so']
341 elif AMBuild.target['platform'] == 'darwin':
342 compiler['POSTLINKFLAGS'][0:0] = ['libtier0.dylib']
343 compiler['POSTLINKFLAGS'][0:0] = ['libvstdlib.dylib']
347 stripper = Stripper()
352 AMBuild.Include(os.path.join('buildbot', 'Versioning'), globals)
356 ['gameshim', 'AMBuilder'],
357 ['loader', 'AMBuilder'],
361 for parts in FileList:
362 AMBuild.Include(os.path.join(*parts), globals)