OLD | NEW |
1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
2 # Copyright 2009 Google Inc. | 2 # Copyright 2009 Google Inc. |
3 # | 3 # |
4 # Licensed under the Apache License, Version 2.0 (the "License"); | 4 # Licensed under the Apache License, Version 2.0 (the "License"); |
5 # you may not use this file except in compliance with the License. | 5 # you may not use this file except in compliance with the License. |
6 # You may obtain a copy of the License at | 6 # You may obtain a copy of the License at |
7 # | 7 # |
8 # http://www.apache.org/licenses/LICENSE-2.0 | 8 # http://www.apache.org/licenses/LICENSE-2.0 |
9 # | 9 # |
10 # Unless required by applicable law or agreed to in writing, software | 10 # Unless required by applicable law or agreed to in writing, software |
(...skipping 19 matching lines...) Expand all Loading... |
30 import subprocess | 30 import subprocess |
31 import platform | 31 import platform |
32 sys.path.append('build') | 32 sys.path.append('build') |
33 import is_admin | 33 import is_admin |
34 from optparse import OptionParser | 34 from optparse import OptionParser |
35 | 35 |
36 | 36 |
37 class GypBuilder(object): | 37 class GypBuilder(object): |
38 """A class to help build gyp projects in a cross platform way""" | 38 """A class to help build gyp projects in a cross platform way""" |
39 | 39 |
| 40 class Builder(object): |
| 41 """Base Class for building.""" |
| 42 |
| 43 def __init__(self, builder): |
| 44 self.builder = builder |
| 45 |
| 46 def Log(self, *args): |
| 47 """Prints something if verbose is true.""" |
| 48 self.builder.Log(args) |
| 49 |
| 50 def Execute(self, args): |
| 51 """Executes an external program if execute is true.""" |
| 52 self.builder.Execute(args) |
| 53 |
| 54 def Dopresubmit(self, targets, options): |
| 55 """Builds and runs both the unit tests and selenium.""" |
| 56 self.Dounit_tests(targets, options) |
| 57 self.Doselenium(targets, options) |
| 58 |
| 59 def Doselenium(self, targets, options): |
| 60 """Builds and runs the selenium tests.""" |
| 61 print "selenium not yet implemented." |
| 62 |
| 63 def Dounit_tests(self, targets, options): |
| 64 """Builds and runs the unit tests.""" |
| 65 print "unit_tests not yet implemented." |
| 66 |
| 67 def CleanTargets(self, targets, options): |
| 68 """Cleans the targets.""" |
| 69 print "clean not implemented for this platform." |
| 70 |
| 71 |
| 72 class OSXBuilder(Builder): |
| 73 """Class for building on OSX.""" |
| 74 |
| 75 def __init__(self, builder): |
| 76 GypBuilder.Builder.__init__(self, builder) |
| 77 |
| 78 def GetSolutionPath(self): |
| 79 """Gets the solution path.""" |
| 80 return '%s.xcodeproj' % GypBuilder.base_name |
| 81 |
| 82 def CleanTargets(self, targets, options): |
| 83 """Cleans the specifed targets.""" |
| 84 solution = self.GetSolutionPath() |
| 85 self.Execute(['xcodebuild', |
| 86 '-project', solution, |
| 87 'clean']) |
| 88 |
| 89 def Dobuild(self, targets, options): |
| 90 """Builds the specifed targets.""" |
| 91 solution = self.GetSolutionPath() |
| 92 self.Execute(['xcodebuild', |
| 93 '-project', solution]) |
| 94 |
| 95 class WinBuilder(Builder): |
| 96 """Class for building on Windows.""" |
| 97 |
| 98 def __init__(self, builder): |
| 99 GypBuilder.Builder.__init__(self, builder) |
| 100 |
| 101 def GetSolutionPath(self): |
| 102 """Gets the solution path.""" |
| 103 return os.path.abspath('%s.sln' % GypBuilder.base_name) |
| 104 |
| 105 def CheckVisualStudioVersionVsSolution(self, solution): |
| 106 """Checks the solution matches the cl version.""" |
| 107 f = open(solution, "r") |
| 108 line = f.readline() |
| 109 f.close() |
| 110 m = re.search(r'Format Version (\d+)\.', line) |
| 111 if m: |
| 112 solution_version = int(m.group(1)) |
| 113 else: |
| 114 print "FAILURE: Unknown solution version in %s" % solution |
| 115 sys.exit(1) |
| 116 |
| 117 output = subprocess.Popen(['cl.exe'], |
| 118 stdout=subprocess.PIPE, |
| 119 stderr=subprocess.PIPE).communicate()[1] |
| 120 m = re.search(r'Compiler Version (\d+)\.', output) |
| 121 if m: |
| 122 compiler_version = int(m.group(1)) |
| 123 else: |
| 124 print "FAILURE: Unknown cl.exe version." |
| 125 sys.exit(1) |
| 126 |
| 127 # Compiler Solution |
| 128 # Visual Studio .NET 2005 14 9 |
| 129 # Visual Studio .NET 2008 15 10 |
| 130 # Visual Studio .NET 2010 ?? ?? |
| 131 if (compiler_version - 14) > (solution_version - 9): |
| 132 vs_map = { |
| 133 14: '2005', |
| 134 15: '2008', |
| 135 16: '2010', |
| 136 } |
| 137 sln_map = { |
| 138 9: '2005', |
| 139 10: '2008', |
| 140 11: '2010', |
| 141 } |
| 142 vs_version = vs_map[compiler_version] |
| 143 print ("ERROR: solution (%s) version does not match " |
| 144 "Visual Studio version (%s)" % |
| 145 (sln_map[solution_version], vs_version)) |
| 146 print "You should 'set GYP_MSVS_VERSION=auto'" |
| 147 print "and run 'gclient runhooks --force'" |
| 148 sys.exit(1) |
| 149 |
| 150 def CleanTargets(self, targets, options): |
| 151 """Cleans the targets.""" |
| 152 solution = self.GetSolutionPath() |
| 153 self.Execute(['devenv.exe', |
| 154 solution, |
| 155 '/clean', |
| 156 options.version]) |
| 157 |
| 158 def Dobuild(self, targets, options): |
| 159 """Builds the specifed targets.""" |
| 160 solution = self.GetSolutionPath() |
| 161 if not is_admin.IsAdmin(): |
| 162 print ("WARNING: selenium_ie will not run unless you run as admin " |
| 163 "or turn off UAC.\nAfter switching to admin run " |
| 164 "'gclient runhooks --force'") |
| 165 self.CheckVisualStudioVersionVsSolution(solution) |
| 166 self.Execute(['msbuild', |
| 167 solution, |
| 168 '/p:Configuration=%s' % options.version]) |
| 169 |
| 170 class LinuxBuilder(Builder): |
| 171 """Class for building on Linux.""" |
| 172 |
| 173 def __init__(self, builder): |
| 174 GypBuilder.Builder.__init__(self, builder) |
| 175 |
| 176 def GetSolutionPath(self): |
| 177 """Gets the solution path.""" |
| 178 return '%s_main.scons' % GypBuilder.base_name |
| 179 |
| 180 def CleanTargets(self, targets, options): |
| 181 """Cleans the targets.""" |
| 182 solution = self.GetSolutionPath() |
| 183 self.Execute(['hammer', |
| 184 '-f', solution, |
| 185 '--clean']) |
| 186 |
| 187 def Dobuild(self, targets, options): |
| 188 """Builds the specifed targets.""" |
| 189 solution = self.GetSolutionPath() |
| 190 self.Execute(['hammer', |
| 191 '-f', solution]) |
| 192 |
| 193 # Use "o3d" for chrome only build? |
| 194 base_name = "o3d_all" |
| 195 |
40 def __init__(self, args): | 196 def __init__(self, args): |
41 self.execute = True | 197 self.execute = True |
42 self.verbose = False | 198 self.verbose = False |
43 | 199 |
44 modes = ["build", "presubmit", "selenium", "unit_tests"] | 200 modes = ["build", "presubmit", "selenium", "unit_tests"] |
45 versions = ["Debug", "Release"] | 201 versions = ["Debug", "Release"] |
46 | 202 |
47 parser = OptionParser() | 203 parser = OptionParser() |
48 parser.add_option( | 204 parser.add_option( |
49 "--list-targets", action="store_true", | 205 "--list-targets", action="store_true", |
50 help="lists all available targets") | 206 help="lists all available targets.") |
51 parser.add_option( | 207 parser.add_option( |
52 "--no-execute", action="store_true", default=False, | 208 "--no-execute", action="store_true", default=False, |
53 help="just print commands that would get executed") | 209 help="just prints commands that would get executed.") |
54 parser.add_option( | 210 parser.add_option( |
55 "--verbose", action="store_true", | 211 "--verbose", action="store_true", |
56 help="prints more output") | 212 help="prints more output.") |
57 parser.add_option( | 213 parser.add_option( |
58 "--targets", action="append", | 214 "--targets", action="append", |
59 help="targets to build separated by commas.") | 215 help="targets to build separated by commas.") |
60 parser.add_option( | 216 parser.add_option( |
61 "--clean", action="store_true", | 217 "--clean", action="store_true", |
62 help="clean the targets") | 218 help="cleans the targets.") |
| 219 parser.add_option( |
| 220 "--rebuild", action="store_true", |
| 221 help="cleans, then builds targets") |
63 parser.add_option( | 222 parser.add_option( |
64 "--version", choices=versions, default="Debug", | 223 "--version", choices=versions, default="Debug", |
65 help="version to build. Versions are '%s'. Default='Debug' " % | 224 help="version to build. Versions are '%s'. Default='Debug' " % |
66 "', '".join(versions)) | 225 "', '".join(versions)) |
67 parser.add_option( | 226 parser.add_option( |
68 "--mode", choices=modes, default="build", | 227 "--mode", choices=modes, default="build", |
69 help="mode to use. Valid modes are '%s'. Default='build' " % | 228 help="mode to use. Valid modes are '%s'. Default='build' " % |
70 "', '".join(modes)) | 229 "', '".join(modes)) |
71 | 230 |
72 (options, args) = parser.parse_args(args=args) | 231 (options, args) = parser.parse_args(args=args) |
73 | 232 |
74 self.verbose = options.verbose | 233 self.verbose = options.verbose |
75 self.execute = not options.no_execute | 234 self.execute = not options.no_execute |
76 | 235 |
77 if options.list_targets: | 236 if options.list_targets: |
78 print "Not yet implemented" | 237 print "Not yet implemented" |
79 sys.exit(0) | 238 sys.exit(0) |
80 | 239 |
81 self.Log("mode:", options.mode) | 240 self.Log("mode:", options.mode) |
82 | 241 |
83 targets = options.targets | 242 targets = options.targets |
84 if targets: | 243 if targets: |
85 # flatten the targets. | 244 # flatten the targets. |
86 targets = sum([t.split(",") for t in targets], []) | 245 targets = sum([t.split(",") for t in targets], []) |
87 | 246 |
| 247 os.chdir("build") |
| 248 |
| 249 # Create a platform specific builder. |
| 250 if os.name == 'nt': |
| 251 builder = self.WinBuilder(self) |
| 252 elif platform.system() == 'Darwin': |
| 253 builder = self.OSXBuilder(self) |
| 254 elif platform.system() == 'Linux': |
| 255 builder = self.LinuxBuilder(self) |
| 256 else: |
| 257 print "ERROR: Unknown platform." |
| 258 sys.exit(1) |
| 259 |
| 260 # clean if asked. |
| 261 if options.clean or options.rebuild: |
| 262 builder.CleanTargets(targets, options) |
| 263 if not options.rebuild: |
| 264 return |
| 265 |
88 # call a Do method based on the mode. | 266 # call a Do method based on the mode. |
89 os.chdir("build") | 267 func = getattr(builder, "Do%s" % options.mode) |
90 func = getattr(self, "Do%s" % options.mode) | |
91 func(targets, options) | 268 func(targets, options) |
92 | 269 |
93 def Log(self, *args): | 270 def Log(self, *args): |
| 271 """Prints something if verbose is true.""" |
94 if self.verbose: | 272 if self.verbose: |
95 print args | 273 print args |
96 | 274 |
97 def Execute(self, args): | 275 def Execute(self, args): |
98 """Executes an external program.""" | 276 """Executes an external program if execute is true.""" |
99 if self.execute: | 277 if self.execute: |
100 self.Log(" ".join(args)) | 278 self.Log(" ".join(args)) |
101 if subprocess.call(args) > 0: | 279 if subprocess.call(args) > 0: |
102 raise RuntimeError("FAILED: " + " ".join(args)) | 280 raise RuntimeError("FAILED: " + " ".join(args)) |
103 else: | 281 else: |
104 print " ".join(args) | 282 print " ".join(args) |
105 | 283 |
106 def CheckVisualStudioVersionVsSolution(self, solution): | |
107 """Checks the solution matches the cl version.""" | |
108 f = open(solution, "r") | |
109 line = f.readline() | |
110 f.close() | |
111 m = re.search(r'Format Version (\d+)\.', line) | |
112 if m: | |
113 solution_version = int(m.group(1)) | |
114 else: | |
115 print "FAILURE: Unknown solution version in %s" % solution | |
116 sys.exit(1) | |
117 | |
118 output = subprocess.Popen(['cl.exe'], | |
119 stdout=subprocess.PIPE, | |
120 stderr=subprocess.PIPE).communicate()[1] | |
121 m = re.search(r'Compiler Version (\d+)\.', output) | |
122 if m: | |
123 compiler_version = int(m.group(1)) | |
124 else: | |
125 print "FAILURE: Unknown cl.exe version." | |
126 sys.exit(1) | |
127 | |
128 # Compiler Solution | |
129 # Visual Studio .NET 2005 14 9 | |
130 # Visual Studio .NET 2008 15 10 | |
131 # Visual Studio .NET 2010 ?? ?? | |
132 if (compiler_version - 14) > (solution_version - 9): | |
133 vs_map = { | |
134 14: '2005', | |
135 15: '2008', | |
136 16: '2010', | |
137 } | |
138 sln_map = { | |
139 9: '2005', | |
140 10: '2008', | |
141 11: '2010', | |
142 } | |
143 vs_version = vs_map[compiler_version] | |
144 print ("ERROR: solution (%s) version does not match " | |
145 "Visual Studio version (%s)" % | |
146 (sln_map[solution_version], vs_version)) | |
147 print "You should 'set GYP_MSVS_VERSION=auto'" | |
148 print "and run 'gclient runhooks --force'" | |
149 sys.exit(1) | |
150 | |
151 def Dobuild(self, targets, options): | |
152 """Builds the specifed targets.""" | |
153 | |
154 # Use "o3d" for chrome only build. | |
155 name = "o3d_all" | |
156 | |
157 if os.name == 'nt': | |
158 solution = os.path.abspath('%s.sln' % name) | |
159 if not is_admin.IsAdmin(): | |
160 print ("WARNING: selenium_ie will not run unless you run as admin " | |
161 "or turn off UAC.\nAfter switching to admin run " | |
162 "'gclient runhooks --force'") | |
163 self.CheckVisualStudioVersionVsSolution(solution) | |
164 self.Execute(['msbuild', | |
165 solution, | |
166 '/p:Configuration=%s' % options.version]) | |
167 elif platform.system() == 'Darwin': | |
168 self.Execute(['xcodebuild', | |
169 '-project', '%s.xcodeproj' % name]) | |
170 elif platform.system() == 'Linux': | |
171 self.Execute(['hammer', | |
172 '-f', '%s_main.scons' % name]) | |
173 else: | |
174 print "Error: Unknown platform", os.name | |
175 | |
176 def Dopresubmit(self, targets, options): | |
177 """Builds and runs both the unit tests and selenium.""" | |
178 self.Dounit_tests(targets, options) | |
179 self.Doselenium(targets, options) | |
180 | |
181 def Doselenium(self, targets, options): | |
182 """Builds and runs the selenium tests.""" | |
183 print "selenium not yet implemented." | |
184 | |
185 def Dounit_tests(self, targets, options): | |
186 """Builds and runs the unit tests.""" | |
187 print "unit_tests not yet implemented." | |
188 | |
189 | 284 |
190 def main(args): | 285 def main(args): |
191 GypBuilder(args[1:]) | 286 GypBuilder(args[1:]) |
192 | 287 |
193 if __name__ == "__main__": | 288 if __name__ == "__main__": |
194 main(sys.argv) | 289 main(sys.argv) |
195 | 290 |
OLD | NEW |