9e1c9ca83af221b0ec262b1a5164e39229dc8972
[platform/upstream/armcl.git] / SConstruct
1 # 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
23 import SCons
24 import os
25 import subprocess
26
27 def 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
38 vars = Variables("scons")
39 vars.AddVariables(
40     BoolVariable("debug", "Debug", False),
41     BoolVariable("asserts", "Enable asserts (this flag is forced to 1 for debug=1)", False),
42     BoolVariable("logging", "Logging (this flag is forced to 1 for debug=1)", False),
43     EnumVariable("arch", "Target Architecture", "armv7a", allowed_values=("armv7a", "arm64-v8a", "arm64-v8.2-a", "x86_32", "x86_64")),
44     EnumVariable("os", "Target OS", "linux", allowed_values=("linux", "android", "bare_metal")),
45     EnumVariable("build", "Build type", "cross_compile", allowed_values=("native", "cross_compile", "embed_only")),
46     BoolVariable("examples", "Build example programs", True),
47     BoolVariable("Werror", "Enable/disable the -Werror compilation flag", True),
48     BoolVariable("standalone", "Builds the tests as standalone executables, links statically with libgcc, libstdc++ and libarm_compute", False),
49     BoolVariable("opencl", "Enable OpenCL support", True),
50     BoolVariable("neon", "Enable Neon support", False),
51     BoolVariable("gles_compute", "Enable OpenGL ES Compute Shader support", False),
52     BoolVariable("embed_kernels", "Embed OpenCL kernels and OpenGL ES compute shaders in library binary", True),
53     BoolVariable("set_soname", "Set the library's soname and shlibversion (requires SCons 2.4 or above)", False),
54     BoolVariable("openmp", "Enable OpenMP backend", False),
55     BoolVariable("cppthreads", "Enable C++11 threads backend", True),
56     PathVariable("build_dir", "Specify sub-folder for the build", ".", PathVariable.PathAccept),
57     ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", "")
58 )
59
60 env = Environment(platform="posix", variables=vars, ENV = os.environ)
61 env.Append(LIBPATH = ["#build/%s" % env['build_dir']])
62 Export('env')
63 Export('vars')
64
65 SConsignFile('build/.%s' % env['build_dir'])
66
67 Help(vars.GenerateHelpText(env))
68
69 if env['build'] == "embed_only":
70     SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
71     Return()
72
73 if env['neon'] and 'x86' in env['arch']:
74     print "Cannot compile NEON for x86"
75     Exit(1)
76
77 if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
78     print "Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above"
79     print "Update your version of SCons or use set_soname=0"
80     Exit(1)
81
82 if env['os'] == 'bare_metal':
83     if env['cppthreads'] or env['openmp']:
84          print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
85          Exit(1)
86
87 env.Append(CXXFLAGS = ['-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
88          '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
89          '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
90          '-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual',
91          '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow','-Wno-implicit-fallthrough'])
92
93 env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
94
95 default_cpp_compiler = 'g++' if env['os'] != 'android' else 'clang++'
96 default_c_compiler = 'gcc' if env['os'] != 'android' else 'clang'
97 cpp_compiler = os.environ.get('CXX', default_cpp_compiler)
98 c_compiler = os.environ.get('CC', default_c_compiler)
99
100 if env['os'] == 'android' and ( cpp_compiler != 'clang++' or c_compiler != 'clang'):
101     print "WARNING: Only clang is officially supported to build the Compute Library for Android"
102
103 if cpp_compiler == 'clang++':
104     env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
105 else:
106     env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel'])
107
108 if env['cppthreads']:
109     env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
110
111 if env['openmp']:
112     if cpp_compiler == 'clang++':
113         print "Clang does not support OpenMP. Use scheduler=cpp."
114         Exit(1)
115
116     env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
117     env.Append(CXXFLAGS = ['-fopenmp'])
118     env.Append(LINKFLAGS = ['-fopenmp'])
119
120 prefix = ""
121 if env['arch'] == 'armv7a':
122     env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
123
124     if env['os'] == 'linux':
125         prefix = "arm-linux-gnueabihf-"
126         env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
127     elif env['os'] == 'bare_metal':
128         prefix = "arm-eabi-"
129         env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
130     elif env['os'] == 'android':
131         prefix = "arm-linux-androideabi-"
132         env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
133 elif env['arch'] == 'arm64-v8a':
134     env.Append(CXXFLAGS = ['-march=armv8-a'])
135     env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8A'])
136     if env['os'] == 'linux':
137         prefix = "aarch64-linux-gnu-"
138     elif env['os'] == 'bare_metal':
139         prefix = "aarch64-elf-"
140     elif env['os'] == 'android':
141         prefix = "aarch64-linux-android-"
142 elif env['arch'] == 'arm64-v8.2-a':
143     env.Append(CXXFLAGS = ['-march=armv8.2-a+fp16']) # explicitly enable fp16 extension otherwise __ARM_FEATURE_FP16_VECTOR_ARITHMETIC is undefined
144     env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8_2'])
145     if cpp_compiler == 'clang++':
146         env.Append(CXXFLAGS = ['-fno-integrated-as'])
147
148     if env['os'] == 'linux':
149         prefix = "aarch64-linux-gnu-"
150     elif env['os'] == 'bare_metal':
151         prefix = "aarch64-elf-"
152     elif env['os'] == 'android':
153         prefix = "aarch64-linux-android-"
154 elif env['arch'] == 'x86_32':
155     env.Append(CCFLAGS = ['-m32'])
156     env.Append(LINKFLAGS = ['-m32'])
157 elif env['arch'] == 'x86_64':
158     env.Append(CCFLAGS = ['-m64'])
159     env.Append(LINKFLAGS = ['-m64'])
160
161 if env['build'] == 'native':
162     prefix = ""
163
164 env['CC'] = prefix + c_compiler
165 env['CXX'] = prefix + cpp_compiler
166 env['LD'] = prefix + "ld"
167 env['AS'] = prefix + "as"
168 env['AR'] = prefix + "ar"
169 env['RANLIB'] = prefix + "ranlib"
170
171 if not GetOption("help"):
172     try:
173         compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
174     except OSError:
175         print("ERROR: Compiler '%s' not found" % env['CXX'])
176         Exit(1)
177
178     if cpp_compiler == 'g++':
179         if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
180             print "GCC 6.2.1 or newer is required to compile armv8.2-a code"
181             Exit(1)
182         elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
183             print "GCC 4.9 or newer is required to compile NEON code for AArch64"
184             Exit(1)
185
186         if version_at_least(compiler_ver, '6.1'):
187             env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
188
189         if compiler_ver == '4.8.3':
190             env.Append(CXXFLAGS = ['-Wno-array-bounds'])
191
192 if env['standalone']:
193     env.Append(CXXFLAGS = ['-fPIC'])
194     env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
195     if env['cppthreads']:
196         env.Append(LINKFLAGS = ['-lpthread'])
197
198 if env['Werror']:
199     env.Append(CXXFLAGS = ['-Werror'])
200
201 if env['os'] == 'android':
202     env.Append(CPPDEFINES = ['ANDROID'])
203     env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
204 elif env['os'] == 'bare_metal':
205     env.Append(LINKFLAGS = ['-static'])
206     env.Append(LINKFLAGS = ['-specs=rdimon.specs'])
207     env.Append(CXXFLAGS = ['-fPIC'])
208     env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
209     env.Append(CPPDEFINES = ['BARE_METAL'])
210
211 if env['opencl']:
212     if env['os'] in ['bare_metal'] or env['standalone']:
213         print("Cannot link OpenCL statically, which is required on bare metal")
214         Exit(1)
215
216 if env['opencl'] or env['gles_compute']:
217     if env['embed_kernels']:
218         env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
219
220 if env['debug']:
221     env['asserts'] = True
222     env['logging'] = True
223     env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
224     env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
225 else:
226     env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
227
228 if env['asserts']:
229     env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
230     env.Append(CXXFLAGS = ['-fstack-protector-strong'])
231
232 if env['logging']:
233     env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
234
235 env.Append(CPPPATH = ['#/include', "#"])
236 env.Append(CXXFLAGS = env['extra_cxx_flags'])
237
238 Export('version_at_least')
239
240 if env['opencl']:
241     SConscript("./opencl-1.2-stubs/SConscript", variant_dir="build/%s/opencl-1.2-stubs" % env['build_dir'], duplicate=0)
242
243 if env['gles_compute'] and env['os'] != 'android':
244     env.Append(CPPPATH = ['#/include/linux'])
245     env.Append(LIBPATH = ["#build/%s/opengles-3.1-stubs" % env['build_dir']])
246     SConscript("./opengles-3.1-stubs/SConscript", variant_dir="build/%s/opengles-3.1-stubs" % env['build_dir'], duplicate=0)
247
248 SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
249
250 if env['examples'] and env['os'] != 'bare_metal':
251     SConscript('./examples/SConscript', variant_dir='#build/%s/examples' % env['build_dir'], duplicate=0)
252
253 if env['os'] != 'bare_metal':
254     SConscript('./tests/SConscript', variant_dir='#build/%s/tests' % env['build_dir'], duplicate=0)