summaryrefslogtreecommitdiff
path: root/patches/elf2flt/4820f0dbb77cd6564d5fa0817218fe2a1fb99f32/100-filter-includes.patch
blob: 18bd46a7ff21f73356ce366d3c00b20c62b62daa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
From 74f694a5147089463eecc93034d68278c1fc7561 Mon Sep 17 00:00:00 2001
From: Alexey Neyman <stilor@att.net>
Date: Mon, 27 Feb 2017 01:20:10 -0800
Subject: [PATCH] When looking for binutils/BFD headers, create a local include
 dir

... and filter only those headers that elf2flt binaries are going
to use, to minimize the chance of clashes with system headers.

Signed-off-by: Alexey Neyman <stilor@att.net>
---
 Makefile.in         | 13 ++++++--
 configure           |  4 +--
 configure.ac        |  4 +--
 mk-local-include.sh | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 103 insertions(+), 7 deletions(-)
 create mode 100755 mk-local-include.sh

diff --git a/Makefile.in b/Makefile.in
index a6feea6..d3e75bd 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -14,7 +14,9 @@ CPU = @target_cpu@
 TARGET = @target_alias@
 SYMBOL_PREFIX = @SYMBOL_PREFIX@
 CFLAGS = @CFLAGS@
-INCLUDES = -I$(srcdir) @bfd_include_dir@ @binutils_include_dir@ @zlib_include_dir@
+INCLUDES = -I$(srcdir) -Ilocal-include @zlib_include_dir@
+BFD_INCLUDE_DIR = @bfd_include_dir@
+BINUTILS_INCLUDE_DIR = @binutils_include_dir@
 CPPFLAGS = @CPPFLAGS@ $(DEFS) $(INCLUDES)
 LDFLAGS = @LDFLAGS@
 LDLIBS = @LIBS@
@@ -101,7 +103,7 @@ check-flthdr:
 check: check-flthdr
 
 clean:
-	-rm -f $(PROGS) *.$(OBJEXT) .deps
+	-rm -f $(PROGS) *.$(OBJEXT) .deps local-include
 
 distclean: clean
 	-rm -f Makefile config.log config.status config.cache ld-elf2flt
@@ -121,5 +123,10 @@ install:
 	$(INSTALL) -m 644 $(SRC_LDFILE) $(DESTDIR)$(target_libdir)/$(LDFILE)
 
 sinclude .deps
