Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / native_client_sdk / src / tools / nacl_config.py
blob07fe263c35aad9964a73a5dd7cfb5d0e790638bd
1 #!/usr/bin/env python
2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """A helper script to print paths of NaCl binaries, includes, libs, etc.
8 It is similar in behavior to pkg-config or sdl-config.
9 """
11 import argparse
12 import os
13 import posixpath
14 import sys
16 import getos
19 if sys.version_info < (2, 7, 0):
20 sys.stderr.write("python 2.7 or later is required run this script\n")
21 sys.exit(1)
24 VALID_ARCHES = ('arm', 'x86_32', 'x86_64', 'i686')
25 VALID_PNACL_ARCHES = (None, 'pnacl')
26 ARCH_NAME = {
27 'arm': 'arm',
28 'x86_32': 'i686',
29 'i686': 'i686',
30 'x86_64': 'x86_64'
33 ARCH_ALT_NAME = {
34 'arm': 'arm',
35 'x86_32': 'x86_32',
36 'i686': 'x86_32',
37 'x86_64': 'x86_64'
40 ARCH_BASE_NAME = {
41 'arm': 'arm',
42 'x86_32': 'x86',
43 'i686': 'x86',
44 'x86_64': 'x86'
47 NACL_TOOLCHAINS = ('newlib', 'glibc', 'pnacl', 'bionic', 'clang-newlib')
48 HOST_TOOLCHAINS = ('linux', 'mac', 'win')
49 VALID_TOOLCHAINS = list(HOST_TOOLCHAINS) + list(NACL_TOOLCHAINS) + ['host']
51 # This is not an exhaustive list of tools, just the ones that need to be
52 # special-cased.
54 # e.g. For PNaCL cc => pnacl-clang
55 # For NaCl cc => pnacl-gcc
57 # Most tools will be passed through directly.
58 # e.g. For PNaCl foo => pnacl-foo
59 # For NaCl foo => x86_64-nacl-foo.
60 CLANG_TOOLS = {
61 'cc': 'clang',
62 'c++': 'clang++',
63 'gcc': 'clang',
64 'g++': 'clang++',
65 'ld': 'clang++'
68 GCC_TOOLS = {
69 'cc': 'gcc',
70 'c++': 'g++',
71 'gcc': 'gcc',
72 'g++': 'g++',
73 'ld': 'g++'
77 class Error(Exception):
78 pass
81 def Expect(condition, message):
82 if not condition:
83 raise Error(message)
86 def ExpectToolchain(toolchain, expected_toolchains):
87 Expect(toolchain in expected_toolchains,
88 'Expected toolchain to be one of [%s], not %s.' % (
89 ', '.join(expected_toolchains), toolchain))
92 def ExpectArch(arch, expected_arches):
93 Expect(arch in expected_arches,
94 'Expected arch to be one of [%s], not %s.' % (
95 ', '.join(map(str, expected_arches)), arch))
98 def CheckValidToolchainArch(toolchain, arch, arch_required=False):
99 if toolchain or arch or arch_required:
100 ExpectToolchain(toolchain, VALID_TOOLCHAINS)
102 if toolchain in HOST_TOOLCHAINS:
103 Expect(arch is None,
104 'Expected no arch for host toolchain %r. Got %r.' % (
105 toolchain, arch))
106 elif toolchain == 'pnacl':
107 Expect(arch is None or arch == 'pnacl',
108 'Expected no arch for toolchain %r. Got %r.' % (toolchain, arch))
109 elif arch_required:
110 Expect(arch is not None,
111 'Expected arch to be one of [%s] for toolchain %r.\n'
112 'Use the -a or --arch flags to specify one.\n' % (
113 ', '.join(VALID_ARCHES), toolchain))
115 if arch:
116 if toolchain == 'pnacl':
117 ExpectArch(arch, VALID_PNACL_ARCHES)
118 else:
119 ExpectArch(arch, VALID_ARCHES)
122 def GetArchName(arch):
123 return ARCH_NAME.get(arch)
126 def GetArchAltName(arch):
127 return ARCH_ALT_NAME.get(arch)
130 def GetArchBaseName(arch):
131 return ARCH_BASE_NAME.get(arch)
134 def CanonicalizeToolchain(toolchain):
135 if toolchain == 'host':
136 return getos.GetPlatform()
137 return toolchain
140 def GetPosixSDKPath():
141 sdk_path = getos.GetSDKPath()
142 if getos.GetPlatform() == 'win':
143 return sdk_path.replace('\\', '/')
144 else:
145 return sdk_path
148 def GetToolchainDir(toolchain, arch=None):
149 ExpectToolchain(toolchain, NACL_TOOLCHAINS)
150 root = GetPosixSDKPath()
151 platform = getos.GetPlatform()
152 if toolchain in ('pnacl', 'clang-newlib'):
153 subdir = '%s_pnacl' % platform
154 else:
155 assert arch is not None
156 subdir = '%s_%s_%s' % (platform, GetArchBaseName(arch), toolchain)
158 return posixpath.join(root, 'toolchain', subdir)
161 def GetToolchainArchDir(toolchain, arch):
162 ExpectToolchain(toolchain, NACL_TOOLCHAINS)
163 assert arch is not None
164 toolchain_dir = GetToolchainDir(toolchain, arch)
165 arch_dir = '%s-nacl' % GetArchName(arch)
166 return posixpath.join(toolchain_dir, arch_dir)
169 def GetToolchainBinDir(toolchain, arch=None):
170 ExpectToolchain(toolchain, NACL_TOOLCHAINS)
171 return posixpath.join(GetToolchainDir(toolchain, arch), 'bin')
174 def GetSDKIncludeDirs(toolchain):
175 root = GetPosixSDKPath()
176 base_include = posixpath.join(root, 'include')
177 if toolchain == 'clang-newlib':
178 toolchain = 'newlib'
179 return [base_include, posixpath.join(base_include, toolchain)]
182 def GetSDKLibDir():
183 return posixpath.join(GetPosixSDKPath(), 'lib')
186 # Commands
188 def GetToolPath(toolchain, arch, tool):
189 if tool == 'gdb':
190 # Always use the same gdb; it supports multiple toolchains/architectures.
191 # NOTE: this is always a i686 executable. i686-nacl-gdb is a symlink to
192 # x86_64-nacl-gdb.
193 return posixpath.join(GetToolchainBinDir('newlib', 'x86_64'),
194 'x86_64-nacl-gdb')
196 if toolchain == 'pnacl':
197 CheckValidToolchainArch(toolchain, arch)
198 tool = CLANG_TOOLS.get(tool, tool)
199 full_tool_name = 'pnacl-%s' % tool
200 else:
201 CheckValidToolchainArch(toolchain, arch, arch_required=True)
202 ExpectArch(arch, VALID_ARCHES)
203 if toolchain == 'clang-newlib':
204 tool = CLANG_TOOLS.get(tool, tool)
205 else:
206 tool = GCC_TOOLS.get(tool, tool)
207 full_tool_name = '%s-nacl-%s' % (GetArchName(arch), tool)
208 return posixpath.join(GetToolchainBinDir(toolchain, arch), full_tool_name)
211 def GetCFlags(toolchain):
212 ExpectToolchain(toolchain, VALID_TOOLCHAINS)
213 return ' '.join('-I%s' % dirname for dirname in GetSDKIncludeDirs(toolchain))
216 def GetIncludeDirs(toolchain):
217 ExpectToolchain(toolchain, VALID_TOOLCHAINS)
218 return ' '.join(GetSDKIncludeDirs(toolchain))
221 def GetLDFlags():
222 return '-L%s' % GetSDKLibDir()
225 def main(args):
226 parser = argparse.ArgumentParser(description=__doc__)
227 parser.add_argument('-t', '--toolchain', help='toolchain name. This can also '
228 'be specified with the NACL_TOOLCHAIN environment '
229 'variable.')
230 parser.add_argument('-a', '--arch', help='architecture name. This can also '
231 'be specified with the NACL_ARCH environment variable.')
233 group = parser.add_argument_group('Commands')
234 group.add_argument('--tool', help='get tool path')
235 group.add_argument('--cflags',
236 help='output all preprocessor and compiler flags',
237 action='store_true')
238 group.add_argument('--libs', '--ldflags', help='output all linker flags',
239 action='store_true')
240 group.add_argument('--include-dirs',
241 help='output include dirs, separated by spaces',
242 action='store_true')
244 options = parser.parse_args(args)
246 # Get toolchain/arch from environment, if not specified on commandline
247 options.toolchain = options.toolchain or os.getenv('NACL_TOOLCHAIN')
248 options.arch = options.arch or os.getenv('NACL_ARCH')
250 options.toolchain = CanonicalizeToolchain(options.toolchain)
251 CheckValidToolchainArch(options.toolchain, options.arch)
253 if options.cflags:
254 print GetCFlags(options.toolchain)
255 elif options.include_dirs:
256 print GetIncludeDirs(options.toolchain)
257 elif options.libs:
258 print GetLDFlags()
259 elif options.tool:
260 print GetToolPath(options.toolchain, options.arch, options.tool)
261 else:
262 parser.error('Expected a command. Run with --help for more information.')
264 return 0
267 if __name__ == '__main__':
268 try:
269 sys.exit(main(sys.argv[1:]))
270 except Error as e:
271 sys.stderr.write(str(e) + '\n')
272 sys.exit(1)