git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@129 270642c3-0616-0410...
[senf.git] / satscons / SatSCons.py
1 import os.path, SCons.Options, SCons.Environment, SCons.Script.SConscript, glob
2
3 opts = None
4 finalizers = []
5
6 def InitOpts():
7     global opts
8     if opts is not None: return
9     opts = SCons.Options.Options('SConfig')
10     opts.Add('CXX', 'C++ compiler to use', 'g++')
11     opts.Add(SCons.Options.BoolOption('final','Enable optimization',0))
12
13 def Finalizer(f):
14     global finalizers
15     finalizers.append(f)
16
17 def UseBoost():
18     global opts
19     InitOpts()
20     opts.Add('BOOST_INCLUDES', 'Boost include directory', '')
21     opts.Add('BOOST_VARIANT', 'The boost variant to use', '')
22     opts.Add('BOOST_TOOLSET', 'The boost toolset to use', '')
23     opts.Add('BOOST_LIBDIR', 'The directory of the boost libraries', '')
24     Finalizer(FinalizeBoost)
25
26 def FinalizeBoost(env):
27     env.Tool('BoostUnitTests', [os.path.split(__file__)[0]])
28
29     if env['BOOST_TOOLSET']:
30         runtime = ""
31         if not env['final'] : runtime += "gd"
32         if env['STLPORT_LIB'] : runtime += "p"
33         if runtime: runtime = "-" + runtime
34         env['BOOST_VARIANT'] = "-" + env['BOOST_TOOLSET'] + runtime
35
36     env['BOOSTTESTLIB'] = 'libboost_unit_test_framework' + env['BOOST_VARIANT']
37
38     env.Append(LIBPATH = [ '$BOOST_LIBDIR' ],
39                CPPPATH = [ '$BOOST_INCLUDES' ])
40
41 def UseSTLPort():
42     global opts
43     InitOpts()
44     opts.Add('STLPORT_INCLUDES', 'STLport include directory', '')
45     opts.Add('STLPORT_LIB', 'Name of the stlport library or empty to not use stlport', '')
46     opts.Add('STLPORT_LIBDIR', 'The directory of the stlport libraries','')
47     Finalizer(FinalizeSTLPort)
48
49 def FinalizeSTLPort(env):
50     env['STLPORT_DEBUGLIB'] = ''
51     if env['STLPORT_LIB']:
52         env['STLPORT_DEBUGLIB'] = env['STLPORT_LIB'] + '_stldebug'
53         env.Append(LIBPATH = [ '$STLPORT_LIBDIR' ],
54                    CPPPATH = [ '$STLPORT_INCLUDES' ])
55         if env['final']:
56             env.Append(LIBS = [ '$STLPORT_LIB' ])
57         else:
58             env.Append(LIBS = [ '$STLPORT_DEBUGLIB' ],
59                        CPPDEFINES = [ '_STLP_DEBUG' ])
60
61 def UseDoxygen():
62     Finalizer(FinalizeDoxygen)
63
64 def FinalizeDoxygen(env):
65     env.Tool('Doxygen', [os.path.split(__file__)[0]])
66     
67 def MakeEnvironment():
68     global opts, finalizers
69     InitOpts()
70     env = SCons.Environment.Environment(options=opts)
71     if SCons.Script.SConscript.Arguments.get('final'):
72         env['final'] = 1
73     env.Help(opts.GenerateHelpText(env))
74     #conf = env.Configure()
75     #env = conf.env
76     if os.environ.has_key('SSH_AUTH_SOCK'):
77         env.Append( ENV = { 'SSH_AUTH_SOCK': os.environ['SSH_AUTH_SOCK'] } )
78
79     for finalizer in finalizers:
80         finalizer(env)
81
82     env.Append(CXXFLAGS = [ '-Wall', '-Woverloaded-virtual', '-Wno-long-long',
83                             '-pedantic', '-ansi' ],
84                LOCALLIBDIR = [ '#' ],
85                LIBPATH = [ '$LOCALLIBDIR' ])
86
87     if env['final']:
88         env.Append(CXXFLAGS = [ '-O3' ],
89                    CPPDEFINES = [ 'NDEBUG' ])
90     else:
91         env.Append(CXXFLAGS = [ '-O0', '-g', '-fno-inline' ],
92                         LINKFLAGS = [ '-g' ])
93
94     #return conf.Finish()
95     return env
96
97 def GlobSources():
98     testSources = glob.glob("*.test.cc")
99     sources = [ x for x in glob.glob("*.cc") if x not in testSources ]
100     return (sources, testSources)
101     
102 def StandardTargets(env):
103     all = env.Alias('all')
104     env.Clean(all, [ '.sconsign', '.sconf_temp', 'config.log', 'ChangeLog.bak', '.clean'
105                      ] + glob.glob("*~"))
106     env.Depends(all, '.')
107
108 def GlobalTargets(env):
109     command = "find -name .svn -prune -o \( -name '*.hh' -o -name '*.ih' -o -name '*.cc' -o -name '*.cci' -o -name '*.ct' -o -name '*.cti' -o -name '*.mpp' \) -print " \
110               "| xargs -r awk -F '//' '/%s/{print ARGV[ARGIND] \":\" FNR \":\" $2}' > $TARGET"
111     env.AlwaysBuild(env.Command('TODOS',None,[ command % 'TODO' ]))
112     env.AlwaysBuild(env.Command('FIXMES',None,[ command % ' FIXME' ]))
113     env.AlwaysBuild(env.Command('BUGS',None,[ command % 'BUG' ] ))
114     env.Alias('status',[ 'TODOS', 'FIXMES', 'BUGS' ])
115
116 def LibPath(lib): return '$LOCALLIBDIR/lib%s.a' % lib
117     
118 def Objects(env, sources, testSources = None, LIBS = []):
119     if type(sources) == type(()):
120         testSources = sources[1]
121         sources = sources[0]
122
123     objects = None
124     if sources:
125         objects = env.Object(sources)
126
127     if testSources:
128         test = env.BoostUnitTests(
129             target = 'test.log',
130             source = sources,
131             test_source = testSources,
132             LIBS = LIBS,
133             DEPENDS = [ env.File(LibPath(x)) for x in LIBS ])
134         env.Alias('all_tests', test)
135         # Hmm ... here I'd like to use an Alias instead of a file
136         # however the alias does not seem to live in the subdirectory
137         # which breaks 'scons -u test'
138         env.Alias(env.File('test'), test)
139
140     return objects
141
142 def Doxygen(env, sources, testSources = None):
143     if type(sources) == type(()):
144         testSources = sources[1]
145         sources = sources[0]
146
147     doc = env.Doxygen(
148         target = 'doc',
149         source = sources )
150
151     env.Alias('all_docs', doc)
152     return doc
153
154 def Lib(env, library, sources, testSources = None, LIBS = []):
155     objects = Objects(env,sources,testSources,LIBS=LIBS)
156     lib = None
157     if objects:
158         lib = env.Library(env.File(LibPath(library)),objects)
159         env.Default(lib)
160         env.Append(ALLLIBS = library)
161     return lib
162
163 def Binary(env, binary, sources, testSources = None, LIBS = []):
164     objects = Objects(env,sources,testSources,LIBS=LIBS)
165     program = None
166     if objects:
167         progEnv = env.Copy()
168         progEnv.Prepend(LIBS = LIBS)
169         program = progEnv.Program(target=binary,source=objects)
170         env.Default(program)
171         env.Depends(program, [ env.File(LibPath(x)) for x in LIBS ])
172     return program