AMBuildScript 13 KB

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