AMBuildScript
author Scott Ehlert <ds@alliedmods.net>
Sat May 17 16:19:00 2014 -0500 (2014-05-17)
changeset 914 d0ea7151b71d
parent 909 57797db1d513
child 916 f280cbd99586
permissions -rw-r--r--
Sync changes from SourceMod's bootstrap script.
     1 # vim: set sts=2 ts=8 sw=2 tw=99 et ft=python:
     2 import os, sys
     3 
     4 class SDK(object):
     5   def __init__(self, sdk, ext, aDef, name, platform, dir):
     6     if dir == 'ep1':
     7       folder = 'hl2sdk'
     8     else:
     9       folder = 'hl2sdk-' + dir
    10     self.envvar = sdk
    11     self.ext = ext
    12     self.code = aDef
    13     self.define = name
    14     self.platform = platform
    15     self.folder = folder # Default folder name.
    16     self.name = dir
    17     self.path = None # Actual path
    18 
    19 WinOnly = ['windows']
    20 WinLinux = ['windows', 'linux']
    21 WinLinuxMac = ['windows', 'linux', 'mac']
    22 
    23 PossibleSDKs = {
    24   'ep1':  SDK('HL2SDK', '1.ep1', '1', 'EPISODEONE', WinLinux, 'ep1'),
    25   'ep2':  SDK('HL2SDKOB', '2.ep2', '3', 'ORANGEBOX', WinLinux, 'ob'),
    26   'css':  SDK('HL2SDKCSS', '2.css', '6', 'CSS', WinLinuxMac, 'css'),
    27   'hl2dm':  SDK('HL2SDKHL2DM', '2.hl2dm', '7', 'HL2DM', WinLinuxMac, 'hl2dm'),
    28   'dods': SDK('HL2SDKDODS', '2.dods', '8', 'DODS', WinLinuxMac, 'dods'),
    29   'sdk2013': SDK('HL2SDK2013', '2.sdk2013', '9', 'SDK2013', WinLinuxMac, '2013'),
    30   'tf2':  SDK('HL2SDKTF2', '2.tf2', '10', 'TF2', WinLinuxMac, 'tf2'),
    31   'l4d':  SDK('HL2SDKL4D', '2.l4d', '11', 'LEFT4DEAD', WinLinuxMac, 'l4d'),
    32   'nd': SDK('HL2SDKND', '2.nd', '12', 'NUCLEARDAWN', WinLinuxMac, 'nd'),
    33   'l4d2': SDK('HL2SDKL4D2', '2.l4d2', '14', 'LEFT4DEAD2', WinLinuxMac, 'l4d2'),
    34   'darkm':  SDK('HL2SDK-DARKM', '2.darkm', '2', 'DARKMESSIAH', WinOnly, 'darkm'),
    35   'swarm':  SDK('HL2SDK-SWARM', '2.swarm', '15', 'ALIENSWARM', WinOnly, 'swarm'),
    36   'bgt':  SDK('HL2SDK-BGT', '2.bgt', '4', 'BLOODYGOODTIME', WinOnly, 'bgt'),
    37   'eye':  SDK('HL2SDK-EYE', '2.eye', '5', 'EYE', WinOnly, 'eye'),
    38   'csgo': SDK('HL2SDKCSGO', '2.csgo', '19', 'CSGO', WinLinuxMac, 'csgo'),
    39   'dota': SDK('HL2SDKDOTA', '2.dota', '20', 'DOTA', WinLinuxMac, 'dota'),
    40   'portal2':  SDK('HL2SDKPORTAL2', '2.portal2', '16', 'PORTAL2', [], 'portal2'),
    41   'blade':  SDK('HL2SDKBLADE', '2.blade', '17', 'BLADE', WinLinux, 'blade'),
    42   'insurgency':  SDK('HL2SDKINSURGENCY', '2.insurgency', '18', 'INSURGENCY', WinLinuxMac, 'insurgency'),
    43   'contagion':  SDK('HL2SDKCONTAGION', '2.contagion', '13', 'CONTAGION', WinOnly, 'contagion'),
    44 }
    45 
    46 def ResolveEnvPath(env, folder):
    47   if env in os.environ:
    48     path = os.environ[env]
    49     if os.path.isdir(path):
    50       return path
    51   else:
    52     head = os.getcwd()
    53     oldhead = None
    54     while head != None and head != oldhead:
    55       path = os.path.join(head, folder)
    56       if os.path.isdir(path):
    57         return path
    58       oldhead = head
    59       head, tail = os.path.split(head)
    60   return None
    61 
    62 class MMSConfig(object):
    63   def __init__(self):
    64     self.sdks = {}
    65     self.binaries = []
    66     self.generated_headers = None
    67     self.versionlib = None
    68 
    69   def detectProductVersion(self):
    70     builder.AddConfigureFile('product.version')
    71 
    72     # For OS X dylib versioning
    73     import re
    74     with open(os.path.join(builder.sourcePath, 'product.version'), 'r') as fp:
    75       productContents = fp.read()
    76     m = re.match('(\d+)\.(\d+)\.(\d+).*', productContents)
    77     if m == None:
    78       self.productVersion = '1.0.0'
    79     else:
    80       major, minor, release = m.groups()
    81       self.productVersion = '{0}.{1}.{2}'.format(major, minor, release)
    82 
    83   def detectSDKs(self):
    84     sdk_list = builder.options.sdks.split(',')
    85     use_all = sdk_list[0] == 'all'
    86     use_present = sdk_list[0] == 'present'
    87 
    88     for sdk_name in PossibleSDKs:
    89       sdk = PossibleSDKs[sdk_name]
    90       if builder.target_platform in sdk.platform:
    91         sdk_path = ResolveEnvPath(sdk.envvar, sdk.folder)
    92         if sdk_path is None:
    93           if use_all or sdk_name in sdk_list:
    94             raise Exception('Could not find a valid path for {0}'.format(sdk.envvar))
    95           continue
    96         if use_all or use_present or sdk_name in sdk_list:
    97           sdk.path = sdk_path
    98           self.sdks[sdk_name] = sdk
    99 
   100     if len(self.sdks) < 1:
   101       raise Exception('At least one SDK must be available.')
   102 
   103   def configure(self):
   104     builder.AddConfigureFile('pushbuild.txt')
   105 
   106     cfg = builder.DetectCompilers()
   107     cxx = cfg.cxx
   108 
   109     if cxx.behavior == 'gcc':
   110       cfg.defines += [
   111         'stricmp=strcasecmp',
   112         '_stricmp=strcasecmp',
   113         '_snprintf=snprintf',
   114         '_vsnprintf=vsnprintf',
   115         'HAVE_STDINT_H',
   116         'GNUC',
   117       ]
   118       cfg.cflags += [
   119         '-pipe',
   120         '-fno-strict-aliasing',
   121         '-Wall',
   122         '-Werror',
   123         '-Wno-uninitialized',
   124         '-Wno-unused',
   125         '-Wno-switch',
   126         '-msse',
   127         '-m32',
   128       ]
   129       if (cxx.name == 'gcc' and cxx.majorVersion >= 4) or cxx.name == 'clang':
   130         cfg.cflags += ['-fvisibility=hidden']
   131         cfg.cxxflags += ['-fvisibility-inlines-hidden']
   132       cfg.linkflags += ['-m32']
   133       cfg.cxxflags += [
   134         '-fno-exceptions',
   135         '-fno-rtti',
   136         '-fno-threadsafe-statics',
   137         '-Wno-non-virtual-dtor',
   138         '-Wno-overloaded-virtual',
   139       ]
   140       if (cxx.name == 'gcc' and cxx.majorVersion >= 4 and cxx.minorVersion >= 7) or \
   141           (cxx.name == 'clang' and cxx.majorVersion >= 3):
   142         cfg.cxxflags += ['-Wno-delete-non-virtual-dtor']
   143       if cxx.name == 'gcc':
   144         cfg.cflags += ['-mfpmath=sse']
   145 
   146     elif cxx.name == 'msvc':
   147       if builder.options.debug == '1':
   148         cfg.cflags += ['/MTd']
   149         cfg.linkflags += ['/NODEFAULTLIB:libcmt']
   150       else:
   151         cfg.cflags += ['/MT']
   152       cfg.defines += [
   153         '_CRT_SECURE_NO_DEPRECATE',
   154         '_CRT_SECURE_NO_WARNINGS',
   155         '_CRT_NONSTDC_NO_DEPRECATE',
   156       ]
   157       cfg.cflags += [
   158         '/W3',
   159         '/Zi',
   160       ]
   161       cfg.cxxflags += ['/TP']
   162       cfg.linkflags += [
   163         '/MACHINE:X86',
   164         '/SUBSYSTEM:WINDOWS',
   165         'kernel32.lib',
   166         'user32.lib',
   167         'gdi32.lib',
   168         'winspool.lib',
   169         'comdlg32.lib',
   170         'advapi32.lib',
   171         'shell32.lib',
   172         'ole32.lib',
   173         'oleaut32.lib',
   174         'uuid.lib',
   175         'odbc32.lib',
   176         'odbccp32.lib',
   177       ] 
   178 
   179     # Optimization
   180     if builder.options.opt == '1':
   181       cfg.defines += ['NDEBUG']
   182       if cxx.behavior == 'gcc':
   183         cfg.cflags += ['-O3']
   184       elif cxx.behavior == 'msvc':
   185         cfg.cflags += ['/Ox']
   186         cfg.linkflags += ['/OPT:ICF', '/OPT:REF']
   187 
   188     # Debugging
   189     if builder.options.debug == '1':
   190       cfg.defines += ['DEBUG', '_DEBUG']
   191       if cxx.behavior == 'gcc':
   192         cfg.cflags += ['-g3']
   193       elif cxx.behavior == 'msvc':
   194         cfg.cflags += ['/Od, /RTC1']
   195 
   196     # This needs to be after our optimization flags which could otherwise disable it.
   197     if cxx.name == 'msvc':
   198       # Don't omit the frame pointer.
   199       cfg.cflags += ['/Oy-']
   200 
   201     # Platform-specifics
   202     if builder.target_platform == 'linux':
   203       cfg.defines += ['_LINUX', 'POSIX']
   204       if cxx.name == 'gcc':
   205         cfg.linkflags += ['-static-libgcc']
   206       elif cxx.name == 'clang':
   207         cfg.linkflags += ['-lgcc_eh']
   208     elif builder.target_platform == 'mac':
   209       cfg.defines += ['OSX', '_OSX', 'POSIX']
   210       cfg.cflags += ['-mmacosx-version-min=10.5']
   211       cfg.linkflags += [
   212         '-mmacosx-version-min=10.5',
   213         '-arch', 'i386',
   214         '-lstdc++',
   215       ]
   216     elif builder.target_platform == 'windows':
   217       cfg.defines += ['WIN32', '_WINDOWS']
   218 
   219     # Finish up.
   220     cfg.defines += ['MMS_GENERATED_BUILD']
   221     cfg.includes += [os.path.join(builder.buildPath, 'includes')]
   222     cfg.includes += [os.path.join(builder.sourcePath, 'versionlib')]
   223     cfg.cxxincludes += [os.path.join(builder.sourcePath, 'public')]
   224 
   225   def HL2Compiler(self, context, sdk):
   226     compiler = context.compiler.clone()
   227     compiler.cxxincludes += [
   228       os.path.join(context.currentSourcePath),
   229       os.path.join(context.currentSourcePath, 'sourcehook'),
   230       os.path.join(context.sourcePath, 'loader'),
   231     ]
   232 
   233     defines = ['SE_' + self.sdks[i].define + '=' + self.sdks[i].code for i in self.sdks]
   234     compiler.defines += defines
   235     paths = [['public'],
   236              ['public', 'engine'],
   237              ['public', 'mathlib'],
   238              ['public', 'vstdlib'],
   239              ['public', 'tier0'], ['public', 'tier1']]
   240     if sdk.name == 'ep1' 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(['game', 'shared'])
   246       paths.append(['common'])
   247 
   248     compiler.defines += ['SOURCE_ENGINE=' + sdk.code]
   249 
   250     if sdk.name == '2013' and compiler.cxx.behavior == '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 
   257     if compiler.cc.behavior == 'msvc':
   258       compiler.defines += ['COMPILER_MSVC', 'COMPILER_MSVC32']
   259     else:
   260       compiler.defines += ['COMPILER_GCC']
   261 
   262     if sdk.name in ['css', 'hl2dm', 'dods', '2013', 'tf2', 'nd', 'l4d2', 'dota']:
   263       if builder.target_platform in ['linux', 'mac']:
   264         compiler.defines += ['NO_HOOK_MALLOC', 'NO_MALLOC_OVERRIDE']
   265 
   266     for path in paths:
   267       compiler.cxxincludes += [os.path.join(sdk.path, *path)]
   268 
   269     return compiler
   270 
   271   def LibraryBuilder(self, compiler, name):
   272     binary = compiler.Library(name)
   273     if builder.target_platform == 'windows':
   274       binary.sources += ['version.rc']
   275       binary.compiler.rcdefines += [
   276         'BINARY_NAME="{0}"'.format(binary.outputFile),
   277         'MMS_GENERATED_BUILD'
   278       ]
   279     elif builder.target_platform == 'mac':
   280       binary.compiler.postlink += [
   281         '-compatibility_version', '1.0.0',
   282         '-current_version', self.productVersion
   283       ]
   284     binary.compiler.linkflags += [self.versionlib]
   285     binary.compiler.sourcedeps += MMS.generated_headers
   286     return binary
   287 
   288   def Library(self, context, name):
   289     compiler = context.compiler.clone()
   290     return self.LibraryBuilder(compiler, name)
   291 
   292   def HL2Library(self, context, name, sdk):
   293     compiler = self.HL2Compiler(context, sdk)
   294 
   295     if builder.target_platform == 'linux':
   296       if sdk.name == 'ep1':
   297         lib_folder = os.path.join(sdk.path, 'linux_sdk')
   298       elif sdk.name == '2013':
   299         lib_folder = os.path.join(sdk.path, 'lib', 'public', 'linux32')
   300       else:
   301         lib_folder = os.path.join(sdk.path, 'lib', 'linux')
   302     elif builder.target_platform == 'mac':
   303       if sdk.name == '2013':
   304         lib_folder = os.path.join(sdk.path, 'lib', 'public', 'osx32')
   305       else:
   306         lib_folder = os.path.join(sdk.path, 'lib', 'mac')
   307 
   308     if builder.target_platform in ['linux', 'mac']:
   309       if sdk.name == '2013':
   310         compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'tier1.a'))]
   311       else:
   312         compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'tier1_i486.a'))]
   313 
   314       if sdk.name in ['blade', 'insurgency', 'csgo', 'dota']:
   315         compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'interfaces_i486.a'))]
   316 
   317     binary = self.LibraryBuilder(compiler, name)
   318 
   319     dynamic_libs = []
   320     if builder.target_platform == 'linux':
   321       compiler.linkflags[0:0] = ['-lm']
   322       if sdk.name in ['css', 'hl2dm', 'dods', 'tf2', '2013', 'nd', 'l4d2']:
   323         dynamic_libs = ['libtier0_srv.so', 'libvstdlib_srv.so']
   324       elif sdk.name in ['l4d', 'blade', 'insurgency', 'csgo', 'dota']:
   325         dynamic_libs = ['libtier0.so', 'libvstdlib.so']
   326       else:
   327         dynamic_libs = ['tier0_i486.so', 'vstdlib_i486.so']
   328     elif builder.target_platform == 'mac':
   329       binary.compiler.linkflags.append('-liconv')
   330       dynamic_libs = ['libtier0.dylib', 'libvstdlib.dylib']
   331     elif builder.target_platform == 'windows':
   332       libs = ['tier0', 'tier1', 'vstdlib']
   333       if sdk.name in ['swarm', 'blade', 'insurgency', 'csgo', 'dota']:
   334         libs.append('interfaces')
   335       for lib in libs:
   336         lib_path = os.path.join(sdk.path, 'lib', 'public', lib) + '.lib'
   337         binary.compiler.linkflags.append(binary.Dep(lib_path))
   338 
   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 
   343       def make_linker(source_path, output_path):
   344         def link(context, binary):
   345           cmd_node, (output,) = context.AddSymlink(source_path, output_path)
   346           return output
   347         return link
   348 
   349       linker = make_linker(source_path, output_path)
   350       binary.compiler.linkflags[0:0] = [binary.Dep(library, linker)]
   351 
   352     return binary
   353 
   354 MMS = MMSConfig()
   355 MMS.detectProductVersion()
   356 MMS.detectSDKs()
   357 MMS.configure()
   358 
   359 MMS.generated_headers = builder.RunScript(
   360   'support/buildbot/Versioning',
   361   { 'MMS': MMS }
   362 )
   363 MMS.versionlib = builder.RunScript(
   364   'versionlib/AMBuildScript',
   365   { 'MMS': MMS }
   366 )
   367 
   368 builder.RunBuildScripts(
   369   [
   370     'loader/AMBuilder',
   371     'core-legacy/AMBuilder',
   372     'core/AMBuilder',
   373     'support/buildbot/PackageScript',
   374   ],
   375   {
   376     'MMS': MMS
   377   }
   378 )
   379