Skip to content
Snippets Groups Projects
SConstruct 4.06 KiB
Newer Older
# -*- python -*-
import os
import os.path
Dan Hirsch's avatar
Dan Hirsch committed
import sys

Dan Hirsch's avatar
Dan Hirsch committed
vars = Variables(None, ARGUMENTS)
vars.Add(PathVariable('DESTDIR', "Root directory to install in (useful for packaging scripts)", None, PathVariable.PathIsDirCreate))
vars.Add(PathVariable('prefix', "Where to install in the FHS", "/usr/local", PathVariable.PathAccept))
vars.Add(ListVariable('bindings', 'Language bindings to build', 'none', ['dotnet', 'perl', 'php', 'python']))
Dan Hirsch's avatar
Dan Hirsch committed

env = Environment(ENV = {'PATH' : os.environ['PATH']},
                  variables = vars,
                  tools=['default', 'scanreplace', 'csharp/mono'],
                  toolpath=['tools'])
Dan Hirsch's avatar
Dan Hirsch committed

if not 'bindings' in env:
    env['bindings'] = []

Dan Hirsch's avatar
Dan Hirsch committed
def calcInstallPath(*elements):
    path = os.path.abspath(os.path.join(*map(env.subst, elements)))
    if 'DESTDIR' in env:
        path = os.path.join(env['DESTDIR'], os.path.relpath(path, start="/"))
    return path

rel_prefix = not os.path.isabs(env['prefix'])
env['prefix'] = os.path.abspath(env['prefix'])
if 'DESTDIR' in env:
    env['DESTDIR'] = os.path.abspath(env['DESTDIR'])
    if rel_prefix:
        print >>sys.stderr, "--!!-- You used a relative prefix with a DESTDIR. This is probably not what you"
        print >>sys.stderr, "--!!-- you want; files will be installed in"
        print >>sys.stderr, "--!!--    %s" % (calcInstallPath("$prefix"),)


env['libpath'] = calcInstallPath("$prefix", "lib")
if env['PLATFORM'] != 'darwin' and platform.machine()[-2:] == '64':
    env['libpath'] += '64'
Dan Hirsch's avatar
Dan Hirsch committed
env['incpath'] = calcInstallPath("$prefix", "include", "hammer")
env['parsersincpath'] = calcInstallPath("$prefix", "include", "hammer", "parsers")
env['backendsincpath'] = calcInstallPath("$prefix", "include", "hammer", "backends")
env['pkgconfigpath'] = calcInstallPath("$prefix", "lib", "pkgconfig")
env.ScanReplace('libhammer.pc.in')
Dan Hirsch's avatar
Dan Hirsch committed

Joe Rozner's avatar
Joe Rozner committed
env.MergeFlags("-std=gnu99 -Wall -Wextra -Werror -Wno-unused-parameter -Wno-attributes")
if env['PLATFORM'] == 'darwin':
Joe Rozner's avatar
Joe Rozner committed
    env.Append(SHLINKFLAGS = '-install_name ' + env["libpath"] + '/${TARGET.file}')
else:
    env.MergeFlags("-lrt")

AddOption("--variant",
          dest="variant",
          nargs=1, type="choice",
          choices=["debug", "opt"],
Dan Hirsch's avatar
Dan Hirsch committed
          default="opt",
          action="store",
          help="Build variant (debug or opt)")

Dan Hirsch's avatar
Dan Hirsch committed
AddOption("--coverage",
          dest="coverage",
          default=False,
          action="store_true",
          help="Build with coverage instrumentation")

AddOption("--in-place",
          dest="in_place",
          default=False,
          action="store_true",
          help="Build in-place, rather than in the build/<variant> tree")


dbg = env.Clone(VARIANT='debug')
dbg.Append(CCFLAGS=['-g'])

opt = env.Clone(VARIANT='opt')

if GetOption("variant") == 'debug':
    env = dbg
else:
    env = opt
Dan Hirsch's avatar
Dan Hirsch committed
if GetOption("coverage"):
    env.Append(CFLAGS=["-fprofile-arcs", "-ftest-coverage"],
               CXXFLAGS=["-fprofile-arcs", "-ftest-coverage"],
               LDFLAGS=["-fprofile-arcs", "-ftest-coverage"],
               LIBS=['gcov'])

env["CC"] = os.getenv("CC") or env["CC"]
env["CXX"] = os.getenv("CXX") or env["CXX"]

if os.getenv("CC") == "clang" or env['PLATFORM'] == 'darwin':
    env.Replace(CC="clang",
                CXX="clang++")
env["ENV"].update(x for x in os.environ.items() if x[0].startswith("CCC_"))

Dan Hirsch's avatar
Dan Hirsch committed
#rootpath = env['ROOTPATH'] = os.path.abspath('.')
#env.Append(CPPPATH=os.path.join('#', "hammer"))

targets = ["$libpath",
           "$incpath",
           "$parsersincpath",
           "$backendsincpath",
           "$pkgconfigpath"]
if not GetOption("in_place"):
    env['BUILD_BASE'] = 'build/$VARIANT'
    lib = env.SConscript(["src/SConscript"], variant_dir='$BUILD_BASE/src')
    env.Alias("examples", env.SConscript(["examples/SConscript"], variant_dir='$BUILD_BASE/examples'))
else:
    env['BUILD_BASE'] = '.'
    lib = env.SConscript(["src/SConscript"])
    env.Alias(env.SConscript(["examples/SConscript"]))

for testrun in testruns:
    env.Alias("test", testrun)
Dan Hirsch's avatar
Dan Hirsch committed