Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: ext-UV
Fetching contributors…

Cannot retrieve contributors at this time

file 279 lines (242 sloc) 9.93 kb
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
# vim: set filetype=python:
import os
import subprocess
import platform
import re
import sys
import json

from os.path import join, dirname, abspath
from types import DictType, StringTypes
from glob import glob

TORA_VERSION_STR='0.0.3'

tools = ['default']
default_prefix = '/usr/local'
if os.name == 'nt':
    tools = ['mingw']
    default_prefix = 'c:/tora'

AddOption('--prefix',
    dest='prefix',
    nargs=1,
    type='string',
    action='store',
    metavar='DIR',
    default=default_prefix,
    help='installation prefix'
)

if os.name == 'nt':
    env = Environment(
        ENV={'PATH': os.environ['PATH']},
        LIBS=['re2', 'pthread', 'ws2_32'],
        LIBPATH=['./'],
        CXXFLAGS=['-std=c++0x'],
        CCFLAGS=['-Wall', '-Wno-sign-compare', '-I' + os.getcwd() + '/vendor/boost_1_49_0/', '-I' + os.getcwd() + '/vendor/re2/', '-I' + os.getcwd() + "/tora/", '-march=native', '-g',
            # '-DPERLISH_CLOSURE'
        ],
        PREFIX=GetOption('prefix'),
        tools=tools
    )
    re2_env = Environment(
        ENV={'PATH': os.environ['PATH']},
        CCFLAGS=['-Wno-sign-compare', '-O2', '-I./vendor/re2/'],
        LIBS=['pthread'],
        tools=tools
    )
    env.MergeFlags([
        '-licuuc48 -licudt48'
    ])
else:
    env = Environment(
        LIBS=['re2', 'pthread', 'dl'],
        LIBPATH=['./'],
        CXXFLAGS=['-std=c++0x'],
        CCFLAGS=['-Wall', '-Wno-sign-compare', '-I' + os.getcwd() + '/vendor/boost_1_49_0/', '-I' + os.getcwd() + '/vendor/re2/', '-I' + os.getcwd() + "/tora/", '-fstack-protector', '-march=native', '-g', '-fPIC'
            # '-DPERLISH_CLOSURE'
        ],
        PREFIX=GetOption('prefix'),
        tools=tools
    )
    re2_env = Environment(
        CCFLAGS=['-pthread', '-Wno-sign-compare', '-O2', '-I./vendor/re2/', '-fPIC'],
        LIBS=['pthread'],
        tools=tools
    )
    env.MergeFlags([
        '!icu-config --cppflags --ldflags'
    ])

env.Append(TORA_LIBPREFIX=env.get('PREFIX') + "/lib/tora-" + TORA_VERSION_STR + "/")

exe_suffix = os.name == 'nt' and '.exe' or ''
ext_suffix = os.name == 'nt' and '.dll' or '.so'
lemon = os.name == 'nt' and '.\\tools\\lemon\\lemon.exe' or './tools/lemon/lemon'

if os.name == 'nt':
    #re2_env.Replace(CXX='g++')
    #env.Replace(CXX='g++')
    #env.Append(
    # LINKFLAGS=['-Wl,-E'],
    #)
    pass
elif os.uname()[0]=='Darwin':
    re2_env.Replace(CXX='clang++', CC='clang')
    env.Replace(CXX='clang++', CC='clang')
    # env.Append(CXXFLAGS=['-Werror'])
    env.Append(
        CCFLAGS=['-Wno-unused-function', '-DBOOST_NO_CHAR16_T', '-DBOOST_NO_CHAR32_T'],
        CXXFLAGS=['-Wno-unneeded-internal-declaration'],
    )
else:
    re2_env.Replace(CXX='g++')
    env.Replace(CXX='g++')
    env.Append(
        LINKFLAGS=['-Wl,-E', '-Wl,-Bsymbolic', '-rdynamic'],
    )

if ARGUMENTS.get('glog', 0):
    env.Append(LIBS=['glog'], CCFLAGS=['-DENABLE_GLOG'])

if ARGUMENTS.get('profile', 0):
    env.Append(CXXFLAGS=['-pg', '-Dprofile'])
    env.Append(LINKFLAGS=['-pg'])

if ARGUMENTS.get('ndebug', 1) != '0':
    env.Append(CCFLAGS=['-DNDEBUG'])
    env.Append(CCFLAGS=['-O2'])
else:
    env.Append(CCFLAGS=['-O0'])

