Chris Parsons | 1f67a7b | 2016-05-23 19:23:24 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright 2016 The Bazel Authors. All rights reserved. |
| 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 | # libtool.sh runs the command passed to it using "xcrunwrapper libtool". |
| 18 | # |
Googler | 48859c1 | 2016-09-23 17:16:37 +0000 | [diff] [blame] | 19 | # It creates symbolic links for all input files with a path-hash appended |
Chris Parsons | 1f67a7b | 2016-05-23 19:23:24 +0000 | [diff] [blame] | 20 | # to their original name (foo.o becomes foo_{md5sum}.o). This is to circumvent |
Googler | 48859c1 | 2016-09-23 17:16:37 +0000 | [diff] [blame] | 21 | # a bug in the original libtool that arises when two input files have the same |
Chris Parsons | 1f67a7b | 2016-05-23 19:23:24 +0000 | [diff] [blame] | 22 | # base name (even if they are in different directories). |
| 23 | |
| 24 | set -eu |
| 25 | |
cparsons | cc21998 | 2017-05-03 22:13:44 +0200 | [diff] [blame] | 26 | # A trick to allow invoking this script in multiple contexts. |
| 27 | if [ -z ${MY_LOCATION+x} ]; then |
| 28 | if [ -d "$0.runfiles/" ]; then |
| 29 | MY_LOCATION="$0.runfiles/bazel_tools/tools/objc" |
| 30 | else |
| 31 | MY_LOCATION="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 32 | fi |
| 33 | fi |
| 34 | |
Chris Parsons | 1f67a7b | 2016-05-23 19:23:24 +0000 | [diff] [blame] | 35 | WRAPPER="${MY_LOCATION}/xcrunwrapper.sh" |
| 36 | |
Chris Parsons | 591d1e1 | 2016-06-14 13:34:30 +0000 | [diff] [blame] | 37 | # Creates a symbolic link to the input argument file and returns the symlink |
| 38 | # file path. |
| 39 | function hash_objfile() { |
| 40 | ORIGINAL_NAME="$1" |
Googler | 2b137e8 | 2016-09-19 16:43:05 +0000 | [diff] [blame] | 41 | ORIGINAL_HASH="$(/sbin/md5 -qs "${ORIGINAL_NAME}")" |
Chris Parsons | 591d1e1 | 2016-06-14 13:34:30 +0000 | [diff] [blame] | 42 | SYMLINK_NAME="${ORIGINAL_NAME%.o}_${ORIGINAL_HASH}.o" |
Googler | 2b137e8 | 2016-09-19 16:43:05 +0000 | [diff] [blame] | 43 | if [[ ! -e "$SYMLINK_NAME" ]]; then |
| 44 | ln -sf "$(basename "$ORIGINAL_NAME")" "$SYMLINK_NAME" |
| 45 | fi |
Chris Parsons | 591d1e1 | 2016-06-14 13:34:30 +0000 | [diff] [blame] | 46 | echo "$SYMLINK_NAME" |
| 47 | } |
| 48 | |
| 49 | ARGS=() |
| 50 | |
| 51 | while [[ $# -gt 0 ]]; do |
| 52 | ARG="$1" |
| 53 | shift |
| 54 | |
| 55 | # Libtool artifact symlinking. Apple's libtool has a bug when there are two |
| 56 | # input files with the same basename. We thus create symlinks that are named |
| 57 | # with a hash suffix for each input, and pass them to libtool. |
| 58 | # See b/28186497. |
| 59 | case "${ARG}" in |
| 60 | # Filelist flag, need to symlink each input in the contents of file and |
| 61 | # pass a new filelist which contains the symlinks. |
| 62 | -filelist) |
| 63 | ARGS+=("${ARG}") |
| 64 | ARG="$1" |
| 65 | shift |
| 66 | HASHED_FILELIST="${ARG%.objlist}_hashes.objlist" |
| 67 | rm -f "${HASHED_FILELIST}" |
Googler | 48859c1 | 2016-09-23 17:16:37 +0000 | [diff] [blame] | 68 | # Use python helper script for fast md5 calculation of many strings. |
| 69 | python "${MY_LOCATION}/make_hashed_objlist.py" "${ARG}" "${HASHED_FILELIST}" |
Chris Parsons | 591d1e1 | 2016-06-14 13:34:30 +0000 | [diff] [blame] | 70 | ARGS+=("${HASHED_FILELIST}") |
| 71 | ;; |
Chris Parsons | 56e6446 | 2016-08-12 13:18:51 +0000 | [diff] [blame] | 72 | # Output flag |
| 73 | -o) |
| 74 | ARGS+=("${ARG}") |
| 75 | ARG="$1" |
| 76 | shift |
| 77 | ARGS+=("${ARG}") |
| 78 | OUTPUTFILE="${ARG}" |
| 79 | ;; |
Chris Parsons | 591d1e1 | 2016-06-14 13:34:30 +0000 | [diff] [blame] | 80 | # Flags with no args |
| 81 | -static|-s|-a|-c|-L|-T|-no_warning_for_no_symbols) |
| 82 | ARGS+=("${ARG}") |
| 83 | ;; |
| 84 | # Single-arg flags |
Chris Parsons | 56e6446 | 2016-08-12 13:18:51 +0000 | [diff] [blame] | 85 | -arch_only|-syslibroot) |
Chris Parsons | 591d1e1 | 2016-06-14 13:34:30 +0000 | [diff] [blame] | 86 | ARGS+=("${ARG}") |
| 87 | ARG="$1" |
| 88 | shift |
| 89 | ARGS+=("${ARG}") |
| 90 | ;; |
| 91 | # Any remaining flags are unexpected and may ruin flag parsing. |
| 92 | -*) |
| 93 | echo "Unrecognized libtool flag ${ARG}" |
| 94 | exit 1 |
| 95 | ;; |
Chris Parsons | ff93aa0 | 2016-09-29 14:29:49 +0000 | [diff] [blame] | 96 | # Archive inputs can remain untouched, as they come from other targets. |
| 97 | *.a) |
| 98 | ARGS+=("${ARG}") |
| 99 | ;; |
Chris Parsons | 591d1e1 | 2016-06-14 13:34:30 +0000 | [diff] [blame] | 100 | # Remaining args are input objects |
| 101 | *) |
| 102 | ARGS+=("$(echo "$(hash_objfile "${ARG}")")") |
| 103 | ;; |
| 104 | esac |
| 105 | done |
| 106 | |
Chris Parsons | 56e6446 | 2016-08-12 13:18:51 +0000 | [diff] [blame] | 107 | # Ensure 0 timestamping for hermetic results. |
| 108 | export ZERO_AR_DATE=1 |
| 109 | |
Chris Parsons | 591d1e1 | 2016-06-14 13:34:30 +0000 | [diff] [blame] | 110 | "${WRAPPER}" libtool "${ARGS[@]}" |
Chris Parsons | 56e6446 | 2016-08-12 13:18:51 +0000 | [diff] [blame] | 111 | |
| 112 | # Prevents a pre-Xcode-8 bug in which passing zero-date archive files to ld |
| 113 | # would cause ld to error. |
| 114 | touch "$OUTPUTFILE" |