c86a256af58d7d8ee8acf58ea224e3cd3028276e
[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     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
57 env = Environment(platform="posix", variables=vars, ENV = os.environ)
58
59 SConsignFile('build/.%s' % env['build_dir'])
60
61 Help(vars.GenerateHelpText(env))
62
63 if env['neon'] and 'x86' in env['arch']:
64     print "Cannot compile NEON for x86"
65     Exit(1)
66
67 if 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
72 if 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
77 env.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'])
82 env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
83
84 if os.environ.get('CXX', 'g++') == 'clang++':
85     env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
86 else:
87     env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel'])
88
89 if env['cppthreads']:
90     env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
91
92 if 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
101 prefix = ""
102 if 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'])
111 elif 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-"
118 elif 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-"
126 elif env['arch'] == 'x86_32':
127     env.Append(CCFLAGS = ['-m32'])
128     env.Append(LINKFLAGS = ['-m32'])
129 elif env['arch'] == 'x86_64':
130     env.Append(CCFLAGS = ['-m64'])
131     env.Append(LINKFLAGS = ['-m64'])
132
133 if env['build'] == 'native':
134     prefix = ""
135
136 env['CC'] = prefix + os.environ.get('CC', 'gcc')
137 env['CXX'] = prefix + os.environ.get('CXX', 'g++')
138 env['LD'] = prefix + "ld"
139 env['AS'] = prefix + "as"
140 env['AR'] = prefix + "ar"
141 env['RANLIB'] = prefix + "ranlib"
142
143 if not GetOption("help"):
144     try:
145         compiler_ver = subprocess.check_output(env['CXX'].split() + ["-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
164 if env['Werror']:
165     env.Append(CXXFLAGS = ['-Werror'])
166
167 if env['os'] == 'android':
168     env.Append(CPPDEFINES = ['ANDROID'])
169     env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
170 elif env['os'] == 'bare_metal':
171     env.Append(LINKFLAGS = ['-static'])
172     env.Append(CXXFLAGS = ['-fPIC'])
173     env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
174
175 if 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
183 if env['debug']:
184     env['asserts'] = True
185     env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
186     env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
187 else:
188     env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
189
190 if env['asserts']:
191     env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
192
193 env.Append(CPPPATH = ['#/include', "#"])
194 env.Append(CXXFLAGS = env['extra_cxx_flags'])
195
196 Export('vars')
197 Export('env')
198 Export('version_at_least')
199
200 SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
201
202 if env['opencl']:
203     SConscript("./opencl-1.2-stubs/SConscript", variant_dir="build/%s/opencl-1.2-stubs" % env['build_dir'], duplicate=0)
204
205 if env['examples']:
206     SConscript('./examples/SConscript', variant_dir='#build/%s/examples' % env['build_dir'], duplicate=0)
207
208 SConscript('./tests/SConscript', variant_dir='#build/%s/tests' % env['build_dir'], duplicate=0)