# scons debug=1
if ARGUMENTS.get('debug', 0):
    env.Append(CCFLAGS=['-g'])

re2files = [
    Glob('vendor/re2/re2/*.cc'),
    'vendor/re2/util/rune.cc',
    'vendor/re2/util/valgrind.cc',
    'vendor/re2/util/stringprintf.cc',
    'vendor/re2/util/strutil.cc',
    'vendor/re2/util/hash.cc',
    'vendor/re2/util/stringpiece.cc',
]
libfiles = [
    "tora/" + x for x in Split('''
parser.cc value.cc compiler.cc
node.cc op.cc vm.cc util.cc
symbol_table.cc package_map.cc frame.cc package.cc operator.cc
builtin.cc
object.cc pad_list.cc
printf.cc

ops.gen.cc token.gen.cc lexer.gen.cc vm.gen.cc nodes.gen.cc symbols.gen.cc

inspector.cc peek.cc disasm.cc

value/code.cc value/hash.cc value/str.cc value/array.cc value/regexp.cc value/range.cc
value/object.cc value/int.cc value/bool.cc value/exception.cc
value/bytes.cc

object/str.cc object/array.cc object/dir.cc object/stat.cc object/env.cc object/time.cc object/file.cc object/internals.cc object/caller.cc object/code.cc object/symbol.cc
object/dynaloader.cc object/object.cc object/metaclass.cc
object/bytes.cc object/regexp.cc object/hash.cc
object/re2_regexp_matched.cc

''')
]

########
# tests.

programs = ['bin/tora' + exe_suffix]
for src in glob("tests/test_*.cc"):
    programs.append(env.Program(src.rstrip(".cc") + '.t' + exe_suffix, [
        libfiles,
        src,
    ]))

if 'test' in COMMAND_LINE_TARGETS:
    prefix = 'PERL5LIB=util/:$PERL5LIB '
    prove_path = 'prove'
    try:
        os.stat('/Users/tokuhirom/perl5/perlbrew/perls/perl-5.15.3/bin/prove') # throws exception if not exists
        prove_path = '/Users/tokuhirom/perl5/perlbrew/perls/perl-5.15.3/bin/prove'
    except: pass
    try:
        os.stat('/Users/tokuhirom/perl5/perlbrew/perls/perl-5.15.2/bin/prove')
        prove_path = '/Users/tokuhirom/perl5/perlbrew/perls/perl-5.15.2/bin/prove'
    except: pass
    env.Command('test', programs, prefix + " " + prove_path + ' --source Tora --source Executable -r tests/ t/tra/*.tra t/tra/*/*.tra --source Perl t')

env.Command('test.valgrind', ['bin/tora' + exe_suffix], 'perl misc/valgrind.pl');

if 'bench' in COMMAND_LINE_TARGETS:
    env.Command('bench', [], 'git log --oneline | head -1 && scons ndebug=1 test && ./bin/tora -V && time ./bin/tora benchmark/fib/fib.tra 39')

if 'op' in COMMAND_LINE_TARGETS:
    env.Command('op', [], 'git log --oneline | head -1 && scons && ./bin/tora -V ; sudo opcontrol --reset; sudo opcontrol --start && time ./bin/tora benchmark/fib/fib.tra 39 ; sudo opcontrol --stop')

env.Command('docs', ['bin/tora' + exe_suffix, Glob("tora/object/*.cc"), Glob("docs/source/*")], './bin/tora util/docgen.tra && cd docs/ && make html')

########
# main programs

env.Command(['tora/nodes.gen.h', 'tora/nodes.gen.cc'], 'tora/nodes.gen.pl', 'perl tora/nodes.gen.pl > tora/nodes.gen.h');
env.Command(['tora/token.gen.cc', 'tora/token.gen.h'], ['tora/token.gen.pl', 'tora/parser.h'], 'perl tora/token.gen.pl');
env.Command(['tora/lexer.gen.cc'], 'tora/lexer.re', 're2c tora/lexer.re > tora/lexer.gen.cc');
env.Command(['tora/vm.gen.cc', 'tora/ops.gen.h', 'tora/ops.gen.cc'], ['tora/vm.gen.pl', 'vm.inc'], 'perl -I misc/Text-MicroTemplate/ tora/vm.gen.pl');
env.Command(['tora/symbols.gen.cc', 'tora/symbols.gen.h'], ['tora/symbols.gen.pl'], 'perl -I misc/Text-MicroTemplate/ tora/symbols.gen.pl');
t = env.Command(['tora/parser.h', 'tora/parser.cc'], [lemon, 'tora/parser.yy', 'tora/lempar.c'], lemon + ' tora/parser.yy');
Clean(t, 'tora/parser.out')

