Explicitly call deprecated AX name calculation API.
[chromium-blink-merge.git] / native_client_sdk / src / build_tools / dsc2gyp.py
blob7032e94c0631d84d167f51e9e63be25507bbbec1
1 #!/usr/bin/env python
2 # Copyright (c) 2012 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 import argparse
7 import StringIO
8 import sys
9 import os
11 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
12 sys.path.append(os.path.join(os.path.dirname(SCRIPT_DIR), 'tools'))
14 import getos
16 valid_tools = ['newlib', 'glibc', getos.GetPlatform()]
19 def Error(msg):
20 print(msg)
21 sys.exit(1)
24 PREAMBLE = """\
26 'includes': ['%s/build_tools/nacl.gypi'],
27 """
29 NEXE_TARGET = """\
31 'target_name': '%(NAME)s_x86_32%(EXT)s',
32 'product_name': '%(NAME)s_x86_32%(EXT)s',
33 'type': '%(GYP_TYPE)s',
34 'sources': %(SOURCES)s,
35 'libraries': %(LIBS)s,
36 'include_dirs': %(INCLUDES)s,
37 'cflags': ['-m32', '-pedantic'] + %(CFLAGS)s,
38 'make_valid_configurations': ['newlib-debug', 'newlib-release',
39 'glibc-debug', 'glibc-release'],
40 'ldflags': ['-m32', '-L../../lib/x86_32/<(CONFIGURATION_NAME)'],
41 'toolset': 'target',
42 %(CONFIGS)s
45 'target_name': '%(NAME)s_x86_64%(EXT)s',
46 'product_name': '%(NAME)s_x86_64%(EXT)s',
47 'type': '%(GYP_TYPE)s',
48 'sources': %(SOURCES)s,
49 'libraries': %(LIBS)s,
50 'include_dirs': %(INCLUDES)s,
51 'make_valid_configurations': ['newlib-debug', 'newlib-release',
52 'glibc-debug', 'glibc-release'],
53 'cflags': ['-m64', '-pedantic'] + %(CFLAGS)s,
54 'ldflags': ['-m64', '-L../../lib/x86_64/<(CONFIGURATION_NAME)'],
55 'toolset': 'target',
56 %(CONFIGS)s
58 """
60 NLIB_TARGET = """\
62 'target_name': '%(NAME)s_x86_32%(EXT)s',
63 'product_name': 'lib%(NAME)s%(EXT)s',
64 'product_dir': '../../lib/x86_32/<(CONFIGURATION_NAME)',
65 'type': '%(GYP_TYPE)s',
66 'sources': %(SOURCES)s,
67 'libraries': %(LIBS)s,
68 'include_dirs': %(INCLUDES)s,
69 'cflags': ['-m32', '-pedantic'] + %(CFLAGS)s,
70 'make_valid_configurations': ['newlib-debug', 'newlib-release',
71 'glibc-debug', 'glibc-release'],
72 'ldflags': ['-m32'],
73 'toolset': 'target',
74 %(CONFIGS)s
77 'target_name': '%(NAME)s_x86_64%(EXT)s',
78 'product_name': 'lib%(NAME)s%(EXT)s',
79 'product_dir': '../../lib/x86_64/<(CONFIGURATION_NAME)',
80 'type': '%(GYP_TYPE)s',
81 'sources': %(SOURCES)s,
82 'libraries': %(LIBS)s,
83 'include_dirs': %(INCLUDES)s,
84 'make_valid_configurations': ['newlib-debug', 'newlib-release',
85 'glibc-debug', 'glibc-release'],
86 'cflags': ['-m64', '-pedantic'] + %(CFLAGS)s,
87 'ldflags': ['-m64'],
88 'toolset': 'target',
89 %(CONFIGS)s
91 """
93 HOST_LIB_TARGET = """\
95 'target_name': '%(NAME)s%(EXT)s',
96 'type': '%(GYP_TYPE)s',
97 'toolset': 'host',
98 'sources': %(SOURCES)s,
99 'cflags': %(CFLAGS)s,
100 'cflags_c': ['-std=gnu99'],
101 'include_dirs': %(INCLUDES)s,
102 'make_valid_configurations': ['host-debug', 'host-release'],
103 'product_dir': '../../lib/%(ARCH)s/<(CONFIGURATION_NAME)',
104 'product_name': '%(NAME)s%(EXT)s',
105 %(CONFIGS)s
109 HOST_EXE_TARGET = """\
111 'target_name': '%(NAME)s%(EXT)s',
112 'type': '%(GYP_TYPE)s',
113 'toolset': 'host',
114 'sources': %(SOURCES)s,
115 'cflags': %(CFLAGS)s,
116 'cflags_c': ['-std=gnu99'],
117 'ldflags': ['-L../../lib/%(ARCH)s/<(CONFIGURATION_NAME)'],
118 'libraries': %(LIBS)s,
119 'include_dirs': %(INCLUDES)s,
120 'make_valid_configurations': ['host-debug', 'host-release'],
121 'msvs_settings': {
122 'VCLinkerTool': {
123 'AdditionalLibraryDirectories':
124 ['../../lib/%(ARCH)s/<(CONFIGURATION_NAME)'],
127 %(CONFIGS)s
131 NMF_TARGET = """\
133 'target_name': '%(NAME)s_%(TOOLCHAIN)s.nmf',
134 'product_name': '%(NAME)s.nmf',
135 'product_dir': '<(PRODUCT_DIR)/%(TOOLCHAIN)s',
136 'type': 'none',
137 'make_valid_configurations': ['%(TOOLCHAIN)s-debug', '%(TOOLCHAIN)s-release'],
138 'actions': [
140 'action_name': 'nmf',
141 'inputs': ['<(PRODUCT_DIR)/%(NAME)s_x86_32.nexe',
142 '<(PRODUCT_DIR)/%(NAME)s_x86_64.nexe'] + %(SODEPS)s,
143 'outputs': ['<(PRODUCT_DIR)/%(NAME)s.nmf'],
144 'action': ['../../tools/create_nmf.py', '-t', '%(TOOLCHAIN)s', '-s',
145 '<(PRODUCT_DIR)'] + %(NMFACTION)s,
151 TOOLCHAIN_CONFIG = """\
152 '%(toolchain)s-release' : {
153 'cflags' : ['-O2'],
155 '%(toolchain)s-debug' : {
156 'cflags' : ['-g', '-O0'],
160 NEXE_CONFIG = """\
161 '%(toolchain)s-release' : {
162 'cflags' : ['--%(toolchain)s', '-O2',
163 '-idirafter', '../../include'],
164 'ldflags' : ['--%(toolchain)s'],
165 'arflags' : ['--%(toolchain)s'],
167 '%(toolchain)s-debug' : {
168 'cflags' : ['--%(toolchain)s', '-g', '-O0',
169 '-idirafter', '../../include'],
170 'ldflags' : ['--%(toolchain)s'],
171 'arflags' : ['--%(toolchain)s'],
175 WIN32_CONFIGS = """\
176 'target_defaults': {
177 'default_configuration': 'Debug_PPAPI',
178 'configurations': {
179 'Debug_PPAPI': {
180 'msvs_configuration_platform': 'PPAPI',
181 'msbuild_configuration_attributes': {
182 'ConfigurationType': 'DynamicLibrary'
184 'include_dirs': ['../../include/win'],
185 'defines': ['_WINDOWS', '_DEBUG', 'WIN32'],
187 'Release_PPAPI': {
188 'msvs_configuration_platform': 'PPAPI',
189 'msbuild_configuration_attributes': {
190 'ConfigurationType': 'DynamicLibrary'
192 'include_dirs': ['../../include/win'],
193 'defines': ['_WINDOWS', 'NDEBUG', 'WIN32'],
195 'Debug_NaCl': {
196 'msvs_configuration_platform': 'NaCl',
197 'msbuild_configuration_attributes': {
198 'ConfigurationType': 'Application'
201 'Release_NaCl': {
202 'msvs_configuration_platform': 'NaCl',
203 'msbuild_configuration_attributes': {
204 'ConfigurationType': 'Application'
212 def WriteNaClTargets(output, target, tools):
213 configs = "'configurations' : {\n"
214 for tc in tools:
215 if tc not in valid_tools:
216 continue
217 if tc in ['newlib', 'glibc']:
218 configs += NEXE_CONFIG % {'toolchain': tc}
219 configs += " }"
220 target['CONFIGS'] = configs
221 if target['TYPE'] == 'lib':
222 output.write(NLIB_TARGET % target)
223 else:
224 output.write(NEXE_TARGET % target)
227 def ConfigName(toolchain):
228 if toolchain == getos.GetPlatform():
229 return 'host'
230 else:
231 return toolchain
234 def ProcessDSC(filename, outfile=None):
235 if not os.path.exists(filename):
236 Error("file not found: %s" % filename)
238 desc = open(filename).read()
239 desc = eval(desc, {}, {})
240 if not desc.get('TARGETS'):
241 Error("no TARGETS found in dsc")
243 if not outfile:
244 outfile = desc['NAME'] + '.gyp'
245 outfile = os.path.join(os.path.dirname(filename), outfile)
247 output = StringIO.StringIO()
249 srcdir = os.path.dirname(SCRIPT_DIR)
250 output.write(PREAMBLE % srcdir.replace("\\", '/'))
252 win32 = sys.platform in ('win32', 'cygwin')
253 if win32:
254 output.write(WIN32_CONFIGS)
255 else:
256 for tc in desc['TOOLS']:
257 if tc in valid_tools:
258 default = '%s-debug' % ConfigName(tc)
259 break
261 output.write("""\
262 'target_defaults': {
263 'default_configuration': '%s',
264 'configurations' : {\n""" % default)
266 for tc in desc['TOOLS']:
267 if tc not in valid_tools:
268 continue
269 output.write(TOOLCHAIN_CONFIG % {'toolchain': ConfigName(tc)})
271 output.write(" }\n },\n")
273 output.write("\n 'targets': [\n")
275 # make a list of all the so target names so that the nmf rules
276 # can depend on them all
277 sofiles = []
278 soremap = []
279 for target in desc['TARGETS']:
280 if target['TYPE'] == 'so':
281 name = target['NAME']
282 sofiles.append('<(PRODUCT_DIR)/%s_x86_64.so' % name)
283 sofiles.append('<(PRODUCT_DIR)/%s_x86_32.so' % name)
284 soremap += ['-n', '%s_x86_64.so,%s.so' % (name, name)]
285 soremap += ['-n', '%s_x86_32.so,%s.so' % (name, name)]
288 # iterate through dsc targets generating gyp targets
289 for target in desc['TARGETS']:
290 target.setdefault('INCLUDES', [])
291 target['INCLUDES'] = [x.replace("$(NACL_SDK_ROOT)", "../..")
292 for x in target['INCLUDES']]
294 libs = target.get('LIBS', [])
295 if win32:
296 libs = [l for l in libs if l not in ('ppapi', 'ppapi_cpp')]
297 target['LIBS'] = ['-l' + l + '.lib' for l in libs]
298 else:
299 target['LIBS'] = ['-l' + l for l in libs]
300 if target['TYPE'] == 'so':
301 if win32:
302 target['EXT'] = ''
303 else:
304 target['EXT'] = '.so'
305 target['GYP_TYPE'] = 'shared_library'
306 elif target['TYPE'] == 'lib':
307 if win32:
308 target['EXT'] = ''
309 else:
310 target['EXT'] = '.a'
311 target['GYP_TYPE'] = 'static_library'
312 elif target['TYPE'] == 'main':
313 target['EXT'] = '.nexe'
314 target['GYP_TYPE'] = 'executable'
315 else:
316 Error("unknown type: %s" % target['TYPE'])
318 target['CFLAGS'] = target.get('CXXFLAGS', [])
320 if not win32 and ('newlib' in desc['TOOLS'] or 'glibc' in desc['TOOLS']):
321 WriteNaClTargets(output, target, desc['TOOLS'])
322 if target['TYPE'] == 'main':
323 target['SODEPS'] = sofiles
324 target['NMFACTION'] = ['-o', '<@(_outputs)', '-L<(NMF_PATH1)',
325 '-L<(NMF_PATH2)', '-D', '<(OBJDUMP)',
326 '<@(_inputs)']
327 target['NMFACTION'] += soremap
328 if 'newlib' in desc['TOOLS']:
329 target['TOOLCHAIN'] = 'newlib'
330 output.write(NMF_TARGET % target)
331 if 'glibc' in desc['TOOLS']:
332 target['TOOLCHAIN'] = 'glibc'
333 output.write(NMF_TARGET % target)
335 if win32 or getos.GetPlatform() in desc['TOOLS']:
336 target['ARCH'] = 'x86_32'
337 target['INCLUDES'].append('../../include')
338 if win32:
339 target['HOST'] = 'win'
340 target['CONFIGS'] = ''
341 target['CFLAGS'] = []
342 else:
343 target['CONFIGS'] = ''
344 target['HOST'] = 'linux'
345 target['CFLAGS'].append('-fPIC')
346 if target['TYPE'] == 'main':
347 target['GYP_TYPE'] = 'shared_library'
348 if win32:
349 target['EXT'] = ''
350 else:
351 target['EXT'] = '.so'
352 output.write(HOST_EXE_TARGET % target)
353 else:
354 output.write(HOST_LIB_TARGET % target)
356 output.write(' ],\n}\n')
358 print('Writing: ' + outfile)
359 open(outfile, 'w').write(output.getvalue())
362 def main(args):
363 parser = argparse.ArgumentParser()
364 parser.add_argument('-o', help='Set output filename.', dest='output')
365 parser.add_argument('dsc', help='dsc to convert')
366 options = parser.parse_args(args)
368 if options.output:
369 outdir = os.path.dirname(options.output)
370 if not os.path.exists(outdir):
371 os.makedirs(outdir)
373 ProcessDSC(options.dsc, options.output)
374 return 0
377 if __name__ == '__main__':
378 sys.exit(main(sys.argv[1:]))