AMBuildScript 14 KB

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