AMBuildScript 14 KB

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