-.deps:
+.deps: local-include/.stamp
 	$(CC) -MM $(CPPFLAGS) $(srcdir)/*.c > .deps
+
+local-include/.stamp: $(srcdir)/mk-local-include.sh
+	bash $(srcdir)/mk-local-include.sh local-include '$(CC)' \
+		'$(BFD_INCLUDE_DIR)' '$(BINUTILS_INCLUDE_DIR)' $(DEFS)
+	touch $@
diff --git a/configure b/configure
index 3a4e8d6..af64990 100755
--- a/configure
+++ b/configure
@@ -3931,12 +3931,12 @@ fi
 
 bfd_include_dir=
 if test "$ac_bfd_include_dir" != "NONE"; then
-  bfd_include_dir="-I$ac_bfd_include_dir"
+  bfd_include_dir="$ac_bfd_include_dir"
 fi
 
 binutils_include_dir=
 if test "$ac_binutils_include_dir" != "NONE"; then
-  binutils_include_dir="-I$ac_binutils_include_dir"
+  binutils_include_dir="$ac_binutils_include_dir"
 fi
 
 zlib_include_dir=
diff --git a/configure.ac b/configure.ac
index 6002894..b7fb790 100644
--- a/configure.ac
+++ b/configure.ac
@@ -135,12 +135,12 @@ fi
 
 bfd_include_dir=
 if test "$ac_bfd_include_dir" != "NONE"; then
-  bfd_include_dir="-I$ac_bfd_include_dir"
+  bfd_include_dir="$ac_bfd_include_dir"
 fi
 
 binutils_include_dir=
 if test "$ac_binutils_include_dir" != "NONE"; then
-  binutils_include_dir="-I$ac_binutils_include_dir"
+  binutils_include_dir="$ac_binutils_include_dir"
 fi
 
 zlib_include_dir=
diff --git a/mk-local-include.sh b/mk-local-include.sh
new file mode 100755
index 0000000..a6e8a63
--- /dev/null
+++ b/mk-local-include.sh
@@ -0,0 +1,89 @@
+#!/bin/bash
+
+# Binutils provide certain headers that are clashing with system-wide
+# headers on Cygwin and MacOS. They do so in different manners, though,
+# so it is not possible to just tweak the inclusion/library order.
+# Hence, we prepare a filtered inclusion directory where we only place
+# the headers that we need for BFD/binutils. To determine them, we'll
+# need to preprocess a sample file including the headers that will be used,
+# and have the compiler output the actual list of included headers for us.
+
+# Usage:
+#   mk-local-include.sh DIR CC BFD-INCLUDES BINUTILS-INCLUDES [DEFINES...]
+
+local_inc=$1
+cc=$2
+bfd_inc=$3
+binutils_inc=$4
+shift 4
+# What remains are defines to pass to GCC
+
+case "${local_inc}" in
+	*/*)	echo "Local include dir may only have one component" >&2; exit 1;;
+	.|..)	echo "Local include dir may not be . or .." >&2; exit 1;;
+esac
+
+# Re-create it from a clean slate
+rm -rf "${local_inc}"
+mkdir -p "${local_inc}"
+
+# Create a dummy source file that we'll preprocess
+{
+	echo "#include <libiberty.h>"
+	echo "#include <filenames.h>"
+	echo "#include <bfd.h>"
+} > "${local_inc}/_dummy.c"
+
+$cc ${bfd_inc:+-I${bfd_inc}} ${binutils_inc:+-I${binutils_inc}} "$@" \
+		-E -o /dev/null -H "${local_inc}/_dummy.c" 2>&1 | \
+	sed -rn -e 's/^\.+ //p' > "${local_inc}/_hdr.list"
+if [ $? != 0 ]; then
+	echo "Failed to locate libiberty.h/bfd.h headers" >&2
+	exit 1
+fi
+
+# Now go over the list twice. First determine how the compiler printed
+# the location of <bfd.h> and <libiberty.h>. They themselves may be in
+# a system include directory. We'll create a mess of symlinks in that case,
+# but they'll still point to the system headers.
+while read f; do
+	case "$f" in
+	*/bfd.h)	bfd_loc=${f%/bfd.h};;
+	*/libiberty.h)	binutils_loc=${f%/libiberty.h};;
+	esac
+done < $local_inc/_hdr.list
+
+# Now symlink the headers that reside in the same directory as the headers
+# we're looking for, or in any subdirectory thereof.
+create_link() {
+	local f=$1
+	local bd=$2
+	local relpath lnkname bname src
+
+	relpath=${f#${bd}/}
+	lnkname=${local_inc}/${relpath}
+	bname=${relpath##*/}
+	if [ "${bname}" != "${relpath}" ]; then
+		mkdir -p "${relpath%/*}"
+	fi
+	case "${f}" in
+	/*)	src=${f};;
+	*)	src=../${f};; # account for one extra path component (DIR)
+	esac
+	if [ ! -L "${lnkname}" ]; then
+		ln -s "${src}" "${lnkname}"
+	fi
+}
+
+while read f; do
+	case "$f" in
+	${bfd_loc}/*)		create_link "$f" "${bfd_loc}";;
+	${binutils_loc}/*)	create_link "$f" "${binutils_loc}";;
+	esac
+done < $local_inc/_hdr.list
+
+# Final bit of secret knowledge. We need ELF headers, and the exact headers
+# depend on the selected target. Fortunately, they are all in the 'elf/'
+# subdirectory of binutils include directory, which we get by searching for
+# <libiberty.h>.
+create_link "${binutils_loc}/elf" "${binutils_loc}"
-- 
2.9.3