1# Copyright (c) 2011 Google Inc. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""gypd output module
6
7This module produces gyp input as its output.  Output files are given the
8.gypd extension to avoid overwriting the .gyp files that they are generated
9from.  Internal references to .gyp files (such as those found in
10"dependencies" sections) are not adjusted to point to .gypd files instead;
11unlike other paths, which are relative to the .gyp or .gypd file, such paths
12are relative to the directory from which gyp was run to create the .gypd file.
13
14This generator module is intended to be a sample and a debugging aid, hence
15the "d" for "debug" in .gypd.  It is useful to inspect the results of the
16various merges, expansions, and conditional evaluations performed by gyp
17and to see a representation of what would be fed to a generator module.
18
19It's not advisable to rename .gypd files produced by this module to .gyp,
20because they will have all merges, expansions, and evaluations already
21performed and the relevant constructs not present in the output; paths to
22dependencies may be wrong; and various sections that do not belong in .gyp
23files such as such as "included_files" and "*_excluded" will be present.
24Output will also be stripped of comments.  This is not intended to be a
25general-purpose gyp pretty-printer; for that, you probably just want to
26run "pprint.pprint(eval(open('source.gyp').read()))", which will still strip
27comments but won't do all of the other things done to this module's output.
28
29The specific formatting of the output generated by this module is subject
30to change.
31"""
32
33
34import gyp.common
35import errno
36import os
37import pprint
38
39
40# These variables should just be spit back out as variable references.
41_generator_identity_variables = [
42  'CONFIGURATION_NAME',
43  'EXECUTABLE_PREFIX',
44  'EXECUTABLE_SUFFIX',
45  'INTERMEDIATE_DIR',
46  'LIB_DIR',
47  'PRODUCT_DIR',
48  'RULE_INPUT_ROOT',
49  'RULE_INPUT_DIRNAME',
50  'RULE_INPUT_EXT',
51  'RULE_INPUT_NAME',
52  'RULE_INPUT_PATH',
53  'SHARED_INTERMEDIATE_DIR',
54  'SHARED_LIB_DIR',
55  'SHARED_LIB_PREFIX',
56  'SHARED_LIB_SUFFIX',
57  'STATIC_LIB_PREFIX',
58  'STATIC_LIB_SUFFIX',
59]
60
61# gypd doesn't define a default value for OS like many other generator
62# modules.  Specify "-D OS=whatever" on the command line to provide a value.
63generator_default_variables = {
64}
65
66# gypd supports multiple toolsets
67generator_supports_multiple_toolsets = True
68
69# TODO(mark): This always uses <, which isn't right.  The input module should
70# notify the generator to tell it which phase it is operating in, and this
71# module should use < for the early phase and then switch to > for the late
72# phase.  Bonus points for carrying @ back into the output too.
73for v in _generator_identity_variables:
74  generator_default_variables[v] = '<(%s)' % v
75
76
77def GenerateOutput(target_list, target_dicts, data, params):
78  output_files = {}
79  for qualified_target in target_list:
80    [input_file, target] = \
81        gyp.common.ParseQualifiedTarget(qualified_target)[0:2]
82
83    if input_file[-4:] != '.gyp':
84      continue
85    input_file_stem = input_file[:-4]
86    output_file = input_file_stem + params['options'].suffix + '.gypd'
87
88    if not output_file in output_files:
89      output_files[output_file] = input_file
90
91  for output_file, input_file in output_files.iteritems():
92    output = open(output_file, 'w')
93    pprint.pprint(data[input_file], output)
94    output.close()
95