#!/usr/bin/python
# {{{ GPL License

# This file is part of gringo - a grounder for logic programs.
# Copyright (C) 2013  Roland Kaminski

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# }}}

from os import mkdir
from os.path import join, exists

if not exists("build"): mkdir("build")

AddOption('--build-dir', default='release', metavar='DIR', nargs=1, type='string', dest='build_dir')
AddOption('--test-case', default=None, metavar='NAME', nargs=1, type='string', dest='test_case')

# Note: workaround for scons limitation (try to get a hand on the internal option parser)

opts_file = join("build", GetOption('build_dir') + ".py")

opts = Variables(opts_file, ARGUMENTS)
opts.AddVariables(
    ('CXX'           , 'compiler'),
    ('CXXFLAGS'      , 'compiler flags'),
    ('CPPPATH'       , 'include paths'),
    ('CPPDEFINES'    , 'defines'),
    ('LIBS'          , 'additional libraries'),
    ('LIBPATH'       , 'library paths'),
    ('LINKFLAGS'     , 'linker flags'),
    ('RPATH'         , 'library paths to embedd into binaries'),
    ('AR'            , 'path to ar'),
    ('ARFLAGS'       , 'ar flags'),
    ('RANLIB'        , 'path to ranlib'),
    ('BISON'         , 'path to bison'),
    ('RE2C'          , 'path to re2c'),
    ('WITH_PYTHON'   , 'enable python integration; None, "auto", or library name or path'),
    ('WITH_LUA'      , 'enable lua integration; None, "auto", library name, or path'),
    ('WITH_TBB'      , 'enable thread support in clasp library using tbb; None, "auto", library name or path'),
    ('WITH_CPPUNIT'  , 'enable target test, running unit tests using cppunit; None, "auto", or library name or test'),
    )

env = Environment()
env['BISON']          = 'bison'
env['RE2C']           = 're2c'
env['CXX']            = 'g++'
env['CXXFLAGS']       = ['-std=c++11', '-O0', '-g', '-Wall', '-W', '-pedantic']
env['LIBS']           = []
env['LINKFLAGS']      = ['-std=c++11', '-O0']
env['CPPDEFINES']     = {}
env['CPPPATH']        = []
env['LIBPATH']        = []
env['RPATH']          = []
env['WITH_PYTHON']    = 'auto'
env['WITH_LUA']       = 'auto'
env['WITH_TBB']       = 'auto'
env['WITH_CPPUNIT']   = 'auto'

if GetOption("build_dir") == "static":
    env['CXXFLAGS']  = ['-std=c++11', '-O3', '-Wall']
    env['LINKFLAGS'] = ['-std=c++11', '-O3', '-static']
    env['CPPDEFINES']['NDEBUG'] = 1
elif GetOption("build_dir") == "release":
    env['CXXFLAGS']   = ['-std=c++11', '-O3', '-Wall']
    env['LINKFLAGS']  = ['-std=c++11', '-O3']
    env['CPPDEFINES']['NDEBUG'] = 1

opts.Update(env)
opts.Save(opts_file, env)
opts.FormatVariableHelpText = lambda env, opt, help, default, actual, other: "%10s: %s (%s)\n" % (opt, help, actual)

Help(
"""
usage: scons [OPTION] [TARGET] ...

Options:
  --build-dir=DIR             Sets the build directory to build/DIR. If DIR is
                              release or static then options are set,
                              respectively. Otherwise, debug options are set.
                              Default: debug
  --test-case=NAME            Selects which test case to run. If empty all
                              tests will be executed.
                              Default: ''

Targets:
  gringo                      Build gringo (built by default).
  clingo                      Build clingo (built by default).
  reify                       Build reify (built by default).
  pyclingo                    Python module (built if python support enabled).
  luaclingo                   Lua module (built if lua support enabled).
  test-clingo                 Run clingo specific acceptence tests.
  test                        Build and run unit tests.
  tags                        Generate ctags file.
  configure                   Only configure and build nothing.

Variables:
""" + opts.GenerateHelpText(env))

# Notes to use gold linker:
#   scons --build=gold \
#     CXXFLAGS="-std=c++11 -O4 -Wall" \
#     LINKFLAGS="-O4 -B build/gold/ld-gold/" \
#     RANLIB=true \
#     ARFLAGS="rc --plugin /usr/lib/llvm/LLVMgold.so"

if not env.GetOption('help'):
    SConscript('SConscript', variant_dir=join('build', GetOption('build_dir')), duplicate=0, exports=['env', 'opts'])

