platform_build_soong/scripts/mkcratersp.py
Peter Collingbourne e7c71c344d Split Rust crate builds into compile and link phases.
Teach rustc to generate object files instead of a linked executable
for binary and shared library crates. This lets us skip the lengthy
Rust compilation phase when only the C++ dependencies of these crates
have changed.

This works using a Python script that replaces the linker invoked by
rustc and converts the linker command line into a rspfile that can be
used during the link phase. The script makes certain assumptions about
how rustc invokes the linker, but since we control the version of Rust
we use, we can update the script if those assumptions ever break. I've
asked the Rust developers about adding an official way to do this so
hopefully we won't need to rely on this hack in the future.

The rustc rule for Kythe has been altered to pass linker=true, somewhat
consistent with the main rustc rule, which now doesn't invoke the actual
linker either. `m xref_rust` still succeeds, but hopefully we can find
someone who knows more about the rest of the Kythe pipeline who can let
us know whether it should still work. Otherwise, let's use this CL as
an opportunity to find out whether the rest of the pipeline is happy
with linker=true, since otherwise we will find it hard to maintain the
Kythe rule in the future.

Change-Id: Ifb9c8689c1bd78c0afcf6fdfd111140d20377536
2023-04-12 15:51:41 -07:00

79 lines
2.2 KiB
Python
Executable file

#!/usr/bin/env python3
#
# Copyright (C) 2023 The Android Open Source Project
#
# 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.
#
"""
This script is used as a replacement for the Rust linker. It converts a linker
command line into a rspfile that can be used during the link phase.
"""
import os
import shutil
import subprocess
import sys
def create_archive(out, objects, archives):
mricmd = f'create {out}\n'
for o in objects:
mricmd += f'addmod {o}\n'
for a in archives:
mricmd += f'addlib {a}\n'
mricmd += 'save\nend\n'
subprocess.run([os.getenv('AR'), '-M'], encoding='utf-8', input=mricmd, check=True)
objects = []
archives = []
linkdirs = []
libs = []
temp_archives = []
version_script = None
for i, arg in enumerate(sys.argv):
if arg == '-o':
out = sys.argv[i+1]
if arg == '-L':
linkdirs.append(sys.argv[i+1])
if arg.startswith('-l') or arg == '-shared':
libs.append(arg)
if arg.startswith('-Wl,--version-script='):
version_script = arg[21:]
if arg[0] == '-':
continue
if arg.endswith('.o') or arg.endswith('.rmeta'):
objects.append(arg)
if arg.endswith('.rlib'):
if arg.startswith(os.getenv('TMPDIR')):
temp_archives.append(arg)
else:
archives.append(arg)
create_archive(f'{out}.whole.a', objects, [])
create_archive(f'{out}.a', [], temp_archives)
with open(out, 'w') as f:
print(f'-Wl,--whole-archive', file=f)
print(f'{out}.whole.a', file=f)
print(f'-Wl,--no-whole-archive', file=f)
print(f'{out}.a', file=f)
for a in archives:
print(a, file=f)
for linkdir in linkdirs:
print(f'-L{linkdir}', file=f)
for l in libs:
print(l, file=f)
if version_script:
shutil.copyfile(version_script, f'{out}.version_script')
print(f'-Wl,--version-script={out}.version_script', file=f)