blob: ea3b64b02fd653a3131d65972304aa589978699c [file] [log] [blame]
armvixl5289c592015-03-02 13:52:04 +00001# Copyright 2015, ARM Limited
armvixlad96eda2013-06-14 11:42:37 +01002# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are met:
6#
7# * Redistributions of source code must retain the above copyright notice,
8# this list of conditions and the following disclaimer.
9# * Redistributions in binary form must reproduce the above copyright notice,
10# this list of conditions and the following disclaimer in the documentation
11# and/or other materials provided with the distribution.
12# * Neither the name of ARM Limited nor the names of its contributors may be
13# used to endorse or promote products derived from this software without
14# specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
armvixldb644342015-07-21 11:37:10 +010027import glob
armvixlad96eda2013-06-14 11:42:37 +010028import os
armvixldb644342015-07-21 11:37:10 +010029from os.path import join
armvixlc68cb642014-09-25 18:49:30 +010030import platform
armvixl4a102ba2014-07-14 09:02:40 +010031import subprocess
armvixlad96eda2013-06-14 11:42:37 +010032import sys
33
armvixl4a102ba2014-07-14 09:02:40 +010034root_dir = os.path.dirname(File('SConstruct').rfile().abspath)
armvixldb644342015-07-21 11:37:10 +010035sys.path.insert(0, join(root_dir, 'tools'))
36import config
armvixl4a102ba2014-07-14 09:02:40 +010037import util
38
armvixlc68cb642014-09-25 18:49:30 +010039
40Help('''
41Build system for the VIXL project.
42See README.md for documentation and details about the build system.
armvixlc68cb642014-09-25 18:49:30 +010043''')
44
45
armvixldb644342015-07-21 11:37:10 +010046# We track top-level targets to automatically generate help and alias them.
47class TopLevelTargets:
48 def __init__(self):
49 self.targets = []
50 self.help_messages = []
51 def Add(self, target, help_message):
52 self.targets.append(target)
53 self.help_messages.append(help_message)
54 def Help(self):
armvixl684cd2a2015-10-23 13:38:33 +010055 res = ""
armvixldb644342015-07-21 11:37:10 +010056 for i in range(len(self.targets)):
57 res += '\t{0:<{1}}{2:<{3}}\n'.format(
58 'scons ' + self.targets[i],
59 len('scons ') + max(map(len, self.targets)),
60 ' : ' + self.help_messages[i],
61 len(' : ') + max(map(len, self.help_messages)))
62 return res
armvixlad96eda2013-06-14 11:42:37 +010063
armvixldb644342015-07-21 11:37:10 +010064top_level_targets = TopLevelTargets()
armvixlad96eda2013-06-14 11:42:37 +010065
66
armvixldb644342015-07-21 11:37:10 +010067
68# Build options ----------------------------------------------------------------
69
70# Store all the options in a dictionary.
71# The SConstruct will check the build variables and construct the build
72# environment as appropriate.
73options = {
74 'all' : { # Unconditionally processed.
75 'CCFLAGS' : ['-Wall',
76 '-Werror',
77 '-fdiagnostics-show-option',
78 '-Wextra',
79 '-Wredundant-decls',
80 '-pedantic',
armvixl684cd2a2015-10-23 13:38:33 +010081 '-Wmissing-noreturn',
armvixldb644342015-07-21 11:37:10 +010082 '-Wwrite-strings'],
83 'CPPPATH' : [config.dir_src_vixl]
84 },
85# 'build_option:value' : {
86# 'environment_key' : 'values to append'
87# },
88 'mode:debug' : {
89 'CCFLAGS' : ['-DVIXL_DEBUG', '-O0']
90 },
91 'mode:release' : {
92 'CCFLAGS' : ['-O3']
93 },
94 'simulator:on' : {
armvixl684cd2a2015-10-23 13:38:33 +010095 'CCFLAGS' : ['-DVIXL_INCLUDE_SIMULATOR'],
armvixldb644342015-07-21 11:37:10 +010096 },
97 'symbols:on' : {
98 'CCFLAGS' : ['-g'],
99 'LINKFLAGS' : ['-g']
100 },
101 }
armvixlad96eda2013-06-14 11:42:37 +0100102
103
armvixldb644342015-07-21 11:37:10 +0100104# A `DefaultVariable` has a default value that depends on elements not known
105# when variables are first evaluated.
106# Each `DefaultVariable` has a handler that will compute the default value for
107# the given environment.
108def modifiable_flags_handler(env):
109 env['modifiable_flags'] = \
110 'on' if 'mode' in env and env['mode'] == 'debug' else 'off'
armvixl6e2c8272015-03-31 11:04:14 +0100111
112
armvixldb644342015-07-21 11:37:10 +0100113def symbols_handler(env):
114 env['symbols'] = 'on' if 'mode' in env and env['mode'] == 'debug' else 'off'
armvixlad96eda2013-06-14 11:42:37 +0100115
116
armvixldb644342015-07-21 11:37:10 +0100117vars_default_handlers = {
118 # variable_name : [ 'default val', 'handler' ]
119 'symbols' : [ 'mode==debug', symbols_handler ],
120 'modifiable_flags' : [ 'mode==debug', modifiable_flags_handler ]
121 }
122
123
124def DefaultVariable(name, help, allowed):
125 default_value = vars_default_handlers[name][0]
126 allowed.append(default_value)
127 return EnumVariable(name, help, default_value, allowed)
128
129
130vars = Variables()
131# Define command line build options.
armvixlc68cb642014-09-25 18:49:30 +0100132sim_default = 'off' if platform.machine() == 'aarch64' else 'on'
armvixldb644342015-07-21 11:37:10 +0100133vars.AddVariables(
134 EnumVariable('mode', 'Build mode',
135 'release', allowed_values=config.build_options_modes),
136 DefaultVariable('symbols', 'Include debugging symbols in the binaries',
137 ['on', 'off']),
138 EnumVariable('simulator', 'Build for the simulator',
139 sim_default, allowed_values=['on', 'off']),
140 ('std', 'C++ standard. The standards tested are: %s.' % \
141 ', '.join(config.tested_cpp_standards))
142 )
armvixlad96eda2013-06-14 11:42:37 +0100143
armvixldb644342015-07-21 11:37:10 +0100144# Abort the build if any command line option is unknown or invalid.
145unknown_build_options = vars.UnknownVariables()
146if unknown_build_options:
147 print 'Unknown build options:', unknown_build_options.keys()
148 Exit(1)
armvixlad96eda2013-06-14 11:42:37 +0100149
armvixldb644342015-07-21 11:37:10 +0100150# We use 'variant directories' to avoid recompiling multiple times when build
151# options are changed, different build paths are used depending on the options
152# set. These are the options that should be reflected in the build directory
153# path.
154options_influencing_build_path = ['mode', 'symbols', 'CXX', 'std', 'simulator']
armvixlad96eda2013-06-14 11:42:37 +0100155
armvixlad96eda2013-06-14 11:42:37 +0100156
armvixldb644342015-07-21 11:37:10 +0100157
158# Build helpers ----------------------------------------------------------------
159
160def RetrieveEnvironmentVariables(env):
161 for key in ['CC', 'CXX', 'CCFLAGS', 'CXXFLAGS', 'AR', 'RANLIB', 'LD']:
162 if os.getenv(key): env[key] = os.getenv(key)
163 if os.getenv('LD_LIBRARY_PATH'): env['LIBPATH'] = os.getenv('LD_LIBRARY_PATH')
164 if os.getenv('CXXFLAGS'):
165 env.Append(CXXFLAGS = os.getenv('CXXFLAGS').split())
166 if os.getenv('LINKFLAGS'):
167 env.Append(LINKFLAGS = os.getenv('LINKFLAGS').split())
168 # This allows colors to be displayed when using with clang.
169 env['ENV']['TERM'] = os.getenv('TERM')
armvixl4a102ba2014-07-14 09:02:40 +0100170
171
armvixldb644342015-07-21 11:37:10 +0100172def ProcessBuildOptions(env):
173 # 'all' is unconditionally processed.
174 if 'all' in options:
175 for var in options['all']:
176 if var in env and env[var]:
177 env[var] += options['all'][var]
178 else:
179 env[var] = options['all'][var]
180 # Other build options must match 'option:value'
181 env_dict = env.Dictionary()
182 for key in env_dict.keys():
183 # First apply the default variables handlers.
184 if key in vars_default_handlers and \
185 env_dict[key] == vars_default_handlers[key][0]:
186 vars_default_handlers[key][1](env_dict)
187 # Then update the environment according to the value of the variable.
188 key_val_couple = key + ':%s' % env_dict[key]
189 if key_val_couple in options:
190 for var in options[key_val_couple]:
191 env[var] += options[key_val_couple][var]
192
193
194def ConfigureEnvironmentForCompiler(env):
195 def is_compiler(compiler):
196 return env['CXX'].find(compiler) == 0
197 if is_compiler('clang++'):
198 # These warnings only work for Clang.
199 # -Wimplicit-fallthrough only works when compiling the code base as C++11 or
200 # newer. The compiler does not complain if the option is passed when
201 # compiling earlier C++ standards.
202 env.Append(CPPFLAGS = ['-Wimplicit-fallthrough', '-Wshorten-64-to-32'])
203
204 # The '-Wunreachable-code' flag breaks builds for clang 3.4.
205 process = subprocess.Popen(env['CXX'] + ' --version | grep "clang.*3\.4"',
206 shell = True,
207 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
208 stdout, stderr = process.communicate()
209 using_clang3_4 = stdout != ''
210 if not using_clang3_4:
211 env.Append(CPPFLAGS = ['-Wunreachable-code'])
212
213 # GCC 4.8 has a bug which produces a warning saying that an anonymous Operand
214 # object might be used uninitialized:
215 # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57045
216 # The bug does not seem to appear in GCC 4.7, or in debug builds with GCC 4.8.
217 if env['mode'] == 'release':
armvixl0f35e362016-05-10 13:57:58 +0100218 process = subprocess.Popen(env['CXX'] + ' --version 2>&1 | grep "g++.*4\.8"',
armvixldb644342015-07-21 11:37:10 +0100219 shell = True,
220 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
armvixl0f35e362016-05-10 13:57:58 +0100221 stdout, unused = process.communicate()
armvixldb644342015-07-21 11:37:10 +0100222 using_gcc48 = stdout != ''
223 if using_gcc48:
224 env.Append(CPPFLAGS = ['-Wno-maybe-uninitialized'])
armvixl0f35e362016-05-10 13:57:58 +0100225 # On OSX, compilers complain about `long long` being a C++11 extension when no
226 # standard is passed.
227 if 'std' not in env or env['std'] == 'c++98':
228 env.Append(CPPFLAGS = ['-Wno-c++11-long-long'])
armvixldb644342015-07-21 11:37:10 +0100229
230
231def ConfigureEnvironment(env):
232 RetrieveEnvironmentVariables(env)
233 ProcessBuildOptions(env)
234 if 'std' in env:
235 env.Append(CPPFLAGS = ['-std=' + env['std']])
236 std_path = env['std']
237 ConfigureEnvironmentForCompiler(env)
238
239
240def TargetBuildDir(env):
241 # Build-time option values are embedded in the build path to avoid requiring a
242 # full build when an option changes.
243 build_dir = config.dir_build
244 for option in options_influencing_build_path:
245 option_value = env[option] if option in env else ''
246 build_dir = join(build_dir, option + '_'+ option_value)
247 return build_dir
248
249
250def PrepareVariantDir(location, build_dir):
251 location_build_dir = join(build_dir, location)
252 VariantDir(location_build_dir, location)
253 return location_build_dir
254
255
256def VIXLLibraryTarget(env):
257 build_dir = TargetBuildDir(env)
258 # Create a link to the latest build directory.
259 subprocess.check_call(["rm", "-f", config.dir_build_latest])
260 util.ensure_dir(build_dir)
261 subprocess.check_call(["ln", "-s", build_dir, config.dir_build_latest])
Alexandre Rames39c32a62016-05-23 15:47:22 +0100262 # Source files are in `src` and in `src/a64/`.
263 variant_dir_vixl = PrepareVariantDir(join('src'), build_dir)
264 variant_dir_a64 = PrepareVariantDir(join('src', 'a64'), build_dir)
armvixldb644342015-07-21 11:37:10 +0100265 sources = [Glob(join(variant_dir_vixl, '*.cc')),
266 Glob(join(variant_dir_a64, '*.cc'))]
267 return env.Library(join(build_dir, 'vixl'), sources)
268
269
270
271# Build ------------------------------------------------------------------------
272
273# The VIXL library, built by default.
274env = Environment(variables = vars)
275ConfigureEnvironment(env)
armvixldb644342015-07-21 11:37:10 +0100276Help(vars.GenerateHelpText(env))
277libvixl = VIXLLibraryTarget(env)
278Default(libvixl)
279env.Alias('libvixl', libvixl)
280top_level_targets.Add('', 'Build the VIXL library.')
281
armvixl4a102ba2014-07-14 09:02:40 +0100282
283# The benchmarks.
armvixldb644342015-07-21 11:37:10 +0100284benchmark_names = util.ListCCFilesWithoutExt(config.dir_benchmarks)
285benchmarks_build_dir = PrepareVariantDir('benchmarks', TargetBuildDir(env))
286benchmark_targets = []
287for bench in benchmark_names:
288 prog = env.Program(join(benchmarks_build_dir, bench),
289 join(benchmarks_build_dir, bench + '.cc'),
290 LIBS=[libvixl])
291 benchmark_targets.append(prog)
292env.Alias('benchmarks', benchmark_targets)
293top_level_targets.Add('benchmarks', 'Build the benchmarks.')
294
armvixl4a102ba2014-07-14 09:02:40 +0100295
296# The examples.
armvixldb644342015-07-21 11:37:10 +0100297example_names = util.ListCCFilesWithoutExt(config.dir_examples)
298examples_build_dir = PrepareVariantDir('examples', TargetBuildDir(env))
299example_targets = []
300for example in example_names:
301 prog = env.Program(join(examples_build_dir, example),
302 join(examples_build_dir, example + '.cc'),
303 LIBS=[libvixl])
304 example_targets.append(prog)
305env.Alias('examples', example_targets)
306top_level_targets.Add('examples', 'Build the examples.')
armvixl4a102ba2014-07-14 09:02:40 +0100307
308
armvixldb644342015-07-21 11:37:10 +0100309# The tests.
310test_build_dir = PrepareVariantDir('test', TargetBuildDir(env))
311# The test requires building the example files with specific options, so we
312# create a separate variant dir for the example objects built this way.
313test_examples_vdir = join(TargetBuildDir(env), 'test', 'test_examples')
314VariantDir(test_examples_vdir, '.')
315test_examples_obj = env.Object(
316 [Glob(join(test_examples_vdir, join('test', 'examples', '*.cc'))),
317 Glob(join(test_examples_vdir, join('examples', '*.cc')))],
318 CCFLAGS = env['CCFLAGS'] + ['-DTEST_EXAMPLES'],
319 CPPPATH = env['CPPPATH'] + [config.dir_examples])
320test = env.Program(join(test_build_dir, 'test-runner'),
321 [Glob(join(test_build_dir, '*.cc')), test_examples_obj],
322 CPPPATH = env['CPPPATH'] + [config.dir_examples],
323 LIBS=[libvixl])
324env.Alias('tests', test)
325top_level_targets.Add('tests', 'Build the tests.')
armvixl4a102ba2014-07-14 09:02:40 +0100326
armvixl4a102ba2014-07-14 09:02:40 +0100327
armvixldb644342015-07-21 11:37:10 +0100328env.Alias('all', top_level_targets.targets)
329top_level_targets.Add('all', 'Build all the targets above.')
330
331Help('\n\nAvailable top level targets:\n' + top_level_targets.Help())