Skip to content

Commit 06305b2

Browse files
authored
Merge pull request #25028 from akyrtzi/utils-build-parser-flexible
2 parents 0b4309f + fbdcd6e commit 06305b2

File tree

1 file changed

+82
-26
lines changed

1 file changed

+82
-26
lines changed

utils/build-parser-lib

Lines changed: 82 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,27 @@ from swift_build_support.swift_build_support.SwiftBuildSupport import (
4040
)
4141
from swift_build_support.swift_build_support.toolchain import host_toolchain
4242

43-
isMac = platform.system() == 'Darwin'
43+
isDarwin = platform.system() == 'Darwin'
4444

4545
class Builder(object):
46-
def __init__(self, toolchain, args):
46+
def __init__(self, toolchain, args, profile_data=None, arch=None, native_build_dir=None):
4747
self.toolchain = toolchain
4848
self.ninja_path = args.ninja_path
4949
self.build_release = args.release
5050
self.enable_assertions = not args.no_assertions
5151
self.lto_type = args.lto_type
5252
self.pgo_type = args.pgo_type
5353
self.profile_input = args.profile_input
54+
self.profile_data = profile_data
5455
self.dry_run = args.dry_run
5556
self.jobs = args.build_jobs
5657
self.verbose = args.verbose
5758
self.build_dir = args.build_dir
58-
self.install_symroot = args.install_symroot
5959
self.install_destdir = args.install_destdir
6060
self.install_prefix = args.install_prefix
6161
self.version = args.version
62+
self.arch = arch
63+
self.native_build_dir = native_build_dir
6264

