Ivan Lozano | f458901 | 2024-11-20 22:18:11 +0000 | [diff] [blame^] | 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 | """ |
| 19 | This script is used as a replacement for the Rust linker to allow fine-grained |
| 20 | control over the what gets emitted to the linker. |
| 21 | """ |
| 22 | |
| 23 | import os |
| 24 | import shutil |
| 25 | import subprocess |
| 26 | import sys |
| 27 | import argparse |
| 28 | |
| 29 | replacementVersionScript = None |
| 30 | |
| 31 | argparser = argparse.ArgumentParser() |
| 32 | argparser.add_argument('--android-clang-bin', required=True) |
| 33 | args = argparser.parse_known_args() |
| 34 | clang_args = [args[0].android_clang_bin] + args[1] |
| 35 | |
| 36 | for 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 | |
| 42 | if 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) |
| 53 | try: |
| 54 | subprocess.run(clang_args, encoding='utf-8', check=True) |
| 55 | except subprocess.CalledProcessError as e: |
| 56 | sys.exit(-1) |
| 57 | |