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