6365
def call(self, command, env=None, without_sleeping=False):
6466
if without_sleeping:
@@ -69,8 +71,25 @@ class Builder(object):
6971
def configure(self, enable_debuginfo, instrumentation=None, profile_data=None):
7072
cmake_args = [self.toolchain.cmake, '-G', 'Ninja']
7173
cmake_args += ['-DCMAKE_MAKE_PROGRAM='+self.ninja_path]
72-
if isMac:
74+
if isDarwin:
7375
cmake_args += ['-DCMAKE_OSX_DEPLOYMENT_TARGET=10.12', '-DSWIFT_DARWIN_DEPLOYMENT_VERSION_OSX=10.12']
76+
if self.arch is not None:
77+
cmake_args += [
78+
'-DLLVM_HOST_TRIPLE:STRING='+self.arch+'-apple-darwin16.0',
79+
'-DSWIFT_HOST_TRIPLE:STRING='+self.arch+'-apple-darwin16.0',
80+
'-DCMAKE_C_FLAGS=-arch '+self.arch,
81+
'-DCMAKE_CXX_FLAGS=-arch '+self.arch,
82+
'-DSWIFT_HOST_VARIANT_ARCH='+self.arch,
83+
]
84+
if self.native_build_dir is not None:
85+
cmake_args += [
86+
'-DLLVM_TABLEGEN='+os.path.join(self.native_build_dir, 'bin', 'llvm-tblgen'),
87+
'-DCLANG_TABLEGEN='+os.path.join(self.native_build_dir, 'bin', 'clang-tblgen'),
88+
'-DLLVM_NATIVE_BUILD='+self.native_build_dir,
89+
'-DSWIFT_NATIVE_LLVM_TOOLS_PATH:STRING='+os.path.join(self.native_build_dir, 'bin'),
90+
'-DSWIFT_NATIVE_CLANG_TOOLS_PATH:STRING='+os.path.join(self.native_build_dir, 'bin'),
91+
'-DSWIFT_NATIVE_SWIFT_TOOLS_PATH:STRING='+os.path.join(self.native_build_dir, 'bin'),
92+
]
7493
else:
7594
dispatch_source_path = os.path.join(SWIFT_SOURCE_ROOT, 'swift-corelibs-libdispatch')
7695
cmake_args += ['-DSWIFT_HOST_VARIANT=linux', '-DSWIFT_HOST_VARIANT_SDK=LINUX', '-DSWIFT_HOST_VARIANT_ARCH=x86_64',
@@ -123,19 +142,7 @@ class Builder(object):
123142
env = {'DESTDIR': self.install_destdir}
124143
self.build_target(self.build_dir, 'tools/swift/tools/libSwiftSyntaxParser/install', env=env)
125144

126-
def extract_symbols(self):
127-
if not isMac:
128-
return
129-
extract_script = os.path.join(SWIFT_SOURCE_ROOT, "swift", "utils", "parser-lib", "darwin-extract-symbols")
130-
print("--- Extracting symbols ---", file=sys.stderr)
131-
env = {'INSTALL_DIR': self.install_destdir,
132-
'INSTALL_PREFIX': self.install_prefix,
133-
'INSTALL_SYMROOT': self.install_symroot,
134-
'BUILD_JOBS': str(self.jobs)}
135-
self.call([extract_script], env=env)
136-
137-
def get_profile_data(self):
138-
profile_dir = os.path.join(self.build_dir, 'profiling')
145+
def get_profile_data(self, profile_dir):
139146
shell.makedirs(profile_dir, dry_run=self.dry_run)
140147
instrumentation = 'IR' if self.pgo_type == 'ir' else 'Frontend'
141148
with shell.pushd(profile_dir, dry_run=self.dry_run):
@@ -145,24 +152,29 @@ class Builder(object):
145152
shell.rmtree("profiles", dry_run=self.dry_run)
146153
self.call([os.path.join("bin", "swift-syntax-parser-test"), self.profile_input, '-time'])
147154
self.call([self.toolchain.llvm_profdata, "merge", "-output=profdata.prof", "profiles"])
148-
return os.path.join(profile_dir, "profdata.prof")
149155

150156
def run(self):
151157
shell.makedirs(self.build_dir, dry_run=self.dry_run)
152158

153-
profile_data = None
154-
if self.pgo_type:
155-
profile_data = self.get_profile_data()
156-
157159
with shell.pushd(self.build_dir, dry_run=self.dry_run):
158-
self.configure(enable_debuginfo=True, profile_data=profile_data)
160+
self.configure(enable_debuginfo=True, profile_data=self.profile_data)
159161

160162
self.build_target(self.build_dir, 'swift-syntax-parser-test')
161163

162164
if self.install_destdir:
163165
self.install()
164-
if self.install_symroot:
165-
self.extract_symbols()
166+
167+
168+
def extract_symbols(install_destdir, install_prefix, install_symroot, jobs):
169+
if not isDarwin:
170+
return
171+
extract_script = os.path.join(SWIFT_SOURCE_ROOT, "swift", "utils", "parser-lib", "darwin-extract-symbols")
172+
print("--- Extracting symbols ---", file=sys.stderr)
173+
env = {'INSTALL_DIR': install_destdir,
174+
'INSTALL_PREFIX': install_prefix,
175+
'INSTALL_SYMROOT': install_symroot,
176+
'BUILD_JOBS': str(jobs)}
177+
shell.call([extract_script], env=env)
166178

167179

168180
def main():
@@ -177,11 +189,12 @@ def main():
177189
store_path = optbuilder.actions.store_path
178190

179191
toolchain = host_toolchain(xcrun_toolchain='default')
192+
default_architectures = platform.machine()
180193

181194
default_profile_input = os.path.join(SWIFT_SOURCE_ROOT, "swift", "utils", "parser-lib", "profile-input.swift")
182195
default_jobs = multiprocessing.cpu_count()
183196
default_build_dir = os.path.join(SWIFT_BUILD_ROOT, 'parser-lib')
184-
default_install_prefix = defaults.DARWIN_INSTALL_PREFIX if isMac else UNIX_INSTALL_PREFIX
197+
default_install_prefix = defaults.DARWIN_INSTALL_PREFIX if isDarwin else UNIX_INSTALL_PREFIX
185198
default_ninja = toolchain.ninja
186199

187200
option('--release', store_true,
@@ -215,6 +228,9 @@ def main():
215228
option('--build-dir', store_path,
216229
default=default_build_dir,
217230
help='the path where the build products will be placed. (default = %s)' % default_build_dir)
231+
option('--architectures', store,
232+
default=default_architectures,
233+
help='space-separated list of architectures to build for. (default = %s)' % default_architectures)
218234
option('--install-symroot', store_path,
219235
help='the path to install debug symbols into')
220236
option('--install-destdir', store_path,
@@ -231,6 +247,46 @@ def main():
231247
parser = optbuilder.build()
232248
args = parser.parse_args()
233249

250+
if isDarwin:
251+
architectures = args.architectures.split(" ")
252+
architectures = [arch for arch in architectures if arch != platform.machine() and arch != ""]
253+
if platform.machine() in architectures: architectures = [platform.machine()] + architectures
254+
architectures = [platform.machine()] + architectures
255+
256+
objroot = args.build_dir
257+
dstroot = args.install_destdir
258+
symroot = args.install_symroot
259+
prefix = args.install_prefix
260+
261+
for arch in architectures:
262+
native = platform.machine() == arch
263+
264+
args.build_dir = os.path.join(objroot, arch, "obj")
265+
args.install_destdir = os.path.join(objroot, arch, "dst")
266+
args.install_prefix = "/"
267+
268+
native_build_dir = None if native else os.path.join(objroot, platform.machine(), "obj")
269+
270+
profile_data = None
271+
if args.pgo_type:
272+
profile_dir = os.path.join(objroot, platform.machine()+'-profiling')
273+
if native:
274+
builder = Builder(toolchain, args)
275+
builder.get_profile_data(profile_dir)
276+
profile_data = os.path.join(profile_dir, "profdata.prof")
277+
278+
builder = Builder(toolchain, args, profile_data=profile_data, arch=arch, native_build_dir=native_build_dir)
279+
builder.run()
280+
281+
lipo = os.path.join(SWIFT_SOURCE_ROOT, "swift", "utils", "recursive-lipo")
282+
dst_dirs = [os.path.join(objroot, arch, "dst") for arch in architectures]
283+
shell.call([lipo, "-v", "--destination", os.path.join(dstroot, "./"+prefix)] + dst_dirs)
284+
285+
if args.install_symroot:
286+
extract_symbols(dstroot, prefix, symroot, args.build_jobs)
287+
288+
return 0
289+
234290
builder = Builder(toolchain, args)
235291
builder.run()
236292
return 0

0 commit comments

Comments
 (0)