add variants and link executable to toplevel
[supertux.git] / SConstruct
1 #
2 # SConstruct build file. See http://www.scons.org for details.
3
4 # based on a script from chenlee@ustc.edu
5 def Glob(dirs, pattern = '*' ):
6     import os, fnmatch 
7     files = []
8     for dir in dirs:
9         for file in os.listdir( Dir(dir).srcnode().abspath ): 
10             if fnmatch.fnmatch(file, pattern) : 
11                 files.append( os.path.join( dir, file ) ) 
12     return files 
13
14 def CheckSDLConfig(context, minVersion):
15     context.Message('Checking for sdl-config >= %s... ' % minVersion)
16     from popen2 import Popen3
17     p = Popen3(['sdl-config', '--version'])
18     ret = p.wait()
19     out = p.fromchild.readlines()
20     if ret != 0:
21         context.Result(False)
22         return False
23     if len(out) != 1:
24         # unable to parse output!
25         context.Result(False)
26         return False
27     # TODO validate output and catch exceptions
28     version = map(int, out[0].strip().split('.'))
29     minVersion = map(int, minVersion.split('.'))
30     # TODO comparing versions that way only works for pure numeric version
31     # numbers and fails for custom extensions. I don't care about this at
32     # the moment as sdl-config never used such version numbers afaik.
33     ret = (version >= minVersion)
34     context.Result(ret)
35     return ret
36
37 opts = Options('custom.py')
38 opts.Add('CXX', 'The C++ compiler', 'g++')
39 opts.Add('CXXFLAGS', 'Additional C++ compiler flags', '')
40 opts.Add('CPPPATH', 'Additional preprocessor paths', '')
41 opts.Add('CPPFLAGS', 'Additional preprocessor flags', '')
42 opts.Add('LIBPATH', 'Additional library paths', '')
43 opts.Add('LIBS', 'Additional libraries', '')
44 opts.Add('DESTDIR', \
45         'destination directory for installation. It is prepended to PREFIX', '')
46 opts.Add('PREFIX', 'Installation prefix', '/usr/local')
47 opts.Add(ListOption('VARIANT', 'Build variant', 'optimize',
48             ['optimize', 'debug', 'profile']))
49
50 env = Environment(options = opts)
51 conf = Configure(env, custom_tests = {
52     'CheckSDLConfig' : CheckSDLConfig
53 })
54
55 # TODO check -config apps in the Configure context
56    
57 if not conf.CheckSDLConfig('1.2.4'):
58     print "Couldn't find libSDL >= 1.2.4"
59     Exit(1)
60 if not conf.CheckLib('SDL_mixer'):
61     print "Couldn't find SDL_mixer library!"
62     Exit(1)
63 if not conf.CheckLib('SDL_image'):
64     print "Couldn't find SDL_image library!"
65     Exit(1)
66 if not conf.CheckLib('GL'):
67     print "Couldn't find OpenGL library!"
68     Exit(1)
69
70 env = conf.Finish()
71
72 if str(env['VARIANT']) == "optimize":
73     env.Append(CXXFLAGS = "-O2 -g")
74 elif str(env['VARIANT']) == "debug":
75     env.Append(CXXFLAGS = "-O0 -g3")
76     env.Append(CPPDEFINES = "DEBUG")
77 elif str(env['VARIANT']) == "profile":
78     env.Append(CXXFLAGS = "-pg -O2")
79
80 env.ParseConfig('sdl-config --cflags --libs')
81 env.Append(CPPPATH = ["#", "#/src", "#/lib"])
82 env.Append(CPPDEFINES = \
83         {'DATA_PREFIX':"'\"" + env['PREFIX'] + "/share/supertux\"'" ,
84          'LOCALEDIR'  :"'\"" + env['PREFIX'] + "/locales\"'"})
85
86 build_dir="build/" + env['PLATFORM'] + "/" + str(env['VARIANT'])
87
88 env.Append(LIBS = ["supertux"])
89 env.Append(LIBPATH=["#" + build_dir + "/lib"])
90
91 env.Export(["env", "Glob"])
92 env.SConscript("lib/SConscript", build_dir=build_dir + "/lib", duplicate=0)
93 env.SConscript("src/SConscript", build_dir=build_dir + "/src", duplicate=0)