summaryrefslogtreecommitdiffstats
path: root/o3d/gypbuild.py
blob: 785e187d54a88421d8f24bfbf6aabf638a618e29 (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
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
#! /usr/bin/env python
# Copyright 2009 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Builds a particlar platform so the user does not have to know platform
# specific build commands for every single platform.

# TODO(gman): Add help.
# TODO(gman): Add cross platform modes like "debug", "opt", "test", "docs"
# TODO(gman): Add cross platform switches like "-clean" and "-rebuild".
# TODO(gman): Add cross platform options like "-verbose".
# TODO(gman): Add cross platform options like "-presubmit", "-selenium",
#     "-unit_tests"

import os
import os.path
import sys
import re
import subprocess
import platform
sys.path.append('build')
import is_admin
from optparse import OptionParser


class GypBuilder(object):
  """A class to help build gyp projects in a cross platform way"""

  class Builder(object):
    """Base Class for building."""

    def __init__(self, builder):
      self.builder = builder

    def Log(self, *args):
      """Prints something if verbose is true."""
      self.builder.Log(args)

    def Execute(self, args):
      """Executes an external program if execute is true."""
      self.builder.Execute(args)

    def Dopresubmit(self, targets, options):
      """Builds and runs both the unit tests and selenium."""
      self.Dounit_tests(targets, options)
      self.Doselenium(targets, options)

    def Doselenium(self, targets, options):
      """Builds and runs the selenium tests."""
      print "selenium not yet implemented."

    def Dounit_tests(self, targets, options):
      """Builds and runs the unit tests."""
      print "unit_tests not yet implemented."

    def CleanTargets(self, targets, options):
      """Cleans the targets."""
      print "clean not implemented for this platform."


  class OSXBuilder(Builder):
    """Class for building on OSX."""

    def __init__(self, builder):
      GypBuilder.Builder.__init__(self, builder)

    def GetSolutionPath(self):
      """Gets the solution path."""
      return '%s.xcodeproj' % GypBuilder.base_name

    def CleanTargets(self, targets, options):
      """Cleans the specifed targets."""
      solution = self.GetSolutionPath()
      self.Execute(['xcodebuild',
                    '-project', solution,
                    'clean'])

    def Dobuild(self, targets, options):
      """Builds the specifed targets."""
      solution = self.GetSolutionPath()
      self.Execute(['xcodebuild',
                    '-project', solution])

  class WinBuilder(Builder):
    """Class for building on Windows."""

    def __init__(self, builder):
      GypBuilder.Builder.__init__(self, builder)

    def GetSolutionPath(self):
      """Gets the solution path."""
      return os.path.abspath('%s.sln' % GypBuilder.base_name)

    def CheckVisualStudioVersionVsSolution(self, solution):
      """Checks the solution matches the cl version."""
      f = open(solution, "r")
      line = f.readline()
      f.close()
      m = re.search(r'Format Version (\d+)\.', line)
      if m:
        solution_version = int(m.group(1))
      else:
        print "FAILURE: Unknown solution version in %s" % solution
        sys.exit(1)

      output = subprocess.Popen(['cl.exe'],
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE).communicate()[1]
      m = re.search(r'Compiler Version (\d+)\.', output)
      if m:
        compiler_version = int(m.group(1))
      else:
        print "FAILURE: Unknown cl.exe version."
        sys.exit(1)

      #                        Compiler    Solution
      # Visual Studio .NET 2005   14          9
      # Visual Studio .NET 2008   15         10
      # Visual Studio .NET 2010   ??         ??
      if (compiler_version - 14) > (solution_version - 9):
        vs_map = {
          14: '2005',
          15: '2008',
          16: '2010',
        }
        sln_map = {
          9: '2005',
          10: '2008',
          11: '2010',
        }
        vs_version = vs_map[compiler_version]
        print ("ERROR: solution (%s) version does not match "
               "Visual Studio version (%s)" %
               (sln_map[solution_version], vs_version))
        print "You should 'set GYP_MSVS_VERSION=auto'"
        print "and run 'gclient runhooks --force'"
        sys.exit(1)

    def CleanTargets(self, targets, options):
      """Cleans the targets."""
      solution = self.GetSolutionPath()
      self.Execute(['devenv.com',
                    solution,
                    '/clean',
                    options.version])

    def Dobuild(self, targets, options):
      """Builds the specifed targets."""
      solution = self.GetSolutionPath()
      if not is_admin.IsAdmin():
        print ("WARNING: selenium_ie will not run unless you run as admin "
               "or turn off UAC.\nAfter switching to admin run "
               "'gclient runhooks --force'")
      self.CheckVisualStudioVersionVsSolution(solution)
      self.Execute(['devenv.com',
                    solution,
                    '/build',
                    options.version])
      # TODO(gman): Should I check for devenv and if it does not exist
      # use msbuild? Msbuild is significantly slower than devenv.
      #self.Execute(['msbuild',
      #              solution,
      #              '/p:Configuration=%s' % options.version])

  class LinuxBuilder(Builder):
    """Class for building on Linux."""

    def __init__(self, builder):
      GypBuilder.Builder.__init__(self, builder)

    def GetSolutionPath(self):
      """Gets the solution path."""
      return '%s.Makefile' % GypBuilder.base_name

    def CleanTargets(self, targets, options):
      """Cleans the targets."""
      print "clean not implemented for this platform."

    def Dobuild(self, targets, options):
      """Builds the specifed targets."""
      solution = self.GetSolutionPath()
      self.Execute(['make', '-f', solution])

  # Use "o3d" for chrome only build?
  base_name = "o3d_all"

  def __init__(self, args):
    self.execute = True
    self.verbose = False

    modes = ["build", "presubmit", "selenium", "unit_tests"]
    versions = ["Debug", "Release"]

    parser = OptionParser()
    parser.add_option(
        "--list-targets", action="store_true",
        help="lists all available targets.")
    parser.add_option(
        "--no-execute", action="store_true", default=False,
        help="just prints commands that would get executed.")
    parser.add_option(
        "--verbose", action="store_true",
        help="prints more output.")
    parser.add_option(
        "--targets", action="append",
        help="targets to build separated by commas.")
    parser.add_option(
        "--clean", action="store_true",
        help="cleans the targets.")
    parser.add_option(
        "--rebuild", action="store_true",
        help="cleans, then builds targets")
    parser.add_option(
        "--version", choices=versions, default="Debug",
        help="version to build. Versions are '%s'. Default='Debug' " %
             "', '".join(versions))
    parser.add_option(
        "--mode", choices=modes, default="build",
        help="mode to use. Valid modes are '%s'. Default='build' " %
             "', '".join(modes))

    (options, args) = parser.parse_args(args=args)

    self.verbose = options.verbose
    self.execute = not options.no_execute

    if options.list_targets:
      print "Not yet implemented"
      sys.exit(0)

    self.Log("mode:", options.mode)

    targets = options.targets
    if targets:
      # flatten the targets.
      targets = sum([t.split(",") for t in targets], [])

    os.chdir("build")

    # Create a platform specific builder.
    if os.name == 'nt':
      builder = self.WinBuilder(self)
    elif platform.system() == 'Darwin':
      builder = self.OSXBuilder(self)
    elif platform.system() == 'Linux':
      builder = self.LinuxBuilder(self)
    else:
      print "ERROR: Unknown platform."
      sys.exit(1)

    # clean if asked.
    if options.clean or options.rebuild:
      builder.CleanTargets(targets, options)
      if not options.rebuild:
        return

    # call a Do method based on the mode.
    func = getattr(builder, "Do%s" % options.mode)
    func(targets, options)

  def Log(self, *args):
    """Prints something if verbose is true."""
    if self.verbose:
      print args

  def Execute(self, args):
    """Executes an external program if execute is true."""
    if self.execute:
      self.Log(" ".join(args))
      if subprocess.call(args) > 0:
        raise RuntimeError("FAILED: " + " ".join(args))
    else:
      print " ".join(args)


def main(args):
  GypBuilder(args[1:])

if __name__ == "__main__":
  main(sys.argv)