redsnow 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (174) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +34 -0
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +20 -0
  5. data/CHANGELOG.md +4 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE +21 -0
  8. data/README.md +62 -0
  9. data/Rakefile +36 -0
  10. data/Vagrantfile +20 -0
  11. data/ext/snowcrash/Makefile +64 -0
  12. data/ext/snowcrash/Vagrantfile +20 -0
  13. data/ext/snowcrash/bin/snowcrash +0 -0
  14. data/ext/snowcrash/common.gypi +163 -0
  15. data/ext/snowcrash/config.gypi +10 -0
  16. data/ext/snowcrash/config.mk +5 -0
  17. data/ext/snowcrash/configure +213 -0
  18. data/ext/snowcrash/provisioning.sh +15 -0
  19. data/ext/snowcrash/snowcrash.gyp +141 -0
  20. data/ext/snowcrash/src/ActionParser.h +503 -0
  21. data/ext/snowcrash/src/AssetParser.h +215 -0
  22. data/ext/snowcrash/src/BlockUtility.h +186 -0
  23. data/ext/snowcrash/src/Blueprint.h +283 -0
  24. data/ext/snowcrash/src/BlueprintParser.h +347 -0
  25. data/ext/snowcrash/src/BlueprintParserCore.h +190 -0
  26. data/ext/snowcrash/src/BlueprintSection.h +140 -0
  27. data/ext/snowcrash/src/BlueprintUtility.h +126 -0
  28. data/ext/snowcrash/src/CBlueprint.cc +600 -0
  29. data/ext/snowcrash/src/CBlueprint.h +354 -0
  30. data/ext/snowcrash/src/CSourceAnnotation.cc +140 -0
  31. data/ext/snowcrash/src/CSourceAnnotation.h +106 -0
  32. data/ext/snowcrash/src/CodeBlockUtility.h +189 -0
  33. data/ext/snowcrash/src/DescriptionSectionUtility.h +156 -0
  34. data/ext/snowcrash/src/HTTP.cc +46 -0
  35. data/ext/snowcrash/src/HTTP.h +105 -0
  36. data/ext/snowcrash/src/HeaderParser.h +289 -0
  37. data/ext/snowcrash/src/ListBlockUtility.h +273 -0
  38. data/ext/snowcrash/src/ListUtility.h +95 -0
  39. data/ext/snowcrash/src/MarkdownBlock.cc +176 -0
  40. data/ext/snowcrash/src/MarkdownBlock.h +93 -0
  41. data/ext/snowcrash/src/MarkdownParser.cc +266 -0
  42. data/ext/snowcrash/src/MarkdownParser.h +88 -0
  43. data/ext/snowcrash/src/ParameterDefinitonParser.h +570 -0
  44. data/ext/snowcrash/src/ParametersParser.h +252 -0
  45. data/ext/snowcrash/src/Parser.cc +71 -0
  46. data/ext/snowcrash/src/Parser.h +29 -0
  47. data/ext/snowcrash/src/ParserCore.cc +120 -0
  48. data/ext/snowcrash/src/ParserCore.h +82 -0
  49. data/ext/snowcrash/src/PayloadParser.h +672 -0
  50. data/ext/snowcrash/src/Platform.h +54 -0
  51. data/ext/snowcrash/src/RegexMatch.h +32 -0
  52. data/ext/snowcrash/src/ResourceGroupParser.h +195 -0
  53. data/ext/snowcrash/src/ResourceParser.h +584 -0
  54. data/ext/snowcrash/src/SectionUtility.h +142 -0
  55. data/ext/snowcrash/src/Serialize.cc +52 -0
  56. data/ext/snowcrash/src/Serialize.h +69 -0
  57. data/ext/snowcrash/src/SerializeJSON.cc +601 -0
  58. data/ext/snowcrash/src/SerializeJSON.h +21 -0
  59. data/ext/snowcrash/src/SerializeYAML.cc +336 -0
  60. data/ext/snowcrash/src/SerializeYAML.h +21 -0
  61. data/ext/snowcrash/src/SourceAnnotation.h +177 -0
  62. data/ext/snowcrash/src/StringUtility.h +109 -0
  63. data/ext/snowcrash/src/SymbolTable.h +83 -0
  64. data/ext/snowcrash/src/UriTemplateParser.cc +195 -0
  65. data/ext/snowcrash/src/UriTemplateParser.h +243 -0
  66. data/ext/snowcrash/src/Version.h +39 -0
  67. data/ext/snowcrash/src/csnowcrash.cc +23 -0
  68. data/ext/snowcrash/src/csnowcrash.h +38 -0
  69. data/ext/snowcrash/src/posix/RegexMatch.cc +99 -0
  70. data/ext/snowcrash/src/snowcrash.cc +18 -0
  71. data/ext/snowcrash/src/snowcrash.h +41 -0
  72. data/ext/snowcrash/src/snowcrash/snowcrash.cc +170 -0
  73. data/ext/snowcrash/src/win/RegexMatch.cc +78 -0
  74. data/ext/snowcrash/sundown/CONTRIBUTING.md +10 -0
  75. data/ext/snowcrash/sundown/Makefile +83 -0
  76. data/ext/snowcrash/sundown/Makefile.win +33 -0
  77. data/ext/snowcrash/sundown/examples/smartypants.c +72 -0
  78. data/ext/snowcrash/sundown/examples/sundown.c +80 -0
  79. data/ext/snowcrash/sundown/html/houdini.h +37 -0
  80. data/ext/snowcrash/sundown/html/houdini_href_e.c +108 -0
  81. data/ext/snowcrash/sundown/html/houdini_html_e.c +84 -0
  82. data/ext/snowcrash/sundown/html/html.c +647 -0
  83. data/ext/snowcrash/sundown/html/html.h +77 -0
  84. data/ext/snowcrash/sundown/html/html_smartypants.c +389 -0
  85. data/ext/snowcrash/sundown/html_block_names.txt +25 -0
  86. data/ext/snowcrash/sundown/src/autolink.c +297 -0
  87. data/ext/snowcrash/sundown/src/autolink.h +51 -0
  88. data/ext/snowcrash/sundown/src/buffer.c +225 -0
  89. data/ext/snowcrash/sundown/src/buffer.h +96 -0
  90. data/ext/snowcrash/sundown/src/html_blocks.h +206 -0
  91. data/ext/snowcrash/sundown/src/markdown.c +2701 -0
  92. data/ext/snowcrash/sundown/src/markdown.h +147 -0
  93. data/ext/snowcrash/sundown/src/src_map.c +200 -0
  94. data/ext/snowcrash/sundown/src/src_map.h +58 -0
  95. data/ext/snowcrash/sundown/src/stack.c +81 -0
  96. data/ext/snowcrash/sundown/src/stack.h +29 -0
  97. data/ext/snowcrash/sundown/sundown.def +20 -0
  98. data/ext/snowcrash/tools/gyp/AUTHORS +11 -0
  99. data/ext/snowcrash/tools/gyp/DEPS +24 -0
  100. data/ext/snowcrash/tools/gyp/OWNERS +1 -0
  101. data/ext/snowcrash/tools/gyp/PRESUBMIT.py +120 -0
  102. data/ext/snowcrash/tools/gyp/buildbot/buildbot_run.py +190 -0
  103. data/ext/snowcrash/tools/gyp/codereview.settings +10 -0
  104. data/ext/snowcrash/tools/gyp/data/win/large-pdb-shim.cc +12 -0
  105. data/ext/snowcrash/tools/gyp/gyp +8 -0
  106. data/ext/snowcrash/tools/gyp/gyp.bat +5 -0
  107. data/ext/snowcrash/tools/gyp/gyp_main.py +18 -0
  108. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSNew.py +340 -0
  109. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSProject.py +208 -0
  110. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSSettings.py +1063 -0
  111. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSToolFile.py +58 -0
  112. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSUserFile.py +147 -0
  113. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSUtil.py +267 -0
  114. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSVersion.py +409 -0
  115. data/ext/snowcrash/tools/gyp/pylib/gyp/__init__.py +537 -0
  116. data/ext/snowcrash/tools/gyp/pylib/gyp/__init__.pyc +0 -0
  117. data/ext/snowcrash/tools/gyp/pylib/gyp/common.py +521 -0
  118. data/ext/snowcrash/tools/gyp/pylib/gyp/common.pyc +0 -0
  119. data/ext/snowcrash/tools/gyp/pylib/gyp/easy_xml.py +157 -0
  120. data/ext/snowcrash/tools/gyp/pylib/gyp/flock_tool.py +49 -0
  121. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/__init__.py +0 -0
  122. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/__init__.pyc +0 -0
  123. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/android.py +1069 -0
  124. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/cmake.py +1143 -0
  125. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/dump_dependency_json.py +81 -0
  126. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/eclipse.py +335 -0
  127. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/gypd.py +87 -0
  128. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/gypsh.py +56 -0
  129. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/make.py +2181 -0
  130. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/make.pyc +0 -0
  131. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/msvs.py +3335 -0
  132. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/ninja.py +2156 -0
  133. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/xcode.py +1224 -0
  134. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/xcode.pyc +0 -0
  135. data/ext/snowcrash/tools/gyp/pylib/gyp/input.py +2809 -0
  136. data/ext/snowcrash/tools/gyp/pylib/gyp/input.pyc +0 -0
  137. data/ext/snowcrash/tools/gyp/pylib/gyp/mac_tool.py +510 -0
  138. data/ext/snowcrash/tools/gyp/pylib/gyp/msvs_emulation.py +972 -0
  139. data/ext/snowcrash/tools/gyp/pylib/gyp/ninja_syntax.py +160 -0
  140. data/ext/snowcrash/tools/gyp/pylib/gyp/ordered_dict.py +289 -0
  141. data/ext/snowcrash/tools/gyp/pylib/gyp/win_tool.py +292 -0
  142. data/ext/snowcrash/tools/gyp/pylib/gyp/xcode_emulation.py +1440 -0
  143. data/ext/snowcrash/tools/gyp/pylib/gyp/xcode_emulation.pyc +0 -0
  144. data/ext/snowcrash/tools/gyp/pylib/gyp/xcodeproj_file.py +2889 -0
  145. data/ext/snowcrash/tools/gyp/pylib/gyp/xcodeproj_file.pyc +0 -0
  146. data/ext/snowcrash/tools/gyp/pylib/gyp/xml_fix.py +69 -0
  147. data/ext/snowcrash/tools/gyp/pylintrc +307 -0
  148. data/ext/snowcrash/tools/gyp/samples/samples +81 -0
  149. data/ext/snowcrash/tools/gyp/samples/samples.bat +5 -0
  150. data/ext/snowcrash/tools/gyp/setup.py +19 -0
  151. data/ext/snowcrash/tools/gyp/tools/Xcode/Specifications/gyp.pbfilespec +27 -0
  152. data/ext/snowcrash/tools/gyp/tools/Xcode/Specifications/gyp.xclangspec +226 -0
  153. data/ext/snowcrash/tools/gyp/tools/emacs/gyp.el +252 -0
  154. data/ext/snowcrash/tools/gyp/tools/graphviz.py +100 -0
  155. data/ext/snowcrash/tools/gyp/tools/pretty_gyp.py +155 -0
  156. data/ext/snowcrash/tools/gyp/tools/pretty_sln.py +168 -0
  157. data/ext/snowcrash/tools/gyp/tools/pretty_vcproj.py +329 -0
  158. data/ext/snowcrash/tools/homebrew/snowcrash.rb +11 -0
  159. data/ext/snowcrash/vcbuild.bat +184 -0
  160. data/lib/redsnow.rb +31 -0
  161. data/lib/redsnow/binding.rb +132 -0
  162. data/lib/redsnow/blueprint.rb +365 -0
  163. data/lib/redsnow/object.rb +18 -0
  164. data/lib/redsnow/parseresult.rb +107 -0
  165. data/lib/redsnow/version.rb +4 -0
  166. data/provisioning.sh +20 -0
  167. data/redsnow.gemspec +35 -0
  168. data/test/_helper.rb +15 -0
  169. data/test/fixtures/sample-api-ast.json +97 -0
  170. data/test/fixtures/sample-api.apib +20 -0
  171. data/test/redsnow_binding_test.rb +35 -0
  172. data/test/redsnow_parseresult_test.rb +50 -0
  173. data/test/redsnow_test.rb +285 -0
  174. metadata +358 -0
