AMBuildScript
author Nicholas Hastings <skamonkey@gmail.com>
Mon Aug 08 08:57:03 2011 -0400 (2011-08-08)
changeset 763 e5a02b89021c
parent 753 2e5621faba48
child 772 bdaa1f0fd8ac
permissions -rw-r--r--
Added support for E.Y.E. Divine Cybermancy (bug 5034, 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.sdkInfo = { }
    12 		self.sdkInfo['ep1'] =   {'sdk': 'HL2SDK',          'ext': '1.ep1',   'def': '1',
    13 		                         'name': 'EPISODEONE',     'platform': ['windows', 'linux']}
    14 		self.sdkInfo['ep2'] =   {'sdk': 'HL2SDKOB',        'ext': '2.ep2',   'def': '3',
    15 		                         'name': 'ORANGEBOX',      'platform': ['windows', 'linux']}
    16 		self.sdkInfo['ep2v'] =  {'sdk': 'HL2SDKOBVALVE',   'ext': '2.ep2v',  'def': '6',
    17 		                         'name': 'ORANGEBOXVALVE', 'platform': ['windows', 'linux', 'darwin']}
    18 		self.sdkInfo['l4d'] =   {'sdk': 'HL2SDKL4D',       'ext': '2.l4d',   'def': '7',
    19 		                         'name': 'LEFT4DEAD',      'platform': ['windows', 'linux', 'darwin']}
    20 		self.sdkInfo['l4d2'] =  {'sdk': 'HL2SDKL4D2',      'ext': '2.l4d2',  'def': '8',
    21 		                         'name': 'LEFT4DEAD2',     'platform': ['windows', 'linux', 'darwin']}
    22 		self.sdkInfo['darkm'] = {'sdk': 'HL2SDK-DARKM',    'ext': '2.darkm', 'def': '2',
    23 		                         'name': 'DARKMESSIAH',    'platform': ['windows']}
    24 		self.sdkInfo['swarm'] = {'sdk': 'HL2SDK-SWARM',    'ext': '2.swarm', 'def': '9',
    25 		                         'name': 'ALIENSWARM',     'platform': ['windows']}
    26 		self.sdkInfo['bgt'] =   {'sdk': 'HL2SDK-BGT',      'ext': '2.bgt', 'def': '4',
    27 		                         'name': 'BLOODYGOODTIME', 'platform': ['windows']}
    28 		self.sdkInfo['eye'] =   {'sdk': 'HL2SDK-EYE',      'ext': '2.eye', 'def': '5',
    29 		                         'name': 'EYE', 'platform': ['windows']}
    30 
    31 		if AMBuild.mode == 'config':
    32 			#Detect compilers
    33 			self.compiler.DetectAll(AMBuild)
    34 
    35 			#Detect variables
    36 			envvars = {}
    37 			if AMBuild.target['platform'] != 'darwin':
    38 				envvars['HL2SDK'] = 'hl2sdk'
    39 				envvars['HL2SDKOB'] = 'hl2sdk-ob'
    40 
    41 			envvars['HL2SDKOBVALVE'] = 'hl2sdk-ob-valve'
    42 			envvars['HL2SDKL4D'] = 'hl2sdk-l4d'
    43 			envvars['HL2SDKL4D2'] = 'hl2sdk-l4d2'
    44 
    45 			#Dark Messiah is Windows-only
    46 			if AMBuild.target['platform'] == 'windows':
    47 				envvars['HL2SDK-DARKM'] = 'hl2sdk-darkm'
    48 				envvars['HL2SDK-SWARM'] = 'hl2sdk-swarm'
    49 				envvars['HL2SDK-BGT']   = 'hl2sdk-bgt'
    50 				envvars['HL2SDK-EYE']   = 'hl2sdk-eye'
    51 
    52 			#Must have a path for each envvar (file a bug if you don't like this)
    53 			for i in envvars:
    54 				if i in os.environ:
    55 					path = os.environ[i]
    56 					if not os.path.isdir(path):
    57 						raise Exception('Path for {0} was not found: {1}'.format(i, path))
    58 				else:
    59 					head = os.getcwd()
    60 					oldhead = None
    61 					while head != None and head != oldhead:
    62 						path = os.path.join(head, envvars[i])
    63 						if os.path.isdir(path):
    64 							break
    65 						oldhead = head
    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)
    70 
    71 			#Set up defines
    72 			cxx = self.compiler.cxx
    73 			if isinstance(cxx, Cpp.CompatGCC):
    74 				if isinstance(cxx, Cpp.GCC):
    75 					self.vendor = 'gcc'
    76 				elif isinstance(cxx, Cpp.Clang):
    77 					self.vendor = '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 				self.compiler.AddToListVar('CDEFINES', 'HAVE_STDINT_H')
   101 				if self.vendor == 'gcc':
   102 					self.compiler.AddToListVar('CFLAGS', '-mfpmath=sse')
   103 			elif isinstance(cxx, Cpp.MSVC):
   104 				self.vendor = 'msvc'
   105 				if AMBuild.options.debug == '1':
   106 					self.compiler.AddToListVar('CFLAGS', '/MTd')
   107 					self.compiler.AddToListVar('POSTLINKFLAGS', '/NODEFAULTLIB:libcmt')
   108 				else:
   109 					self.compiler.AddToListVar('CFLAGS', '/MT')
   110 				self.compiler.AddToListVar('CDEFINES', '_CRT_SECURE_NO_DEPRECATE')
   111 				self.compiler.AddToListVar('CDEFINES', '_CRT_SECURE_NO_WARNINGS')
   112 				self.compiler.AddToListVar('CDEFINES', '_CRT_NONSTDC_NO_DEPRECATE')
   113 				self.compiler.AddToListVar('CXXFLAGS', '/EHsc')
   114 				self.compiler.AddToListVar('CXXFLAGS', '/GR-')
   115 				self.compiler.AddToListVar('CFLAGS', '/W3')
   116 				self.compiler.AddToListVar('CFLAGS', '/nologo')
   117 				self.compiler.AddToListVar('CFLAGS', '/Zi')
   118 				self.compiler.AddToListVar('CXXFLAGS', '/TP')
   119 				self.compiler.AddToListVar('POSTLINKFLAGS', '/DEBUG')
   120 				self.compiler.AddToListVar('POSTLINKFLAGS', '/MACHINE:X86')
   121 				self.compiler.AddToListVar('POSTLINKFLAGS', '/SUBSYSTEM:WINDOWS')
   122 				self.compiler.AddToListVar('POSTLINKFLAGS', 'kernel32.lib')
   123 				self.compiler.AddToListVar('POSTLINKFLAGS', 'user32.lib')
   124 				self.compiler.AddToListVar('POSTLINKFLAGS', 'gdi32.lib')
   125 				self.compiler.AddToListVar('POSTLINKFLAGS', 'winspool.lib')
   126 				self.compiler.AddToListVar('POSTLINKFLAGS', 'comdlg32.lib')
   127 				self.compiler.AddToListVar('POSTLINKFLAGS', 'advapi32.lib')
   128 				self.compiler.AddToListVar('POSTLINKFLAGS', 'shell32.lib')
   129 				self.compiler.AddToListVar('POSTLINKFLAGS', 'ole32.lib')
   130 				self.compiler.AddToListVar('POSTLINKFLAGS', 'oleaut32.lib')
   131 				self.compiler.AddToListVar('POSTLINKFLAGS', 'uuid.lib')
   132 				self.compiler.AddToListVar('POSTLINKFLAGS', 'odbc32.lib')
   133 				self.compiler.AddToListVar('POSTLINKFLAGS', 'odbccp32.lib')
   134 
   135 			#Optimization
   136 			if AMBuild.options.opt == '1':
   137 				self.compiler.AddToListVar('CDEFINES', 'NDEBUG')
   138 				if self.vendor == 'gcc' or self.vendor == 'clang':
   139 					self.compiler.AddToListVar('CFLAGS', '-O3')
   140 				elif self.vendor == 'msvc':
   141 					self.compiler.AddToListVar('CFLAGS', '/Ox')
   142 					self.compiler.AddToListVar('POSTLINKFLAGS', '/OPT:ICF')
   143 					self.compiler.AddToListVar('POSTLINKFLAGS', '/OPT:REF')
   144 
   145 			#Debugging
   146 			if AMBuild.options.debug == '1':
   147 				self.compiler.AddToListVar('CDEFINES', 'DEBUG')
   148 				self.compiler.AddToListVar('CDEFINES', '_DEBUG')
   149 				if self.vendor == 'gcc' or self.vendor == 'clang':
   150 					self.compiler.AddToListVar('CFLAGS', '-g3')
   151 				elif self.vendor == 'msvc':
   152 					self.compiler.AddToListVar('CFLAGS', '/Od')
   153 					self.compiler.AddToListVar('CFLAGS', '/RTC1')
   154 
   155 			#Platform-specifics
   156 			if AMBuild.target['platform'] == 'linux':
   157 				self.compiler.AddToListVar('CDEFINES', '_LINUX')
   158 				if self.vendor == 'gcc':
   159 					self.compiler.AddToListVar('POSTLINKFLAGS', '-static-libgcc')
   160 				if self.vendor == 'clang':
   161 					self.compiler.AddToListVar('POSTLINKFLAGS', '-lgcc_eh')
   162 			elif AMBuild.target['platform'] == 'darwin':
   163 				self.compiler.AddToListVar('CFLAGS', ['-isysroot',
   164 				                                      '/Developer/SDKs/MacOSX10.5.sdk'])
   165 				self.compiler.AddToListVar('POSTLINKFLAGS', '-mmacosx-version-min=10.5')
   166 				self.compiler.AddToListVar('POSTLINKFLAGS', ['-arch', 'i386'])
   167 				self.compiler.AddToListVar('POSTLINKFLAGS', '-lstdc++')
   168 
   169 				# For OS X dylib versioning
   170 				import re
   171 				productFile = open(os.path.join(AMBuild.sourceFolder, 'product.version'), 'r')
   172 				productContents = productFile.read()
   173 				productFile.close()
   174 				m = re.match('(\d+)\.(\d+)\.(\d+).*', productContents)
   175 				if m == None:
   176 					self.version = '1.0.0'
   177 				else:
   178 					major, minor, release = m.groups()
   179 					self.version = '{0}.{1}.{2}'.format(major, minor, release)
   180 				AMBuild.cache.CacheVariable('version', self.version)
   181 			elif AMBuild.target['platform'] == 'windows':
   182 				self.compiler.AddToListVar('CDEFINES', 'WIN32')
   183 				self.compiler.AddToListVar('CDEFINES', '_WINDOWS')
   184 
   185 			#Finish up
   186 			self.compiler.AddToListVar('CDEFINES', 'MMS_GENERATED_BUILD')
   187 			self.compiler.AddToListVar('CINCLUDES',
   188 			                           os.path.join(AMBuild.outputFolder, 'includes'))
   189 			self.compiler.ToConfig(AMBuild, 'compiler')
   190 			AMBuild.cache.CacheVariable('vendor', self.vendor)
   191 			self.targetMap = { }
   192 			AMBuild.cache.CacheVariable('targetMap', self.targetMap)
   193 		else:
   194 			self.compiler.FromConfig(AMBuild, 'compiler')
   195 			self.targetMap = AMBuild.cache['targetMap']
   196 
   197 		if AMBuild.target['platform'] == 'windows':
   198 			self.compiler.AddToListVar('RCINCLUDES', os.path.join(AMBuild.sourceFolder, 'public'))
   199 			self.compiler.AddToListVar('RCINCLUDES',
   200 			                           os.path.join(AMBuild.outputFolder, 'includes'))
   201 
   202 	def DefaultCompiler(self):
   203 		compiler = self.compiler.Clone()
   204 		compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, 'public'))
   205 		return compiler
   206 
   207 	def JobMatters(self, jobname):
   208 		file = sys._getframe().f_code.co_filename
   209 		if AMBuild.mode == 'config':
   210 			self.targetMap[jobname] = file
   211 			return True
   212 		if len(AMBuild.args) == 0:
   213 			return True
   214 		if not jobname in AMBuild.args:
   215 			return False
   216 
   217 	def AutoVersion(self, folder, binary):
   218 		if AMBuild.target['platform'] == 'windows':
   219 			env = {'RCDEFINES': ['BINARY_NAME="' + binary.binaryFile + '"', 'MMS_GENERATED_BUILD']}
   220 			binary.AddResourceFile(os.path.join(folder, 'version.rc' ), env)
   221 		elif AMBuild.target['platform'] == 'darwin' and isinstance(binary, Cpp.LibraryBuilder):
   222 			binary.compiler['POSTLINKFLAGS'].extend(['-compatibility_version', '1.0.0'])
   223 			binary.compiler['POSTLINKFLAGS'].extend(['-current_version', AMBuild.cache['version']])
   224 		else:
   225 			return
   226 
   227 	def PreSetupHL2Job(self, job, builder, sdk):
   228 		info = self.sdkInfo[sdk]
   229 		sdkPath = AMBuild.cache[info['sdk']]
   230 		if AMBuild.target['platform'] == 'linux':
   231 			if sdk == 'ep1':
   232 				staticLibs = os.path.join(sdkPath, 'linux_sdk')
   233 			else:
   234 				staticLibs = os.path.join(sdkPath, 'lib', 'linux')
   235 			workFolder = os.path.join(AMBuild.outputFolder, job.workFolder)
   236 			if sdk in ['ep2v', 'l4d', 'l4d2']:
   237 				for i in ['tier1_i486.a', 'libvstdlib.so', 'libtier0.so']:
   238 					link = os.path.join(workFolder, i)
   239 					target = os.path.join(staticLibs, i)
   240 					try:
   241 						os.lstat(link)
   242 					except:
   243 						job.AddCommand(SymlinkCommand(link, target))
   244 			else:
   245 				for i in ['tier1_i486.a', 'vstdlib_i486.so', 'tier0_i486.so']:
   246 					link = os.path.join(workFolder, i)
   247 					target = os.path.join(staticLibs, i)
   248 					try:
   249 						os.lstat(link)
   250 					except:
   251 						job.AddCommand(SymlinkCommand(link, target))
   252 		elif AMBuild.target['platform'] == 'darwin':
   253 			staticLibs = os.path.join(sdkPath, 'lib', 'mac')
   254 			workFolder = os.path.join(AMBuild.outputFolder, job.workFolder)
   255 			for i in ['tier1_i486.a', 'libvstdlib.dylib', 'libtier0.dylib']:
   256 				link = os.path.join(workFolder, i)
   257 				target = os.path.join(staticLibs, i)
   258 				try:
   259 					os.lstat(link)
   260 				except:
   261 					job.AddCommand(SymlinkCommand(link, target))
   262 		elif AMBuild.target['platform'] == 'windows':
   263 			libs = ['tier0', 'tier1', 'vstdlib']
   264 			if sdk == 'swarm':
   265 				libs.append('interfaces')
   266 			for lib in libs:
   267 				libPath = os.path.join(sdkPath, 'lib', 'public', lib) + '.lib'
   268 				builder.RebuildIfNewer(libPath)
   269 				builder['POSTLINKFLAGS'].append(libPath)
   270 
   271 	def PostSetupHL2Job(self, job, builder, sdk):
   272 		if AMBuild.target['platform'] in ['linux', 'darwin']:
   273 			builder.AddObjectFiles(['tier1_i486.a'])
   274 
   275 	def DefaultHL2Compiler(self, path, sdk, noLink = False):
   276 		compiler = self.DefaultCompiler()
   277 
   278 		compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, path))
   279 		compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, path, 'sourcehook'))
   280 		compiler['CXXINCLUDES'].append(os.path.join(AMBuild.sourceFolder, 'loader'))
   281 
   282 		info = self.sdkInfo
   283 		compiler['CDEFINES'].extend(['SE_' + info[i]['name'] + '=' + info[i]['def'] for i in info])
   284 
   285 		paths = [['public'], ['public', 'engine'], ['public', 'mathlib'], ['public', 'vstdlib'],
   286 						 ['public', 'tier0'], ['public', 'tier1']]
   287 		if sdk == 'ep1' or sdk == 'darkm':
   288 			paths.append(['public', 'dlls'])
   289 			paths.append(['game_shared'])
   290 		else:
   291 			paths.append(['public', 'game', 'server'])
   292 			paths.append(['game', 'shared'])
   293 			paths.append(['common'])
   294 
   295 		info = self.sdkInfo[sdk]
   296 		sdkPath = AMBuild.cache[info['sdk']]
   297 
   298 		compiler['CDEFINES'].append('SOURCE_ENGINE=' + info['def'])
   299 
   300 		if sdk == 'swarm' and AMBuild.target['platform'] == 'windows':
   301 			compiler['CDEFINES'].extend(['COMPILER_MSVC', 'COMPILER_MSVC32'])
   302 
   303 		if sdk == 'ep1':
   304 			if AMBuild.target['platform'] == 'linux':
   305 				staticLibs = os.path.join(sdkPath, 'linux_sdk')
   306 		else:
   307 			if AMBuild.target['platform'] == 'linux':
   308 				staticLibs = os.path.join(sdkPath, 'lib', 'linux')
   309 			elif AMBuild.target['platform'] == 'darwin':
   310 				staticLibs = os.path.join(sdkPath, 'lib', 'mac')
   311 
   312 		for i in paths:
   313 			compiler['CXXINCLUDES'].append(os.path.join(sdkPath, *i))
   314 
   315 		if not noLink:
   316 			if AMBuild.target['platform'] == 'linux':
   317 				compiler['POSTLINKFLAGS'][0:0] = ['-lm']
   318 				if sdk in ['ep2v', 'l4d', 'l4d2']:
   319 					compiler['POSTLINKFLAGS'][0:0] = ['libtier0.so']
   320 					compiler['POSTLINKFLAGS'][0:0] = ['libvstdlib.so']
   321 				else:
   322 					compiler['POSTLINKFLAGS'][0:0] = ['tier0_i486.so']
   323 					compiler['POSTLINKFLAGS'][0:0] = ['vstdlib_i486.so']				
   324 			elif AMBuild.target['platform'] == 'darwin':
   325 				compiler['POSTLINKFLAGS'][0:0] = ['libtier0.dylib']
   326 				compiler['POSTLINKFLAGS'][0:0] = ['libvstdlib.dylib']
   327 
   328 		return compiler
   329 
   330 mms = MMS()
   331 globals = {
   332 	'MMS': mms
   333 }
   334 
   335 AMBuild.Include(os.path.join('support', 'buildbot', 'Versioning'), globals)
   336 
   337 FileList = [
   338 		['loader', 'AMBuilder'],
   339 		['core', 'AMBuilder'],
   340 		['core-legacy', 'AMBuilder'],
   341 		['support', 'buildbot', 'PackageScript']
   342 	]
   343 
   344 for parts in FileList:
   345 	AMBuild.Include(os.path.join(*parts), globals)
   346