- fixed background
[supertux.git] / SConstruct
1 #
2 # SConstruct build file. See http://www.scons.org for details.
3 import os
4
5 class ConfigHeader:
6     def __init__(self):
7         self.defines = { }
8         self.prefix = ""
9         self.postfix = ""
10
11     def SetPrefix(self, prefix):
12         self.prefix = prefix
13
14     def SetPostfix(self, postfix):
15         self.postfix = postfix
16
17     def Define(self, name, value = ""):
18         self.defines[name] = value
19
20     def Save(self, filename):
21         file = open(filename, 'w')
22         file.write("/* %s. Generated by SConstruct */\n" % (filename))
23         file.write("\n")
24         file.write(self.prefix + "\n")
25         for key, value in self.defines.iteritems():
26             file.write("#define %s \"%s\"\n" % (key, value))
27         file.write(self.postfix + "\n")
28         file.close()
29
30 def Glob(dirs, pattern = '*' ):
31     import os, fnmatch
32     files = []
33     for dir in dirs:
34         try:
35             for file in os.listdir( Dir(dir).srcnode().abspath ):
36                 if fnmatch.fnmatch(file, pattern) :
37                     files.append( os.path.join( dir, file ) )
38         except Exception, e:
39             print "Warning, couldn't find directory '%s': %s" % (dir, str(e))
40         
41     return files
42
43 # thanks to Michael P Jung
44 def CheckSDLConfig(context, minVersion):
45     context.Message('Checking for sdl-config >= %s... ' % minVersion)
46     from popen2 import Popen3
47     p = Popen3(['sdl-config', '--version'])
48     ret = p.wait()
49     out = p.fromchild.readlines()
50     if ret != 0:
51         context.Result(False)
52         return False
53     if len(out) != 1:
54         # unable to parse output!
55         context.Result(False)
56         return False
57     # TODO validate output and catch exceptions
58     version = map(int, out[0].strip().split('.'))
59     minVersion = map(int, minVersion.split('.'))
60     # TODO comparing versions that way only works for pure numeric version
61     # numbers and fails for custom extensions. I don't care about this at
62     # the moment as sdl-config never used such version numbers afaik.
63     ret = (version >= minVersion)
64     context.Result(ret)
65     return ret
66
67 # Package options
68 PACKAGE_NAME = "SuperTux"
69 PACKAGE_VERSION = "0.2-cvs"
70 PACKAGE_BUGREPORT = "supertux-devel@lists.sourceforge.net"
71 PACKAGE = PACKAGE_NAME.lower()
72 PACKAGE_STRING = PACKAGE_NAME + " " + PACKAGE_VERSION
73
74 # User configurable options
75 opts = Options('build_config.py')
76 opts.Add('CXX', 'The C++ compiler', 'g++')
77 opts.Add('CXXFLAGS', 'Additional C++ compiler flags', '')
78 opts.Add('CPPPATH', 'Additional preprocessor paths', '')
79 opts.Add('CPPFLAGS', 'Additional preprocessor flags', '')
80 opts.Add('CPPDEFINES', 'defined constants', '')
81 opts.Add('LIBPATH', 'Additional library paths', '')
82 opts.Add('LIBS', 'Additional libraries', '')
83 opts.Add('DESTDIR', \
84         'destination directory for installation. It is prepended to PREFIX', '')
85 opts.Add('PREFIX', 'Installation prefix', '/usr/local')
86 opts.Add(EnumOption('VARIANT', 'Build variant', 'optimize',
87             ['optimize', 'debug', 'profile']))
88
89 env = Environment(options = opts)
90 Help(opts.GenerateHelpText(env))
91
92 # Create build_config.py and config.h
93 if not os.path.exists("build_config.py") or not os.path.exists("config.h"):
94     print "build_config.py or config.h don't exist - Generating new build config..."
95
96     header = ConfigHeader()
97     header.Define("PACKAGE", PACKAGE)
98     header.Define("PACKAGE_NAME", PACKAGE_NAME)
99     header.Define("PACKAGE_VERSION", PACKAGE_VERSION)
100     header.Define("PACKAGE_BUGREPORT", PACKAGE_BUGREPORT)
101     header.Define("PACKAGE_STRING", PACKAGE_STRING)
102         
103     conf = Configure(env, custom_tests = {
104         'CheckSDLConfig' : CheckSDLConfig
105     })
106     if not conf.CheckSDLConfig('1.2.4'):
107         print "Couldn't find libSDL >= 1.2.4"
108         Exit(1)
109     if not conf.CheckLib('SDL_mixer'):
110         print "Couldn't find SDL_mixer library!"
111         Exit(1)
112     if not conf.CheckLib('SDL_image'):
113         print "Couldn't find SDL_image library!"
114         Exit(1)
115     if not conf.CheckLib('GL'):
116         print "Couldn't find OpenGL library!"
117         Exit(1)
118
119     env = conf.Finish()
120
121     env.ParseConfig('sdl-config --cflags --libs')
122     env.Append(CPPDEFINES = \
123         {'DATA_PREFIX':"'\"" + env['PREFIX'] + "/share/supertux\"'" ,
124          'LOCALEDIR'  :"'\"" + env['PREFIX'] + "/locales\"'"})
125     opts.Save("build_config.py", env)
126     header.Save("config.h")
127 else:
128     print "Using build_config.py"
129
130 if env['VARIANT'] == "optimize":
131     env.Append(CXXFLAGS = "-O2 -g -Wall")
132 elif env['VARIANT'] == "debug":
133     env.Append(CXXFLAGS = "-O0 -g3 -Wall -Werror")
134     env.Append(CPPDEFINES = { "DEBUG":"1" })
135 elif env['VARIANT'] == "profile":
136     env.Append(CXXFLAGS = "-pg -O2")
137
138 build_dir="build/" + env['PLATFORM'] + "/" + env['VARIANT']
139
140 env.Append(CPPPATH = ["#", "#/src", "#/lib"])
141 env.Append(LIBS = ["supertux"])
142 env.Append(LIBPATH=["#" + build_dir + "/lib"])
143
144 env.Export(["env", "Glob"])
145 env.SConscript("lib/SConscript", build_dir=build_dir + "/lib", duplicate=0)
146 env.SConscript("src/SConscript", build_dir=build_dir + "/src", duplicate=0)
147 env.SConscript("SConscript", build_dir=build_dir, duplicate=0)