summaryrefslogtreecommitdiffstats
path: root/o3d/gypbuild.py
blob: 2e042bf24a4540740099d1a7ccb65890f00246b4 (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
#! /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 subprocess
import platform
from optparse import OptionParser


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

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

    modes = ["build", "presubmit", "selenium", "unit_tests"]

    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 print 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="clean the targets")
    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], [])

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

  def Log(self, *args):
    if self.verbose:
      print args

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

  def Dobuild(self, targets, options):
    """Builds the specifed targets."""
    if os.name == 'nt':
      self.Execute(['msbuild',
                    os.path.abspath('all.sln')])
    elif platform.system() == 'Darwin':
      self.Execute(['xcodebuild',
                    '-project', 'all.xcodeproj'])
    elif platform.system() == 'Linux':
      self.Execute(['hammer',
                    '-f', 'all_main.scons'])
    else:
      print "Error: Unknown platform", os.name

  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 main(args):
  GypBuilder(args[1:])

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