AMBuildScript 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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']
  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. for path in paths:
  257. compiler.cxxincludes += [os.path.join(sdk.path, *path)]
  258. return compiler
  259. def AddVersioning(self, binary, arch):
  260. if builder.target.platform == 'windows':
  261. # todo: verify this for MSVC support
  262. # binary.sources += ['version.rc']
  263. # binary.compiler.rcdefines += [
  264. # 'BINARY_NAME="{0}"'.format(binary.outputFile),
  265. # 'RC_COMPILE'
  266. # ]
  267. pass
  268. elif builder.target.platform == 'mac' and binary.type == 'library':
  269. binary.compiler.postlink += [
  270. '-compatibility_version', '1.0.0',
  271. '-current_version', self.productVersion
  272. ]
  273. return binary
  274. def LibraryBuilder(self, compiler, name, arch):
  275. binary = compiler.Library(name)
  276. AppendArchSuffix(binary, name, arch)
  277. self.AddVersioning(binary, arch)
  278. return binary
  279. def HL2Library(self, context, name, sdk, arch):
  280. compiler = self.HL2Compiler(context, sdk, arch)
  281. SetArchFlags(compiler, arch, builder.target.platform)
  282. if builder.target.platform == 'linux':
  283. if sdk.name == 'episode1':
  284. lib_folder = os.path.join(sdk.path, 'linux_sdk')
  285. elif sdk.name in ['sdk2013', 'bms']:
  286. lib_folder = os.path.join(sdk.path, 'lib', 'public', 'linux32')
  287. elif arch == 'x64':
  288. lib_folder = os.path.join(sdk.path, 'lib', 'linux64')
  289. else:
  290. lib_folder = os.path.join(sdk.path, 'lib', 'linux')
  291. elif builder.target.platform == 'mac':
  292. if sdk.name in ['sdk2013', 'bms']:
  293. lib_folder = os.path.join(sdk.path, 'lib', 'public', 'osx32')
  294. elif arch == 'x64':
  295. lib_folder = os.path.join(sdk.path, 'lib', 'osx64')
  296. else:
  297. lib_folder = os.path.join(sdk.path, 'lib', 'mac')
  298. if builder.target.platform in ['linux', 'mac']:
  299. if sdk.name in ['sdk2013', 'bms'] or arch == 'x64':
  300. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'tier1.a'))]
  301. else:
  302. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'tier1_i486.a'))]
  303. if sdk.name in ['blade', 'insurgency', 'doi', 'csgo', 'dota']:
  304. if arch == 'x64':
  305. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'interfaces.a'))]
  306. else:
  307. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'interfaces_i486.a'))]
  308. if sdk.name == 'bms':
  309. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'mathlib.a'))]
  310. binary = self.LibraryBuilder(compiler, name, arch)
  311. dynamic_libs = [] # todo: this whole section is slightly different, but I imagine it is "more correct"
  312. if builder.target.platform == 'linux':
  313. compiler.linkflags[0:0] = ['-lm', '-ldl'] # todo: do we need -ldl?
  314. if sdk.name in ['css', 'hl2dm', 'dods', 'tf2', 'sdk2013', 'bms', 'nucleardawn', 'l4d2', 'insurgency', 'doi']:
  315. dynamic_libs = ['libtier0_srv.so', 'libvstdlib_srv.so']
  316. elif arch == 'x64' and sdk.name == 'csgo':
  317. dynamic_libs = ['libtier0_client.so', 'libvstdlib_client.so']
  318. elif sdk.name in ['l4d', 'blade', 'insurgency', 'doi', 'csgo', 'dota']:
  319. dynamic_libs = ['libtier0.so', 'libvstdlib.so']
  320. else:
  321. dynamic_libs = ['tier0_i486.so', 'vstdlib_i486.so']
  322. elif builder.target.platform == 'mac':
  323. binary.compiler.linkflags.append('-liconv')
  324. dynamic_libs = ['libtier0.dylib', 'libvstdlib.dylib']
  325. elif builder.target.platform == 'windows':
  326. # todo: verify this for MSVC support
  327. libs = ['tier0', 'tier1', 'vstdlib']
  328. if sdk.name in ['swarm', 'blade', 'insurgency', 'doi', 'csgo', 'dota']:
  329. libs.append('interfaces')
  330. if sdk.name == 'bms':
  331. libs.append('mathlib')
  332. for lib in libs:
  333. if arch == 'x86':
  334. lib_path = os.path.join(sdk.path, 'lib', 'public', lib) + '.lib'
  335. elif arch == 'x64':
  336. lib_path = os.path.join(sdk.path, 'lib', 'public', 'win64', lib) + '.lib'
  337. binary.compiler.linkflags.append(binary.Dep(lib_path))
  338. for library in dynamic_libs:
  339. source_path = os.path.join(lib_folder, library)
  340. output_path = os.path.join(binary.localFolder, library)
  341. def make_linker(source_path, output_path):
  342. def link(context, binary):
  343. cmd_node, (output,) = context.AddSymlink(source_path, output_path)
  344. return output
  345. return link
  346. linker = make_linker(source_path, output_path)
  347. binary.compiler.linkflags[0:0] = [binary.Dep(library, linker)]
  348. return binary
  349. MMS = MMSConfig()
  350. MMS.detectProductVersion()
  351. MMS.detectSDKs()
  352. MMS.configure()
  353. BuildScripts = [
  354. 'AMBuilder'
  355. ]
  356. if getattr(builder.options, 'enable_tests', False):
  357. BuildScripts += [] # add tests here
  358. import os
  359. if builder.backend == 'amb2':
  360. BuildScripts += [
  361. 'PackageScript',
  362. ]
  363. builder.Build(BuildScripts, { 'MMS': MMS })