blob: c71d49830ef7be15383a9286c1c50d7a181b724d [file] [log] [blame]
Marcel Hlopko74b94322016-10-11 15:30:49 +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# This script handles interface library generation for dynamic library
18# link action.
19#
20# Bazel can be configured to generate external interface library script
21# to generate interface libraries in CppLinkAction for dynamic libraries.
22# This is not needed on Windows (as the "interface" libraries are
23# generated by default). This script therefore handles the cases when
24# external script is provided, or when no script should be used.
25
26set -eu
27
28E_LINKER_COMMAND_NOT_FOUND=12
29E_INTERFACE_BUILDER_NOT_FOUND=13
30
Marcel Hlopko74b94322016-10-11 15:30:49 +000031
plf3010e572019-03-11 07:02:28 -070032SUFFIX=".rewritten"
Marcel Hlopko74b94322016-10-11 15:30:49 +000033
plf3010e572019-03-11 07:02:28 -070034other_args=""
Marcel Hlopko74b94322016-10-11 15:30:49 +000035
plf3010e572019-03-11 07:02:28 -070036if [[ "$#" -eq 1 ]]; then
37 if [[ "$1" != @* ]]; then
38 echo "Parameter file must start with @" 1>&2;
39 exit "$E_LINKER_COMMAND_NOT_FOUND"
40 fi
41
42 filename=$(echo "$1" | cut -c2-)
43 first_five_lines=$(head -n 5 $filename)
44
45 # Should generate interface library switch (<yes|no>); if the value is "no",
46 # following 3 args are ignored (but must be present)
47 GENERATE_INTERFACE_LIBRARY=$(echo "$first_five_lines" | head -n1 | tail -n1)
48 # Tool which can generate interface library from dynamic library file
49 INTERFACE_LIBRARY_BUILDER=$(echo "$first_five_lines" | head -n2 | tail -n1)
50 # Dynamic library from which we want to generate interface library
51 DYNAMIC_LIBRARY=$(echo "$first_five_lines" | head -n3 | tail -n1)
52 # Resulting interface library
53 INTERFACE_LIBRARY=$(echo "$first_five_lines" | head -n4 | tail -n1)
54 # The command used to generate the dynamic library
55 LINKER_COMMAND=$(echo "$first_five_lines" | head -n5 | tail -n1)
56
57 rest_of_lines=$(tail -n +6 $filename)
58 new_param_file="${filename}${SUFFIX}"
59 echo "$rest_of_lines" > $new_param_file
60 other_args="@$new_param_file"
61
62 if [[ ! -e "$LINKER_COMMAND" ]]; then
63 echo "Linker command ($LINKER_COMMAND) not found." 1>&2;
64 exit "$E_LINKER_COMMAND_NOT_FOUND"
65 fi
66
67 if [[ "no" == "$GENERATE_INTERFACE_LIBRARY" ]]; then
68 INTERFACE_GENERATION=:
69 else
70 if [[ ! -e "$INTERFACE_LIBRARY_BUILDER" ]]; then
71 echo "Interface library builder ($INTERFACE_LIBRARY_BUILDER)
72 not found." 1>&2;
73 exit "$E_INTERFACE_BUILDER_NOT_FOUND"
74 fi
75 INTERFACE_GENERATION="${INTERFACE_LIBRARY_BUILDER} ${DYNAMIC_LIBRARY}
76 ${INTERFACE_LIBRARY}"
77 fi
78
79 ${LINKER_COMMAND} "$other_args" && ${INTERFACE_GENERATION}
Marcel Hlopko74b94322016-10-11 15:30:49 +000080else
plf3010e572019-03-11 07:02:28 -070081 # TODO(b/113358321): Remove this branch once projects are migrated to not
82 # splitting the linking command line.
83 # Should generate interface library switch (<yes|no>); if the value is "no",
84 # following 3 args are ignored (but must be present)
85 GENERATE_INTERFACE_LIBRARY="$1"
86 # Tool which can generate interface library from dynamic library file
87 INTERFACE_LIBRARY_BUILDER="$2"
88 # Dynamic library from which we want to generate interface library
89 DYNAMIC_LIBRARY="$3"
90 # Resulting interface library
91 INTERFACE_LIBRARY="$4"
92 # The command used to generate the dynamic library
93 LINKER_COMMAND="$5"
94 shift 5
95 if [[ ! -e "$LINKER_COMMAND" ]]; then
96 echo "Linker command ($LINKER_COMMAND) not found." 1>&2;
97 exit "$E_LINKER_COMMAND_NOT_FOUND"
98 fi
Marcel Hlopko74b94322016-10-11 15:30:49 +000099
plf3010e572019-03-11 07:02:28 -0700100 if [[ "no" == "$GENERATE_INTERFACE_LIBRARY" ]]; then
101 INTERFACE_GENERATION=:
102 else
103 if [[ ! -e "$INTERFACE_LIBRARY_BUILDER" ]]; then
104 echo "Interface library builder ($INTERFACE_LIBRARY_BUILDER)
105 not found." 1>&2;
106 exit "$E_INTERFACE_BUILDER_NOT_FOUND"
107 fi
108 INTERFACE_GENERATION="${INTERFACE_LIBRARY_BUILDER} ${DYNAMIC_LIBRARY}
109 ${INTERFACE_LIBRARY}"
110 fi
111
112 ${LINKER_COMMAND} "$@" && ${INTERFACE_GENERATION}
113fi