Define POSIX and GNUC when compiling for any build on linux/mac.
1 # vim: set ts=2 sw=2 tw=99 noet ft=python:
4 from ambuild.command import SymlinkCommand
8 self.compiler = Cpp.Compiler()
11 self.possibleSdks = { }
12 self.possibleSdks['ep1'] = {'sdk': 'HL2SDK', 'ext': '1.ep1', 'def': '1',
13 'name': 'EPISODEONE', 'platform': ['windows', 'linux'],
15 self.possibleSdks['ep2'] = {'sdk': 'HL2SDKOB', 'ext': '2.ep2', 'def': '3',
16 'name': 'ORANGEBOX', 'platform': ['windows', 'linux'],
18 self.possibleSdks['css'] = {'sdk': 'HL2SDKCSS', 'ext': '2.css', 'def': '6',
19 'name': 'CSS', 'platform': ['windows', 'linux', 'darwin'],
21 self.possibleSdks['ep2v'] = {'sdk': 'HL2SDKOBVALVE', 'ext': '2.ep2v', 'def': '7',
22 'name': 'ORANGEBOXVALVE', 'platform': ['windows', 'linux', 'darwin'],
23 'dir': 'hl2sdk-ob-valve'}
24 self.possibleSdks['l4d'] = {'sdk': 'HL2SDKL4D', 'ext': '2.l4d', 'def': '8',
25 'name': 'LEFT4DEAD', 'platform': ['windows', 'linux', 'darwin'],
27 self.possibleSdks['l4d2'] = {'sdk': 'HL2SDKL4D2', 'ext': '2.l4d2', 'def': '9',
28 'name': 'LEFT4DEAD2', 'platform': ['windows', 'linux', 'darwin'],
30 self.possibleSdks['darkm'] = {'sdk': 'HL2SDK-DARKM', 'ext': '2.darkm', 'def': '2',
31 'name': 'DARKMESSIAH', 'platform': ['windows'],
32 'dir': 'hl2sdk-darkm'}
33 self.possibleSdks['swarm'] = {'sdk': 'HL2SDK-SWARM', 'ext': '2.swarm', 'def': '10',
34 'name': 'ALIENSWARM', 'platform': ['windows'],
35 'dir': 'hl2sdk-swarm'}
36 self.possibleSdks['bgt'] = {'sdk': 'HL2SDK-BGT', 'ext': '2.bgt', 'def': '4',
37 'name': 'BLOODYGOODTIME', 'platform': ['windows'],
39 self.possibleSdks['eye'] = {'sdk': 'HL2SDK-EYE', 'ext': '2.eye', 'def': '5',
40 'name': 'EYE', 'platform': ['windows'],
42 self.possibleSdks['csgo'] = {'sdk': 'HL2SDKCSGO', 'ext': '2.csgo', 'def': '12',
43 'name': 'CSGO', 'platform': ['windows', 'linux', 'darwin'],
45 self.possibleSdks['dota'] = {'sdk': 'HL2SDKDOTA', 'ext': '2.dota', 'def': '13',
46 'name': 'DOTA', 'platform': ['windows'],
48 # self.possibleSdks['portal2'] = {'sdk': 'HL2SDK-PORTAL2', 'ext': '2.portal2', 'def': '11',
49 # 'name': 'PORTAL2', 'platform': ['windows'],
50 # 'dir': 'hl2sdk-portal2'}
54 if AMBuild.mode == 'config':
56 self.compiler.DetectAll(AMBuild)
58 #Look for SDK directories
59 for sdk in self.possibleSdks:
60 #Get list of SDKs to build against or 'all' or 'present'
61 sdkList = AMBuild.options.sdks.split(',')
62 #Build against all supported SDKs?
63 useAll = sdkList[0] == 'all'
64 #Build against supported SDKs that exist?
65 usePresent = sdkList[0] == 'present'
67 info = self.possibleSdks[sdk]
68 if AMBuild.target['platform'] in info['platform']:
71 sdkPath = self.ResolveEnvPath(env, dir)
73 if useAll or sdk in sdkList:
74 raise Exception('Could not find a valid path for {0}'.format(env))
77 if useAll or usePresent or sdk in sdkList:
78 self.sdkInfo[sdk] = info
79 AMBuild.cache.CacheVariable(env, sdkPath)
81 if len(self.sdkInfo) < 1:
82 raise Exception('At least one SDK must be available.')
84 AMBuild.cache.CacheVariable('sdkInfo', self.sdkInfo)
87 cxx = self.compiler.cxx
88 if isinstance(cxx, Cpp.CompatGCC):
89 if isinstance(cxx, Cpp.GCC):
91 elif isinstance(cxx, Cpp.Clang):
93 self.compiler.AddToListVar('CDEFINES', 'stricmp=strcasecmp')
94 self.compiler.AddToListVar('CDEFINES', '_stricmp=strcasecmp')
95 self.compiler.AddToListVar('CDEFINES', '_snprintf=snprintf')
96 self.compiler.AddToListVar('CDEFINES', '_vsnprintf=vsnprintf')
97 self.compiler.AddToListVar('CFLAGS', '-pipe')
98 self.compiler.AddToListVar('CFLAGS', '-fno-strict-aliasing')
99 if (self.vendor == 'gcc' and cxx.majorVersion >= 4) or self.vendor == 'clang':
100 self.compiler.AddToListVar('CFLAGS', '-fvisibility=hidden')
101 self.compiler.AddToListVar('CXXFLAGS', '-fvisibility-inlines-hidden')
102 self.compiler.AddToListVar('CFLAGS', '-Wall')
103 self.compiler.AddToListVar('CFLAGS', '-Werror')
104 self.compiler.AddToListVar('CFLAGS', '-Wno-uninitialized')
105 self.compiler.AddToListVar('CFLAGS', '-Wno-unused')
106 self.compiler.AddToListVar('CFLAGS', '-Wno-switch')
107 self.compiler.AddToListVar('CFLAGS', '-msse')
108 self.compiler.AddToListVar('CFLAGS', '-m32')
109 self.compiler.AddToListVar('POSTLINKFLAGS', '-m32')
110 self.compiler.AddToListVar('CXXFLAGS', '-fno-exceptions')
111 self.compiler.AddToListVar('CXXFLAGS', '-fno-rtti')
112 self.compiler.AddToListVar('CXXFLAGS', '-fno-threadsafe-statics')
113 self.compiler.AddToListVar('CXXFLAGS', '-Wno-non-virtual-dtor')
114 self.compiler.AddToListVar('CXXFLAGS', '-Wno-overloaded-virtual')
115 if (self.vendor == 'gcc' and cxx.majorVersion >= 4 and cxx.minorVersion >= 7) or \
116 (self.vendor == 'clang' and cxx.majorVersion >= 3):
117 self.compiler.AddToListVar('CXXFLAGS', '-Wno-delete-non-virtual-dtor')
118 self.compiler.AddToListVar('CDEFINES', 'HAVE_STDINT_H')
119 self.compiler.AddToListVar('CDEFINES', 'GNUC')
120 if self.vendor == 'gcc':
121 self.compiler.AddToListVar('CFLAGS', '-mfpmath=sse')
122 elif isinstance(cxx, Cpp.MSVC):
124 if AMBuild.options.debug == '1':
125 self.compiler.AddToListVar('CFLAGS', '/MTd')
126 self.compiler.AddToListVar('POSTLINKFLAGS', '/NODEFAULTLIB:libcmt')
128 self.compiler.AddToListVar('CFLAGS', '/MT')
129 self.compiler.AddToListVar('CDEFINES', '_CRT_SECURE_NO_DEPRECATE')
130 self.compiler.AddToListVar('CDEFINES', '_CRT_SECURE_NO_WARNINGS')
131 self.compiler.AddToListVar('CDEFINES', '_CRT_NONSTDC_NO_DEPRECATE')
132 self.compiler.AddToListVar('CXXFLAGS', '/EHsc')
133 self.compiler.AddToListVar('CXXFLAGS', '/GR-')
134 self.compiler.AddToListVar('CFLAGS', '/W3')
135 self.compiler.AddToListVar('CFLAGS', '/nologo')
136 self.compiler.AddToListVar('CFLAGS', '/Zi')
137 self.compiler.AddToListVar('CXXFLAGS', '/TP')
138 self.compiler.AddToListVar('POSTLINKFLAGS', '/DEBUG')
139 self.compiler.AddToListVar('POSTLINKFLAGS', '/MACHINE:X86')
140 self.compiler.AddToListVar('POSTLINKFLAGS', '/SUBSYSTEM:WINDOWS')
141 self.compiler.AddToListVar('POSTLINKFLAGS', 'kernel32.lib')
142 self.compiler.AddToListVar('POSTLINKFLAGS', 'user32.lib')
143 self.compiler.AddToListVar('POSTLINKFLAGS', 'gdi32.lib')
144 self.compiler.AddToListVar('POSTLINKFLAGS', 'winspool.lib')
145 self.compiler.AddToListVar('POSTLINKFLAGS', 'comdlg32.lib')
146 self.compiler.AddToListVar('POSTLINKFLAGS', 'advapi32.lib')
147 self.compiler.AddToListVar('POSTLINKFLAGS', 'shell32.lib')
148 self.compiler.AddToListVar('POSTLINKFLAGS', 'ole32.lib')
149 self.compiler.AddToListVar('POSTLINKFLAGS', 'oleaut32.lib')
150 self.compiler.AddToListVar('POSTLINKFLAGS', 'uuid.lib')
151 self.compiler.AddToListVar('POSTLINKFLAGS', 'odbc32.lib')
152 self.compiler.AddToListVar('POSTLINKFLAGS', 'odbccp32.lib')
155 if AMBuild.options.opt == '1':
156 self.compiler.AddToListVar('CDEFINES', 'NDEBUG')
157 if self.vendor == 'gcc' or self.vendor == 'clang':
158 self.compiler.AddToListVar('CFLAGS', '-O3')
159 elif self.vendor == 'msvc':
160 self.compiler.AddToListVar('CFLAGS', '/Ox')
161 self.compiler.AddToListVar('POSTLINKFLAGS', '/OPT:ICF')
162 self.compiler.AddToListVar('POSTLINKFLAGS', '/OPT:REF')
165 if AMBuild.options.debug == '1':
166 self.compiler.AddToListVar('CDEFINES', 'DEBUG')
167 self.compiler.AddToListVar('CDEFINES', '_DEBUG')
168 if self.vendor == 'gcc' or self.vendor == 'clang':
169 self.compiler.AddToListVar('CFLAGS', '-g3')
170 elif self.vendor == 'msvc':
171 self.compiler.AddToListVar('CFLAGS', '/Od')
172 self.compiler.AddToListVar('CFLAGS', '/RTC1')
174 #This needs to be after our optimization flags which could otherwise disable it.
175 if self.vendor == 'msvc':
176 # Don't omit frame pointer
177 self.compiler.AddToListVar('CFLAGS', '/Oy-')
180 if AMBuild.target['platform'] == 'linux':
181 self.compiler.AddToListVar('CDEFINES', '_LINUX')
182 self.compiler.AddToListVar('CDEFINES', 'POSIX')
183 if self.vendor == 'gcc':
184 self.compiler.AddToListVar('POSTLINKFLAGS', '-static-libgcc')
185 if self.vendor == 'clang':
186 self.compiler.AddToListVar('POSTLINKFLAGS', '-lgcc_eh')
187 elif AMBuild.target['platform'] == 'darwin':
188 self.compiler.AddToListVar('CDEFINES', 'OSX')
189 self.compiler.AddToListVar('CDEFINES', '_OSX')
190 self.compiler.AddToListVar('CDEFINES', 'POSIX')
191 self.compiler.AddToListVar('POSTLINKFLAGS', '-mmacosx-version-min=10.5')
192 self.compiler.AddToListVar('POSTLINKFLAGS', ['-arch', 'i386'])
193 self.compiler.AddToListVar('POSTLINKFLAGS', '-lstdc++')
195 # For OS X dylib versioning
197 productFile = open(os.path.join(AMBuild.sourceFolder, 'product.version'), 'r')
198 productContents = productFile.read()
200 m = re.match('(\d+)\.(\d+)\.(\d+).*', productContents)
202 self.version = '1.0.0'
204 major, minor, release = m.groups()
205 self.version = '{0}.{1}.{2}'.format(major, minor, release)
206 AMBuild.cache.CacheVariable('version', self.version)
207 elif AMBuild.target['platform'] == 'windows':
208 self.compiler.AddToListVar('CDEFINES', 'WIN32')
209 self.compiler.AddToListVar('CDEFINES', '_WINDOWS')
212 self.compiler.AddToListVar('CDEFINES', 'MMS_GENERATED_BUILD')
213 self.compiler.AddToListVar('CINCLUDES',
214 os.path.join(AMBuild.outputFolder, 'includes'))
215 self.compiler.ToConfig(AMBuild, 'compiler')
216 AMBuild.cache.CacheVariable('vendor', self.vendor)
218 AMBuild.cache.CacheVariable('targetMap', self.targetMap)
220 self.sdkInfo = AMBuild.cache['sdkInfo']
221 self.compiler.FromConfig(AMBuild, 'compiler')
222 self.targetMap = AMBuild.cache['targetMap']
224 if AMBuild.target['platform'] == 'windows':
225 self.compiler.AddToListVar('RCINCLUDES', os.path.join(AMBuild.sourceFolder, 'public'))
226 self.compiler.AddToListVar('RCINCLUDES',
227 os.path.join(AMBuild.outputFolder, 'includes'))
229 def DefaultCompiler(self):
230 compiler = self.compiler.Clone()
231 compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, 'public'))
234 def JobMatters(self, jobname):
235 file = sys._getframe().f_code.co_filename
236 if AMBuild.mode == 'config':
237 self.targetMap[jobname] = file
239 if len(AMBuild.args) == 0:
241 if not jobname in AMBuild.args:
244 def AutoVersion(self, folder, binary):
245 if AMBuild.target['platform'] == 'windows':
246 env = {'RCDEFINES': ['BINARY_NAME="' + binary.binaryFile + '"', 'MMS_GENERATED_BUILD']}
247 binary.AddResourceFile(os.path.join(folder, 'version.rc' ), env)
248 elif AMBuild.target['platform'] == 'darwin' and isinstance(binary, Cpp.LibraryBuilder):
249 binary.compiler['POSTLINKFLAGS'].extend(['-compatibility_version', '1.0.0'])
250 binary.compiler['POSTLINKFLAGS'].extend(['-current_version', AMBuild.cache['version']])
254 def PreSetupHL2Job(self, job, builder, sdk):
255 info = self.sdkInfo[sdk]
256 sdkPath = AMBuild.cache[info['sdk']]
257 if AMBuild.target['platform'] == 'linux':
259 staticLibs = os.path.join(sdkPath, 'linux_sdk')
261 staticLibs = os.path.join(sdkPath, 'lib', 'linux')
262 workFolder = os.path.join(AMBuild.outputFolder, job.workFolder)
263 if sdk in ['ep2v', 'css', 'l4d2']:
264 libs = ['tier1_i486.a', 'libvstdlib_srv.so', 'libtier0_srv.so']
266 link = os.path.join(workFolder, lib)
267 target = os.path.join(staticLibs, lib)
271 job.AddCommand(SymlinkCommand(link, target))
272 elif sdk in ['l4d', 'csgo']:
273 libs = ['tier1_i486.a', 'libvstdlib.so', 'libtier0.so']
275 libs.insert(0, 'interfaces_i486.a')
277 link = os.path.join(workFolder, lib)
278 target = os.path.join(staticLibs, lib)
282 job.AddCommand(SymlinkCommand(link, target))
284 for i in ['tier1_i486.a', 'vstdlib_i486.so', 'tier0_i486.so']:
285 link = os.path.join(workFolder, i)
286 target = os.path.join(staticLibs, i)
290 job.AddCommand(SymlinkCommand(link, target))
291 elif AMBuild.target['platform'] == 'darwin':
292 staticLibs = os.path.join(sdkPath, 'lib', 'mac')
293 workFolder = os.path.join(AMBuild.outputFolder, job.workFolder)
294 libs = ['tier1_i486.a', 'libvstdlib.dylib', 'libtier0.dylib']
296 libs.append('interfaces_i486.a')
298 link = os.path.join(workFolder, lib)
299 target = os.path.join(staticLibs, lib)
303 job.AddCommand(SymlinkCommand(link, target))
304 elif AMBuild.target['platform'] == 'windows':
305 libs = ['tier0', 'tier1', 'vstdlib']
306 if sdk in ['swarm', 'csgo', 'dota']:
307 libs.append('interfaces')
309 libPath = os.path.join(sdkPath, 'lib', 'public', lib) + '.lib'
310 builder.RebuildIfNewer(libPath)
311 builder['POSTLINKFLAGS'].append(libPath)
313 def PostSetupHL2Job(self, job, builder, sdk):
314 if AMBuild.target['platform'] in ['linux', 'darwin']:
315 builder.AddObjectFiles(['tier1_i486.a'])
316 if( sdk in ['csgo', 'dota'] ):
317 builder.AddObjectFiles(['interfaces_i486.a'])
319 def DefaultHL2Compiler(self, path, sdk, noLink = False):
320 compiler = self.DefaultCompiler()
322 compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, path))
323 compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, path, 'sourcehook'))
324 compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, 'loader'))
326 info = self.possibleSdks
327 compiler['CDEFINES'].extend(['SE_' + info[i]['name'] + '=' + info[i]['def'] for i in info])
329 # We don't build for Portal 2 (yet?, ever?), but using this define in code as
330 # it saves trouble if we ever need to
331 compiler['CDEFINES'].append('SE_PORTAL2=11')
333 paths = [['public'], ['public', 'engine'], ['public', 'mathlib'], ['public', 'vstdlib'],
334 ['public', 'tier0'], ['public', 'tier1']]
335 if sdk == 'ep1' or sdk == 'darkm':
336 paths.append(['public', 'dlls'])
337 paths.append(['game_shared'])
339 paths.append(['public', 'game', 'server'])
340 paths.append(['game', 'shared'])
341 paths.append(['common'])
343 info = self.sdkInfo[sdk]
344 sdkPath = AMBuild.cache[info['sdk']]
346 compiler['CDEFINES'].append('SOURCE_ENGINE=' + info['def'])
348 if sdk in ['swarm', 'csgo', 'dota']:
349 if AMBuild.target['platform'] == 'windows':
350 compiler['CDEFINES'].extend(['COMPILER_MSVC', 'COMPILER_MSVC32'])
352 compiler['CDEFINES'].extend(['COMPILER_GCC'])
355 if AMBuild.target['platform'] == 'linux':
356 staticLibs = os.path.join(sdkPath, 'linux_sdk')
358 if AMBuild.target['platform'] == 'linux':
359 staticLibs = os.path.join(sdkPath, 'lib', 'linux')
360 elif AMBuild.target['platform'] == 'darwin':
361 staticLibs = os.path.join(sdkPath, 'lib', 'mac')
364 compiler['CXXINCLUDES'].append(os.path.join(sdkPath, *i))
367 if AMBuild.target['platform'] == 'linux':
368 compiler['POSTLINKFLAGS'][0:0] = ['-lm']
369 if sdk in ['ep2v', 'css', 'l4d2']:
370 compiler['POSTLINKFLAGS'][0:0] = ['libtier0_srv.so']
371 compiler['POSTLINKFLAGS'][0:0] = ['libvstdlib_srv.so']
372 elif sdk in ['l4d', 'csgo']:
373 compiler['POSTLINKFLAGS'][0:0] = ['libtier0.so']
374 compiler['POSTLINKFLAGS'][0:0] = ['libvstdlib.so']
376 compiler['POSTLINKFLAGS'][0:0] = ['tier0_i486.so']
377 compiler['POSTLINKFLAGS'][0:0] = ['vstdlib_i486.so']
378 elif AMBuild.target['platform'] == 'darwin':
379 compiler['POSTLINKFLAGS'][0:0] = ['libtier0.dylib']
380 compiler['POSTLINKFLAGS'][0:0] = ['libvstdlib.dylib']
384 def ResolveEnvPath(self, env, defaultDir):
385 if env in os.environ:
386 path = os.environ[env]
387 if os.path.isdir(path):
392 while head != None and head != oldhead:
393 path = os.path.join(head, defaultDir)
394 if os.path.isdir(path):
397 head, tail = os.path.split(head)
405 AMBuild.Include(os.path.join('support', 'buildbot', 'Versioning'), globals)
408 ['loader', 'AMBuilder'],
409 ['core', 'AMBuilder'],
410 ['core-legacy', 'AMBuilder'],
411 ['support', 'buildbot', 'PackageScript']
414 for parts in FileList:
415 AMBuild.Include(os.path.join(*parts), globals)