backport to buster
[hcoop/debian/openafs.git] / src / config / cc-wrapper.in
CommitLineData
805e021f
CE
1#!/bin/sh
2#
3# usage:
4# cc-wrapper cc /path/to/cc -c -o foo.o foo.c
5# cc-wrapper ld /path/to/ld -o foo foo.o
6#
7# This wraps running the compiler or linker, so we can tweak the files
8# generated. Currently, this just involves running ctfconvert or ctfmerge on
9# the relevant files, if available.
10
11set -e
12
13CTFCONVERT=@CTFCONVERT@
14CTFMERGE=@CTFMERGE@
15ctf_label=openafs
16
17mode="$1"
18shift
19
20# First, run the command that we're wrapping.
21"$@"
22
23# If we've reached here, the compiler/linker finished successfully, so now we
24# can run ctfconvert/ctfmerge, if we're configured to. If we're not going to
25# process ctf stuff, we can just exit immediately.
26if [ x"$CTFCONVERT" = x ] || [ x"$CTFMERGE" = x ] ; then
27 exit 0
28fi
29
30# Look for our target file (indicated by '-o target'), and if debugging is
31# turned on (indicated by '-g')
32target=
33target_next=
34debug=
35for arg in "$@" ; do
36 if [ x"$target_next" = x1 ] ; then
37 target="$arg"
38 target_next=
39 continue
40 fi
41
42 case "$arg" in
43 -o) target_next=1
44 ;;
45 -g) debug=1
46 ;;
47 esac
48done
49
50if [ x"$OPENAFS_CC_WRAPPER_DEBUG_FLAG" != x ] ; then
51 # Normally we try to determine if debugging is turned on by searching $@
52 # for "-g". But sometimes we link something with debugging info and don't
53 # pass -g (the Solaris kernel module, for example). In this case, the
54 # caller can specify whether that debugging is turned on by setting the env
55 # var OPENAFS_CC_WRAPPER_DEBUG_FLAG to a nonempty string.
56 debug=1
57fi
58
59if [ x"$debug" = x ] ; then
60 # Don't run ctfconvert/ctfmerge if debugging isn't turned on.
61 exit 0
62fi
63
64if [ x"$target" = x ] ; then
65 echo "$0: error: cannot extract target from args" >&2
66 exit 1
67fi
68
69echo_run() {
70 if [ x"$V" != x0 ] ; then
71 echo "$@"
72 fi
73 "$@"
74}
75
76case "$mode" in
77cc)
78 # It looks like we compiled a single object. For that, we just need to run
79 # 'ctfconvert' against the target .o.
80 echo_run "$CTFCONVERT" -g -l $ctf_label "$target"
81 ;;
82
83ld)
84 # It looks like we're linking an executable. For that, we need to give
85 # 'ctfmerge' every .o and .a that we're linking against.
86
87 merge_objs=
88 for arg in "$@" ; do
89 case "$arg" in
90 *.[oa])
91 merge_objs="$merge_objs $arg"
92 ;;
93 esac
94 done
95
96 if [ x"$merge_objs" = x ] ; then
97 # If we can't find any .o or .a files that we're linking into the
98 # target executable, our caller is probably compiling/linking directly
99 # from foo.c into foo (skipping foo.o). For that, we just need to run
100 # ctfconvert/ctfmerge on the executable itself.
101 echo_run "$CTFCONVERT" -g -l $ctf_label "$target"
102 merge_objs="$target"
103 fi
104
105 echo_run "$CTFMERGE" -g -l $ctf_label -o "$target" $merge_objs
106 ;;
107
108*)
109 echo "$0: Unknown mode '$mode'" >&2
110 exit 1
111 ;;
112esac