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