blob: 3293263d58e34301bc79daeab91c84f5b669fab7 [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):
55 res = ""
56 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',
81 '-Wwrite-strings'],
82 'CPPPATH' : [config.dir_src_vixl]
83 },
84# 'build_option:value' : {
85# 'environment_key' : 'values to append'
86# },
87 'mode:debug' : {
88 'CCFLAGS' : ['-DVIXL_DEBUG', '-O0']
89 },
90 'mode:release' : {
91 'CCFLAGS' : ['-O3']
92 },
93 'simulator:on' : {
94 'CCFLAGS' : ['-DUSE_SIMULATOR'],
95 },
96 'symbols:on' : {
97 'CCFLAGS' : ['-g'],
98 'LINKFLAGS' : ['-g']
99 },
100 }
armvixlad96eda2013-06-14 11:42:37 +0100101
102
armvixldb644342015-07-21 11:37:10 +0100103# A `DefaultVariable` has a default value that depends on elements not known
104# when variables are first evaluated.
105# Each `DefaultVariable` has a handler that will compute the default value for
106# the given environment.
107def modifiable_flags_handler(env):
108 env['modifiable_flags'] = \
109 'on' if 'mode' in env and env['mode'] == 'debug' else 'off'
armvixl6e2c8272015-03-31 11:04:14 +0100110
111
armvixldb644342015-07-21 11:37:10 +0100112def symbols_handler(env):
113 env['symbols'] = 'on' if 'mode' in env and env['mode'] == 'debug' else 'off'
armvixlad96eda2013-06-14 11:42:37 +0100114
115
armvixldb644342015-07-21 11:37:10 +0100116vars_default_handlers = {
117 # variable_name : [ 'default val', 'handler' ]
118 'symbols' : [ 'mode==debug', symbols_handler ],
119 'modifiable_flags' : [ 'mode==debug', modifiable_flags_handler ]
120 }
121
122
123def DefaultVariable(name, help, allowed):
124 default_value = vars_default_handlers[name][0]
125 allowed.append(default_value)
126 return EnumVariable(name, help, default_value, allowed)
127
128
129vars = Variables()
130# Define command line build options.
armvixlc68cb642014-09-25 18:49:30 +0100131sim_default = 'off' if platform.machine() == 'aarch64' else 'on'
armvixldb644342015-07-21 11:37:10 +0100132vars.AddVariables(
133 EnumVariable('mode', 'Build mode',
134 'release', allowed_values=config.build_options_modes),
135 DefaultVariable('symbols', 'Include debugging symbols in the binaries',
136 ['on', 'off']),
137 EnumVariable('simulator', 'Build for the simulator',
138 sim_default, allowed_values=['on', 'off']),
139 ('std', 'C++ standard. The standards tested are: %s.' % \
140 ', '.join(config.tested_cpp_standards))
141 )
armvixlad96eda2013-06-14 11:42:37 +0100142
armvixldb644342015-07-21 11:37:10 +0100143# Abort the build if any command line option is unknown or invalid.
144unknown_build_options = vars.UnknownVariables()
145if unknown_build_options:
146 print 'Unknown build options:', unknown_build_options.keys()
147 Exit(1)
armvixlad96eda2013-06-14 11:42:37 +0100148
armvixldb644342015-07-21 11:37:10 +0100149# We use 'variant directories' to avoid recompiling multiple times when build
150# options are changed, different build paths are used depending on the options
151# set. These are the options that should be reflected in the build directory
152# path.
153options_influencing_build_path = ['mode', 'symbols', 'CXX', 'std', 'simulator']
armvixlad96eda2013-06-14 11:42:37 +0100154
armvixlad96eda2013-06-14 11:42:37 +0100155
armvixldb644342015-07-21 11:37:10 +0100156
157# Build helpers ----------------------------------------------------------------
158
159def RetrieveEnvironmentVariables(env):
160 for key in ['CC', 'CXX', 'CCFLAGS', 'CXXFLAGS', 'AR', 'RANLIB', 'LD']:
161 if os.getenv(key): env[key] = os.getenv(key)
162 if os.getenv('LD_LIBRARY_PATH'): env['LIBPATH'] = os.getenv('LD_LIBRARY_PATH')
163 if os.getenv('CXXFLAGS'):
164 env.Append(CXXFLAGS = os.getenv('CXXFLAGS').split())
165 if os.getenv('LINKFLAGS'):
166 env.Append(LINKFLAGS = os.getenv('LINKFLAGS').split())
167 # This allows colors to be displayed when using with clang.
168 env['ENV']['TERM'] = os.getenv('TERM')
armvixl4a102ba2014-07-14 09:02:40 +0100169
170
armvixldb644342015-07-21 11:37:10 +0100171def ProcessBuildOptions(env):
172 # 'all' is unconditionally processed.
173 if 'all' in options:
174 for var in options['all']:
175 if var in env and env[var]:
176 env[var] += options['all'][var]
177 else:
178 env[var] = options['all'][var]
179 # Other build options must match 'option:value'
180 env_dict = env.Dictionary()
181 for key in env_dict.keys():
182 # First apply the default variables handlers.
183 if key in vars_default_handlers and \
184 env_dict[key] == vars_default_handlers[key][0]:
185 vars_default_handlers[key][1](env_dict)
186 # Then update the environment according to the value of the variable.
187 key_val_couple = key + ':%s' % env_dict[key]
188 if key_val_couple in options:
189 for var in options[key_val_couple]:
190 env[var] += options[key_val_couple][var]
191
192
193def ConfigureEnvironmentForCompiler(env):
194 def is_compiler(compiler):
195 return env['CXX'].find(compiler) == 0
196 if is_compiler('clang++'):
197 # These warnings only work for Clang.
198 # -Wimplicit-fallthrough only works when compiling the code base as C++11 or
199 # newer. The compiler does not complain if the option is passed when
200 # compiling earlier C++ standards.
201 env.Append(CPPFLAGS = ['-Wimplicit-fallthrough', '-Wshorten-64-to-32'])
202
203 # The '-Wunreachable-code' flag breaks builds for clang 3.4.
204 process = subprocess.Popen(env['CXX'] + ' --version | grep "clang.*3\.4"',
205 shell = True,
206 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
207 stdout, stderr = process.communicate()
208 using_clang3_4 = stdout != ''
209 if not using_clang3_4:
210 env.Append(CPPFLAGS = ['-Wunreachable-code'])
211
212 # GCC 4.8 has a bug which produces a warning saying that an anonymous Operand
213 # object might be used uninitialized:
214 # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57045
215 # The bug does not seem to appear in GCC 4.7, or in debug builds with GCC 4.8.
216 if env['mode'] == 'release':
217 process = subprocess.Popen(env['CXX'] + ' --version | grep "g++.*4\.8"',
218 shell = True,
219 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
220 stdout, stderr = process.communicate()
221 using_gcc48 = stdout != ''
222 if using_gcc48:
223 env.Append(CPPFLAGS = ['-Wno-maybe-uninitialized'])
224
225
226def ConfigureEnvironment(env):
227 RetrieveEnvironmentVariables(env)
228 ProcessBuildOptions(env)
229 if 'std' in env:
230 env.Append(CPPFLAGS = ['-std=' + env['std']])
231 std_path = env['std']
232 ConfigureEnvironmentForCompiler(env)
233
234
235def TargetBuildDir(env):
236 # Build-time option values are embedded in the build path to avoid requiring a
237 # full build when an option changes.
238 build_dir = config.dir_build
239 for option in options_influencing_build_path:
240 option_value = env[option] if option in env else ''
241 build_dir = join(build_dir, option + '_'+ option_value)
242 return build_dir
243
244
245def PrepareVariantDir(location, build_dir):
246 location_build_dir = join(build_dir, location)
247 VariantDir(location_build_dir, location)
248 return location_build_dir
249
250
251def VIXLLibraryTarget(env):
252 build_dir = TargetBuildDir(env)
253 # Create a link to the latest build directory.
254 subprocess.check_call(["rm", "-f", config.dir_build_latest])
255 util.ensure_dir(build_dir)
256 subprocess.check_call(["ln", "-s", build_dir, config.dir_build_latest])
257 # Source files are in `src/vixl` and in `src/vixl/a64/`.
258 variant_dir_vixl = PrepareVariantDir(join('src', 'vixl'), build_dir)
259 variant_dir_a64 = PrepareVariantDir(join('src', 'vixl', 'a64'), build_dir)
260 sources = [Glob(join(variant_dir_vixl, '*.cc')),
261 Glob(join(variant_dir_a64, '*.cc'))]
262 return env.Library(join(build_dir, 'vixl'), sources)
263
264
265
266# Build ------------------------------------------------------------------------
267
268# The VIXL library, built by default.
269env = Environment(variables = vars)
270ConfigureEnvironment(env)
271ProcessBuildOptions(env)
272Help(vars.GenerateHelpText(env))
273libvixl = VIXLLibraryTarget(env)
274Default(libvixl)
275env.Alias('libvixl', libvixl)
276top_level_targets.Add('', 'Build the VIXL library.')
277
armvixl4a102ba2014-07-14 09:02:40 +0100278
279# The benchmarks.
armvixldb644342015-07-21 11:37:10 +0100280benchmark_names = util.ListCCFilesWithoutExt(config.dir_benchmarks)
281benchmarks_build_dir = PrepareVariantDir('benchmarks', TargetBuildDir(env))
282benchmark_targets = []
283for bench in benchmark_names:
284 prog = env.Program(join(benchmarks_build_dir, bench),
285 join(benchmarks_build_dir, bench + '.cc'),
286 LIBS=[libvixl])
287 benchmark_targets.append(prog)
288env.Alias('benchmarks', benchmark_targets)
289top_level_targets.Add('benchmarks', 'Build the benchmarks.')
290
armvixl4a102ba2014-07-14 09:02:40 +0100291
292# The examples.
armvixldb644342015-07-21 11:37:10 +0100293example_names = util.ListCCFilesWithoutExt(config.dir_examples)
294examples_build_dir = PrepareVariantDir('examples', TargetBuildDir(env))
295example_targets = []
296for example in example_names:
297 prog = env.Program(join(examples_build_dir, example),
298 join(examples_build_dir, example + '.cc'),
299 LIBS=[libvixl])
300 example_targets.append(prog)
301env.Alias('examples', example_targets)
302top_level_targets.Add('examples', 'Build the examples.')
armvixl4a102ba2014-07-14 09:02:40 +0100303
304
armvixldb644342015-07-21 11:37:10 +0100305# The tests.
306test_build_dir = PrepareVariantDir('test', TargetBuildDir(env))
307# The test requires building the example files with specific options, so we
308# create a separate variant dir for the example objects built this way.
309test_examples_vdir = join(TargetBuildDir(env), 'test', 'test_examples')
310VariantDir(test_examples_vdir, '.')
311test_examples_obj = env.Object(
312 [Glob(join(test_examples_vdir, join('test', 'examples', '*.cc'))),
313 Glob(join(test_examples_vdir, join('examples', '*.cc')))],
314 CCFLAGS = env['CCFLAGS'] + ['-DTEST_EXAMPLES'],
315 CPPPATH = env['CPPPATH'] + [config.dir_examples])
316test = env.Program(join(test_build_dir, 'test-runner'),
317 [Glob(join(test_build_dir, '*.cc')), test_examples_obj],
318 CPPPATH = env['CPPPATH'] + [config.dir_examples],
319 LIBS=[libvixl])
320env.Alias('tests', test)
321top_level_targets.Add('tests', 'Build the tests.')
armvixl4a102ba2014-07-14 09:02:40 +0100322
armvixl4a102ba2014-07-14 09:02:40 +0100323
armvixldb644342015-07-21 11:37:10 +0100324env.Alias('all', top_level_targets.targets)
325top_level_targets.Add('all', 'Build all the targets above.')
326
327Help('\n\nAvailable top level targets:\n' + top_level_targets.Help())