Debian Codegraph @

test/hello-static-glibc/Makefile

raw symbols SCIP
1 THIS_MAKEFILE = $(lastword $(MAKEFILE_LIST))
2 DIRNAME := $(realpath .)
3 BASENAME := $(shell basename "$(DIRNAME)" | sed 's/-.*//' )
4 # $(warning BASENAME is $(BASENAME))
5
6 OUTPUTS := $(BASENAME) $(BASENAME).map $(BASENAME).cmd $(BASENAME).repeat-cmd $(BASENAME).collect-cmd $(BASENAME).env
7
8 OBJECTS ?= $(BASENAME).o
9
10 default: $(OUTPUTS) repeat
11
12 LDFLAGS ?= -static
13
14 # Avoid LTO/plugin cruft in our link commands, for now.
15 LDFLAGS += -fno-lto
16
17 # Avoid relaxation, for now. ARGH: this seems not to prevent all instruction rewriting.
18 LDFLAGS += -Wl,--no-relax
19
20 # Cancel built-in rules -- why?
21 %: %.c
22 %: %.cc
23 %: %.o
24
25 .SECONDARY: $(OBJECTS)
26
27 # We scrape the actual link command,
28 # then produce local copies of all the inputs
29 # and rewrite the command to use these copies.
30 $(BASENAME).cmd $(BASENAME).map $(BASENAME): build-$(BASENAME)
31
32 # Which compiler we need to use to drive the linker
33 # depends on the source binary.
34 ifeq ($(shell basename "$(DIRNAME)" | sed 's/.*c++.*/c++/'),c++)
35 COMPILER := $(CXX)
36 else
37 COMPILER := $(CC)
38 endif
39 $(warning COMPILER is $(COMPILER))
40
41 .PHONY: build-$(BASENAME)
42 build-$(BASENAME): $(OBJECTS)
43 $(COMPILER) -save-temps -o "$(BASENAME)" $+ -Wl,-Map=$(BASENAME).map $(LDFLAGS) $(LDLIBS)
44 $(COMPILER) -save-temps -\#\#\# -o "$(BASENAME)" $+ -Wl,-Map=$(BASENAME).map $(LDFLAGS) $(LDLIBS) 2>&1 | tee $(BASENAME).cmd
45
46 $(BASENAME).env: $(BASENAME).cmd
47 cat "$<" | grep '^[A-Z_]*=' | sed 's/^\([A-Z_]*=\)\(.*\)/\1"\2"/' > "$@"
48
49 $(BASENAME).collect-cmd: $(BASENAME).cmd
50 cat "$<" | grep /collect > "$@"
51
52 ifeq ($(filter -static,$(LDFLAGS)),)
53 LIB_SUFFIXES := so a
54 else
55 LIB_SUFFIXES := a
56 endif
57
58 $(BASENAME).repeat-cmd: $(BASENAME).collect-cmd $(BASENAME).env
59 unquote () { \
60 eval echo "$$1"; \
61 }; \
62 lds_to_cmdline () { \
63 sed 's/AS_NEEDED *(\([^)]*\))/--as-needed \1 --no-as-needed/g' | \
64 sed 's/OUTPUT_FORMAT(\([^)]*\))//g' | \
65 sed 's/GROUP *(\([^)]*\))/--start-group \1 --end-group/g' | \
66 tr '\n' ' ' | sed 's^/\*.*\*/^^'; \
67 }; \
68 for arg in $$( cat "$<" ); do \
69 unquoted_arg="$$( unquote "$$arg" )"; \
70 case "$$unquoted_arg" in \
71 (*/collect*) \
72 echo "ld" ;; \
73 (/*) cp "$$unquoted_arg" ./ && echo $$(basename "$$unquoted_arg" );; \
74 (-l*) ( . ./$(BASENAME).env && \
75 tmpfile=$$(mktemp) && \
76 echo $$LIBRARY_PATH | tr ':' '\n' > $$tmpfile && \
77 found="" && for suffix in $(LIB_SUFFIXES); do while read line ; do \
78 search_fname_stem="$$line"/lib$$( echo "$$unquoted_arg" | sed 's/^-l//'); \
79 if [ -e "$$search_fname_stem".$$suffix ]; then \
80 echo -n "Found: " 1>&2; \
81 cp "$$search_fname_stem".$$suffix ./ && \
82 found="$$search_fname_stem".$$suffix && \
83 (file "$$found" 2>&1 | grep ASCII >/dev/null && (for word in $$(cat "$$found" | lds_to_cmdline); do \
84 case "$$word" in \
85 (/*) cp "$$word" ./ && basename "$$word";; \
86 (*) echo "$$word" ;; \
87 esac; \
88 done) || \
89 echo $$(basename "$$search_fname_stem".$$suffix) ) | tee /dev/stderr; break 2; \
90 else \
91 echo "Did not find $$search_fname_stem.$$suffix; trying next in path" 1>&2; \
92 fi; \
93 done < "$$tmpfile"; \
94 if [ -z "$$found" ]; then echo "Did not find $$unquoted_arg with suffix $$suffix" 1>&2; fi; done; \
95 rm -f "$$tmpfile"; \
96 ) \
97 ;; \
98 (-L*) ;; \
99 (-f*) ;; \
100 (-*) echo "$$unquoted_arg" ;; \
101 (*) echo "$$unquoted_arg" ;; \
102 esac; \
103 done | tr '\n' ' ' > "$@"; echo >> "$@"
104
105 .PHONY: repeat
106 repeat: $(OBJECTS) $(BASENAME).repeat-cmd
107 eval $$( cat $(BASENAME).repeat-cmd )
108
109 .PHONY: repeat
110 $(BASENAME).with-relocs: $(OBJECTS) $(BASENAME).repeat-cmd
111 eval $$( cat $(BASENAME).repeat-cmd | sed 's/^ld/ld -q/' | sed 's#-o $(BASENAME)#-o $@#' )
112 clean:
113 rm -f $(OUTPUTS) *.o *.a *.so *.so.*