Changed all SF references to BerliOS
[supertux.git] / SConstruct
1 #
2 # SConstruct build file. See http://www.scons.org for details.
3 import os
4 import glob
5
6 class ConfigHeader:
7     def __init__(self):
8         self.defines = { }
9         self.prefix = ""
10         self.postfix = ""
11
12     def SetPrefix(self, prefix):
13         self.prefix = prefix
14
15     def SetPostfix(self, postfix):
16         self.postfix = postfix
17
18     def Define(self, name, value = ""):
19         self.defines[name] = value
20
21     def Save(self, filename):
22         file = open(filename, 'w')
23         file.write("/* %s. Generated by SConstruct */\n" % (filename))
24         file.write("\n")
25         file.write(self.prefix + "\n")
26         for key, value in self.defines.iteritems():
27             file.write("#define %s \"%s\"\n" % (key, value))
28         file.write(self.postfix + "\n")
29         file.close()
30
31 def Glob(pattern):
32     path = GetBuildPath('SConscript').replace('SConscript', '')
33
34     result = []
35     for i in glob.glob(path + pattern): 
36         result.append(i.replace(path, ''))
37
38     return result
39
40 def InstallData(files):
41     for file in files:
42         dir = os.path.dirname(file)
43         destdir = os.path.join(env.subst('$DESTDIR/$APPDATADIR'), dir)
44         env.Install(destdir, file)                                        
45
46 def InstallExec(files):
47     for file in files:
48         destfile = env.subst('$DESTDIR/$BINDIR/$PROGRAM_PREFIX') + file + \
49             env.subst('$PROGRAM_POSTFIX')
50         env.InstallAs(destfile, file)
51
52 # thanks to Michael P Jung
53 def CheckSDLConfig(context, minVersion):
54     context.Message('Checking for sdl-config >= %s... ' % minVersion)
55     from popen2 import Popen3
56     p = Popen3(['sdl-config', '--version'])
57     ret = p.wait()
58     out = p.fromchild.readlines()
59     if ret != 0:
60         context.Result(False)
61         return False
62     if len(out) != 1:
63         # unable to parse output!
64         context.Result(False)
65         return False
66     # TODO validate output and catch exceptions
67     version = map(int, out[0].strip().split('.'))
68     minVersion = map(int, minVersion.split('.'))
69     # TODO comparing versions that way only works for pure numeric version
70     # numbers and fails for custom extensions. I don't care about this at
71     # the moment as sdl-config never used such version numbers afaik.
72     ret = (version >= minVersion)
73     context.Result(ret)
74     return ret
75
76 # User configurable options
77 opts = Options('build_config.py')
78 opts.Add('CXX', 'The C++ compiler', 'g++')
79 opts.Add('CXXFLAGS', 'Additional C++ compiler flags', '')
80 opts.Add('CPPPATH', 'Additional preprocessor paths', '')
81 opts.Add('CPPFLAGS', 'Additional preprocessor flags', '')
82 opts.Add('CPPDEFINES', 'defined constants', '')
83 opts.Add('LIBPATH', 'Additional library paths', '')
84 opts.Add('LIBS', 'Additional libraries', '')
85
86 # installation path options
87 opts.Add('PREFIX', 'prefix for architecture-independent files', '/usr/local')
88 opts.Add('EPREFIX', 'prefix for architecture-dependent files', '$PREFIX')
89 opts.Add('BINDIR', 'user executables directory', '$EPREFIX/bin')
90 #opts.Add('SBINDIR', 'system admin executables directory', '$EPREFIX/sbin')
91 #opts.Add('LIBEXECDIR', 'program executables directory', '$EPREFIX/libexec')
92 opts.Add('DATADIR', 'read-only architecture-independent data directory',
93     '$PREFIX/share')
94 #opts.Add('SYSCONFDIR', 'read-only single-machine data directory', '$PREFIX/etc')
95 #opts.Add('SHAREDSTATEDIR', 'modifiable architecture-independent data directory',
96 #    '$PREFIX/com')
97 #opts.Add('LOCALSTATEDIR', 'modifiable single-machine data directory',
98 #    '$PREFIX/var')
99 opts.Add('LIBDIR', 'object code libraries directory', '$EPREFIX/lib')
100 opts.Add('INCLUDEDIR', 'C header files directory', '$PREFIX/include')
101 #opts.Add('OLDINCLUDEDIR', 'C header files for non-gcc directory',
102 #    '$PREFIX/include')
103 #opts.Add('INFODIR', 'info documentation directory', '$PREFIX/info')
104 #opts.Add('MANDIR', 'man documentation directory', '$PREFIX/man')
105 opts.Add('DESTDIR', \
106         'destination directory for installation. It is prepended to PREFIX', '')
107
108 # misc options
109 opts.Add('PROGRAM_PREFIX', 'prepend PREFIX to installed program names', '')
110 opts.Add('PROGRAM_SUFFIX', 'append SUFFIX to installed program names', '')
111 opts.Add(EnumOption('VARIANT', 'Build variant', 'optimize',
112             ['optimize', 'debug', 'profile']))
113
114 env = Environment(options = opts)
115 Help(opts.GenerateHelpText(env))
116
117 # Package options
118 env['PACKAGE_NAME'] = 'SuperTux'
119 env['PACKAGE_VERSION'] = '0.2-cvs'
120 env['PACKAGE_BUGREPORT'] = 'supertux-devel@lists.berlios.de'
121 env['PACKAGE'] = env['PACKAGE_NAME'].lower()
122 env['PACKAGE_STRING'] = env['PACKAGE_NAME'] + " " + env['PACKAGE_VERSION']
123
124 # directories
125 env['APPDATADIR'] = "$DATADIR/$PACKAGE"
126 env['LOCALEDIR'] = "$DATADIR/locale"
127
128
129 # Create build_config.py and config.h
130 if not os.path.exists("build_config.py") or not os.path.exists("config.h"):
131     print "build_config.py or config.h don't exist - Generating new build config..."
132
133     header = ConfigHeader()
134     header.Define("PACKAGE", env['PACKAGE'])
135     header.Define("PACKAGE_NAME", env['PACKAGE_NAME'])
136     header.Define("PACKAGE_VERSION", env['PACKAGE_VERSION'])
137     header.Define("PACKAGE_BUGREPORT", env['PACKAGE_BUGREPORT'])
138     header.Define("PACKAGE_STRING", env['PACKAGE_STRING'])
139         
140     conf = Configure(env, custom_tests = {
141         'CheckSDLConfig' : CheckSDLConfig
142     })
143     if not conf.CheckSDLConfig('1.2.4'):
144         print "Couldn't find libSDL >= 1.2.4"
145         Exit(1)
146     if not conf.CheckLib('SDL_mixer'):
147         print "Couldn't find SDL_mixer library!"
148         Exit(1)
149     if not conf.CheckLib('SDL_image'):
150         print "Couldn't find SDL_image library!"
151         Exit(1)
152     if not conf.CheckLib('GL'):
153         print "Couldn't find OpenGL library!"
154         Exit(1)
155
156     env = conf.Finish()
157
158     env.ParseConfig('sdl-config --cflags --libs')
159     env.Append(CPPDEFINES = \
160         {'DATA_PREFIX':"'\"" + env.subst('$APPDATADIR') + "\"'" ,
161          'LOCALEDIR'  :"'\"" + env.subst('$LOCALEDIR') + "\"'"})
162     opts.Save("build_config.py", env)
163     header.Save("config.h")
164 else:
165     print "Using build_config.py"
166
167 if env['VARIANT'] == "optimize":
168     env.Append(CXXFLAGS = "-O2 -g -Wall")
169 elif env['VARIANT'] == "debug":
170     env.Append(CXXFLAGS = "-O0 -g3 -Wall -Werror")
171     env.Append(CPPDEFINES = { "DEBUG":"1" })
172 elif env['VARIANT'] == "profile":
173     env.Append(CXXFLAGS = "-pg -O2")
174
175 build_dir="build/" + env['PLATFORM'] + "/" + env['VARIANT']
176
177 # create some install aliases (only add paths here that are really used)
178 env.Alias('install-data', env.subst('$DESTDIR/$APPDATADIR'))
179 env.Alias('install-exec', env.subst('$DESTDIR/$BINDIR'))
180 env.Alias('install', ['install-data', 'install-exec'])
181
182 # append some include dirs and link libsupertux with main app
183 env.Append(CPPPATH = ["#", "#/src", "#/lib"])
184 env.Append(LIBS = ["supertux"])
185 env.Append(LIBPATH=["#" + build_dir + "/lib"])
186
187 env.Export(["env", "Glob", "InstallData", "InstallExec"])
188 env.SConscript("lib/SConscript", build_dir=build_dir + "/lib", duplicate=0)
189 env.SConscript("src/SConscript", build_dir=build_dir + "/src", duplicate=0)
190 env.SConscript("data/SConscript", build_dir=build_dir + "/data", duplicate=0)
191 env.SConscript("SConscript", build_dir=build_dir, duplicate=0)