AMBuildScript
author Scott Ehlert <ds@alliedmods.net>
Wed Mar 21 07:27:26 2012 -0500 (2012-03-21)
changeset 774 330fa28bde8e
parent 773 1bff89b1550d
child 777 62e002cdecdb
permissions -rw-r--r--
Hopefully fixed OS X build.
     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('CXXFLAGS', '-Wno-delete-non-virtual-dtor')
   122 				self.compiler.AddToListVar('CDEFINES', 'HAVE_STDINT_H')
   123 				if self.vendor == 'gcc':
   124 					self.compiler.AddToListVar('CFLAGS', '-mfpmath=sse')
   125 			elif isinstance(cxx, Cpp.MSVC):
   126 				self.vendor = 'msvc'
   127 				if AMBuild.options.debug == '1':
   128 					self.compiler.AddToListVar('CFLAGS', '/MTd')
   129 					self.compiler.AddToListVar('POSTLINKFLAGS', '/NODEFAULTLIB:libcmt')
   130 				else:
   131 					self.compiler.AddToListVar('CFLAGS', '/MT')
   132 				self.compiler.AddToListVar('CDEFINES', '_CRT_SECURE_NO_DEPRECATE')
   133 				self.compiler.AddToListVar('CDEFINES', '_CRT_SECURE_NO_WARNINGS')
   134 				self.compiler.AddToListVar('CDEFINES', '_CRT_NONSTDC_NO_DEPRECATE')
   135 				self.compiler.AddToListVar('CXXFLAGS', '/EHsc')
   136 				self.compiler.AddToListVar('CXXFLAGS', '/GR-')
   137 				self.compiler.AddToListVar('CFLAGS', '/W3')
   138 				self.compiler.AddToListVar('CFLAGS', '/nologo')
   139 				self.compiler.AddToListVar('CFLAGS', '/Zi')
   140 				self.compiler.AddToListVar('CXXFLAGS', '/TP')
   141 				self.compiler.AddToListVar('POSTLINKFLAGS', '/DEBUG')
   142 				self.compiler.AddToListVar('POSTLINKFLAGS', '/MACHINE:X86')
   143 				self.compiler.AddToListVar('POSTLINKFLAGS', '/SUBSYSTEM:WINDOWS')
   144 				self.compiler.AddToListVar('POSTLINKFLAGS', 'kernel32.lib')
   145 				self.compiler.AddToListVar('POSTLINKFLAGS', 'user32.lib')
   146 				self.compiler.AddToListVar('POSTLINKFLAGS', 'gdi32.lib')
   147 				self.compiler.AddToListVar('POSTLINKFLAGS', 'winspool.lib')
   148 				self.compiler.AddToListVar('POSTLINKFLAGS', 'comdlg32.lib')
   149 				self.compiler.AddToListVar('POSTLINKFLAGS', 'advapi32.lib')
   150 				self.compiler.AddToListVar('POSTLINKFLAGS', 'shell32.lib')
   151 				self.compiler.AddToListVar('POSTLINKFLAGS', 'ole32.lib')
   152 				self.compiler.AddToListVar('POSTLINKFLAGS', 'oleaut32.lib')
   153 				self.compiler.AddToListVar('POSTLINKFLAGS', 'uuid.lib')
   154 				self.compiler.AddToListVar('POSTLINKFLAGS', 'odbc32.lib')
   155 				self.compiler.AddToListVar('POSTLINKFLAGS', 'odbccp32.lib')
   156 
   157 			#Optimization
   158 			if AMBuild.options.opt == '1':
   159 				self.compiler.AddToListVar('CDEFINES', 'NDEBUG')
   160 				if self.vendor == 'gcc' or self.vendor == 'clang':
   161 					self.compiler.AddToListVar('CFLAGS', '-O3')
   162 				elif self.vendor == 'msvc':
   163 					self.compiler.AddToListVar('CFLAGS', '/Ox')
   164 					self.compiler.AddToListVar('POSTLINKFLAGS', '/OPT:ICF')
   165 					self.compiler.AddToListVar('POSTLINKFLAGS', '/OPT:REF')
   166 
   167 			#Debugging
   168 			if AMBuild.options.debug == '1':
   169 				self.compiler.AddToListVar('CDEFINES', 'DEBUG')
   170 				self.compiler.AddToListVar('CDEFINES', '_DEBUG')
   171 				if self.vendor == 'gcc' or self.vendor == 'clang':
   172 					self.compiler.AddToListVar('CFLAGS', '-g3')
   173 				elif self.vendor == 'msvc':
   174 					self.compiler.AddToListVar('CFLAGS', '/Od')
   175 					self.compiler.AddToListVar('CFLAGS', '/RTC1')
   176 
   177 			#Platform-specifics
   178 			if AMBuild.target['platform'] == 'linux':
   179 				self.compiler.AddToListVar('CDEFINES', '_LINUX')
   180 				if self.vendor == 'gcc':
   181 					self.compiler.AddToListVar('POSTLINKFLAGS', '-static-libgcc')
   182 				if self.vendor == 'clang':
   183 					self.compiler.AddToListVar('POSTLINKFLAGS', '-lgcc_eh')
   184 			elif AMBuild.target['platform'] == 'darwin':
   185 				self.compiler.AddToListVar('POSTLINKFLAGS', '-mmacosx-version-min=10.5')
   186 				self.compiler.AddToListVar('POSTLINKFLAGS', ['-arch', 'i386'])
   187 				self.compiler.AddToListVar('POSTLINKFLAGS', '-lstdc++')
   188 
   189 				# For OS X dylib versioning
   190 				import re
   191 				productFile = open(os.path.join(AMBuild.sourceFolder, 'product.version'), 'r')
   192 				productContents = productFile.read()
   193 				productFile.close()
   194 				m = re.match('(\d+)\.(\d+)\.(\d+).*', productContents)
   195 				if m == None:
   196 					self.version = '1.0.0'
   197 				else:
   198 					major, minor, release = m.groups()
   199 					self.version = '{0}.{1}.{2}'.format(major, minor, release)
   200 				AMBuild.cache.CacheVariable('version', self.version)
   201 			elif AMBuild.target['platform'] == 'windows':
   202 				self.compiler.AddToListVar('CDEFINES', 'WIN32')
   203 				self.compiler.AddToListVar('CDEFINES', '_WINDOWS')
   204 
   205 			#Finish up
   206 			self.compiler.AddToListVar('CDEFINES', 'MMS_GENERATED_BUILD')
   207 			self.compiler.AddToListVar('CINCLUDES',
   208 			                           os.path.join(AMBuild.outputFolder, 'includes'))
   209 			self.compiler.ToConfig(AMBuild, 'compiler')
   210 			AMBuild.cache.CacheVariable('vendor', self.vendor)
   211 			self.targetMap = { }
   212 			AMBuild.cache.CacheVariable('targetMap', self.targetMap)
   213 		else:
   214 			self.sdkInfo = AMBuild.cache['sdkInfo']
   215 			self.compiler.FromConfig(AMBuild, 'compiler')
   216 			self.targetMap = AMBuild.cache['targetMap']
   217 
   218 		if AMBuild.target['platform'] == 'windows':
   219 			self.compiler.AddToListVar('RCINCLUDES', os.path.join(AMBuild.sourceFolder, 'public'))
   220 			self.compiler.AddToListVar('RCINCLUDES',
   221 			                           os.path.join(AMBuild.outputFolder, 'includes'))
   222 
   223 	def DefaultCompiler(self):
   224 		compiler = self.compiler.Clone()
   225 		compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, 'public'))
   226 		return compiler
   227 
   228 	def JobMatters(self, jobname):
   229 		file = sys._getframe().f_code.co_filename
   230 		if AMBuild.mode == 'config':
   231 			self.targetMap[jobname] = file
   232 			return True
   233 		if len(AMBuild.args) == 0:
   234 			return True
   235 		if not jobname in AMBuild.args:
   236 			return False
   237 
   238 	def AutoVersion(self, folder, binary):
   239 		if AMBuild.target['platform'] == 'windows':
   240 			env = {'RCDEFINES': ['BINARY_NAME="' + binary.binaryFile + '"', 'MMS_GENERATED_BUILD']}
   241 			binary.AddResourceFile(os.path.join(folder, 'version.rc' ), env)
   242 		elif AMBuild.target['platform'] == 'darwin' and isinstance(binary, Cpp.LibraryBuilder):
   243 			binary.compiler['POSTLINKFLAGS'].extend(['-compatibility_version', '1.0.0'])
   244 			binary.compiler['POSTLINKFLAGS'].extend(['-current_version', AMBuild.cache['version']])
   245 		else:
   246 			return
   247 
   248 	def PreSetupHL2Job(self, job, builder, sdk):
   249 		info = self.sdkInfo[sdk]
   250 		sdkPath = AMBuild.cache[info['sdk']]
   251 		if AMBuild.target['platform'] == 'linux':
   252 			if sdk == 'ep1':
   253 				staticLibs = os.path.join(sdkPath, 'linux_sdk')
   254 			else:
   255 				staticLibs = os.path.join(sdkPath, 'lib', 'linux')
   256 			workFolder = os.path.join(AMBuild.outputFolder, job.workFolder)
   257 			if sdk in ['ep2v', 'l4d', 'l4d2']:
   258 				for i in ['tier1_i486.a', 'libvstdlib.so', 'libtier0.so']:
   259 					link = os.path.join(workFolder, i)
   260 					target = os.path.join(staticLibs, i)
   261 					try:
   262 						os.lstat(link)
   263 					except:
   264 						job.AddCommand(SymlinkCommand(link, target))
   265 			else:
   266 				for i in ['tier1_i486.a', 'vstdlib_i486.so', 'tier0_i486.so']:
   267 					link = os.path.join(workFolder, i)
   268 					target = os.path.join(staticLibs, i)
   269 					try:
   270 						os.lstat(link)
   271 					except:
   272 						job.AddCommand(SymlinkCommand(link, target))
   273 		elif AMBuild.target['platform'] == 'darwin':
   274 			staticLibs = os.path.join(sdkPath, 'lib', 'mac')
   275 			workFolder = os.path.join(AMBuild.outputFolder, job.workFolder)
   276 			for i in ['tier1_i486.a', 'libvstdlib.dylib', 'libtier0.dylib']:
   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'] == 'windows':
   284 			libs = ['tier0', 'tier1', 'vstdlib']
   285 			if sdk == 'swarm':
   286 				libs.append('interfaces')
   287 			for lib in libs:
   288 				libPath = os.path.join(sdkPath, 'lib', 'public', lib) + '.lib'
   289 				builder.RebuildIfNewer(libPath)
   290 				builder['POSTLINKFLAGS'].append(libPath)
   291 
   292 	def PostSetupHL2Job(self, job, builder, sdk):
   293 		if AMBuild.target['platform'] in ['linux', 'darwin']:
   294 			builder.AddObjectFiles(['tier1_i486.a'])
   295 
   296 	def DefaultHL2Compiler(self, path, sdk, noLink = False):
   297 		compiler = self.DefaultCompiler()
   298 
   299 		compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, path))
   300 		compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, path, 'sourcehook'))
   301 		compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, 'loader'))
   302 
   303 		info = self.possibleSdks
   304 		compiler['CDEFINES'].extend(['SE_' + info[i]['name'] + '=' + info[i]['def'] for i in info])
   305 
   306 		paths = [['public'], ['public', 'engine'], ['public', 'mathlib'], ['public', 'vstdlib'],
   307 						 ['public', 'tier0'], ['public', 'tier1']]
   308 		if sdk == 'ep1' or sdk == 'darkm':
   309 			paths.append(['public', 'dlls'])
   310 			paths.append(['game_shared'])
   311 		else:
   312 			paths.append(['public', 'game', 'server'])
   313 			paths.append(['game', 'shared'])
   314 			paths.append(['common'])
   315 
   316 		info = self.sdkInfo[sdk]
   317 		sdkPath = AMBuild.cache[info['sdk']]
   318 
   319 		compiler['CDEFINES'].append('SOURCE_ENGINE=' + info['def'])
   320 
   321 		if sdk == 'swarm' and AMBuild.target['platform'] == 'windows':
   322 			compiler['CDEFINES'].extend(['COMPILER_MSVC', 'COMPILER_MSVC32'])
   323 
   324 		if sdk == 'ep1':
   325 			if AMBuild.target['platform'] == 'linux':
   326 				staticLibs = os.path.join(sdkPath, 'linux_sdk')
   327 		else:
   328 			if AMBuild.target['platform'] == 'linux':
   329 				staticLibs = os.path.join(sdkPath, 'lib', 'linux')
   330 			elif AMBuild.target['platform'] == 'darwin':
   331 				staticLibs = os.path.join(sdkPath, 'lib', 'mac')
   332 
   333 		for i in paths:
   334 			compiler['CXXINCLUDES'].append(os.path.join(sdkPath, *i))
   335 
   336 		if not noLink:
   337 			if AMBuild.target['platform'] == 'linux':
   338 				compiler['POSTLINKFLAGS'][0:0] = ['-lm']
   339 				if sdk in ['ep2v', 'l4d', 'l4d2']:
   340 					compiler['POSTLINKFLAGS'][0:0] = ['libtier0.so']
   341 					compiler['POSTLINKFLAGS'][0:0] = ['libvstdlib.so']
   342 				else:
   343 					compiler['POSTLINKFLAGS'][0:0] = ['tier0_i486.so']
   344 					compiler['POSTLINKFLAGS'][0:0] = ['vstdlib_i486.so']				
   345 			elif AMBuild.target['platform'] == 'darwin':
   346 				compiler['POSTLINKFLAGS'][0:0] = ['libtier0.dylib']
   347 				compiler['POSTLINKFLAGS'][0:0] = ['libvstdlib.dylib']
   348 
   349 		return compiler
   350 
   351 mms = MMS()
   352 globals = {
   353 	'MMS': mms
   354 }
   355 
   356 AMBuild.Include(os.path.join('support', 'buildbot', 'Versioning'), globals)
   357 
   358 FileList = [
   359 		['loader', 'AMBuilder'],
   360 		['core', 'AMBuilder'],
   361 		['core-legacy', 'AMBuilder'],
   362 		['support', 'buildbot', 'PackageScript']
   363 	]
   364 
   365 for parts in FileList:
   366 	AMBuild.Include(os.path.join(*parts), globals)
   367