Commit | Line | Data |
---|---|---|
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 | ||
11 | set -e | |
12 | ||
13 | CTFCONVERT=@CTFCONVERT@ | |
14 | CTFMERGE=@CTFMERGE@ | |
15 | ctf_label=openafs | |
16 | ||
17 | mode="$1" | |
18 | shift | |
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. | |
26 | if [ x"$CTFCONVERT" = x ] || [ x"$CTFMERGE" = x ] ; then | |
27 | exit 0 | |
28 | fi | |
29 | ||
30 | # Look for our target file (indicated by '-o target'), and if debugging is | |
31 | # turned on (indicated by '-g') | |
32 | target= | |
33 | target_next= | |
34 | debug= | |
35 | for 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 | |
48 | done | |
49 | ||
50 | if [ 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 | |
57 | fi | |
58 | ||
59 | if [ x"$debug" = x ] ; then | |
60 | # Don't run ctfconvert/ctfmerge if debugging isn't turned on. | |
61 | exit 0 | |
62 | fi | |
63 | ||
64 | if [ x"$target" = x ] ; then | |
65 | echo "$0: error: cannot extract target from args" >&2 | |
66 | exit 1 | |
67 | fi | |
68 | ||
69 | echo_run() { | |
70 | if [ x"$V" != x0 ] ; then | |
71 | echo "$@" | |
72 | fi | |
73 | "$@" | |
74 | } | |
75 | ||
76 | case "$mode" in | |
77 | cc) | |
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 | ||
83 | ld) | |
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 | ;; | |
112 | esac |