OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2012 The Native Client Authors. All rights reserved. | 2 # Copyright (c) 2012 The Native Client Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Nacl SDK tool SCons.""" | 6 """Nacl SDK tool SCons.""" |
7 | 7 |
8 import __builtin__ | 8 import __builtin__ |
9 import re | 9 import re |
10 import os | 10 import os |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 }, | 43 }, |
44 }, | 44 }, |
45 'mac': { | 45 'mac': { |
46 'x86': { | 46 'x86': { |
47 '32': 'mac_x86', | 47 '32': 'mac_x86', |
48 '64': 'mac_x86', | 48 '64': 'mac_x86', |
49 }, | 49 }, |
50 }, | 50 }, |
51 } | 51 } |
52 | 52 |
| 53 NACL_TOOL_MAP = { |
| 54 'arm': { |
| 55 '32': { |
| 56 'tooldir': 'arm-nacl', |
| 57 'as_flag': '', |
| 58 'cc_flag': '', |
| 59 'ld_flag': '', |
| 60 }, |
| 61 }, |
| 62 'x86': { |
| 63 '32': { |
| 64 'tooldir': 'i686-nacl', |
| 65 'other_libdir': 'lib32', |
| 66 'as_flag': '--32', |
| 67 'cc_flag': '-m32', |
| 68 'ld_flag': ' -melf_nacl', |
| 69 }, |
| 70 '64': { |
| 71 'tooldir': 'x86_64-nacl', |
| 72 'other_libdir': 'lib64', |
| 73 'as_flag': '--64', |
| 74 'cc_flag': '-m64', |
| 75 'ld_flag': ' -melf64_nacl', |
| 76 }, |
| 77 }, |
| 78 } |
| 79 |
53 def _PlatformSubdirs(env): | 80 def _PlatformSubdirs(env): |
54 if env.Bit('bitcode'): | 81 if env.Bit('bitcode'): |
55 os = NACL_CANONICAL_PLATFORM_MAP[env['PLATFORM']] | 82 os = NACL_CANONICAL_PLATFORM_MAP[env['PLATFORM']] |
56 import platform | 83 import platform |
57 machine = platform.machine() | 84 machine = platform.machine() |
58 # x86 or i[0-9]86 should be converted to x86_32 | 85 # x86 or i[0-9]86 should be converted to x86_32 |
59 if re.match(r'x86$|i[0-9]86', machine): | 86 if re.match(r'x86$|i[0-9]86', machine): |
60 machine = 'x86_32' | 87 machine = 'x86_32' |
61 elif machine == 'x86_64': | 88 elif machine == 'x86_64': |
62 # Windows and Mac toolchains are only available as 32-bit binaries. | 89 # Windows and Mac toolchains are only available as 32-bit binaries. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 141 |
115 | 142 |
116 def _SetEnvForNativeSdk(env, sdk_path): | 143 def _SetEnvForNativeSdk(env, sdk_path): |
117 """Initialize environment according to target architecture.""" | 144 """Initialize environment according to target architecture.""" |
118 | 145 |
119 bin_path = os.path.join(sdk_path, 'bin') | 146 bin_path = os.path.join(sdk_path, 'bin') |
120 # NOTE: attempts to eliminate this PATH setting and use | 147 # NOTE: attempts to eliminate this PATH setting and use |
121 # absolute path have been futile | 148 # absolute path have been futile |
122 env.PrependENVPath('PATH', bin_path) | 149 env.PrependENVPath('PATH', bin_path) |
123 | 150 |
124 if os.path.exists(os.path.join(sdk_path, 'nacl64')): | 151 tool_prefix = None |
125 arch = 'nacl64' | 152 tool_map = NACL_TOOL_MAP[env['TARGET_ARCHITECTURE']] |
126 default_subarch = '64' | 153 subarch_spec = tool_map[env['TARGET_SUBARCH']] |
127 elif os.path.exists(os.path.join(sdk_path, 'x86_64-nacl')): | 154 tooldir = subarch_spec['tooldir'] |
128 arch = 'x86_64-nacl' | 155 if os.path.exists(os.path.join(sdk_path, tooldir)): |
129 default_subarch = '64' | 156 # The tooldir for the build target exists. |
130 elif os.path.exists(os.path.join(sdk_path, 'arm-nacl')): | 157 # The tools there do the right thing without special options. |
131 arch = 'arm-nacl' | 158 tool_prefix = tooldir |
132 default_subarch = env['TARGET_SUBARCH'] | 159 libdir = os.path.join(tooldir, 'lib') |
133 else: | |
134 # This fallback allows the Scons build to work if we have a | |
135 # 32-bit-by-default toolchain that lacks "nacl64" compatibility | |
136 # symlinks. | |
137 arch = 'nacl' | |
138 default_subarch = '32' | |
139 | |
140 # Although "lib32" is symlinked to "lib/32" and "lib64" is symlinked | |
141 # to "lib", we use the actual directories because, on Windows, Scons | |
142 # does not run under Cygwin and does not follow Cygwin symlinks. | |
143 if env['TARGET_SUBARCH'] == default_subarch: | |
144 libsuffix = 'lib' | |
145 as_mode_flag = '' | 160 as_mode_flag = '' |
| 161 cc_mode_flag = '' |
146 ld_mode_flag = '' | 162 ld_mode_flag = '' |
147 else: | 163 else: |
148 libsuffix = 'lib%s' % env['TARGET_SUBARCH'] | 164 # We're building for a target for which there is no matching tooldir. |
149 as_mode_flag = '--%s' % env['TARGET_SUBARCH'] | 165 # For example, for x86-32 when only <sdk_path>/x86_64-nacl/ exists. |
150 if env['TARGET_SUBARCH'] == '64': | 166 # Find a tooldir for a different subarch that does exist. |
151 ld_mode_flag = ' -melf64_nacl' | 167 others_map = tool_map.copy() |
152 else: | 168 del others_map[env['TARGET_SUBARCH']] |
153 ld_mode_flag = ' -melf_nacl' | 169 for subarch, tool_spec in others_map.iteritems(): |
| 170 tooldir = tool_spec['tooldir'] |
| 171 if os.path.exists(os.path.join(sdk_path, tooldir)): |
| 172 # OK, this is the other subarch to use as tooldir. |
| 173 tool_prefix = tooldir |
| 174 # We need to pass it extra options for the subarch we are building. |
| 175 as_mode_flag = subarch_spec['as_flag'] |
| 176 cc_mode_flag = subarch_spec['cc_flag'] |
| 177 ld_mode_flag = subarch_spec['ld_flag'] |
| 178 # The lib directory may have an alternate name, i.e. |
| 179 # 'lib32' in the x86_64-nacl tooldir. |
| 180 libdir = os.path.join(tooldir, subarch_spec.get('other_libdir', 'lib')) |
| 181 break |
154 | 182 |
155 if arch == 'arm-nacl': | 183 if tool_prefix is None: |
156 cc_mode_flag = '' | 184 raise Exception("Cannot find a toolchain for %s in %s" % |
157 else: | 185 (env['TARGET_FULLARCH'], sdk_path)) |
158 cc_mode_flag = '-m%s' % env['TARGET_SUBARCH'] | |
159 | 186 |
160 env.Replace(# Replace header and lib paths. | 187 env.Replace(# Replace header and lib paths. |
161 # where to put nacl extra sdk headers | 188 # where to put nacl extra sdk headers |
162 # TODO(robertm): switch to using the mechanism that | 189 # TODO(robertm): switch to using the mechanism that |
163 # passes arguments to scons | 190 # passes arguments to scons |
164 NACL_SDK_INCLUDE='%s/%s/include' % (sdk_path, arch), | 191 NACL_SDK_INCLUDE='%s/%s/include' % (sdk_path, tool_prefix), |
165 # where to find/put nacl generic extra sdk libraries | 192 # where to find/put nacl generic extra sdk libraries |
166 NACL_SDK_LIB='%s/%s/%s' % (sdk_path, arch, libsuffix), | 193 NACL_SDK_LIB='%s/%s' % (sdk_path, libdir), |
167 # Replace the normal unix tools with the NaCl ones. | 194 # Replace the normal unix tools with the NaCl ones. |
168 CC=os.path.join(bin_path, '%s-gcc' % arch), | 195 CC=os.path.join(bin_path, '%s-gcc' % tool_prefix), |
169 CXX=os.path.join(bin_path, '%s-g++' % arch), | 196 CXX=os.path.join(bin_path, '%s-g++' % tool_prefix), |
170 AR=os.path.join(bin_path, '%s-ar' % arch), | 197 AR=os.path.join(bin_path, '%s-ar' % tool_prefix), |
171 AS=os.path.join(bin_path, '%s-as' % arch), | 198 AS=os.path.join(bin_path, '%s-as' % tool_prefix), |
172 ASPP=os.path.join(bin_path, '%s-gcc' % arch), | 199 ASPP=os.path.join(bin_path, '%s-gcc' % tool_prefix), |
173 GDB=os.path.join(bin_path, '%s-gdb' % arch), | 200 GDB=os.path.join(bin_path, '%s-gdb' % tool_prefix), |
174 # NOTE: use g++ for linking so we can handle C AND C++. | 201 # NOTE: use g++ for linking so we can handle C AND C++. |
175 LINK=os.path.join(bin_path, '%s-g++' % arch), | 202 LINK=os.path.join(bin_path, '%s-g++' % tool_prefix), |
176 # Grrr... and sometimes we really need ld. | 203 # Grrr... and sometimes we really need ld. |
177 LD=os.path.join(bin_path, '%s-ld%s' % (arch, ld_mode_flag)), | 204 LD=os.path.join(bin_path, '%s-ld' % tool_prefix) + ld_mode_flag, |
178 RANLIB=os.path.join(bin_path, '%s-ranlib' % arch), | 205 RANLIB=os.path.join(bin_path, '%s-ranlib' % tool_prefix), |
179 OBJDUMP=os.path.join(bin_path, '%s-objdump' % arch), | 206 OBJDUMP=os.path.join(bin_path, '%s-objdump' % tool_prefix), |
180 STRIP=os.path.join(bin_path, '%s-strip' % arch), | 207 STRIP=os.path.join(bin_path, '%s-strip' % tool_prefix), |
181 ADDR2LINE=os.path.join(bin_path, '%s-addr2line' % arch), | 208 ADDR2LINE=os.path.join(bin_path, '%s-addr2line' % tool_prefix), |
182 BASE_LINKFLAGS=[cc_mode_flag], | 209 BASE_LINKFLAGS=[cc_mode_flag], |
183 BASE_CFLAGS=[cc_mode_flag], | 210 BASE_CFLAGS=[cc_mode_flag], |
184 BASE_CXXFLAGS=[cc_mode_flag], | 211 BASE_CXXFLAGS=[cc_mode_flag], |
185 BASE_ASFLAGS=[as_mode_flag], | 212 BASE_ASFLAGS=[as_mode_flag], |
186 BASE_ASPPFLAGS=[cc_mode_flag], | 213 BASE_ASPPFLAGS=[cc_mode_flag], |
187 CFLAGS=['-std=gnu99'], | 214 CFLAGS=['-std=gnu99'], |
188 CCFLAGS=['-O3', | 215 CCFLAGS=['-O3', |
189 '-Werror', | 216 '-Werror', |
190 '-Wall', | 217 '-Wall', |
191 '-Wno-variadic-macros', | 218 '-Wno-variadic-macros', |
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 # Dependency files it produces are to be found in ${LIBPATH}. | 669 # Dependency files it produces are to be found in ${LIBPATH}. |
643 # It is applied recursively to those dependencies in case | 670 # It is applied recursively to those dependencies in case |
644 # some of them are linker scripts too. | 671 # some of them are linker scripts too. |
645 ldscript_scanner = SCons.Scanner.Base( | 672 ldscript_scanner = SCons.Scanner.Base( |
646 function=ScanLinkerScript, | 673 function=ScanLinkerScript, |
647 skeys=['.a', '.so', '.pso'], | 674 skeys=['.a', '.so', '.pso'], |
648 path_function=SCons.Scanner.FindPathDirs('LIBPATH'), | 675 path_function=SCons.Scanner.FindPathDirs('LIBPATH'), |
649 recursive=True | 676 recursive=True |
650 ) | 677 ) |
651 env.Append(SCANNERS=ldscript_scanner) | 678 env.Append(SCANNERS=ldscript_scanner) |
OLD | NEW |