summaryrefslogtreecommitdiffstats
path: root/native_client_sdk/src/tools/nacl_config.py
blob: 1a9b4697a9c1e02389cf372a2764c1a49254aff6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""A helper script to print paths of NaCl binaries, includes, libs, etc.

It is similar in behavior to pkg-config or sdl-config.
"""

import optparse
import os
import posixpath
import sys

import getos


if sys.version_info < (2, 6, 0):
  sys.stderr.write("python 2.6 or later is required run this script\n")
  sys.exit(1)


VALID_ARCHES = ('arm', 'x86_32', 'x86_64', 'i686')
ARCH_NAME = {
  'arm': 'arm',
  'x86_32': 'i686',
  'i686': 'i686',
  'x86_64': 'x86_64'
}

ARCH_ALT_NAME = {
  'arm': 'arm',
  'x86_32': 'x86_32',
  'i686': 'x86_32',
  'x86_64': 'x86_64'
}

ARCH_BASE_NAME = {
  'arm': 'arm',
  'x86_32': 'x86',
  'i686': 'x86',
  'x86_64': 'x86'
}

NACL_TOOLCHAINS = ('newlib', 'glibc', 'pnacl')
HOST_TOOLCHAINS = ('linux', 'mac', 'win')
VALID_TOOLCHAINS = list(HOST_TOOLCHAINS) + list(NACL_TOOLCHAINS) + ['host']

# This is not an exhaustive list of tools, just the ones that need to be
# special-cased.

# e.g. For PNaCL cc => pnacl-clang
#      For NaCl  cc => pnacl-gcc
#
# Most tools will be passed through directly.
# e.g. For PNaCl foo => pnacl-foo
#      For NaCl  foo => x86_64-nacl-foo.
PNACL_TOOLS = {
  'cc': 'clang',
  'c++': 'clang++',
  'gcc': 'clang',
  'g++': 'clang++',
  'ld': 'clang++'
}

NACL_TOOLS = {
  'cc': 'gcc',
  'c++': 'g++',
  'gcc': 'gcc',
  'g++': 'g++',
  'ld': 'g++'
}


class Error(Exception):
  pass


def Expect(condition, message):
  if not condition:
    raise Error(message)


def ExpectToolchain(toolchain, expected_toolchains):
  Expect(toolchain in expected_toolchains,
         'Expected toolchain to be one of [%s], not %s.' % (
             ', '.join(expected_toolchains), toolchain))


def ExpectArch(arch, expected_arches):
  Expect(arch in expected_arches,
         'Expected arch to be one of [%s], not %s.' % (
             ', '.join(expected_arches), arch))


def CheckValidToolchainArch(toolchain, arch, arch_required=False):
  if toolchain or arch or arch_required:
    ExpectToolchain(toolchain, VALID_TOOLCHAINS)

  if toolchain in HOST_TOOLCHAINS:
    Expect(arch is None,
           'Expected no arch for host toolchain %r. Got %r.' % (
               toolchain, arch))
  elif toolchain == 'pnacl':
    Expect(arch is None,
           'Expected no arch for toolchain %r. Got %r.' % (toolchain, arch))
  elif arch_required:
    Expect(arch is not None,
           'Expected arch to be one of [%s] for toolchain %r.\n'
           'Use the -a or --arch flags to specify one.\n' % (
               ', '.join(VALID_ARCHES), toolchain))

  if arch:
    ExpectArch(arch, VALID_ARCHES)
    if arch == 'arm':
      Expect(toolchain == 'newlib', 'The arm arch only supports newlib.')


def GetArchName(arch):
  return ARCH_NAME.get(arch)


def GetArchAltName(arch):
  return ARCH_ALT_NAME.get(arch)


def GetArchBaseName(arch):
  return ARCH_BASE_NAME.get(arch)


def CanonicalizeToolchain(toolchain):
  if toolchain == 'host':
    return getos.GetPlatform()
  return toolchain


def GetPosixSDKPath():
  sdk_path = getos.GetSDKPath()
  if getos.GetPlatform() == 'win':
    return sdk_path.replace('\\', '/')
  else:
    return sdk_path


def GetToolchainDir(toolchain, arch=None):
  ExpectToolchain(toolchain, NACL_TOOLCHAINS)
  root = GetPosixSDKPath()
  platform = getos.GetPlatform()
  if toolchain == 'pnacl':
    assert arch is None
    subdir = '%s_pnacl' % platform
  else:
    assert arch is not None
    subdir = '%s_%s_%s' % (platform, GetArchBaseName(arch), toolchain)

  return posixpath.join(root, 'toolchain', subdir)


def GetToolchainArchDir(toolchain, arch):
  ExpectToolchain(toolchain, NACL_TOOLCHAINS)
  assert arch is not None
  toolchain_dir = GetToolchainDir(toolchain, arch)
  arch_dir = '%s-nacl' % GetArchName(arch)
  return posixpath.join(toolchain_dir, arch_dir)


def GetToolchainBinDir(toolchain, arch=None):
  ExpectToolchain(toolchain, NACL_TOOLCHAINS)
  return posixpath.join(GetToolchainDir(toolchain, arch), 'bin')


def GetSDKIncludeDirs(toolchain):
  root = GetPosixSDKPath()
  base_include = posixpath.join(root, 'include')
  return [base_include, posixpath.join(base_include, toolchain)]


def GetSDKLibDir():
  return posixpath.join(GetPosixSDKPath(), 'lib')


# Commands

def GetToolPath(toolchain, arch, tool):
  if tool == 'gdb':
    # Always use the same gdb; it supports multiple toolchains/architectures.
    # NOTE: this is always a i686 executable. i686-nacl-gdb is a symlink to
    # x86_64-nacl-gdb.
    return posixpath.join(GetToolchainBinDir('newlib', 'x86_64'),
                        'x86_64-nacl-gdb')

  if toolchain == 'pnacl':
    tool = PNACL_TOOLS.get(tool, tool)
    full_tool_name = 'pnacl-%s' % tool
  else:
    ExpectArch(arch, VALID_ARCHES)
    tool = NACL_TOOLS.get(tool, tool)
    full_tool_name = '%s-nacl-%s' % (GetArchName(arch), tool)
  return posixpath.join(GetToolchainBinDir(toolchain, arch), full_tool_name)


def GetCFlags(toolchain):
  ExpectToolchain(toolchain, VALID_TOOLCHAINS)
  return ' '.join('-I%s' % dirname for dirname in GetSDKIncludeDirs(toolchain))


def GetLDFlags():
  return '-L%s' % GetSDKLibDir()


def main(args):
  usage = 'Usage: %prog [options] <command>'
  parser = optparse.OptionParser(usage=usage, description=__doc__)
  parser.add_option('-t', '--toolchain', help='toolchain name. This can also '
                    'be specified with the NACL_TOOLCHAIN environment '
                    'variable.')
  parser.add_option('-a', '--arch', help='architecture name. This can also be '
                    'specified with the NACL_ARCH environment variable.')

  group = optparse.OptionGroup(parser, 'Commands')
  group.add_option('--tool', help='get tool path')
  group.add_option('--cflags',
                    help='output all preprocessor and compiler flags',
                    action='store_true')
  group.add_option('--libs', '--ldflags', help='output all linker flags',
                    action='store_true')
  parser.add_option_group(group)

  options, _ = parser.parse_args(args)

  # Get toolchain/arch from environment, if not specified on commandline
  options.toolchain = options.toolchain or os.getenv('NACL_TOOLCHAIN')
  options.arch = options.arch or os.getenv('NACL_ARCH')

  options.toolchain = CanonicalizeToolchain(options.toolchain)
  CheckValidToolchainArch(options.toolchain, options.arch)

  if options.cflags:
    print GetCFlags(options.toolchain)
  elif options.libs:
    print GetLDFlags()
  elif options.tool:
    CheckValidToolchainArch(options.toolchain, options.arch, True)
    print GetToolPath(options.toolchain, options.arch, options.tool)
  else:
    parser.error('Expected a command. Run with --help for more information.')

  return 0


if __name__ == '__main__':
  try:
    sys.exit(main(sys.argv[1:]))
  except Error as e:
    sys.stderr.write(str(e) + '\n')
    sys.exit(1)