
import os

Help("""

  This is the third scons example. In this example we see more things that we
  can do with scons. Namely: arguments, variant build directory, and install.
  
  One can add the help for the expected build arguments using this help 
  function call
  
  DEBUG=1,0             - enable debug build, default 1
  ASSERT=1,0            - enable run time assertion, default 1 in DEBUG=1 and 0 otherwise
  COMPILER=compilerName - compiler name
  BUILDNAME=buildName   - build name

""")

"""
Processing arguments
"""
debugArg = ARGUMENTS.get('DEBUG', '1')
if debugArg not in ['0', '1']:
  raise RuntimeError('Check the DEBUG input argument.')

assertArg = '1' if debugArg == '1' else ARGUMENTS.get('ASSERT', '0')
if assertArg not in ['0', '1']:
  raise RuntimeError('Check the ASSERT input argument.')

compilerName = ARGUMENTS.get('COMPILER', '')
buildName = ARGUMENTS.get('BUILDNAME', 'default')

"""
Initializing the environment
"""
env = Environment(ENV=os.environ,
                  CC='clang',
                  CXX='clang++')
ccFlags = []
linkFlags = []
if compilerName == 'gcc':
  env.Replace(CC='gcc',
              CXX='g++')
  ccFlags = ["-std=c++1z", "-Wall", "-Wuninitialized", "-Wunused", "-Wconversion", "-fpermissive"]
  if debugArg == '0':
    ccFlags += ["-O3"]
    linkFlags = ["-Wl,--export-dynamic"]
  else:
    ccFlags += ["-g", "-O0"]
    linkFlags = ["-g"]
else:
  ccFlags = ["-std=c++1z", "-Wall", "-Wuninitialized", "-Wunused", "-Wconversion", "-fpermissive"]
  if debugArg == '0':
    ccFlags += ["-O3"]
    linkFlags = ["-Wl,--export-dynamic"]
  else:
    ccFlags += ["-g", "-O0"]
    linkFlags = ["-g"]

env['CPPFLAGS'] = ccFlags
env['LINKFLAGS'] = linkFlags

cppDefine = {}

if assertArg == '0':
  cppDefine['NDEBUG'] = None

env['CPPDEFINES'] = cppDefine

"""
Paths setup
"""

env.srcDir = Dir('#').abspath

env.buildDir = os.path.join(env.srcDir, 'build', buildName)
env.installDir = os.path.join(env.srcDir, 'install', buildName)

VariantDir(variant_dir=env.buildDir,
           src_dir=env.srcDir,
           duplicate=0)

"""
Add build targets 
"""

SConscript(os.path.join(env.buildDir, 'SConscript'),
           exports={'env' : env})
