1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
#!/usr/bin/python
# {{{1 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/>.
# {{{1 Preamble
import os
from os.path import join
# {{{1 Auxiliary functions
def find_files(env, path):
oldcwd = os.getcwd()
try:
os.chdir(Dir('#').abspath)
sources = []
for root, dirnames, filenames in os.walk(path):
for filename in filenames:
if filename.endswith(".cc") or filename.endswith(".cpp"):
sources.append(os.path.join(root, filename))
if filename.endswith(".yy"):
target = os.path.join(root, filename[:-3], "grammar.cc")
source = "#"+os.path.join(root, filename)
sources.append(target)
env.Bison(target, source)
if filename.endswith(".xh"):
target = os.path.join(root, filename[:-3] + ".hh")
source = "#"+os.path.join(root, filename)
env.Re2c(target, source)
return sources
finally:
os.chdir(oldcwd)
def shared(env, sources):
return [env.SharedObject(x) for x in sources]
def bison_emit(target, source, env):
path = os.path.split(str(target[0]))[0];
target += [os.path.join(path, "grammar.hh"), os.path.join(path, "grammar.out")]
return target, source
def CheckBison(context):
context.Message('Checking for bison 2.5... ')
(result, output) = context.TryAction("${BISON} ${SOURCE} -o ${TARGET}", '%require "2.5"\n%%\nstart:', ".y")
context.Result(result)
return result
def CheckRe2c(context):
context.Message('Checking for re2c... ')
(result, output) = context.TryAction("${RE2C} ${SOURCE}", '', ".x")
context.Result(result)
return result
def CheckNeedRT(context):
context.Message('Checking if need library rt... ')
srcCode = """
#include <tbb/compat/condition_variable>
int main(int argc, char **argv)
{
tbb::interface5::unique_lock<tbb::mutex> lock;
tbb::tick_count::interval_t i;
tbb::interface5::condition_variable cv;
cv.wait_for(lock, i);
return 0;
}
"""
result = not context.TryLink(srcCode, '.cc')
context.Result(result)
return result
def CheckMyFun(context, name, code, header):
source = header + "\nint main() {\n" + code + "\nreturn 0; }"
context.Message('Checking for C++ function ' + name + '()... ')
result = context.TryLink(source, '.cc')
context.Result(result)
return result
# {{{1 Basic environment
Import('env')
bison_action = Action("${BISON} -r all --report-file=${str(TARGET)[:-3]}.out -o ${TARGET} ${SOURCE} ${test}")
bison_builder = Builder(
action = bison_action,
emitter = bison_emit,
suffix = '.cc',
src_suffix = '.yy'
)
re2c_action = Action("${RE2C} -o ${TARGET} ${SOURCE}")
re2c_builder = Builder(
action = re2c_action,
suffix = '.hh',
src_suffix = '.xh'
)
env['ENV']['PATH'] = os.environ['PATH']
if 'LD_LIBRARY_PATH' in os.environ: env['ENV']['LD_LIBRARY_PATH'] = os.environ['LD_LIBRARY_PATH']
env['BUILDERS']['Bison'] = bison_builder
env['BUILDERS']['Re2c'] = re2c_builder
# {{{1 Gringo specific configuration
conf = Configure(env, custom_tests = {'CheckBison' : CheckBison, 'CheckRe2c' : CheckRe2c, 'CheckMyFun' : CheckMyFun}, log_file = join("build", GetOption('build_dir') + ".log"))
DEFS = {}
failure = False
if not conf.CheckBison():
print 'error: no usable bison version found'
failure = True
if not conf.CheckRe2c():
print 'error: no usable re2c version found'
failure = True
if not conf.CheckCXX():
print 'error: no usable C++ compiler found'
Exit(1)
if (env['WITH_PYTHON'] is not None or env["WITH_LUA"] is not None) and not conf.CheckSHCXX():
print 'error: no usable (shared) C++ compiler found'
Exit(1)
if env['WITH_PYTHON']:
if not conf.CheckLibWithHeader(env['WITH_PYTHON'], 'Python.h', 'C++'):
print 'error: python library not found'
failure = True
else:
DEFS["WITH_PYTHON"] = 1
if env['WITH_LUA']:
if not conf.CheckLibWithHeader(env['WITH_LUA'], 'lua.hpp', 'C++'):
print 'error: lua library not found'
failure = True
else:
DEFS["WITH_LUA"] = 1
if not conf.CheckMyFun('snprintf', 'char buf[256]; snprintf (buf,256,"");', '#include <cstdio>'):
if conf.CheckMyFun('__builtin_snprintf', 'char buf[256]; __builtin_snprintf (buf,256,"");', '#include <cstdio>'):
DEFS['snprintf']='__builtin_snprintf'
if not conf.CheckMyFun('vsnprintf', 'char buf[256]; va_list args; vsnprintf (buf,256,"", args);', "#include <cstdio>\n#include <cstdarg>"):
if conf.CheckMyFun('__builtin_vsnprintf', 'char buf[256]; va_list args; __builtin_vsnprintf (buf,256,"", args);', "#include <cstdio>\n#include <cstdarg>"):
DEFS['vsnprintf']='__builtin_vsnprintf'
if not conf.CheckMyFun('std::to_string', 'std::to_string(10);', "#include <string>"):
DEFS['MISSING_STD_TO_STRING']=1
env = conf.Finish()
env.PrependUnique(LIBPATH=[Dir('.')])
env.Append(CPPDEFINES=DEFS)
# {{{1 Clasp specific configuration
claspEnv = env.Clone()
claspConf = Configure(claspEnv, custom_tests = {'CheckNeedRT' : CheckNeedRT}, log_file = join("build", GetOption('build_dir') + ".log"))
DEFS = {}
DEFS["WITH_THREADS"] = 0
if env['WITH_TBB']:
if not claspConf.CheckLibWithHeader(env['WITH_TBB'], 'tbb/tbb.h', 'C++'):
print 'error: tbb library not found'
failure = True
else:
DEFS["WITH_THREADS"] = 1
if claspConf.CheckNeedRT():
if not claspConf.CheckLibWithHeader('rt', 'time.h', 'C++'):
print 'error: rt library not found'
failure = True
claspEnv = claspConf.Finish()
claspEnv.Append(CPPDEFINES=DEFS)
# {{{1 Test specific configuration
if env['WITH_CPPUNIT']:
testEnv = claspEnv.Clone()
testConf = Configure(testEnv, custom_tests = {'CheckBison' : CheckBison, 'CheckRe2c' : CheckRe2c}, log_file = join("build", GetOption('build_dir') + ".log"))
if not testConf.CheckLibWithHeader(env['WITH_CPPUNIT'], 'cppunit/TestFixture.h', 'C++'):
print 'error: cppunit library not found'
failure = True
testEnv = testConf.Finish()
# {{{1 Check configuration
if failure: Exit(1)
# {{{1 Opts: Library
LIBOPTS_SOURCES = find_files(env, 'libprogram_opts/src')
LIBOPTS_HEADERS = [Dir('#libprogram_opts'), Dir('#libprogram_opts/src')]
optsEnv = env.Clone()
optsEnv.Append(CPPPATH = LIBOPTS_HEADERS)
optsLib = optsEnv.StaticLibrary('libprogram_opts', LIBOPTS_SOURCES)
optsLibS = optsEnv.StaticLibrary('libprogram_opts_shared', shared(optsEnv, LIBOPTS_SOURCES))
# {{{1 Clasp: Library
LIBCLASP_SOURCES = find_files(env, 'libclasp/src')
LIBCLASP_HEADERS = [Dir('#libclasp'), Dir('#libclasp/src'), Dir('#libprogram_opts')]
claspEnv.Append(CPPPATH = LIBCLASP_HEADERS)
claspLib = claspEnv.StaticLibrary('libclasp', LIBCLASP_SOURCES)
claspLibS = claspEnv.StaticLibrary('libclasp_shared', shared(claspEnv, LIBCLASP_SOURCES))
# {{{1 Gringo: Library
LIBGRINGO_SOURCES = find_files(env, 'libgringo/src')
LIBGRINGO_HEADERS = [Dir('#libgringo'), 'libgringo/src']
gringoEnv = env.Clone()
gringoEnv.Append(CPPPATH = LIBGRINGO_HEADERS + LIBOPTS_HEADERS)
gringoLib = gringoEnv.StaticLibrary('libgringo', LIBGRINGO_SOURCES)
gringoLibS = gringoEnv.StaticLibrary('libgringo_shared', shared(gringoEnv, LIBGRINGO_SOURCES))
# {{{1 Gringo: Program
GRINGO_SOURCES = find_files(env, 'app/gringo')
gringoProgramEnv = gringoEnv.Clone()
gringoProgramEnv.Prepend(LIBS=[ gringoLib, optsLib ])
gringoProgram = gringoProgramEnv.Program('gringo', GRINGO_SOURCES)
gringoProgramEnv.Alias('gringo', gringoProgram)
if not env.GetOption('clean'):
Default(gringoProgram)
# {{{1 Clingo: Program
CLINGO_SOURCES = find_files(env, 'app/clingo/src') + find_files(env, 'app/shared/src')
clingoProgramEnv = claspEnv.Clone()
clingoProgramEnv.Prepend(LIBS=[ gringoLib, claspLib, optsLib ])
clingoProgramEnv.Append(CPPPATH = [Dir('#app/shared/include')] + LIBGRINGO_HEADERS + LIBCLASP_HEADERS + LIBOPTS_HEADERS)
clingoProgram = clingoProgramEnv.Program('clingo', CLINGO_SOURCES)
clingoProgramEnv.Alias('clingo', clingoProgram)
if not env.GetOption('clean'):
Default(clingoProgram)
# {{{1 PyClingo + LuaClingo
sharedLibS = None
if env["WITH_PYTHON"] or env["WITH_LUA"]:
SHARED_SOURCES = find_files(env, 'app/shared/src')
sharedEnv = claspEnv.Clone()
sharedEnv.Append(CPPPATH = [Dir('#app/shared/include'), LIBGRINGO_HEADERS])
sharedLibS = sharedEnv.StaticLibrary('libshared_shared', shared(sharedEnv, SHARED_SOURCES))
if env["WITH_PYTHON"]:
PYCLINGO_SOURCES = find_files(env, 'app/pyclingo/src')
pyclingoEnv = sharedEnv.Clone()
pyclingoEnv["LIBPREFIX"] = ""
pyclingoEnv.Prepend(LIBS = [sharedLibS, gringoLibS, claspLibS, optsLibS])
pyclingo = pyclingoEnv.SharedLibrary('python/gringo', PYCLINGO_SOURCES)
pyclingoEnv.Alias('pyclingo', pyclingo)
if not env.GetOption('clean'):
Default(pyclingo)
if env["WITH_LUA"]:
LUACLINGO_SOURCES = find_files(env, 'app/luaclingo/src')
luaclingoEnv = sharedEnv.Clone()
luaclingoEnv["LIBPREFIX"] = ""
luaclingoEnv.Prepend(LIBS = [sharedLibS, gringoLibS, claspLibS, optsLibS])
luaclingo = luaclingoEnv.SharedLibrary('lua/gringo', LUACLINGO_SOURCES)
luaclingoEnv.Alias('luaclingo', luaclingo)
if not env.GetOption('clean'):
Default(luaclingo)
# {{{1 Gringo: Tests
if env['WITH_CPPUNIT']:
TEST_LIBGRINGO_SOURCES = find_files(env, 'libgringo/tests')
gringoTestEnv = testEnv.Clone()
gringoTestEnv.Append(CPPPATH = LIBGRINGO_HEADERS + LIBCLASP_HEADERS)
gringoTestEnv.Prepend(LIBS = [gringoLib, claspLib])
testGringoProgram = gringoTestEnv.Program('test_libgringo', TEST_LIBGRINGO_SOURCES)
testGringoAlias = gringoTestEnv.Alias('test', [testGringoProgram], testGringoProgram[0].path + (" " + GetOption("test_case") if GetOption("test_case") else ""))
AlwaysBuild(testGringoAlias)
# {{{1 Clingo: Tests
clingoTestCommand = env.Command('clingo-test', clingoProgram, '/bin/zsh app/clingo/tests/run.sh $SOURCE' + (" -- -t8" if env["WITH_TBB"] else ""))
clingoTest = env.Alias('test-clingo', [clingoTestCommand])
env.AlwaysBuild(clingoTest)
# {{{1 Clingo: Configure
clingoConfigure = env.Alias('configure', [])
# {{{1 Ctags
ctagsCommand = env.Command('ctags', [], 'ctags --c++-kinds=+p --fields=+imaS --extra=+q -R libgringo app')
ctagsAlias = env.Alias('tags', [ctagsCommand])
env.AlwaysBuild(ctagsCommand)
|