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  'EXECUTABLE_PREFIX',
43  'EXECUTABLE_SUFFIX',
44  'INTERMEDIATE_DIR',
45  'PRODUCT_DIR',
46  'RULE_INPUT_ROOT',
47  'RULE_INPUT_DIRNAME',
48  'RULE_INPUT_EXT',
49  'RULE_INPUT_NAME',
50  'RULE_INPUT_PATH',
51  'SHARED_INTERMEDIATE_DIR',
52]
53
54# gypd doesn't define a default value for OS like many other generator
55# modules.  Specify "-D OS=whatever" on the command line to provide a value.
56generator_default_variables = {
57}
58
59# gypd supports multiple toolsets
60generator_supports_multiple_toolsets = True
61
62# TODO(mark): This always uses <, which isn't right.  The input module should
63# notify the generator to tell it which phase it is operating in, and this
64# module should use < for the early phase and then switch to > for the late
65# phase.  Bonus points for carrying @ back into the output too.
66for v in _generator_identity_variables:
67  generator_default_variables[v] = '<(%s)' % v
68
69
70def GenerateOutput(target_list, target_dicts, data, params):
71  output_files = {}
72  for qualified_target in target_list:
73    [input_file, target] = \
74        gyp.common.ParseQualifiedTarget(qualified_target)[0:2]
75
76    if input_file[-4:] != '.gyp':
77      continue
78    input_file_stem = input_file[:-4]
79    output_file = input_file_stem + params['options'].suffix + '.gypd'
80
81    if not output_file in output_files:
82      output_files[output_file] = input_file
83
84  for output_file, input_file in output_files.iteritems():
85    output = open(output_file, 'w')
86    pprint.pprint(data[input_file], output)
87    output.close()
88