wsba-hockey 1.0.2__py3-none-any.whl → 1.0.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. wsba_hockey/data_pipelines.py +183 -0
  2. wsba_hockey/evidence/weakside-breakout/node_modules/duckdb/vendor.py +146 -0
  3. wsba_hockey/evidence/weakside-breakout/node_modules/flatted/python/flatted.py +149 -0
  4. wsba_hockey/evidence/weakside-breakout/node_modules/flatted/python/test.py +63 -0
  5. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/gyp_main.py +45 -0
  6. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/MSVSNew.py +367 -0
  7. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/MSVSProject.py +206 -0
  8. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/MSVSSettings.py +1270 -0
  9. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/MSVSSettings_test.py +1547 -0
  10. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/MSVSToolFile.py +59 -0
  11. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/MSVSUserFile.py +153 -0
  12. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/MSVSUtil.py +271 -0
  13. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/MSVSVersion.py +574 -0
  14. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/__init__.py +690 -0
  15. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/common.py +661 -0
  16. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/common_test.py +78 -0
  17. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/easy_xml.py +165 -0
  18. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/easy_xml_test.py +109 -0
  19. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/flock_tool.py +55 -0
  20. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/__init__.py +0 -0
  21. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/analyzer.py +808 -0
  22. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/android.py +1173 -0
  23. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/cmake.py +1321 -0
  24. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/compile_commands_json.py +120 -0
  25. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/dump_dependency_json.py +103 -0
  26. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/eclipse.py +464 -0
  27. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/gypd.py +89 -0
  28. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/gypsh.py +58 -0
  29. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/make.py +2714 -0
  30. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/msvs.py +3981 -0
  31. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/msvs_test.py +44 -0
  32. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/ninja.py +2936 -0
  33. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/ninja_test.py +55 -0
  34. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/xcode.py +1394 -0
  35. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/generator/xcode_test.py +25 -0
  36. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/input.py +3130 -0
  37. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/input_test.py +98 -0
  38. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/mac_tool.py +771 -0
  39. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/msvs_emulation.py +1271 -0
  40. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/ninja_syntax.py +174 -0
  41. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/simple_copy.py +61 -0
  42. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/win_tool.py +374 -0
  43. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation.py +1939 -0
  44. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/xcode_ninja.py +302 -0
  45. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/xcodeproj_file.py +3197 -0
  46. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/pylib/gyp/xml_fix.py +65 -0
  47. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/test_gyp.py +261 -0
  48. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/tools/graphviz.py +102 -0
  49. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/tools/pretty_gyp.py +156 -0
  50. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/tools/pretty_sln.py +181 -0
  51. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/gyp/tools/pretty_vcproj.py +339 -0
  52. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/test/fixtures/test-charmap.py +31 -0
  53. wsba_hockey/evidence/weakside-breakout/node_modules/node-gyp/update-gyp.py +64 -0
  54. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/gyp_main.py +45 -0
  55. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/MSVSNew.py +367 -0
  56. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/MSVSProject.py +206 -0
  57. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/MSVSSettings.py +1270 -0
  58. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/MSVSSettings_test.py +1547 -0
  59. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/MSVSToolFile.py +59 -0
  60. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/MSVSUserFile.py +153 -0
  61. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/MSVSUtil.py +271 -0
  62. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/MSVSVersion.py +574 -0
  63. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/__init__.py +666 -0
  64. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/common.py +654 -0
  65. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/common_test.py +78 -0
  66. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/easy_xml.py +165 -0
  67. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/easy_xml_test.py +109 -0
  68. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/flock_tool.py +55 -0
  69. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/__init__.py +0 -0
  70. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/analyzer.py +808 -0
  71. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/android.py +1173 -0
  72. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/cmake.py +1321 -0
  73. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/compile_commands_json.py +120 -0
  74. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/dump_dependency_json.py +103 -0
  75. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/eclipse.py +464 -0
  76. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/gypd.py +89 -0
  77. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/gypsh.py +58 -0
  78. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/make.py +2518 -0
  79. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/msvs.py +3978 -0
  80. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/msvs_test.py +44 -0
  81. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/ninja.py +2936 -0
  82. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/ninja_test.py +55 -0
  83. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/xcode.py +1394 -0
  84. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/generator/xcode_test.py +25 -0
  85. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/input.py +3137 -0
  86. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/input_test.py +98 -0
  87. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/mac_tool.py +771 -0
  88. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/msvs_emulation.py +1271 -0
  89. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/ninja_syntax.py +174 -0
  90. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/simple_copy.py +61 -0
  91. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/win_tool.py +374 -0
  92. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation.py +1939 -0
  93. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/xcode_ninja.py +302 -0
  94. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/xcodeproj_file.py +3197 -0
  95. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/pylib/gyp/xml_fix.py +65 -0
  96. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/setup.py +42 -0
  97. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/test_gyp.py +260 -0
  98. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/tools/graphviz.py +102 -0
  99. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/tools/pretty_gyp.py +156 -0
  100. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/tools/pretty_sln.py +181 -0
  101. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/gyp/tools/pretty_vcproj.py +339 -0
  102. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/test/fixtures/test-charmap.py +31 -0
  103. wsba_hockey/evidence/weakside-breakout/node_modules/sqlite3/node_modules/node-gyp/update-gyp.py +46 -0
  104. wsba_hockey/evidence/weakside-breakout/wsba_nhl_apps/wsba_nhl_apps/game_stats/app.py +401 -0
  105. wsba_hockey/evidence/weakside-breakout/wsba_nhl_apps/wsba_nhl_apps/game_stats/name_fix.py +47 -0
  106. wsba_hockey/evidence/weakside-breakout/wsba_nhl_apps/wsba_nhl_apps/heatmaps/app.py +108 -0
  107. wsba_hockey/evidence/weakside-breakout/wsba_nhl_apps/wsba_nhl_apps/heatmaps/plot.py +93 -0
  108. wsba_hockey/evidence/weakside-breakout/wsba_nhl_apps/wsba_nhl_apps/heatmaps/rink_plot.py +245 -0
  109. wsba_hockey/evidence/weakside-breakout/wsba_nhl_apps/wsba_nhl_apps/matchups/app.py +145 -0
  110. wsba_hockey/evidence/weakside-breakout/wsba_nhl_apps/wsba_nhl_apps/matchups/plot.py +77 -0
  111. wsba_hockey/evidence/weakside-breakout/wsba_nhl_apps/wsba_nhl_apps/matchups/rink_plot.py +245 -0
  112. wsba_hockey/evidence/weakside-breakout/wsba_nhl_apps/wsba_nhl_apps/pbp/app.py +389 -0
  113. wsba_hockey/evidence/weakside-breakout/wsba_nhl_apps/wsba_nhl_apps/pbp/plot.py +70 -0
  114. wsba_hockey/evidence/weakside-breakout/wsba_nhl_apps/wsba_nhl_apps/pbp/rink_plot.py +245 -0
  115. wsba_hockey/evidence/weakside-breakout/wsba_nhl_apps/wsba_nhl_apps/skater/app.py +110 -0
  116. wsba_hockey/evidence/weakside-breakout/wsba_nhl_apps/wsba_nhl_apps/skater/plot.py +58 -0
  117. wsba_hockey/evidence/weakside-breakout/wsba_nhl_apps/wsba_nhl_apps/skater/rink_plot.py +245 -0
  118. wsba_hockey/tools/agg.py +242 -53
  119. wsba_hockey/tools/plotting.py +15 -20
  120. wsba_hockey/tools/scraping.py +149 -258
  121. wsba_hockey/tools/xg_model.py +370 -298
  122. wsba_hockey/workspace.py +22 -101
  123. wsba_hockey/wsba_main.py +494 -147
  124. {wsba_hockey-1.0.2.dist-info → wsba_hockey-1.0.4.dist-info}/METADATA +2 -2
  125. wsba_hockey-1.0.4.dist-info/RECORD +135 -0
  126. {wsba_hockey-1.0.2.dist-info → wsba_hockey-1.0.4.dist-info}/WHEEL +1 -1
  127. wsba_hockey/stats/calculate_viz/shot_impact.py +0 -2
  128. wsba_hockey-1.0.2.dist-info/RECORD +0 -19
  129. {wsba_hockey-1.0.2.dist-info → wsba_hockey-1.0.4.dist-info}/licenses/LICENSE +0 -0
  130. {wsba_hockey-1.0.2.dist-info → wsba_hockey-1.0.4.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,3137 @@
1
+ # Copyright (c) 2012 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
+
6
+ import ast
7
+
8
+ import gyp.common
9
+ import gyp.simple_copy
10
+ import multiprocessing
11
+ import os.path
12
+ import re
13
+ import shlex
14
+ import signal
15
+ import subprocess
16
+ import sys
17
+ import threading
18
+ import traceback
19
+ from distutils.version import StrictVersion
20
+ from gyp.common import GypError
21
+ from gyp.common import OrderedSet
22
+
23
+ # A list of types that are treated as linkable.
24
+ linkable_types = [
25
+ "executable",
26
+ "shared_library",
27
+ "loadable_module",
28
+ "mac_kernel_extension",
29
+ "windows_driver",
30
+ ]
31
+
32
+ # A list of sections that contain links to other targets.
33
+ dependency_sections = ["dependencies", "export_dependent_settings"]
34
+
35
+ # base_path_sections is a list of sections defined by GYP that contain
36
+ # pathnames. The generators can provide more keys, the two lists are merged
37
+ # into path_sections, but you should call IsPathSection instead of using either
38
+ # list directly.
39
+ base_path_sections = [
40
+ "destination",
41
+ "files",
42
+ "include_dirs",
43
+ "inputs",
44
+ "libraries",
45
+ "outputs",
46
+ "sources",
47
+ ]
48
+ path_sections = set()
49
+
50
+ # These per-process dictionaries are used to cache build file data when loading
51
+ # in parallel mode.
52
+ per_process_data = {}
53
+ per_process_aux_data = {}
54
+
55
+
56
+ def IsPathSection(section):
57
+ # If section ends in one of the '=+?!' characters, it's applied to a section
58
+ # without the trailing characters. '/' is notably absent from this list,
59
+ # because there's no way for a regular expression to be treated as a path.
60
+ while section and section[-1:] in "=+?!":
61
+ section = section[:-1]
62
+
63
+ if section in path_sections:
64
+ return True
65
+
66
+ # Sections matching the regexp '_(dir|file|path)s?$' are also
67
+ # considered PathSections. Using manual string matching since that
68
+ # is much faster than the regexp and this can be called hundreds of
69
+ # thousands of times so micro performance matters.
70
+ if "_" in section:
71
+ tail = section[-6:]
72
+ if tail[-1] == "s":
73
+ tail = tail[:-1]
74
+ if tail[-5:] in ("_file", "_path"):
75
+ return True
76
+ return tail[-4:] == "_dir"
77
+
78
+ return False
79
+
80
+
81
+ # base_non_configuration_keys is a list of key names that belong in the target
82
+ # itself and should not be propagated into its configurations. It is merged
83
+ # with a list that can come from the generator to
84
+ # create non_configuration_keys.
85
+ base_non_configuration_keys = [
86
+ # Sections that must exist inside targets and not configurations.
87
+ "actions",
88
+ "configurations",
89
+ "copies",
90
+ "default_configuration",
91
+ "dependencies",
92
+ "dependencies_original",
93
+ "libraries",
94
+ "postbuilds",
95
+ "product_dir",
96
+ "product_extension",
97
+ "product_name",
98
+ "product_prefix",
99
+ "rules",
100
+ "run_as",
101
+ "sources",
102
+ "standalone_static_library",
103
+ "suppress_wildcard",
104
+ "target_name",
105
+ "toolset",
106
+ "toolsets",
107
+ "type",
108
+ # Sections that can be found inside targets or configurations, but that
109
+ # should not be propagated from targets into their configurations.
110
+ "variables",
111
+ ]
112
+ non_configuration_keys = []
113
+
114
+ # Keys that do not belong inside a configuration dictionary.
115
+ invalid_configuration_keys = [
116
+ "actions",
117
+ "all_dependent_settings",
118
+ "configurations",
119
+ "dependencies",
120
+ "direct_dependent_settings",
121
+ "libraries",
122
+ "link_settings",
123
+ "sources",
124
+ "standalone_static_library",
125
+ "target_name",
126
+ "type",
127
+ ]
128
+
129
+ # Controls whether or not the generator supports multiple toolsets.
130
+ multiple_toolsets = False
131
+
132
+ # Paths for converting filelist paths to output paths: {
133
+ # toplevel,
134
+ # qualified_output_dir,
135
+ # }
136
+ generator_filelist_paths = None
137
+
138
+
139
+ def GetIncludedBuildFiles(build_file_path, aux_data, included=None):
140
+ """Return a list of all build files included into build_file_path.
141
+
142
+ The returned list will contain build_file_path as well as all other files
143
+ that it included, either directly or indirectly. Note that the list may
144
+ contain files that were included into a conditional section that evaluated
145
+ to false and was not merged into build_file_path's dict.
146
+
147
+ aux_data is a dict containing a key for each build file or included build
148
+ file. Those keys provide access to dicts whose "included" keys contain
149
+ lists of all other files included by the build file.
150
+
151
+ included should be left at its default None value by external callers. It
152
+ is used for recursion.
153
+
154
+ The returned list will not contain any duplicate entries. Each build file
155
+ in the list will be relative to the current directory.
156
+ """
157
+
158
+ if included is None:
159
+ included = []
160
+
161
+ if build_file_path in included:
162
+ return included
163
+
164
+ included.append(build_file_path)
165
+
166
+ for included_build_file in aux_data[build_file_path].get("included", []):
167
+ GetIncludedBuildFiles(included_build_file, aux_data, included)
168
+
169
+ return included
170
+
171
+
172
+ def CheckedEval(file_contents):
173
+ """Return the eval of a gyp file.
174
+ The gyp file is restricted to dictionaries and lists only, and
175
+ repeated keys are not allowed.
176
+ Note that this is slower than eval() is.
177
+ """
178
+
179
+ syntax_tree = ast.parse(file_contents)
180
+ assert isinstance(syntax_tree, ast.Module)
181
+ c1 = syntax_tree.body
182
+ assert len(c1) == 1
183
+ c2 = c1[0]
184
+ assert isinstance(c2, ast.Expr)
185
+ return CheckNode(c2.value, [])
186
+
187
+
188
+ def CheckNode(node, keypath):
189
+ if isinstance(node, ast.Dict):
190
+ dict = {}
191
+ for key, value in zip(node.keys, node.values):
192
+ assert isinstance(key, ast.Str)
193
+ key = key.s
194
+ if key in dict:
195
+ raise GypError(
196
+ "Key '"
197
+ + key
198
+ + "' repeated at level "
199
+ + repr(len(keypath) + 1)
200
+ + " with key path '"
201
+ + ".".join(keypath)
202
+ + "'"
203
+ )
204
+ kp = list(keypath) # Make a copy of the list for descending this node.
205
+ kp.append(key)
206
+ dict[key] = CheckNode(value, kp)
207
+ return dict
208
+ elif isinstance(node, ast.List):
209
+ children = []
210
+ for index, child in enumerate(node.elts):
211
+ kp = list(keypath) # Copy list.
212
+ kp.append(repr(index))
213
+ children.append(CheckNode(child, kp))
214
+ return children
215
+ elif isinstance(node, ast.Str):
216
+ return node.s
217
+ else:
218
+ raise TypeError(
219
+ "Unknown AST node at key path '" + ".".join(keypath) + "': " + repr(node)
220
+ )
221
+
222
+
223
+ def LoadOneBuildFile(build_file_path, data, aux_data, includes, is_target, check):
224
+ if build_file_path in data:
225
+ return data[build_file_path]
226
+
227
+ if os.path.exists(build_file_path):
228
+ build_file_contents = open(build_file_path, encoding='utf-8').read()
229
+ else:
230
+ raise GypError(f"{build_file_path} not found (cwd: {os.getcwd()})")
231
+
232
+ build_file_data = None
233
+ try:
234
+ if check:
235
+ build_file_data = CheckedEval(build_file_contents)
236
+ else:
237
+ build_file_data = eval(build_file_contents, {"__builtins__": {}}, None)
238
+ except SyntaxError as e:
239
+ e.filename = build_file_path
240
+ raise
241
+ except Exception as e:
242
+ gyp.common.ExceptionAppend(e, "while reading " + build_file_path)
243
+ raise
244
+
245
+ if type(build_file_data) is not dict:
246
+ raise GypError("%s does not evaluate to a dictionary." % build_file_path)
247
+
248
+ data[build_file_path] = build_file_data
249
+ aux_data[build_file_path] = {}
250
+
251
+ # Scan for includes and merge them in.
252
+ if "skip_includes" not in build_file_data or not build_file_data["skip_includes"]:
253
+ try:
254
+ if is_target:
255
+ LoadBuildFileIncludesIntoDict(
256
+ build_file_data, build_file_path, data, aux_data, includes, check
257
+ )
258
+ else:
259
+ LoadBuildFileIncludesIntoDict(
260
+ build_file_data, build_file_path, data, aux_data, None, check
261
+ )
262
+ except Exception as e:
263
+ gyp.common.ExceptionAppend(
264
+ e, "while reading includes of " + build_file_path
265
+ )
266
+ raise
267
+
268
+ return build_file_data
269
+
270
+
271
+ def LoadBuildFileIncludesIntoDict(
272
+ subdict, subdict_path, data, aux_data, includes, check
273
+ ):
274
+ includes_list = []
275
+ if includes is not None:
276
+ includes_list.extend(includes)
277
+ if "includes" in subdict:
278
+ for include in subdict["includes"]:
279
+ # "include" is specified relative to subdict_path, so compute the real
280
+ # path to include by appending the provided "include" to the directory
281
+ # in which subdict_path resides.
282
+ relative_include = os.path.normpath(
283
+ os.path.join(os.path.dirname(subdict_path), include)
284
+ )
285
+ includes_list.append(relative_include)
286
+ # Unhook the includes list, it's no longer needed.
287
+ del subdict["includes"]
288
+
289
+ # Merge in the included files.
290
+ for include in includes_list:
291
+ if "included" not in aux_data[subdict_path]:
292
+ aux_data[subdict_path]["included"] = []
293
+ aux_data[subdict_path]["included"].append(include)
294
+
295
+ gyp.DebugOutput(gyp.DEBUG_INCLUDES, "Loading Included File: '%s'", include)
296
+
297
+ MergeDicts(
298
+ subdict,
299
+ LoadOneBuildFile(include, data, aux_data, None, False, check),
300
+ subdict_path,
301
+ include,
302
+ )
303
+
304
+ # Recurse into subdictionaries.
305
+ for k, v in subdict.items():
306
+ if type(v) is dict:
307
+ LoadBuildFileIncludesIntoDict(v, subdict_path, data, aux_data, None, check)
308
+ elif type(v) is list:
309
+ LoadBuildFileIncludesIntoList(v, subdict_path, data, aux_data, check)
310
+
311
+
312
+ # This recurses into lists so that it can look for dicts.
313
+ def LoadBuildFileIncludesIntoList(sublist, sublist_path, data, aux_data, check):
314
+ for item in sublist:
315
+ if type(item) is dict:
316
+ LoadBuildFileIncludesIntoDict(
317
+ item, sublist_path, data, aux_data, None, check
318
+ )
319
+ elif type(item) is list:
320
+ LoadBuildFileIncludesIntoList(item, sublist_path, data, aux_data, check)
321
+
322
+
323
+ # Processes toolsets in all the targets. This recurses into condition entries
324
+ # since they can contain toolsets as well.
325
+ def ProcessToolsetsInDict(data):
326
+ if "targets" in data:
327
+ target_list = data["targets"]
328
+ new_target_list = []
329
+ for target in target_list:
330
+ # If this target already has an explicit 'toolset', and no 'toolsets'
331
+ # list, don't modify it further.
332
+ if "toolset" in target and "toolsets" not in target:
333
+ new_target_list.append(target)
334
+ continue
335
+ if multiple_toolsets:
336
+ toolsets = target.get("toolsets", ["target"])
337
+ else:
338
+ toolsets = ["target"]
339
+ # Make sure this 'toolsets' definition is only processed once.
340
+ if "toolsets" in target:
341
+ del target["toolsets"]
342
+ if len(toolsets) > 0:
343
+ # Optimization: only do copies if more than one toolset is specified.
344
+ for build in toolsets[1:]:
345
+ new_target = gyp.simple_copy.deepcopy(target)
346
+ new_target["toolset"] = build
347
+ new_target_list.append(new_target)
348
+ target["toolset"] = toolsets[0]
349
+ new_target_list.append(target)
350
+ data["targets"] = new_target_list
351
+ if "conditions" in data:
352
+ for condition in data["conditions"]:
353
+ if type(condition) is list:
354
+ for condition_dict in condition[1:]:
355
+ if type(condition_dict) is dict:
356
+ ProcessToolsetsInDict(condition_dict)
357
+
358
+
359
+ # TODO(mark): I don't love this name. It just means that it's going to load
360
+ # a build file that contains targets and is expected to provide a targets dict
361
+ # that contains the targets...
362
+ def LoadTargetBuildFile(
363
+ build_file_path,
364
+ data,
365
+ aux_data,
366
+ variables,
367
+ includes,
368
+ depth,
369
+ check,
370
+ load_dependencies,
371
+ ):
372
+ # If depth is set, predefine the DEPTH variable to be a relative path from
373
+ # this build file's directory to the directory identified by depth.
374
+ if depth:
375
+ # TODO(dglazkov) The backslash/forward-slash replacement at the end is a
376
+ # temporary measure. This should really be addressed by keeping all paths
377
+ # in POSIX until actual project generation.
378
+ d = gyp.common.RelativePath(depth, os.path.dirname(build_file_path))
379
+ if d == "":
380
+ variables["DEPTH"] = "."
381
+ else:
382
+ variables["DEPTH"] = d.replace("\\", "/")
383
+
384
+ # The 'target_build_files' key is only set when loading target build files in
385
+ # the non-parallel code path, where LoadTargetBuildFile is called
386
+ # recursively. In the parallel code path, we don't need to check whether the
387
+ # |build_file_path| has already been loaded, because the 'scheduled' set in
388
+ # ParallelState guarantees that we never load the same |build_file_path|
389
+ # twice.
390
+ if "target_build_files" in data:
391
+ if build_file_path in data["target_build_files"]:
392
+ # Already loaded.
393
+ return False
394
+ data["target_build_files"].add(build_file_path)
395
+
396
+ gyp.DebugOutput(
397
+ gyp.DEBUG_INCLUDES, "Loading Target Build File '%s'", build_file_path
398
+ )
399
+
400
+ build_file_data = LoadOneBuildFile(
401
+ build_file_path, data, aux_data, includes, True, check
402
+ )
403
+
404
+ # Store DEPTH for later use in generators.
405
+ build_file_data["_DEPTH"] = depth
406
+
407
+ # Set up the included_files key indicating which .gyp files contributed to
408
+ # this target dict.
409
+ if "included_files" in build_file_data:
410
+ raise GypError(build_file_path + " must not contain included_files key")
411
+
412
+ included = GetIncludedBuildFiles(build_file_path, aux_data)
413
+ build_file_data["included_files"] = []
414
+ for included_file in included:
415
+ # included_file is relative to the current directory, but it needs to
416
+ # be made relative to build_file_path's directory.
417
+ included_relative = gyp.common.RelativePath(
418
+ included_file, os.path.dirname(build_file_path)
419
+ )
420
+ build_file_data["included_files"].append(included_relative)
421
+
422
+ # Do a first round of toolsets expansion so that conditions can be defined
423
+ # per toolset.
424
+ ProcessToolsetsInDict(build_file_data)
425
+
426
+ # Apply "pre"/"early" variable expansions and condition evaluations.
427
+ ProcessVariablesAndConditionsInDict(
428
+ build_file_data, PHASE_EARLY, variables, build_file_path
429
+ )
430
+
431
+ # Since some toolsets might have been defined conditionally, perform
432
+ # a second round of toolsets expansion now.
433
+ ProcessToolsetsInDict(build_file_data)
434
+
435
+ # Look at each project's target_defaults dict, and merge settings into
436
+ # targets.
437
+ if "target_defaults" in build_file_data:
438
+ if "targets" not in build_file_data:
439
+ raise GypError("Unable to find targets in build file %s" % build_file_path)
440
+
441
+ index = 0
442
+ while index < len(build_file_data["targets"]):
443
+ # This procedure needs to give the impression that target_defaults is
444
+ # used as defaults, and the individual targets inherit from that.
445
+ # The individual targets need to be merged into the defaults. Make
446
+ # a deep copy of the defaults for each target, merge the target dict
447
+ # as found in the input file into that copy, and then hook up the
448
+ # copy with the target-specific data merged into it as the replacement
449
+ # target dict.
450
+ old_target_dict = build_file_data["targets"][index]
451
+ new_target_dict = gyp.simple_copy.deepcopy(
452
+ build_file_data["target_defaults"]
453
+ )
454
+ MergeDicts(
455
+ new_target_dict, old_target_dict, build_file_path, build_file_path
456
+ )
457
+ build_file_data["targets"][index] = new_target_dict
458
+ index += 1
459
+
460
+ # No longer needed.
461
+ del build_file_data["target_defaults"]
462
+
463
+ # Look for dependencies. This means that dependency resolution occurs
464
+ # after "pre" conditionals and variable expansion, but before "post" -
465
+ # in other words, you can't put a "dependencies" section inside a "post"
466
+ # conditional within a target.
467
+
468
+ dependencies = []
469
+ if "targets" in build_file_data:
470
+ for target_dict in build_file_data["targets"]:
471
+ if "dependencies" not in target_dict:
472
+ continue
473
+ for dependency in target_dict["dependencies"]:
474
+ dependencies.append(
475
+ gyp.common.ResolveTarget(build_file_path, dependency, None)[0]
476
+ )
477
+
478
+ if load_dependencies:
479
+ for dependency in dependencies:
480
+ try:
481
+ LoadTargetBuildFile(
482
+ dependency,
483
+ data,
484
+ aux_data,
485
+ variables,
486
+ includes,
487
+ depth,
488
+ check,
489
+ load_dependencies,
490
+ )
491
+ except Exception as e:
492
+ gyp.common.ExceptionAppend(
493
+ e, "while loading dependencies of %s" % build_file_path
494
+ )
495
+ raise
496
+ else:
497
+ return (build_file_path, dependencies)
498
+
499
+
500
+ def CallLoadTargetBuildFile(
501
+ global_flags,
502
+ build_file_path,
503
+ variables,
504
+ includes,
505
+ depth,
506
+ check,
507
+ generator_input_info,
508
+ ):
509
+ """Wrapper around LoadTargetBuildFile for parallel processing.
510
+
511
+ This wrapper is used when LoadTargetBuildFile is executed in
512
+ a worker process.
513
+ """
514
+
515
+ try:
516
+ signal.signal(signal.SIGINT, signal.SIG_IGN)
517
+
518
+ # Apply globals so that the worker process behaves the same.
519
+ for key, value in global_flags.items():
520
+ globals()[key] = value
521
+
522
+ SetGeneratorGlobals(generator_input_info)
523
+ result = LoadTargetBuildFile(
524
+ build_file_path,
525
+ per_process_data,
526
+ per_process_aux_data,
527
+ variables,
528
+ includes,
529
+ depth,
530
+ check,
531
+ False,
532
+ )
533
+ if not result:
534
+ return result
535
+
536
+ (build_file_path, dependencies) = result
537
+
538
+ # We can safely pop the build_file_data from per_process_data because it
539
+ # will never be referenced by this process again, so we don't need to keep
540
+ # it in the cache.
541
+ build_file_data = per_process_data.pop(build_file_path)
542
+
543
+ # This gets serialized and sent back to the main process via a pipe.
544
+ # It's handled in LoadTargetBuildFileCallback.
545
+ return (build_file_path, build_file_data, dependencies)
546
+ except GypError as e:
547
+ sys.stderr.write("gyp: %s\n" % e)
548
+ return None
549
+ except Exception as e:
550
+ print("Exception:", e, file=sys.stderr)
551
+ print(traceback.format_exc(), file=sys.stderr)
552
+ return None
553
+
554
+
555
+ class ParallelProcessingError(Exception):
556
+ pass
557
+
558
+
559
+ class ParallelState:
560
+ """Class to keep track of state when processing input files in parallel.
561
+
562
+ If build files are loaded in parallel, use this to keep track of
563
+ state during farming out and processing parallel jobs. It's stored
564
+ in a global so that the callback function can have access to it.
565
+ """
566
+
567
+ def __init__(self):
568
+ # The multiprocessing pool.
569
+ self.pool = None
570
+ # The condition variable used to protect this object and notify
571
+ # the main loop when there might be more data to process.
572
+ self.condition = None
573
+ # The "data" dict that was passed to LoadTargetBuildFileParallel
574
+ self.data = None
575
+ # The number of parallel calls outstanding; decremented when a response
576
+ # was received.
577
+ self.pending = 0
578
+ # The set of all build files that have been scheduled, so we don't
579
+ # schedule the same one twice.
580
+ self.scheduled = set()
581
+ # A list of dependency build file paths that haven't been scheduled yet.
582
+ self.dependencies = []
583
+ # Flag to indicate if there was an error in a child process.
584
+ self.error = False
585
+
586
+ def LoadTargetBuildFileCallback(self, result):
587
+ """Handle the results of running LoadTargetBuildFile in another process.
588
+ """
589
+ self.condition.acquire()
590
+ if not result:
591
+ self.error = True
592
+ self.condition.notify()
593
+ self.condition.release()
594
+ return
595
+ (build_file_path0, build_file_data0, dependencies0) = result
596
+ self.data[build_file_path0] = build_file_data0
597
+ self.data["target_build_files"].add(build_file_path0)
598
+ for new_dependency in dependencies0:
599
+ if new_dependency not in self.scheduled:
600
+ self.scheduled.add(new_dependency)
601
+ self.dependencies.append(new_dependency)
602
+ self.pending -= 1
603
+ self.condition.notify()
604
+ self.condition.release()
605
+
606
+
607
+ def LoadTargetBuildFilesParallel(
608
+ build_files, data, variables, includes, depth, check, generator_input_info
609
+ ):
610
+ parallel_state = ParallelState()
611
+ parallel_state.condition = threading.Condition()
612
+ # Make copies of the build_files argument that we can modify while working.
613
+ parallel_state.dependencies = list(build_files)
614
+ parallel_state.scheduled = set(build_files)
615
+ parallel_state.pending = 0
616
+ parallel_state.data = data
617
+
618
+ try:
619
+ parallel_state.condition.acquire()
620
+ while parallel_state.dependencies or parallel_state.pending:
621
+ if parallel_state.error:
622
+ break
623
+ if not parallel_state.dependencies:
624
+ parallel_state.condition.wait()
625
+ continue
626
+
627
+ dependency = parallel_state.dependencies.pop()
628
+
629
+ parallel_state.pending += 1
630
+ global_flags = {
631
+ "path_sections": globals()["path_sections"],
632
+ "non_configuration_keys": globals()["non_configuration_keys"],
633
+ "multiple_toolsets": globals()["multiple_toolsets"],
634
+ }
635
+
636
+ if not parallel_state.pool:
637
+ parallel_state.pool = multiprocessing.Pool(multiprocessing.cpu_count())
638
+ parallel_state.pool.apply_async(
639
+ CallLoadTargetBuildFile,
640
+ args=(
641
+ global_flags,
642
+ dependency,
643
+ variables,
644
+ includes,
645
+ depth,
646
+ check,
647
+ generator_input_info,
648
+ ),
649
+ callback=parallel_state.LoadTargetBuildFileCallback,
650
+ )
651
+ except KeyboardInterrupt as e:
652
+ parallel_state.pool.terminate()
653
+ raise e
654
+
655
+ parallel_state.condition.release()
656
+
657
+ parallel_state.pool.close()
658
+ parallel_state.pool.join()
659
+ parallel_state.pool = None
660
+
661
+ if parallel_state.error:
662
+ sys.exit(1)
663
+
664
+
665
+ # Look for the bracket that matches the first bracket seen in a
666
+ # string, and return the start and end as a tuple. For example, if
667
+ # the input is something like "<(foo <(bar)) blah", then it would
668
+ # return (1, 13), indicating the entire string except for the leading
669
+ # "<" and trailing " blah".
670
+ LBRACKETS = set("{[(")
671
+ BRACKETS = {"}": "{", "]": "[", ")": "("}
672
+
673
+
674
+ def FindEnclosingBracketGroup(input_str):
675
+ stack = []
676
+ start = -1
677
+ for index, char in enumerate(input_str):
678
+ if char in LBRACKETS:
679
+ stack.append(char)
680
+ if start == -1:
681
+ start = index
682
+ elif char in BRACKETS:
683
+ if not stack:
684
+ return (-1, -1)
685
+ if stack.pop() != BRACKETS[char]:
686
+ return (-1, -1)
687
+ if not stack:
688
+ return (start, index + 1)
689
+ return (-1, -1)
690
+
691
+
692
+ def IsStrCanonicalInt(string):
693
+ """Returns True if |string| is in its canonical integer form.
694
+
695
+ The canonical form is such that str(int(string)) == string.
696
+ """
697
+ if type(string) is str:
698
+ # This function is called a lot so for maximum performance, avoid
699
+ # involving regexps which would otherwise make the code much
700
+ # shorter. Regexps would need twice the time of this function.
701
+ if string:
702
+ if string == "0":
703
+ return True
704
+ if string[0] == "-":
705
+ string = string[1:]
706
+ if not string:
707
+ return False
708
+ if "1" <= string[0] <= "9":
709
+ return string.isdigit()
710
+
711
+ return False
712
+
713
+
714
+ # This matches things like "<(asdf)", "<!(cmd)", "<!@(cmd)", "<|(list)",
715
+ # "<!interpreter(arguments)", "<([list])", and even "<([)" and "<(<())".
716
+ # In the last case, the inner "<()" is captured in match['content'].
717
+ early_variable_re = re.compile(
718
+ r"(?P<replace>(?P<type><(?:(?:!?@?)|\|)?)"
719
+ r"(?P<command_string>[-a-zA-Z0-9_.]+)?"
720
+ r"\((?P<is_array>\s*\[?)"
721
+ r"(?P<content>.*?)(\]?)\))"
722
+ )
723
+
724
+ # This matches the same as early_variable_re, but with '>' instead of '<'.
725
+ late_variable_re = re.compile(
726
+ r"(?P<replace>(?P<type>>(?:(?:!?@?)|\|)?)"
727
+ r"(?P<command_string>[-a-zA-Z0-9_.]+)?"
728
+ r"\((?P<is_array>\s*\[?)"
729
+ r"(?P<content>.*?)(\]?)\))"
730
+ )
731
+
732
+ # This matches the same as early_variable_re, but with '^' instead of '<'.
733
+ latelate_variable_re = re.compile(
734
+ r"(?P<replace>(?P<type>[\^](?:(?:!?@?)|\|)?)"
735
+ r"(?P<command_string>[-a-zA-Z0-9_.]+)?"
736
+ r"\((?P<is_array>\s*\[?)"
737
+ r"(?P<content>.*?)(\]?)\))"
738
+ )
739
+
740
+ # Global cache of results from running commands so they don't have to be run
741
+ # more then once.
742
+ cached_command_results = {}
743
+
744
+
745
+ def FixupPlatformCommand(cmd):
746
+ if sys.platform == "win32":
747
+ if type(cmd) is list:
748
+ cmd = [re.sub("^cat ", "type ", cmd[0])] + cmd[1:]
749
+ else:
750
+ cmd = re.sub("^cat ", "type ", cmd)
751
+ return cmd
752
+
753
+
754
+ PHASE_EARLY = 0
755
+ PHASE_LATE = 1
756
+ PHASE_LATELATE = 2
757
+
758
+
759
+ def ExpandVariables(input, phase, variables, build_file):
760
+ # Look for the pattern that gets expanded into variables
761
+ if phase == PHASE_EARLY:
762
+ variable_re = early_variable_re
763
+ expansion_symbol = "<"
764
+ elif phase == PHASE_LATE:
765
+ variable_re = late_variable_re
766
+ expansion_symbol = ">"
767
+ elif phase == PHASE_LATELATE:
768
+ variable_re = latelate_variable_re
769
+ expansion_symbol = "^"
770
+ else:
771
+ assert False
772
+
773
+ input_str = str(input)
774
+ if IsStrCanonicalInt(input_str):
775
+ return int(input_str)
776
+
777
+ # Do a quick scan to determine if an expensive regex search is warranted.
778
+ if expansion_symbol not in input_str:
779
+ return input_str
780
+
781
+ # Get the entire list of matches as a list of MatchObject instances.
782
+ # (using findall here would return strings instead of MatchObjects).
783
+ matches = list(variable_re.finditer(input_str))
784
+ if not matches:
785
+ return input_str
786
+
787
+ output = input_str
788
+ # Reverse the list of matches so that replacements are done right-to-left.
789
+ # That ensures that earlier replacements won't mess up the string in a
790
+ # way that causes later calls to find the earlier substituted text instead
791
+ # of what's intended for replacement.
792
+ matches.reverse()
793
+ for match_group in matches:
794
+ match = match_group.groupdict()
795
+ gyp.DebugOutput(gyp.DEBUG_VARIABLES, "Matches: %r", match)
796
+ # match['replace'] is the substring to look for, match['type']
797
+ # is the character code for the replacement type (< > <! >! <| >| <@
798
+ # >@ <!@ >!@), match['is_array'] contains a '[' for command
799
+ # arrays, and match['content'] is the name of the variable (< >)
800
+ # or command to run (<! >!). match['command_string'] is an optional
801
+ # command string. Currently, only 'pymod_do_main' is supported.
802
+
803
+ # run_command is true if a ! variant is used.
804
+ run_command = "!" in match["type"]
805
+ command_string = match["command_string"]
806
+
807
+ # file_list is true if a | variant is used.
808
+ file_list = "|" in match["type"]
809
+
810
+ # Capture these now so we can adjust them later.
811
+ replace_start = match_group.start("replace")
812
+ replace_end = match_group.end("replace")
813
+
814
+ # Find the ending paren, and re-evaluate the contained string.
815
+ (c_start, c_end) = FindEnclosingBracketGroup(input_str[replace_start:])
816
+
817
+ # Adjust the replacement range to match the entire command
818
+ # found by FindEnclosingBracketGroup (since the variable_re
819
+ # probably doesn't match the entire command if it contained
820
+ # nested variables).
821
+ replace_end = replace_start + c_end
822
+
823
+ # Find the "real" replacement, matching the appropriate closing
824
+ # paren, and adjust the replacement start and end.
825
+ replacement = input_str[replace_start:replace_end]
826
+
827
+ # Figure out what the contents of the variable parens are.
828
+ contents_start = replace_start + c_start + 1
829
+ contents_end = replace_end - 1
830
+ contents = input_str[contents_start:contents_end]
831
+
832
+ # Do filter substitution now for <|().
833
+ # Admittedly, this is different than the evaluation order in other
834
+ # contexts. However, since filtration has no chance to run on <|(),
835
+ # this seems like the only obvious way to give them access to filters.
836
+ if file_list:
837
+ processed_variables = gyp.simple_copy.deepcopy(variables)
838
+ ProcessListFiltersInDict(contents, processed_variables)
839
+ # Recurse to expand variables in the contents
840
+ contents = ExpandVariables(contents, phase, processed_variables, build_file)
841
+ else:
842
+ # Recurse to expand variables in the contents
843
+ contents = ExpandVariables(contents, phase, variables, build_file)
844
+
845
+ # Strip off leading/trailing whitespace so that variable matches are
846
+ # simpler below (and because they are rarely needed).
847
+ contents = contents.strip()
848
+
849
+ # expand_to_list is true if an @ variant is used. In that case,
850
+ # the expansion should result in a list. Note that the caller
851
+ # is to be expecting a list in return, and not all callers do
852
+ # because not all are working in list context. Also, for list
853
+ # expansions, there can be no other text besides the variable
854
+ # expansion in the input string.
855
+ expand_to_list = "@" in match["type"] and input_str == replacement
856
+
857
+ if run_command or file_list:
858
+ # Find the build file's directory, so commands can be run or file lists
859
+ # generated relative to it.
860
+ build_file_dir = os.path.dirname(build_file)
861
+ if build_file_dir == "" and not file_list:
862
+ # If build_file is just a leaf filename indicating a file in the
863
+ # current directory, build_file_dir might be an empty string. Set
864
+ # it to None to signal to subprocess.Popen that it should run the
865
+ # command in the current directory.
866
+ build_file_dir = None
867
+
868
+ # Support <|(listfile.txt ...) which generates a file
869
+ # containing items from a gyp list, generated at gyp time.
870
+ # This works around actions/rules which have more inputs than will
871
+ # fit on the command line.
872
+ if file_list:
873
+ if type(contents) is list:
874
+ contents_list = contents
875
+ else:
876
+ contents_list = contents.split(" ")
877
+ replacement = contents_list[0]
878
+ if os.path.isabs(replacement):
879
+ raise GypError('| cannot handle absolute paths, got "%s"' % replacement)
880
+
881
+ if not generator_filelist_paths:
882
+ path = os.path.join(build_file_dir, replacement)
883
+ else:
884
+ if os.path.isabs(build_file_dir):
885
+ toplevel = generator_filelist_paths["toplevel"]
886
+ rel_build_file_dir = gyp.common.RelativePath(
887
+ build_file_dir, toplevel
888
+ )
889
+ else:
890
+ rel_build_file_dir = build_file_dir
891
+ qualified_out_dir = generator_filelist_paths["qualified_out_dir"]
892
+ path = os.path.join(qualified_out_dir, rel_build_file_dir, replacement)
893
+ gyp.common.EnsureDirExists(path)
894
+
895
+ replacement = gyp.common.RelativePath(path, build_file_dir)
896
+ f = gyp.common.WriteOnDiff(path)
897
+ for i in contents_list[1:]:
898
+ f.write("%s\n" % i)
899
+ f.close()
900
+
901
+ elif run_command:
902
+ use_shell = True
903
+ if match["is_array"]:
904
+ contents = eval(contents)
905
+ use_shell = False
906
+
907
+ # Check for a cached value to avoid executing commands, or generating
908
+ # file lists more than once. The cache key contains the command to be
909
+ # run as well as the directory to run it from, to account for commands
910
+ # that depend on their current directory.
911
+ # TODO(http://code.google.com/p/gyp/issues/detail?id=111): In theory,
912
+ # someone could author a set of GYP files where each time the command
913
+ # is invoked it produces different output by design. When the need
914
+ # arises, the syntax should be extended to support no caching off a
915
+ # command's output so it is run every time.
916
+ cache_key = (str(contents), build_file_dir)
917
+ cached_value = cached_command_results.get(cache_key, None)
918
+ if cached_value is None:
919
+ gyp.DebugOutput(
920
+ gyp.DEBUG_VARIABLES,
921
+ "Executing command '%s' in directory '%s'",
922
+ contents,
923
+ build_file_dir,
924
+ )
925
+
926
+ replacement = ""
927
+
928
+ if command_string == "pymod_do_main":
929
+ # <!pymod_do_main(modulename param eters) loads |modulename| as a
930
+ # python module and then calls that module's DoMain() function,
931
+ # passing ["param", "eters"] as a single list argument. For modules
932
+ # that don't load quickly, this can be faster than
933
+ # <!(python modulename param eters). Do this in |build_file_dir|.
934
+ oldwd = os.getcwd() # Python doesn't like os.open('.'): no fchdir.
935
+ if build_file_dir: # build_file_dir may be None (see above).
936
+ os.chdir(build_file_dir)
937
+ sys.path.append(os.getcwd())
938
+ try:
939
+
940
+ parsed_contents = shlex.split(contents)
941
+ try:
942
+ py_module = __import__(parsed_contents[0])
943
+ except ImportError as e:
944
+ raise GypError(
945
+ "Error importing pymod_do_main"
946
+ "module (%s): %s" % (parsed_contents[0], e)
947
+ )
948
+ replacement = str(
949
+ py_module.DoMain(parsed_contents[1:])
950
+ ).rstrip()
951
+ finally:
952
+ sys.path.pop()
953
+ os.chdir(oldwd)
954
+ assert replacement is not None
955
+ elif command_string:
956
+ raise GypError(
957
+ "Unknown command string '%s' in '%s'."
958
+ % (command_string, contents)
959
+ )
960
+ else:
961
+ # Fix up command with platform specific workarounds.
962
+ contents = FixupPlatformCommand(contents)
963
+ try:
964
+ p = subprocess.Popen(
965
+ contents,
966
+ shell=use_shell,
967
+ stdout=subprocess.PIPE,
968
+ stderr=subprocess.PIPE,
969
+ stdin=subprocess.PIPE,
970
+ cwd=build_file_dir,
971
+ )
972
+ except Exception as e:
973
+ raise GypError(
974
+ "%s while executing command '%s' in %s"
975
+ % (e, contents, build_file)
976
+ )
977
+
978
+ p_stdout, p_stderr = p.communicate("")
979
+ p_stdout = p_stdout.decode("utf-8")
980
+ p_stderr = p_stderr.decode("utf-8")
981
+
982
+ if p.wait() != 0 or p_stderr:
983
+ sys.stderr.write(p_stderr)
984
+ # Simulate check_call behavior, since check_call only exists
985
+ # in python 2.5 and later.
986
+ raise GypError(
987
+ "Call to '%s' returned exit status %d while in %s."
988
+ % (contents, p.returncode, build_file)
989
+ )
990
+ replacement = p_stdout.rstrip()
991
+
992
+ cached_command_results[cache_key] = replacement
993
+ else:
994
+ gyp.DebugOutput(
995
+ gyp.DEBUG_VARIABLES,
996
+ "Had cache value for command '%s' in directory '%s'",
997
+ contents,
998
+ build_file_dir,
999
+ )
1000
+ replacement = cached_value
1001
+
1002
+ else:
1003
+ if contents not in variables:
1004
+ if contents[-1] in ["!", "/"]:
1005
+ # In order to allow cross-compiles (nacl) to happen more naturally,
1006
+ # we will allow references to >(sources/) etc. to resolve to
1007
+ # and empty list if undefined. This allows actions to:
1008
+ # 'action!': [
1009
+ # '>@(_sources!)',
1010
+ # ],
1011
+ # 'action/': [
1012
+ # '>@(_sources/)',
1013
+ # ],
1014
+ replacement = []
1015
+ else:
1016
+ raise GypError(
1017
+ "Undefined variable " + contents + " in " + build_file
1018
+ )
1019
+ else:
1020
+ replacement = variables[contents]
1021
+
1022
+ if isinstance(replacement, bytes) and not isinstance(replacement, str):
1023
+ replacement = replacement.decode("utf-8") # done on Python 3 only
1024
+ if type(replacement) is list:
1025
+ for item in replacement:
1026
+ if isinstance(item, bytes) and not isinstance(item, str):
1027
+ item = item.decode("utf-8") # done on Python 3 only
1028
+ if not contents[-1] == "/" and type(item) not in (str, int):
1029
+ raise GypError(
1030
+ "Variable "
1031
+ + contents
1032
+ + " must expand to a string or list of strings; "
1033
+ + "list contains a "
1034
+ + item.__class__.__name__
1035
+ )
1036
+ # Run through the list and handle variable expansions in it. Since
1037
+ # the list is guaranteed not to contain dicts, this won't do anything
1038
+ # with conditions sections.
1039
+ ProcessVariablesAndConditionsInList(
1040
+ replacement, phase, variables, build_file
1041
+ )
1042
+ elif type(replacement) not in (str, int):
1043
+ raise GypError(
1044
+ "Variable "
1045
+ + contents
1046
+ + " must expand to a string or list of strings; "
1047
+ + "found a "
1048
+ + replacement.__class__.__name__
1049
+ )
1050
+
1051
+ if expand_to_list:
1052
+ # Expanding in list context. It's guaranteed that there's only one
1053
+ # replacement to do in |input_str| and that it's this replacement. See
1054
+ # above.
1055
+ if type(replacement) is list:
1056
+ # If it's already a list, make a copy.
1057
+ output = replacement[:]
1058
+ else:
1059
+ # Split it the same way sh would split arguments.
1060
+ output = shlex.split(str(replacement))
1061
+ else:
1062
+ # Expanding in string context.
1063
+ encoded_replacement = ""
1064
+ if type(replacement) is list:
1065
+ # When expanding a list into string context, turn the list items
1066
+ # into a string in a way that will work with a subprocess call.
1067
+ #
1068
+ # TODO(mark): This isn't completely correct. This should
1069
+ # call a generator-provided function that observes the
1070
+ # proper list-to-argument quoting rules on a specific
1071
+ # platform instead of just calling the POSIX encoding
1072
+ # routine.
1073
+ encoded_replacement = gyp.common.EncodePOSIXShellList(replacement)
1074
+ else:
1075
+ encoded_replacement = replacement
1076
+
1077
+ output = (
1078
+ output[:replace_start] + str(encoded_replacement) + output[replace_end:]
1079
+ )
1080
+ # Prepare for the next match iteration.
1081
+ input_str = output
1082
+
1083
+ if output == input:
1084
+ gyp.DebugOutput(
1085
+ gyp.DEBUG_VARIABLES,
1086
+ "Found only identity matches on %r, avoiding infinite " "recursion.",
1087
+ output,
1088
+ )
1089
+ else:
1090
+ # Look for more matches now that we've replaced some, to deal with
1091
+ # expanding local variables (variables defined in the same
1092
+ # variables block as this one).
1093
+ gyp.DebugOutput(gyp.DEBUG_VARIABLES, "Found output %r, recursing.", output)
1094
+ if type(output) is list:
1095
+ if output and type(output[0]) is list:
1096
+ # Leave output alone if it's a list of lists.
1097
+ # We don't want such lists to be stringified.
1098
+ pass
1099
+ else:
1100
+ new_output = []
1101
+ for item in output:
1102
+ new_output.append(
1103
+ ExpandVariables(item, phase, variables, build_file)
1104
+ )
1105
+ output = new_output
1106
+ else:
1107
+ output = ExpandVariables(output, phase, variables, build_file)
1108
+
1109
+ # Convert all strings that are canonically-represented integers into integers.
1110
+ if type(output) is list:
1111
+ for index, outstr in enumerate(output):
1112
+ if IsStrCanonicalInt(outstr):
1113
+ output[index] = int(outstr)
1114
+ elif IsStrCanonicalInt(output):
1115
+ output = int(output)
1116
+
1117
+ return output
1118
+
1119
+
1120
+ # The same condition is often evaluated over and over again so it
1121
+ # makes sense to cache as much as possible between evaluations.
1122
+ cached_conditions_asts = {}
1123
+
1124
+
1125
+ def EvalCondition(condition, conditions_key, phase, variables, build_file):
1126
+ """Returns the dict that should be used or None if the result was
1127
+ that nothing should be used."""
1128
+ if type(condition) is not list:
1129
+ raise GypError(conditions_key + " must be a list")
1130
+ if len(condition) < 2:
1131
+ # It's possible that condition[0] won't work in which case this
1132
+ # attempt will raise its own IndexError. That's probably fine.
1133
+ raise GypError(
1134
+ conditions_key
1135
+ + " "
1136
+ + condition[0]
1137
+ + " must be at least length 2, not "
1138
+ + str(len(condition))
1139
+ )
1140
+
1141
+ i = 0
1142
+ result = None
1143
+ while i < len(condition):
1144
+ cond_expr = condition[i]
1145
+ true_dict = condition[i + 1]
1146
+ if type(true_dict) is not dict:
1147
+ raise GypError(
1148
+ "{} {} must be followed by a dictionary, not {}".format(
1149
+ conditions_key, cond_expr, type(true_dict)
1150
+ )
1151
+ )
1152
+ if len(condition) > i + 2 and type(condition[i + 2]) is dict:
1153
+ false_dict = condition[i + 2]
1154
+ i = i + 3
1155
+ if i != len(condition):
1156
+ raise GypError(
1157
+ "{} {} has {} unexpected trailing items".format(
1158
+ conditions_key, cond_expr, len(condition) - i
1159
+ )
1160
+ )
1161
+ else:
1162
+ false_dict = None
1163
+ i = i + 2
1164
+ if result is None:
1165
+ result = EvalSingleCondition(
1166
+ cond_expr, true_dict, false_dict, phase, variables, build_file
1167
+ )
1168
+
1169
+ return result
1170
+
1171
+
1172
+ def EvalSingleCondition(cond_expr, true_dict, false_dict, phase, variables, build_file):
1173
+ """Returns true_dict if cond_expr evaluates to true, and false_dict
1174
+ otherwise."""
1175
+ # Do expansions on the condition itself. Since the condition can naturally
1176
+ # contain variable references without needing to resort to GYP expansion
1177
+ # syntax, this is of dubious value for variables, but someone might want to
1178
+ # use a command expansion directly inside a condition.
1179
+ cond_expr_expanded = ExpandVariables(cond_expr, phase, variables, build_file)
1180
+ if type(cond_expr_expanded) not in (str, int):
1181
+ raise ValueError(
1182
+ "Variable expansion in this context permits str and int "
1183
+ + "only, found "
1184
+ + cond_expr_expanded.__class__.__name__
1185
+ )
1186
+
1187
+ try:
1188
+ if cond_expr_expanded in cached_conditions_asts:
1189
+ ast_code = cached_conditions_asts[cond_expr_expanded]
1190
+ else:
1191
+ ast_code = compile(cond_expr_expanded, "<string>", "eval")
1192
+ cached_conditions_asts[cond_expr_expanded] = ast_code
1193
+ env = {"__builtins__": {}, "v": StrictVersion}
1194
+ if eval(ast_code, env, variables):
1195
+ return true_dict
1196
+ return false_dict
1197
+ except SyntaxError as e:
1198
+ syntax_error = SyntaxError(
1199
+ "%s while evaluating condition '%s' in %s "
1200
+ "at character %d." % (str(e.args[0]), e.text, build_file, e.offset),
1201
+ e.filename,
1202
+ e.lineno,
1203
+ e.offset,
1204
+ e.text,
1205
+ )
1206
+ raise syntax_error
1207
+ except NameError as e:
1208
+ gyp.common.ExceptionAppend(
1209
+ e,
1210
+ f"while evaluating condition '{cond_expr_expanded}' in {build_file}",
1211
+ )
1212
+ raise GypError(e)
1213
+
1214
+
1215
+ def ProcessConditionsInDict(the_dict, phase, variables, build_file):
1216
+ # Process a 'conditions' or 'target_conditions' section in the_dict,
1217
+ # depending on phase.
1218
+ # early -> conditions
1219
+ # late -> target_conditions
1220
+ # latelate -> no conditions
1221
+ #
1222
+ # Each item in a conditions list consists of cond_expr, a string expression
1223
+ # evaluated as the condition, and true_dict, a dict that will be merged into
1224
+ # the_dict if cond_expr evaluates to true. Optionally, a third item,
1225
+ # false_dict, may be present. false_dict is merged into the_dict if
1226
+ # cond_expr evaluates to false.
1227
+ #
1228
+ # Any dict merged into the_dict will be recursively processed for nested
1229
+ # conditionals and other expansions, also according to phase, immediately
1230
+ # prior to being merged.
1231
+
1232
+ if phase == PHASE_EARLY:
1233
+ conditions_key = "conditions"
1234
+ elif phase == PHASE_LATE:
1235
+ conditions_key = "target_conditions"
1236
+ elif phase == PHASE_LATELATE:
1237
+ return
1238
+ else:
1239
+ assert False
1240
+
1241
+ if conditions_key not in the_dict:
1242
+ return
1243
+
1244
+ conditions_list = the_dict[conditions_key]
1245
+ # Unhook the conditions list, it's no longer needed.
1246
+ del the_dict[conditions_key]
1247
+
1248
+ for condition in conditions_list:
1249
+ merge_dict = EvalCondition(
1250
+ condition, conditions_key, phase, variables, build_file
1251
+ )
1252
+
1253
+ if merge_dict is not None:
1254
+ # Expand variables and nested conditinals in the merge_dict before
1255
+ # merging it.
1256
+ ProcessVariablesAndConditionsInDict(
1257
+ merge_dict, phase, variables, build_file
1258
+ )
1259
+
1260
+ MergeDicts(the_dict, merge_dict, build_file, build_file)
1261
+
1262
+
1263
+ def LoadAutomaticVariablesFromDict(variables, the_dict):
1264
+ # Any keys with plain string values in the_dict become automatic variables.
1265
+ # The variable name is the key name with a "_" character prepended.
1266
+ for key, value in the_dict.items():
1267
+ if type(value) in (str, int, list):
1268
+ variables["_" + key] = value
1269
+
1270
+
1271
+ def LoadVariablesFromVariablesDict(variables, the_dict, the_dict_key):
1272
+ # Any keys in the_dict's "variables" dict, if it has one, becomes a
1273
+ # variable. The variable name is the key name in the "variables" dict.
1274
+ # Variables that end with the % character are set only if they are unset in
1275
+ # the variables dict. the_dict_key is the name of the key that accesses
1276
+ # the_dict in the_dict's parent dict. If the_dict's parent is not a dict
1277
+ # (it could be a list or it could be parentless because it is a root dict),
1278
+ # the_dict_key will be None.
1279
+ for key, value in the_dict.get("variables", {}).items():
1280
+ if type(value) not in (str, int, list):
1281
+ continue
1282
+
1283
+ if key.endswith("%"):
1284
+ variable_name = key[:-1]
1285
+ if variable_name in variables:
1286
+ # If the variable is already set, don't set it.
1287
+ continue
1288
+ if the_dict_key == "variables" and variable_name in the_dict:
1289
+ # If the variable is set without a % in the_dict, and the_dict is a
1290
+ # variables dict (making |variables| a variables sub-dict of a
1291
+ # variables dict), use the_dict's definition.
1292
+ value = the_dict[variable_name]
1293
+ else:
1294
+ variable_name = key
1295
+
1296
+ variables[variable_name] = value
1297
+
1298
+
1299
+ def ProcessVariablesAndConditionsInDict(
1300
+ the_dict, phase, variables_in, build_file, the_dict_key=None
1301
+ ):
1302
+ """Handle all variable and command expansion and conditional evaluation.
1303
+
1304
+ This function is the public entry point for all variable expansions and
1305
+ conditional evaluations. The variables_in dictionary will not be modified
1306
+ by this function.
1307
+ """
1308
+
1309
+ # Make a copy of the variables_in dict that can be modified during the
1310
+ # loading of automatics and the loading of the variables dict.
1311
+ variables = variables_in.copy()
1312
+ LoadAutomaticVariablesFromDict(variables, the_dict)
1313
+
1314
+ if "variables" in the_dict:
1315
+ # Make sure all the local variables are added to the variables
1316
+ # list before we process them so that you can reference one
1317
+ # variable from another. They will be fully expanded by recursion
1318
+ # in ExpandVariables.
1319
+ for key, value in the_dict["variables"].items():
1320
+ variables[key] = value
1321
+
1322
+ # Handle the associated variables dict first, so that any variable
1323
+ # references within can be resolved prior to using them as variables.
1324
+ # Pass a copy of the variables dict to avoid having it be tainted.
1325
+ # Otherwise, it would have extra automatics added for everything that
1326
+ # should just be an ordinary variable in this scope.
1327
+ ProcessVariablesAndConditionsInDict(
1328
+ the_dict["variables"], phase, variables, build_file, "variables"
1329
+ )
1330
+
1331
+ LoadVariablesFromVariablesDict(variables, the_dict, the_dict_key)
1332
+
1333
+ for key, value in the_dict.items():
1334
+ # Skip "variables", which was already processed if present.
1335
+ if key != "variables" and type(value) is str:
1336
+ expanded = ExpandVariables(value, phase, variables, build_file)
1337
+ if type(expanded) not in (str, int):
1338
+ raise ValueError(
1339
+ "Variable expansion in this context permits str and int "
1340
+ + "only, found "
1341
+ + expanded.__class__.__name__
1342
+ + " for "
1343
+ + key
1344
+ )
1345
+ the_dict[key] = expanded
1346
+
1347
+ # Variable expansion may have resulted in changes to automatics. Reload.
1348
+ # TODO(mark): Optimization: only reload if no changes were made.
1349
+ variables = variables_in.copy()
1350
+ LoadAutomaticVariablesFromDict(variables, the_dict)
1351
+ LoadVariablesFromVariablesDict(variables, the_dict, the_dict_key)
1352
+
1353
+ # Process conditions in this dict. This is done after variable expansion
1354
+ # so that conditions may take advantage of expanded variables. For example,
1355
+ # if the_dict contains:
1356
+ # {'type': '<(library_type)',
1357
+ # 'conditions': [['_type=="static_library"', { ... }]]},
1358
+ # _type, as used in the condition, will only be set to the value of
1359
+ # library_type if variable expansion is performed before condition
1360
+ # processing. However, condition processing should occur prior to recursion
1361
+ # so that variables (both automatic and "variables" dict type) may be
1362
+ # adjusted by conditions sections, merged into the_dict, and have the
1363
+ # intended impact on contained dicts.
1364
+ #
1365
+ # This arrangement means that a "conditions" section containing a "variables"
1366
+ # section will only have those variables effective in subdicts, not in
1367
+ # the_dict. The workaround is to put a "conditions" section within a
1368
+ # "variables" section. For example:
1369
+ # {'conditions': [['os=="mac"', {'variables': {'define': 'IS_MAC'}}]],
1370
+ # 'defines': ['<(define)'],
1371
+ # 'my_subdict': {'defines': ['<(define)']}},
1372
+ # will not result in "IS_MAC" being appended to the "defines" list in the
1373
+ # current scope but would result in it being appended to the "defines" list
1374
+ # within "my_subdict". By comparison:
1375
+ # {'variables': {'conditions': [['os=="mac"', {'define': 'IS_MAC'}]]},
1376
+ # 'defines': ['<(define)'],
1377
+ # 'my_subdict': {'defines': ['<(define)']}},
1378
+ # will append "IS_MAC" to both "defines" lists.
1379
+
1380
+ # Evaluate conditions sections, allowing variable expansions within them
1381
+ # as well as nested conditionals. This will process a 'conditions' or
1382
+ # 'target_conditions' section, perform appropriate merging and recursive
1383
+ # conditional and variable processing, and then remove the conditions section
1384
+ # from the_dict if it is present.
1385
+ ProcessConditionsInDict(the_dict, phase, variables, build_file)
1386
+
1387
+ # Conditional processing may have resulted in changes to automatics or the
1388
+ # variables dict. Reload.
1389
+ variables = variables_in.copy()
1390
+ LoadAutomaticVariablesFromDict(variables, the_dict)
1391
+ LoadVariablesFromVariablesDict(variables, the_dict, the_dict_key)
1392
+
1393
+ # Recurse into child dicts, or process child lists which may result in
1394
+ # further recursion into descendant dicts.
1395
+ for key, value in the_dict.items():
1396
+ # Skip "variables" and string values, which were already processed if
1397
+ # present.
1398
+ if key == "variables" or type(value) is str:
1399
+ continue
1400
+ if type(value) is dict:
1401
+ # Pass a copy of the variables dict so that subdicts can't influence
1402
+ # parents.
1403
+ ProcessVariablesAndConditionsInDict(
1404
+ value, phase, variables, build_file, key
1405
+ )
1406
+ elif type(value) is list:
1407
+ # The list itself can't influence the variables dict, and
1408
+ # ProcessVariablesAndConditionsInList will make copies of the variables
1409
+ # dict if it needs to pass it to something that can influence it. No
1410
+ # copy is necessary here.
1411
+ ProcessVariablesAndConditionsInList(value, phase, variables, build_file)
1412
+ elif type(value) is not int:
1413
+ raise TypeError("Unknown type " + value.__class__.__name__ + " for " + key)
1414
+
1415
+
1416
+ def ProcessVariablesAndConditionsInList(the_list, phase, variables, build_file):
1417
+ # Iterate using an index so that new values can be assigned into the_list.
1418
+ index = 0
1419
+ while index < len(the_list):
1420
+ item = the_list[index]
1421
+ if type(item) is dict:
1422
+ # Make a copy of the variables dict so that it won't influence anything
1423
+ # outside of its own scope.
1424
+ ProcessVariablesAndConditionsInDict(item, phase, variables, build_file)
1425
+ elif type(item) is list:
1426
+ ProcessVariablesAndConditionsInList(item, phase, variables, build_file)
1427
+ elif type(item) is str:
1428
+ expanded = ExpandVariables(item, phase, variables, build_file)
1429
+ if type(expanded) in (str, int):
1430
+ the_list[index] = expanded
1431
+ elif type(expanded) is list:
1432
+ the_list[index : index + 1] = expanded
1433
+ index += len(expanded)
1434
+
1435
+ # index now identifies the next item to examine. Continue right now
1436
+ # without falling into the index increment below.
1437
+ continue
1438
+ else:
1439
+ raise ValueError(
1440
+ "Variable expansion in this context permits strings and "
1441
+ + "lists only, found "
1442
+ + expanded.__class__.__name__
1443
+ + " at "
1444
+ + index
1445
+ )
1446
+ elif type(item) is not int:
1447
+ raise TypeError(
1448
+ "Unknown type " + item.__class__.__name__ + " at index " + index
1449
+ )
1450
+ index = index + 1
1451
+
1452
+
1453
+ def BuildTargetsDict(data):
1454
+ """Builds a dict mapping fully-qualified target names to their target dicts.
1455
+
1456
+ |data| is a dict mapping loaded build files by pathname relative to the
1457
+ current directory. Values in |data| are build file contents. For each
1458
+ |data| value with a "targets" key, the value of the "targets" key is taken
1459
+ as a list containing target dicts. Each target's fully-qualified name is
1460
+ constructed from the pathname of the build file (|data| key) and its
1461
+ "target_name" property. These fully-qualified names are used as the keys
1462
+ in the returned dict. These keys provide access to the target dicts,
1463
+ the dicts in the "targets" lists.
1464
+ """
1465
+
1466
+ targets = {}
1467
+ for build_file in data["target_build_files"]:
1468
+ for target in data[build_file].get("targets", []):
1469
+ target_name = gyp.common.QualifiedTarget(
1470
+ build_file, target["target_name"], target["toolset"]
1471
+ )
1472
+ if target_name in targets:
1473
+ raise GypError("Duplicate target definitions for " + target_name)
1474
+ targets[target_name] = target
1475
+
1476
+ return targets
1477
+
1478
+
1479
+ def QualifyDependencies(targets):
1480
+ """Make dependency links fully-qualified relative to the current directory.
1481
+
1482
+ |targets| is a dict mapping fully-qualified target names to their target
1483
+ dicts. For each target in this dict, keys known to contain dependency
1484
+ links are examined, and any dependencies referenced will be rewritten
1485
+ so that they are fully-qualified and relative to the current directory.
1486
+ All rewritten dependencies are suitable for use as keys to |targets| or a
1487
+ similar dict.
1488
+ """
1489
+
1490
+ all_dependency_sections = [
1491
+ dep + op for dep in dependency_sections for op in ("", "!", "/")
1492
+ ]
1493
+
1494
+ for target, target_dict in targets.items():
1495
+ target_build_file = gyp.common.BuildFile(target)
1496
+ toolset = target_dict["toolset"]
1497
+ for dependency_key in all_dependency_sections:
1498
+ dependencies = target_dict.get(dependency_key, [])
1499
+ for index, dep in enumerate(dependencies):
1500
+ dep_file, dep_target, dep_toolset = gyp.common.ResolveTarget(
1501
+ target_build_file, dep, toolset
1502
+ )
1503
+ if not multiple_toolsets:
1504
+ # Ignore toolset specification in the dependency if it is specified.
1505
+ dep_toolset = toolset
1506
+ dependency = gyp.common.QualifiedTarget(
1507
+ dep_file, dep_target, dep_toolset
1508
+ )
1509
+ dependencies[index] = dependency
1510
+
1511
+ # Make sure anything appearing in a list other than "dependencies" also
1512
+ # appears in the "dependencies" list.
1513
+ if (
1514
+ dependency_key != "dependencies"
1515
+ and dependency not in target_dict["dependencies"]
1516
+ ):
1517
+ raise GypError(
1518
+ "Found "
1519
+ + dependency
1520
+ + " in "
1521
+ + dependency_key
1522
+ + " of "
1523
+ + target
1524
+ + ", but not in dependencies"
1525
+ )
1526
+
1527
+
1528
+ def ExpandWildcardDependencies(targets, data):
1529
+ """Expands dependencies specified as build_file:*.
1530
+
1531
+ For each target in |targets|, examines sections containing links to other
1532
+ targets. If any such section contains a link of the form build_file:*, it
1533
+ is taken as a wildcard link, and is expanded to list each target in
1534
+ build_file. The |data| dict provides access to build file dicts.
1535
+
1536
+ Any target that does not wish to be included by wildcard can provide an
1537
+ optional "suppress_wildcard" key in its target dict. When present and
1538
+ true, a wildcard dependency link will not include such targets.
1539
+
1540
+ All dependency names, including the keys to |targets| and the values in each
1541
+ dependency list, must be qualified when this function is called.
1542
+ """
1543
+
1544
+ for target, target_dict in targets.items():
1545
+ target_build_file = gyp.common.BuildFile(target)
1546
+ for dependency_key in dependency_sections:
1547
+ dependencies = target_dict.get(dependency_key, [])
1548
+
1549
+ # Loop this way instead of "for dependency in" or "for index in range"
1550
+ # because the dependencies list will be modified within the loop body.
1551
+ index = 0
1552
+ while index < len(dependencies):
1553
+ (
1554
+ dependency_build_file,
1555
+ dependency_target,
1556
+ dependency_toolset,
1557
+ ) = gyp.common.ParseQualifiedTarget(dependencies[index])
1558
+ if dependency_target != "*" and dependency_toolset != "*":
1559
+ # Not a wildcard. Keep it moving.
1560
+ index = index + 1
1561
+ continue
1562
+
1563
+ if dependency_build_file == target_build_file:
1564
+ # It's an error for a target to depend on all other targets in
1565
+ # the same file, because a target cannot depend on itself.
1566
+ raise GypError(
1567
+ "Found wildcard in "
1568
+ + dependency_key
1569
+ + " of "
1570
+ + target
1571
+ + " referring to same build file"
1572
+ )
1573
+
1574
+ # Take the wildcard out and adjust the index so that the next
1575
+ # dependency in the list will be processed the next time through the
1576
+ # loop.
1577
+ del dependencies[index]
1578
+ index = index - 1
1579
+
1580
+ # Loop through the targets in the other build file, adding them to
1581
+ # this target's list of dependencies in place of the removed
1582
+ # wildcard.
1583
+ dependency_target_dicts = data[dependency_build_file]["targets"]
1584
+ for dependency_target_dict in dependency_target_dicts:
1585
+ if int(dependency_target_dict.get("suppress_wildcard", False)):
1586
+ continue
1587
+ dependency_target_name = dependency_target_dict["target_name"]
1588
+ if (
1589
+ dependency_target != "*"
1590
+ and dependency_target != dependency_target_name
1591
+ ):
1592
+ continue
1593
+ dependency_target_toolset = dependency_target_dict["toolset"]
1594
+ if (
1595
+ dependency_toolset != "*"
1596
+ and dependency_toolset != dependency_target_toolset
1597
+ ):
1598
+ continue
1599
+ dependency = gyp.common.QualifiedTarget(
1600
+ dependency_build_file,
1601
+ dependency_target_name,
1602
+ dependency_target_toolset,
1603
+ )
1604
+ index = index + 1
1605
+ dependencies.insert(index, dependency)
1606
+
1607
+ index = index + 1
1608
+
1609
+
1610
+ def Unify(items):
1611
+ """Removes duplicate elements from items, keeping the first element."""
1612
+ seen = {}
1613
+ return [seen.setdefault(e, e) for e in items if e not in seen]
1614
+
1615
+
1616
+ def RemoveDuplicateDependencies(targets):
1617
+ """Makes sure every dependency appears only once in all targets's dependency
1618
+ lists."""
1619
+ for target_name, target_dict in targets.items():
1620
+ for dependency_key in dependency_sections:
1621
+ dependencies = target_dict.get(dependency_key, [])
1622
+ if dependencies:
1623
+ target_dict[dependency_key] = Unify(dependencies)
1624
+
1625
+
1626
+ def Filter(items, item):
1627
+ """Removes item from items."""
1628
+ res = {}
1629
+ return [res.setdefault(e, e) for e in items if e != item]
1630
+
1631
+
1632
+ def RemoveSelfDependencies(targets):
1633
+ """Remove self dependencies from targets that have the prune_self_dependency
1634
+ variable set."""
1635
+ for target_name, target_dict in targets.items():
1636
+ for dependency_key in dependency_sections:
1637
+ dependencies = target_dict.get(dependency_key, [])
1638
+ if dependencies:
1639
+ for t in dependencies:
1640
+ if t == target_name:
1641
+ if (
1642
+ targets[t]
1643
+ .get("variables", {})
1644
+ .get("prune_self_dependency", 0)
1645
+ ):
1646
+ target_dict[dependency_key] = Filter(
1647
+ dependencies, target_name
1648
+ )
1649
+
1650
+
1651
+ def RemoveLinkDependenciesFromNoneTargets(targets):
1652
+ """Remove dependencies having the 'link_dependency' attribute from the 'none'
1653
+ targets."""
1654
+ for target_name, target_dict in targets.items():
1655
+ for dependency_key in dependency_sections:
1656
+ dependencies = target_dict.get(dependency_key, [])
1657
+ if dependencies:
1658
+ for t in dependencies:
1659
+ if target_dict.get("type", None) == "none":
1660
+ if targets[t].get("variables", {}).get("link_dependency", 0):
1661
+ target_dict[dependency_key] = Filter(
1662
+ target_dict[dependency_key], t
1663
+ )
1664
+
1665
+
1666
+ class DependencyGraphNode:
1667
+ """
1668
+
1669
+ Attributes:
1670
+ ref: A reference to an object that this DependencyGraphNode represents.
1671
+ dependencies: List of DependencyGraphNodes on which this one depends.
1672
+ dependents: List of DependencyGraphNodes that depend on this one.
1673
+ """
1674
+
1675
+ class CircularException(GypError):
1676
+ pass
1677
+
1678
+ def __init__(self, ref):
1679
+ self.ref = ref
1680
+ self.dependencies = []
1681
+ self.dependents = []
1682
+
1683
+ def __repr__(self):
1684
+ return "<DependencyGraphNode: %r>" % self.ref
1685
+
1686
+ def FlattenToList(self):
1687
+ # flat_list is the sorted list of dependencies - actually, the list items
1688
+ # are the "ref" attributes of DependencyGraphNodes. Every target will
1689
+ # appear in flat_list after all of its dependencies, and before all of its
1690
+ # dependents.
1691
+ flat_list = OrderedSet()
1692
+
1693
+ def ExtractNodeRef(node):
1694
+ """Extracts the object that the node represents from the given node."""
1695
+ return node.ref
1696
+
1697
+ # in_degree_zeros is the list of DependencyGraphNodes that have no
1698
+ # dependencies not in flat_list. Initially, it is a copy of the children
1699
+ # of this node, because when the graph was built, nodes with no
1700
+ # dependencies were made implicit dependents of the root node.
1701
+ in_degree_zeros = sorted(self.dependents[:], key=ExtractNodeRef)
1702
+
1703
+ while in_degree_zeros:
1704
+ # Nodes in in_degree_zeros have no dependencies not in flat_list, so they
1705
+ # can be appended to flat_list. Take these nodes out of in_degree_zeros
1706
+ # as work progresses, so that the next node to process from the list can
1707
+ # always be accessed at a consistent position.
1708
+ node = in_degree_zeros.pop()
1709
+ flat_list.add(node.ref)
1710
+
1711
+ # Look at dependents of the node just added to flat_list. Some of them
1712
+ # may now belong in in_degree_zeros.
1713
+ for node_dependent in sorted(node.dependents, key=ExtractNodeRef):
1714
+ is_in_degree_zero = True
1715
+ # TODO: We want to check through the
1716
+ # node_dependent.dependencies list but if it's long and we
1717
+ # always start at the beginning, then we get O(n^2) behaviour.
1718
+ for node_dependent_dependency in sorted(
1719
+ node_dependent.dependencies, key=ExtractNodeRef
1720
+ ):
1721
+ if node_dependent_dependency.ref not in flat_list:
1722
+ # The dependent one or more dependencies not in flat_list.
1723
+ # There will be more chances to add it to flat_list
1724
+ # when examining it again as a dependent of those other
1725
+ # dependencies, provided that there are no cycles.
1726
+ is_in_degree_zero = False
1727
+ break
1728
+
1729
+ if is_in_degree_zero:
1730
+ # All of the dependent's dependencies are already in flat_list. Add
1731
+ # it to in_degree_zeros where it will be processed in a future
1732
+ # iteration of the outer loop.
1733
+ in_degree_zeros += [node_dependent]
1734
+
1735
+ return list(flat_list)
1736
+
1737
+ def FindCycles(self):
1738
+ """
1739
+ Returns a list of cycles in the graph, where each cycle is its own list.
1740
+ """
1741
+ results = []
1742
+ visited = set()
1743
+
1744
+ def Visit(node, path):
1745
+ for child in node.dependents:
1746
+ if child in path:
1747
+ results.append([child] + path[: path.index(child) + 1])
1748
+ elif child not in visited:
1749
+ visited.add(child)
1750
+ Visit(child, [child] + path)
1751
+
1752
+ visited.add(self)
1753
+ Visit(self, [self])
1754
+
1755
+ return results
1756
+
1757
+ def DirectDependencies(self, dependencies=None):
1758
+ """Returns a list of just direct dependencies."""
1759
+ if dependencies is None:
1760
+ dependencies = []
1761
+
1762
+ for dependency in self.dependencies:
1763
+ # Check for None, corresponding to the root node.
1764
+ if dependency.ref and dependency.ref not in dependencies:
1765
+ dependencies.append(dependency.ref)
1766
+
1767
+ return dependencies
1768
+
1769
+ def _AddImportedDependencies(self, targets, dependencies=None):
1770
+ """Given a list of direct dependencies, adds indirect dependencies that
1771
+ other dependencies have declared to export their settings.
1772
+
1773
+ This method does not operate on self. Rather, it operates on the list
1774
+ of dependencies in the |dependencies| argument. For each dependency in
1775
+ that list, if any declares that it exports the settings of one of its
1776
+ own dependencies, those dependencies whose settings are "passed through"
1777
+ are added to the list. As new items are added to the list, they too will
1778
+ be processed, so it is possible to import settings through multiple levels
1779
+ of dependencies.
1780
+
1781
+ This method is not terribly useful on its own, it depends on being
1782
+ "primed" with a list of direct dependencies such as one provided by
1783
+ DirectDependencies. DirectAndImportedDependencies is intended to be the
1784
+ public entry point.
1785
+ """
1786
+
1787
+ if dependencies is None:
1788
+ dependencies = []
1789
+
1790
+ index = 0
1791
+ while index < len(dependencies):
1792
+ dependency = dependencies[index]
1793
+ dependency_dict = targets[dependency]
1794
+ # Add any dependencies whose settings should be imported to the list
1795
+ # if not already present. Newly-added items will be checked for
1796
+ # their own imports when the list iteration reaches them.
1797
+ # Rather than simply appending new items, insert them after the
1798
+ # dependency that exported them. This is done to more closely match
1799
+ # the depth-first method used by DeepDependencies.
1800
+ add_index = 1
1801
+ for imported_dependency in dependency_dict.get(
1802
+ "export_dependent_settings", []
1803
+ ):
1804
+ if imported_dependency not in dependencies:
1805
+ dependencies.insert(index + add_index, imported_dependency)
1806
+ add_index = add_index + 1
1807
+ index = index + 1
1808
+
1809
+ return dependencies
1810
+
1811
+ def DirectAndImportedDependencies(self, targets, dependencies=None):
1812
+ """Returns a list of a target's direct dependencies and all indirect
1813
+ dependencies that a dependency has advertised settings should be exported
1814
+ through the dependency for.
1815
+ """
1816
+
1817
+ dependencies = self.DirectDependencies(dependencies)
1818
+ return self._AddImportedDependencies(targets, dependencies)
1819
+
1820
+ def DeepDependencies(self, dependencies=None):
1821
+ """Returns an OrderedSet of all of a target's dependencies, recursively."""
1822
+ if dependencies is None:
1823
+ # Using a list to get ordered output and a set to do fast "is it
1824
+ # already added" checks.
1825
+ dependencies = OrderedSet()
1826
+
1827
+ for dependency in self.dependencies:
1828
+ # Check for None, corresponding to the root node.
1829
+ if dependency.ref is None:
1830
+ continue
1831
+ if dependency.ref not in dependencies:
1832
+ dependency.DeepDependencies(dependencies)
1833
+ dependencies.add(dependency.ref)
1834
+
1835
+ return dependencies
1836
+
1837
+ def _LinkDependenciesInternal(
1838
+ self, targets, include_shared_libraries, dependencies=None, initial=True
1839
+ ):
1840
+ """Returns an OrderedSet of dependency targets that are linked
1841
+ into this target.
1842
+
1843
+ This function has a split personality, depending on the setting of
1844
+ |initial|. Outside callers should always leave |initial| at its default
1845
+ setting.
1846
+
1847
+ When adding a target to the list of dependencies, this function will
1848
+ recurse into itself with |initial| set to False, to collect dependencies
1849
+ that are linked into the linkable target for which the list is being built.
1850
+
1851
+ If |include_shared_libraries| is False, the resulting dependencies will not
1852
+ include shared_library targets that are linked into this target.
1853
+ """
1854
+ if dependencies is None:
1855
+ # Using a list to get ordered output and a set to do fast "is it
1856
+ # already added" checks.
1857
+ dependencies = OrderedSet()
1858
+
1859
+ # Check for None, corresponding to the root node.
1860
+ if self.ref is None:
1861
+ return dependencies
1862
+
1863
+ # It's kind of sucky that |targets| has to be passed into this function,
1864
+ # but that's presently the easiest way to access the target dicts so that
1865
+ # this function can find target types.
1866
+
1867
+ if "target_name" not in targets[self.ref]:
1868
+ raise GypError("Missing 'target_name' field in target.")
1869
+
1870
+ if "type" not in targets[self.ref]:
1871
+ raise GypError(
1872
+ "Missing 'type' field in target %s" % targets[self.ref]["target_name"]
1873
+ )
1874
+
1875
+ target_type = targets[self.ref]["type"]
1876
+
1877
+ is_linkable = target_type in linkable_types
1878
+
1879
+ if initial and not is_linkable:
1880
+ # If this is the first target being examined and it's not linkable,
1881
+ # return an empty list of link dependencies, because the link
1882
+ # dependencies are intended to apply to the target itself (initial is
1883
+ # True) and this target won't be linked.
1884
+ return dependencies
1885
+
1886
+ # Don't traverse 'none' targets if explicitly excluded.
1887
+ if target_type == "none" and not targets[self.ref].get(
1888
+ "dependencies_traverse", True
1889
+ ):
1890
+ dependencies.add(self.ref)
1891
+ return dependencies
1892
+
1893
+ # Executables, mac kernel extensions, windows drivers and loadable modules
1894
+ # are already fully and finally linked. Nothing else can be a link
1895
+ # dependency of them, there can only be dependencies in the sense that a
1896
+ # dependent target might run an executable or load the loadable_module.
1897
+ if not initial and target_type in (
1898
+ "executable",
1899
+ "loadable_module",
1900
+ "mac_kernel_extension",
1901
+ "windows_driver",
1902
+ ):
1903
+ return dependencies
1904
+
1905
+ # Shared libraries are already fully linked. They should only be included
1906
+ # in |dependencies| when adjusting static library dependencies (in order to
1907
+ # link against the shared_library's import lib), but should not be included
1908
+ # in |dependencies| when propagating link_settings.
1909
+ # The |include_shared_libraries| flag controls which of these two cases we
1910
+ # are handling.
1911
+ if (
1912
+ not initial
1913
+ and target_type == "shared_library"
1914
+ and not include_shared_libraries
1915
+ ):
1916
+ return dependencies
1917
+
1918
+ # The target is linkable, add it to the list of link dependencies.
1919
+ if self.ref not in dependencies:
1920
+ dependencies.add(self.ref)
1921
+ if initial or not is_linkable:
1922
+ # If this is a subsequent target and it's linkable, don't look any
1923
+ # further for linkable dependencies, as they'll already be linked into
1924
+ # this target linkable. Always look at dependencies of the initial
1925
+ # target, and always look at dependencies of non-linkables.
1926
+ for dependency in self.dependencies:
1927
+ dependency._LinkDependenciesInternal(
1928
+ targets, include_shared_libraries, dependencies, False
1929
+ )
1930
+
1931
+ return dependencies
1932
+
1933
+ def DependenciesForLinkSettings(self, targets):
1934
+ """
1935
+ Returns a list of dependency targets whose link_settings should be merged
1936
+ into this target.
1937
+ """
1938
+
1939
+ # TODO(sbaig) Currently, chrome depends on the bug that shared libraries'
1940
+ # link_settings are propagated. So for now, we will allow it, unless the
1941
+ # 'allow_sharedlib_linksettings_propagation' flag is explicitly set to
1942
+ # False. Once chrome is fixed, we can remove this flag.
1943
+ include_shared_libraries = targets[self.ref].get(
1944
+ "allow_sharedlib_linksettings_propagation", True
1945
+ )
1946
+ return self._LinkDependenciesInternal(targets, include_shared_libraries)
1947
+
1948
+ def DependenciesToLinkAgainst(self, targets):
1949
+ """
1950
+ Returns a list of dependency targets that are linked into this target.
1951
+ """
1952
+ return self._LinkDependenciesInternal(targets, True)
1953
+
1954
+
1955
+ def BuildDependencyList(targets):
1956
+ # Create a DependencyGraphNode for each target. Put it into a dict for easy
1957
+ # access.
1958
+ dependency_nodes = {}
1959
+ for target, spec in targets.items():
1960
+ if target not in dependency_nodes:
1961
+ dependency_nodes[target] = DependencyGraphNode(target)
1962
+
1963
+ # Set up the dependency links. Targets that have no dependencies are treated
1964
+ # as dependent on root_node.
1965
+ root_node = DependencyGraphNode(None)
1966
+ for target, spec in targets.items():
1967
+ target_node = dependency_nodes[target]
1968
+ dependencies = spec.get("dependencies")
1969
+ if not dependencies:
1970
+ target_node.dependencies = [root_node]
1971
+ root_node.dependents.append(target_node)
1972
+ else:
1973
+ for dependency in dependencies:
1974
+ dependency_node = dependency_nodes.get(dependency)
1975
+ if not dependency_node:
1976
+ raise GypError(
1977
+ "Dependency '%s' not found while "
1978
+ "trying to load target %s" % (dependency, target)
1979
+ )
1980
+ target_node.dependencies.append(dependency_node)
1981
+ dependency_node.dependents.append(target_node)
1982
+
1983
+ flat_list = root_node.FlattenToList()
1984
+
1985
+ # If there's anything left unvisited, there must be a circular dependency
1986
+ # (cycle).
1987
+ if len(flat_list) != len(targets):
1988
+ if not root_node.dependents:
1989
+ # If all targets have dependencies, add the first target as a dependent
1990
+ # of root_node so that the cycle can be discovered from root_node.
1991
+ target = next(iter(targets))
1992
+ target_node = dependency_nodes[target]
1993
+ target_node.dependencies.append(root_node)
1994
+ root_node.dependents.append(target_node)
1995
+
1996
+ cycles = []
1997
+ for cycle in root_node.FindCycles():
1998
+ paths = [node.ref for node in cycle]
1999
+ cycles.append("Cycle: %s" % " -> ".join(paths))
2000
+ raise DependencyGraphNode.CircularException(
2001
+ "Cycles in dependency graph detected:\n" + "\n".join(cycles)
2002
+ )
2003
+
2004
+ return [dependency_nodes, flat_list]
2005
+
2006
+
2007
+ def VerifyNoGYPFileCircularDependencies(targets):
2008
+ # Create a DependencyGraphNode for each gyp file containing a target. Put
2009
+ # it into a dict for easy access.
2010
+ dependency_nodes = {}
2011
+ for target in targets:
2012
+ build_file = gyp.common.BuildFile(target)
2013
+ if build_file not in dependency_nodes:
2014
+ dependency_nodes[build_file] = DependencyGraphNode(build_file)
2015
+
2016
+ # Set up the dependency links.
2017
+ for target, spec in targets.items():
2018
+ build_file = gyp.common.BuildFile(target)
2019
+ build_file_node = dependency_nodes[build_file]
2020
+ target_dependencies = spec.get("dependencies", [])
2021
+ for dependency in target_dependencies:
2022
+ try:
2023
+ dependency_build_file = gyp.common.BuildFile(dependency)
2024
+ except GypError as e:
2025
+ gyp.common.ExceptionAppend(
2026
+ e, "while computing dependencies of .gyp file %s" % build_file
2027
+ )
2028
+ raise
2029
+
2030
+ if dependency_build_file == build_file:
2031
+ # A .gyp file is allowed to refer back to itself.
2032
+ continue
2033
+ dependency_node = dependency_nodes.get(dependency_build_file)
2034
+ if not dependency_node:
2035
+ raise GypError("Dependency '%s' not found" % dependency_build_file)
2036
+ if dependency_node not in build_file_node.dependencies:
2037
+ build_file_node.dependencies.append(dependency_node)
2038
+ dependency_node.dependents.append(build_file_node)
2039
+
2040
+ # Files that have no dependencies are treated as dependent on root_node.
2041
+ root_node = DependencyGraphNode(None)
2042
+ for build_file_node in dependency_nodes.values():
2043
+ if len(build_file_node.dependencies) == 0:
2044
+ build_file_node.dependencies.append(root_node)
2045
+ root_node.dependents.append(build_file_node)
2046
+
2047
+ flat_list = root_node.FlattenToList()
2048
+
2049
+ # If there's anything left unvisited, there must be a circular dependency
2050
+ # (cycle).
2051
+ if len(flat_list) != len(dependency_nodes):
2052
+ if not root_node.dependents:
2053
+ # If all files have dependencies, add the first file as a dependent
2054
+ # of root_node so that the cycle can be discovered from root_node.
2055
+ file_node = next(iter(dependency_nodes.values()))
2056
+ file_node.dependencies.append(root_node)
2057
+ root_node.dependents.append(file_node)
2058
+ cycles = []
2059
+ for cycle in root_node.FindCycles():
2060
+ paths = [node.ref for node in cycle]
2061
+ cycles.append("Cycle: %s" % " -> ".join(paths))
2062
+ raise DependencyGraphNode.CircularException(
2063
+ "Cycles in .gyp file dependency graph detected:\n" + "\n".join(cycles)
2064
+ )
2065
+
2066
+
2067
+ def DoDependentSettings(key, flat_list, targets, dependency_nodes):
2068
+ # key should be one of all_dependent_settings, direct_dependent_settings,
2069
+ # or link_settings.
2070
+
2071
+ for target in flat_list:
2072
+ target_dict = targets[target]
2073
+ build_file = gyp.common.BuildFile(target)
2074
+
2075
+ if key == "all_dependent_settings":
2076
+ dependencies = dependency_nodes[target].DeepDependencies()
2077
+ elif key == "direct_dependent_settings":
2078
+ dependencies = dependency_nodes[target].DirectAndImportedDependencies(
2079
+ targets
2080
+ )
2081
+ elif key == "link_settings":
2082
+ dependencies = dependency_nodes[target].DependenciesForLinkSettings(targets)
2083
+ else:
2084
+ raise GypError(
2085
+ "DoDependentSettings doesn't know how to determine "
2086
+ "dependencies for " + key
2087
+ )
2088
+
2089
+ for dependency in dependencies:
2090
+ dependency_dict = targets[dependency]
2091
+ if key not in dependency_dict:
2092
+ continue
2093
+ dependency_build_file = gyp.common.BuildFile(dependency)
2094
+ MergeDicts(
2095
+ target_dict, dependency_dict[key], build_file, dependency_build_file
2096
+ )
2097
+
2098
+
2099
+ def AdjustStaticLibraryDependencies(
2100
+ flat_list, targets, dependency_nodes, sort_dependencies
2101
+ ):
2102
+ # Recompute target "dependencies" properties. For each static library
2103
+ # target, remove "dependencies" entries referring to other static libraries,
2104
+ # unless the dependency has the "hard_dependency" attribute set. For each
2105
+ # linkable target, add a "dependencies" entry referring to all of the
2106
+ # target's computed list of link dependencies (including static libraries
2107
+ # if no such entry is already present.
2108
+ for target in flat_list:
2109
+ target_dict = targets[target]
2110
+ target_type = target_dict["type"]
2111
+
2112
+ if target_type == "static_library":
2113
+ if "dependencies" not in target_dict:
2114
+ continue
2115
+
2116
+ target_dict["dependencies_original"] = target_dict.get("dependencies", [])[
2117
+ :
2118
+ ]
2119
+
2120
+ # A static library should not depend on another static library unless
2121
+ # the dependency relationship is "hard," which should only be done when
2122
+ # a dependent relies on some side effect other than just the build
2123
+ # product, like a rule or action output. Further, if a target has a
2124
+ # non-hard dependency, but that dependency exports a hard dependency,
2125
+ # the non-hard dependency can safely be removed, but the exported hard
2126
+ # dependency must be added to the target to keep the same dependency
2127
+ # ordering.
2128
+ dependencies = dependency_nodes[target].DirectAndImportedDependencies(
2129
+ targets
2130
+ )
2131
+ index = 0
2132
+ while index < len(dependencies):
2133
+ dependency = dependencies[index]
2134
+ dependency_dict = targets[dependency]
2135
+
2136
+ # Remove every non-hard static library dependency and remove every
2137
+ # non-static library dependency that isn't a direct dependency.
2138
+ if (
2139
+ dependency_dict["type"] == "static_library"
2140
+ and not dependency_dict.get("hard_dependency", False)
2141
+ ) or (
2142
+ dependency_dict["type"] != "static_library"
2143
+ and dependency not in target_dict["dependencies"]
2144
+ ):
2145
+ # Take the dependency out of the list, and don't increment index
2146
+ # because the next dependency to analyze will shift into the index
2147
+ # formerly occupied by the one being removed.
2148
+ del dependencies[index]
2149
+ else:
2150
+ index = index + 1
2151
+
2152
+ # Update the dependencies. If the dependencies list is empty, it's not
2153
+ # needed, so unhook it.
2154
+ if len(dependencies) > 0:
2155
+ target_dict["dependencies"] = dependencies
2156
+ else:
2157
+ del target_dict["dependencies"]
2158
+
2159
+ elif target_type in linkable_types:
2160
+ # Get a list of dependency targets that should be linked into this
2161
+ # target. Add them to the dependencies list if they're not already
2162
+ # present.
2163
+
2164
+ link_dependencies = dependency_nodes[target].DependenciesToLinkAgainst(
2165
+ targets
2166
+ )
2167
+ for dependency in link_dependencies:
2168
+ if dependency == target:
2169
+ continue
2170
+ if "dependencies" not in target_dict:
2171
+ target_dict["dependencies"] = []
2172
+ if dependency not in target_dict["dependencies"]:
2173
+ target_dict["dependencies"].append(dependency)
2174
+ # Sort the dependencies list in the order from dependents to dependencies.
2175
+ # e.g. If A and B depend on C and C depends on D, sort them in A, B, C, D.
2176
+ # Note: flat_list is already sorted in the order from dependencies to
2177
+ # dependents.
2178
+ if sort_dependencies and "dependencies" in target_dict:
2179
+ target_dict["dependencies"] = [
2180
+ dep
2181
+ for dep in reversed(flat_list)
2182
+ if dep in target_dict["dependencies"]
2183
+ ]
2184
+
2185
+
2186
+ # Initialize this here to speed up MakePathRelative.
2187
+ exception_re = re.compile(r"""["']?[-/$<>^]""")
2188
+
2189
+
2190
+ def MakePathRelative(to_file, fro_file, item):
2191
+ # If item is a relative path, it's relative to the build file dict that it's
2192
+ # coming from. Fix it up to make it relative to the build file dict that
2193
+ # it's going into.
2194
+ # Exception: any |item| that begins with these special characters is
2195
+ # returned without modification.
2196
+ # / Used when a path is already absolute (shortcut optimization;
2197
+ # such paths would be returned as absolute anyway)
2198
+ # $ Used for build environment variables
2199
+ # - Used for some build environment flags (such as -lapr-1 in a
2200
+ # "libraries" section)
2201
+ # < Used for our own variable and command expansions (see ExpandVariables)
2202
+ # > Used for our own variable and command expansions (see ExpandVariables)
2203
+ # ^ Used for our own variable and command expansions (see ExpandVariables)
2204
+ #
2205
+ # "/' Used when a value is quoted. If these are present, then we
2206
+ # check the second character instead.
2207
+ #
2208
+ if to_file == fro_file or exception_re.match(item):
2209
+ return item
2210
+ else:
2211
+ # TODO(dglazkov) The backslash/forward-slash replacement at the end is a
2212
+ # temporary measure. This should really be addressed by keeping all paths
2213
+ # in POSIX until actual project generation.
2214
+ ret = os.path.normpath(
2215
+ os.path.join(
2216
+ gyp.common.RelativePath(
2217
+ os.path.dirname(fro_file), os.path.dirname(to_file)
2218
+ ),
2219
+ item,
2220
+ )
2221
+ ).replace("\\", "/")
2222
+ if item.endswith("/"):
2223
+ ret += "/"
2224
+ return ret
2225
+
2226
+
2227
+ def MergeLists(to, fro, to_file, fro_file, is_paths=False, append=True):
2228
+ # Python documentation recommends objects which do not support hash
2229
+ # set this value to None. Python library objects follow this rule.
2230
+ def is_hashable(val):
2231
+ return val.__hash__
2232
+
2233
+ # If x is hashable, returns whether x is in s. Else returns whether x is in items.
2234
+ def is_in_set_or_list(x, s, items):
2235
+ if is_hashable(x):
2236
+ return x in s
2237
+ return x in items
2238
+
2239
+ prepend_index = 0
2240
+
2241
+ # Make membership testing of hashables in |to| (in particular, strings)
2242
+ # faster.
2243
+ hashable_to_set = {x for x in to if is_hashable(x)}
2244
+ for item in fro:
2245
+ singleton = False
2246
+ if type(item) in (str, int):
2247
+ # The cheap and easy case.
2248
+ if is_paths:
2249
+ to_item = MakePathRelative(to_file, fro_file, item)
2250
+ else:
2251
+ to_item = item
2252
+
2253
+ if not (type(item) is str and item.startswith("-")):
2254
+ # Any string that doesn't begin with a "-" is a singleton - it can
2255
+ # only appear once in a list, to be enforced by the list merge append
2256
+ # or prepend.
2257
+ singleton = True
2258
+ elif type(item) is dict:
2259
+ # Make a copy of the dictionary, continuing to look for paths to fix.
2260
+ # The other intelligent aspects of merge processing won't apply because
2261
+ # item is being merged into an empty dict.
2262
+ to_item = {}
2263
+ MergeDicts(to_item, item, to_file, fro_file)
2264
+ elif type(item) is list:
2265
+ # Recurse, making a copy of the list. If the list contains any
2266
+ # descendant dicts, path fixing will occur. Note that here, custom
2267
+ # values for is_paths and append are dropped; those are only to be
2268
+ # applied to |to| and |fro|, not sublists of |fro|. append shouldn't
2269
+ # matter anyway because the new |to_item| list is empty.
2270
+ to_item = []
2271
+ MergeLists(to_item, item, to_file, fro_file)
2272
+ else:
2273
+ raise TypeError(
2274
+ "Attempt to merge list item of unsupported type "
2275
+ + item.__class__.__name__
2276
+ )
2277
+
2278
+ if append:
2279
+ # If appending a singleton that's already in the list, don't append.
2280
+ # This ensures that the earliest occurrence of the item will stay put.
2281
+ if not singleton or not is_in_set_or_list(to_item, hashable_to_set, to):
2282
+ to.append(to_item)
2283
+ if is_hashable(to_item):
2284
+ hashable_to_set.add(to_item)
2285
+ else:
2286
+ # If prepending a singleton that's already in the list, remove the
2287
+ # existing instance and proceed with the prepend. This ensures that the
2288
+ # item appears at the earliest possible position in the list.
2289
+ while singleton and to_item in to:
2290
+ to.remove(to_item)
2291
+
2292
+ # Don't just insert everything at index 0. That would prepend the new
2293
+ # items to the list in reverse order, which would be an unwelcome
2294
+ # surprise.
2295
+ to.insert(prepend_index, to_item)
2296
+ if is_hashable(to_item):
2297
+ hashable_to_set.add(to_item)
2298
+ prepend_index = prepend_index + 1
2299
+
2300
+
2301
+ def MergeDicts(to, fro, to_file, fro_file):
2302
+ # I wanted to name the parameter "from" but it's a Python keyword...
2303
+ for k, v in fro.items():
2304
+ # It would be nice to do "if not k in to: to[k] = v" but that wouldn't give
2305
+ # copy semantics. Something else may want to merge from the |fro| dict
2306
+ # later, and having the same dict ref pointed to twice in the tree isn't
2307
+ # what anyone wants considering that the dicts may subsequently be
2308
+ # modified.
2309
+ if k in to:
2310
+ bad_merge = False
2311
+ if type(v) in (str, int):
2312
+ if type(to[k]) not in (str, int):
2313
+ bad_merge = True
2314
+ elif not isinstance(v, type(to[k])):
2315
+ bad_merge = True
2316
+
2317
+ if bad_merge:
2318
+ raise TypeError(
2319
+ "Attempt to merge dict value of type "
2320
+ + v.__class__.__name__
2321
+ + " into incompatible type "
2322
+ + to[k].__class__.__name__
2323
+ + " for key "
2324
+ + k
2325
+ )
2326
+ if type(v) in (str, int):
2327
+ # Overwrite the existing value, if any. Cheap and easy.
2328
+ is_path = IsPathSection(k)
2329
+ if is_path:
2330
+ to[k] = MakePathRelative(to_file, fro_file, v)
2331
+ else:
2332
+ to[k] = v
2333
+ elif type(v) is dict:
2334
+ # Recurse, guaranteeing copies will be made of objects that require it.
2335
+ if k not in to:
2336
+ to[k] = {}
2337
+ MergeDicts(to[k], v, to_file, fro_file)
2338
+ elif type(v) is list:
2339
+ # Lists in dicts can be merged with different policies, depending on
2340
+ # how the key in the "from" dict (k, the from-key) is written.
2341
+ #
2342
+ # If the from-key has ...the to-list will have this action
2343
+ # this character appended:... applied when receiving the from-list:
2344
+ # = replace
2345
+ # + prepend
2346
+ # ? set, only if to-list does not yet exist
2347
+ # (none) append
2348
+ #
2349
+ # This logic is list-specific, but since it relies on the associated
2350
+ # dict key, it's checked in this dict-oriented function.
2351
+ ext = k[-1]
2352
+ append = True
2353
+ if ext == "=":
2354
+ list_base = k[:-1]
2355
+ lists_incompatible = [list_base, list_base + "?"]
2356
+ to[list_base] = []
2357
+ elif ext == "+":
2358
+ list_base = k[:-1]
2359
+ lists_incompatible = [list_base + "=", list_base + "?"]
2360
+ append = False
2361
+ elif ext == "?":
2362
+ list_base = k[:-1]
2363
+ lists_incompatible = [list_base, list_base + "=", list_base + "+"]
2364
+ else:
2365
+ list_base = k
2366
+ lists_incompatible = [list_base + "=", list_base + "?"]
2367
+
2368
+ # Some combinations of merge policies appearing together are meaningless.
2369
+ # It's stupid to replace and append simultaneously, for example. Append
2370
+ # and prepend are the only policies that can coexist.
2371
+ for list_incompatible in lists_incompatible:
2372
+ if list_incompatible in fro:
2373
+ raise GypError(
2374
+ "Incompatible list policies " + k + " and " + list_incompatible
2375
+ )
2376
+
2377
+ if list_base in to:
2378
+ if ext == "?":
2379
+ # If the key ends in "?", the list will only be merged if it doesn't
2380
+ # already exist.
2381
+ continue
2382
+ elif type(to[list_base]) is not list:
2383
+ # This may not have been checked above if merging in a list with an
2384
+ # extension character.
2385
+ raise TypeError(
2386
+ "Attempt to merge dict value of type "
2387
+ + v.__class__.__name__
2388
+ + " into incompatible type "
2389
+ + to[list_base].__class__.__name__
2390
+ + " for key "
2391
+ + list_base
2392
+ + "("
2393
+ + k
2394
+ + ")"
2395
+ )
2396
+ else:
2397
+ to[list_base] = []
2398
+
2399
+ # Call MergeLists, which will make copies of objects that require it.
2400
+ # MergeLists can recurse back into MergeDicts, although this will be
2401
+ # to make copies of dicts (with paths fixed), there will be no
2402
+ # subsequent dict "merging" once entering a list because lists are
2403
+ # always replaced, appended to, or prepended to.
2404
+ is_paths = IsPathSection(list_base)
2405
+ MergeLists(to[list_base], v, to_file, fro_file, is_paths, append)
2406
+ else:
2407
+ raise TypeError(
2408
+ "Attempt to merge dict value of unsupported type "
2409
+ + v.__class__.__name__
2410
+ + " for key "
2411
+ + k
2412
+ )
2413
+
2414
+
2415
+ def MergeConfigWithInheritance(
2416
+ new_configuration_dict, build_file, target_dict, configuration, visited
2417
+ ):
2418
+ # Skip if previously visited.
2419
+ if configuration in visited:
2420
+ return
2421
+
2422
+ # Look at this configuration.
2423
+ configuration_dict = target_dict["configurations"][configuration]
2424
+
2425
+ # Merge in parents.
2426
+ for parent in configuration_dict.get("inherit_from", []):
2427
+ MergeConfigWithInheritance(
2428
+ new_configuration_dict,
2429
+ build_file,
2430
+ target_dict,
2431
+ parent,
2432
+ visited + [configuration],
2433
+ )
2434
+
2435
+ # Merge it into the new config.
2436
+ MergeDicts(new_configuration_dict, configuration_dict, build_file, build_file)
2437
+
2438
+ # Drop abstract.
2439
+ if "abstract" in new_configuration_dict:
2440
+ del new_configuration_dict["abstract"]
2441
+
2442
+
2443
+ def SetUpConfigurations(target, target_dict):
2444
+ # key_suffixes is a list of key suffixes that might appear on key names.
2445
+ # These suffixes are handled in conditional evaluations (for =, +, and ?)
2446
+ # and rules/exclude processing (for ! and /). Keys with these suffixes
2447
+ # should be treated the same as keys without.
2448
+ key_suffixes = ["=", "+", "?", "!", "/"]
2449
+
2450
+ build_file = gyp.common.BuildFile(target)
2451
+
2452
+ # Provide a single configuration by default if none exists.
2453
+ # TODO(mark): Signal an error if default_configurations exists but
2454
+ # configurations does not.
2455
+ if "configurations" not in target_dict:
2456
+ target_dict["configurations"] = {"Default": {}}
2457
+ if "default_configuration" not in target_dict:
2458
+ concrete = [
2459
+ i
2460
+ for (i, config) in target_dict["configurations"].items()
2461
+ if not config.get("abstract")
2462
+ ]
2463
+ target_dict["default_configuration"] = sorted(concrete)[0]
2464
+
2465
+ merged_configurations = {}
2466
+ configs = target_dict["configurations"]
2467
+ for (configuration, old_configuration_dict) in configs.items():
2468
+ # Skip abstract configurations (saves work only).
2469
+ if old_configuration_dict.get("abstract"):
2470
+ continue
2471
+ # Configurations inherit (most) settings from the enclosing target scope.
2472
+ # Get the inheritance relationship right by making a copy of the target
2473
+ # dict.
2474
+ new_configuration_dict = {}
2475
+ for (key, target_val) in target_dict.items():
2476
+ key_ext = key[-1:]
2477
+ if key_ext in key_suffixes:
2478
+ key_base = key[:-1]
2479
+ else:
2480
+ key_base = key
2481
+ if key_base not in non_configuration_keys:
2482
+ new_configuration_dict[key] = gyp.simple_copy.deepcopy(target_val)
2483
+
2484
+ # Merge in configuration (with all its parents first).
2485
+ MergeConfigWithInheritance(
2486
+ new_configuration_dict, build_file, target_dict, configuration, []
2487
+ )
2488
+
2489
+ merged_configurations[configuration] = new_configuration_dict
2490
+
2491
+ # Put the new configurations back into the target dict as a configuration.
2492
+ for configuration in merged_configurations.keys():
2493
+ target_dict["configurations"][configuration] = merged_configurations[
2494
+ configuration
2495
+ ]
2496
+
2497
+ # Now drop all the abstract ones.
2498
+ configs = target_dict["configurations"]
2499
+ target_dict["configurations"] = {
2500
+ k: v for k, v in configs.items() if not v.get("abstract")
2501
+ }
2502
+
2503
+ # Now that all of the target's configurations have been built, go through
2504
+ # the target dict's keys and remove everything that's been moved into a
2505
+ # "configurations" section.
2506
+ delete_keys = []
2507
+ for key in target_dict:
2508
+ key_ext = key[-1:]
2509
+ if key_ext in key_suffixes:
2510
+ key_base = key[:-1]
2511
+ else:
2512
+ key_base = key
2513
+ if key_base not in non_configuration_keys:
2514
+ delete_keys.append(key)
2515
+ for key in delete_keys:
2516
+ del target_dict[key]
2517
+
2518
+ # Check the configurations to see if they contain invalid keys.
2519
+ for configuration in target_dict["configurations"].keys():
2520
+ configuration_dict = target_dict["configurations"][configuration]
2521
+ for key in configuration_dict.keys():
2522
+ if key in invalid_configuration_keys:
2523
+ raise GypError(
2524
+ "%s not allowed in the %s configuration, found in "
2525
+ "target %s" % (key, configuration, target)
2526
+ )
2527
+
2528
+
2529
+ def ProcessListFiltersInDict(name, the_dict):
2530
+ """Process regular expression and exclusion-based filters on lists.
2531
+
2532
+ An exclusion list is in a dict key named with a trailing "!", like
2533
+ "sources!". Every item in such a list is removed from the associated
2534
+ main list, which in this example, would be "sources". Removed items are
2535
+ placed into a "sources_excluded" list in the dict.
2536
+
2537
+ Regular expression (regex) filters are contained in dict keys named with a
2538
+ trailing "/", such as "sources/" to operate on the "sources" list. Regex
2539
+ filters in a dict take the form:
2540
+ 'sources/': [ ['exclude', '_(linux|mac|win)\\.cc$'],
2541
+ ['include', '_mac\\.cc$'] ],
2542
+ The first filter says to exclude all files ending in _linux.cc, _mac.cc, and
2543
+ _win.cc. The second filter then includes all files ending in _mac.cc that
2544
+ are now or were once in the "sources" list. Items matching an "exclude"
2545
+ filter are subject to the same processing as would occur if they were listed
2546
+ by name in an exclusion list (ending in "!"). Items matching an "include"
2547
+ filter are brought back into the main list if previously excluded by an
2548
+ exclusion list or exclusion regex filter. Subsequent matching "exclude"
2549
+ patterns can still cause items to be excluded after matching an "include".
2550
+ """
2551
+
2552
+ # Look through the dictionary for any lists whose keys end in "!" or "/".
2553
+ # These are lists that will be treated as exclude lists and regular
2554
+ # expression-based exclude/include lists. Collect the lists that are
2555
+ # needed first, looking for the lists that they operate on, and assemble
2556
+ # then into |lists|. This is done in a separate loop up front, because
2557
+ # the _included and _excluded keys need to be added to the_dict, and that
2558
+ # can't be done while iterating through it.
2559
+
2560
+ lists = []
2561
+ del_lists = []
2562
+ for key, value in the_dict.items():
2563
+ operation = key[-1]
2564
+ if operation != "!" and operation != "/":
2565
+ continue
2566
+
2567
+ if type(value) is not list:
2568
+ raise ValueError(
2569
+ name + " key " + key + " must be list, not " + value.__class__.__name__
2570
+ )
2571
+
2572
+ list_key = key[:-1]
2573
+ if list_key not in the_dict:
2574
+ # This happens when there's a list like "sources!" but no corresponding
2575
+ # "sources" list. Since there's nothing for it to operate on, queue up
2576
+ # the "sources!" list for deletion now.
2577
+ del_lists.append(key)
2578
+ continue
2579
+
2580
+ if type(the_dict[list_key]) is not list:
2581
+ value = the_dict[list_key]
2582
+ raise ValueError(
2583
+ name
2584
+ + " key "
2585
+ + list_key
2586
+ + " must be list, not "
2587
+ + value.__class__.__name__
2588
+ + " when applying "
2589
+ + {"!": "exclusion", "/": "regex"}[operation]
2590
+ )
2591
+
2592
+ if list_key not in lists:
2593
+ lists.append(list_key)
2594
+
2595
+ # Delete the lists that are known to be unneeded at this point.
2596
+ for del_list in del_lists:
2597
+ del the_dict[del_list]
2598
+
2599
+ for list_key in lists:
2600
+ the_list = the_dict[list_key]
2601
+
2602
+ # Initialize the list_actions list, which is parallel to the_list. Each
2603
+ # item in list_actions identifies whether the corresponding item in
2604
+ # the_list should be excluded, unconditionally preserved (included), or
2605
+ # whether no exclusion or inclusion has been applied. Items for which
2606
+ # no exclusion or inclusion has been applied (yet) have value -1, items
2607
+ # excluded have value 0, and items included have value 1. Includes and
2608
+ # excludes override previous actions. All items in list_actions are
2609
+ # initialized to -1 because no excludes or includes have been processed
2610
+ # yet.
2611
+ list_actions = list((-1,) * len(the_list))
2612
+
2613
+ exclude_key = list_key + "!"
2614
+ if exclude_key in the_dict:
2615
+ for exclude_item in the_dict[exclude_key]:
2616
+ for index, list_item in enumerate(the_list):
2617
+ if exclude_item == list_item:
2618
+ # This item matches the exclude_item, so set its action to 0
2619
+ # (exclude).
2620
+ list_actions[index] = 0
2621
+
2622
+ # The "whatever!" list is no longer needed, dump it.
2623
+ del the_dict[exclude_key]
2624
+
2625
+ regex_key = list_key + "/"
2626
+ if regex_key in the_dict:
2627
+ for regex_item in the_dict[regex_key]:
2628
+ [action, pattern] = regex_item
2629
+ pattern_re = re.compile(pattern)
2630
+
2631
+ if action == "exclude":
2632
+ # This item matches an exclude regex, set its value to 0 (exclude).
2633
+ action_value = 0
2634
+ elif action == "include":
2635
+ # This item matches an include regex, set its value to 1 (include).
2636
+ action_value = 1
2637
+ else:
2638
+ # This is an action that doesn't make any sense.
2639
+ raise ValueError(
2640
+ "Unrecognized action "
2641
+ + action
2642
+ + " in "
2643
+ + name
2644
+ + " key "
2645
+ + regex_key
2646
+ )
2647
+
2648
+ for index, list_item in enumerate(the_list):
2649
+ if list_actions[index] == action_value:
2650
+ # Even if the regex matches, nothing will change so continue
2651
+ # (regex searches are expensive).
2652
+ continue
2653
+ if pattern_re.search(list_item):
2654
+ # Regular expression match.
2655
+ list_actions[index] = action_value
2656
+
2657
+ # The "whatever/" list is no longer needed, dump it.
2658
+ del the_dict[regex_key]
2659
+
2660
+ # Add excluded items to the excluded list.
2661
+ #
2662
+ # Note that exclude_key ("sources!") is different from excluded_key
2663
+ # ("sources_excluded"). The exclude_key list is input and it was already
2664
+ # processed and deleted; the excluded_key list is output and it's about
2665
+ # to be created.
2666
+ excluded_key = list_key + "_excluded"
2667
+ if excluded_key in the_dict:
2668
+ raise GypError(
2669
+ name + " key " + excluded_key + " must not be present prior "
2670
+ " to applying exclusion/regex filters for " + list_key
2671
+ )
2672
+
2673
+ excluded_list = []
2674
+
2675
+ # Go backwards through the list_actions list so that as items are deleted,
2676
+ # the indices of items that haven't been seen yet don't shift. That means
2677
+ # that things need to be prepended to excluded_list to maintain them in the
2678
+ # same order that they existed in the_list.
2679
+ for index in range(len(list_actions) - 1, -1, -1):
2680
+ if list_actions[index] == 0:
2681
+ # Dump anything with action 0 (exclude). Keep anything with action 1
2682
+ # (include) or -1 (no include or exclude seen for the item).
2683
+ excluded_list.insert(0, the_list[index])
2684
+ del the_list[index]
2685
+
2686
+ # If anything was excluded, put the excluded list into the_dict at
2687
+ # excluded_key.
2688
+ if len(excluded_list) > 0:
2689
+ the_dict[excluded_key] = excluded_list
2690
+
2691
+ # Now recurse into subdicts and lists that may contain dicts.
2692
+ for key, value in the_dict.items():
2693
+ if type(value) is dict:
2694
+ ProcessListFiltersInDict(key, value)
2695
+ elif type(value) is list:
2696
+ ProcessListFiltersInList(key, value)
2697
+
2698
+
2699
+ def ProcessListFiltersInList(name, the_list):
2700
+ for item in the_list:
2701
+ if type(item) is dict:
2702
+ ProcessListFiltersInDict(name, item)
2703
+ elif type(item) is list:
2704
+ ProcessListFiltersInList(name, item)
2705
+
2706
+
2707
+ def ValidateTargetType(target, target_dict):
2708
+ """Ensures the 'type' field on the target is one of the known types.
2709
+
2710
+ Arguments:
2711
+ target: string, name of target.
2712
+ target_dict: dict, target spec.
2713
+
2714
+ Raises an exception on error.
2715
+ """
2716
+ VALID_TARGET_TYPES = (
2717
+ "executable",
2718
+ "loadable_module",
2719
+ "static_library",
2720
+ "shared_library",
2721
+ "mac_kernel_extension",
2722
+ "none",
2723
+ "windows_driver",
2724
+ )
2725
+ target_type = target_dict.get("type", None)
2726
+ if target_type not in VALID_TARGET_TYPES:
2727
+ raise GypError(
2728
+ "Target %s has an invalid target type '%s'. "
2729
+ "Must be one of %s." % (target, target_type, "/".join(VALID_TARGET_TYPES))
2730
+ )
2731
+ if (
2732
+ target_dict.get("standalone_static_library", 0)
2733
+ and not target_type == "static_library"
2734
+ ):
2735
+ raise GypError(
2736
+ "Target %s has type %s but standalone_static_library flag is"
2737
+ " only valid for static_library type." % (target, target_type)
2738
+ )
2739
+
2740
+
2741
+ def ValidateRulesInTarget(target, target_dict, extra_sources_for_rules):
2742
+ """Ensures that the rules sections in target_dict are valid and consistent,
2743
+ and determines which sources they apply to.
2744
+
2745
+ Arguments:
2746
+ target: string, name of target.
2747
+ target_dict: dict, target spec containing "rules" and "sources" lists.
2748
+ extra_sources_for_rules: a list of keys to scan for rule matches in
2749
+ addition to 'sources'.
2750
+ """
2751
+
2752
+ # Dicts to map between values found in rules' 'rule_name' and 'extension'
2753
+ # keys and the rule dicts themselves.
2754
+ rule_names = {}
2755
+ rule_extensions = {}
2756
+
2757
+ rules = target_dict.get("rules", [])
2758
+ for rule in rules:
2759
+ # Make sure that there's no conflict among rule names and extensions.
2760
+ rule_name = rule["rule_name"]
2761
+ if rule_name in rule_names:
2762
+ raise GypError(
2763
+ f"rule {rule_name} exists in duplicate, target {target}"
2764
+ )
2765
+ rule_names[rule_name] = rule
2766
+
2767
+ rule_extension = rule["extension"]
2768
+ if rule_extension.startswith("."):
2769
+ rule_extension = rule_extension[1:]
2770
+ if rule_extension in rule_extensions:
2771
+ raise GypError(
2772
+ (
2773
+ "extension %s associated with multiple rules, "
2774
+ + "target %s rules %s and %s"
2775
+ )
2776
+ % (
2777
+ rule_extension,
2778
+ target,
2779
+ rule_extensions[rule_extension]["rule_name"],
2780
+ rule_name,
2781
+ )
2782
+ )
2783
+ rule_extensions[rule_extension] = rule
2784
+
2785
+ # Make sure rule_sources isn't already there. It's going to be
2786
+ # created below if needed.
2787
+ if "rule_sources" in rule:
2788
+ raise GypError(
2789
+ "rule_sources must not exist in input, target %s rule %s"
2790
+ % (target, rule_name)
2791
+ )
2792
+
2793
+ rule_sources = []
2794
+ source_keys = ["sources"]
2795
+ source_keys.extend(extra_sources_for_rules)
2796
+ for source_key in source_keys:
2797
+ for source in target_dict.get(source_key, []):
2798
+ (source_root, source_extension) = os.path.splitext(source)
2799
+ if source_extension.startswith("."):
2800
+ source_extension = source_extension[1:]
2801
+ if source_extension == rule_extension:
2802
+ rule_sources.append(source)
2803
+
2804
+ if len(rule_sources) > 0:
2805
+ rule["rule_sources"] = rule_sources
2806
+
2807
+
2808
+ def ValidateRunAsInTarget(target, target_dict, build_file):
2809
+ target_name = target_dict.get("target_name")
2810
+ run_as = target_dict.get("run_as")
2811
+ if not run_as:
2812
+ return
2813
+ if type(run_as) is not dict:
2814
+ raise GypError(
2815
+ "The 'run_as' in target %s from file %s should be a "
2816
+ "dictionary." % (target_name, build_file)
2817
+ )
2818
+ action = run_as.get("action")
2819
+ if not action:
2820
+ raise GypError(
2821
+ "The 'run_as' in target %s from file %s must have an "
2822
+ "'action' section." % (target_name, build_file)
2823
+ )
2824
+ if type(action) is not list:
2825
+ raise GypError(
2826
+ "The 'action' for 'run_as' in target %s from file %s "
2827
+ "must be a list." % (target_name, build_file)
2828
+ )
2829
+ working_directory = run_as.get("working_directory")
2830
+ if working_directory and type(working_directory) is not str:
2831
+ raise GypError(
2832
+ "The 'working_directory' for 'run_as' in target %s "
2833
+ "in file %s should be a string." % (target_name, build_file)
2834
+ )
2835
+ environment = run_as.get("environment")
2836
+ if environment and type(environment) is not dict:
2837
+ raise GypError(
2838
+ "The 'environment' for 'run_as' in target %s "
2839
+ "in file %s should be a dictionary." % (target_name, build_file)
2840
+ )
2841
+
2842
+
2843
+ def ValidateActionsInTarget(target, target_dict, build_file):
2844
+ """Validates the inputs to the actions in a target."""
2845
+ target_name = target_dict.get("target_name")
2846
+ actions = target_dict.get("actions", [])
2847
+ for action in actions:
2848
+ action_name = action.get("action_name")
2849
+ if not action_name:
2850
+ raise GypError(
2851
+ "Anonymous action in target %s. "
2852
+ "An action must have an 'action_name' field." % target_name
2853
+ )
2854
+ inputs = action.get("inputs", None)
2855
+ if inputs is None:
2856
+ raise GypError("Action in target %s has no inputs." % target_name)
2857
+ action_command = action.get("action")
2858
+ if action_command and not action_command[0]:
2859
+ raise GypError("Empty action as command in target %s." % target_name)
2860
+
2861
+
2862
+ def TurnIntIntoStrInDict(the_dict):
2863
+ """Given dict the_dict, recursively converts all integers into strings.
2864
+ """
2865
+ # Use items instead of iteritems because there's no need to try to look at
2866
+ # reinserted keys and their associated values.
2867
+ for k, v in the_dict.items():
2868
+ if type(v) is int:
2869
+ v = str(v)
2870
+ the_dict[k] = v
2871
+ elif type(v) is dict:
2872
+ TurnIntIntoStrInDict(v)
2873
+ elif type(v) is list:
2874
+ TurnIntIntoStrInList(v)
2875
+
2876
+ if type(k) is int:
2877
+ del the_dict[k]
2878
+ the_dict[str(k)] = v
2879
+
2880
+
2881
+ def TurnIntIntoStrInList(the_list):
2882
+ """Given list the_list, recursively converts all integers into strings.
2883
+ """
2884
+ for index, item in enumerate(the_list):
2885
+ if type(item) is int:
2886
+ the_list[index] = str(item)
2887
+ elif type(item) is dict:
2888
+ TurnIntIntoStrInDict(item)
2889
+ elif type(item) is list:
2890
+ TurnIntIntoStrInList(item)
2891
+
2892
+
2893
+ def PruneUnwantedTargets(targets, flat_list, dependency_nodes, root_targets, data):
2894
+ """Return only the targets that are deep dependencies of |root_targets|."""
2895
+ qualified_root_targets = []
2896
+ for target in root_targets:
2897
+ target = target.strip()
2898
+ qualified_targets = gyp.common.FindQualifiedTargets(target, flat_list)
2899
+ if not qualified_targets:
2900
+ raise GypError("Could not find target %s" % target)
2901
+ qualified_root_targets.extend(qualified_targets)
2902
+
2903
+ wanted_targets = {}
2904
+ for target in qualified_root_targets:
2905
+ wanted_targets[target] = targets[target]
2906
+ for dependency in dependency_nodes[target].DeepDependencies():
2907
+ wanted_targets[dependency] = targets[dependency]
2908
+
2909
+ wanted_flat_list = [t for t in flat_list if t in wanted_targets]
2910
+
2911
+ # Prune unwanted targets from each build_file's data dict.
2912
+ for build_file in data["target_build_files"]:
2913
+ if "targets" not in data[build_file]:
2914
+ continue
2915
+ new_targets = []
2916
+ for target in data[build_file]["targets"]:
2917
+ qualified_name = gyp.common.QualifiedTarget(
2918
+ build_file, target["target_name"], target["toolset"]
2919
+ )
2920
+ if qualified_name in wanted_targets:
2921
+ new_targets.append(target)
2922
+ data[build_file]["targets"] = new_targets
2923
+
2924
+ return wanted_targets, wanted_flat_list
2925
+
2926
+
2927
+ def VerifyNoCollidingTargets(targets):
2928
+ """Verify that no two targets in the same directory share the same name.
2929
+
2930
+ Arguments:
2931
+ targets: A list of targets in the form 'path/to/file.gyp:target_name'.
2932
+ """
2933
+ # Keep a dict going from 'subdirectory:target_name' to 'foo.gyp'.
2934
+ used = {}
2935
+ for target in targets:
2936
+ # Separate out 'path/to/file.gyp, 'target_name' from
2937
+ # 'path/to/file.gyp:target_name'.
2938
+ path, name = target.rsplit(":", 1)
2939
+ # Separate out 'path/to', 'file.gyp' from 'path/to/file.gyp'.
2940
+ subdir, gyp = os.path.split(path)
2941
+ # Use '.' for the current directory '', so that the error messages make
2942
+ # more sense.
2943
+ if not subdir:
2944
+ subdir = "."
2945
+ # Prepare a key like 'path/to:target_name'.
2946
+ key = subdir + ":" + name
2947
+ if key in used:
2948
+ # Complain if this target is already used.
2949
+ raise GypError(
2950
+ 'Duplicate target name "%s" in directory "%s" used both '
2951
+ 'in "%s" and "%s".' % (name, subdir, gyp, used[key])
2952
+ )
2953
+ used[key] = gyp
2954
+
2955
+
2956
+ def SetGeneratorGlobals(generator_input_info):
2957
+ # Set up path_sections and non_configuration_keys with the default data plus
2958
+ # the generator-specific data.
2959
+ global path_sections
2960
+ path_sections = set(base_path_sections)
2961
+ path_sections.update(generator_input_info["path_sections"])
2962
+
2963
+ global non_configuration_keys
2964
+ non_configuration_keys = base_non_configuration_keys[:]
2965
+ non_configuration_keys.extend(generator_input_info["non_configuration_keys"])
2966
+
2967
+ global multiple_toolsets
2968
+ multiple_toolsets = generator_input_info["generator_supports_multiple_toolsets"]
2969
+
2970
+ global generator_filelist_paths
2971
+ generator_filelist_paths = generator_input_info["generator_filelist_paths"]
2972
+
2973
+
2974
+ def Load(
2975
+ build_files,
2976
+ variables,
2977
+ includes,
2978
+ depth,
2979
+ generator_input_info,
2980
+ check,
2981
+ circular_check,
2982
+ parallel,
2983
+ root_targets,
2984
+ ):
2985
+ SetGeneratorGlobals(generator_input_info)
2986
+ # A generator can have other lists (in addition to sources) be processed
2987
+ # for rules.
2988
+ extra_sources_for_rules = generator_input_info["extra_sources_for_rules"]
2989
+
2990
+ # Load build files. This loads every target-containing build file into
2991
+ # the |data| dictionary such that the keys to |data| are build file names,
2992
+ # and the values are the entire build file contents after "early" or "pre"
2993
+ # processing has been done and includes have been resolved.
2994
+ # NOTE: data contains both "target" files (.gyp) and "includes" (.gypi), as
2995
+ # well as meta-data (e.g. 'included_files' key). 'target_build_files' keeps
2996
+ # track of the keys corresponding to "target" files.
2997
+ data = {"target_build_files": set()}
2998
+ # Normalize paths everywhere. This is important because paths will be
2999
+ # used as keys to the data dict and for references between input files.
3000
+ build_files = set(map(os.path.normpath, build_files))
3001
+ if parallel:
3002
+ LoadTargetBuildFilesParallel(
3003
+ build_files, data, variables, includes, depth, check, generator_input_info
3004
+ )
3005
+ else:
3006
+ aux_data = {}
3007
+ for build_file in build_files:
3008
+ try:
3009
+ LoadTargetBuildFile(
3010
+ build_file, data, aux_data, variables, includes, depth, check, True
3011
+ )
3012
+ except Exception as e:
3013
+ gyp.common.ExceptionAppend(e, "while trying to load %s" % build_file)
3014
+ raise
3015
+
3016
+ # Build a dict to access each target's subdict by qualified name.
3017
+ targets = BuildTargetsDict(data)
3018
+
3019
+ # Fully qualify all dependency links.
3020
+ QualifyDependencies(targets)
3021
+
3022
+ # Remove self-dependencies from targets that have 'prune_self_dependencies'
3023
+ # set to 1.
3024
+ RemoveSelfDependencies(targets)
3025
+
3026
+ # Expand dependencies specified as build_file:*.
3027
+ ExpandWildcardDependencies(targets, data)
3028
+
3029
+ # Remove all dependencies marked as 'link_dependency' from the targets of
3030
+ # type 'none'.
3031
+ RemoveLinkDependenciesFromNoneTargets(targets)
3032
+
3033
+ # Apply exclude (!) and regex (/) list filters only for dependency_sections.
3034
+ for target_name, target_dict in targets.items():
3035
+ tmp_dict = {}
3036
+ for key_base in dependency_sections:
3037
+ for op in ("", "!", "/"):
3038
+ key = key_base + op
3039
+ if key in target_dict:
3040
+ tmp_dict[key] = target_dict[key]
3041
+ del target_dict[key]
3042
+ ProcessListFiltersInDict(target_name, tmp_dict)
3043
+ # Write the results back to |target_dict|.
3044
+ for key in tmp_dict:
3045
+ target_dict[key] = tmp_dict[key]
3046
+
3047
+ # Make sure every dependency appears at most once.
3048
+ RemoveDuplicateDependencies(targets)
3049
+
3050
+ if circular_check:
3051
+ # Make sure that any targets in a.gyp don't contain dependencies in other
3052
+ # .gyp files that further depend on a.gyp.
3053
+ VerifyNoGYPFileCircularDependencies(targets)
3054
+
3055
+ [dependency_nodes, flat_list] = BuildDependencyList(targets)
3056
+
3057
+ if root_targets:
3058
+ # Remove, from |targets| and |flat_list|, the targets that are not deep
3059
+ # dependencies of the targets specified in |root_targets|.
3060
+ targets, flat_list = PruneUnwantedTargets(
3061
+ targets, flat_list, dependency_nodes, root_targets, data
3062
+ )
3063
+
3064
+ # Check that no two targets in the same directory have the same name.
3065
+ VerifyNoCollidingTargets(flat_list)
3066
+
3067
+ # Handle dependent settings of various types.
3068
+ for settings_type in [
3069
+ "all_dependent_settings",
3070
+ "direct_dependent_settings",
3071
+ "link_settings",
3072
+ ]:
3073
+ DoDependentSettings(settings_type, flat_list, targets, dependency_nodes)
3074
+
3075
+ # Take out the dependent settings now that they've been published to all
3076
+ # of the targets that require them.
3077
+ for target in flat_list:
3078
+ if settings_type in targets[target]:
3079
+ del targets[target][settings_type]
3080
+
3081
+ # Make sure static libraries don't declare dependencies on other static
3082
+ # libraries, but that linkables depend on all unlinked static libraries
3083
+ # that they need so that their link steps will be correct.
3084
+ gii = generator_input_info
3085
+ if gii["generator_wants_static_library_dependencies_adjusted"]:
3086
+ AdjustStaticLibraryDependencies(
3087
+ flat_list,
3088
+ targets,
3089
+ dependency_nodes,
3090
+ gii["generator_wants_sorted_dependencies"],
3091
+ )
3092
+
3093
+ # Apply "post"/"late"/"target" variable expansions and condition evaluations.
3094
+ for target in flat_list:
3095
+ target_dict = targets[target]
3096
+ build_file = gyp.common.BuildFile(target)
3097
+ ProcessVariablesAndConditionsInDict(
3098
+ target_dict, PHASE_LATE, variables, build_file
3099
+ )
3100
+
3101
+ # Move everything that can go into a "configurations" section into one.
3102
+ for target in flat_list:
3103
+ target_dict = targets[target]
3104
+ SetUpConfigurations(target, target_dict)
3105
+
3106
+ # Apply exclude (!) and regex (/) list filters.
3107
+ for target in flat_list:
3108
+ target_dict = targets[target]
3109
+ ProcessListFiltersInDict(target, target_dict)
3110
+
3111
+ # Apply "latelate" variable expansions and condition evaluations.
3112
+ for target in flat_list:
3113
+ target_dict = targets[target]
3114
+ build_file = gyp.common.BuildFile(target)
3115
+ ProcessVariablesAndConditionsInDict(
3116
+ target_dict, PHASE_LATELATE, variables, build_file
3117
+ )
3118
+
3119
+ # Make sure that the rules make sense, and build up rule_sources lists as
3120
+ # needed. Not all generators will need to use the rule_sources lists, but
3121
+ # some may, and it seems best to build the list in a common spot.
3122
+ # Also validate actions and run_as elements in targets.
3123
+ for target in flat_list:
3124
+ target_dict = targets[target]
3125
+ build_file = gyp.common.BuildFile(target)
3126
+ ValidateTargetType(target, target_dict)
3127
+ ValidateRulesInTarget(target, target_dict, extra_sources_for_rules)
3128
+ ValidateRunAsInTarget(target, target_dict, build_file)
3129
+ ValidateActionsInTarget(target, target_dict, build_file)
3130
+
3131
+ # Generators might not expect ints. Turn them into strs.
3132
+ TurnIntIntoStrInDict(data)
3133
+
3134
+ # TODO(mark): Return |data| for now because the generator needs a list of
3135
+ # build files that came in. In the future, maybe it should just accept
3136
+ # a list, and not the whole data dict.
3137
+ return [flat_list, targets, data]