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