blob: 3927e3acc908a9295ac8f2c520588486687b48b7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001# Copyright (c) 2016, 2017 ARM Limited.
2#
3# SPDX-License-Identifier: MIT
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to
7# deal in the Software without restriction, including without limitation the
8# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9# sell copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in all
13# copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
22
23import SCons
24import os
25import subprocess
26
27def version_at_least(version, required):
28 end = min(len(version), len(required))
29
30 for i in range(0, end, 2):
31 if int(version[i]) < int(required[i]):
32 return False
33 elif int(version[i]) > int(required[i]):
34 return True
35
36 return True
37
38vars = Variables("scons")
39vars.AddVariables(
40 BoolVariable("debug", "Debug", False),
41 BoolVariable("asserts", "Enable asserts (this flag is forced to 1 for debug=1)", False),
42 EnumVariable("arch", "Target Architecture", "armv7a", allowed_values=("armv7a", "arm64-v8a", "arm64-v8.2-a", "x86_32", "x86_64")),
43 EnumVariable("os", "Target OS", "linux", allowed_values=("linux", "android", "bare_metal")),
44 EnumVariable("build", "Build type", "cross_compile", allowed_values=("native", "cross_compile")),
45 BoolVariable("examples", "Build example programs", True),
46 BoolVariable("Werror", "Enable/disable the -Werror compilation flag", True),
47 BoolVariable("opencl", "Enable OpenCL support", True),
48 BoolVariable("neon", "Enable Neon support", False),
49 BoolVariable("embed_kernels", "Embed OpenCL kernels in library binary", False),
50 BoolVariable("set_soname", "Set the library's soname and shlibversion (requires SCons 2.4 or above)", False),
51 BoolVariable("openmp", "Enable OpenMP backend", False),
52 BoolVariable("cppthreads", "Enable C++11 threads backend", True),
53 PathVariable("build_dir", "Specify sub-folder for the build", ".", PathVariable.PathAccept),
54 ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", "")
55)
56
57env = Environment(platform="posix", variables=vars, ENV = os.environ)
58
59SConsignFile('build/.%s' % env['build_dir'])
60
61Help(vars.GenerateHelpText(env))
62
63if env['neon'] and 'x86' in env['arch']:
64 print "Cannot compile NEON for x86"
65 Exit(1)
66
67if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
68 print "Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above"
69 print "Update your version of SCons or use set_soname=0"
70 Exit(1)
71
72if env['os'] == 'bare_metal':
73 if env['cppthreads'] or env['openmp']:
74 print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
75 Exit(1)
76
77env.Append(CXXFLAGS = ['-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
78 '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
79 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
80 '-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual',
81 '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow'])
82env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
83
84if os.environ.get('CXX', 'g++') == 'clang++':
85 env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
86else:
87 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel'])
88
89if env['cppthreads']:
90 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
91
92if env['openmp']:
93 if os.environ.get('CXX', 'g++') == 'clang++':
94 print "Clang does not support OpenMP. Use scheduler=cpp."
95 Exit(1)
96
97 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
98 env.Append(CXXFLAGS = ['-fopenmp'])
99 env.Append(LINKFLAGS = ['-fopenmp'])
100
101prefix = ""
102if env['arch'] == 'armv7a':
103 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
104
105 if env['os'] in ['linux', 'bare_metal']:
106 prefix = "arm-linux-gnueabihf-"
107 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
108 elif env['os'] == 'android':
109 prefix = "arm-linux-androideabi-"
110 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
111elif env['arch'] == 'arm64-v8a':
112 env.Append(CXXFLAGS = ['-march=armv8-a'])
113
114 if env['os'] in ['linux', 'bare_metal']:
115 prefix = "aarch64-linux-gnu-"
116 elif env['os'] == 'android':
117 prefix = "aarch64-linux-android-"
118elif env['arch'] == 'arm64-v8.2-a':
119 env.Append(CXXFLAGS = ['-march=armv8.2-a+fp16+simd'])
120 env.Append(CPPDEFINES = ['ARM_COMPUTE_ENABLE_FP16'])
121
122 if env['os'] in ['linux', 'bare_metal']:
123 prefix = "aarch64-linux-gnu-"
124 elif env['os'] == 'android':
125 prefix = "aarch64-linux-android-"
126elif env['arch'] == 'x86_32':
127 env.Append(CCFLAGS = ['-m32'])
128 env.Append(LINKFLAGS = ['-m32'])
129elif env['arch'] == 'x86_64':
130 env.Append(CCFLAGS = ['-m64'])
131 env.Append(LINKFLAGS = ['-m64'])
132
133if env['build'] == 'native':
134 prefix = ""
135
136env['CC'] = prefix + os.environ.get('CC', 'gcc')
137env['CXX'] = prefix + os.environ.get('CXX', 'g++')
138env['LD'] = prefix + "ld"
139env['AS'] = prefix + "as"
140env['AR'] = prefix + "ar"
141env['RANLIB'] = prefix + "ranlib"
142
143if not GetOption("help"):
144 try:
145 compiler_ver = subprocess.check_output([env['CXX'], "-dumpversion"]).strip()
146 except OSError:
147 print("ERROR: Compiler '%s' not found" % env['CXX'])
148 Exit(1)
149
150 if os.environ.get('CXX','g++') == 'g++':
151 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
152 print "GCC 6.2.1 or newer is required to compile armv8.2-a code"
153 Exit(1)
154 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
155 print "GCC 4.9 or newer is required to compile NEON code for AArch64"
156 Exit(1)
157
158 if version_at_least(compiler_ver, '6.1'):
159 env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
160
161 if compiler_ver == '4.8.3':
162 env.Append(CXXFLAGS = ['-Wno-array-bounds'])
163
164if env['Werror']:
165 env.Append(CXXFLAGS = ['-Werror'])
166
167if env['os'] == 'android':
168 env.Append(CPPDEFINES = ['ANDROID'])
169 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
170elif env['os'] == 'bare_metal':
171 env.Append(LINKFLAGS = ['-static'])
172 env.Append(CXXFLAGS = ['-fPIC'])
173 env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
174
175if env['opencl']:
176 if env['os'] == 'bare_metal':
177 print("Cannot link OpenCL statically, which is required on bare metal")
178 Exit(1)
179
180 if env['embed_kernels']:
181 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
182
183if env['debug']:
184 env['asserts'] = True
185 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
186 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
187else:
188 env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
189
190if env['asserts']:
191 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
192
193env.Append(CPPPATH = ['#/include', "#"])
194env.Append(CXXFLAGS = env['extra_cxx_flags'])
195
196Export('vars')
197Export('env')
198Export('version_at_least')
199
200SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
201
202if env['opencl']:
203 SConscript("./opencl-1.2-stubs/SConscript", variant_dir="build/%s/opencl-1.2-stubs" % env['build_dir'], duplicate=0)
204
205if env['examples']:
206 SConscript('./examples/SConscript', variant_dir='#build/%s/examples' % env['build_dir'], duplicate=0)
207
208SConscript('./tests/SConscript', variant_dir='#build/%s/tests' % env['build_dir'], duplicate=0)