summaryrefslogtreecommitdiffstats
path: root/third_party/python/gyp/tools/pretty_sln.py
blob: 12a6dadd173578c629f2af092bfb2b96f3201826 (plain)
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
#!/usr/bin/env python

# Copyright (c) 2012 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Prints the information in a sln file in a diffable way.

   It first outputs each projects in alphabetical order with their
   dependencies.

   Then it outputs a possible build order.
"""

from __future__ import print_function

__author__ = 'nsylvain (Nicolas Sylvain)'

import os
import re
import sys
import pretty_vcproj

def BuildProject(project, built, projects, deps):
  # if all dependencies are done, we can build it, otherwise we try to build the
  # dependency.
  # This is not infinite-recursion proof.
  for dep in deps[project]:
    if dep not in built:
      BuildProject(dep, built, projects, deps)
  print(project)
  built.append(project)

def ParseSolution(solution_file):
  # All projects, their clsid and paths.
  projects = dict()

  # A list of dependencies associated with a project.
  dependencies = dict()

  # Regular expressions that matches the SLN format.
  # The first line of a project definition.
  begin_project = re.compile(r'^Project\("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942'
                             r'}"\) = "(.*)", "(.*)", "(.*)"$')
  # The last line of a project definition.
  end_project = re.compile('^EndProject$')
  # The first line of a dependency list.
  begin_dep = re.compile(
      r'ProjectSection\(ProjectDependencies\) = postProject$')
  # The last line of a dependency list.
  end_dep = re.compile('EndProjectSection$')
  # A line describing a dependency.
  dep_line = re.compile(' *({.*}) = ({.*})$')

  in_deps = False
  solution = open(solution_file)
  for line in solution:
    results = begin_project.search(line)
    if results:
      # Hack to remove icu because the diff is too different.
      if results.group(1).find('icu') != -1:
        continue
      # We remove "_gyp" from the names because it helps to diff them.
      current_project = results.group(1).replace('_gyp', '')
      projects[current_project] = [results.group(2).replace('_gyp', ''),
                                   results.group(3),
                                   results.group(2)]
      dependencies[current_project] = []
      continue

    results = end_project.search(line)
    if results:
      current_project = None
      continue

    results = begin_dep.search(line)
    if results:
      in_deps = True
      continue

    results = end_dep.search(line)
    if results:
      in_deps = False
      continue

    results = dep_line.search(line)
    if results and in_deps and current_project:
      dependencies[current_project].append(results.group(1))
      continue

  # Change all dependencies clsid to name instead.
  for project in dependencies:
    # For each dependencies in this project
    new_dep_array = []
    for dep in dependencies[project]:
      # Look for the project name matching this cldis
      for project_info in projects:
        if projects[project_info][1] == dep:
          new_dep_array.append(project_info)
    dependencies[project] = sorted(new_dep_array)

  return (projects, dependencies)

def PrintDependencies(projects, deps):
  print("---------------------------------------")
  print("Dependencies for all projects")
  print("---------------------------------------")
  print("--                                   --")

  for (project, dep_list) in sorted(deps.items()):
    print("Project : %s" % project)
    print("Path : %s" % projects[project][0])
    if dep_list:
      for dep in dep_list:
        print("  - %s" % dep)
    print("")

  print("--                                   --")

def PrintBuildOrder(projects, deps):
  print("---------------------------------------")
  print("Build order                            ")
  print("---------------------------------------")
  print("--                                   --")

  built = []
  for (project, _) in sorted(deps.items()):
    if project not in built:
      BuildProject(project, built, projects, deps)

  print("--                                   --")

def PrintVCProj(projects):

  for project in projects:
    print("-------------------------------------")
    print("-------------------------------------")
    print(project)
    print(project)
    print(project)
    print("-------------------------------------")
    print("-------------------------------------")

    project_path = os.path.abspath(os.path.join(os.path.dirname(sys.argv[1]),
                                                projects[project][2]))

    pretty = pretty_vcproj
    argv = [ '',
             project_path,
             '$(SolutionDir)=%s\\' % os.path.dirname(sys.argv[1]),
           ]
    argv.extend(sys.argv[3:])
    pretty.main(argv)

def main():
  # check if we have exactly 1 parameter.
  if len(sys.argv) < 2:
    print('Usage: %s "c:\\path\\to\\project.sln"' % sys.argv[0])
    return 1

  (projects, deps) = ParseSolution(sys.argv[1])
  PrintDependencies(projects, deps)
  PrintBuildOrder(projects, deps)

  if '--recursive' in sys.argv:
    PrintVCProj(projects)
  return 0


if __name__ == '__main__':
  sys.exit(main())