1# Copyright 2015 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Buildgen vsprojects plugin.
15
16This parses the list of libraries, and generates globals "vsprojects"
17and "vsproject_dict", to be used by the visual studio generators.
18
19"""
20
21import hashlib
22import re
23
24
25def mako_plugin(dictionary):
26    """The exported plugin code for generate_vsprojeccts
27
28  We want to help the work of the visual studio generators.
29
30  """
31
32    libs = dictionary.get('libs', [])
33    targets = dictionary.get('targets', [])
34
35    for lib in libs:
36        lib['is_library'] = True
37    for target in targets:
38        target['is_library'] = False
39
40    projects = []
41    projects.extend(libs)
42    projects.extend(targets)
43    for target in projects:
44        if 'build' in target and target['build'] == 'test':
45            default_test_dir = 'test'
46        else:
47            default_test_dir = '.'
48        if 'vs_config_type' not in target:
49            if 'build' in target and target['build'] == 'test':
50                target['vs_config_type'] = 'Application'
51            else:
52                target['vs_config_type'] = 'StaticLibrary'
53        if 'vs_packages' not in target:
54            target['vs_packages'] = []
55        if 'vs_props' not in target:
56            target['vs_props'] = []
57        target['vs_proj_dir'] = target.get('vs_proj_dir', default_test_dir)
58        if target.get('vs_project_guid',
59                      None) is None and 'windows' in target.get(
60                          'platforms', ['windows']):
61            name = target['name']
62            guid = re.sub('(........)(....)(....)(....)(.*)',
63                          r'{\1-\2-\3-\4-\5}',
64                          hashlib.md5(name).hexdigest())
65            target['vs_project_guid'] = guid.upper()
66    # Exclude projects without a visual project guid, such as the tests.
67    projects = [
68        project for project in projects if project.get('vs_project_guid', None)
69    ]
70
71    projects = [
72        project for project in projects
73        if project['language'] != 'c++' or project['build'] == 'all' or
74        project['build'] == 'protoc' or (project['language'] == 'c++' and (
75            project['build'] == 'test' or project['build'] == 'private'))
76    ]
77
78    project_dict = dict([(p['name'], p) for p in projects])
79
80    packages = dictionary.get('vspackages', [])
81    packages_dict = dict([(p['name'], p) for p in packages])
82
83    dictionary['vsprojects'] = projects
84    dictionary['vsproject_dict'] = project_dict
85    dictionary['vspackages_dict'] = packages_dict
86