AMBuildScript 16 KB

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