-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdk_builder.py
197 lines (157 loc) · 7.03 KB
/
sdk_builder.py
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
##############################################################################
# CEGUI dependencies build script for Windows
#
# Copyright (C) 2014-2016 Timotei Dolean <[email protected]>
# and contributing authors (see AUTHORS file)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##############################################################################
from __future__ import print_function
import multiprocessing
from abc import ABCMeta
import abc
import argparse
import json
import os
import subprocess
import time
from distutils import spawn
import build_utils
#TODO: rename compiler to toolchain?
#TODO: samples
class CMakeArgs:
def __init__(self, generator, extraArgs):
self.generator = generator
self.extraArgs = extraArgs
class BuildDetails:
def __init__(self, compiler, buildDir, cmakeArgs, buildCommands):
self.compiler = compiler
self.buildDir = buildDir
self.cmakeArgs = cmakeArgs
self.buildCommands = buildCommands
class SDKBuilder:
__metaclass__ = ABCMeta
_toolchainToCMakeGeneratorMappings = {
"msvc2008": "Visual Studio 9 2008",
"msvc2010": "Visual Studio 10 2010",
"msvc2012": "Visual Studio 11 2012",
"msvc2013": "Visual Studio 12 2013",
"msvc2015": "Visual Studio 14 2015",
"mingw": "MinGW Makefiles"
}
def __init__(self, args, sdkName):
print("*** Using args: ")
for key, value in vars(args).iteritems():
print(' ', key, '=', value)
print("*** Builder for", sdkName, "| Current time: ", time.strftime("%c"),
"| Available cores: " + str(multiprocessing.cpu_count()))
self.sdkName = sdkName
self.args = args
self.srcDir = args.src_dir
self.artifactsPath = args.artifacts_dir
self.artifactsUnarchivedPath = args.artifacts_unarchived_dir
self.toolchain = args.toolchain
self.ensureCanBuildSDK()
self.builds = self.createSDKBuilds()
self.config = self.loadConfig()
build_utils.setupPath(self.artifactsPath, False)
build_utils.setupPath(self.artifactsUnarchivedPath, False)
@staticmethod
def hasExe(name):
return spawn.find_executable(name) is not None
def ensureCanBuildSDK(self):
def ensureHasExe(name):
if not self.hasExe(name):
print("No program named '%s' could be found on PATH! Aborting... " % name)
exit(1)
ensureHasExe('cmake')
if self.toolchain == "mingw":
ensureHasExe('mingw32-make')
else:
ensureHasExe('msbuild')
def build(self):
old_path = os.getcwd()
os.chdir(self.srcDir)
depsStartTime = time.time()
print("*** Building ...")
for compiler, builds in self.builds.iteritems():
compilerStartTime = time.time()
print("\n*** Using '%s' compiler... | Current time: %s " % (compiler, time.strftime("%c")))
for build in builds:
buildDir = os.path.join(self.srcDir, build.buildDir)
build_utils.setupPath(buildDir, not self.args.quick_mode)
os.chdir(buildDir)
if build_utils.invokeCMake(self.srcDir, build.cmakeArgs.generator, build.cmakeArgs.extraArgs) != 0:
print("*** Error configuring CMake for", compiler)
exit(1)
for command in build.buildCommands:
print("*** Executing compiler command:", command)
returnCode = subprocess.Popen(command).wait()
if returnCode != 0:
print("*** Compilation failed!")
exit(1)
print("*** Compilation using '%s' took %f minutes." % (compiler, self.minsUntilNow(compilerStartTime)))
self.onAfterBuild(compiler, builds)
self.gatherArtifacts(compiler, builds)
self.saveConfig()
print("***", self.sdkName, "total build time:", self.minsUntilNow(depsStartTime),
"minutes. | Current time: ", time.strftime("%c"))
os.chdir(old_path)
@staticmethod
def minsUntilNow(startTime):
return (time.time() - startTime) / 60.0
@abc.abstractmethod
def createSDKBuilds(self):
raise NotImplementedError
@abc.abstractmethod
def gatherArtifacts(self, compiler, builds):
raise NotImplementedError
def onAfterBuild(self, compiler, builds):
pass
@classmethod
def getAvailableToolchains(cls):
return cls._toolchainToCMakeGeneratorMappings.keys()
@classmethod
def getCMakeGenerator(cls, toolchain):
return cls._toolchainToCMakeGeneratorMappings[toolchain]
@classmethod
def getDefaultArgParse(cls, sdkName):
currentPath = os.getcwd()
parser = argparse.ArgumentParser(description="Build " + sdkName + " for Windows.")
parser.add_argument("-s", "--src-dir", required=True,
help="Path to the " + sdkName + " sources")
parser.add_argument("-t", "--toolchain", required=True,
help="The toolchain to be used when generating the SDK",
choices=cls.getAvailableToolchains())
parser.add_argument("--config-file", default=os.path.join(os.path.abspath(os.path.dirname(__file__)), "config.json"),
help="Path where to store the configuration file for the builder script.")
parser.add_argument("--artifacts-dir", default=os.path.join(currentPath, "artifacts"),
help="Directory where to store the final artifacts")
parser.add_argument("--artifacts-unarchived-dir",
default=os.path.join(currentPath, "artifacts", "unarchived"),
help="Directory where to store the final unarchived artifacts")
parser.add_argument("--quick-mode", action="store_true", help=argparse.SUPPRESS)
return parser
def saveConfig(self):
with open(self.args.config_file, 'w') as f:
json.dump(self.config, f)
def loadConfig(self):
try:
with open(self.args.config_file, 'r') as f:
return json.load(f)
except:
print("*** No config file found at", self.args.config_file, ". Creating a default one...")
with open(self.args.config_file, 'w') as f:
json.dump({}, f)
return {}