Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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), |
Pablo Tello | c6cb35a | 2017-06-21 15:39:47 +0100 | [diff] [blame] | 47 | BoolVariable("standalone", "Builds the tests as standalone executables, links statically with libgcc, libstdc++ and libarm_compute", False), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | BoolVariable("opencl", "Enable OpenCL support", True), |
| 49 | BoolVariable("neon", "Enable Neon support", False), |
| 50 | BoolVariable("embed_kernels", "Embed OpenCL kernels in library binary", False), |
| 51 | BoolVariable("set_soname", "Set the library's soname and shlibversion (requires SCons 2.4 or above)", False), |
| 52 | BoolVariable("openmp", "Enable OpenMP backend", False), |
| 53 | BoolVariable("cppthreads", "Enable C++11 threads backend", True), |
| 54 | PathVariable("build_dir", "Specify sub-folder for the build", ".", PathVariable.PathAccept), |
| 55 | ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", "") |
| 56 | ) |
| 57 | |
| 58 | env = Environment(platform="posix", variables=vars, ENV = os.environ) |
| 59 | |
| 60 | SConsignFile('build/.%s' % env['build_dir']) |
| 61 | |
| 62 | Help(vars.GenerateHelpText(env)) |
| 63 | |
| 64 | if env['neon'] and 'x86' in env['arch']: |
| 65 | print "Cannot compile NEON for x86" |
| 66 | Exit(1) |
| 67 | |
| 68 | if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"): |
| 69 | print "Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above" |
| 70 | print "Update your version of SCons or use set_soname=0" |
| 71 | Exit(1) |
| 72 | |
| 73 | if env['os'] == 'bare_metal': |
| 74 | if env['cppthreads'] or env['openmp']: |
| 75 | print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0") |
| 76 | Exit(1) |
| 77 | |
| 78 | env.Append(CXXFLAGS = ['-Wno-deprecated-declarations','-Wall','-DARCH_ARM', |
| 79 | '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2', |
| 80 | '-Winit-self','-Wstrict-overflow=2','-Wswitch-default', |
| 81 | '-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual', |
| 82 | '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow']) |
| 83 | env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP']) |
| 84 | |
| 85 | if os.environ.get('CXX', 'g++') == 'clang++': |
| 86 | env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags']) |
| 87 | else: |
| 88 | env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel']) |
| 89 | |
| 90 | if env['cppthreads']: |
| 91 | env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)]) |
| 92 | |
| 93 | if env['openmp']: |
| 94 | if os.environ.get('CXX', 'g++') == 'clang++': |
| 95 | print "Clang does not support OpenMP. Use scheduler=cpp." |
| 96 | Exit(1) |
| 97 | |
| 98 | env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)]) |
| 99 | env.Append(CXXFLAGS = ['-fopenmp']) |
| 100 | env.Append(LINKFLAGS = ['-fopenmp']) |
| 101 | |
| 102 | prefix = "" |
| 103 | if env['arch'] == 'armv7a': |
| 104 | env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon']) |
| 105 | |
| 106 | if env['os'] in ['linux', 'bare_metal']: |
| 107 | prefix = "arm-linux-gnueabihf-" |
| 108 | env.Append(CXXFLAGS = ['-mfloat-abi=hard']) |
| 109 | elif env['os'] == 'android': |
| 110 | prefix = "arm-linux-androideabi-" |
| 111 | env.Append(CXXFLAGS = ['-mfloat-abi=softfp']) |
| 112 | elif env['arch'] == 'arm64-v8a': |
| 113 | env.Append(CXXFLAGS = ['-march=armv8-a']) |
| 114 | |
| 115 | if env['os'] in ['linux', 'bare_metal']: |
| 116 | prefix = "aarch64-linux-gnu-" |
| 117 | elif env['os'] == 'android': |
| 118 | prefix = "aarch64-linux-android-" |
| 119 | elif env['arch'] == 'arm64-v8.2-a': |
| 120 | env.Append(CXXFLAGS = ['-march=armv8.2-a+fp16+simd']) |
| 121 | env.Append(CPPDEFINES = ['ARM_COMPUTE_ENABLE_FP16']) |
| 122 | |
| 123 | if env['os'] in ['linux', 'bare_metal']: |
| 124 | prefix = "aarch64-linux-gnu-" |
| 125 | elif env['os'] == 'android': |
| 126 | prefix = "aarch64-linux-android-" |
| 127 | elif env['arch'] == 'x86_32': |
| 128 | env.Append(CCFLAGS = ['-m32']) |
| 129 | env.Append(LINKFLAGS = ['-m32']) |
| 130 | elif env['arch'] == 'x86_64': |
| 131 | env.Append(CCFLAGS = ['-m64']) |
| 132 | env.Append(LINKFLAGS = ['-m64']) |
| 133 | |
| 134 | if env['build'] == 'native': |
| 135 | prefix = "" |
| 136 | |
| 137 | env['CC'] = prefix + os.environ.get('CC', 'gcc') |
| 138 | env['CXX'] = prefix + os.environ.get('CXX', 'g++') |
| 139 | env['LD'] = prefix + "ld" |
| 140 | env['AS'] = prefix + "as" |
| 141 | env['AR'] = prefix + "ar" |
| 142 | env['RANLIB'] = prefix + "ranlib" |
| 143 | |
| 144 | if not GetOption("help"): |
| 145 | try: |
Anthony Barbier | 907dba8 | 2017-08-25 10:11:39 +0100 | [diff] [blame] | 146 | compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | except OSError: |
| 148 | print("ERROR: Compiler '%s' not found" % env['CXX']) |
| 149 | Exit(1) |
| 150 | |
| 151 | if os.environ.get('CXX','g++') == 'g++': |
| 152 | if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'): |
| 153 | print "GCC 6.2.1 or newer is required to compile armv8.2-a code" |
| 154 | Exit(1) |
| 155 | elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'): |
| 156 | print "GCC 4.9 or newer is required to compile NEON code for AArch64" |
| 157 | Exit(1) |
| 158 | |
| 159 | if version_at_least(compiler_ver, '6.1'): |
| 160 | env.Append(CXXFLAGS = ['-Wno-ignored-attributes']) |
| 161 | |
| 162 | if compiler_ver == '4.8.3': |
| 163 | env.Append(CXXFLAGS = ['-Wno-array-bounds']) |
| 164 | |
Pablo Tello | c6cb35a | 2017-06-21 15:39:47 +0100 | [diff] [blame] | 165 | if env['standalone']: |
| 166 | env.Append(CXXFLAGS = ['-fPIC']) |
| 167 | env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++']) |
| 168 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | if env['Werror']: |
| 170 | env.Append(CXXFLAGS = ['-Werror']) |
| 171 | |
| 172 | if env['os'] == 'android': |
| 173 | env.Append(CPPDEFINES = ['ANDROID']) |
| 174 | env.Append(LINKFLAGS = ['-pie', '-static-libstdc++']) |
| 175 | elif env['os'] == 'bare_metal': |
| 176 | env.Append(LINKFLAGS = ['-static']) |
| 177 | env.Append(CXXFLAGS = ['-fPIC']) |
| 178 | env.Append(CPPDEFINES = ['NO_MULTI_THREADING']) |
| 179 | |
| 180 | if env['opencl']: |
| 181 | if env['os'] == 'bare_metal': |
| 182 | print("Cannot link OpenCL statically, which is required on bare metal") |
| 183 | Exit(1) |
| 184 | |
| 185 | if env['embed_kernels']: |
| 186 | env.Append(CPPDEFINES = ['EMBEDDED_KERNELS']) |
| 187 | |
| 188 | if env['debug']: |
| 189 | env['asserts'] = True |
| 190 | env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2']) |
| 191 | env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED']) |
| 192 | else: |
| 193 | env.Append(CXXFLAGS = ['-O3','-ftree-vectorize']) |
| 194 | |
| 195 | if env['asserts']: |
| 196 | env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED']) |
| 197 | |
| 198 | env.Append(CPPPATH = ['#/include', "#"]) |
| 199 | env.Append(CXXFLAGS = env['extra_cxx_flags']) |
| 200 | |
| 201 | Export('vars') |
| 202 | Export('env') |
| 203 | Export('version_at_least') |
| 204 | |
| 205 | SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0) |
| 206 | |
| 207 | if env['opencl']: |
| 208 | SConscript("./opencl-1.2-stubs/SConscript", variant_dir="build/%s/opencl-1.2-stubs" % env['build_dir'], duplicate=0) |
| 209 | |
| 210 | if env['examples']: |
| 211 | SConscript('./examples/SConscript', variant_dir='#build/%s/examples' % env['build_dir'], duplicate=0) |
| 212 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 213 | SConscript('./framework/SConscript', variant_dir='#build/%s/framework' % env['build_dir'], duplicate=0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 214 | SConscript('./tests/SConscript', variant_dir='#build/%s/tests' % env['build_dir'], duplicate=0) |