1#!/usr/bin/env python3 2# 3# Copyright (C) 2024 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18""" 19This script is used as a replacement for the Rust linker to allow fine-grained 20control over the what gets emitted to the linker. 21""" 22 23import os 24import shutil 25import subprocess 26import sys 27import argparse 28 29replacementVersionScript = None 30 31argparser = argparse.ArgumentParser() 32argparser.add_argument('--android-clang-bin', required=True) 33args = argparser.parse_known_args() 34clang_args = [args[0].android_clang_bin] + args[1] 35 36for i, arg in enumerate(clang_args): 37 if arg.startswith('-Wl,--android-version-script='): 38 replacementVersionScript = arg.split("=")[1] 39 del clang_args[i] 40 break 41 42if replacementVersionScript: 43 versionScriptFound = False 44 for i, arg in enumerate(clang_args): 45 if arg.startswith('-Wl,--version-script='): 46 clang_args[i] ='-Wl,--version-script=' + replacementVersionScript 47 versionScriptFound = True 48 break 49 50 if not versionScriptFound: 51 # If rustc did not emit a version script, just append the arg 52 clang_args.append('-Wl,--version-script=' + replacementVersionScript) 53try: 54 subprocess.run(clang_args, encoding='utf-8', check=True) 55except subprocess.CalledProcessError as e: 56 sys.exit(-1) 57 58