AMBuildScript 13 KB

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