AMBuildScript 16 KB

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