libre2 = re2_env.Library('re2', re2files)

TORA_PREFIX = env.get('PREFIX')
TORA_CC = env.get('CC')
TORA_CXX = env.get('CXX')
TORA_CCFLAGS = ' '.join(env.get('CCFLAGS')).replace("\\", "/")
TORA_CXXFLAGS = ' '.join(env.get('CXXFLAGS')).replace("\\", "/")
# config.h
with open('tora/config.h', 'w') as f:
    f.write("#pragma once\n")
    f.write('#define TORA_CC "' + TORA_CC + "\"\n")
    f.write('#define TORA_CXX "' + TORA_CXX + "\"\n")
    f.write('#define TORA_CCFLAGS "' + TORA_CCFLAGS + "\"\n")
    f.write('#define TORA_PREFIX "' + TORA_PREFIX + "\"\n")
    f.write('#define TORA_VERSION_STR "' + TORA_VERSION_STR + "\"\n")

with open('config.json', 'w') as f:
    f.write(json.dumps({
        'PATH': os.environ['PATH'],
        'CC': TORA_CC,
        'CXX': TORA_CXX,
        'CCFLAGS': env.get('CCFLAGS'),
        'CPPPATH': env.get('CPPPATH') or [],
        'CXXFLAGS': env.get('CXXFLAGS'),
        'PREFIX': env.get('PREFIX'),
        'LIBS': ['tora'] + env.get('LIBS'),
        'TOOLS': tools,
        'SHLIBPREFIX': '',
        'LINKFLAGS': ['' + x for x in env.get('LINKFLAGS')[1:]],
        'TORA_VERSION_STR': TORA_VERSION_STR,
        'TORA_LIBPREFIX': env.get('PREFIX') + "/lib/tora-" + TORA_VERSION_STR + "/",
        'TORA_EXESUFFIX': exe_suffix,
        'TORA_EXTSUFFIX': ext_suffix,
    }))

with open('lib/Config.tra', 'w') as f:
    f.write("my $Config = {\n")
    f.write(" SHLIBSUFFIX => '" + env.get("SHLIBSUFFIX") + "',\n")
    f.write(" TORA_PREFIX => '" + TORA_PREFIX + "',\n")
    f.write(" TORA_CC => '" + TORA_CC + "',\n")
    f.write(" TORA_CXX => '" + TORA_CXX + "',\n")
    f.write(" TORA_CCFLAGS => '" + TORA_CCFLAGS + "',\n")
    f.write(" TORA_CXXFLAGS => '" + TORA_CXXFLAGS + "',\n")
    f.write(" TORA_VERSION_STR => '" + TORA_VERSION_STR + "',\n")
    f.write("};\n")
    f.write("sub tora_config() {\n")
    f.write(" $Config;\n")
    f.write("}\n")

libtora = env.Library('tora', [
    libfiles,
    libre2,
])

tora = env.Program('bin/tora' + exe_suffix, [
    ['tora/main.cc'],
    libtora,
    libre2
])
Default(tora)


# lemon
lemon_env = Environment()
lemon_env.Append(CCFLAGS=['-O2'])
lemon_env.Program(lemon, ['tools/lemon/lemon.c']);

# ----------------------------------------------------------------------
# test extensions
def test_exts():
    exts = []
    for ext in os.listdir('ext'):
        exts += [env.Command(['ext/%s/_tested' % ext], Glob('ext/' + ext + '/*'), 'cd ext/%s/ && scons test' % ext)]
    env.Command('test.ext', exts, 'echo ok')
test_exts()

# ----------------------------------------------------------------------
# instalation
installs = []
installs += [env.Install(env['PREFIX']+'/bin/', 'bin/tora' + exe_suffix)];
installs+=[env.InstallAs(env['PREFIX']+'/lib/tora-'+TORA_VERSION_STR, 'lib/')]
exts = []
for ext in os.listdir('ext'):
    exts += [env.Command(['ext/%s/_installed' % ext], Glob('ext/' + ext + "/*"), "cd ext/" + ext + " && scons install")]
env.Alias('install', [exts, env['PREFIX']+'/bin/', installs])
Something went wrong with that request. Please try again.