diff options
author | 2024-11-20 22:18:11 +0000 | |
---|---|---|
committer | 2024-12-13 18:12:57 +0000 | |
commit | f4589017b4bf593c73741deeeaff1831882f9f29 (patch) | |
tree | 62fb64dbf1b5a3a95002f7a9208eca95185e0477 /scripts/rustc_linker.py | |
parent | f7bbd2fe40e43f7fda7e08d08eeb9c2a93552ad9 (diff) |
rust: Add version scripts and symbol exports
This allows Rust modules to define a version_script for shared library
variants.
This requires using a wrapper for the linker (clang++) to intercept
the flags which rustc emits.
This also adds the ability to export additional symbols in addition
to those exported by rustc by default, e.g. whole_static_library
symbols.
Bug: 314309643
Test: New Soong tests pass.
Test: m
Test: m <simple version script module>
Test: m <simple extra symbols module>
Change-Id: I93c9552e5e1181df4663d194c4df4b7053553dd4
Diffstat (limited to 'scripts/rustc_linker.py')
-rwxr-xr-x | scripts/rustc_linker.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/scripts/rustc_linker.py b/scripts/rustc_linker.py new file mode 100755 index 000000000..3f60708e2 --- /dev/null +++ b/scripts/rustc_linker.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2024 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 to allow fine-grained +control over the what gets emitted to the linker. +""" + +import os +import shutil +import subprocess +import sys +import argparse + +replacementVersionScript = None + +argparser = argparse.ArgumentParser() +argparser.add_argument('--android-clang-bin', required=True) +args = argparser.parse_known_args() +clang_args = [args[0].android_clang_bin] + args[1] + +for i, arg in enumerate(clang_args): + if arg.startswith('-Wl,--android-version-script='): + replacementVersionScript = arg.split("=")[1] + del clang_args[i] + break + +if replacementVersionScript: + versionScriptFound = False + for i, arg in enumerate(clang_args): + if arg.startswith('-Wl,--version-script='): + clang_args[i] ='-Wl,--version-script=' + replacementVersionScript + versionScriptFound = True + break + + if not versionScriptFound: + # If rustc did not emit a version script, just append the arg + clang_args.append('-Wl,--version-script=' + replacementVersionScript) +try: + subprocess.run(clang_args, encoding='utf-8', check=True) +except subprocess.CalledProcessError as e: + sys.exit(-1) + |