AMBuildScript 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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':
  254. cxx.defines += [
  255. 'WINDOWS',
  256. ]
  257. def HL2Compiler(self, context, sdk, arch):
  258. compiler = context.cxx.clone()
  259. compiler.cxxincludes += [
  260. os.path.join(context.currentSourcePath),
  261. ]
  262. defines = ['SE_' + PossibleSDKs[i].define + '=' + PossibleSDKs[i].code for i in PossibleSDKs]
  263. compiler.defines += defines
  264. paths = [
  265. ['public'],
  266. ['public', 'engine'],
  267. ['public', 'mathlib'],
  268. ['public', 'vstdlib'],
  269. ['public', 'tier0'],
  270. ['public', 'tier1'],
  271. ]
  272. if not builder.options.mms_path:
  273. raise Exception('Metamod:Source path is missing. Supply a --mms_path flag')
  274. if sdk.name == 'episode1' or sdk.name == 'darkm':
  275. paths.append(['public', 'dlls'])
  276. compiler.cxxincludes.append(os.path.join(builder.options.mms_path, 'core-legacy'))
  277. compiler.cxxincludes.append(os.path.join(builder.options.mms_path, 'core-legacy', 'sourcehook'))
  278. else:
  279. paths.append(['public', 'game', 'server'])
  280. compiler.cxxincludes.append(os.path.join(builder.options.mms_path, 'core'))
  281. compiler.cxxincludes.append(os.path.join(builder.options.mms_path, 'core', 'sourcehook'))
  282. compiler.defines += ['SOURCE_ENGINE=' + sdk.code]
  283. if sdk.name in ['sdk2013', 'bms'] and compiler.like('gcc'):
  284. # The 2013 SDK already has these in public/tier0/basetypes.h
  285. compiler.defines.remove('stricmp=strcasecmp')
  286. compiler.defines.remove('_stricmp=strcasecmp')
  287. compiler.defines.remove('_snprintf=snprintf')
  288. compiler.defines.remove('_vsnprintf=vsnprintf')
  289. if compiler.family == 'msvc':
  290. # todo: verify this for MSVC support
  291. compiler.defines += ['COMPILER_MSVC']
  292. if arch == 'x86':
  293. compiler.defines += ['COMPILER_MSVC32']
  294. elif arch == 'x64':
  295. compiler.defines += ['COMPILER_MSVC64']
  296. if compiler.version >= 1900:
  297. compiler.linkflags += ['legacy_stdio_definitions.lib']
  298. else: # todo: is it better to check compiler.behavior?
  299. compiler.defines += ['COMPILER_GCC']
  300. for path in paths:
  301. compiler.cxxincludes += [os.path.join(sdk.path, *path)]
  302. return compiler
  303. def AddVersioning(self, binary, arch):
  304. if builder.target.platform == 'windows':
  305. # todo: verify this for MSVC support
  306. # binary.sources += ['version.rc']
  307. # binary.compiler.rcdefines += [
  308. # 'BINARY_NAME="{0}"'.format(binary.outputFile),
  309. # 'RC_COMPILE'
  310. # ]
  311. pass
  312. elif builder.target.platform == 'mac' and binary.type == 'library':
  313. binary.compiler.postlink += [
  314. '-compatibility_version', '1.0.0',
  315. '-current_version', self.productVersion
  316. ]
  317. return binary
  318. def LibraryBuilder(self, compiler, name, arch):
  319. binary = compiler.Library(name)
  320. AppendArchSuffix(binary, name, arch)
  321. self.AddVersioning(binary, arch)
  322. return binary
  323. def HL2Library(self, context, name, sdk, arch):
  324. compiler = self.HL2Compiler(context, sdk, arch)
  325. SetArchFlags(compiler, arch, builder.target.platform)
  326. if builder.target.platform == 'linux':
  327. if sdk.name == 'episode1':
  328. lib_folder = os.path.join(sdk.path, 'linux_sdk')
  329. elif sdk.name in ['sdk2013', 'bms']:
  330. lib_folder = os.path.join(sdk.path, 'lib', 'public', 'linux32')
  331. elif arch == 'x64':
  332. lib_folder = os.path.join(sdk.path, 'lib', 'linux64')
  333. else:
  334. lib_folder = os.path.join(sdk.path, 'lib', 'linux')
  335. elif builder.target.platform == 'mac':
  336. if sdk.name in ['sdk2013', 'bms']:
  337. lib_folder = os.path.join(sdk.path, 'lib', 'public', 'osx32')
  338. elif arch == 'x64':
  339. lib_folder = os.path.join(sdk.path, 'lib', 'osx64')
  340. else:
  341. lib_folder = os.path.join(sdk.path, 'lib', 'mac')
  342. if builder.target.platform in ['linux', 'mac']:
  343. if sdk.name in ['sdk2013', 'bms'] or arch == 'x64':
  344. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'tier1.a'))]
  345. else:
  346. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'tier1_i486.a'))]
  347. if sdk.name in ['blade', 'insurgency', 'doi', 'csgo', 'dota']:
  348. if arch == 'x64':
  349. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'interfaces.a'))]
  350. else:
  351. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'interfaces_i486.a'))]
  352. if sdk.name == 'bms':
  353. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'mathlib.a'))]
  354. binary = self.LibraryBuilder(compiler, name, arch)
  355. dynamic_libs = [] # todo: this whole section is slightly different, but I imagine it is "more correct"
  356. if builder.target.platform == 'linux':
  357. binary.compiler.linkflags[0:0] = ['-lm', '-ldl', '-lelf'] # todo: do we need -ldl?
  358. if sdk.name in ['css', 'hl2dm', 'dods', 'tf2', 'sdk2013', 'bms', 'nucleardawn', 'l4d2', 'insurgency', 'doi']:
  359. dynamic_libs = ['libtier0_srv.so', 'libvstdlib_srv.so']
  360. elif arch == 'x64' and sdk.name == 'csgo':
  361. dynamic_libs = ['libtier0_client.so', 'libvstdlib_client.so']
  362. elif sdk.name in ['l4d', 'blade', 'insurgency', 'doi', 'csgo', 'dota']:
  363. dynamic_libs = ['libtier0.so', 'libvstdlib.so']
  364. else:
  365. dynamic_libs = ['tier0_i486.so', 'vstdlib_i486.so']
  366. elif builder.target.platform == 'mac':
  367. binary.compiler.linkflags.append('-liconv')
  368. dynamic_libs = ['libtier0.dylib', 'libvstdlib.dylib']
  369. elif builder.target.platform == 'windows':
  370. # todo: verify this for MSVC support
  371. libs = ['tier0', 'tier1', 'vstdlib']
  372. if sdk.name in ['swarm', 'blade', 'insurgency', 'doi', 'csgo', 'dota']:
  373. libs.append('interfaces')
  374. if sdk.name == 'bms':
  375. libs.append('mathlib')
  376. for lib in libs:
  377. if arch == 'x86':
  378. lib_path = os.path.join(sdk.path, 'lib', 'public', lib) + '.lib'
  379. elif arch == 'x64':
  380. lib_path = os.path.join(sdk.path, 'lib', 'public', 'win64', lib) + '.lib'
  381. binary.compiler.linkflags.append(binary.Dep(lib_path))
  382. for library in dynamic_libs:
  383. source_path = os.path.join(lib_folder, library)
  384. output_path = os.path.join(binary.localFolder, library)
  385. def make_linker(source_path, output_path):
  386. def link(context, binary):
  387. cmd_node, (output,) = context.AddSymlink(source_path, output_path)
  388. return output
  389. return link
  390. linker = make_linker(source_path, output_path)
  391. binary.compiler.linkflags[0:0] = [binary.Dep(library, linker)]
  392. return binary
  393. MMS = MMSConfig()
  394. MMS.detectProductVersion()
  395. MMS.detectSDKs()
  396. MMS.configure()
  397. BuildScripts = [] # add sub-modules here
  398. if getattr(builder.options, 'enable_tests', False):
  399. BuildScripts += [] # add tests here
  400. import os
  401. for sdk_name in MMS.sdks:
  402. for arch in MMS.archs:
  403. sdk = MMS.sdks[sdk_name]
  404. if not arch in sdk.platformSpec[builder.target.platform]:
  405. continue
  406. name = proj_name + '.' + sdk.ext
  407. binary = MMS.HL2Library(builder, name, sdk, arch)
  408. binary.sources += proj_srcs
  409. nodes = builder.Add(binary)
  410. MMS.binaries += [nodes]
  411. builder.Build(BuildScripts, { 'MMS': MMS })