@@ -0,0 +1,3335 @@
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
+ import collections
6
+ import copy
7
+ import ntpath
8
+ import os
9
+ import posixpath
10
+ import re
11
+ import subprocess
12
+ import sys
13
+
14
+ import gyp.common
15
+ import gyp.easy_xml as easy_xml
16
+ import gyp.MSVSNew as MSVSNew
17
+ import gyp.MSVSProject as MSVSProject
18
+ import gyp.MSVSSettings as MSVSSettings
19
+ import gyp.MSVSToolFile as MSVSToolFile
20
+ import gyp.MSVSUserFile as MSVSUserFile
21
+ import gyp.MSVSUtil as MSVSUtil
22
+ import gyp.MSVSVersion as MSVSVersion
23
+ from gyp.common import GypError
24
+
25
+ # TODO: Remove once bots are on 2.7, http://crbug.com/241769
26
+ def _import_OrderedDict():
27
+ import collections
28
+ try:
29
+ return collections.OrderedDict
30
+ except AttributeError:
31
+ import gyp.ordered_dict
32
+ return gyp.ordered_dict.OrderedDict
33
+ OrderedDict = _import_OrderedDict()
34
+
35
+
36
+ # Regular expression for validating Visual Studio GUIDs. If the GUID
37
+ # contains lowercase hex letters, MSVS will be fine. However,
38
+ # IncrediBuild BuildConsole will parse the solution file, but then
39
+ # silently skip building the target causing hard to track down errors.
40
+ # Note that this only happens with the BuildConsole, and does not occur
41
+ # if IncrediBuild is executed from inside Visual Studio. This regex
42
+ # validates that the string looks like a GUID with all uppercase hex
43
+ # letters.
44
+ VALID_MSVS_GUID_CHARS = re.compile('^[A-F0-9\-]+$')
45
+
46
+
47
+ generator_default_variables = {
48
+ 'EXECUTABLE_PREFIX': '',
49
+ 'EXECUTABLE_SUFFIX': '.exe',
50
+ 'STATIC_LIB_PREFIX': '',
51
+ 'SHARED_LIB_PREFIX': '',
52
+ 'STATIC_LIB_SUFFIX': '.lib',
53
+ 'SHARED_LIB_SUFFIX': '.dll',
54
+ 'INTERMEDIATE_DIR': '$(IntDir)',
55
+ 'SHARED_INTERMEDIATE_DIR': '$(OutDir)obj/global_intermediate',
56
+ 'OS': 'win',
57
+ 'PRODUCT_DIR': '$(OutDir)',
58
+ 'LIB_DIR': '$(OutDir)lib',
59
+ 'RULE_INPUT_ROOT': '$(InputName)',
60
+ 'RULE_INPUT_DIRNAME': '$(InputDir)',
61
+ 'RULE_INPUT_EXT': '$(InputExt)',
62
+ 'RULE_INPUT_NAME': '$(InputFileName)',
63
+ 'RULE_INPUT_PATH': '$(InputPath)',
64
+ 'CONFIGURATION_NAME': '$(ConfigurationName)',
65
+ }
66
+
67
+
68
+ # The msvs specific sections that hold paths
69
+ generator_additional_path_sections = [
70
+ 'msvs_cygwin_dirs',
71
+ 'msvs_props',
72
+ ]
73
+
74
+
75
+ generator_additional_non_configuration_keys = [
76
+ 'msvs_cygwin_dirs',
77
+ 'msvs_cygwin_shell',
78
+ 'msvs_large_pdb',
79
+ 'msvs_shard',
80
+ 'msvs_external_builder',
81
+ 'msvs_external_builder_out_dir',
82
+ 'msvs_external_builder_build_cmd',
83
+ 'msvs_external_builder_clean_cmd',
84
+ ]
85
+
86
+
87
+ # List of precompiled header related keys.
88
+ precomp_keys = [
89
+ 'msvs_precompiled_header',
90
+ 'msvs_precompiled_source',
91
+ ]
92
+
93
+
94
+ cached_username = None
95
+
96
+
97
+ cached_domain = None
98
+
99
+
100
+ # Based on http://code.activestate.com/recipes/576694/.
101
+ class OrderedSet(collections.MutableSet):
102
+ def __init__(self, iterable=None):
103
+ self.end = end = []
104
+ end += [None, end, end] # sentinel node for doubly linked list
105
+ self.map = {} # key --> [key, prev, next]
106
+ if iterable is not None:
107
+ self |= iterable
108
+
109
+ def __len__(self):
110
+ return len(self.map)
111
+
112
+ def discard(self, key):
113
+ if key in self.map:
114
+ key, prev, next = self.map.pop(key)
115
+ prev[2] = next
116
+ next[1] = prev
117
+
118
+ def __contains__(self, key):
119
+ return key in self.map
120
+
121
+ def add(self, key):
122
+ if key not in self.map:
123
+ end = self.end
124
+ curr = end[1]
125
+ curr[2] = end[1] = self.map[key] = [key, curr, end]
126
+
127
+ def update(self, iterable):
128
+ for i in iterable:
129
+ if i not in self:
130
+ self.add(i)
131
+
132
+ def __iter__(self):
133
+ end = self.end
134
+ curr = end[2]
135
+ while curr is not end:
136
+ yield curr[0]
137
+ curr = curr[2]
138
+
139
+
140
+ # TODO(gspencer): Switch the os.environ calls to be
141
+ # win32api.GetDomainName() and win32api.GetUserName() once the
142
+ # python version in depot_tools has been updated to work on Vista
143
+ # 64-bit.
144
+ def _GetDomainAndUserName():
145
+ if sys.platform not in ('win32', 'cygwin'):
146
+ return ('DOMAIN', 'USERNAME')
147
+ global cached_username
148
+ global cached_domain
149
+ if not cached_domain or not cached_username:
150
+ domain = os.environ.get('USERDOMAIN')
151
+ username = os.environ.get('USERNAME')
152
+ if not domain or not username:
153
+ call = subprocess.Popen(['net', 'config', 'Workstation'],
154
+ stdout=subprocess.PIPE)
155
+ config = call.communicate()[0]
156
+ username_re = re.compile('^User name\s+(\S+)', re.MULTILINE)
157
+ username_match = username_re.search(config)
158
+ if username_match:
159
+ username = username_match.group(1)
160
+ domain_re = re.compile('^Logon domain\s+(\S+)', re.MULTILINE)
161
+ domain_match = domain_re.search(config)
162
+ if domain_match:
163
+ domain = domain_match.group(1)
164
+ cached_domain = domain
165
+ cached_username = username
166
+ return (cached_domain, cached_username)
167
+
168
+ fixpath_prefix = None
169
+
170
+
171
+ def _NormalizedSource(source):
172
+ """Normalize the path.
173
+
174
+ But not if that gets rid of a variable, as this may expand to something
175
+ larger than one directory.
176
+
177
+ Arguments:
178
+ source: The path to be normalize.d
179
+
180
+ Returns:
181
+ The normalized path.
182
+ """
183
+ normalized = os.path.normpath(source)
184
+ if source.count('$') == normalized.count('$'):
185
+ source = normalized
186
+ return source
187
+
188
+
189
+ def _FixPath(path):
190
+ """Convert paths to a form that will make sense in a vcproj file.
191
+
192
+ Arguments:
193
+ path: The path to convert, may contain / etc.
194
+ Returns:
195
+ The path with all slashes made into backslashes.
196
+ """
197
+ if fixpath_prefix and path and not os.path.isabs(path) and not path[0] == '$':
198
+ path = os.path.join(fixpath_prefix, path)
199
+ path = path.replace('/', '\\')
200
+ path = _NormalizedSource(path)
201
+ if path and path[-1] == '\\':
202
+ path = path[:-1]
203
+ return path
204
+
205
+
206
+ def _FixPaths(paths):
207
+ """Fix each of the paths of the list."""
208
+ return [_FixPath(i) for i in paths]
209
+
210
+
211
+ def _ConvertSourcesToFilterHierarchy(sources, prefix=None, excluded=None,
212
+ list_excluded=True, msvs_version=None):
213
+ """Converts a list split source file paths into a vcproj folder hierarchy.
214
+
215
+ Arguments:
216
+ sources: A list of source file paths split.
217
+ prefix: A list of source file path layers meant to apply to each of sources.
218
+ excluded: A set of excluded files.
219
+ msvs_version: A MSVSVersion object.
220
+
221
+ Returns:
222
+ A hierarchy of filenames and MSVSProject.Filter objects that matches the
223
+ layout of the source tree.
224
+ For example:
225
+ _ConvertSourcesToFilterHierarchy([['a', 'bob1.c'], ['b', 'bob2.c']],
226
+ prefix=['joe'])
227
+ -->
228
+ [MSVSProject.Filter('a', contents=['joe\\a\\bob1.c']),
229
+ MSVSProject.Filter('b', contents=['joe\\b\\bob2.c'])]
230
+ """
231
+ if not prefix: prefix = []
232
+ result = []
233
+ excluded_result = []
234
+ folders = OrderedDict()
235
+ # Gather files into the final result, excluded, or folders.
236
+ for s in sources:
237
+ if len(s) == 1:
238
+ filename = _NormalizedSource('\\'.join(prefix + s))
239
+ if filename in excluded:
240
+ excluded_result.append(filename)
241
+ else:
242
+ result.append(filename)
243
+ elif msvs_version and not msvs_version.UsesVcxproj():
244
+ # For MSVS 2008 and earlier, we need to process all files before walking
245
+ # the sub folders.
246
+ if not folders.get(s[0]):
247
+ folders[s[0]] = []
248
+ folders[s[0]].append(s[1:])
249
+ else:
250
+ contents = _ConvertSourcesToFilterHierarchy([s[1:]], prefix + [s[0]],
251
+ excluded=excluded,
252
+ list_excluded=list_excluded,
253
+ msvs_version=msvs_version)
254
+ contents = MSVSProject.Filter(s[0], contents=contents)
255
+ result.append(contents)
256
+ # Add a folder for excluded files.
257
+ if excluded_result and list_excluded:
258
+ excluded_folder = MSVSProject.Filter('_excluded_files',
259
+ contents=excluded_result)
260
+ result.append(excluded_folder)
261
+
262
+ if msvs_version and msvs_version.UsesVcxproj():
263
+ return result
264
+
265
+ # Populate all the folders.
266
+ for f in folders:
267
+ contents = _ConvertSourcesToFilterHierarchy(folders[f], prefix=prefix + [f],
268
+ excluded=excluded,
269
+ list_excluded=list_excluded,
270
+ msvs_version=msvs_version)
271
+ contents = MSVSProject.Filter(f, contents=contents)
272
+ result.append(contents)
273
+ return result
274
+
275
+
276
+ def _ToolAppend(tools, tool_name, setting, value, only_if_unset=False):
277
+ if not value: return
278
+ _ToolSetOrAppend(tools, tool_name, setting, value, only_if_unset)
279
+
280
+
281
+ def _ToolSetOrAppend(tools, tool_name, setting, value, only_if_unset=False):
282
+ # TODO(bradnelson): ugly hack, fix this more generally!!!
283
+ if 'Directories' in setting or 'Dependencies' in setting:
284
+ if type(value) == str:
285
+ value = value.replace('/', '\\')
286
+ else:
287
+ value = [i.replace('/', '\\') for i in value]
288
+ if not tools.get(tool_name):
289
+ tools[tool_name] = dict()
290
+ tool = tools[tool_name]
291
+ if tool.get(setting):
292
+ if only_if_unset: return
293
+ if type(tool[setting]) == list and type(value) == list:
294
+ tool[setting] += value
295
+ else:
296
+ raise TypeError(
297
+ 'Appending "%s" to a non-list setting "%s" for tool "%s" is '
298
+ 'not allowed, previous value: %s' % (
299
+ value, setting, tool_name, str(tool[setting])))
300
+ else:
301
+ tool[setting] = value
302
+
303
+
304
+ def _ConfigPlatform(config_data):
305
+ return config_data.get('msvs_configuration_platform', 'Win32')
306
+
307
+
308
+ def _ConfigBaseName(config_name, platform_name):
309
+ if config_name.endswith('_' + platform_name):
310
+ return config_name[0:-len(platform_name) - 1]
311
+ else:
312
+ return config_name
313
+
314
+
315
+ def _ConfigFullName(config_name, config_data):
316
+ platform_name = _ConfigPlatform(config_data)
317
+ return '%s|%s' % (_ConfigBaseName(config_name, platform_name), platform_name)
318
+
319
+
320
+ def _BuildCommandLineForRuleRaw(spec, cmd, cygwin_shell, has_input_path,
321
+ quote_cmd, do_setup_env):
322
+
323
+ if [x for x in cmd if '$(InputDir)' in x]:
324
+ input_dir_preamble = (
325
+ 'set INPUTDIR=$(InputDir)\n'
326
+ 'set INPUTDIR=%INPUTDIR:$(ProjectDir)=%\n'
327
+ 'set INPUTDIR=%INPUTDIR:~0,-1%\n'
328
+ )
329
+ else:
330
+ input_dir_preamble = ''
331
+
332
+ if cygwin_shell:
333
+ # Find path to cygwin.
334
+ cygwin_dir = _FixPath(spec.get('msvs_cygwin_dirs', ['.'])[0])
335
+ # Prepare command.
336
+ direct_cmd = cmd
337
+ direct_cmd = [i.replace('$(IntDir)',
338
+ '`cygpath -m "${INTDIR}"`') for i in direct_cmd]
339
+ direct_cmd = [i.replace('$(OutDir)',
340
+ '`cygpath -m "${OUTDIR}"`') for i in direct_cmd]
341
+ direct_cmd = [i.replace('$(InputDir)',
342
+ '`cygpath -m "${INPUTDIR}"`') for i in direct_cmd]
343
+ if has_input_path:
344
+ direct_cmd = [i.replace('$(InputPath)',
345
+ '`cygpath -m "${INPUTPATH}"`')
346
+ for i in direct_cmd]
347
+ direct_cmd = ['\\"%s\\"' % i.replace('"', '\\\\\\"') for i in direct_cmd]
348
+ # direct_cmd = gyp.common.EncodePOSIXShellList(direct_cmd)
349
+ direct_cmd = ' '.join(direct_cmd)
350
+ # TODO(quote): regularize quoting path names throughout the module
351
+ cmd = ''
352
+ if do_setup_env:
353
+ cmd += 'call "$(ProjectDir)%(cygwin_dir)s\\setup_env.bat" && '
354
+ cmd += 'set CYGWIN=nontsec&& '
355
+ if direct_cmd.find('NUMBER_OF_PROCESSORS') >= 0:
356
+ cmd += 'set /a NUMBER_OF_PROCESSORS_PLUS_1=%%NUMBER_OF_PROCESSORS%%+1&& '
357
+ if direct_cmd.find('INTDIR') >= 0:
358
+ cmd += 'set INTDIR=$(IntDir)&& '
359
+ if direct_cmd.find('OUTDIR') >= 0:
360
+ cmd += 'set OUTDIR=$(OutDir)&& '
361
+ if has_input_path and direct_cmd.find('INPUTPATH') >= 0:
362
+ cmd += 'set INPUTPATH=$(InputPath) && '
363
+ cmd += 'bash -c "%(cmd)s"'
364
+ cmd = cmd % {'cygwin_dir': cygwin_dir,
365
+ 'cmd': direct_cmd}
366
+ return input_dir_preamble + cmd
367
+ else:
368
+ # Convert cat --> type to mimic unix.
369
+ if cmd[0] == 'cat':
370
+ command = ['type']
371
+ else:
372
+ command = [cmd[0].replace('/', '\\')]
373
+ # Add call before command to ensure that commands can be tied together one
374
+ # after the other without aborting in Incredibuild, since IB makes a bat
375
+ # file out of the raw command string, and some commands (like python) are
376
+ # actually batch files themselves.
377
+ command.insert(0, 'call')
378
+ # Fix the paths
379
+ # TODO(quote): This is a really ugly heuristic, and will miss path fixing
380
+ # for arguments like "--arg=path" or "/opt:path".
381
+ # If the argument starts with a slash or dash, it's probably a command line
382
+ # switch
383
+ arguments = [i if (i[:1] in "/-") else _FixPath(i) for i in cmd[1:]]
384
+ arguments = [i.replace('$(InputDir)', '%INPUTDIR%') for i in arguments]
385
+ arguments = [MSVSSettings.FixVCMacroSlashes(i) for i in arguments]
386
+ if quote_cmd:
387
+ # Support a mode for using cmd directly.
388
+ # Convert any paths to native form (first element is used directly).
389
+ # TODO(quote): regularize quoting path names throughout the module
390
+ arguments = ['"%s"' % i for i in arguments]
391
+ # Collapse into a single command.
392
+ return input_dir_preamble + ' '.join(command + arguments)
393
+
394
+
395
+ def _BuildCommandLineForRule(spec, rule, has_input_path, do_setup_env):
396
+ # Currently this weird argument munging is used to duplicate the way a
397
+ # python script would need to be run as part of the chrome tree.
398
+ # Eventually we should add some sort of rule_default option to set this
399
+ # per project. For now the behavior chrome needs is the default.
400
+ mcs = rule.get('msvs_cygwin_shell')
401
+ if mcs is None:
402
+ mcs = int(spec.get('msvs_cygwin_shell', 1))
403
+ elif isinstance(mcs, str):
404
+ mcs = int(mcs)
405
+ quote_cmd = int(rule.get('msvs_quote_cmd', 1))
406
+ return _BuildCommandLineForRuleRaw(spec, rule['action'], mcs, has_input_path,
407
+ quote_cmd, do_setup_env=do_setup_env)
408
+
409
+
410
+ def _AddActionStep(actions_dict, inputs, outputs, description, command):
411
+ """Merge action into an existing list of actions.
412
+
413
+ Care must be taken so that actions which have overlapping inputs either don't
414
+ get assigned to the same input, or get collapsed into one.
415
+
416
+ Arguments:
417
+ actions_dict: dictionary keyed on input name, which maps to a list of
418
+ dicts describing the actions attached to that input file.
419
+ inputs: list of inputs
420
+ outputs: list of outputs
421
+ description: description of the action
422
+ command: command line to execute
423
+ """
424
+ # Require there to be at least one input (call sites will ensure this).
425
+ assert inputs
426
+
427
+ action = {
428
+ 'inputs': inputs,
429
+ 'outputs': outputs,
430
+ 'description': description,
431
+ 'command': command,
432
+ }
433
+
434
+ # Pick where to stick this action.
435
+ # While less than optimal in terms of build time, attach them to the first
436
+ # input for now.
437
+ chosen_input = inputs[0]
438
+
439
+ # Add it there.
440
+ if chosen_input not in actions_dict:
441
+ actions_dict[chosen_input] = []
442
+ actions_dict[chosen_input].append(action)
443
+
444
+
445
+ def _AddCustomBuildToolForMSVS(p, spec, primary_input,
446
+ inputs, outputs, description, cmd):
447
+ """Add a custom build tool to execute something.
448
+
449
+ Arguments:
450
+ p: the target project
451
+ spec: the target project dict
452
+ primary_input: input file to attach the build tool to
453
+ inputs: list of inputs
454
+ outputs: list of outputs
455
+ description: description of the action
456
+ cmd: command line to execute
457
+ """
458
+ inputs = _FixPaths(inputs)
459
+ outputs = _FixPaths(outputs)
460
+ tool = MSVSProject.Tool(
461
+ 'VCCustomBuildTool',
462
+ {'Description': description,
463
+ 'AdditionalDependencies': ';'.join(inputs),
464
+ 'Outputs': ';'.join(outputs),
465
+ 'CommandLine': cmd,
466
+ })
467
+ # Add to the properties of primary input for each config.
468
+ for config_name, c_data in spec['configurations'].iteritems():
469
+ p.AddFileConfig(_FixPath(primary_input),
470
+ _ConfigFullName(config_name, c_data), tools=[tool])
471
+
472
+
473
+ def _AddAccumulatedActionsToMSVS(p, spec, actions_dict):
474
+ """Add actions accumulated into an actions_dict, merging as needed.
475
+
476
+ Arguments:
477
+ p: the target project
478
+ spec: the target project dict
479
+ actions_dict: dictionary keyed on input name, which maps to a list of
480
+ dicts describing the actions attached to that input file.
481
+ """
482
+ for primary_input in actions_dict:
483
+ inputs = OrderedSet()
484
+ outputs = OrderedSet()
485
+ descriptions = []
486
+ commands = []
487
+ for action in actions_dict[primary_input]:
488
+ inputs.update(OrderedSet(action['inputs']))
489
+ outputs.update(OrderedSet(action['outputs']))
490
+ descriptions.append(action['description'])
491
+ commands.append(action['command'])
492
+ # Add the custom build step for one input file.
493
+ description = ', and also '.join(descriptions)
494
+ command = '\r\n'.join(commands)
495
+ _AddCustomBuildToolForMSVS(p, spec,
496
+ primary_input=primary_input,
497
+ inputs=inputs,
498
+ outputs=outputs,
499
+ description=description,
500
+ cmd=command)
501
+
502
+
503
+ def _RuleExpandPath(path, input_file):
504
+ """Given the input file to which a rule applied, string substitute a path.
505
+
506
+ Arguments:
507
+ path: a path to string expand
508
+ input_file: the file to which the rule applied.
509
+ Returns:
510
+ The string substituted path.
511
+ """
512
+ path = path.replace('$(InputName)',
513
+ os.path.splitext(os.path.split(input_file)[1])[0])
514
+ path = path.replace('$(InputDir)', os.path.dirname(input_file))
515
+ path = path.replace('$(InputExt)',
516
+ os.path.splitext(os.path.split(input_file)[1])[1])
517
+ path = path.replace('$(InputFileName)', os.path.split(input_file)[1])
518
+ path = path.replace('$(InputPath)', input_file)
519
+ return path
520
+
521
+
522
+ def _FindRuleTriggerFiles(rule, sources):
523
+ """Find the list of files which a particular rule applies to.
524
+
525
+ Arguments:
526
+ rule: the rule in question
527
+ sources: the set of all known source files for this project
528
+ Returns:
529
+ The list of sources that trigger a particular rule.
530
+ """
531
+ return rule.get('rule_sources', [])
532
+
533
+
534
+ def _RuleInputsAndOutputs(rule, trigger_file):
535
+ """Find the inputs and outputs generated by a rule.
536
+
537
+ Arguments:
538
+ rule: the rule in question.
539
+ trigger_file: the main trigger for this rule.
540
+ Returns:
541
+ The pair of (inputs, outputs) involved in this rule.
542
+ """
543
+ raw_inputs = _FixPaths(rule.get('inputs', []))
544
+ raw_outputs = _FixPaths(rule.get('outputs', []))
545
+ inputs = OrderedSet()
546
+ outputs = OrderedSet()
547
+ inputs.add(trigger_file)
548
+ for i in raw_inputs:
549
+ inputs.add(_RuleExpandPath(i, trigger_file))
550
+ for o in raw_outputs:
551
+ outputs.add(_RuleExpandPath(o, trigger_file))
552
+ return (inputs, outputs)
553
+
554
+
555
+ def _GenerateNativeRulesForMSVS(p, rules, output_dir, spec, options):
556
+ """Generate a native rules file.
557
+
558
+ Arguments:
559
+ p: the target project
560
+ rules: the set of rules to include
561
+ output_dir: the directory in which the project/gyp resides
562
+ spec: the project dict
563
+ options: global generator options
564
+ """
565
+ rules_filename = '%s%s.rules' % (spec['target_name'],
566
+ options.suffix)
567
+ rules_file = MSVSToolFile.Writer(os.path.join(output_dir, rules_filename),
568
+ spec['target_name'])
569
+ # Add each rule.
570
+ for r in rules:
571
+ rule_name = r['rule_name']
572
+ rule_ext = r['extension']
573
+ inputs = _FixPaths(r.get('inputs', []))
574
+ outputs = _FixPaths(r.get('outputs', []))
575
+ # Skip a rule with no action and no inputs.
576
+ if 'action' not in r and not r.get('rule_sources', []):
577
+ continue
578
+ cmd = _BuildCommandLineForRule(spec, r, has_input_path=True,
579
+ do_setup_env=True)
580
+ rules_file.AddCustomBuildRule(name=rule_name,
581
+ description=r.get('message', rule_name),
582
+ extensions=[rule_ext],
583
+ additional_dependencies=inputs,
584
+ outputs=outputs,
585
+ cmd=cmd)
586
+ # Write out rules file.
587
+ rules_file.WriteIfChanged()
588
+
589
+ # Add rules file to project.
590
+ p.AddToolFile(rules_filename)
591
+
592
+
593
+ def _Cygwinify(path):
594
+ path = path.replace('$(OutDir)', '$(OutDirCygwin)')
595
+ path = path.replace('$(IntDir)', '$(IntDirCygwin)')
596
+ return path
597
+
598
+
599
+ def _GenerateExternalRules(rules, output_dir, spec,
600
+ sources, options, actions_to_add):
601
+ """Generate an external makefile to do a set of rules.
602
+
603
+ Arguments:
604
+ rules: the list of rules to include
605
+ output_dir: path containing project and gyp files
606
+ spec: project specification data
607
+ sources: set of sources known
608
+ options: global generator options
609
+ actions_to_add: The list of actions we will add to.
610
+ """
611
+ filename = '%s_rules%s.mk' % (spec['target_name'], options.suffix)
612
+ mk_file = gyp.common.WriteOnDiff(os.path.join(output_dir, filename))
613
+ # Find cygwin style versions of some paths.
614
+ mk_file.write('OutDirCygwin:=$(shell cygpath -u "$(OutDir)")\n')
615
+ mk_file.write('IntDirCygwin:=$(shell cygpath -u "$(IntDir)")\n')
616
+ # Gather stuff needed to emit all: target.
617
+ all_inputs = OrderedSet()
618
+ all_outputs = OrderedSet()
619
+ all_output_dirs = OrderedSet()
620
+ first_outputs = []
621
+ for rule in rules:
622
+ trigger_files = _FindRuleTriggerFiles(rule, sources)
623
+ for tf in trigger_files:
624
+ inputs, outputs = _RuleInputsAndOutputs(rule, tf)
625
+ all_inputs.update(OrderedSet(inputs))
626
+ all_outputs.update(OrderedSet(outputs))
627
+ # Only use one target from each rule as the dependency for
628
+ # 'all' so we don't try to build each rule multiple times.
629
+ first_outputs.append(list(outputs)[0])
630
+ # Get the unique output directories for this rule.
631
+ output_dirs = [os.path.split(i)[0] for i in outputs]
632
+ for od in output_dirs:
633
+ all_output_dirs.add(od)
634
+ first_outputs_cyg = [_Cygwinify(i) for i in first_outputs]
635
+ # Write out all: target, including mkdir for each output directory.
636
+ mk_file.write('all: %s\n' % ' '.join(first_outputs_cyg))
637
+ for od in all_output_dirs:
638
+ if od:
639
+ mk_file.write('\tmkdir -p `cygpath -u "%s"`\n' % od)
640
+ mk_file.write('\n')
641
+ # Define how each output is generated.
642
+ for rule in rules:
643
+ trigger_files = _FindRuleTriggerFiles(rule, sources)
644
+ for tf in trigger_files:
645
+ # Get all the inputs and outputs for this rule for this trigger file.
646
+ inputs, outputs = _RuleInputsAndOutputs(rule, tf)
647
+ inputs = [_Cygwinify(i) for i in inputs]
648
+ outputs = [_Cygwinify(i) for i in outputs]
649
+ # Prepare the command line for this rule.
650
+ cmd = [_RuleExpandPath(c, tf) for c in rule['action']]
651
+ cmd = ['"%s"' % i for i in cmd]
652
+ cmd = ' '.join(cmd)
653
+ # Add it to the makefile.
654
+ mk_file.write('%s: %s\n' % (' '.join(outputs), ' '.join(inputs)))
655
+ mk_file.write('\t%s\n\n' % cmd)
656
+ # Close up the file.
657
+ mk_file.close()
658
+
659
+ # Add makefile to list of sources.
660
+ sources.add(filename)
661
+ # Add a build action to call makefile.
662
+ cmd = ['make',
663
+ 'OutDir=$(OutDir)',
664
+ 'IntDir=$(IntDir)',
665
+ '-j', '${NUMBER_OF_PROCESSORS_PLUS_1}',
666
+ '-f', filename]
667
+ cmd = _BuildCommandLineForRuleRaw(spec, cmd, True, False, True, True)
668
+ # Insert makefile as 0'th input, so it gets the action attached there,
669
+ # as this is easier to understand from in the IDE.
670
+ all_inputs = list(all_inputs)
671
+ all_inputs.insert(0, filename)
672
+ _AddActionStep(actions_to_add,
673
+ inputs=_FixPaths(all_inputs),
674
+ outputs=_FixPaths(all_outputs),
675
+ description='Running external rules for %s' %
676
+ spec['target_name'],
677
+ command=cmd)
678
+
679
+
680
+ def _EscapeEnvironmentVariableExpansion(s):
681
+ """Escapes % characters.
682
+
683
+ Escapes any % characters so that Windows-style environment variable
684
+ expansions will leave them alone.
685
+ See http://connect.microsoft.com/VisualStudio/feedback/details/106127/cl-d-name-text-containing-percentage-characters-doesnt-compile
686
+ to understand why we have to do this.
687
+
688
+ Args:
689
+ s: The string to be escaped.
690
+
691
+ Returns:
692
+ The escaped string.
693
+ """
694
+ s = s.replace('%', '%%')
695
+ return s
696
+
697
+
698
+ quote_replacer_regex = re.compile(r'(\\*)"')
699
+
700
+
701
+ def _EscapeCommandLineArgumentForMSVS(s):
702
+ """Escapes a Windows command-line argument.
703
+
704
+ So that the Win32 CommandLineToArgv function will turn the escaped result back
705
+ into the original string.
706
+ See http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
707
+ ("Parsing C++ Command-Line Arguments") to understand why we have to do
708
+ this.
709
+
710
+ Args:
711
+ s: the string to be escaped.
712
+ Returns:
713
+ the escaped string.
714
+ """
715
+
716
+ def _Replace(match):
717
+ # For a literal quote, CommandLineToArgv requires an odd number of
718
+ # backslashes preceding it, and it produces half as many literal backslashes
719
+ # (rounded down). So we need to produce 2n+1 backslashes.
720
+ return 2 * match.group(1) + '\\"'
721
+
722
+ # Escape all quotes so that they are interpreted literally.
723
+ s = quote_replacer_regex.sub(_Replace, s)
724
+ # Now add unescaped quotes so that any whitespace is interpreted literally.
725
+ s = '"' + s + '"'
726
+ return s
727
+
728
+
729
+ delimiters_replacer_regex = re.compile(r'(\\*)([,;]+)')
730
+
731
+
732
+ def _EscapeVCProjCommandLineArgListItem(s):
733
+ """Escapes command line arguments for MSVS.
734
+
735
+ The VCProj format stores string lists in a single string using commas and
736
+ semi-colons as separators, which must be quoted if they are to be
737
+ interpreted literally. However, command-line arguments may already have
738
+ quotes, and the VCProj parser is ignorant of the backslash escaping
739
+ convention used by CommandLineToArgv, so the command-line quotes and the
740
+ VCProj quotes may not be the same quotes. So to store a general
741
+ command-line argument in a VCProj list, we need to parse the existing
742
+ quoting according to VCProj's convention and quote any delimiters that are
743
+ not already quoted by that convention. The quotes that we add will also be
744
+ seen by CommandLineToArgv, so if backslashes precede them then we also have
745
+ to escape those backslashes according to the CommandLineToArgv
746
+ convention.
747
+
748
+ Args:
749
+ s: the string to be escaped.
750
+ Returns:
751
+ the escaped string.
752
+ """
753
+
754
+ def _Replace(match):
755
+ # For a non-literal quote, CommandLineToArgv requires an even number of
756
+ # backslashes preceding it, and it produces half as many literal
757
+ # backslashes. So we need to produce 2n backslashes.
758
+ return 2 * match.group(1) + '"' + match.group(2) + '"'
759
+
760
+ segments = s.split('"')
761
+ # The unquoted segments are at the even-numbered indices.
762
+ for i in range(0, len(segments), 2):
763
+ segments[i] = delimiters_replacer_regex.sub(_Replace, segments[i])
764
+ # Concatenate back into a single string
765
+ s = '"'.join(segments)
766
+ if len(segments) % 2 == 0:
767
+ # String ends while still quoted according to VCProj's convention. This
768
+ # means the delimiter and the next list item that follow this one in the
769
+ # .vcproj file will be misinterpreted as part of this item. There is nothing
770
+ # we can do about this. Adding an extra quote would correct the problem in
771
+ # the VCProj but cause the same problem on the final command-line. Moving
772
+ # the item to the end of the list does works, but that's only possible if
773
+ # there's only one such item. Let's just warn the user.
774
+ print >> sys.stderr, ('Warning: MSVS may misinterpret the odd number of ' +
775
+ 'quotes in ' + s)
776
+ return s
777
+
778
+
779
+ def _EscapeCppDefineForMSVS(s):
780
+ """Escapes a CPP define so that it will reach the compiler unaltered."""
781
+ s = _EscapeEnvironmentVariableExpansion(s)
782
+ s = _EscapeCommandLineArgumentForMSVS(s)
783
+ s = _EscapeVCProjCommandLineArgListItem(s)
784
+ # cl.exe replaces literal # characters with = in preprocesor definitions for
785
+ # some reason. Octal-encode to work around that.
786
+ s = s.replace('#', '\\%03o' % ord('#'))
787
+ return s
788
+
789
+
790
+ quote_replacer_regex2 = re.compile(r'(\\+)"')
791
+
792
+
793
+ def _EscapeCommandLineArgumentForMSBuild(s):
794
+ """Escapes a Windows command-line argument for use by MSBuild."""
795
+
796
+ def _Replace(match):
797
+ return (len(match.group(1)) / 2 * 4) * '\\' + '\\"'
798
+
799
+ # Escape all quotes so that they are interpreted literally.
800
+ s = quote_replacer_regex2.sub(_Replace, s)
801
+ return s
802
+
803
+
804
+ def _EscapeMSBuildSpecialCharacters(s):
805
+ escape_dictionary = {
806
+ '%': '%25',
807
+ '$': '%24',
808
+ '@': '%40',
809
+ "'": '%27',
810
+ ';': '%3B',
811
+ '?': '%3F',
812
+ '*': '%2A'
813
+ }
814
+ result = ''.join([escape_dictionary.get(c, c) for c in s])
815
+ return result
816
+
817
+
818
+ def _EscapeCppDefineForMSBuild(s):
819
+ """Escapes a CPP define so that it will reach the compiler unaltered."""
820
+ s = _EscapeEnvironmentVariableExpansion(s)
821
+ s = _EscapeCommandLineArgumentForMSBuild(s)
822
+ s = _EscapeMSBuildSpecialCharacters(s)
823
+ # cl.exe replaces literal # characters with = in preprocesor definitions for
824
+ # some reason. Octal-encode to work around that.
825
+ s = s.replace('#', '\\%03o' % ord('#'))
826
+ return s
827
+
828
+
829
+ def _GenerateRulesForMSVS(p, output_dir, options, spec,
830
+ sources, excluded_sources,
831
+ actions_to_add):
832
+ """Generate all the rules for a particular project.
833
+
834
+ Arguments:
835
+ p: the project
836
+ output_dir: directory to emit rules to
837
+ options: global options passed to the generator
838
+ spec: the specification for this project
839
+ sources: the set of all known source files in this project
840
+ excluded_sources: the set of sources excluded from normal processing
841
+ actions_to_add: deferred list of actions to add in
842
+ """
843
+ rules = spec.get('rules', [])
844
+ rules_native = [r for r in rules if not int(r.get('msvs_external_rule', 0))]
845
+ rules_external = [r for r in rules if int(r.get('msvs_external_rule', 0))]
846
+
847
+ # Handle rules that use a native rules file.
848
+ if rules_native:
849
+ _GenerateNativeRulesForMSVS(p, rules_native, output_dir, spec, options)
850
+
851
+ # Handle external rules (non-native rules).
852
+ if rules_external:
853
+ _GenerateExternalRules(rules_external, output_dir, spec,
854
+ sources, options, actions_to_add)
855
+ _AdjustSourcesForRules(spec, rules, sources, excluded_sources)
856
+
857
+
858
+ def _AdjustSourcesForRules(spec, rules, sources, excluded_sources):
859
+ # Add outputs generated by each rule (if applicable).
860
+ for rule in rules:
861
+ # Done if not processing outputs as sources.
862
+ if int(rule.get('process_outputs_as_sources', False)):
863
+ # Add in the outputs from this rule.
864
+ trigger_files = _FindRuleTriggerFiles(rule, sources)
865
+ for trigger_file in trigger_files:
866
+ inputs, outputs = _RuleInputsAndOutputs(rule, trigger_file)
867
+ inputs = OrderedSet(_FixPaths(inputs))
868
+ outputs = OrderedSet(_FixPaths(outputs))
869
+ inputs.remove(_FixPath(trigger_file))
870
+ sources.update(inputs)
871
+ if not spec.get('msvs_external_builder'):
872
+ excluded_sources.update(inputs)
873
+ sources.update(outputs)
874
+
875
+
876
+ def _FilterActionsFromExcluded(excluded_sources, actions_to_add):
877
+ """Take inputs with actions attached out of the list of exclusions.
878
+
879
+ Arguments:
880
+ excluded_sources: list of source files not to be built.
881
+ actions_to_add: dict of actions keyed on source file they're attached to.
882
+ Returns:
883
+ excluded_sources with files that have actions attached removed.
884
+ """
885
+ must_keep = OrderedSet(_FixPaths(actions_to_add.keys()))
886
+ return [s for s in excluded_sources if s not in must_keep]
887
+
888
+
889
+ def _GetDefaultConfiguration(spec):
890
+ return spec['configurations'][spec['default_configuration']]
891
+
892
+
893
+ def _GetGuidOfProject(proj_path, spec):
894
+ """Get the guid for the project.
895
+
896
+ Arguments:
897
+ proj_path: Path of the vcproj or vcxproj file to generate.
898
+ spec: The target dictionary containing the properties of the target.
899
+ Returns:
900
+ the guid.
901
+ Raises:
902
+ ValueError: if the specified GUID is invalid.
903
+ """
904
+ # Pluck out the default configuration.
905
+ default_config = _GetDefaultConfiguration(spec)
906
+ # Decide the guid of the project.
907
+ guid = default_config.get('msvs_guid')
908
+ if guid:
909
+ if VALID_MSVS_GUID_CHARS.match(guid) is None:
910
+ raise ValueError('Invalid MSVS guid: "%s". Must match regex: "%s".' %
911
+ (guid, VALID_MSVS_GUID_CHARS.pattern))
912
+ guid = '{%s}' % guid
913
+ guid = guid or MSVSNew.MakeGuid(proj_path)
914
+ return guid
915
+
916
+
917
+ def _GetMsbuildToolsetOfProject(proj_path, spec, version):
918
+ """Get the platform toolset for the project.
919
+
920
+ Arguments:
921
+ proj_path: Path of the vcproj or vcxproj file to generate.
922
+ spec: The target dictionary containing the properties of the target.
923
+ version: The MSVSVersion object.
924
+ Returns:
925
+ the platform toolset string or None.
926
+ """
927
+ # Pluck out the default configuration.
928
+ default_config = _GetDefaultConfiguration(spec)
929
+ toolset = default_config.get('msbuild_toolset')
930
+ if not toolset and version.DefaultToolset():
931
+ toolset = version.DefaultToolset()
932
+ return toolset
933
+
934
+
935
+ def _GenerateProject(project, options, version, generator_flags):
936
+ """Generates a vcproj file.
937
+
938
+ Arguments:
939
+ project: the MSVSProject object.
940
+ options: global generator options.
941
+ version: the MSVSVersion object.
942
+ generator_flags: dict of generator-specific flags.
943
+ Returns:
944
+ A list of source files that cannot be found on disk.
945
+ """
946
+ default_config = _GetDefaultConfiguration(project.spec)
947
+
948
+ # Skip emitting anything if told to with msvs_existing_vcproj option.
949
+ if default_config.get('msvs_existing_vcproj'):
950
+ return []
951
+
952
+ if version.UsesVcxproj():
953
+ return _GenerateMSBuildProject(project, options, version, generator_flags)
954
+ else:
955
+ return _GenerateMSVSProject(project, options, version, generator_flags)
956
+
957
+
958
+ def _GenerateMSVSProject(project, options, version, generator_flags):
959
+ """Generates a .vcproj file. It may create .rules and .user files too.
960
+
961
+ Arguments:
962
+ project: The project object we will generate the file for.
963
+ options: Global options passed to the generator.
964
+ version: The VisualStudioVersion object.
965
+ generator_flags: dict of generator-specific flags.
966
+ """
967
+ spec = project.spec
968
+ gyp.common.EnsureDirExists(project.path)
969
+
970
+ platforms = _GetUniquePlatforms(spec)
971
+ p = MSVSProject.Writer(project.path, version, spec['target_name'],
972
+ project.guid, platforms)
973
+
974
+ # Get directory project file is in.
975
+ project_dir = os.path.split(project.path)[0]
976
+ gyp_path = _NormalizedSource(project.build_file)
977
+ relative_path_of_gyp_file = gyp.common.RelativePath(gyp_path, project_dir)
978
+
979
+ config_type = _GetMSVSConfigurationType(spec, project.build_file)
980
+ for config_name, config in spec['configurations'].iteritems():
981
+ _AddConfigurationToMSVSProject(p, spec, config_type, config_name, config)
982
+
983
+ # Prepare list of sources and excluded sources.
984
+ gyp_file = os.path.split(project.build_file)[1]
985
+ sources, excluded_sources = _PrepareListOfSources(spec, generator_flags,
986
+ gyp_file)
987
+
988
+ # Add rules.
989
+ actions_to_add = {}
990
+ _GenerateRulesForMSVS(p, project_dir, options, spec,
991
+ sources, excluded_sources,
992
+ actions_to_add)
993
+ list_excluded = generator_flags.get('msvs_list_excluded_files', True)
994
+ sources, excluded_sources, excluded_idl = (
995
+ _AdjustSourcesAndConvertToFilterHierarchy(spec, options, project_dir,
996
+ sources, excluded_sources,
997
+ list_excluded, version))
998
+
999
+ # Add in files.
1000
+ missing_sources = _VerifySourcesExist(sources, project_dir)
1001
+ p.AddFiles(sources)
1002
+
1003
+ _AddToolFilesToMSVS(p, spec)
1004
+ _HandlePreCompiledHeaders(p, sources, spec)
1005
+ _AddActions(actions_to_add, spec, relative_path_of_gyp_file)
1006
+ _AddCopies(actions_to_add, spec)
1007
+ _WriteMSVSUserFile(project.path, version, spec)
1008
+
1009
+ # NOTE: this stanza must appear after all actions have been decided.
1010
+ # Don't excluded sources with actions attached, or they won't run.
1011
+ excluded_sources = _FilterActionsFromExcluded(
1012
+ excluded_sources, actions_to_add)
1013
+ _ExcludeFilesFromBeingBuilt(p, spec, excluded_sources, excluded_idl,
1014
+ list_excluded)
1015
+ _AddAccumulatedActionsToMSVS(p, spec, actions_to_add)
1016
+
1017
+ # Write it out.
1018
+ p.WriteIfChanged()
1019
+
1020
+ return missing_sources
1021
+
1022
+
1023
+ def _GetUniquePlatforms(spec):
1024
+ """Returns the list of unique platforms for this spec, e.g ['win32', ...].
1025
+
1026
+ Arguments:
1027
+ spec: The target dictionary containing the properties of the target.
1028
+ Returns:
1029
+ The MSVSUserFile object created.
1030
+ """
1031
+ # Gather list of unique platforms.
1032
+ platforms = OrderedSet()
1033
+ for configuration in spec['configurations']:
1034
+ platforms.add(_ConfigPlatform(spec['configurations'][configuration]))
1035
+ platforms = list(platforms)
1036
+ return platforms
1037
+
1038
+
1039
+ def _CreateMSVSUserFile(proj_path, version, spec):
1040
+ """Generates a .user file for the user running this Gyp program.
1041
+
1042
+ Arguments:
1043
+ proj_path: The path of the project file being created. The .user file
1044
+ shares the same path (with an appropriate suffix).
1045
+ version: The VisualStudioVersion object.
1046
+ spec: The target dictionary containing the properties of the target.
1047
+ Returns:
1048
+ The MSVSUserFile object created.
1049
+ """
1050
+ (domain, username) = _GetDomainAndUserName()
1051
+ vcuser_filename = '.'.join([proj_path, domain, username, 'user'])
1052
+ user_file = MSVSUserFile.Writer(vcuser_filename, version,
1053
+ spec['target_name'])
1054
+ return user_file
1055
+
1056
+
1057
+ def _GetMSVSConfigurationType(spec, build_file):
1058
+ """Returns the configuration type for this project.
1059
+
1060
+ It's a number defined by Microsoft. May raise an exception.
1061
+
1062
+ Args:
1063
+ spec: The target dictionary containing the properties of the target.
1064
+ build_file: The path of the gyp file.
1065
+ Returns:
1066
+ An integer, the configuration type.
1067
+ """
1068
+ try:
1069
+ config_type = {
1070
+ 'executable': '1', # .exe
1071
+ 'shared_library': '2', # .dll
1072
+ 'loadable_module': '2', # .dll
1073
+ 'static_library': '4', # .lib
1074
+ 'none': '10', # Utility type
1075
+ }[spec['type']]
1076
+ except KeyError:
1077
+ if spec.get('type'):
1078
+ raise GypError('Target type %s is not a valid target type for '
1079
+ 'target %s in %s.' %
1080
+ (spec['type'], spec['target_name'], build_file))
1081
+ else:
1082
+ raise GypError('Missing type field for target %s in %s.' %
1083
+ (spec['target_name'], build_file))
1084
+ return config_type
1085
+
1086
+
1087
+ def _AddConfigurationToMSVSProject(p, spec, config_type, config_name, config):
1088
+ """Adds a configuration to the MSVS project.
1089
+
1090
+ Many settings in a vcproj file are specific to a configuration. This
1091
+ function the main part of the vcproj file that's configuration specific.
1092
+
1093
+ Arguments:
1094
+ p: The target project being generated.
1095
+ spec: The target dictionary containing the properties of the target.
1096
+ config_type: The configuration type, a number as defined by Microsoft.
1097
+ config_name: The name of the configuration.
1098
+ config: The dictionary that defines the special processing to be done
1099
+ for this configuration.
1100
+ """
1101
+ # Get the information for this configuration
1102
+ include_dirs, resource_include_dirs = _GetIncludeDirs(config)
1103
+ libraries = _GetLibraries(spec)
1104
+ library_dirs = _GetLibraryDirs(config)
1105
+ out_file, vc_tool, _ = _GetOutputFilePathAndTool(spec, msbuild=False)
1106
+ defines = _GetDefines(config)
1107
+ defines = [_EscapeCppDefineForMSVS(d) for d in defines]
1108
+ disabled_warnings = _GetDisabledWarnings(config)
1109
+ prebuild = config.get('msvs_prebuild')
1110
+ postbuild = config.get('msvs_postbuild')
1111
+ def_file = _GetModuleDefinition(spec)
1112
+ precompiled_header = config.get('msvs_precompiled_header')
1113
+
1114
+ # Prepare the list of tools as a dictionary.
1115
+ tools = dict()
1116
+ # Add in user specified msvs_settings.
1117
+ msvs_settings = config.get('msvs_settings', {})
1118
+ MSVSSettings.ValidateMSVSSettings(msvs_settings)
1119
+
1120
+ # Prevent default library inheritance from the environment.
1121
+ _ToolAppend(tools, 'VCLinkerTool', 'AdditionalDependencies', ['$(NOINHERIT)'])
1122
+
1123
+ for tool in msvs_settings:
1124
+ settings = config['msvs_settings'][tool]
1125
+ for setting in settings:
1126
+ _ToolAppend(tools, tool, setting, settings[setting])
1127
+ # Add the information to the appropriate tool
1128
+ _ToolAppend(tools, 'VCCLCompilerTool',
1129
+ 'AdditionalIncludeDirectories', include_dirs)
1130
+ _ToolAppend(tools, 'VCResourceCompilerTool',
1131
+ 'AdditionalIncludeDirectories', resource_include_dirs)
1132
+ # Add in libraries.
1133
+ _ToolAppend(tools, 'VCLinkerTool', 'AdditionalDependencies', libraries)
1134
+ _ToolAppend(tools, 'VCLinkerTool', 'AdditionalLibraryDirectories',
1135
+ library_dirs)
1136
+ if out_file:
1137
+ _ToolAppend(tools, vc_tool, 'OutputFile', out_file, only_if_unset=True)
1138
+ # Add defines.
1139
+ _ToolAppend(tools, 'VCCLCompilerTool', 'PreprocessorDefinitions', defines)
1140
+ _ToolAppend(tools, 'VCResourceCompilerTool', 'PreprocessorDefinitions',
1141
+ defines)
1142
+ # Change program database directory to prevent collisions.
1143
+ _ToolAppend(tools, 'VCCLCompilerTool', 'ProgramDataBaseFileName',
1144
+ '$(IntDir)$(ProjectName)\\vc80.pdb', only_if_unset=True)
1145
+ # Add disabled warnings.
1146
+ _ToolAppend(tools, 'VCCLCompilerTool',
1147
+ 'DisableSpecificWarnings', disabled_warnings)
1148
+ # Add Pre-build.
1149
+ _ToolAppend(tools, 'VCPreBuildEventTool', 'CommandLine', prebuild)
1150
+ # Add Post-build.
1151
+ _ToolAppend(tools, 'VCPostBuildEventTool', 'CommandLine', postbuild)
1152
+ # Turn on precompiled headers if appropriate.
1153
+ if precompiled_header:
1154
+ precompiled_header = os.path.split(precompiled_header)[1]
1155
+ _ToolAppend(tools, 'VCCLCompilerTool', 'UsePrecompiledHeader', '2')
1156
+ _ToolAppend(tools, 'VCCLCompilerTool',
1157
+ 'PrecompiledHeaderThrough', precompiled_header)
1158
+ _ToolAppend(tools, 'VCCLCompilerTool',
1159
+ 'ForcedIncludeFiles', precompiled_header)
1160
+ # Loadable modules don't generate import libraries;
1161
+ # tell dependent projects to not expect one.
1162
+ if spec['type'] == 'loadable_module':
1163
+ _ToolAppend(tools, 'VCLinkerTool', 'IgnoreImportLibrary', 'true')
1164
+ # Set the module definition file if any.
1165
+ if def_file:
1166
+ _ToolAppend(tools, 'VCLinkerTool', 'ModuleDefinitionFile', def_file)
1167
+
1168
+ _AddConfigurationToMSVS(p, spec, tools, config, config_type, config_name)
1169
+
1170
+
1171
+ def _GetIncludeDirs(config):
1172
+ """Returns the list of directories to be used for #include directives.
1173
+
1174
+ Arguments:
1175
+ config: The dictionary that defines the special processing to be done
1176
+ for this configuration.
1177
+ Returns:
1178
+ The list of directory paths.
1179
+ """
1180
+ # TODO(bradnelson): include_dirs should really be flexible enough not to
1181
+ # require this sort of thing.
1182
+ include_dirs = (
1183
+ config.get('include_dirs', []) +
1184
+ config.get('msvs_system_include_dirs', []))
1185
+ resource_include_dirs = config.get('resource_include_dirs', include_dirs)
1186
+ include_dirs = _FixPaths(include_dirs)
1187
+ resource_include_dirs = _FixPaths(resource_include_dirs)
1188
+ return include_dirs, resource_include_dirs
1189
+
1190
+
1191
+ def _GetLibraryDirs(config):
1192
+ """Returns the list of directories to be used for library search paths.
1193
+
1194
+ Arguments:
1195
+ config: The dictionary that defines the special processing to be done
1196
+ for this configuration.
1197
+ Returns:
1198
+ The list of directory paths.
1199
+ """
1200
+
1201
+ library_dirs = config.get('library_dirs', [])
1202
+ library_dirs = _FixPaths(library_dirs)
1203
+ return library_dirs
1204
+
1205
+
1206
+ def _GetLibraries(spec):
1207
+ """Returns the list of libraries for this configuration.
1208
+
1209
+ Arguments:
1210
+ spec: The target dictionary containing the properties of the target.
1211
+ Returns:
1212
+ The list of directory paths.
1213
+ """
1214
+ libraries = spec.get('libraries', [])
1215
+ # Strip out -l, as it is not used on windows (but is needed so we can pass
1216
+ # in libraries that are assumed to be in the default library path).
1217
+ # Also remove duplicate entries, leaving only the last duplicate, while
1218
+ # preserving order.
1219
+ found = OrderedSet()
1220
+ unique_libraries_list = []
1221
+ for entry in reversed(libraries):
1222
+ library = re.sub('^\-l', '', entry)
1223
+ if not os.path.splitext(library)[1]:
1224
+ library += '.lib'
1225
+ if library not in found:
1226
+ found.add(library)
1227
+ unique_libraries_list.append(library)
1228
+ unique_libraries_list.reverse()
1229
+ return unique_libraries_list
1230
+
1231
+
1232
+ def _GetOutputFilePathAndTool(spec, msbuild):
1233
+ """Returns the path and tool to use for this target.
1234
+
1235
+ Figures out the path of the file this spec will create and the name of
1236
+ the VC tool that will create it.
1237
+
1238
+ Arguments:
1239
+ spec: The target dictionary containing the properties of the target.
1240
+ Returns:
1241
+ A triple of (file path, name of the vc tool, name of the msbuild tool)
1242
+ """
1243
+ # Select a name for the output file.
1244
+ out_file = ''
1245
+ vc_tool = ''
1246
+ msbuild_tool = ''
1247
+ output_file_map = {
1248
+ 'executable': ('VCLinkerTool', 'Link', '$(OutDir)', '.exe'),
1249
+ 'shared_library': ('VCLinkerTool', 'Link', '$(OutDir)', '.dll'),
1250
+ 'loadable_module': ('VCLinkerTool', 'Link', '$(OutDir)', '.dll'),
1251
+ 'static_library': ('VCLibrarianTool', 'Lib', '$(OutDir)lib\\', '.lib'),
1252
+ }
1253
+ output_file_props = output_file_map.get(spec['type'])
1254
+ if output_file_props and int(spec.get('msvs_auto_output_file', 1)):
1255
+ vc_tool, msbuild_tool, out_dir, suffix = output_file_props
1256
+ if spec.get('standalone_static_library', 0):
1257
+ out_dir = '$(OutDir)'
1258
+ out_dir = spec.get('product_dir', out_dir)
1259
+ product_extension = spec.get('product_extension')
1260
+ if product_extension:
1261
+ suffix = '.' + product_extension
1262
+ elif msbuild:
1263
+ suffix = '$(TargetExt)'
1264
+ prefix = spec.get('product_prefix', '')
1265
+ product_name = spec.get('product_name', '$(ProjectName)')
1266
+ out_file = ntpath.join(out_dir, prefix + product_name + suffix)
1267
+ return out_file, vc_tool, msbuild_tool
1268
+
1269
+
1270
+ def _GetOutputTargetExt(spec):
1271
+ """Returns the extension for this target, including the dot
1272
+
1273
+ If product_extension is specified, set target_extension to this to avoid
1274
+ MSB8012, returns None otherwise. Ignores any target_extension settings in
1275
+ the input files.
1276
+
1277
+ Arguments:
1278
+ spec: The target dictionary containing the properties of the target.
1279
+ Returns:
1280
+ A string with the extension, or None
1281
+ """
1282
+ target_extension = spec.get('product_extension')
1283
+ if target_extension:
1284
+ return '.' + target_extension
1285
+ return None
1286
+
1287
+
1288
+ def _GetDefines(config):
1289
+ """Returns the list of preprocessor definitions for this configuation.
1290
+
1291
+ Arguments:
1292
+ config: The dictionary that defines the special processing to be done
1293
+ for this configuration.
1294
+ Returns:
1295
+ The list of preprocessor definitions.
1296
+ """
1297
+ defines = []
1298
+ for d in config.get('defines', []):
1299
+ if type(d) == list:
1300
+ fd = '='.join([str(dpart) for dpart in d])
1301
+ else:
1302
+ fd = str(d)
1303
+ defines.append(fd)
1304
+ return defines
1305
+
1306
+
1307
+ def _GetDisabledWarnings(config):
1308
+ return [str(i) for i in config.get('msvs_disabled_warnings', [])]
1309
+
1310
+
1311
+ def _GetModuleDefinition(spec):
1312
+ def_file = ''
1313
+ if spec['type'] in ['shared_library', 'loadable_module', 'executable']:
1314
+ def_files = [s for s in spec.get('sources', []) if s.endswith('.def')]
1315
+ if len(def_files) == 1:
1316
+ def_file = _FixPath(def_files[0])
1317
+ elif def_files:
1318
+ raise ValueError(
1319
+ 'Multiple module definition files in one target, target %s lists '
1320
+ 'multiple .def files: %s' % (
1321
+ spec['target_name'], ' '.join(def_files)))
1322
+ return def_file
1323
+
1324
+
1325
+ def _ConvertToolsToExpectedForm(tools):
1326
+ """Convert tools to a form expected by Visual Studio.
1327
+
1328
+ Arguments:
1329
+ tools: A dictionary of settings; the tool name is the key.
1330
+ Returns:
1331
+ A list of Tool objects.
1332
+ """
1333
+ tool_list = []
1334
+ for tool, settings in tools.iteritems():
1335
+ # Collapse settings with lists.
1336
+ settings_fixed = {}
1337
+ for setting, value in settings.iteritems():
1338
+ if type(value) == list:
1339
+ if ((tool == 'VCLinkerTool' and
1340
+ setting == 'AdditionalDependencies') or
1341
+ setting == 'AdditionalOptions'):
1342
+ settings_fixed[setting] = ' '.join(value)
1343
+ else:
1344
+ settings_fixed[setting] = ';'.join(value)
1345
+ else:
1346
+ settings_fixed[setting] = value
1347
+ # Add in this tool.
1348
+ tool_list.append(MSVSProject.Tool(tool, settings_fixed))
1349
+ return tool_list
1350
+
1351
+
1352
+ def _AddConfigurationToMSVS(p, spec, tools, config, config_type, config_name):
1353
+ """Add to the project file the configuration specified by config.
1354
+
1355
+ Arguments:
1356
+ p: The target project being generated.
1357
+ spec: the target project dict.
1358
+ tools: A dictionary of settings; the tool name is the key.
1359
+ config: The dictionary that defines the special processing to be done
1360
+ for this configuration.
1361
+ config_type: The configuration type, a number as defined by Microsoft.
1362
+ config_name: The name of the configuration.
1363
+ """
1364
+ attributes = _GetMSVSAttributes(spec, config, config_type)
1365
+ # Add in this configuration.
1366
+ tool_list = _ConvertToolsToExpectedForm(tools)
1367
+ p.AddConfig(_ConfigFullName(config_name, config),
1368
+ attrs=attributes, tools=tool_list)
1369
+
1370
+
1371
+ def _GetMSVSAttributes(spec, config, config_type):
1372
+ # Prepare configuration attributes.
1373
+ prepared_attrs = {}
1374
+ source_attrs = config.get('msvs_configuration_attributes', {})
1375
+ for a in source_attrs:
1376
+ prepared_attrs[a] = source_attrs[a]
1377
+ # Add props files.
1378
+ vsprops_dirs = config.get('msvs_props', [])
1379
+ vsprops_dirs = _FixPaths(vsprops_dirs)
1380
+ if vsprops_dirs:
1381
+ prepared_attrs['InheritedPropertySheets'] = ';'.join(vsprops_dirs)
1382
+ # Set configuration type.
1383
+ prepared_attrs['ConfigurationType'] = config_type
1384
+ output_dir = prepared_attrs.get('OutputDirectory',
1385
+ '$(SolutionDir)$(ConfigurationName)')
1386
+ prepared_attrs['OutputDirectory'] = _FixPath(output_dir) + '\\'
1387
+ if 'IntermediateDirectory' not in prepared_attrs:
1388
+ intermediate = '$(ConfigurationName)\\obj\\$(ProjectName)'
1389
+ prepared_attrs['IntermediateDirectory'] = _FixPath(intermediate) + '\\'
1390
+ else:
1391
+ intermediate = _FixPath(prepared_attrs['IntermediateDirectory']) + '\\'
1392
+ intermediate = MSVSSettings.FixVCMacroSlashes(intermediate)
1393
+ prepared_attrs['IntermediateDirectory'] = intermediate
1394
+ return prepared_attrs
1395
+
1396
+
1397
+ def _AddNormalizedSources(sources_set, sources_array):
1398
+ sources_set.update(_NormalizedSource(s) for s in sources_array)
1399
+
1400
+
1401
+ def _PrepareListOfSources(spec, generator_flags, gyp_file):
1402
+ """Prepare list of sources and excluded sources.
1403
+
1404
+ Besides the sources specified directly in the spec, adds the gyp file so
1405
+ that a change to it will cause a re-compile. Also adds appropriate sources
1406
+ for actions and copies. Assumes later stage will un-exclude files which
1407
+ have custom build steps attached.
1408
+
1409
+ Arguments:
1410
+ spec: The target dictionary containing the properties of the target.
1411
+ gyp_file: The name of the gyp file.
1412
+ Returns:
1413
+ A pair of (list of sources, list of excluded sources).
1414
+ The sources will be relative to the gyp file.
1415
+ """
1416
+ sources = OrderedSet()
1417
+ _AddNormalizedSources(sources, spec.get('sources', []))
1418
+ excluded_sources = OrderedSet()
1419
+ # Add in the gyp file.
1420
+ if not generator_flags.get('standalone'):
1421
+ sources.add(gyp_file)
1422
+
1423
+ # Add in 'action' inputs and outputs.
1424
+ for a in spec.get('actions', []):
1425
+ inputs = a['inputs']
1426
+ inputs = [_NormalizedSource(i) for i in inputs]
1427
+ # Add all inputs to sources and excluded sources.
1428
+ inputs = OrderedSet(inputs)
1429
+ sources.update(inputs)
1430
+ if not spec.get('msvs_external_builder'):
1431
+ excluded_sources.update(inputs)
1432
+ if int(a.get('process_outputs_as_sources', False)):
1433
+ _AddNormalizedSources(sources, a.get('outputs', []))
1434
+ # Add in 'copies' inputs and outputs.
1435
+ for cpy in spec.get('copies', []):
1436
+ _AddNormalizedSources(sources, cpy.get('files', []))
1437
+ return (sources, excluded_sources)
1438
+
1439
+
1440
+ def _AdjustSourcesAndConvertToFilterHierarchy(
1441
+ spec, options, gyp_dir, sources, excluded_sources, list_excluded, version):
1442
+ """Adjusts the list of sources and excluded sources.
1443
+
1444
+ Also converts the sets to lists.
1445
+
1446
+ Arguments:
1447
+ spec: The target dictionary containing the properties of the target.
1448
+ options: Global generator options.
1449
+ gyp_dir: The path to the gyp file being processed.
1450
+ sources: A set of sources to be included for this project.
1451
+ excluded_sources: A set of sources to be excluded for this project.
1452
+ version: A MSVSVersion object.
1453
+ Returns:
1454
+ A trio of (list of sources, list of excluded sources,
1455
+ path of excluded IDL file)
1456
+ """
1457
+ # Exclude excluded sources coming into the generator.
1458
+ excluded_sources.update(OrderedSet(spec.get('sources_excluded', [])))
1459
+ # Add excluded sources into sources for good measure.
1460
+ sources.update(excluded_sources)
1461
+ # Convert to proper windows form.
1462
+ # NOTE: sources goes from being a set to a list here.
1463
+ # NOTE: excluded_sources goes from being a set to a list here.
1464
+ sources = _FixPaths(sources)
1465
+ # Convert to proper windows form.
1466
+ excluded_sources = _FixPaths(excluded_sources)
1467
+
1468
+ excluded_idl = _IdlFilesHandledNonNatively(spec, sources)
1469
+
1470
+ precompiled_related = _GetPrecompileRelatedFiles(spec)
1471
+ # Find the excluded ones, minus the precompiled header related ones.
1472
+ fully_excluded = [i for i in excluded_sources if i not in precompiled_related]
1473
+
1474
+ # Convert to folders and the right slashes.
1475
+ sources = [i.split('\\') for i in sources]
1476
+ sources = _ConvertSourcesToFilterHierarchy(sources, excluded=fully_excluded,
1477
+ list_excluded=list_excluded,
1478
+ msvs_version=version)
1479
+
1480
+ # Prune filters with a single child to flatten ugly directory structures
1481
+ # such as ../../src/modules/module1 etc.
1482
+ while len(sources) == 1 and isinstance(sources[0], MSVSProject.Filter):
1483
+ sources = sources[0].contents
1484
+
1485
+ return sources, excluded_sources, excluded_idl
1486
+
1487
+
1488
+ def _IdlFilesHandledNonNatively(spec, sources):
1489
+ # If any non-native rules use 'idl' as an extension exclude idl files.
1490
+ # Gather a list here to use later.
1491
+ using_idl = False
1492
+ for rule in spec.get('rules', []):
1493
+ if rule['extension'] == 'idl' and int(rule.get('msvs_external_rule', 0)):
1494
+ using_idl = True
1495
+ break
1496
+ if using_idl:
1497
+ excluded_idl = [i for i in sources if i.endswith('.idl')]
1498
+ else:
1499
+ excluded_idl = []
1500
+ return excluded_idl
1501
+
1502
+
1503
+ def _GetPrecompileRelatedFiles(spec):
1504
+ # Gather a list of precompiled header related sources.
1505
+ precompiled_related = []
1506
+ for _, config in spec['configurations'].iteritems():
1507
+ for k in precomp_keys:
1508
+ f = config.get(k)
1509
+ if f:
1510
+ precompiled_related.append(_FixPath(f))
1511
+ return precompiled_related
1512
+
1513
+
1514
+ def _ExcludeFilesFromBeingBuilt(p, spec, excluded_sources, excluded_idl,
1515
+ list_excluded):
1516
+ exclusions = _GetExcludedFilesFromBuild(spec, excluded_sources, excluded_idl)
1517
+ for file_name, excluded_configs in exclusions.iteritems():
1518
+ if (not list_excluded and
1519
+ len(excluded_configs) == len(spec['configurations'])):
1520
+ # If we're not listing excluded files, then they won't appear in the
1521
+ # project, so don't try to configure them to be excluded.
1522
+ pass
1523
+ else:
1524
+ for config_name, config in excluded_configs:
1525
+ p.AddFileConfig(file_name, _ConfigFullName(config_name, config),
1526
+ {'ExcludedFromBuild': 'true'})
1527
+
1528
+
1529
+ def _GetExcludedFilesFromBuild(spec, excluded_sources, excluded_idl):
1530
+ exclusions = {}
1531
+ # Exclude excluded sources from being built.
1532
+ for f in excluded_sources:
1533
+ excluded_configs = []
1534
+ for config_name, config in spec['configurations'].iteritems():
1535
+ precomped = [_FixPath(config.get(i, '')) for i in precomp_keys]
1536
+ # Don't do this for ones that are precompiled header related.
1537
+ if f not in precomped:
1538
+ excluded_configs.append((config_name, config))
1539
+ exclusions[f] = excluded_configs
1540
+ # If any non-native rules use 'idl' as an extension exclude idl files.
1541
+ # Exclude them now.
1542
+ for f in excluded_idl:
1543
+ excluded_configs = []
1544
+ for config_name, config in spec['configurations'].iteritems():
1545
+ excluded_configs.append((config_name, config))
1546
+ exclusions[f] = excluded_configs
1547
+ return exclusions
1548
+
1549
+
1550
+ def _AddToolFilesToMSVS(p, spec):
1551
+ # Add in tool files (rules).
1552
+ tool_files = OrderedSet()
1553
+ for _, config in spec['configurations'].iteritems():
1554
+ for f in config.get('msvs_tool_files', []):
1555
+ tool_files.add(f)
1556
+ for f in tool_files:
1557
+ p.AddToolFile(f)
1558
+
1559
+
1560
+ def _HandlePreCompiledHeaders(p, sources, spec):
1561
+ # Pre-compiled header source stubs need a different compiler flag
1562
+ # (generate precompiled header) and any source file not of the same
1563
+ # kind (i.e. C vs. C++) as the precompiled header source stub needs
1564
+ # to have use of precompiled headers disabled.
1565
+ extensions_excluded_from_precompile = []
1566
+ for config_name, config in spec['configurations'].iteritems():
1567
+ source = config.get('msvs_precompiled_source')
1568
+ if source:
1569
+ source = _FixPath(source)
1570
+ # UsePrecompiledHeader=1 for if using precompiled headers.
1571
+ tool = MSVSProject.Tool('VCCLCompilerTool',
1572
+ {'UsePrecompiledHeader': '1'})
1573
+ p.AddFileConfig(source, _ConfigFullName(config_name, config),
1574
+ {}, tools=[tool])
1575
+ basename, extension = os.path.splitext(source)
1576
+ if extension == '.c':
1577
+ extensions_excluded_from_precompile = ['.cc', '.cpp', '.cxx']
1578
+ else:
1579
+ extensions_excluded_from_precompile = ['.c']
1580
+ def DisableForSourceTree(source_tree):
1581
+ for source in source_tree:
1582
+ if isinstance(source, MSVSProject.Filter):
1583
+ DisableForSourceTree(source.contents)
1584
+ else:
1585
+ basename, extension = os.path.splitext(source)
1586
+ if extension in extensions_excluded_from_precompile:
1587
+ for config_name, config in spec['configurations'].iteritems():
1588
+ tool = MSVSProject.Tool('VCCLCompilerTool',
1589
+ {'UsePrecompiledHeader': '0',
1590
+ 'ForcedIncludeFiles': '$(NOINHERIT)'})
1591
+ p.AddFileConfig(_FixPath(source),
1592
+ _ConfigFullName(config_name, config),
1593
+ {}, tools=[tool])
1594
+ # Do nothing if there was no precompiled source.
1595
+ if extensions_excluded_from_precompile:
1596
+ DisableForSourceTree(sources)
1597
+
1598
+
1599
+ def _AddActions(actions_to_add, spec, relative_path_of_gyp_file):
1600
+ # Add actions.
1601
+ actions = spec.get('actions', [])
1602
+ # Don't setup_env every time. When all the actions are run together in one
1603
+ # batch file in VS, the PATH will grow too long.
1604
+ # Membership in this set means that the cygwin environment has been set up,
1605
+ # and does not need to be set up again.
1606
+ have_setup_env = set()
1607
+ for a in actions:
1608
+ # Attach actions to the gyp file if nothing else is there.
1609
+ inputs = a.get('inputs') or [relative_path_of_gyp_file]
1610
+ attached_to = inputs[0]
1611
+ need_setup_env = attached_to not in have_setup_env
1612
+ cmd = _BuildCommandLineForRule(spec, a, has_input_path=False,
1613
+ do_setup_env=need_setup_env)
1614
+ have_setup_env.add(attached_to)
1615
+ # Add the action.
1616
+ _AddActionStep(actions_to_add,
1617
+ inputs=inputs,
1618
+ outputs=a.get('outputs', []),
1619
+ description=a.get('message', a['action_name']),
1620
+ command=cmd)
1621
+
1622
+
1623
+ def _WriteMSVSUserFile(project_path, version, spec):
1624
+ # Add run_as and test targets.
1625
+ if 'run_as' in spec:
1626
+ run_as = spec['run_as']
1627
+ action = run_as.get('action', [])
1628
+ environment = run_as.get('environment', [])
1629
+ working_directory = run_as.get('working_directory', '.')
1630
+ elif int(spec.get('test', 0)):
1631
+ action = ['$(TargetPath)', '--gtest_print_time']
1632
+ environment = []
1633
+ working_directory = '.'
1634
+ else:
1635
+ return # Nothing to add
1636
+ # Write out the user file.
1637
+ user_file = _CreateMSVSUserFile(project_path, version, spec)
1638
+ for config_name, c_data in spec['configurations'].iteritems():
1639
+ user_file.AddDebugSettings(_ConfigFullName(config_name, c_data),
1640
+ action, environment, working_directory)
1641
+ user_file.WriteIfChanged()
1642
+
1643
+
1644
+ def _AddCopies(actions_to_add, spec):
1645
+ copies = _GetCopies(spec)
1646
+ for inputs, outputs, cmd, description in copies:
1647
+ _AddActionStep(actions_to_add, inputs=inputs, outputs=outputs,
1648
+ description=description, command=cmd)
1649
+
1650
+
1651
+ def _GetCopies(spec):
1652
+ copies = []
1653
+ # Add copies.
1654
+ for cpy in spec.get('copies', []):
1655
+ for src in cpy.get('files', []):
1656
+ dst = os.path.join(cpy['destination'], os.path.basename(src))
1657
+ # _AddCustomBuildToolForMSVS() will call _FixPath() on the inputs and
1658
+ # outputs, so do the same for our generated command line.
1659
+ if src.endswith('/'):
1660
+ src_bare = src[:-1]
1661
+ base_dir = posixpath.split(src_bare)[0]
1662
+ outer_dir = posixpath.split(src_bare)[1]
1663
+ cmd = 'cd "%s" && xcopy /e /f /y "%s" "%s\\%s\\"' % (
1664
+ _FixPath(base_dir), outer_dir, _FixPath(dst), outer_dir)
1665
+ copies.append(([src], ['dummy_copies', dst], cmd,
1666
+ 'Copying %s to %s' % (src, dst)))
1667
+ else:
1668
+ cmd = 'mkdir "%s" 2>nul & set ERRORLEVEL=0 & copy /Y "%s" "%s"' % (
1669
+ _FixPath(cpy['destination']), _FixPath(src), _FixPath(dst))
1670
+ copies.append(([src], [dst], cmd, 'Copying %s to %s' % (src, dst)))
1671
+ return copies
1672
+
1673
+
1674
+ def _GetPathDict(root, path):
1675
+ # |path| will eventually be empty (in the recursive calls) if it was initially
1676
+ # relative; otherwise it will eventually end up as '\', 'D:\', etc.
1677
+ if not path or path.endswith(os.sep):
1678
+ return root
1679
+ parent, folder = os.path.split(path)
1680
+ parent_dict = _GetPathDict(root, parent)
1681
+ if folder not in parent_dict:
1682
+ parent_dict[folder] = dict()
1683
+ return parent_dict[folder]
1684
+
1685
+
1686
+ def _DictsToFolders(base_path, bucket, flat):
1687
+ # Convert to folders recursively.
1688
+ children = []
1689
+ for folder, contents in bucket.iteritems():
1690
+ if type(contents) == dict:
1691
+ folder_children = _DictsToFolders(os.path.join(base_path, folder),
1692
+ contents, flat)
1693
+ if flat:
1694
+ children += folder_children
1695
+ else:
1696
+ folder_children = MSVSNew.MSVSFolder(os.path.join(base_path, folder),
1697
+ name='(' + folder + ')',
1698
+ entries=folder_children)
1699
+ children.append(folder_children)
1700
+ else:
1701
+ children.append(contents)
1702
+ return children
1703
+
1704
+
1705
+ def _CollapseSingles(parent, node):
1706
+ # Recursively explorer the tree of dicts looking for projects which are
1707
+ # the sole item in a folder which has the same name as the project. Bring
1708
+ # such projects up one level.
1709
+ if (type(node) == dict and
1710
+ len(node) == 1 and
1711
+ node.keys()[0] == parent + '.vcproj'):
1712
+ return node[node.keys()[0]]
1713
+ if type(node) != dict:
1714
+ return node
1715
+ for child in node:
1716
+ node[child] = _CollapseSingles(child, node[child])
1717
+ return node
1718
+
1719
+
1720
+ def _GatherSolutionFolders(sln_projects, project_objects, flat):
1721
+ root = {}
1722
+ # Convert into a tree of dicts on path.
1723
+ for p in sln_projects:
1724
+ gyp_file, target = gyp.common.ParseQualifiedTarget(p)[0:2]
1725
+ gyp_dir = os.path.dirname(gyp_file)
1726
+ path_dict = _GetPathDict(root, gyp_dir)
1727
+ path_dict[target + '.vcproj'] = project_objects[p]
1728
+ # Walk down from the top until we hit a folder that has more than one entry.
1729
+ # In practice, this strips the top-level "src/" dir from the hierarchy in
1730
+ # the solution.
1731
+ while len(root) == 1 and type(root[root.keys()[0]]) == dict:
1732
+ root = root[root.keys()[0]]
1733
+ # Collapse singles.
1734
+ root = _CollapseSingles('', root)
1735
+ # Merge buckets until everything is a root entry.
1736
+ return _DictsToFolders('', root, flat)
1737
+
1738
+
1739
+ def _GetPathOfProject(qualified_target, spec, options, msvs_version):
1740
+ default_config = _GetDefaultConfiguration(spec)
1741
+ proj_filename = default_config.get('msvs_existing_vcproj')
1742
+ if not proj_filename:
1743
+ proj_filename = (spec['target_name'] + options.suffix +
1744
+ msvs_version.ProjectExtension())
1745
+
1746
+ build_file = gyp.common.BuildFile(qualified_target)
1747
+ proj_path = os.path.join(os.path.dirname(build_file), proj_filename)
1748
+ fix_prefix = None
1749
+ if options.generator_output:
1750
+ project_dir_path = os.path.dirname(os.path.abspath(proj_path))
1751
+ proj_path = os.path.join(options.generator_output, proj_path)
1752
+ fix_prefix = gyp.common.RelativePath(project_dir_path,
1753
+ os.path.dirname(proj_path))
1754
+ return proj_path, fix_prefix
1755
+
1756
+
1757
+ def _GetPlatformOverridesOfProject(spec):
1758
+ # Prepare a dict indicating which project configurations are used for which
1759
+ # solution configurations for this target.
1760
+ config_platform_overrides = {}
1761
+ for config_name, c in spec['configurations'].iteritems():
1762
+ config_fullname = _ConfigFullName(config_name, c)
1763
+ platform = c.get('msvs_target_platform', _ConfigPlatform(c))
1764
+ fixed_config_fullname = '%s|%s' % (
1765
+ _ConfigBaseName(config_name, _ConfigPlatform(c)), platform)
1766
+ config_platform_overrides[config_fullname] = fixed_config_fullname
1767
+ return config_platform_overrides
1768
+
1769
+
1770
+ def _CreateProjectObjects(target_list, target_dicts, options, msvs_version):
1771
+ """Create a MSVSProject object for the targets found in target list.
1772
+
1773
+ Arguments:
1774
+ target_list: the list of targets to generate project objects for.
1775
+ target_dicts: the dictionary of specifications.
1776
+ options: global generator options.
1777
+ msvs_version: the MSVSVersion object.
1778
+ Returns:
1779
+ A set of created projects, keyed by target.
1780
+ """
1781
+ global fixpath_prefix
1782
+ # Generate each project.
1783
+ projects = {}
1784
+ for qualified_target in target_list:
1785
+ spec = target_dicts[qualified_target]
1786
+ if spec['toolset'] != 'target':
1787
+ raise GypError(
1788
+ 'Multiple toolsets not supported in msvs build (target %s)' %
1789
+ qualified_target)
1790
+ proj_path, fixpath_prefix = _GetPathOfProject(qualified_target, spec,
1791
+ options, msvs_version)
1792
+ guid = _GetGuidOfProject(proj_path, spec)
1793
+ overrides = _GetPlatformOverridesOfProject(spec)
1794
+ build_file = gyp.common.BuildFile(qualified_target)
1795
+ # Create object for this project.
1796
+ obj = MSVSNew.MSVSProject(
1797
+ proj_path,
1798
+ name=spec['target_name'],
1799
+ guid=guid,
1800
+ spec=spec,
1801
+ build_file=build_file,
1802
+ config_platform_overrides=overrides,
1803
+ fixpath_prefix=fixpath_prefix)
1804
+ # Set project toolset if any (MS build only)
1805
+ if msvs_version.UsesVcxproj():
1806
+ obj.set_msbuild_toolset(
1807
+ _GetMsbuildToolsetOfProject(proj_path, spec, msvs_version))
1808
+ projects[qualified_target] = obj
1809
+ # Set all the dependencies, but not if we are using an external builder like
1810
+ # ninja
1811
+ for project in projects.values():
1812
+ if not project.spec.get('msvs_external_builder'):
1813
+ deps = project.spec.get('dependencies', [])
1814
+ deps = [projects[d] for d in deps]
1815
+ project.set_dependencies(deps)
1816
+ return projects
1817
+
1818
+
1819
+ def _InitNinjaFlavor(options, target_list, target_dicts):
1820
+ """Initialize targets for the ninja flavor.
1821
+
1822
+ This sets up the necessary variables in the targets to generate msvs projects
1823
+ that use ninja as an external builder. The variables in the spec are only set
1824
+ if they have not been set. This allows individual specs to override the
1825
+ default values initialized here.
1826
+ Arguments:
1827
+ options: Options provided to the generator.
1828
+ target_list: List of target pairs: 'base/base.gyp:base'.
1829
+ target_dicts: Dict of target properties keyed on target pair.
1830
+ """
1831
+ for qualified_target in target_list:
1832
+ spec = target_dicts[qualified_target]
1833
+ if spec.get('msvs_external_builder'):
1834
+ # The spec explicitly defined an external builder, so don't change it.
1835
+ continue
1836
+
1837
+ path_to_ninja = spec.get('msvs_path_to_ninja', 'ninja.exe')
1838
+
1839
+ spec['msvs_external_builder'] = 'ninja'
1840
+ if not spec.get('msvs_external_builder_out_dir'):
1841
+ spec['msvs_external_builder_out_dir'] = \
1842
+ options.depth + '/out/$(Configuration)'
1843
+ if not spec.get('msvs_external_builder_build_cmd'):
1844
+ spec['msvs_external_builder_build_cmd'] = [
1845
+ path_to_ninja,
1846
+ '-C',
1847
+ '$(OutDir)',
1848
+ '$(ProjectName)',
1849
+ ]
1850
+ if not spec.get('msvs_external_builder_clean_cmd'):
1851
+ spec['msvs_external_builder_clean_cmd'] = [
1852
+ path_to_ninja,
1853
+ '-C',
1854
+ '$(OutDir)',
1855
+ '-t',
1856
+ 'clean',
1857
+ '$(ProjectName)',
1858
+ ]
1859
+
1860
+
1861
+ def CalculateVariables(default_variables, params):
1862
+ """Generated variables that require params to be known."""
1863
+
1864
+ generator_flags = params.get('generator_flags', {})
1865
+
1866
+ # Select project file format version (if unset, default to auto detecting).
1867
+ msvs_version = MSVSVersion.SelectVisualStudioVersion(
1868
+ generator_flags.get('msvs_version', 'auto'))
1869
+ # Stash msvs_version for later (so we don't have to probe the system twice).
1870
+ params['msvs_version'] = msvs_version
1871
+
1872
+ # Set a variable so conditions can be based on msvs_version.
1873
+ default_variables['MSVS_VERSION'] = msvs_version.ShortName()
1874
+
1875
+ # To determine processor word size on Windows, in addition to checking
1876
+ # PROCESSOR_ARCHITECTURE (which reflects the word size of the current
1877
+ # process), it is also necessary to check PROCESSOR_ARCITEW6432 (which
1878
+ # contains the actual word size of the system when running thru WOW64).
1879
+ if (os.environ.get('PROCESSOR_ARCHITECTURE', '').find('64') >= 0 or
1880
+ os.environ.get('PROCESSOR_ARCHITEW6432', '').find('64') >= 0):
1881
+ default_variables['MSVS_OS_BITS'] = 64
1882
+ else:
1883
+ default_variables['MSVS_OS_BITS'] = 32
1884
+
1885
+ if gyp.common.GetFlavor(params) == 'ninja':
1886
+ default_variables['SHARED_INTERMEDIATE_DIR'] = '$(OutDir)gen'
1887
+
1888
+
1889
+ def PerformBuild(data, configurations, params):
1890
+ options = params['options']
1891
+ msvs_version = params['msvs_version']
1892
+ devenv = os.path.join(msvs_version.path, 'Common7', 'IDE', 'devenv.com')
1893
+
1894
+ for build_file, build_file_dict in data.iteritems():
1895
+ (build_file_root, build_file_ext) = os.path.splitext(build_file)
1896
+ if build_file_ext != '.gyp':
1897
+ continue
1898
+ sln_path = build_file_root + options.suffix + '.sln'
1899
+ if options.generator_output:
1900
+ sln_path = os.path.join(options.generator_output, sln_path)
1901
+
1902
+ for config in configurations:
1903
+ arguments = [devenv, sln_path, '/Build', config]
1904
+ print 'Building [%s]: %s' % (config, arguments)
1905
+ rtn = subprocess.check_call(arguments)
1906
+
1907
+
1908
+ def GenerateOutput(target_list, target_dicts, data, params):
1909
+ """Generate .sln and .vcproj files.
1910
+
1911
+ This is the entry point for this generator.
1912
+ Arguments:
1913
+ target_list: List of target pairs: 'base/base.gyp:base'.
1914
+ target_dicts: Dict of target properties keyed on target pair.
1915
+ data: Dictionary containing per .gyp data.
1916
+ """
1917
+ global fixpath_prefix
1918
+
1919
+ options = params['options']
1920
+
1921
+ # Get the project file format version back out of where we stashed it in
1922
+ # GeneratorCalculatedVariables.
1923
+ msvs_version = params['msvs_version']
1924
+
1925
+ generator_flags = params.get('generator_flags', {})
1926
+
1927
+ # Optionally shard targets marked with 'msvs_shard': SHARD_COUNT.
1928
+ (target_list, target_dicts) = MSVSUtil.ShardTargets(target_list, target_dicts)
1929
+
1930
+ # Optionally use the large PDB workaround for targets marked with
1931
+ # 'msvs_large_pdb': 1.
1932
+ (target_list, target_dicts) = MSVSUtil.InsertLargePdbShims(
1933
+ target_list, target_dicts, generator_default_variables)
1934
+
1935
+ # Optionally configure each spec to use ninja as the external builder.
1936
+ if params.get('flavor') == 'ninja':
1937
+ _InitNinjaFlavor(options, target_list, target_dicts)
1938
+
1939
+ # Prepare the set of configurations.
1940
+ configs = set()
1941
+ for qualified_target in target_list:
1942
+ spec = target_dicts[qualified_target]
1943
+ for config_name, config in spec['configurations'].iteritems():
1944
+ configs.add(_ConfigFullName(config_name, config))
1945
+ configs = list(configs)
1946
+
1947
+ # Figure out all the projects that will be generated and their guids
1948
+ project_objects = _CreateProjectObjects(target_list, target_dicts, options,
1949
+ msvs_version)
1950
+
1951
+ # Generate each project.
1952
+ missing_sources = []
1953
+ for project in project_objects.values():
1954
+ fixpath_prefix = project.fixpath_prefix
1955
+ missing_sources.extend(_GenerateProject(project, options, msvs_version,
1956
+ generator_flags))
1957
+ fixpath_prefix = None
1958
+
1959
+ for build_file in data:
1960
+ # Validate build_file extension
1961
+ if not build_file.endswith('.gyp'):
1962
+ continue
1963
+ sln_path = os.path.splitext(build_file)[0] + options.suffix + '.sln'
1964
+ if options.generator_output:
1965
+ sln_path = os.path.join(options.generator_output, sln_path)
1966
+ # Get projects in the solution, and their dependents.
1967
+ sln_projects = gyp.common.BuildFileTargets(target_list, build_file)
1968
+ sln_projects += gyp.common.DeepDependencyTargets(target_dicts, sln_projects)
1969
+ # Create folder hierarchy.
1970
+ root_entries = _GatherSolutionFolders(
1971
+ sln_projects, project_objects, flat=msvs_version.FlatSolution())
1972
+ # Create solution.
1973
+ sln = MSVSNew.MSVSSolution(sln_path,
1974
+ entries=root_entries,
1975
+ variants=configs,
1976
+ websiteProperties=False,
1977
+ version=msvs_version)
1978
+ sln.Write()
1979
+
1980
+ if missing_sources:
1981
+ error_message = "Missing input files:\n" + \
1982
+ '\n'.join(set(missing_sources))
1983
+ if generator_flags.get('msvs_error_on_missing_sources', False):
1984
+ raise GypError(error_message)
1985
+ else:
1986
+ print >> sys.stdout, "Warning: " + error_message
1987
+
1988
+
1989
+ def _GenerateMSBuildFiltersFile(filters_path, source_files,
1990
+ extension_to_rule_name):
1991
+ """Generate the filters file.
1992
+
1993
+ This file is used by Visual Studio to organize the presentation of source
1994
+ files into folders.
1995
+
1996
+ Arguments:
1997
+ filters_path: The path of the file to be created.
1998
+ source_files: The hierarchical structure of all the sources.
1999
+ extension_to_rule_name: A dictionary mapping file extensions to rules.
2000
+ """
2001
+ filter_group = []
2002
+ source_group = []
2003
+ _AppendFiltersForMSBuild('', source_files, extension_to_rule_name,
2004
+ filter_group, source_group)
2005
+ if filter_group:
2006
+ content = ['Project',
2007
+ {'ToolsVersion': '4.0',
2008
+ 'xmlns': 'http://schemas.microsoft.com/developer/msbuild/2003'
2009
+ },
2010
+ ['ItemGroup'] + filter_group,
2011
+ ['ItemGroup'] + source_group
2012
+ ]
2013
+ easy_xml.WriteXmlIfChanged(content, filters_path, pretty=True, win32=True)
2014
+ elif os.path.exists(filters_path):
2015
+ # We don't need this filter anymore. Delete the old filter file.
2016
+ os.unlink(filters_path)
2017
+
2018
+
2019
+ def _AppendFiltersForMSBuild(parent_filter_name, sources,
2020
+ extension_to_rule_name,
2021
+ filter_group, source_group):
2022
+ """Creates the list of filters and sources to be added in the filter file.
2023
+
2024
+ Args:
2025
+ parent_filter_name: The name of the filter under which the sources are
2026
+ found.
2027
+ sources: The hierarchy of filters and sources to process.
2028
+ extension_to_rule_name: A dictionary mapping file extensions to rules.
2029
+ filter_group: The list to which filter entries will be appended.
2030
+ source_group: The list to which source entries will be appeneded.
2031
+ """
2032
+ for source in sources:
2033
+ if isinstance(source, MSVSProject.Filter):
2034
+ # We have a sub-filter. Create the name of that sub-filter.
2035
+ if not parent_filter_name:
2036
+ filter_name = source.name
2037
+ else:
2038
+ filter_name = '%s\\%s' % (parent_filter_name, source.name)
2039
+ # Add the filter to the group.
2040
+ filter_group.append(
2041
+ ['Filter', {'Include': filter_name},
2042
+ ['UniqueIdentifier', MSVSNew.MakeGuid(source.name)]])
2043
+ # Recurse and add its dependents.
2044
+ _AppendFiltersForMSBuild(filter_name, source.contents,
2045
+ extension_to_rule_name,
2046
+ filter_group, source_group)
2047
+ else:
2048
+ # It's a source. Create a source entry.
2049
+ _, element = _MapFileToMsBuildSourceType(source, extension_to_rule_name)
2050
+ source_entry = [element, {'Include': source}]
2051
+ # Specify the filter it is part of, if any.
2052
+ if parent_filter_name:
2053
+ source_entry.append(['Filter', parent_filter_name])
2054
+ source_group.append(source_entry)
2055
+
2056
+
2057
+ def _MapFileToMsBuildSourceType(source, extension_to_rule_name):
2058
+ """Returns the group and element type of the source file.
2059
+
2060
+ Arguments:
2061
+ source: The source file name.
2062
+ extension_to_rule_name: A dictionary mapping file extensions to rules.
2063
+
2064
+ Returns:
2065
+ A pair of (group this file should be part of, the label of element)
2066
+ """
2067
+ _, ext = os.path.splitext(source)
2068
+ if ext in extension_to_rule_name:
2069
+ group = 'rule'
2070
+ element = extension_to_rule_name[ext]
2071
+ elif ext in ['.cc', '.cpp', '.c', '.cxx']:
2072
+ group = 'compile'
2073
+ element = 'ClCompile'
2074
+ elif ext in ['.h', '.hxx']:
2075
+ group = 'include'
2076
+ element = 'ClInclude'
2077
+ elif ext == '.rc':
2078
+ group = 'resource'
2079
+ element = 'ResourceCompile'
2080
+ elif ext == '.idl':
2081
+ group = 'midl'
2082
+ element = 'Midl'
2083
+ else:
2084
+ group = 'none'
2085
+ element = 'None'
2086
+ return (group, element)
2087
+
2088
+
2089
+ def _GenerateRulesForMSBuild(output_dir, options, spec,
2090
+ sources, excluded_sources,
2091
+ props_files_of_rules, targets_files_of_rules,
2092
+ actions_to_add, extension_to_rule_name):
2093
+ # MSBuild rules are implemented using three files: an XML file, a .targets
2094
+ # file and a .props file.
2095
+ # See http://blogs.msdn.com/b/vcblog/archive/2010/04/21/quick-help-on-vs2010-custom-build-rule.aspx
2096
+ # for more details.
2097
+ rules = spec.get('rules', [])
2098
+ rules_native = [r for r in rules if not int(r.get('msvs_external_rule', 0))]
2099
+ rules_external = [r for r in rules if int(r.get('msvs_external_rule', 0))]
2100
+
2101
+ msbuild_rules = []
2102
+ for rule in rules_native:
2103
+ # Skip a rule with no action and no inputs.
2104
+ if 'action' not in rule and not rule.get('rule_sources', []):
2105
+ continue
2106
+ msbuild_rule = MSBuildRule(rule, spec)
2107
+ msbuild_rules.append(msbuild_rule)
2108
+ extension_to_rule_name[msbuild_rule.extension] = msbuild_rule.rule_name
2109
+ if msbuild_rules:
2110
+ base = spec['target_name'] + options.suffix
2111
+ props_name = base + '.props'
2112
+ targets_name = base + '.targets'
2113
+ xml_name = base + '.xml'
2114
+
2115
+ props_files_of_rules.add(props_name)
2116
+ targets_files_of_rules.add(targets_name)
2117
+
2118
+ props_path = os.path.join(output_dir, props_name)
2119
+ targets_path = os.path.join(output_dir, targets_name)
2120
+ xml_path = os.path.join(output_dir, xml_name)
2121
+
2122
+ _GenerateMSBuildRulePropsFile(props_path, msbuild_rules)
2123
+ _GenerateMSBuildRuleTargetsFile(targets_path, msbuild_rules)
2124
+ _GenerateMSBuildRuleXmlFile(xml_path, msbuild_rules)
2125
+
2126
+ if rules_external:
2127
+ _GenerateExternalRules(rules_external, output_dir, spec,
2128
+ sources, options, actions_to_add)
2129
+ _AdjustSourcesForRules(spec, rules, sources, excluded_sources)
2130
+
2131
+
2132
+ class MSBuildRule(object):
2133
+ """Used to store information used to generate an MSBuild rule.
2134
+
2135
+ Attributes:
2136
+ rule_name: The rule name, sanitized to use in XML.
2137
+ target_name: The name of the target.
2138
+ after_targets: The name of the AfterTargets element.
2139
+ before_targets: The name of the BeforeTargets element.
2140
+ depends_on: The name of the DependsOn element.
2141
+ compute_output: The name of the ComputeOutput element.
2142
+ dirs_to_make: The name of the DirsToMake element.
2143
+ inputs: The name of the _inputs element.
2144
+ tlog: The name of the _tlog element.
2145
+ extension: The extension this rule applies to.
2146
+ description: The message displayed when this rule is invoked.
2147
+ additional_dependencies: A string listing additional dependencies.
2148
+ outputs: The outputs of this rule.
2149
+ command: The command used to run the rule.
2150
+ """
2151
+
2152
+ def __init__(self, rule, spec):
2153
+ self.display_name = rule['rule_name']
2154
+ # Assure that the rule name is only characters and numbers
2155
+ self.rule_name = re.sub(r'\W', '_', self.display_name)
2156
+ # Create the various element names, following the example set by the
2157
+ # Visual Studio 2008 to 2010 conversion. I don't know if VS2010
2158
+ # is sensitive to the exact names.
2159
+ self.target_name = '_' + self.rule_name
2160
+ self.after_targets = self.rule_name + 'AfterTargets'
2161
+ self.before_targets = self.rule_name + 'BeforeTargets'
2162
+ self.depends_on = self.rule_name + 'DependsOn'
2163
+ self.compute_output = 'Compute%sOutput' % self.rule_name
2164
+ self.dirs_to_make = self.rule_name + 'DirsToMake'
2165
+ self.inputs = self.rule_name + '_inputs'
2166
+ self.tlog = self.rule_name + '_tlog'
2167
+ self.extension = rule['extension']
2168
+ if not self.extension.startswith('.'):
2169
+ self.extension = '.' + self.extension
2170
+
2171
+ self.description = MSVSSettings.ConvertVCMacrosToMSBuild(
2172
+ rule.get('message', self.rule_name))
2173
+ old_additional_dependencies = _FixPaths(rule.get('inputs', []))
2174
+ self.additional_dependencies = (
2175
+ ';'.join([MSVSSettings.ConvertVCMacrosToMSBuild(i)
2176
+ for i in old_additional_dependencies]))
2177
+ old_outputs = _FixPaths(rule.get('outputs', []))
2178
+ self.outputs = ';'.join([MSVSSettings.ConvertVCMacrosToMSBuild(i)
2179
+ for i in old_outputs])
2180
+ old_command = _BuildCommandLineForRule(spec, rule, has_input_path=True,
2181
+ do_setup_env=True)
2182
+ self.command = MSVSSettings.ConvertVCMacrosToMSBuild(old_command)
2183
+
2184
+
2185
+ def _GenerateMSBuildRulePropsFile(props_path, msbuild_rules):
2186
+ """Generate the .props file."""
2187
+ content = ['Project',
2188
+ {'xmlns': 'http://schemas.microsoft.com/developer/msbuild/2003'}]
2189
+ for rule in msbuild_rules:
2190
+ content.extend([
2191
+ ['PropertyGroup',
2192
+ {'Condition': "'$(%s)' == '' and '$(%s)' == '' and "
2193
+ "'$(ConfigurationType)' != 'Makefile'" % (rule.before_targets,
2194
+ rule.after_targets)
2195
+ },
2196
+ [rule.before_targets, 'Midl'],
2197
+ [rule.after_targets, 'CustomBuild'],
2198
+ ],
2199
+ ['PropertyGroup',
2200
+ [rule.depends_on,
2201
+ {'Condition': "'$(ConfigurationType)' != 'Makefile'"},
2202
+ '_SelectedFiles;$(%s)' % rule.depends_on
2203
+ ],
2204
+ ],
2205
+ ['ItemDefinitionGroup',
2206
+ [rule.rule_name,
2207
+ ['CommandLineTemplate', rule.command],
2208
+ ['Outputs', rule.outputs],
2209
+ ['ExecutionDescription', rule.description],
2210
+ ['AdditionalDependencies', rule.additional_dependencies],
2211
+ ],
2212
+ ]
2213
+ ])
2214
+ easy_xml.WriteXmlIfChanged(content, props_path, pretty=True, win32=True)
2215
+
2216
+
2217
+ def _GenerateMSBuildRuleTargetsFile(targets_path, msbuild_rules):
2218
+ """Generate the .targets file."""
2219
+ content = ['Project',
2220
+ {'xmlns': 'http://schemas.microsoft.com/developer/msbuild/2003'
2221
+ }
2222
+ ]
2223
+ item_group = [
2224
+ 'ItemGroup',
2225
+ ['PropertyPageSchema',
2226
+ {'Include': '$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml'}
2227
+ ]
2228
+ ]
2229
+ for rule in msbuild_rules:
2230
+ item_group.append(
2231
+ ['AvailableItemName',
2232
+ {'Include': rule.rule_name},
2233
+ ['Targets', rule.target_name],
2234
+ ])
2235
+ content.append(item_group)
2236
+
2237
+ for rule in msbuild_rules:
2238
+ content.append(
2239
+ ['UsingTask',
2240
+ {'TaskName': rule.rule_name,
2241
+ 'TaskFactory': 'XamlTaskFactory',
2242
+ 'AssemblyName': 'Microsoft.Build.Tasks.v4.0'
2243
+ },
2244
+ ['Task', '$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml'],
2245
+ ])
2246
+ for rule in msbuild_rules:
2247
+ rule_name = rule.rule_name
2248
+ target_outputs = '%%(%s.Outputs)' % rule_name
2249
+ target_inputs = ('%%(%s.Identity);%%(%s.AdditionalDependencies);'
2250
+ '$(MSBuildProjectFile)') % (rule_name, rule_name)
2251
+ rule_inputs = '%%(%s.Identity)' % rule_name
2252
+ extension_condition = ("'%(Extension)'=='.obj' or "
2253
+ "'%(Extension)'=='.res' or "
2254
+ "'%(Extension)'=='.rsc' or "
2255
+ "'%(Extension)'=='.lib'")
2256
+ remove_section = [
2257
+ 'ItemGroup',
2258
+ {'Condition': "'@(SelectedFiles)' != ''"},
2259
+ [rule_name,
2260
+ {'Remove': '@(%s)' % rule_name,
2261
+ 'Condition': "'%(Identity)' != '@(SelectedFiles)'"
2262
+ }
2263
+ ]
2264
+ ]
2265
+ inputs_section = [
2266
+ 'ItemGroup',
2267
+ [rule.inputs, {'Include': '%%(%s.AdditionalDependencies)' % rule_name}]
2268
+ ]
2269
+ logging_section = [
2270
+ 'ItemGroup',
2271
+ [rule.tlog,
2272
+ {'Include': '%%(%s.Outputs)' % rule_name,
2273
+ 'Condition': ("'%%(%s.Outputs)' != '' and "
2274
+ "'%%(%s.ExcludedFromBuild)' != 'true'" %
2275
+ (rule_name, rule_name))
2276
+ },
2277
+ ['Source', "@(%s, '|')" % rule_name],
2278
+ ['Inputs', "@(%s -> '%%(Fullpath)', ';')" % rule.inputs],
2279
+ ],
2280
+ ]
2281
+ message_section = [
2282
+ 'Message',
2283
+ {'Importance': 'High',
2284
+ 'Text': '%%(%s.ExecutionDescription)' % rule_name
2285
+ }
2286
+ ]
2287
+ write_tlog_section = [
2288
+ 'WriteLinesToFile',
2289
+ {'Condition': "'@(%s)' != '' and '%%(%s.ExcludedFromBuild)' != "
2290
+ "'true'" % (rule.tlog, rule.tlog),
2291
+ 'File': '$(IntDir)$(ProjectName).write.1.tlog',
2292
+ 'Lines': "^%%(%s.Source);@(%s->'%%(Fullpath)')" % (rule.tlog,
2293
+ rule.tlog)
2294
+ }
2295
+ ]
2296
+ read_tlog_section = [
2297
+ 'WriteLinesToFile',
2298
+ {'Condition': "'@(%s)' != '' and '%%(%s.ExcludedFromBuild)' != "
2299
+ "'true'" % (rule.tlog, rule.tlog),
2300
+ 'File': '$(IntDir)$(ProjectName).read.1.tlog',
2301
+ 'Lines': "^%%(%s.Source);%%(%s.Inputs)" % (rule.tlog, rule.tlog)
2302
+ }
2303
+ ]
2304
+ command_and_input_section = [
2305
+ rule_name,
2306
+ {'Condition': "'@(%s)' != '' and '%%(%s.ExcludedFromBuild)' != "
2307
+ "'true'" % (rule_name, rule_name),
2308
+ 'CommandLineTemplate': '%%(%s.CommandLineTemplate)' % rule_name,
2309
+ 'AdditionalOptions': '%%(%s.AdditionalOptions)' % rule_name,
2310
+ 'Inputs': rule_inputs
2311
+ }
2312
+ ]
2313
+ content.extend([
2314
+ ['Target',
2315
+ {'Name': rule.target_name,
2316
+ 'BeforeTargets': '$(%s)' % rule.before_targets,
2317
+ 'AfterTargets': '$(%s)' % rule.after_targets,
2318
+ 'Condition': "'@(%s)' != ''" % rule_name,
2319
+ 'DependsOnTargets': '$(%s);%s' % (rule.depends_on,
2320
+ rule.compute_output),
2321
+ 'Outputs': target_outputs,
2322
+ 'Inputs': target_inputs
2323
+ },
2324
+ remove_section,
2325
+ inputs_section,
2326
+ logging_section,
2327
+ message_section,
2328
+ write_tlog_section,
2329
+ read_tlog_section,
2330
+ command_and_input_section,
2331
+ ],
2332
+ ['PropertyGroup',
2333
+ ['ComputeLinkInputsTargets',
2334
+ '$(ComputeLinkInputsTargets);',
2335
+ '%s;' % rule.compute_output
2336
+ ],
2337
+ ['ComputeLibInputsTargets',
2338
+ '$(ComputeLibInputsTargets);',
2339
+ '%s;' % rule.compute_output
2340
+ ],
2341
+ ],
2342
+ ['Target',
2343
+ {'Name': rule.compute_output,
2344
+ 'Condition': "'@(%s)' != ''" % rule_name
2345
+ },
2346
+ ['ItemGroup',
2347
+ [rule.dirs_to_make,
2348
+ {'Condition': "'@(%s)' != '' and "
2349
+ "'%%(%s.ExcludedFromBuild)' != 'true'" % (rule_name, rule_name),
2350
+ 'Include': '%%(%s.Outputs)' % rule_name
2351
+ }
2352
+ ],
2353
+ ['Link',
2354
+ {'Include': '%%(%s.Identity)' % rule.dirs_to_make,
2355
+ 'Condition': extension_condition
2356
+ }
2357
+ ],
2358
+ ['Lib',
2359
+ {'Include': '%%(%s.Identity)' % rule.dirs_to_make,
2360
+ 'Condition': extension_condition
2361
+ }
2362
+ ],
2363
+ ['ImpLib',
2364
+ {'Include': '%%(%s.Identity)' % rule.dirs_to_make,
2365
+ 'Condition': extension_condition
2366
+ }
2367
+ ],
2368
+ ],
2369
+ ['MakeDir',
2370
+ {'Directories': ("@(%s->'%%(RootDir)%%(Directory)')" %
2371
+ rule.dirs_to_make)
2372
+ }
2373
+ ]
2374
+ ],
2375
+ ])
2376
+ easy_xml.WriteXmlIfChanged(content, targets_path, pretty=True, win32=True)
2377
+
2378
+
2379
+ def _GenerateMSBuildRuleXmlFile(xml_path, msbuild_rules):
2380
+ # Generate the .xml file
2381
+ content = [
2382
+ 'ProjectSchemaDefinitions',
2383
+ {'xmlns': ('clr-namespace:Microsoft.Build.Framework.XamlTypes;'
2384
+ 'assembly=Microsoft.Build.Framework'),
2385
+ 'xmlns:x': 'http://schemas.microsoft.com/winfx/2006/xaml',
2386
+ 'xmlns:sys': 'clr-namespace:System;assembly=mscorlib',
2387
+ 'xmlns:transformCallback':
2388
+ 'Microsoft.Cpp.Dev10.ConvertPropertyCallback'
2389
+ }
2390
+ ]
2391
+ for rule in msbuild_rules:
2392
+ content.extend([
2393
+ ['Rule',
2394
+ {'Name': rule.rule_name,
2395
+ 'PageTemplate': 'tool',
2396
+ 'DisplayName': rule.display_name,
2397
+ 'Order': '200'
2398
+ },
2399
+ ['Rule.DataSource',
2400
+ ['DataSource',
2401
+ {'Persistence': 'ProjectFile',
2402
+ 'ItemType': rule.rule_name
2403
+ }
2404
+ ]
2405
+ ],
2406
+ ['Rule.Categories',
2407
+ ['Category',
2408
+ {'Name': 'General'},
2409
+ ['Category.DisplayName',
2410
+ ['sys:String', 'General'],
2411
+ ],
2412
+ ],
2413
+ ['Category',
2414
+ {'Name': 'Command Line',
2415
+ 'Subtype': 'CommandLine'
2416
+ },
2417
+ ['Category.DisplayName',
2418
+ ['sys:String', 'Command Line'],
2419
+ ],
2420
+ ],
2421
+ ],
2422
+ ['StringListProperty',
2423
+ {'Name': 'Inputs',
2424
+ 'Category': 'Command Line',
2425
+ 'IsRequired': 'true',
2426
+ 'Switch': ' '
2427
+ },
2428
+ ['StringListProperty.DataSource',
2429
+ ['DataSource',
2430
+ {'Persistence': 'ProjectFile',
2431
+ 'ItemType': rule.rule_name,
2432
+ 'SourceType': 'Item'
2433
+ }
2434
+ ]
2435
+ ],
2436
+ ],
2437
+ ['StringProperty',
2438
+ {'Name': 'CommandLineTemplate',
2439
+ 'DisplayName': 'Command Line',
2440
+ 'Visible': 'False',
2441
+ 'IncludeInCommandLine': 'False'
2442
+ }
2443
+ ],
2444
+ ['DynamicEnumProperty',
2445
+ {'Name': rule.before_targets,
2446
+ 'Category': 'General',
2447
+ 'EnumProvider': 'Targets',
2448
+ 'IncludeInCommandLine': 'False'
2449
+ },
2450
+ ['DynamicEnumProperty.DisplayName',
2451
+ ['sys:String', 'Execute Before'],
2452
+ ],
2453
+ ['DynamicEnumProperty.Description',
2454
+ ['sys:String', 'Specifies the targets for the build customization'
2455
+ ' to run before.'
2456
+ ],
2457
+ ],
2458
+ ['DynamicEnumProperty.ProviderSettings',
2459
+ ['NameValuePair',
2460
+ {'Name': 'Exclude',
2461
+ 'Value': '^%s|^Compute' % rule.before_targets
2462
+ }
2463
+ ]
2464
+ ],
2465
+ ['DynamicEnumProperty.DataSource',
2466
+ ['DataSource',
2467
+ {'Persistence': 'ProjectFile',
2468
+ 'HasConfigurationCondition': 'true'
2469
+ }
2470
+ ]
2471
+ ],
2472
+ ],
2473
+ ['DynamicEnumProperty',
2474
+ {'Name': rule.after_targets,
2475
+ 'Category': 'General',
2476
+ 'EnumProvider': 'Targets',
2477
+ 'IncludeInCommandLine': 'False'
2478
+ },
2479
+ ['DynamicEnumProperty.DisplayName',
2480
+ ['sys:String', 'Execute After'],
2481
+ ],
2482
+ ['DynamicEnumProperty.Description',
2483
+ ['sys:String', ('Specifies the targets for the build customization'
2484
+ ' to run after.')
2485
+ ],
2486
+ ],
2487
+ ['DynamicEnumProperty.ProviderSettings',
2488
+ ['NameValuePair',
2489
+ {'Name': 'Exclude',
2490
+ 'Value': '^%s|^Compute' % rule.after_targets
2491
+ }
2492
+ ]
2493
+ ],
2494
+ ['DynamicEnumProperty.DataSource',
2495
+ ['DataSource',
2496
+ {'Persistence': 'ProjectFile',
2497
+ 'ItemType': '',
2498
+ 'HasConfigurationCondition': 'true'
2499
+ }
2500
+ ]
2501
+ ],
2502
+ ],
2503
+ ['StringListProperty',
2504
+ {'Name': 'Outputs',
2505
+ 'DisplayName': 'Outputs',
2506
+ 'Visible': 'False',
2507
+ 'IncludeInCommandLine': 'False'
2508
+ }
2509
+ ],
2510
+ ['StringProperty',
2511
+ {'Name': 'ExecutionDescription',
2512
+ 'DisplayName': 'Execution Description',
2513
+ 'Visible': 'False',
2514
+ 'IncludeInCommandLine': 'False'
2515
+ }
2516
+ ],
2517
+ ['StringListProperty',
2518
+ {'Name': 'AdditionalDependencies',
2519
+ 'DisplayName': 'Additional Dependencies',
2520
+ 'IncludeInCommandLine': 'False',
2521
+ 'Visible': 'false'
2522
+ }
2523
+ ],
2524
+ ['StringProperty',
2525
+ {'Subtype': 'AdditionalOptions',
2526
+ 'Name': 'AdditionalOptions',
2527
+ 'Category': 'Command Line'
2528
+ },
2529
+ ['StringProperty.DisplayName',
2530
+ ['sys:String', 'Additional Options'],
2531
+ ],
2532
+ ['StringProperty.Description',
2533
+ ['sys:String', 'Additional Options'],
2534
+ ],
2535
+ ],
2536
+ ],
2537
+ ['ItemType',
2538
+ {'Name': rule.rule_name,
2539
+ 'DisplayName': rule.display_name
2540
+ }
2541
+ ],
2542
+ ['FileExtension',
2543
+ {'Name': '*' + rule.extension,
2544
+ 'ContentType': rule.rule_name
2545
+ }
2546
+ ],
2547
+ ['ContentType',
2548
+ {'Name': rule.rule_name,
2549
+ 'DisplayName': '',
2550
+ 'ItemType': rule.rule_name
2551
+ }
2552
+ ]
2553
+ ])
2554
+ easy_xml.WriteXmlIfChanged(content, xml_path, pretty=True, win32=True)
2555
+
2556
+
2557
+ def _GetConfigurationAndPlatform(name, settings):
2558
+ configuration = name.rsplit('_', 1)[0]
2559
+ platform = settings.get('msvs_configuration_platform', 'Win32')
2560
+ return (configuration, platform)
2561
+
2562
+
2563
+ def _GetConfigurationCondition(name, settings):
2564
+ return (r"'$(Configuration)|$(Platform)'=='%s|%s'" %
2565
+ _GetConfigurationAndPlatform(name, settings))
2566
+
2567
+
2568
+ def _GetMSBuildProjectConfigurations(configurations):
2569
+ group = ['ItemGroup', {'Label': 'ProjectConfigurations'}]
2570
+ for (name, settings) in sorted(configurations.iteritems()):
2571
+ configuration, platform = _GetConfigurationAndPlatform(name, settings)
2572
+ designation = '%s|%s' % (configuration, platform)
2573
+ group.append(
2574
+ ['ProjectConfiguration', {'Include': designation},
2575
+ ['Configuration', configuration],
2576
+ ['Platform', platform]])
2577
+ return [group]
2578
+
2579
+
2580
+ def _GetMSBuildGlobalProperties(spec, guid, gyp_file_name):
2581
+ namespace = os.path.splitext(gyp_file_name)[0]
2582
+ return [
2583
+ ['PropertyGroup', {'Label': 'Globals'},
2584
+ ['ProjectGuid', guid],
2585
+ ['Keyword', 'Win32Proj'],
2586
+ ['RootNamespace', namespace],
2587
+ ]
2588
+ ]
2589
+
2590
+
2591
+ def _GetMSBuildConfigurationDetails(spec, build_file):
2592
+ properties = {}
2593
+ for name, settings in spec['configurations'].iteritems():
2594
+ msbuild_attributes = _GetMSBuildAttributes(spec, settings, build_file)
2595
+ condition = _GetConfigurationCondition(name, settings)
2596
+ character_set = msbuild_attributes.get('CharacterSet')
2597
+ _AddConditionalProperty(properties, condition, 'ConfigurationType',
2598
+ msbuild_attributes['ConfigurationType'])
2599
+ if character_set:
2600
+ _AddConditionalProperty(properties, condition, 'CharacterSet',
2601
+ character_set)
2602
+ return _GetMSBuildPropertyGroup(spec, 'Configuration', properties)
2603
+
2604
+
2605
+ def _GetMSBuildLocalProperties(msbuild_toolset):
2606
+ # Currently the only local property we support is PlatformToolset
2607
+ properties = {}
2608
+ if msbuild_toolset:
2609
+ properties = [
2610
+ ['PropertyGroup', {'Label': 'Locals'},
2611
+ ['PlatformToolset', msbuild_toolset],
2612
+ ]
2613
+ ]
2614
+ return properties
2615
+
2616
+
2617
+ def _GetMSBuildPropertySheets(configurations):
2618
+ user_props = r'$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props'
2619
+ additional_props = {}
2620
+ props_specified = False
2621
+ for name, settings in sorted(configurations.iteritems()):
2622
+ configuration = _GetConfigurationCondition(name, settings)
2623
+ if settings.has_key('msbuild_props'):
2624
+ additional_props[configuration] = _FixPaths(settings['msbuild_props'])
2625
+ props_specified = True
2626
+ else:
2627
+ additional_props[configuration] = ''
2628
+
2629
+ if not props_specified:
2630
+ return [
2631
+ ['ImportGroup',
2632
+ {'Label': 'PropertySheets'},
2633
+ ['Import',
2634
+ {'Project': user_props,
2635
+ 'Condition': "exists('%s')" % user_props,
2636
+ 'Label': 'LocalAppDataPlatform'
2637
+ }
2638
+ ]
2639
+ ]
2640
+ ]
2641
+ else:
2642
+ sheets = []
2643
+ for condition, props in additional_props.iteritems():
2644
+ import_group = [
2645
+ 'ImportGroup',
2646
+ {'Label': 'PropertySheets',
2647
+ 'Condition': condition
2648
+ },
2649
+ ['Import',
2650
+ {'Project': user_props,
2651
+ 'Condition': "exists('%s')" % user_props,
2652
+ 'Label': 'LocalAppDataPlatform'
2653
+ }
2654
+ ]
2655
+ ]
2656
+ for props_file in props:
2657
+ import_group.append(['Import', {'Project':props_file}])
2658
+ sheets.append(import_group)
2659
+ return sheets
2660
+
2661
+ def _ConvertMSVSBuildAttributes(spec, config, build_file):
2662
+ config_type = _GetMSVSConfigurationType(spec, build_file)
2663
+ msvs_attributes = _GetMSVSAttributes(spec, config, config_type)
2664
+ msbuild_attributes = {}
2665
+ for a in msvs_attributes:
2666
+ if a in ['IntermediateDirectory', 'OutputDirectory']:
2667
+ directory = MSVSSettings.ConvertVCMacrosToMSBuild(msvs_attributes[a])
2668
+ if not directory.endswith('\\'):
2669
+ directory += '\\'
2670
+ msbuild_attributes[a] = directory
2671
+ elif a == 'CharacterSet':
2672
+ msbuild_attributes[a] = _ConvertMSVSCharacterSet(msvs_attributes[a])
2673
+ elif a == 'ConfigurationType':
2674
+ msbuild_attributes[a] = _ConvertMSVSConfigurationType(msvs_attributes[a])
2675
+ else:
2676
+ print 'Warning: Do not know how to convert MSVS attribute ' + a
2677
+ return msbuild_attributes
2678
+
2679
+
2680
+ def _ConvertMSVSCharacterSet(char_set):
2681
+ if char_set.isdigit():
2682
+ char_set = {
2683
+ '0': 'MultiByte',
2684
+ '1': 'Unicode',
2685
+ '2': 'MultiByte',
2686
+ }[char_set]
2687
+ return char_set
2688
+
2689
+
2690
+ def _ConvertMSVSConfigurationType(config_type):
2691
+ if config_type.isdigit():
2692
+ config_type = {
2693
+ '1': 'Application',
2694
+ '2': 'DynamicLibrary',
2695
+ '4': 'StaticLibrary',
2696
+ '10': 'Utility'
2697
+ }[config_type]
2698
+ return config_type
2699
+
2700
+
2701
+ def _GetMSBuildAttributes(spec, config, build_file):
2702
+ if 'msbuild_configuration_attributes' not in config:
2703
+ msbuild_attributes = _ConvertMSVSBuildAttributes(spec, config, build_file)
2704
+
2705
+ else:
2706
+ config_type = _GetMSVSConfigurationType(spec, build_file)
2707
+ config_type = _ConvertMSVSConfigurationType(config_type)
2708
+ msbuild_attributes = config.get('msbuild_configuration_attributes', {})
2709
+ msbuild_attributes.setdefault('ConfigurationType', config_type)
2710
+ output_dir = msbuild_attributes.get('OutputDirectory',
2711
+ '$(SolutionDir)$(Configuration)')
2712
+ msbuild_attributes['OutputDirectory'] = _FixPath(output_dir) + '\\'
2713
+ if 'IntermediateDirectory' not in msbuild_attributes:
2714
+ intermediate = _FixPath('$(Configuration)') + '\\'
2715
+ msbuild_attributes['IntermediateDirectory'] = intermediate
2716
+ if 'CharacterSet' in msbuild_attributes:
2717
+ msbuild_attributes['CharacterSet'] = _ConvertMSVSCharacterSet(
2718
+ msbuild_attributes['CharacterSet'])
2719
+ if 'TargetName' not in msbuild_attributes:
2720
+ prefix = spec.get('product_prefix', '')
2721
+ product_name = spec.get('product_name', '$(ProjectName)')
2722
+ target_name = prefix + product_name
2723
+ msbuild_attributes['TargetName'] = target_name
2724
+
2725
+ if spec.get('msvs_external_builder'):
2726
+ external_out_dir = spec.get('msvs_external_builder_out_dir', '.')
2727
+ msbuild_attributes['OutputDirectory'] = _FixPath(external_out_dir) + '\\'
2728
+
2729
+ # Make sure that 'TargetPath' matches 'Lib.OutputFile' or 'Link.OutputFile'
2730
+ # (depending on the tool used) to avoid MSB8012 warning.
2731
+ msbuild_tool_map = {
2732
+ 'executable': 'Link',
2733
+ 'shared_library': 'Link',
2734
+ 'loadable_module': 'Link',
2735
+ 'static_library': 'Lib',
2736
+ }
2737
+ msbuild_tool = msbuild_tool_map.get(spec['type'])
2738
+ if msbuild_tool:
2739
+ msbuild_settings = config['finalized_msbuild_settings']
2740
+ out_file = msbuild_settings[msbuild_tool].get('OutputFile')
2741
+ if out_file:
2742
+ msbuild_attributes['TargetPath'] = _FixPath(out_file)
2743
+ target_ext = msbuild_settings[msbuild_tool].get('TargetExt')
2744
+ if target_ext:
2745
+ msbuild_attributes['TargetExt'] = target_ext
2746
+
2747
+ return msbuild_attributes
2748
+
2749
+
2750
+ def _GetMSBuildConfigurationGlobalProperties(spec, configurations, build_file):
2751
+ # TODO(jeanluc) We could optimize out the following and do it only if
2752
+ # there are actions.
2753
+ # TODO(jeanluc) Handle the equivalent of setting 'CYGWIN=nontsec'.
2754
+ new_paths = []
2755
+ cygwin_dirs = spec.get('msvs_cygwin_dirs', ['.'])[0]
2756
+ if cygwin_dirs:
2757
+ cyg_path = '$(MSBuildProjectDirectory)\\%s\\bin\\' % _FixPath(cygwin_dirs)
2758
+ new_paths.append(cyg_path)
2759
+ # TODO(jeanluc) Change the convention to have both a cygwin_dir and a
2760
+ # python_dir.
2761
+ python_path = cyg_path.replace('cygwin\\bin', 'python_26')
2762
+ new_paths.append(python_path)
2763
+ if new_paths:
2764
+ new_paths = '$(ExecutablePath);' + ';'.join(new_paths)
2765
+
2766
+ properties = {}
2767
+ for (name, configuration) in sorted(configurations.iteritems()):
2768
+ condition = _GetConfigurationCondition(name, configuration)
2769
+ attributes = _GetMSBuildAttributes(spec, configuration, build_file)
2770
+ msbuild_settings = configuration['finalized_msbuild_settings']
2771
+ _AddConditionalProperty(properties, condition, 'IntDir',
2772
+ attributes['IntermediateDirectory'])
2773
+ _AddConditionalProperty(properties, condition, 'OutDir',
2774
+ attributes['OutputDirectory'])
2775
+ _AddConditionalProperty(properties, condition, 'TargetName',
2776
+ attributes['TargetName'])
2777
+
2778
+ if attributes.get('TargetPath'):
2779
+ _AddConditionalProperty(properties, condition, 'TargetPath',
2780
+ attributes['TargetPath'])
2781
+ if attributes.get('TargetExt'):
2782
+ _AddConditionalProperty(properties, condition, 'TargetExt',
2783
+ attributes['TargetExt'])
2784
+
2785
+ if new_paths:
2786
+ _AddConditionalProperty(properties, condition, 'ExecutablePath',
2787
+ new_paths)
2788
+ tool_settings = msbuild_settings.get('', {})
2789
+ for name, value in sorted(tool_settings.iteritems()):
2790
+ formatted_value = _GetValueFormattedForMSBuild('', name, value)
2791
+ _AddConditionalProperty(properties, condition, name, formatted_value)
2792
+ return _GetMSBuildPropertyGroup(spec, None, properties)
2793
+
2794
+
2795
+ def _AddConditionalProperty(properties, condition, name, value):
2796
+ """Adds a property / conditional value pair to a dictionary.
2797
+
2798
+ Arguments:
2799
+ properties: The dictionary to be modified. The key is the name of the
2800
+ property. The value is itself a dictionary; its key is the value and
2801
+ the value a list of condition for which this value is true.
2802
+ condition: The condition under which the named property has the value.
2803
+ name: The name of the property.
2804
+ value: The value of the property.
2805
+ """
2806
+ if name not in properties:
2807
+ properties[name] = {}
2808
+ values = properties[name]
2809
+ if value not in values:
2810
+ values[value] = []
2811
+ conditions = values[value]
2812
+ conditions.append(condition)
2813
+
2814
+
2815
+ # Regex for msvs variable references ( i.e. $(FOO) ).
2816
+ MSVS_VARIABLE_REFERENCE = re.compile('\$\(([a-zA-Z_][a-zA-Z0-9_]*)\)')
2817
+
2818
+
2819
+ def _GetMSBuildPropertyGroup(spec, label, properties):
2820
+ """Returns a PropertyGroup definition for the specified properties.
2821
+
2822
+ Arguments:
2823
+ spec: The target project dict.
2824
+ label: An optional label for the PropertyGroup.
2825
+ properties: The dictionary to be converted. The key is the name of the
2826
+ property. The value is itself a dictionary; its key is the value and
2827
+ the value a list of condition for which this value is true.
2828
+ """
2829
+ group = ['PropertyGroup']
2830
+ if label:
2831
+ group.append({'Label': label})
2832
+ num_configurations = len(spec['configurations'])
2833
+ def GetEdges(node):
2834
+ # Use a definition of edges such that user_of_variable -> used_varible.
2835
+ # This happens to be easier in this case, since a variable's
2836
+ # definition contains all variables it references in a single string.
2837
+ edges = set()
2838
+ for value in sorted(properties[node].keys()):
2839
+ # Add to edges all $(...) references to variables.
2840
+ #
2841
+ # Variable references that refer to names not in properties are excluded
2842
+ # These can exist for instance to refer built in definitions like
2843
+ # $(SolutionDir).
2844
+ #
2845
+ # Self references are ignored. Self reference is used in a few places to
2846
+ # append to the default value. I.e. PATH=$(PATH);other_path
2847
+ edges.update(set([v for v in MSVS_VARIABLE_REFERENCE.findall(value)
2848
+ if v in properties and v != node]))
2849
+ return edges
2850
+ properties_ordered = gyp.common.TopologicallySorted(
2851
+ properties.keys(), GetEdges)
2852
+ # Walk properties in the reverse of a topological sort on
2853
+ # user_of_variable -> used_variable as this ensures variables are
2854
+ # defined before they are used.
2855
+ # NOTE: reverse(topsort(DAG)) = topsort(reverse_edges(DAG))
2856
+ for name in reversed(properties_ordered):
2857
+ values = properties[name]
2858
+ for value, conditions in sorted(values.iteritems()):
2859
+ if len(conditions) == num_configurations:
2860
+ # If the value is the same all configurations,
2861
+ # just add one unconditional entry.
2862
+ group.append([name, value])
2863
+ else:
2864
+ for condition in conditions:
2865
+ group.append([name, {'Condition': condition}, value])
2866
+ return [group]
2867
+
2868
+
2869
+ def _GetMSBuildToolSettingsSections(spec, configurations):
2870
+ groups = []
2871
+ for (name, configuration) in sorted(configurations.iteritems()):
2872
+ msbuild_settings = configuration['finalized_msbuild_settings']
2873
+ group = ['ItemDefinitionGroup',
2874
+ {'Condition': _GetConfigurationCondition(name, configuration)}
2875
+ ]
2876
+ for tool_name, tool_settings in sorted(msbuild_settings.iteritems()):
2877
+ # Skip the tool named '' which is a holder of global settings handled
2878
+ # by _GetMSBuildConfigurationGlobalProperties.
2879
+ if tool_name:
2880
+ if tool_settings:
2881
+ tool = [tool_name]
2882
+ for name, value in sorted(tool_settings.iteritems()):
2883
+ formatted_value = _GetValueFormattedForMSBuild(tool_name, name,
2884
+ value)
2885
+ tool.append([name, formatted_value])
2886
+ group.append(tool)
2887
+ groups.append(group)
2888
+ return groups
2889
+
2890
+
2891
+ def _FinalizeMSBuildSettings(spec, configuration):
2892
+ if 'msbuild_settings' in configuration:
2893
+ converted = False
2894
+ msbuild_settings = configuration['msbuild_settings']
2895
+ MSVSSettings.ValidateMSBuildSettings(msbuild_settings)
2896
+ else:
2897
+ converted = True
2898
+ msvs_settings = configuration.get('msvs_settings', {})
2899
+ msbuild_settings = MSVSSettings.ConvertToMSBuildSettings(msvs_settings)
2900
+ include_dirs, resource_include_dirs = _GetIncludeDirs(configuration)
2901
+ libraries = _GetLibraries(spec)
2902
+ library_dirs = _GetLibraryDirs(configuration)
2903
+ out_file, _, msbuild_tool = _GetOutputFilePathAndTool(spec, msbuild=True)
2904
+ target_ext = _GetOutputTargetExt(spec)
2905
+ defines = _GetDefines(configuration)
2906
+ if converted:
2907
+ # Visual Studio 2010 has TR1
2908
+ defines = [d for d in defines if d != '_HAS_TR1=0']
2909
+ # Warn of ignored settings
2910
+ ignored_settings = ['msvs_tool_files']
2911
+ for ignored_setting in ignored_settings:
2912
+ value = configuration.get(ignored_setting)
2913
+ if value:
2914
+ print ('Warning: The automatic conversion to MSBuild does not handle '
2915
+ '%s. Ignoring setting of %s' % (ignored_setting, str(value)))
2916
+
2917
+ defines = [_EscapeCppDefineForMSBuild(d) for d in defines]
2918
+ disabled_warnings = _GetDisabledWarnings(configuration)
2919
+ prebuild = configuration.get('msvs_prebuild')
2920
+ postbuild = configuration.get('msvs_postbuild')
2921
+ def_file = _GetModuleDefinition(spec)
2922
+ precompiled_header = configuration.get('msvs_precompiled_header')
2923
+
2924
+ # Add the information to the appropriate tool
2925
+ # TODO(jeanluc) We could optimize and generate these settings only if
2926
+ # the corresponding files are found, e.g. don't generate ResourceCompile
2927
+ # if you don't have any resources.
2928
+ _ToolAppend(msbuild_settings, 'ClCompile',
2929
+ 'AdditionalIncludeDirectories', include_dirs)
2930
+ _ToolAppend(msbuild_settings, 'ResourceCompile',
2931
+ 'AdditionalIncludeDirectories', resource_include_dirs)
2932
+ # Add in libraries, note that even for empty libraries, we want this
2933
+ # set, to prevent inheriting default libraries from the enviroment.
2934
+ _ToolSetOrAppend(msbuild_settings, 'Link', 'AdditionalDependencies',
2935
+ libraries)
2936
+ _ToolAppend(msbuild_settings, 'Link', 'AdditionalLibraryDirectories',
2937
+ library_dirs)
2938
+ if out_file:
2939
+ _ToolAppend(msbuild_settings, msbuild_tool, 'OutputFile', out_file,
2940
+ only_if_unset=True)
2941
+ if target_ext:
2942
+ _ToolAppend(msbuild_settings, msbuild_tool, 'TargetExt', target_ext,
2943
+ only_if_unset=True)
2944
+ # Add defines.
2945
+ _ToolAppend(msbuild_settings, 'ClCompile',
2946
+ 'PreprocessorDefinitions', defines)
2947
+ _ToolAppend(msbuild_settings, 'ResourceCompile',
2948
+ 'PreprocessorDefinitions', defines)
2949
+ # Add disabled warnings.
2950
+ _ToolAppend(msbuild_settings, 'ClCompile',
2951
+ 'DisableSpecificWarnings', disabled_warnings)
2952
+ # Turn on precompiled headers if appropriate.
2953
+ if precompiled_header:
2954
+ precompiled_header = os.path.split(precompiled_header)[1]
2955
+ _ToolAppend(msbuild_settings, 'ClCompile', 'PrecompiledHeader', 'Use')
2956
+ _ToolAppend(msbuild_settings, 'ClCompile',
2957
+ 'PrecompiledHeaderFile', precompiled_header)
2958
+ _ToolAppend(msbuild_settings, 'ClCompile',
2959
+ 'ForcedIncludeFiles', [precompiled_header])
2960
+ # Loadable modules don't generate import libraries;
2961
+ # tell dependent projects to not expect one.
2962
+ if spec['type'] == 'loadable_module':
2963
+ _ToolAppend(msbuild_settings, '', 'IgnoreImportLibrary', 'true')
2964
+ # Set the module definition file if any.
2965
+ if def_file:
2966
+ _ToolAppend(msbuild_settings, 'Link', 'ModuleDefinitionFile', def_file)
2967
+ configuration['finalized_msbuild_settings'] = msbuild_settings
2968
+ if prebuild:
2969
+ _ToolAppend(msbuild_settings, 'PreBuildEvent', 'Command', prebuild)
2970
+ if postbuild:
2971
+ _ToolAppend(msbuild_settings, 'PostBuildEvent', 'Command', postbuild)
2972
+
2973
+
2974
+ def _GetValueFormattedForMSBuild(tool_name, name, value):
2975
+ if type(value) == list:
2976
+ # For some settings, VS2010 does not automatically extends the settings
2977
+ # TODO(jeanluc) Is this what we want?
2978
+ if name in ['AdditionalIncludeDirectories',
2979
+ 'AdditionalLibraryDirectories',
2980
+ 'AdditionalOptions',
2981
+ 'DelayLoadDLLs',
2982
+ 'DisableSpecificWarnings',
2983
+ 'PreprocessorDefinitions']:
2984
+ value.append('%%(%s)' % name)
2985
+ # For most tools, entries in a list should be separated with ';' but some
2986
+ # settings use a space. Check for those first.
2987
+ exceptions = {
2988
+ 'ClCompile': ['AdditionalOptions'],
2989
+ 'Link': ['AdditionalOptions'],
2990
+ 'Lib': ['AdditionalOptions']}
2991
+ if tool_name in exceptions and name in exceptions[tool_name]:
2992
+ char = ' '
2993
+ else:
2994
+ char = ';'
2995
+ formatted_value = char.join(
2996
+ [MSVSSettings.ConvertVCMacrosToMSBuild(i) for i in value])
2997
+ else:
2998
+ formatted_value = MSVSSettings.ConvertVCMacrosToMSBuild(value)
2999
+ return formatted_value
3000
+
3001
+
3002
+ def _VerifySourcesExist(sources, root_dir):
3003
+ """Verifies that all source files exist on disk.
3004
+
3005
+ Checks that all regular source files, i.e. not created at run time,
3006
+ exist on disk. Missing files cause needless recompilation but no otherwise
3007
+ visible errors.
3008
+
3009
+ Arguments:
3010
+ sources: A recursive list of Filter/file names.
3011
+ root_dir: The root directory for the relative path names.
3012
+ Returns:
3013
+ A list of source files that cannot be found on disk.
3014
+ """
3015
+ missing_sources = []
3016
+ for source in sources:
3017
+ if isinstance(source, MSVSProject.Filter):
3018
+ missing_sources.extend(_VerifySourcesExist(source.contents, root_dir))
3019
+ else:
3020
+ if '$' not in source:
3021
+ full_path = os.path.join(root_dir, source)
3022
+ if not os.path.exists(full_path):
3023
+ missing_sources.append(full_path)
3024
+ return missing_sources
3025
+
3026
+
3027
+ def _GetMSBuildSources(spec, sources, exclusions, extension_to_rule_name,
3028
+ actions_spec, sources_handled_by_action, list_excluded):
3029
+ groups = ['none', 'midl', 'include', 'compile', 'resource', 'rule']
3030
+ grouped_sources = {}
3031
+ for g in groups:
3032
+ grouped_sources[g] = []
3033
+
3034
+ _AddSources2(spec, sources, exclusions, grouped_sources,
3035
+ extension_to_rule_name, sources_handled_by_action, list_excluded)
3036
+ sources = []
3037
+ for g in groups:
3038
+ if grouped_sources[g]:
3039
+ sources.append(['ItemGroup'] + grouped_sources[g])
3040
+ if actions_spec:
3041
+ sources.append(['ItemGroup'] + actions_spec)
3042
+ return sources
3043
+
3044
+
3045
+ def _AddSources2(spec, sources, exclusions, grouped_sources,
3046
+ extension_to_rule_name, sources_handled_by_action,
3047
+ list_excluded):
3048
+ extensions_excluded_from_precompile = []
3049
+ for source in sources:
3050
+ if isinstance(source, MSVSProject.Filter):
3051
+ _AddSources2(spec, source.contents, exclusions, grouped_sources,
3052
+ extension_to_rule_name, sources_handled_by_action,
3053
+ list_excluded)
3054
+ else:
3055
+ if not source in sources_handled_by_action:
3056
+ detail = []
3057
+ excluded_configurations = exclusions.get(source, [])
3058
+ if len(excluded_configurations) == len(spec['configurations']):
3059
+ detail.append(['ExcludedFromBuild', 'true'])
3060
+ else:
3061
+ for config_name, configuration in sorted(excluded_configurations):
3062
+ condition = _GetConfigurationCondition(config_name, configuration)
3063
+ detail.append(['ExcludedFromBuild',
3064
+ {'Condition': condition},
3065
+ 'true'])
3066
+ # Add precompile if needed
3067
+ for config_name, configuration in spec['configurations'].iteritems():
3068
+ precompiled_source = configuration.get('msvs_precompiled_source', '')
3069
+ if precompiled_source != '':
3070
+ precompiled_source = _FixPath(precompiled_source)
3071
+ if not extensions_excluded_from_precompile:
3072
+ # If the precompiled header is generated by a C source, we must
3073
+ # not try to use it for C++ sources, and vice versa.
3074
+ basename, extension = os.path.splitext(precompiled_source)
3075
+ if extension == '.c':
3076
+ extensions_excluded_from_precompile = ['.cc', '.cpp', '.cxx']
3077
+ else:
3078
+ extensions_excluded_from_precompile = ['.c']
3079
+
3080
+ if precompiled_source == source:
3081
+ condition = _GetConfigurationCondition(config_name, configuration)
3082
+ detail.append(['PrecompiledHeader',
3083
+ {'Condition': condition},
3084
+ 'Create'
3085
+ ])
3086
+ else:
3087
+ # Turn off precompiled header usage for source files of a
3088
+ # different type than the file that generated the
3089
+ # precompiled header.
3090
+ for extension in extensions_excluded_from_precompile:
3091
+ if source.endswith(extension):
3092
+ detail.append(['PrecompiledHeader', ''])
3093
+ detail.append(['ForcedIncludeFiles', ''])
3094
+
3095
+ group, element = _MapFileToMsBuildSourceType(source,
3096
+ extension_to_rule_name)
3097
+ grouped_sources[group].append([element, {'Include': source}] + detail)
3098
+
3099
+
3100
+ def _GetMSBuildProjectReferences(project):
3101
+ references = []
3102
+ if project.dependencies:
3103
+ group = ['ItemGroup']
3104
+ for dependency in project.dependencies:
3105
+ guid = dependency.guid
3106
+ project_dir = os.path.split(project.path)[0]
3107
+ relative_path = gyp.common.RelativePath(dependency.path, project_dir)
3108
+ project_ref = ['ProjectReference',
3109
+ {'Include': relative_path},
3110
+ ['Project', guid],
3111
+ ['ReferenceOutputAssembly', 'false']
3112
+ ]
3113
+ for config in dependency.spec.get('configurations', {}).itervalues():
3114
+ # If it's disabled in any config, turn it off in the reference.
3115
+ if config.get('msvs_2010_disable_uldi_when_referenced', 0):
3116
+ project_ref.append(['UseLibraryDependencyInputs', 'false'])
3117
+ break
3118
+ group.append(project_ref)
3119
+ references.append(group)
3120
+ return references
3121
+
3122
+
3123
+ def _GenerateMSBuildProject(project, options, version, generator_flags):
3124
+ spec = project.spec
3125
+ configurations = spec['configurations']
3126
+ project_dir, project_file_name = os.path.split(project.path)
3127
+ gyp.common.EnsureDirExists(project.path)
3128
+ # Prepare list of sources and excluded sources.
3129
+ gyp_path = _NormalizedSource(project.build_file)
3130
+ relative_path_of_gyp_file = gyp.common.RelativePath(gyp_path, project_dir)
3131
+
3132
+ gyp_file = os.path.split(project.build_file)[1]
3133
+ sources, excluded_sources = _PrepareListOfSources(spec, generator_flags,
3134
+ gyp_file)
3135
+ # Add rules.
3136
+ actions_to_add = {}
3137
+ props_files_of_rules = set()
3138
+ targets_files_of_rules = set()
3139
+ extension_to_rule_name = {}
3140
+ list_excluded = generator_flags.get('msvs_list_excluded_files', True)
3141
+
3142
+ # Don't generate rules if we are using an external builder like ninja.
3143
+ if not spec.get('msvs_external_builder'):
3144
+ _GenerateRulesForMSBuild(project_dir, options, spec,
3145
+ sources, excluded_sources,
3146
+ props_files_of_rules, targets_files_of_rules,
3147
+ actions_to_add, extension_to_rule_name)
3148
+ else:
3149
+ rules = spec.get('rules', [])
3150
+ _AdjustSourcesForRules(spec, rules, sources, excluded_sources)
3151
+
3152
+ sources, excluded_sources, excluded_idl = (
3153
+ _AdjustSourcesAndConvertToFilterHierarchy(spec, options,
3154
+ project_dir, sources,
3155
+ excluded_sources,
3156
+ list_excluded, version))
3157
+
3158
+ # Don't add actions if we are using an external builder like ninja.
3159
+ if not spec.get('msvs_external_builder'):
3160
+ _AddActions(actions_to_add, spec, project.build_file)
3161
+ _AddCopies(actions_to_add, spec)
3162
+
3163
+ # NOTE: this stanza must appear after all actions have been decided.
3164
+ # Don't excluded sources with actions attached, or they won't run.
3165
+ excluded_sources = _FilterActionsFromExcluded(
3166
+ excluded_sources, actions_to_add)
3167
+
3168
+ exclusions = _GetExcludedFilesFromBuild(spec, excluded_sources, excluded_idl)
3169
+ actions_spec, sources_handled_by_action = _GenerateActionsForMSBuild(
3170
+ spec, actions_to_add)
3171
+
3172
+ _GenerateMSBuildFiltersFile(project.path + '.filters', sources,
3173
+ extension_to_rule_name)
3174
+ missing_sources = _VerifySourcesExist(sources, project_dir)
3175
+
3176
+ for configuration in configurations.itervalues():
3177
+ _FinalizeMSBuildSettings(spec, configuration)
3178
+
3179
+ # Add attributes to root element
3180
+
3181
+ import_default_section = [
3182
+ ['Import', {'Project': r'$(VCTargetsPath)\Microsoft.Cpp.Default.props'}]]
3183
+ import_cpp_props_section = [
3184
+ ['Import', {'Project': r'$(VCTargetsPath)\Microsoft.Cpp.props'}]]
3185
+ import_cpp_targets_section = [
3186
+ ['Import', {'Project': r'$(VCTargetsPath)\Microsoft.Cpp.targets'}]]
3187
+ macro_section = [['PropertyGroup', {'Label': 'UserMacros'}]]
3188
+
3189
+ content = [
3190
+ 'Project',
3191
+ {'xmlns': 'http://schemas.microsoft.com/developer/msbuild/2003',
3192
+ 'ToolsVersion': version.ProjectVersion(),
3193
+ 'DefaultTargets': 'Build'
3194
+ }]
3195
+
3196
+ content += _GetMSBuildProjectConfigurations(configurations)
3197
+ content += _GetMSBuildGlobalProperties(spec, project.guid, project_file_name)
3198
+ content += import_default_section
3199
+ content += _GetMSBuildConfigurationDetails(spec, project.build_file)
3200
+ content += _GetMSBuildLocalProperties(project.msbuild_toolset)
3201
+ content += import_cpp_props_section
3202
+ content += _GetMSBuildExtensions(props_files_of_rules)
3203
+ content += _GetMSBuildPropertySheets(configurations)
3204
+ content += macro_section
3205
+ content += _GetMSBuildConfigurationGlobalProperties(spec, configurations,
3206
+ project.build_file)
3207
+ content += _GetMSBuildToolSettingsSections(spec, configurations)
3208
+ content += _GetMSBuildSources(
3209
+ spec, sources, exclusions, extension_to_rule_name, actions_spec,
3210
+ sources_handled_by_action, list_excluded)
3211
+ content += _GetMSBuildProjectReferences(project)
3212
+ content += import_cpp_targets_section
3213
+ content += _GetMSBuildExtensionTargets(targets_files_of_rules)
3214
+
3215
+ if spec.get('msvs_external_builder'):
3216
+ content += _GetMSBuildExternalBuilderTargets(spec)
3217
+
3218
+ # TODO(jeanluc) File a bug to get rid of runas. We had in MSVS:
3219
+ # has_run_as = _WriteMSVSUserFile(project.path, version, spec)
3220
+
3221
+ easy_xml.WriteXmlIfChanged(content, project.path, pretty=True, win32=True)
3222
+
3223
+ return missing_sources
3224
+
3225
+
3226
+ def _GetMSBuildExternalBuilderTargets(spec):
3227
+ """Return a list of MSBuild targets for external builders.
3228
+
3229
+ Right now, only "Build" and "Clean" targets are generated.
3230
+
3231
+ Arguments:
3232
+ spec: The gyp target spec.
3233
+ Returns:
3234
+ List of MSBuild 'Target' specs.
3235
+ """
3236
+ build_cmd = _BuildCommandLineForRuleRaw(
3237
+ spec, spec['msvs_external_builder_build_cmd'],
3238
+ False, False, False, False)
3239
+ build_target = ['Target', {'Name': 'Build'}]
3240
+ build_target.append(['Exec', {'Command': build_cmd}])
3241
+
3242
+ clean_cmd = _BuildCommandLineForRuleRaw(
3243
+ spec, spec['msvs_external_builder_clean_cmd'],
3244
+ False, False, False, False)
3245
+ clean_target = ['Target', {'Name': 'Clean'}]
3246
+ clean_target.append(['Exec', {'Command': clean_cmd}])
3247
+
3248
+ return [build_target, clean_target]
3249
+
3250
+
3251
+ def _GetMSBuildExtensions(props_files_of_rules):
3252
+ extensions = ['ImportGroup', {'Label': 'ExtensionSettings'}]
3253
+ for props_file in props_files_of_rules:
3254
+ extensions.append(['Import', {'Project': props_file}])
3255
+ return [extensions]
3256
+
3257
+
3258
+ def _GetMSBuildExtensionTargets(targets_files_of_rules):
3259
+ targets_node = ['ImportGroup', {'Label': 'ExtensionTargets'}]
3260
+ for targets_file in sorted(targets_files_of_rules):
3261
+ targets_node.append(['Import', {'Project': targets_file}])
3262
+ return [targets_node]
3263
+
3264
+
3265
+ def _GenerateActionsForMSBuild(spec, actions_to_add):
3266
+ """Add actions accumulated into an actions_to_add, merging as needed.
3267
+
3268
+ Arguments:
3269
+ spec: the target project dict
3270
+ actions_to_add: dictionary keyed on input name, which maps to a list of
3271
+ dicts describing the actions attached to that input file.
3272
+
3273
+ Returns:
3274
+ A pair of (action specification, the sources handled by this action).
3275
+ """
3276
+ sources_handled_by_action = OrderedSet()
3277
+ actions_spec = []
3278
+ for primary_input, actions in actions_to_add.iteritems():
3279
+ inputs = OrderedSet()
3280
+ outputs = OrderedSet()
3281
+ descriptions = []
3282
+ commands = []
3283
+ for action in actions:
3284
+ inputs.update(OrderedSet(action['inputs']))
3285
+ outputs.update(OrderedSet(action['outputs']))
3286
+ descriptions.append(action['description'])
3287
+ cmd = action['command']
3288
+ # For most actions, add 'call' so that actions that invoke batch files
3289
+ # return and continue executing. msbuild_use_call provides a way to
3290
+ # disable this but I have not seen any adverse effect from doing that
3291
+ # for everything.
3292
+ if action.get('msbuild_use_call', True):
3293
+ cmd = 'call ' + cmd
3294
+ commands.append(cmd)
3295
+ # Add the custom build action for one input file.
3296
+ description = ', and also '.join(descriptions)
3297
+
3298
+ # We can't join the commands simply with && because the command line will
3299
+ # get too long. See also _AddActions: cygwin's setup_env mustn't be called
3300
+ # for every invocation or the command that sets the PATH will grow too
3301
+ # long.
3302
+ command = (
3303
+ '\r\nif %errorlevel% neq 0 exit /b %errorlevel%\r\n'.join(commands))
3304
+ _AddMSBuildAction(spec,
3305
+ primary_input,
3306
+ inputs,
3307
+ outputs,
3308
+ command,
3309
+ description,
3310
+ sources_handled_by_action,
3311
+ actions_spec)
3312
+ return actions_spec, sources_handled_by_action
3313
+
3314
+
3315
+ def _AddMSBuildAction(spec, primary_input, inputs, outputs, cmd, description,
3316
+ sources_handled_by_action, actions_spec):
3317
+ command = MSVSSettings.ConvertVCMacrosToMSBuild(cmd)
3318
+ primary_input = _FixPath(primary_input)
3319
+ inputs_array = _FixPaths(inputs)
3320
+ outputs_array = _FixPaths(outputs)
3321
+ additional_inputs = ';'.join([i for i in inputs_array
3322
+ if i != primary_input])
3323
+ outputs = ';'.join(outputs_array)
3324
+ sources_handled_by_action.add(primary_input)
3325
+ action_spec = ['CustomBuild', {'Include': primary_input}]
3326
+ action_spec.extend(
3327
+ # TODO(jeanluc) 'Document' for all or just if as_sources?
3328
+ [['FileType', 'Document'],
3329
+ ['Command', command],
3330
+ ['Message', description],
3331
+ ['Outputs', outputs]
3332
+ ])
3333
+ if additional_inputs:
3334
+ action_spec.append(['AdditionalInputs', additional_inputs])
3335
+ actions_spec.append(action_spec)