AMBuildScript 15 KB

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