1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
|
#!/usr/bin/python2.4
# Copyright 2008, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Visual Studio solution output of component targets for SCons."""
import copy
import md5
import os
import sys
import xml.dom
import xml.dom.minidom
import SCons
#------------------------------------------------------------------------------
def MakeGuid(name, seed='component_targets_msvs'):
"""Returns a GUID for the specified target name.
Args:
name: Target name.
seed: Seed for MD5 hash.
Returns:
A GUID-line string calculated from the name and seed.
This generates something which looks like a GUID, but depends only on the
name and seed. This means the same name/seed will always generate the same
GUID, so that projects and solutions which refer to each other can explicitly
determine the GUID to refer to explicitly. It also means that the GUID will
not change when the project for a target is rebuilt.
"""
# Calculate a MD5 signature for the seed and name.
d = md5.new(str(seed) + str(name)).hexdigest().upper()
# Convert most of the signature to GUID form (discard the rest)
guid = ('{' + d[:8] + '-' + d[8:12] + '-' + d[12:16] + '-' + d[16:20]
+ '-' + d[20:32] + '}')
return guid
#------------------------------------------------------------------------------
def GetGuidAndNameFromVSProject(project_path):
"""Reads the GUID from a Visual Studio project file.
Args:
project_path: Path to the Visual Studio project file.
Returns:
The GUID string from the file.
The project name from the file.
"""
doc = xml.dom.minidom.parse(project_path)
try:
n_root = doc.documentElement
if n_root.nodeName != 'VisualStudioProject':
raise SCons.Errors.UserError('%s is not a Visual Studio project.' %
project_path)
return (
str(n_root.attributes['ProjectGUID'].nodeValue),
str(n_root.attributes['Name'].nodeValue),
)
finally:
# Clean up doc
doc.unlink()
#------------------------------------------------------------------------------
class VSProjectWriter(object):
"""Visual Studio XML project writer."""
def __init__(self, project_path):
"""Initializes the project.
Args:
project_path: Path to the project file.
"""
self.project_path = project_path
self.doc = None
def Create(self, name):
"""Creates the project document.
Args:
name: Name of the project.
"""
self.name = name
self.guid = MakeGuid(name)
# Create XML doc
xml_impl = xml.dom.getDOMImplementation()
self.doc = xml_impl.createDocument(None, 'VisualStudioProject', None)
# Add attributes to root element
self.n_root = self.doc.documentElement
self.n_root.setAttribute('ProjectType', 'Visual C++')
self.n_root.setAttribute('Version', '8.00')
self.n_root.setAttribute('Name', self.name)
self.n_root.setAttribute('ProjectGUID', self.guid)
self.n_root.setAttribute('RootNamespace', self.name)
self.n_root.setAttribute('Keyword', 'MakeFileProj')
# Add platform list
n_platform = self.doc.createElement('Platforms')
self.n_root.appendChild(n_platform)
n = self.doc.createElement('Platform')
n.setAttribute('Name', 'Win32')
n_platform.appendChild(n)
# Add empty ToolFiles section
self.n_root.appendChild(self.doc.createElement('ToolFiles'))
# Add configurations section
self.n_configs = self.doc.createElement('Configurations')
self.n_root.appendChild(self.n_configs)
# Add files section
self.n_files = self.doc.createElement('Files')
self.n_root.appendChild(self.n_files)
# Add empty Globals section
self.n_root.appendChild(self.doc.createElement('Globals'))
def AddConfig(self, name, attrs, tool_attrs):
"""Adds a configuration to the project.
Args:
name: Configuration name.
attrs: Dict of configuration attributes.
tool_attrs: Dict of tool attributes.
"""
# Add configuration node
n_config = self.doc.createElement('Configuration')
n_config.setAttribute('Name', '%s|Win32' % name)
n_config.setAttribute('ConfigurationType', '0')
for k, v in attrs.items():
n_config.setAttribute(k, v)
self.n_configs.appendChild(n_config)
# Add tool node
n_tool = self.doc.createElement('Tool')
n_tool.setAttribute('Name', 'VCNMakeTool')
n_tool.setAttribute('IncludeSearchPath', '')
n_tool.setAttribute('ForcedIncludes', '')
n_tool.setAttribute('AssemblySearchPath', '')
n_tool.setAttribute('ForcedUsingAssemblies', '')
n_tool.setAttribute('CompileAsManaged', '')
n_tool.setAttribute('PreprocessorDefinitions', '')
for k, v in tool_attrs.items():
n_tool.setAttribute(k, v)
n_config.appendChild(n_tool)
def _WalkFolders(self, folder_dict, parent):
"""Recursively walks the folder tree.
Args:
folder_dict: Dict of folder entries. Entry is
either subdir_name:subdir_dict or relative_path_to_file:None.
parent: Parent node (folder node for that dict)
"""
entries = folder_dict.keys()
entries.sort()
for e in entries:
if folder_dict[e]:
# Folder
n_subfolder = self.doc.createElement('Filter')
n_subfolder.setAttribute('Name', e)
parent.appendChild(n_subfolder)
self._WalkFolders(folder_dict[e], n_subfolder)
else:
# File
n_file = self.doc.createElement('File')
n_file.setAttribute('RelativePath', e)
parent.appendChild(n_file)
def AddFiles(self, name, files_dict):
"""Adds files to the project.
Args:
name: Name of the folder. If None, files/folders will be added directly
to the files list.
files_dict: A dict of files / folders.
Within the files_dict:
* A file entry is relative_path:None
* A folder entry is folder_name:files_dict, where files_dict is another
dict of this form.
"""
# Create subfolder if necessary
if name:
n_folder = self.doc.createElement('Filter')
n_folder.setAttribute('Name', name)
self.n_files.appendChild(n_folder)
else:
n_folder = self.n_files
# Recursively add files to the folder
self._WalkFolders(files_dict, n_folder)
def Write(self):
"""Writes the project file."""
f = open(self.project_path, 'wt')
self.doc.writexml(f, encoding='Windows-1252', addindent=' ', newl='\n')
f.close()
#------------------------------------------------------------------------------
def ComponentVSProjectBuilder(target, source, env):
"""Visual Studio project builder.
Args:
target: Destination file.
source: List of sources to be added to the target.
env: Environment context.
Returns:
Zero if successful.
"""
source = source # Silence gpylint
target_name = env['TARGET_NAME']
project_file = target[0].path
project_to_main = env.RelativePath(target[0].dir, env.Dir('$MAIN_DIR'),
sep='/')
env_hammer_bat = env.Clone(VS_PROJECT_TO_MAIN_DIR=project_to_main)
hammer_bat = env_hammer_bat.subst('$COMPONENT_VS_PROJECT_SCRIPT_PATH', raw=1)
vsp = VSProjectWriter(project_file)
vsp.Create(target_name)
# Add configuration per build mode supported by this target
target_path = env['TARGET_PATH']
for mode, path in target_path.items():
attrs = {}
attrs['OutputDirectory'] = '$(ProjectDir)/%s/%s/out' % (mode, target_name)
attrs['IntermediateDirectory'] = ('$(ProjectDir)/%s/%s/tmp' %
(mode, target_name))
tool_attrs = {}
if path:
tool_attrs['Output'] = env.RelativePath(target[0].dir,
env.Entry(path), sep='/')
build_cmd = '%s --mode=%s %s' % (hammer_bat, mode, target_name)
clean_cmd = '%s --mode=%s -c %s' % (hammer_bat, mode, target_name)
tool_attrs['BuildCommandLine'] = build_cmd
tool_attrs['CleanCommandLine'] = clean_cmd
tool_attrs['ReBuildCommandLine'] = clean_cmd + ' && ' + build_cmd
vsp.AddConfig(mode, attrs, tool_attrs)
# TODO(rspangler): Fill in files - at least, the .scons file invoking the
# target.
# Write project
vsp.Write()
return 0
def ComponentVSProject(self, target_name, **kwargs):
"""Visual Studio project pseudo-builder for the specified target.
Args:
self: Environment context.
target_name: Name of the target.
kwargs: Optional keyword arguments override environment variables in the
derived environment used to create the project.
Returns:
A list of output nodes.
"""
# Builder only works on Windows
if sys.platform not in ('win32', 'cygwin'):
return []
# Clone environment and add keyword args
env = self.Clone()
for k, v in kwargs.items():
env[k] = v
# Save the target name
env['TARGET_NAME'] = target_name
# Extract the target properties and save in the environment for use by the
# real builder.
t = GetTargets().get(target_name)
env['TARGET_PATH'] = {}
if t:
for mode, mode_properties in t.mode_properties.items():
# Since the target path is what Visual Studio will run, use the EXE
# property in preference to TARGET_PATH.
target_path = mode_properties.get('EXE',
mode_properties.get('TARGET_PATH'))
env.Append(TARGET_PATH={mode: target_path})
else:
# No modes declared for this target. Could be a custom alias created by
# a SConscript, rather than a component builder. Assume it can be built in
# all modes, but produces no output.
for mode in GetTargetModes():
env.Append(TARGET_PATH={mode: None})
# Call the real builder
return env.ComponentVSProjectBuilder(
'$COMPONENT_VS_PROJECT_DIR/${TARGET_NAME}', [])
#------------------------------------------------------------------------------
class SourceWalker(object):
"""Iterator for walking a node tree and collecting sources.
This is depth-first, children are visited before the parent. The object
can be initialized with any node, and returns the next node on the descent
with each Next() call.
This class does not get caught in node cycles caused, for example, by C
header file include loops.
Based on SCons.Node.Walker.
"""
def __init__(self, node, seen, print_interval = 1000):
"""Initializes the source walker.
Args:
node: Node to start walk from.
seen: Set to add seen nodes to, if not None.
print_interval: Interval in nodes examined at which to print a progress
indicator.
"""
self.interval = print_interval
# Put node on stack
self.stack = [node]
# Scan for node's children, then copy them to node.wkids
node.wkids = copy.copy(node.children(scan=1))
# Keep track of nodes we've seen (returned)
if seen is None:
seen = set()
self.seen = seen
# Add node to history for cycle detection
self.history = set()
self.history.add(node)
# We've seen one node now
self.nodes_examined = 1
self.unique_nodes = 1
def Next(self):
"""Get the next node for this walk of the tree.
Returns:
The next node, or None if no more nodes.
This function is intentionally iterative, not recursive, to sidestep any
issues of stack size limitations.
"""
while self.stack:
if self.stack[-1].wkids:
# Node has children we haven't examined, so iterate into the first
# child
node = self.stack[-1].wkids.pop(0)
if not self.stack[-1].wkids:
# No more children of this node
self.stack[-1].wkids = None
self.nodes_examined += 1
if self.interval and not self.nodes_examined % self.interval:
self.PrintProgress()
if (node not in self.history) and (node not in self.seen):
# Haven't hit a cycle or a node we've already seen
node.wkids = copy.copy(node.children(scan=1))
self.stack.append(node)
self.history.add(node)
else:
# Coming back from iterating, so return the next node on the stack.
node = self.stack.pop()
self.history.remove(node)
self.seen.add(node)
self.unique_nodes += 1
return node
return None
def PrintProgress(self):
"""Prints a progress indicator."""
print ' Examined %d nodes, found %d unique...' % (
self.nodes_examined, self.unique_nodes)
def WalkAll(self):
"""Walks all nodes in the the tree."""
while self.Next():
pass
if self.interval:
self.PrintProgress()
def ComponentVSSourceProjectBuilder(target, source, env):
"""Visual Studio source project builder.
Args:
target: Destination file.
source: List of sources to be added to the target.
env: Environment context.
Returns:
Zero if successful.
"""
source = source # Silence gpylint
target_name = env['PROJECT_NAME']
project_file = target[0].path
project_dir = target[0].dir
# Get list of suffixes to include
suffixes = env.SubstList2('$COMPONENT_VS_SOURCE_SUFFIXES')
# Convert source folders to absolute paths
folders = []
for f in env['COMPONENT_VS_SOURCE_FOLDERS']:
# (folder name, folder abspath, dict of contents)
folders.append((f[0], env.Dir(f[1]).abspath, {}))
# TODO(rspangler): Additional enhancements:
# * Should be able to specify paths in folder name (i.e., foo/bar) and
# create the nested folder nodes ('foo' and 'bar')
# * Should be tolerant of a folder being specified more than once with
# the same name (probably necessary to support nested folder nodes anyway)
# Can probably accomplish both of those by creating a parent fodler dict and
# calling WalkFolders() only once.
# Create a temporary solution alias to point to all the targets, so we can
# make a single call to SourceWalker()
tmp_alias = env.Alias('vs_source_project_' + target_name,
map(env.Alias, env['COMPONENT_VS_SOURCE_TARGETS']))
# Scan all targets and add unique nodes to set of sources
print ' Scanning dependency tree ...'
all_srcs = set()
walker = SourceWalker(tmp_alias[0], all_srcs)
walker.WalkAll()
# Walk all sources and build directory trees
print ' Building source tree...'
for n in all_srcs:
if not hasattr(n, 'isfile') or not n.isfile():
continue # Not a file
if n.has_builder():
continue # Not a leaf node
if n.suffix not in suffixes:
continue # Not a file type we include
path = n.rfile().abspath
for f in folders:
if path.startswith(f[1]):
if f[0] is None:
# Folder name of None is a filter
break
relpath = path[len(f[1]) + 1:].split(os.sep)
folder_dict = f[2]
# Recursively add subdirs
for pathseg in relpath[:-1]:
if pathseg not in folder_dict:
folder_dict[pathseg] = {}
folder_dict = folder_dict[pathseg]
# Add file to last subdir. No dict, since this isn't a subdir
folder_dict[env.RelativePath(project_dir, path)] = None
break
print ' Writing project file...'
vsp = VSProjectWriter(project_file)
vsp.Create(target_name)
# One configuration for all build modes
vsp.AddConfig('all', {}, {})
# Add files
for f in folders:
if f[0] is None:
continue # Skip filters
vsp.AddFiles(f[0], f[2])
vsp.Write()
return 0
def ComponentVSSourceProject(self, project_name, target_names, **kwargs):
"""Visual Studio source project pseudo-builder.
Args:
self: Environment context.
project_name: Name of the project.
target_names: List of target names to include source for.
kwargs: Optional keyword arguments override environment variables in the
derived environment used to create the project.
Returns:
A list of output nodes.
"""
# Builder only works on Windows
if sys.platform not in ('win32', 'cygwin'):
return []
# Clone environment and add keyword args
env = self.Clone()
for k, v in kwargs.items():
env[k] = v
# Save the project name and targets
env['PROJECT_NAME'] = project_name
env['COMPONENT_VS_SOURCE_TARGETS'] = target_names
# Call the real builder
return env.ComponentVSSourceProjectBuilder(
'$COMPONENT_VS_PROJECT_DIR/${PROJECT_NAME}', [])
#------------------------------------------------------------------------------
def FindSources(env, dest, source, suffixes=None):
"""Recursively finds sources and adds them to the destination set.
Args:
env: Environment context.
dest: Set to add source nodes to.
source: Source file(s) to find. May be a string, Node, or a list of
mixed strings or Nodes. Strings will be passed through env.Glob() to
evaluate wildcards. If a source evaluates to a directory, the entire
directory will be recursively added.
suffixes: List of suffixes to include. If not None, only files with these
suffixes will be added to dest.
"""
for source_entry in env.Flatten(source):
if type(source_entry) == str:
# Search for matches for each source entry
source_nodes = env.Glob(source_entry)
else:
# Source entry is already a file or directory node; no need to glob it
source_nodes = [source_entry]
for s in source_nodes:
if str(s.__class__) == 'SCons.Node.FS.Dir':
# Recursively search subdir. Since glob('*') doesn't match dot files,
# also glob('.*').
FindSources(env, dest, [s.abspath + '/*', s.abspath + '/.*'], suffixes)
elif suffixes and s.suffix in suffixes:
dest.add(s)
def ComponentVSDirProjectBuilder(target, source, env):
"""Visual Studio directory project builder.
Args:
target: Destination file.
source: List of sources to be added to the target.
env: Environment context.
Returns:
Zero if successful.
"""
source = source # Silence gpylint
target_name = env['PROJECT_NAME']
project_file = target[0].path
project_dir = target[0].dir
# Convert source folders to absolute paths
folders = []
for f in env['COMPONENT_VS_SOURCE_FOLDERS']:
# (folder name, folder abspath, dict of contents)
folders.append((f[0], env.Dir(f[1]).abspath, {}))
# Recursively scan source directories
print ' Scanning directories for source...'
all_srcs = set()
FindSources(env, all_srcs, env['PROJECT_SOURCES'],
suffixes=env.SubstList2('$COMPONENT_VS_SOURCE_SUFFIXES'))
# Walk all sources and build directory trees
print ' Building source tree...'
for n in all_srcs:
# Map addRepository'd source to its real location.
path = n.rfile().abspath
for f in folders:
if path.startswith(f[1]):
if f[0] is None:
# Folder name of None is a filter
break
relpath = path[len(f[1]) + 1:].split(os.sep)
folder_dict = f[2]
# Recursively add subdirs
for pathseg in relpath[:-1]:
if pathseg not in folder_dict:
folder_dict[pathseg] = {}
folder_dict = folder_dict[pathseg]
# Add file to last subdir. No dict, since this isn't a subdir
folder_dict[env.RelativePath(project_dir, path)] = None
break
print ' Writing project file...'
vsp = VSProjectWriter(project_file)
vsp.Create(target_name)
# One configuration for all build modes
vsp.AddConfig('all', {}, {})
# Add files
for f in folders:
if f[0] is None:
continue # Skip filters
vsp.AddFiles(f[0], f[2])
vsp.Write()
return 0
def ComponentVSDirProject(self, project_name, source, **kwargs):
"""Visual Studio directory project pseudo-builder.
Args:
self: Environment context.
project_name: Name of the project.
source: List of source files and/or directories.
kwargs: Optional keyword arguments override environment variables in the
derived environment used to create the project.
Returns:
A list of output nodes.
"""
# Builder only works on Windows
if sys.platform not in ('win32', 'cygwin'):
return []
# Clone environment and add keyword args
env = self.Clone()
for k, v in kwargs.items():
env[k] = v
# Save the project name and sources
env['PROJECT_NAME'] = project_name
env['PROJECT_SOURCES'] = source
# Call the real builder
return env.ComponentVSDirProjectBuilder(
'$COMPONENT_VS_PROJECT_DIR/${PROJECT_NAME}', [])
#------------------------------------------------------------------------------
def ComponentVSSolutionBuilder(target, source, env):
"""Visual Studio solution builder.
Args:
target: Destination file.
source: List of sources to be added to the target.
env: Environment context.
Returns:
Zero if successful.
"""
source = source # Silence gpylint
solution_file = target[0].path
projects = env['SOLUTION_PROJECTS']
folders = env['SOLUTION_FOLDERS']
# Scan externally-generated projects
external_projects = []
for p in source:
guid, name = GetGuidAndNameFromVSProject(p.abspath)
external_projects.append((p, name, guid))
f = open(solution_file, 'wt')
f.write('Microsoft Visual Studio Solution File, Format Version 9.00\n')
f.write('# Visual Studio 2005\n')
# Projects generated by ComponentVSSolution()
for p in projects:
project_file = env.File(
'$COMPONENT_VS_PROJECT_DIR/%s$COMPONENT_VS_PROJECT_SUFFIX' % p)
f.write('Project("%s") = "%s", "%s", "%s"\nEndProject\n' % (
'{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}', # External makefile GUID
p, # Project name
env.RelativePath(target[0].dir, project_file), # Path to project file
MakeGuid(p), # Project GUID
))
# Projects generated elsewhere
for p, name, guid in external_projects:
f.write('Project("%s") = "%s", "%s", "%s"\nEndProject\n' % (
# TODO(rspangler): What if this project isn't type external makefile?
# How to tell what type it is?
'{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}', # External makefile GUID
name, # Project name
env.RelativePath(target[0].dir, p), # Path to project file
guid, # Project GUID
))
# Folders from build groups
# TODO(rspangler): Currently no way to add external project (specified in
# sources) to a solution folder.
for folder in folders:
f.write('Project("%s") = "%s", "%s", "%s"\nEndProject\n' % (
'{2150E333-8FDC-42A3-9474-1A3956D46DE8}', # Solution folder GUID
folder, # Folder name
folder, # Folder name (again)
# Use a different seed so the folder won't get the same GUID as a
# project.
MakeGuid(folder, seed='folder'), # Project GUID
))
f.write('\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n')
for mode in GetTargetModes():
f.write('\t\t%s|Win32 = %s|Win32\n' % (mode, mode))
f.write('\tEndGlobalSection\n')
# Determine which projects should be enabled
# TODO(rspangler): This is somewhat clunky. DEFAULT_TARGETS is global, and
# what we really need is something mode-specific. In theory we could make
# this a mode-specific dict rather than a list, but that'd also be a pain to
# populate.
# These variable names are also getting REALLY long. Perhaps we should
# define shorter ones (with the default value redirecting to the longer
# ones for legacy compatibility).
enable_projects = env.SubstList2('$COMPONENT_VS_ENABLED_PROJECTS')
f.write('\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\n')
# Projects generated by ComponentVSSolution()
for p in projects:
for mode in GetTargetModes():
f.write('\t\t%s.%s|Win32.ActiveCfg = %s|Win32\n' % (
MakeGuid(p), # Project GUID
mode, # Solution build configuration
mode, # Project build config for that solution config
))
t = GetTargets().get(p)
# Determine if project should be enabled in this mode
enabled = t and mode in t.mode_properties
if enable_projects and p not in enable_projects:
# Enable list specified, but target isn't in it
# TODO(rspangler): Since we env.Default(scons-out) elsewhere, this
# likely causes all projects to be disabled by default. But that's
# realistically better than enabling them all...
enabled = False
if enabled:
# Target can be built in this mode
f.write('\t\t%s.%s|Win32.Build.0 = %s|Win32\n' % (
MakeGuid(p), # Project GUID
mode, # Solution build configuration
mode, # Project build config for that solution config
))
# Projects generated elsewhere
for p, name, guid in external_projects:
for mode in GetTargetModes():
f.write('\t\t%s.%s|Win32.ActiveCfg = %s|Win32\n' % (
guid, # Project GUID
mode, # Solution build configuration
mode, # Project build config for that solution config
))
if name in enable_projects or not enable_projects:
# Build target in this mode
f.write('\t\t%s.%s|Win32.Build.0 = %s|Win32\n' % (
guid, # Project GUID
mode, # Solution build configuration
mode, # Project build config for that solution config
))
f.write('\tEndGlobalSection\n')
f.write('\tGlobalSection(SolutionProperties) = preSolution\n')
f.write('\t\tHideSolutionNode = FALSE\n')
f.write('\tEndGlobalSection\n')
if folders:
f.write('\tGlobalSection(NestedProjects) = preSolution\n')
for p, folder in projects.items():
f.write('\t\t%s = %s\n' % (MakeGuid(p), MakeGuid(folder, seed='folder')))
f.write('\tEndGlobalSection\n')
f.write('EndGlobal\n')
f.close()
return 0
def ComponentVSSolution(self, solution_name, target_names, projects=None,
**kwargs):
"""Visual Studio solution pseudo-builder.
Args:
self: Environment context.
solution_name: Name of the solution.
target_names: Names of targets or target groups to include in the solution.
This will automatically build projects for them.
projects: List of aditional projects not generated by this solution to
include in the solution.
kwargs: Optional keyword arguments override environment variables in the
derived environment used to create the solution.
Returns:
The list of output nodes.
"""
# TODO(rspangler): Should have option to build source project also. Perhaps
# select using a --source_project option, since it needs to use gather_inputs
# to scan the DAG and will blow up the null build time.
# TODO(rspangler): Should also have autobuild_projects option. If false,
# don't build them.
# TODO(rspangler): Should also be able to specify a target group directly
# (i.e. 'run_all_tests')
# Builder only works on Windows
if sys.platform not in ('win32', 'cygwin'):
return []
# Clone environment and add keyword args
env = self.Clone()
for k, v in kwargs.items():
env[k] = v
# Save the target name
env['SOLUTION_NAME'] = solution_name
# Get list of targets to make projects for. At this point we haven't
# determined whether they're groups or targets.
target_names = env.SubstList2(target_names)
env['SOLUTION_TARGETS'] = target_names
# Save the default targets list as an environment variable
env['COMPONENT_VS_SCONS_DEFAULT_TARGETS'] = SCons.Script.DEFAULT_TARGETS
# Expand target_names into project names, and create project-to-folder
# mappings
project_names = {}
folders = []
if target_names:
# Expand target_names into project names
for target in target_names:
if target in GetTargetGroups():
# Add target to folders
folders.append(target)
# Add all project_names in the group
for t in GetTargetGroups()[target].GetTargetNames():
project_names[t] = target
elif target in GetTargets():
# Just a target name
project_names[target] = None
else:
print 'Warning: ignoring unknown target "%s"' % target
else:
# No target names specified, so use all projects
for t in GetTargets():
project_names[t] = None
env['SOLUTION_FOLDERS'] = folders
env['SOLUTION_PROJECTS'] = project_names
# Call the real builder
out_nodes = env.ComponentVSSolutionBuilder(
'$COMPONENT_VS_SOLUTION_DIR/${SOLUTION_NAME}', projects or [])
# Call the real builder for the projects we generate
for p in project_names:
out_nodes += env.ComponentVSProject(p)
# Add the solution target
# TODO(rspangler): Should really defer the rest of the work above, since
# otherwise we can't build a solution which has a target to rebuild itself.
env.Alias('all_solutions', env.Alias(solution_name, out_nodes))
# TODO(rspangler): To rebuild the solution properly, need to override its
# project configuration so it only has '--mode=all' (or some other way of
# setting the subset of modes which it should use to rebuild itself).
# Rebuilding with the property below will strip it down to just the current
# build mode, which isn't what we want.
# Let component_targets know this target is available in the current mode
#self.SetTargetProperty(solution_name, TARGET_PATH=out_nodes[0])
return out_nodes
#------------------------------------------------------------------------------
def generate(env):
# NOTE: SCons requires the use of this name, which fails gpylint.
"""SCons entry point for this tool."""
# Add pseudo-builders to set up the project and solution builders. These
# need to be available on all platforms so that SConscripts which reference
# them will work.
env.AddMethod(ComponentVSDirProject)
env.AddMethod(ComponentVSProject)
env.AddMethod(ComponentVSSolution)
env.AddMethod(ComponentVSSourceProject)
# If not on Windows, don't do anything else
if sys.platform not in ('win32', 'cygwin'):
return
# Include tools we need
env.Tool('gather_inputs')
env.SetDefault(
COMPONENT_VS_PROJECT_DIR='$COMPONENT_VS_SOLUTION_DIR/projects',
COMPONENT_VS_PROJECT_SCRIPT_NAME = 'hammer.bat',
COMPONENT_VS_PROJECT_SCRIPT_PATH = (
'$$(ProjectDir)/$VS_PROJECT_TO_MAIN_DIR/'
'$COMPONENT_VS_PROJECT_SCRIPT_NAME'),
COMPONENT_VS_PROJECT_SUFFIX='.vcproj',
COMPONENT_VS_SOLUTION_DIR='$DESTINATION_ROOT/solution',
COMPONENT_VS_SOLUTION_SUFFIX='.sln',
COMPONENT_VS_ENABLED_PROJECTS=['$COMPONENT_VS_SCONS_DEFAULT_TARGETS'],
COMPONENT_VS_SOURCE_SUFFIXES=['$CPPSUFFIXES', '.rc', '.scons'],
COMPONENT_VS_SOURCE_FOLDERS=[('source', '$MAIN_DIR')],
)
AddTargetGroup('all_solutions', 'solutions can be built')
# Add builders
vcprojaction = SCons.Script.Action(ComponentVSProjectBuilder, varlist=[
'COMPONENT_VS_PROJECT_SCRIPT_PATH',
'TARGET_NAME',
'TARGET_PATH',
])
vcprojbuilder = SCons.Script.Builder(
action=vcprojaction,
suffix='$COMPONENT_VS_PROJECT_SUFFIX')
source_vcproj_action = SCons.Script.Action(
ComponentVSSourceProjectBuilder, varlist=[
'COMPONENT_VS_SOURCE_FOLDERS',
'COMPONENT_VS_SOURCE_SUFFIXES',
'COMPONENT_VS_SOURCE_TARGETS',
])
source_vcproj_builder = SCons.Script.Builder(
action=source_vcproj_action,
suffix='$COMPONENT_VS_PROJECT_SUFFIX')
dir_vcproj_action = SCons.Script.Action(
ComponentVSDirProjectBuilder, varlist=[
'COMPONENT_VS_SOURCE_FOLDERS',
'COMPONENT_VS_SOURCE_SUFFIXES',
'PROJECT_SOURCES',
])
dir_vcproj_builder = SCons.Script.Builder(
action=dir_vcproj_action,
suffix='$COMPONENT_VS_PROJECT_SUFFIX')
slnaction = SCons.Script.Action(ComponentVSSolutionBuilder, varlist=[
'COMPONENT_VS_ENABLED_PROJECTS',
'SOLUTION_FOLDERS',
'SOLUTION_PROJECTS',
'SOLUTION_TARGETS',
])
slnbuilder = SCons.Script.Builder(
action=slnaction,
suffix='$COMPONENT_VS_SOLUTION_SUFFIX')
env.Append(BUILDERS={
'ComponentVSDirProjectBuilder': dir_vcproj_builder,
'ComponentVSProjectBuilder': vcprojbuilder,
'ComponentVSSolutionBuilder': slnbuilder,
'ComponentVSSourceProjectBuilder': source_vcproj_builder,
})
|