AMBuildScript
author Nicholas Hastings <skamonkey@gmail.com>
Sat May 26 17:07:26 2012 -0400 (2012-05-26)
changeset 780 35f2a207badf
parent 779 87fe1357aa5f
child 791 a1b97ab6e0b1
permissions -rw-r--r--
Added support for CS: Global Offensive (bug 5228, r=DS).
     1 # vim: set ts=2 sw=2 tw=99 noet ft=python:
     2 import os
     3 import sys
     4 from ambuild.command import SymlinkCommand
     5 
     6 class MMS:
     7 	def __init__(self):
     8 		self.compiler = Cpp.Compiler()
     9 
    10 		#Build SDK info
    11 		self.possibleSdks = { }
    12 		self.possibleSdks['ep1'] =   {'sdk': 'HL2SDK',          'ext': '1.ep1',   'def': '1',
    13 		                              'name': 'EPISODEONE',     'platform': ['windows', 'linux']}
    14 		self.possibleSdks['ep2'] =   {'sdk': 'HL2SDKOB',        'ext': '2.ep2',   'def': '3',
    15 		                              'name': 'ORANGEBOX',      'platform': ['windows', 'linux']}
    16 		self.possibleSdks['ep2v'] =  {'sdk': 'HL2SDKOBVALVE',   'ext': '2.ep2v',  'def': '6',
    17 		                              'name': 'ORANGEBOXVALVE', 'platform': ['windows', 'linux', 'darwin']}
    18 		self.possibleSdks['l4d'] =   {'sdk': 'HL2SDKL4D',       'ext': '2.l4d',   'def': '7',
    19 		                              'name': 'LEFT4DEAD',      'platform': ['windows', 'linux', 'darwin']}
    20 		self.possibleSdks['l4d2'] =  {'sdk': 'HL2SDKL4D2',      'ext': '2.l4d2',  'def': '8',
    21 		                              'name': 'LEFT4DEAD2',     'platform': ['windows', 'linux', 'darwin']}
    22 		self.possibleSdks['darkm'] = {'sdk': 'HL2SDK-DARKM',    'ext': '2.darkm', 'def': '2',
    23 		                              'name': 'DARKMESSIAH',    'platform': ['windows']}
    24 		self.possibleSdks['swarm'] = {'sdk': 'HL2SDK-SWARM',    'ext': '2.swarm', 'def': '9',
    25 		                              'name': 'ALIENSWARM',     'platform': ['windows']}
    26 		self.possibleSdks['bgt'] =   {'sdk': 'HL2SDK-BGT',      'ext': '2.bgt', 'def': '4',
    27 		                              'name': 'BLOODYGOODTIME', 'platform': ['windows']}
    28 		self.possibleSdks['eye'] =   {'sdk': 'HL2SDK-EYE',      'ext': '2.eye', 'def': '5',
    29 		                              'name': 'EYE', 'platform': ['windows']}
    30 		self.possibleSdks['csgo'] =   {'sdk': 'HL2SDKCSGO',      'ext': '2.csgo', 'def': '11',
    31 		                              'name': 'CSGO', 'platform': ['windows', 'linux']}
    32 		# self.possibleSdks['portal2'] =   {'sdk': 'HL2SDK-PORTAL2',      'ext': '2.portal2', 'def': '10',
    33 		#                               'name': 'PORTAL2', 'platform': ['windows']}
    34 		
    35 		self.sdkInfo = { }
    36 		
    37 		if AMBuild.mode == 'config':
    38 			#Detect compilers
    39 			self.compiler.DetectAll(AMBuild)
    40 
    41 			#Detect variables
    42 			envvars = {}
    43 			if AMBuild.target['platform'] != 'darwin':
    44 				envvars['HL2SDK'] = 'hl2sdk'
    45 				envvars['HL2SDKOB'] = 'hl2sdk-ob'
    46 				envvars['HL2SDKCSGO'] = 'hl2sdk-csgo'
    47 
    48 			envvars['HL2SDKOBVALVE'] = 'hl2sdk-ob-valve'
    49 			envvars['HL2SDKL4D'] = 'hl2sdk-l4d'
    50 			envvars['HL2SDKL4D2'] = 'hl2sdk-l4d2'
    51 
    52 			#Dark Messiah is Windows-only
    53 			if AMBuild.target['platform'] == 'windows':
    54 				envvars['HL2SDK-DARKM'] = 'hl2sdk-darkm'
    55 				envvars['HL2SDK-SWARM'] = 'hl2sdk-swarm'
    56 				envvars['HL2SDK-BGT']   = 'hl2sdk-bgt'
    57 				envvars['HL2SDK-EYE']   = 'hl2sdk-eye'
    58 
    59 			# Finds if a dict with `key` set to `value` is present on the dict of dicts `dictionary`
    60 			def findDictByKey(dictionary, key, value):
    61 				for index in dictionary:
    62 					elem = dictionary[index]
    63 					if elem[key] == value:
    64 						return (elem, index)
    65 				return None
    66 			
    67 			for i in envvars:
    68 				if i in os.environ:
    69 					path = os.environ[i]
    70 					if not os.path.isdir(path):
    71 						raise Exception('Path for {0} was not found: {1}'.format(i, path))
    72 					elif i.startswith('HL2SDK'):
    73 						(info, sdk) = findDictByKey(self.possibleSdks, 'sdk', i)
    74 						self.sdkInfo[sdk] = info
    75 				else:
    76 					head = os.getcwd()
    77 					oldhead = None
    78 					while head != None and head != oldhead:
    79 						path = os.path.join(head, envvars[i])
    80 						if os.path.isdir(path):
    81 							break
    82 						oldhead = head
    83 						head, tail = os.path.split(head)
    84 					if i.startswith('HL2SDK'):
    85 						if head != None and head != oldhead:
    86 							(info, sdk) = findDictByKey(self.possibleSdks, 'sdk', i)
    87 							self.sdkInfo[sdk] = info
    88 					elif head == None or head == oldhead:
    89 						raise Exception('Could not find a valid path for {0}'.format(i))
    90 				AMBuild.cache.CacheVariable(i, path)
    91 				
    92 			if len(self.sdkInfo) < 1:
    93 				raise Exception('At least one SDK must be available.')
    94 
    95 			AMBuild.cache.CacheVariable('sdkInfo', self.sdkInfo)
    96 
    97 			#Set up defines
    98 			cxx = self.compiler.cxx
    99 			if isinstance(cxx, Cpp.CompatGCC):
   100 				if isinstance(cxx, Cpp.GCC):
   101 					self.vendor = 'gcc'
   102 				elif isinstance(cxx, Cpp.Clang):
   103 					self.vendor = 'clang'
   104 				self.compiler.AddToListVar('CDEFINES', 'stricmp=strcasecmp')
   105 				self.compiler.AddToListVar('CDEFINES', '_stricmp=strcasecmp')
   106 				self.compiler.AddToListVar('CDEFINES', '_snprintf=snprintf')
   107 				self.compiler.AddToListVar('CDEFINES', '_vsnprintf=vsnprintf')
   108 				self.compiler.AddToListVar('CFLAGS', '-pipe')
   109 				self.compiler.AddToListVar('CFLAGS', '-fno-strict-aliasing')
   110 				if (self.vendor == 'gcc' and cxx.majorVersion >= 4) or self.vendor == 'clang':
   111 					self.compiler.AddToListVar('CFLAGS', '-fvisibility=hidden')
   112 					self.compiler.AddToListVar('CXXFLAGS', '-fvisibility-inlines-hidden')
   113 				self.compiler.AddToListVar('CFLAGS', '-Wall')
   114 				self.compiler.AddToListVar('CFLAGS', '-Werror')
   115 				self.compiler.AddToListVar('CFLAGS', '-Wno-uninitialized')
   116 				self.compiler.AddToListVar('CFLAGS', '-Wno-unused')
   117 				self.compiler.AddToListVar('CFLAGS', '-Wno-switch')
   118 				self.compiler.AddToListVar('CFLAGS', '-msse')
   119 				self.compiler.AddToListVar('CFLAGS', '-m32')
   120 				self.compiler.AddToListVar('POSTLINKFLAGS', '-m32')
   121 				self.compiler.AddToListVar('CXXFLAGS', '-fno-exceptions')
   122 				self.compiler.AddToListVar('CXXFLAGS', '-fno-rtti')
   123 				self.compiler.AddToListVar('CXXFLAGS', '-fno-threadsafe-statics')
   124 				self.compiler.AddToListVar('CXXFLAGS', '-Wno-non-virtual-dtor')
   125 				self.compiler.AddToListVar('CXXFLAGS', '-Wno-overloaded-virtual')
   126 				if (self.vendor == 'gcc' and cxx.majorVersion >= 4 and cxx.minorVersion >= 7) or \
   127 						(self.vendor == 'clang' and cxx.majorVersion >= 3):
   128 					self.compiler.AddToListVar('CXXFLAGS', '-Wno-delete-non-virtual-dtor')
   129 				self.compiler.AddToListVar('CDEFINES', 'HAVE_STDINT_H')
   130 				if self.vendor == 'gcc':
   131 					self.compiler.AddToListVar('CFLAGS', '-mfpmath=sse')
   132 			elif isinstance(cxx, Cpp.MSVC):
   133 				self.vendor = 'msvc'
   134 				if AMBuild.options.debug == '1':
   135 					self.compiler.AddToListVar('CFLAGS', '/MTd')
   136 					self.compiler.AddToListVar('POSTLINKFLAGS', '/NODEFAULTLIB:libcmt')
   137 				else:
   138 					self.compiler.AddToListVar('CFLAGS', '/MT')
   139 				self.compiler.AddToListVar('CDEFINES', '_CRT_SECURE_NO_DEPRECATE')
   140 				self.compiler.AddToListVar('CDEFINES', '_CRT_SECURE_NO_WARNINGS')
   141 				self.compiler.AddToListVar('CDEFINES', '_CRT_NONSTDC_NO_DEPRECATE')
   142 				self.compiler.AddToListVar('CXXFLAGS', '/EHsc')
   143 				self.compiler.AddToListVar('CXXFLAGS', '/GR-')
   144 				self.compiler.AddToListVar('CFLAGS', '/W3')
   145 				self.compiler.AddToListVar('CFLAGS', '/nologo')
   146 				self.compiler.AddToListVar('CFLAGS', '/Zi')
   147 				self.compiler.AddToListVar('CXXFLAGS', '/TP')
   148 				self.compiler.AddToListVar('POSTLINKFLAGS', '/DEBUG')
   149 				self.compiler.AddToListVar('POSTLINKFLAGS', '/MACHINE:X86')
   150 				self.compiler.AddToListVar('POSTLINKFLAGS', '/SUBSYSTEM:WINDOWS')
   151 				self.compiler.AddToListVar('POSTLINKFLAGS', 'kernel32.lib')
   152 				self.compiler.AddToListVar('POSTLINKFLAGS', 'user32.lib')
   153 				self.compiler.AddToListVar('POSTLINKFLAGS', 'gdi32.lib')
   154 				self.compiler.AddToListVar('POSTLINKFLAGS', 'winspool.lib')
   155 				self.compiler.AddToListVar('POSTLINKFLAGS', 'comdlg32.lib')
   156 				self.compiler.AddToListVar('POSTLINKFLAGS', 'advapi32.lib')
   157 				self.compiler.AddToListVar('POSTLINKFLAGS', 'shell32.lib')
   158 				self.compiler.AddToListVar('POSTLINKFLAGS', 'ole32.lib')
   159 				self.compiler.AddToListVar('POSTLINKFLAGS', 'oleaut32.lib')
   160 				self.compiler.AddToListVar('POSTLINKFLAGS', 'uuid.lib')
   161 				self.compiler.AddToListVar('POSTLINKFLAGS', 'odbc32.lib')
   162 				self.compiler.AddToListVar('POSTLINKFLAGS', 'odbccp32.lib')
   163 
   164 			#Optimization
   165 			if AMBuild.options.opt == '1':
   166 				self.compiler.AddToListVar('CDEFINES', 'NDEBUG')
   167 				if self.vendor == 'gcc' or self.vendor == 'clang':
   168 					self.compiler.AddToListVar('CFLAGS', '-O3')
   169 				elif self.vendor == 'msvc':
   170 					self.compiler.AddToListVar('CFLAGS', '/Ox')
   171 					self.compiler.AddToListVar('POSTLINKFLAGS', '/OPT:ICF')
   172 					self.compiler.AddToListVar('POSTLINKFLAGS', '/OPT:REF')
   173 
   174 			#Debugging
   175 			if AMBuild.options.debug == '1':
   176 				self.compiler.AddToListVar('CDEFINES', 'DEBUG')
   177 				self.compiler.AddToListVar('CDEFINES', '_DEBUG')
   178 				if self.vendor == 'gcc' or self.vendor == 'clang':
   179 					self.compiler.AddToListVar('CFLAGS', '-g3')
   180 				elif self.vendor == 'msvc':
   181 					self.compiler.AddToListVar('CFLAGS', '/Od')
   182 					self.compiler.AddToListVar('CFLAGS', '/RTC1')
   183 
   184 			#Platform-specifics
   185 			if AMBuild.target['platform'] == 'linux':
   186 				self.compiler.AddToListVar('CDEFINES', '_LINUX')
   187 				if self.vendor == 'gcc':
   188 					self.compiler.AddToListVar('POSTLINKFLAGS', '-static-libgcc')
   189 				if self.vendor == 'clang':
   190 					self.compiler.AddToListVar('POSTLINKFLAGS', '-lgcc_eh')
   191 			elif AMBuild.target['platform'] == 'darwin':
   192 				self.compiler.AddToListVar('POSTLINKFLAGS', '-mmacosx-version-min=10.5')
   193 				self.compiler.AddToListVar('POSTLINKFLAGS', ['-arch', 'i386'])
   194 				self.compiler.AddToListVar('POSTLINKFLAGS', '-lstdc++')
   195 
   196 				# For OS X dylib versioning
   197 				import re
   198 				productFile = open(os.path.join(AMBuild.sourceFolder, 'product.version'), 'r')
   199 				productContents = productFile.read()
   200 				productFile.close()
   201 				m = re.match('(\d+)\.(\d+)\.(\d+).*', productContents)
   202 				if m == None:
   203 					self.version = '1.0.0'
   204 				else:
   205 					major, minor, release = m.groups()
   206 					self.version = '{0}.{1}.{2}'.format(major, minor, release)
   207 				AMBuild.cache.CacheVariable('version', self.version)
   208 			elif AMBuild.target['platform'] == 'windows':
   209 				self.compiler.AddToListVar('CDEFINES', 'WIN32')
   210 				self.compiler.AddToListVar('CDEFINES', '_WINDOWS')
   211 
   212 			#Finish up
   213 			self.compiler.AddToListVar('CDEFINES', 'MMS_GENERATED_BUILD')
   214 			self.compiler.AddToListVar('CINCLUDES',
   215 			                           os.path.join(AMBuild.outputFolder, 'includes'))
   216 			self.compiler.ToConfig(AMBuild, 'compiler')
   217 			AMBuild.cache.CacheVariable('vendor', self.vendor)
   218 			self.targetMap = { }
   219 			AMBuild.cache.CacheVariable('targetMap', self.targetMap)
   220 		else:
   221 			self.sdkInfo = AMBuild.cache['sdkInfo']
   222 			self.compiler.FromConfig(AMBuild, 'compiler')
   223 			self.targetMap = AMBuild.cache['targetMap']
   224 
   225 		if AMBuild.target['platform'] == 'windows':
   226 			self.compiler.AddToListVar('RCINCLUDES', os.path.join(AMBuild.sourceFolder, 'public'))
   227 			self.compiler.AddToListVar('RCINCLUDES',
   228 			                           os.path.join(AMBuild.outputFolder, 'includes'))
   229 
   230 	def DefaultCompiler(self):
   231 		compiler = self.compiler.Clone()
   232 		compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, 'public'))
   233 		return compiler
   234 
   235 	def JobMatters(self, jobname):
   236 		file = sys._getframe().f_code.co_filename
   237 		if AMBuild.mode == 'config':
   238 			self.targetMap[jobname] = file
   239 			return True
   240 		if len(AMBuild.args) == 0:
   241 			return True
   242 		if not jobname in AMBuild.args:
   243 			return False
   244 
   245 	def AutoVersion(self, folder, binary):
   246 		if AMBuild.target['platform'] == 'windows':
   247 			env = {'RCDEFINES': ['BINARY_NAME="' + binary.binaryFile + '"', 'MMS_GENERATED_BUILD']}
   248 			binary.AddResourceFile(os.path.join(folder, 'version.rc' ), env)
   249 		elif AMBuild.target['platform'] == 'darwin' and isinstance(binary, Cpp.LibraryBuilder):
   250 			binary.compiler['POSTLINKFLAGS'].extend(['-compatibility_version', '1.0.0'])
   251 			binary.compiler['POSTLINKFLAGS'].extend(['-current_version', AMBuild.cache['version']])
   252 		else:
   253 			return
   254 
   255 	def PreSetupHL2Job(self, job, builder, sdk):
   256 		info = self.sdkInfo[sdk]
   257 		sdkPath = AMBuild.cache[info['sdk']]
   258 		if AMBuild.target['platform'] == 'linux':
   259 			if sdk == 'ep1':
   260 				staticLibs = os.path.join(sdkPath, 'linux_sdk')
   261 			else:
   262 				staticLibs = os.path.join(sdkPath, 'lib', 'linux')
   263 			workFolder = os.path.join(AMBuild.outputFolder, job.workFolder)
   264 			if sdk in ['ep2v', 'l4d', 'l4d2', 'csgo']:
   265 				libs = ['tier1_i486.a', 'libvstdlib.so', 'libtier0.so']
   266 				if sdk == 'csgo':
   267 					libs.insert(0, 'interfaces_i486.a')
   268 				for lib in libs:
   269 					link = os.path.join(workFolder, lib)
   270 					target = os.path.join(staticLibs, lib)
   271 					try:
   272 						os.lstat(link)
   273 					except:
   274 						job.AddCommand(SymlinkCommand(link, target))
   275 			else:
   276 				for i in ['tier1_i486.a', 'vstdlib_i486.so', 'tier0_i486.so']:
   277 					link = os.path.join(workFolder, i)
   278 					target = os.path.join(staticLibs, i)
   279 					try:
   280 						os.lstat(link)
   281 					except:
   282 						job.AddCommand(SymlinkCommand(link, target))
   283 		elif AMBuild.target['platform'] == 'darwin':
   284 			staticLibs = os.path.join(sdkPath, 'lib', 'mac')
   285 			workFolder = os.path.join(AMBuild.outputFolder, job.workFolder)
   286 			libs = ['tier1_i486.a', 'libvstdlib.dylib', 'libtier0.dylib']
   287 			if sdk == 'csgo':
   288 				libs.append('interfaces_i486.a')
   289 			for lib in libs:
   290 				link = os.path.join(workFolder, lib)
   291 				target = os.path.join(staticLibs, lib)
   292 				try:
   293 					os.lstat(link)
   294 				except:
   295 					job.AddCommand(SymlinkCommand(link, target))
   296 		elif AMBuild.target['platform'] == 'windows':
   297 			libs = ['tier0', 'tier1', 'vstdlib']
   298 			if sdk in ['swarm', 'csgo']:
   299 				libs.append('interfaces')
   300 			for lib in libs:
   301 				libPath = os.path.join(sdkPath, 'lib', 'public', lib) + '.lib'
   302 				builder.RebuildIfNewer(libPath)
   303 				builder['POSTLINKFLAGS'].append(libPath)
   304 
   305 	def PostSetupHL2Job(self, job, builder, sdk):
   306 		if AMBuild.target['platform'] in ['linux', 'darwin']:
   307 			builder.AddObjectFiles(['tier1_i486.a'])
   308 			if( sdk == 'csgo' ):
   309 				builder.AddObjectFiles(['interfaces_i486.a'])
   310 
   311 	def DefaultHL2Compiler(self, path, sdk, noLink = False):
   312 		compiler = self.DefaultCompiler()
   313 
   314 		compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, path))
   315 		compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, path, 'sourcehook'))
   316 		compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, 'loader'))
   317 
   318 		info = self.possibleSdks
   319 		compiler['CDEFINES'].extend(['SE_' + info[i]['name'] + '=' + info[i]['def'] for i in info])
   320 		
   321 		# We don't build for Portal 2 (yet?, ever?), but using this define in code as 
   322 		# it saves trouble if we ever need to
   323 		compiler['CDEFINES'].append('SE_PORTAL2=10')
   324 
   325 		paths = [['public'], ['public', 'engine'], ['public', 'mathlib'], ['public', 'vstdlib'],
   326 						 ['public', 'tier0'], ['public', 'tier1']]
   327 		if sdk == 'ep1' or sdk == 'darkm':
   328 			paths.append(['public', 'dlls'])
   329 			paths.append(['game_shared'])
   330 		else:
   331 			paths.append(['public', 'game', 'server'])
   332 			paths.append(['game', 'shared'])
   333 			paths.append(['common'])
   334 
   335 		info = self.sdkInfo[sdk]
   336 		sdkPath = AMBuild.cache[info['sdk']]
   337 
   338 		compiler['CDEFINES'].append('SOURCE_ENGINE=' + info['def'])
   339 
   340 		if sdk in ['swarm', 'csgo']:
   341 			if AMBuild.target['platform'] == 'windows':
   342 				compiler['CDEFINES'].extend(['COMPILER_MSVC', 'COMPILER_MSVC32'])
   343 			else:
   344 				compiler['CDEFINES'].extend(['COMPILER_GCC', 'POSIX'])
   345 
   346 		if sdk == 'ep1':
   347 			if AMBuild.target['platform'] == 'linux':
   348 				staticLibs = os.path.join(sdkPath, 'linux_sdk')
   349 		else:
   350 			if AMBuild.target['platform'] == 'linux':
   351 				staticLibs = os.path.join(sdkPath, 'lib', 'linux')
   352 			elif AMBuild.target['platform'] == 'darwin':
   353 				staticLibs = os.path.join(sdkPath, 'lib', 'mac')
   354 
   355 		for i in paths:
   356 			compiler['CXXINCLUDES'].append(os.path.join(sdkPath, *i))
   357 
   358 		if not noLink:
   359 			if AMBuild.target['platform'] == 'linux':
   360 				compiler['POSTLINKFLAGS'][0:0] = ['-lm']
   361 				if sdk in ['ep2v', 'l4d', 'l4d2', 'csgo']:
   362 					compiler['POSTLINKFLAGS'][0:0] = ['libtier0.so']
   363 					compiler['POSTLINKFLAGS'][0:0] = ['libvstdlib.so']
   364 				else:
   365 					compiler['POSTLINKFLAGS'][0:0] = ['tier0_i486.so']
   366 					compiler['POSTLINKFLAGS'][0:0] = ['vstdlib_i486.so']				
   367 			elif AMBuild.target['platform'] == 'darwin':
   368 				compiler['POSTLINKFLAGS'][0:0] = ['libtier0.dylib']
   369 				compiler['POSTLINKFLAGS'][0:0] = ['libvstdlib.dylib']
   370 
   371 		return compiler
   372 
   373 mms = MMS()
   374 globals = {
   375 	'MMS': mms
   376 }
   377 
   378 AMBuild.Include(os.path.join('support', 'buildbot', 'Versioning'), globals)
   379 
   380 FileList = [
   381 		['loader', 'AMBuilder'],
   382 		['core', 'AMBuilder'],
   383 		['core-legacy', 'AMBuilder'],
   384 		['support', 'buildbot', 'PackageScript']
   385 	]
   386 
   387 for parts in FileList:
   388 	AMBuild.Include(os.path.join(*parts), globals)
   389