config.h file is now generated
[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
91 # Create build_config.py and config.h
92 if not os.path.exists("build_config.py") or not os.path.exists("config.h"):
93     print "build_config.py or config.h don't exist - Generating new build config..."
94
95     header = ConfigHeader()
96     header.Define("PACKAGE", PACKAGE)
97     header.Define("PACKAGE_NAME", PACKAGE_NAME)
98     header.Define("PACKAGE_VERSION", PACKAGE_VERSION)
99     header.Define("PACKAGE_BUGREPORT", PACKAGE_BUGREPORT)
100     header.Define("PACKAGE_STRING", PACKAGE_STRING)
101         
102     conf = Configure(env, custom_tests = {
103         'CheckSDLConfig' : CheckSDLConfig
104     })
105     if not conf.CheckSDLConfig('1.2.4'):
106         print "Couldn't find libSDL >= 1.2.4"
107         Exit(1)
108     if not conf.CheckLib('SDL_mixer'):
109         print "Couldn't find SDL_mixer library!"
110         Exit(1)
111     if not conf.CheckLib('SDL_image'):
112         print "Couldn't find SDL_image library!"
113         Exit(1)
114     if not conf.CheckLib('GL'):
115         print "Couldn't find OpenGL library!"
116         Exit(1)
117
118     env = conf.Finish()
119
120     env.ParseConfig('sdl-config --cflags --libs')
121     env.Append(CPPDEFINES = \
122         {'DATA_PREFIX':"'\"" + env['PREFIX'] + "/share/supertux\"'" ,
123          'LOCALEDIR'  :"'\"" + env['PREFIX'] + "/locales\"'"})
124     opts.Save("build_config.py", env)
125     header.Save("config.h")
126 else:
127     print "Using build_config.py"
128
129 if env['VARIANT'] == "optimize":
130     env.Append(CXXFLAGS = "-O2 -g -Wall")
131 elif env['VARIANT'] == "debug":
132     env.Append(CXXFLAGS = "-O0 -g3 -Wall -Werror")
133     env.Append(CPPDEFINES = { "DEBUG":"1" })
134 elif env['VARIANT'] == "profile":
135     env.Append(CXXFLAGS = "-pg -O2")
136
137 build_dir="build/" + env['PLATFORM'] + "/" + env['VARIANT']
138
139 env.Append(CPPPATH = ["#", "#/src", "#/lib"])
140 env.Append(LIBS = ["supertux"])
141 env.Append(LIBPATH=["#" + build_dir + "/lib"])
142
143 env.Export(["env", "Glob"])
144 env.SConscript("lib/SConscript", build_dir=build_dir + "/lib", duplicate=0)
145 env.SConscript("src/SConscript", build_dir=build_dir + "/src", duplicate=0)