arm_compute v18.05
[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     ("compiler_cache", "Command to prefix to the C and C++ compiler (e.g ccache)", "")
59 )
60
61 env = Environment(platform="posix", variables=vars, ENV = os.environ)
62 env.Append(LIBPATH = ["#build/%s" % env['build_dir']])
63 Export('env')
64 Export('vars')
65
66 SConsignFile('build/.%s' % env['build_dir'])
67
68 Help(vars.GenerateHelpText(env))
69
70 if env['build'] == "embed_only":
71     SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
72     Return()
73
74 if env['neon'] and 'x86' in env['arch']:
75     print "Cannot compile NEON for x86"
76     Exit(1)
77
78 if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
79     print "Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above"
80     print "Update your version of SCons or use set_soname=0"
81     Exit(1)
82
83 if env['os'] == 'bare_metal':
84     if env['cppthreads'] or env['openmp']:
85          print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
86          Exit(1)
87
88 env.Append(CXXFLAGS = ['-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
89          '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
90          '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
91          '-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual',
92          '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow','-Wno-implicit-fallthrough'])
93
94 env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
95
96 default_cpp_compiler = 'g++' if env['os'] != 'android' else 'clang++'
97 default_c_compiler = 'gcc' if env['os'] != 'android' else 'clang'
98 cpp_compiler = os.environ.get('CXX', default_cpp_compiler)
99 c_compiler = os.environ.get('CC', default_c_compiler)
100
101 if env['os'] == 'android' and ( 'clang++' not in cpp_compiler or 'clang' not in c_compiler ):
102     print "WARNING: Only clang is officially supported to build the Compute Library for Android"
103
104 if 'clang++' in cpp_compiler:
105     env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
106 else:
107     env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel'])
108
109 if env['cppthreads']:
110     env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
111
112 if env['openmp']:
113     if 'clang++' in cpp_compiler:
114         print "Clang does not support OpenMP. Use scheduler=cpp."
115         Exit(1)
116
117     env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
118     env.Append(CXXFLAGS = ['-fopenmp'])
119     env.Append(LINKFLAGS = ['-fopenmp'])
120
121 prefix = ""
122 if env['arch'] == 'armv7a':
123     env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
124
125     if env['os'] == 'linux':
126         prefix = "arm-linux-gnueabihf-"
127         env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
128     elif env['os'] == 'bare_metal':
129         prefix = "arm-eabi-"
130         env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
131     elif env['os'] == 'android':
132         prefix = "arm-linux-androideabi-"
133         env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
134 elif env['arch'] == 'arm64-v8a':
135     env.Append(CXXFLAGS = ['-march=armv8-a'])
136     env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8A','NO_DOT_IN_TOOLCHAIN'])
137     if env['os'] == 'linux':
138         prefix = "aarch64-linux-gnu-"
139     elif env['os'] == 'bare_metal':
140         prefix = "aarch64-elf-"
141     elif env['os'] == 'android':
142         prefix = "aarch64-linux-android-"
143     if 'clang++' in cpp_compiler:
144         env.Append(CXXFLAGS = ['-no-integrated-as'])
145 elif env['arch'] == 'arm64-v8.2-a':
146     env.Append(CXXFLAGS = ['-march=armv8.2-a+fp16']) # explicitly enable fp16 extension otherwise __ARM_FEATURE_FP16_VECTOR_ARITHMETIC is undefined
147     env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8_2','NO_DOT_IN_TOOLCHAIN'])
148     if 'clang++' in cpp_compiler:
149         env.Append(CXXFLAGS = ['-no-integrated-as'])
150     if env['os'] == 'linux':
151         prefix = "aarch64-linux-gnu-"
152     elif env['os'] == 'bare_metal':
153         prefix = "aarch64-elf-"
154     elif env['os'] == 'android':
155         prefix = "aarch64-linux-android-"
156 elif env['arch'] == 'x86_32':
157     env.Append(CCFLAGS = ['-m32'])
158     env.Append(LINKFLAGS = ['-m32'])
159 elif env['arch'] == 'x86_64':
160     env.Append(CCFLAGS = ['-m64'])
161     env.Append(LINKFLAGS = ['-m64'])
162
163 if env['build'] == 'native':
164     prefix = ""
165
166 env['CC'] = env['compiler_cache']+" "+prefix + c_compiler
167 env['CXX'] = env['compiler_cache']+" "+prefix + cpp_compiler
168 env['LD'] = prefix + "ld"
169 env['AS'] = prefix + "as"
170 env['AR'] = prefix + "ar"
171 env['RANLIB'] = prefix + "ranlib"
172
173 if not GetOption("help"):
174     try:
175         compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
176     except OSError:
177         print("ERROR: Compiler '%s' not found" % env['CXX'])
178         Exit(1)
179
180     if 'clang++' not in cpp_compiler:
181         if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
182             print "GCC 6.2.1 or newer is required to compile armv8.2-a code"
183             Exit(1)
184         elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
185             print "GCC 4.9 or newer is required to compile NEON code for AArch64"
186             Exit(1)
187
188         if version_at_least(compiler_ver, '6.1'):
189             env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
190
191         if compiler_ver == '4.8.3':
192             env.Append(CXXFLAGS = ['-Wno-array-bounds'])
193
194 if env['standalone']:
195     env.Append(CXXFLAGS = ['-fPIC'])
196     env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
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 for bare metal / standalone builds")
214         Exit(1)
215
216 if env['gles_compute']:
217     if env['os'] in ['bare_metal'] or env['standalone']:
218         print("Cannot link OpenGLES statically, which is required for bare metal / standalone builds")
219         Exit(1)
220
221 if env["os"] not in ["android", "bare_metal"] and (env['opencl'] or env['cppthreads']):
222     env.Append(LIBS = ['pthread'])
223
224 if env['opencl'] or env['gles_compute']:
225     if env['embed_kernels']:
226         env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
227
228 if env['debug']:
229     env['asserts'] = True
230     env['logging'] = True
231     env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
232     env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
233 else:
234     env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
235
236 if env['asserts']:
237     env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
238     env.Append(CXXFLAGS = ['-fstack-protector-strong'])
239
240 if env['logging']:
241     env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
242
243 env.Append(CPPPATH = ['#/include', "#"])
244 env.Append(CXXFLAGS = env['extra_cxx_flags'])
245
246 Export('version_at_least')
247
248 if env['opencl']:
249     SConscript("./opencl-1.2-stubs/SConscript", variant_dir="build/%s/opencl-1.2-stubs" % env['build_dir'], duplicate=0)
250
251 if env['gles_compute'] and env['os'] != 'android':
252     env.Append(CPPPATH = ['#/include/linux'])
253     SConscript("./opengles-3.1-stubs/SConscript", variant_dir="build/%s/opengles-3.1-stubs" % env['build_dir'], duplicate=0)
254
255 SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
256
257 if env['examples'] and env['os'] != 'bare_metal':
258     SConscript('./examples/SConscript', variant_dir='#build/%s/examples' % env['build_dir'], duplicate=0)
259
260 if env['os'] != 'bare_metal':
261     SConscript('./tests/SConscript', variant_dir='#build/%s/tests' % env['build_dir'], duplicate=0)