AMBuildScript 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. import os, sys
  2. class SDK(object):
  3. def __init__(self, sdk, ext, aDef, name, platform, dir):
  4. self.folder = 'hl2sdk-' + dir
  5. self.envvar = sdk
  6. self.ext = ext
  7. self.code = aDef
  8. self.define = name
  9. self.name = dir
  10. self.path = None # Actual path
  11. self.platformSpec = platform
  12. # By default, nothing supports x64.
  13. if type(platform) is list:
  14. self.platformSpec = {p: ['x86'] for p in platform}
  15. else:
  16. self.platformSpec = platform
  17. def shouldBuild(self, target, archs):
  18. if target.platform not in self.platformSpec:
  19. return False
  20. if not len([i for i in self.platformSpec[target.platform] if i in archs]):
  21. return False
  22. return True
  23. WinOnly = ['windows']
  24. WinLinux = ['windows', 'linux']
  25. WinLinuxMac = ['windows', 'linux', 'mac']
  26. CSGO = {
  27. 'windows': ['x86'],
  28. 'linux': ['x86', 'x64'],
  29. 'mac': ['x64']
  30. }
  31. Source2 = {
  32. 'windows': ['x86', 'x64'],
  33. 'linux': ['x64'],
  34. }
  35. PossibleSDKs = {
  36. 'tf2': SDK('HL2SDKTF2', '2.tf2', '11', 'TF2', WinLinuxMac, 'tf2'),
  37. }
  38. def ResolveEnvPath(env, folder):
  39. if env in os.environ:
  40. path = os.environ[env]
  41. if os.path.isdir(path):
  42. return path
  43. else:
  44. head = os.getcwd()
  45. oldhead = None
  46. while head != None and head != oldhead:
  47. path = os.path.join(head, folder)
  48. if os.path.isdir(path):
  49. return path
  50. oldhead = head
  51. head, tail = os.path.split(head)
  52. return None
  53. def SetArchFlags(compiler, arch, platform):
  54. if compiler.behavior == 'gcc':
  55. if arch == 'x86':
  56. compiler.cflags += ['-m32']
  57. compiler.linkflags += ['-m32']
  58. if platform == 'mac':
  59. compiler.linkflags += ['-arch', 'i386']
  60. elif arch == 'x64':
  61. compiler.cflags += ['-m64', '-fPIC']
  62. compiler.linkflags += ['-m64']
  63. if platform == 'mac':
  64. compiler.linkflags += ['-arch', 'x86_64']
  65. elif compiler.like('msvc'):
  66. if arch == 'x86':
  67. compiler.linkflags += ['/MACHINE:X86']
  68. elif arch == 'x64':
  69. compiler.linkflags += ['/MACHINE:X64']
  70. def AppendArchSuffix(binary, name, arch):
  71. if arch == 'x64':
  72. binary.localFolder = name + '.x64'
  73. class MMSConfig(object):
  74. def __init__(self):
  75. self.sdks = {}
  76. self.binaries = []
  77. self.generated_headers = None
  78. self.archs = builder.target.arch.replace('x86_64', 'x64').split(',')
  79. def detectProductVersion(self):
  80. builder.AddConfigureFile('product.version')
  81. # For OS X dylib versioning
  82. import re
  83. with open(os.path.join(builder.sourcePath, 'product.version'), 'r') as fp:
  84. productContents = fp.read()
  85. m = re.match('(\d+)\.(\d+)\.(\d+).*', productContents)
  86. if m == None:
  87. self.productVersion = '1.0.0'
  88. else:
  89. major, minor, release = m.groups()
  90. self.productVersion = '{0}.{1}.{2}'.format(major, minor, release)
  91. def detectSDKs(self):
  92. sdk_list = builder.options.sdks.split(',')
  93. use_all = sdk_list[0] == 'all'
  94. use_present = sdk_list[0] == 'present'
  95. if sdk_list[0] == '':
  96. sdk_list = []
  97. for sdk_name in PossibleSDKs:
  98. sdk = PossibleSDKs[sdk_name]
  99. if sdk.shouldBuild(builder.target, self.archs):
  100. if builder.options.hl2sdk_root:
  101. sdk_path = os.path.join(builder.options.hl2sdk_root, sdk.folder)
  102. else:
  103. sdk_path = ResolveEnvPath(sdk.envvar, sdk.folder)
  104. if sdk_path is None or not os.path.isdir(sdk_path):
  105. if use_all or sdk_name in sdk_list:
  106. raise Exception('Could not find a valid path for {0}'.format(sdk.envvar))
  107. continue
  108. if use_all or use_present or sdk_name in sdk_list:
  109. sdk.path = sdk_path
  110. self.sdks[sdk_name] = sdk
  111. if len(self.sdks) < 1 and len(sdk_list):
  112. raise Exception('No SDKs were found that build on {0}-{1}, nothing to do.'.format(
  113. builder.target.platform, builder.target.arch))
  114. def configure(self):
  115. if not set(self.archs).issubset(['x86', 'x64']):
  116. raise Exception('Unknown target architecture: {0}'.format(builder.target.arch))
  117. cxx = builder.DetectCxx()
  118. if cxx.like('msvc') and len(self.archs) > 1:
  119. raise Exception('Building multiple archs with MSVC is not currently supported')
  120. if cxx.behavior == 'gcc':
  121. cxx.defines += [
  122. 'stricmp=strcasecmp',
  123. '_stricmp=strcasecmp',
  124. '_snprintf=snprintf',
  125. '_vsnprintf=vsnprintf',
  126. '_alloca=alloca',
  127. 'GNUC'
  128. ]
  129. cxx.cflags += [ # todo: what is the difference between cflags and cxxflags
  130. '-fPIC',
  131. '-fno-exceptions',
  132. '-fno-rtti',
  133. '-msse',
  134. '-fno-strict-aliasing',
  135. ]
  136. if (cxx.version >= 'gcc-4.0') or cxx.family == 'clang':
  137. cxx.cflags += [
  138. '-fvisibility=hidden',
  139. '-fvisibility-inlines-hidden',
  140. '-std=c++11',
  141. ]
  142. # apple clang <-> llvm clang version correspondence is just a guess as there is no way to figure it out for real
  143. if (cxx.version >= 'gcc-4.7' or cxx.version >= 'clang-3.0' or cxx.version >= 'apple-clang-5.1'):
  144. cxx.cflags += [
  145. '-Wno-delete-non-virtual-dtor',
  146. '-Wno-unused-private-field',
  147. '-Wno-deprecated-register',
  148. ]
  149. if cxx.family == 'clang':
  150. if cxx.version >= 'clang-3.9' or cxx.version >= 'apple-clang-10.0':
  151. cxx.cxxflags += ['-Wno-expansion-to-defined']
  152. elif cxx.like('msvc'):
  153. # raise Exception('MSVC builds should use the Visual Studio projects until somebody implements support') # todo: implement MSVC support
  154. if builder.options.debug == '1':
  155. cxx.cflags += ['/MTd']
  156. cxx.linkflags += ['/NODEFAULTLIB:libcmt']
  157. else:
  158. cxx.cflags += ['/MT']
  159. cxx.defines += [
  160. '_CRT_SECURE_NO_DEPRECATE',
  161. '_CRT_SECURE_NO_WARNINGS',
  162. '_CRT_NONSTDC_NO_DEPRECATE',
  163. '_ITERATOR_DEBUG_LEVEL=0',
  164. 'WIN32',
  165. '_WINDOWS'
  166. ]
  167. cxx.cflags += [
  168. '/W3',
  169. ]
  170. cxx.cxxflags += [
  171. '/EHsc',
  172. '/GR-',
  173. '/TP',
  174. ]
  175. cxx.linkflags += [
  176. '/MACHINE:X86',
  177. ]
  178. # Optimization
  179. if builder.options.opt == '1':
  180. if cxx.behavior == 'gcc':
  181. cxx.cflags += proj_c_flags_opt
  182. # elif cxx.behavior == 'msvc': # todo: implement MSVC support
  183. # Debugging
  184. if builder.options.debug == '1':
  185. cxx.defines += ['_DEBUG']
  186. if cxx.behavior == 'gcc':
  187. cxx.cflags += proj_c_flags_dbg
  188. # elif cxx.behavior == 'msvc': # todo: implement MSVC support
  189. # This needs to be after our optimization flags which could otherwise disable it.
  190. # if cxx.family == 'msvc': # todo: implement MSVC support
  191. # Platform-specifics
  192. if builder.target.platform == 'linux':
  193. cxx.defines += [
  194. 'POSIX',
  195. '_LINUX',
  196. ]
  197. cxx.linkflags += ['-shared', '-lelf']
  198. if cxx.family == 'gcc':
  199. cxx.linkflags += ['-static-libgcc']
  200. elif builder.target.platform == 'mac':
  201. cxx.defines += [
  202. 'POSIX',
  203. 'OSX',
  204. '_OSX',
  205. ]
  206. cxx.cflags += ['-mmacosx-version-min=10.9']
  207. cxx.linkflags += [
  208. '-dynamiclib',
  209. '-lc++',
  210. '-mmacosx-version-min=10.9',
  211. ]
  212. # elif builder.target.platform == 'windows': # todo: implement MSVC support
  213. def HL2Compiler(self, context, sdk, arch):
  214. compiler = context.cxx.clone()
  215. compiler.cxxincludes += [
  216. os.path.join(context.currentSourcePath),
  217. ]
  218. defines = ['SE_' + PossibleSDKs[i].define + '=' + PossibleSDKs[i].code for i in PossibleSDKs]
  219. compiler.defines += defines
  220. paths = [
  221. ['public'],
  222. ['public', 'engine'],
  223. ['public', 'mathlib'],
  224. ['public', 'vstdlib'],
  225. ['public', 'tier0'],
  226. ['public', 'tier1'],
  227. ]
  228. if not builder.options.mms_path:
  229. raise Exception('Metamod:Source path is missing. Supply a --mms_path flag')
  230. if sdk.name == 'episode1' or sdk.name == 'darkm':
  231. paths.append(['public', 'dlls'])
  232. compiler.cxxincludes.append(os.path.join(builder.options.mms_path, 'core-legacy'))
  233. compiler.cxxincludes.append(os.path.join(builder.options.mms_path, 'core-legacy', 'sourcehook'))
  234. else:
  235. paths.append(['public', 'game', 'server'])
  236. compiler.cxxincludes.append(os.path.join(builder.options.mms_path, 'core'))
  237. compiler.cxxincludes.append(os.path.join(builder.options.mms_path, 'core', 'sourcehook'))
  238. compiler.defines += ['SOURCE_ENGINE=' + sdk.code]
  239. if sdk.name in ['sdk2013', 'bms'] and compiler.like('gcc'):
  240. # The 2013 SDK already has these in public/tier0/basetypes.h
  241. compiler.defines.remove('stricmp=strcasecmp')
  242. compiler.defines.remove('_stricmp=strcasecmp')
  243. compiler.defines.remove('_snprintf=snprintf')
  244. compiler.defines.remove('_vsnprintf=vsnprintf')
  245. if compiler.family == 'msvc':
  246. # todo: verify this for MSVC support
  247. compiler.defines += ['COMPILER_MSVC']
  248. if arch == 'x86':
  249. compiler.defines += ['COMPILER_MSVC32']
  250. elif arch == 'x64':
  251. compiler.defines += ['COMPILER_MSVC64']
  252. if compiler.version >= 1900:
  253. compiler.linkflags += ['legacy_stdio_definitions.lib']
  254. else: # todo: is it better to check compiler.behavior?
  255. compiler.defines += ['COMPILER_GCC']
  256. if sdk.name in ['css', 'hl2dm', 'dods', 'sdk2013', 'bms', 'tf2', 'l4d', 'nucleardawn', 'l4d2', 'dota', 'pvkii']:
  257. if builder.target.platform in ['linux', 'mac']:
  258. compiler.defines += ['NO_HOOK_MALLOC', 'NO_MALLOC_OVERRIDE']
  259. for path in paths:
  260. compiler.cxxincludes += [os.path.join(sdk.path, *path)]
  261. return compiler
  262. def AddVersioning(self, binary, arch):
  263. if builder.target.platform == 'windows':
  264. # todo: verify this for MSVC support
  265. # binary.sources += ['version.rc']
  266. # binary.compiler.rcdefines += [
  267. # 'BINARY_NAME="{0}"'.format(binary.outputFile),
  268. # 'RC_COMPILE'
  269. # ]
  270. pass
  271. elif builder.target.platform == 'mac' and binary.type == 'library':
  272. binary.compiler.postlink += [
  273. '-compatibility_version', '1.0.0',
  274. '-current_version', self.productVersion
  275. ]
  276. return binary
  277. def LibraryBuilder(self, compiler, name, arch):
  278. binary = compiler.Library(name)
  279. AppendArchSuffix(binary, name, arch)
  280. self.AddVersioning(binary, arch)
  281. return binary
  282. def HL2Library(self, context, name, sdk, arch):
  283. compiler = self.HL2Compiler(context, sdk, arch)
  284. SetArchFlags(compiler, arch, builder.target.platform)
  285. if builder.target.platform == 'linux':
  286. if sdk.name == 'episode1':
  287. lib_folder = os.path.join(sdk.path, 'linux_sdk')
  288. elif sdk.name in ['sdk2013', 'bms']:
  289. lib_folder = os.path.join(sdk.path, 'lib', 'public', 'linux32')
  290. elif arch == 'x64':
  291. lib_folder = os.path.join(sdk.path, 'lib', 'linux64')
  292. else:
  293. lib_folder = os.path.join(sdk.path, 'lib', 'linux')
  294. elif builder.target.platform == 'mac':
  295. if sdk.name in ['sdk2013', 'bms']:
  296. lib_folder = os.path.join(sdk.path, 'lib', 'public', 'osx32')
  297. elif arch == 'x64':
  298. lib_folder = os.path.join(sdk.path, 'lib', 'osx64')
  299. else:
  300. lib_folder = os.path.join(sdk.path, 'lib', 'mac')
  301. if builder.target.platform in ['linux', 'mac']:
  302. if sdk.name in ['sdk2013', 'bms'] or arch == 'x64':
  303. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'tier1.a'))]
  304. else:
  305. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'tier1_i486.a'))]
  306. if sdk.name in ['blade', 'insurgency', 'doi', 'csgo', 'dota']:
  307. if arch == 'x64':
  308. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'interfaces.a'))]
  309. else:
  310. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'interfaces_i486.a'))]
  311. if sdk.name == 'bms':
  312. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'mathlib.a'))]
  313. binary = self.LibraryBuilder(compiler, name, arch)
  314. dynamic_libs = [] # todo: this whole section is slightly different, but I imagine it is "more correct"
  315. if builder.target.platform == 'linux':
  316. compiler.linkflags[0:0] = ['-lm', '-ldl'] # todo: do we need -ldl?
  317. if sdk.name in ['css', 'hl2dm', 'dods', 'tf2', 'sdk2013', 'bms', 'nucleardawn', 'l4d2', 'insurgency', 'doi']:
  318. dynamic_libs = ['libtier0_srv.so', 'libvstdlib_srv.so']
  319. elif arch == 'x64' and sdk.name == 'csgo':
  320. dynamic_libs = ['libtier0_client.so', 'libvstdlib_client.so']
  321. elif sdk.name in ['l4d', 'blade', 'insurgency', 'doi', 'csgo', 'dota']:
  322. dynamic_libs = ['libtier0.so', 'libvstdlib.so']
  323. else:
  324. dynamic_libs = ['tier0_i486.so', 'vstdlib_i486.so']
  325. elif builder.target.platform == 'mac':
  326. binary.compiler.linkflags.append('-liconv')
  327. dynamic_libs = ['libtier0.dylib', 'libvstdlib.dylib']
  328. elif builder.target.platform == 'windows':
  329. # todo: verify this for MSVC support
  330. libs = ['tier0', 'tier1', 'vstdlib']
  331. if sdk.name in ['swarm', 'blade', 'insurgency', 'doi', 'csgo', 'dota']:
  332. libs.append('interfaces')
  333. if sdk.name == 'bms':
  334. libs.append('mathlib')
  335. for lib in libs:
  336. if arch == 'x86':
  337. lib_path = os.path.join(sdk.path, 'lib', 'public', lib) + '.lib'
  338. elif arch == 'x64':
  339. lib_path = os.path.join(sdk.path, 'lib', 'public', 'win64', lib) + '.lib'
  340. binary.compiler.linkflags.append(binary.Dep(lib_path))
  341. for library in dynamic_libs:
  342. source_path = os.path.join(lib_folder, library)
  343. output_path = os.path.join(binary.localFolder, library)
  344. def make_linker(source_path, output_path):
  345. def link(context, binary):
  346. cmd_node, (output,) = context.AddSymlink(source_path, output_path)
  347. return output
  348. return link
  349. linker = make_linker(source_path, output_path)
  350. binary.compiler.linkflags[0:0] = [binary.Dep(library, linker)]
  351. return binary
  352. MMS = MMSConfig()
  353. MMS.detectProductVersion()
  354. MMS.detectSDKs()
  355. MMS.configure()
  356. BuildScripts = [
  357. 'AMBuilder'
  358. ]
  359. if getattr(builder.options, 'enable_tests', False):
  360. BuildScripts += [] # add tests here
  361. import os
  362. if builder.backend == 'amb2':
  363. BuildScripts += [
  364. 'PackageScript',
  365. ]
  366. builder.Build(BuildScripts, { 'MMS': MMS })