AMBuildScript 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. # vim: set sts=2 ts=8 sw=2 tw=99 et ft=python:
  2. import os, sys
  3. # Simple extensions do not need to modify this file.
  4. class SDK(object):
  5. def __init__(self, sdk, ext, aDef, name, platform, dir):
  6. self.folder = 'hl2sdk-' + dir
  7. self.envvar = sdk
  8. self.ext = ext
  9. self.code = aDef
  10. self.define = name
  11. self.platform = platform
  12. self.name = dir
  13. self.path = None # Actual path
  14. WinOnly = ['windows']
  15. WinLinux = ['windows', 'linux']
  16. WinLinuxMac = ['windows', 'linux', 'mac']
  17. PossibleSDKs = {
  18. 'episode1': SDK('HL2SDK', '1.ep1', '1', 'EPISODEONE', WinLinux, 'episode1'),
  19. 'ep2': SDK('HL2SDKOB', '2.ep2', '3', 'ORANGEBOX', WinLinux, 'orangebox'),
  20. 'css': SDK('HL2SDKCSS', '2.css', '6', 'CSS', WinLinuxMac, 'css'),
  21. 'hl2dm': SDK('HL2SDKHL2DM', '2.hl2dm', '7', 'HL2DM', WinLinuxMac, 'hl2dm'),
  22. 'dods': SDK('HL2SDKDODS', '2.dods', '8', 'DODS', WinLinuxMac, 'dods'),
  23. 'sdk2013': SDK('HL2SDK2013', '2.sdk2013', '9', 'SDK2013', WinLinuxMac, 'sdk2013'),
  24. 'tf2': SDK('HL2SDKTF2', '2.tf2', '11', 'TF2', WinLinuxMac, 'tf2'),
  25. 'l4d': SDK('HL2SDKL4D', '2.l4d', '12', 'LEFT4DEAD', WinLinuxMac, 'l4d'),
  26. 'nucleardawn': SDK('HL2SDKND', '2.nd', '13', 'NUCLEARDAWN', WinLinuxMac, 'nucleardawn'),
  27. 'l4d2': SDK('HL2SDKL4D2', '2.l4d2', '15', 'LEFT4DEAD2', WinLinuxMac, 'l4d2'),
  28. 'darkm': SDK('HL2SDK-DARKM', '2.darkm', '2', 'DARKMESSIAH', WinOnly, 'darkm'),
  29. 'swarm': SDK('HL2SDK-SWARM', '2.swarm', '16', 'ALIENSWARM', WinOnly, 'swarm'),
  30. 'bgt': SDK('HL2SDK-BGT', '2.bgt', '4', 'BLOODYGOODTIME', WinOnly, 'bgt'),
  31. 'eye': SDK('HL2SDK-EYE', '2.eye', '5', 'EYE', WinOnly, 'eye'),
  32. 'csgo': SDK('HL2SDKCSGO', '2.csgo', '21', 'CSGO', WinLinuxMac, 'csgo'),
  33. 'portal2': SDK('HL2SDKPORTAL2', '2.portal2', '17', 'PORTAL2', [], 'portal2'),
  34. 'blade': SDK('HL2SDKBLADE', '2.blade', '18', 'BLADE', WinLinux, 'blade'),
  35. 'insurgency': SDK('HL2SDKINSURGENCY', '2.insurgency', '19', 'INSURGENCY', WinLinuxMac, 'insurgency'),
  36. 'contagion': SDK('HL2SDKCONTAGION', '2.contagion', '14', 'CONTAGION', WinOnly, 'contagion'),
  37. 'bms': SDK('HL2SDKBMS', '2.bms', '10', 'BMS', WinLinux, 'bms'),
  38. 'doi': SDK('HL2SDKDOI', '2.doi', '20', 'DOI', WinLinuxMac, 'doi'),
  39. }
  40. def ResolveEnvPath(env, folder):
  41. if env in os.environ:
  42. path = os.environ[env]
  43. if os.path.isdir(path):
  44. return path
  45. return None
  46. head = os.getcwd()
  47. oldhead = None
  48. while head != None and head != oldhead:
  49. path = os.path.join(head, folder)
  50. if os.path.isdir(path):
  51. return path
  52. oldhead = head
  53. head, tail = os.path.split(head)
  54. return None
  55. def Normalize(path):
  56. return os.path.abspath(os.path.normpath(path))
  57. class ExtensionConfig(object):
  58. def __init__(self):
  59. self.sdks = {}
  60. self.binaries = []
  61. self.extensions = []
  62. self.generated_headers = None
  63. self.mms_root = None
  64. self.sm_root = None
  65. @property
  66. def tag(self):
  67. if builder.options.debug == '1':
  68. return 'Debug'
  69. return 'Release'
  70. def detectSDKs(self):
  71. sdk_list = builder.options.sdks.split(',')
  72. use_all = sdk_list[0] == 'all'
  73. use_present = sdk_list[0] == 'present'
  74. for sdk_name in PossibleSDKs:
  75. sdk = PossibleSDKs[sdk_name]
  76. if builder.target_platform in sdk.platform:
  77. if builder.options.hl2sdk_root:
  78. sdk_path = os.path.join(builder.options.hl2sdk_root, sdk.folder)
  79. else:
  80. sdk_path = ResolveEnvPath(sdk.envvar, sdk.folder)
  81. if sdk_path is None or not os.path.isdir(sdk_path):
  82. if use_all or sdk_name in sdk_list:
  83. raise Exception('Could not find a valid path for {0}'.format(sdk.envvar))
  84. continue
  85. if use_all or use_present or sdk_name in sdk_list:
  86. sdk.path = Normalize(sdk_path)
  87. self.sdks[sdk_name] = sdk
  88. if len(self.sdks) < 1:
  89. raise Exception('At least one SDK must be available.')
  90. if builder.options.sm_path:
  91. self.sm_root = builder.options.sm_path
  92. else:
  93. self.sm_root = ResolveEnvPath('SOURCEMOD18', 'sourcemod-1.8')
  94. if not self.sm_root:
  95. self.sm_root = ResolveEnvPath('SOURCEMOD', 'sourcemod')
  96. if not self.sm_root:
  97. self.sm_root = ResolveEnvPath('SOURCEMOD_DEV', 'sourcemod-central')
  98. if not self.sm_root or not os.path.isdir(self.sm_root):
  99. raise Exception('Could not find a source copy of SourceMod')
  100. self.sm_root = Normalize(self.sm_root)
  101. if builder.options.mms_path:
  102. self.mms_root = builder.options.mms_path
  103. else:
  104. self.mms_root = ResolveEnvPath('MMSOURCE110', 'mmsource-1.10')
  105. if not self.mms_root:
  106. self.mms_root = ResolveEnvPath('MMSOURCE', 'metamod-source')
  107. if not self.mms_root:
  108. self.mms_root = ResolveEnvPath('MMSOURCE_DEV', 'mmsource-central')
  109. if not self.mms_root or not os.path.isdir(self.mms_root):
  110. raise Exception('Could not find a source copy of Metamod:Source')
  111. self.mms_root = Normalize(self.mms_root)
  112. def configure(self):
  113. cxx = builder.DetectCompilers()
  114. if cxx.like('gcc'):
  115. self.configure_gcc(cxx)
  116. elif cxx.vendor == 'msvc':
  117. self.configure_msvc(cxx)
  118. # Optimization
  119. if builder.options.opt == '1':
  120. cxx.defines += ['NDEBUG']
  121. # Debugging
  122. if builder.options.debug == '1':
  123. cxx.defines += ['DEBUG', '_DEBUG']
  124. # Platform-specifics
  125. if builder.target_platform == 'linux':
  126. self.configure_linux(cxx)
  127. elif builder.target_platform == 'mac':
  128. self.configure_mac(cxx)
  129. elif builder.target_platform == 'windows':
  130. self.configure_windows(cxx)
  131. # Finish up.
  132. cxx.includes += [
  133. os.path.join(self.sm_root, 'public'),
  134. ]
  135. def configure_gcc(self, cxx):
  136. cxx.defines += [
  137. 'stricmp=strcasecmp',
  138. '_stricmp=strcasecmp',
  139. '_snprintf=snprintf',
  140. '_vsnprintf=vsnprintf',
  141. 'HAVE_STDINT_H',
  142. 'GNUC',
  143. ]
  144. cxx.cflags += [
  145. '-pipe',
  146. '-fno-strict-aliasing',
  147. '-Wall',
  148. '-Werror',
  149. '-Wno-unused',
  150. '-Wno-switch',
  151. '-Wno-array-bounds',
  152. '-msse',
  153. '-m32',
  154. '-fvisibility=hidden',
  155. ]
  156. cxx.cxxflags += [
  157. '-std=c++14',
  158. '-fno-exceptions',
  159. '-fno-threadsafe-statics',
  160. '-Wno-non-virtual-dtor',
  161. '-Wno-overloaded-virtual',
  162. '-fvisibility-inlines-hidden',
  163. ]
  164. cxx.linkflags += ['-m32']
  165. have_gcc = cxx.vendor == 'gcc'
  166. have_clang = cxx.vendor == 'clang'
  167. if cxx.version >= 'clang-3.6':
  168. cxx.cxxflags += ['-Wno-inconsistent-missing-override']
  169. if have_clang or (cxx.version >= 'gcc-4.6'):
  170. cxx.cflags += ['-Wno-narrowing']
  171. if have_clang or (cxx.version >= 'gcc-4.7'):
  172. cxx.cxxflags += ['-Wno-delete-non-virtual-dtor']
  173. if cxx.version >= 'gcc-4.8':
  174. cxx.cflags += ['-Wno-unused-result']
  175. if have_clang:
  176. cxx.cxxflags += ['-Wno-implicit-exception-spec-mismatch']
  177. if cxx.version >= 'apple-clang-5.1' or cxx.version >= 'clang-3.4':
  178. cxx.cxxflags += ['-Wno-deprecated-register']
  179. else:
  180. cxx.cxxflags += ['-Wno-deprecated']
  181. cxx.cflags += ['-Wno-sometimes-uninitialized']
  182. if have_gcc:
  183. cxx.cflags += ['-mfpmath=sse']
  184. if builder.options.opt == '1':
  185. cxx.cflags += ['-O3']
  186. def configure_msvc(self, cxx):
  187. if builder.options.debug == '1':
  188. cxx.cflags += ['/MTd']
  189. cxx.linkflags += ['/NODEFAULTLIB:libcmt']
  190. else:
  191. cxx.cflags += ['/MT']
  192. cxx.defines += [
  193. '_CRT_SECURE_NO_DEPRECATE',
  194. '_CRT_SECURE_NO_WARNINGS',
  195. '_CRT_NONSTDC_NO_DEPRECATE',
  196. '_ITERATOR_DEBUG_LEVEL=0',
  197. ]
  198. cxx.cflags += [
  199. '/W3',
  200. ]
  201. cxx.cxxflags += [
  202. '/EHsc',
  203. '/GR-',
  204. '/TP',
  205. ]
  206. cxx.linkflags += [
  207. '/MACHINE:X86',
  208. 'kernel32.lib',
  209. 'user32.lib',
  210. 'gdi32.lib',
  211. 'winspool.lib',
  212. 'comdlg32.lib',
  213. 'advapi32.lib',
  214. 'shell32.lib',
  215. 'ole32.lib',
  216. 'oleaut32.lib',
  217. 'uuid.lib',
  218. 'odbc32.lib',
  219. 'odbccp32.lib',
  220. ]
  221. if builder.options.opt == '1':
  222. cxx.cflags += ['/Ox', '/Zo']
  223. cxx.linkflags += ['/OPT:ICF', '/OPT:REF']
  224. if builder.options.debug == '1':
  225. cxx.cflags += ['/Od', '/RTC1']
  226. # This needs to be after our optimization flags which could otherwise disable it.
  227. # Don't omit the frame pointer.
  228. cxx.cflags += ['/Oy-']
  229. def configure_linux(self, cxx):
  230. cxx.defines += ['_LINUX', 'POSIX']
  231. cxx.linkflags += ['-Wl,--exclude-libs,ALL', '-lm']
  232. if cxx.vendor == 'gcc':
  233. cxx.linkflags += ['-static-libgcc']
  234. elif cxx.vendor == 'clang':
  235. cxx.linkflags += ['-lgcc_eh']
  236. def configure_mac(self, cxx):
  237. cxx.defines += ['OSX', '_OSX', 'POSIX']
  238. cxx.cflags += ['-mmacosx-version-min=10.5']
  239. cxx.linkflags += [
  240. '-mmacosx-version-min=10.5',
  241. '-arch', 'i386',
  242. '-lstdc++',
  243. '-stdlib=libstdc++',
  244. ]
  245. cxx.cxxflags += ['-stdlib=libstdc++']
  246. def configure_windows(self, cxx):
  247. cxx.defines += ['WIN32', '_WINDOWS']
  248. def ConfigureForExtension(self, context, compiler):
  249. compiler.cxxincludes += [
  250. os.path.join(context.currentSourcePath),
  251. os.path.join(context.currentSourcePath, 'sdk'),
  252. os.path.join(self.sm_root, 'public'),
  253. os.path.join(self.sm_root, 'public', 'extensions'),
  254. os.path.join(self.sm_root, 'sourcepawn', 'include'),
  255. os.path.join(self.sm_root, 'public', 'amtl', 'amtl'),
  256. os.path.join(self.sm_root, 'public', 'amtl'),
  257. ]
  258. return compiler
  259. def ConfigureForHL2(self, binary, sdk):
  260. compiler = binary.compiler
  261. if sdk.name == 'episode1':
  262. mms_path = os.path.join(self.mms_root, 'core-legacy')
  263. else:
  264. mms_path = os.path.join(self.mms_root, 'core')
  265. compiler.cxxincludes += [
  266. os.path.join(mms_path),
  267. os.path.join(mms_path, 'sourcehook'),
  268. ]
  269. defines = ['SE_' + PossibleSDKs[i].define + '=' + PossibleSDKs[i].code for i in PossibleSDKs]
  270. compiler.defines += defines
  271. paths = [
  272. ['public'],
  273. ['public', 'engine'],
  274. ['public', 'mathlib'],
  275. ['public', 'vstdlib'],
  276. ['public', 'tier0'],
  277. ['public', 'tier1']
  278. ]
  279. if sdk.name == 'episode1' or sdk.name == 'darkm':
  280. paths.append(['public', 'dlls'])
  281. paths.append(['game_shared'])
  282. else:
  283. paths.append(['public', 'game', 'server'])
  284. paths.append(['public', 'toolframework'])
  285. paths.append(['game', 'shared'])
  286. paths.append(['common'])
  287. compiler.defines += ['SOURCE_ENGINE=' + sdk.code]
  288. if sdk.name in ['sdk2013', 'bms'] and compiler.like('gcc'):
  289. # The 2013 SDK already has these in public/tier0/basetypes.h
  290. compiler.defines.remove('stricmp=strcasecmp')
  291. compiler.defines.remove('_stricmp=strcasecmp')
  292. compiler.defines.remove('_snprintf=snprintf')
  293. compiler.defines.remove('_vsnprintf=vsnprintf')
  294. if compiler.like('msvc'):
  295. compiler.defines += ['COMPILER_MSVC', 'COMPILER_MSVC32']
  296. compiler.linkflags += ['legacy_stdio_definitions.lib']
  297. else:
  298. compiler.defines += ['COMPILER_GCC']
  299. # For everything after Swarm, this needs to be defined for entity networking
  300. # to work properly with sendprop value changes.
  301. if sdk.name in ['blade', 'insurgency', 'doi', 'csgo']:
  302. compiler.defines += ['NETWORK_VARS_ENABLED']
  303. if sdk.name in ['css', 'hl2dm', 'dods', 'sdk2013', 'bms', 'tf2', 'l4d', 'nucleardawn', 'l4d2']:
  304. if builder.target_platform in ['linux', 'mac']:
  305. compiler.defines += ['NO_HOOK_MALLOC', 'NO_MALLOC_OVERRIDE']
  306. if sdk.name == 'csgo' and builder.target_platform == 'linux':
  307. compiler.linkflags += ['-lstdc++']
  308. for path in paths:
  309. compiler.cxxincludes += [os.path.join(sdk.path, *path)]
  310. if builder.target_platform == 'linux':
  311. if sdk.name == 'episode1':
  312. lib_folder = os.path.join(sdk.path, 'linux_sdk')
  313. elif sdk.name in ['sdk2013', 'bms']:
  314. lib_folder = os.path.join(sdk.path, 'lib', 'public', 'linux32')
  315. else:
  316. lib_folder = os.path.join(sdk.path, 'lib', 'linux')
  317. elif builder.target_platform == 'mac':
  318. if sdk.name in ['sdk2013', 'bms']:
  319. lib_folder = os.path.join(sdk.path, 'lib', 'public', 'osx32')
  320. else:
  321. lib_folder = os.path.join(sdk.path, 'lib', 'mac')
  322. if builder.target_platform in ['linux', 'mac']:
  323. if sdk.name in ['sdk2013', 'bms']:
  324. compiler.postlink += [
  325. compiler.Dep(os.path.join(lib_folder, 'tier1.a')),
  326. compiler.Dep(os.path.join(lib_folder, 'mathlib.a'))
  327. ]
  328. else:
  329. compiler.postlink += [
  330. compiler.Dep(os.path.join(lib_folder, 'tier1_i486.a')),
  331. compiler.Dep(os.path.join(lib_folder, 'mathlib_i486.a'))
  332. ]
  333. if sdk.name in ['blade', 'insurgency', 'doi', 'csgo']:
  334. compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'interfaces_i486.a'))]
  335. dynamic_libs = []
  336. if builder.target_platform == 'linux':
  337. if sdk.name in ['css', 'hl2dm', 'dods', 'tf2', 'sdk2013', 'bms', 'nucleardawn', 'l4d2', 'insurgency', 'doi']:
  338. dynamic_libs = ['libtier0_srv.so', 'libvstdlib_srv.so']
  339. elif sdk.name in ['l4d', 'blade', 'insurgency', 'doi', 'csgo']:
  340. dynamic_libs = ['libtier0.so', 'libvstdlib.so']
  341. else:
  342. dynamic_libs = ['tier0_i486.so', 'vstdlib_i486.so']
  343. elif builder.target_platform == 'mac':
  344. compiler.linkflags.append('-liconv')
  345. dynamic_libs = ['libtier0.dylib', 'libvstdlib.dylib']
  346. elif builder.target_platform == 'windows':
  347. libs = ['tier0', 'tier1', 'vstdlib', 'mathlib']
  348. if sdk.name in ['swarm', 'blade', 'insurgency', 'doi', 'csgo']:
  349. libs.append('interfaces')
  350. for lib in libs:
  351. lib_path = os.path.join(sdk.path, 'lib', 'public', lib) + '.lib'
  352. compiler.linkflags.append(compiler.Dep(lib_path))
  353. for library in dynamic_libs:
  354. source_path = os.path.join(lib_folder, library)
  355. output_path = os.path.join(binary.localFolder, library)
  356. def make_linker(source_path, output_path):
  357. def link(context, binary):
  358. cmd_node, (output,) = context.AddSymlink(source_path, output_path)
  359. return output
  360. return link
  361. linker = make_linker(source_path, output_path)
  362. compiler.linkflags[0:0] = [compiler.Dep(library, linker)]
  363. return binary
  364. def HL2Library(self, context, name, sdk):
  365. binary = context.compiler.Library(name)
  366. self.ConfigureForExtension(context, binary.compiler)
  367. return self.ConfigureForHL2(binary, sdk)
  368. def HL2Project(self, context, name):
  369. project = context.compiler.LibraryProject(name)
  370. self.ConfigureForExtension(context, project.compiler)
  371. return project
  372. def HL2Config(self, project, name, sdk):
  373. binary = project.Configure(name, '{0} - {1}'.format(self.tag, sdk.name))
  374. return self.ConfigureForHL2(binary, sdk)
  375. Extension = ExtensionConfig()
  376. Extension.detectSDKs()
  377. Extension.configure()
  378. # Add additional buildscripts here
  379. BuildScripts = [
  380. 'AMBuilder',
  381. ]
  382. if builder.backend == 'amb2':
  383. BuildScripts += [
  384. 'PackageScript',
  385. ]
  386. builder.RunBuildScripts(BuildScripts, { 'Extension': Extension })