blob: e1275758ea44f616f868a2248039991e528a8bb6 [file] [log] [blame]
Chris Parsons1f67a7b2016-05-23 19:23:24 +00001#!/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#
Googler48859c12016-09-23 17:16:37 +000019# It creates symbolic links for all input files with a path-hash appended
Chris Parsons1f67a7b2016-05-23 19:23:24 +000020# to their original name (foo.o becomes foo_{md5sum}.o). This is to circumvent
Googler48859c12016-09-23 17:16:37 +000021# a bug in the original libtool that arises when two input files have the same
Chris Parsons1f67a7b2016-05-23 19:23:24 +000022# base name (even if they are in different directories).
23
24set -eu
25
cparsonscc219982017-05-03 22:13:44 +020026# A trick to allow invoking this script in multiple contexts.
27if [ -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
33fi
34
Chris Parsons1f67a7b2016-05-23 19:23:24 +000035WRAPPER="${MY_LOCATION}/xcrunwrapper.sh"
36
Chris Parsons591d1e12016-06-14 13:34:30 +000037# Creates a symbolic link to the input argument file and returns the symlink
38# file path.
39function hash_objfile() {
40 ORIGINAL_NAME="$1"
Googler2b137e82016-09-19 16:43:05 +000041 ORIGINAL_HASH="$(/sbin/md5 -qs "${ORIGINAL_NAME}")"
Chris Parsons591d1e12016-06-14 13:34:30 +000042 SYMLINK_NAME="${ORIGINAL_NAME%.o}_${ORIGINAL_HASH}.o"
Googler2b137e82016-09-19 16:43:05 +000043 if [[ ! -e "$SYMLINK_NAME" ]]; then
44 ln -sf "$(basename "$ORIGINAL_NAME")" "$SYMLINK_NAME"
45 fi
Chris Parsons591d1e12016-06-14 13:34:30 +000046 echo "$SYMLINK_NAME"
47}
48
49ARGS=()
50
51while [[ $# -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}"
Googler48859c12016-09-23 17:16:37 +000068 # Use python helper script for fast md5 calculation of many strings.
69 python "${MY_LOCATION}/make_hashed_objlist.py" "${ARG}" "${HASHED_FILELIST}"
Chris Parsons591d1e12016-06-14 13:34:30 +000070 ARGS+=("${HASHED_FILELIST}")
71 ;;
Chris Parsons56e64462016-08-12 13:18:51 +000072 # Output flag
73 -o)
74 ARGS+=("${ARG}")
75 ARG="$1"
76 shift
77 ARGS+=("${ARG}")
78 OUTPUTFILE="${ARG}"
79 ;;
Chris Parsons591d1e12016-06-14 13:34:30 +000080 # 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 Parsons56e64462016-08-12 13:18:51 +000085 -arch_only|-syslibroot)
Chris Parsons591d1e12016-06-14 13:34:30 +000086 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 Parsonsff93aa02016-09-29 14:29:49 +000096 # Archive inputs can remain untouched, as they come from other targets.
97 *.a)
98 ARGS+=("${ARG}")
99 ;;
Chris Parsons591d1e12016-06-14 13:34:30 +0000100 # Remaining args are input objects
101 *)
102 ARGS+=("$(echo "$(hash_objfile "${ARG}")")")
103 ;;
104 esac
105done
106
Chris Parsons56e64462016-08-12 13:18:51 +0000107# Ensure 0 timestamping for hermetic results.
108export ZERO_AR_DATE=1
109
Chris Parsons591d1e12016-06-14 13:34:30 +0000110"${WRAPPER}" libtool "${ARGS[@]}"
Chris Parsons56e64462016-08-12 13:18:51 +0000111
112# Prevents a pre-Xcode-8 bug in which passing zero-date archive files to ld
113# would cause ld to error.
114touch "$OUTPUTFILE"