Debian Codegraph @

junixsocket-native/crossclang/bin/prepare-target-sdk

raw
1 #!/usr/bin/env bash
2 #
3 # crossclang
4 # A helper script to simplify cross-compilation
5 #
6 # prepare-target-sdk is to be run on the target platform
7 #
8 # Copyright 2019, 2020 Christian Kohlschütter
9 #
10 # Licensed under the Apache License, Version 2.0 (the "License");
11 # you may not use this file except in compliance with the License.
12 # You may obtain a copy of the License at
13 #
14 # http://www.apache.org/licenses/LICENSE-2.0
15 #
16 # Unless required by applicable law or agreed to in writing, software
17 # distributed under the License is distributed on an "AS IS" BASIS,
18 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 # See the License for the specific language governing permissions and
20 # limitations under the License.
21 #
22
23 targetrootbase="$(dirname $0)/../target-sdks/"
24 triple=
25
26 printHelp=0
27 compiler=("clang" "gcc")
28 linkerBinary=("ld")
29 allPaths=0
30 includeInstallLibs=0
31 while [ $# -gt 0 ]; do
32 v="$1"
33 shift
34
35 case "$v" in
36 -h | --help ) printHelp=1 ;;
37 --compiler ) compiler=("$1"); shift; ;;
38 --linker ) linkerBinary=("$1"); shift; ;;
39 --all-paths ) allPaths=1; ;;
40 --include-install-libs ) includeInstallLibs=1; ;;
41 * ) echo "Illegal argument: $v" >&2 ; printHelp=1; ;;
42 esac
43 done
44
45 if [ $printHelp -eq 1 ]; then
46 cat <<EOT
47 Syntax: $0 [--help|-h]
48 [--compiler <path-to-compiler-binary>] [--linker <path-to-linker-binary>]
49 [--all-paths] [--include-install-libs]
50 EOT
51 exit 1
52 fi
53
54 compiler="$(PATH=/usr/bin:$PATH which ${compiler[@]} 2>/dev/null | grep -e "^/" | head -n 1)"
55 if [ -z "$compiler" ]; then
56 echo "clang/gcc not available; configuration may not be accurate" >&2
57 else
58 echo "Using compiler: $compiler"
59 fi
60
61 linkerBinary="$(PATH=/usr/bin:$PATH which ${linkerBinary[@]} 2>/dev/null | grep -e "^/" | head -n 1)"
62 if [ -z "$linkerBinary" ]; then
63 echo "Linker not available; configuration may not be accurate" >&2
64 else
65 echo "Using linker: $linkerBinary"
66 fi
67
68 if [ -z "$triple" ]; then
69 target=$($compiler -v 2>&1 | grep "^Target: " | head -n 1)
70 target=${target##* }
71 if [ -n "$target" ]; then
72 triple="$target"
73 fi
74 fi
75
76 echo "Compiler default triple: $triple"
77
78 if [ -z "$triple" ]; then
79 triple=$(cd /usr/lib/gcc/ ; ls -d *-* | head -n 1)
80 echo "Inferring triple from /usr/lib/gcc: $triple"
81 fi
82 if [ -z "$triple" ]; then
83 echo "Could not determine target triple" >&2
84 exit 1
85 fi
86 if [ -z "$targetrootbase" ]; then
87 echo "No targetrootbase specified" >&2
88 exit 1
89 fi
90
91 while [ "${targetrootbase:(-1)}" == "/" ]; do
92 targetrootbase="${targetrootbase%*/}"
93 done
94
95 targetroot="$targetrootbase/$triple"
96 echo "Target: $triple"
97 mkdir -p "$targetroot"
98
99 cd "$targetroot"
100 targetpwd=$(pwd)
101 cd - >/dev/null
102
103 echo "Location of target SDK: $targetpwd"
104
105 # add a few locations to the start of the include path (order is important!)
106 include_path=(
107 )
108 include_cpp_path=(
109 )
110
111 library_path=(
112 # /usr/lib/gcc/$triple/*
113 )
114
115 framework_path=(
116 )
117
118 include_path_count_initial=${#include_path[@]}
119 include_cpp_path_count_initial=${#include_cpp_path[@]}
120 library_path_count_initial=${#library_path[@]}
121 framework_path_count_initial=${#framework_path[@]}
122
123 # add linker paths from ld64 and friends
124 section=
125 IFS=$'\n'; for line in $(LANG=C "$compiler" -Xlinker -v 2>&1 ); do
126 if [ "${line:0:1}" == $'\t' ]; then
127 path="${line:1}"
128 case "$section" in
129 library) library_path+=("$path") ;;
130 framework) framework_path+=("$path") ;;
131 esac
132 else
133 section=
134 if [ "$line" == "Library search paths:" ]; then
135 section="library"
136 elif [ "$line" == "Framework search paths:" ]; then
137 section="framework"
138 fi
139 fi
140 done
141
142 # add linker paths from ld and friends
143 old_IFS=$IFS
144 IFS=' '; for f in $(LANG=C "$compiler" -Xlinker --verbose 2>/dev/null | grep SEARCH_DIR); do
145 f=${f#SEARCH_DIR(\"}
146 f=${f%\");}
147 # strip optional leading "="
148 f=${f#=}
149
150 if [[ -n "$f" && -d "$f" ]]; then
151 library_path+=("$f")
152 fi
153 done
154 IFS=${old_IFS}
155
156 # add include paths from clang or gcc
157 for lang in c c++; do
158 section=
159
160 # NOTE: Not using -nobuiltininc because it's not supported everywhere
161 IFS=$'\n'; for line in $( LANG=C "$compiler" -E -x "$lang" /dev/null -v 2>&1 ); do
162 if [ -z "$line" ]; then
163 continue
164 fi
165 if [ "${line:0:1}" != " " ]; then
166 section=
167 if [ "$line" == "#include <...> search starts here:" ]; then
168 section="include"
169 fi
170 continue
171 fi
172 path="${line:1}"
173
174 if [ "$path" == *" (framework directory)" ]; then
175 continue
176 fi
177
178 if [ ! -d "$path" ]; then
179 continue
180 fi
181 if [[ "$path" == *".xctoolchain/"* ]]; then
182 # echo "Ignoring toolchain include: $path" >&2
183 continue
184 fi
185
186 if [ "$lang" == "c" ]; then
187 include_path+=("$path")
188 elif [ "$lang" == "c++" ]; then
189 existing=0
190 for p in ${include_path[@]}; do
191 if [ "$p" == "$path" ]; then
192 existing=1
193 break
194 fi
195 done
196 if [ $existing -eq 0 ]; then
197 include_cpp_path+=("$path")
198 fi
199 else
200 echo "Unexpected language: $lang" >&2
201 fi
202 done
203 done
204
205 if [[ /bin/true ]]; then
206 libPaths="$(LANG=C "$compiler" -print-search-dirs | grep "^libraries: " | head -n 1)"
207 libPaths="${libPaths#libraries: }"
208 library_path+=($(
209 IFS=:
210 for f in $libPaths; do
211 # strip optional leading "="
212 f=${f#=}
213
214 echo $f
215 done
216 ))
217 fi
218
219
220 if [[ "$includeInstallLibs" -eq 1 ]]; then
221 installPaths="$(LANG=C "$compiler" -print-search-dirs | grep "^install: " | head -n 1)"
222 installPaths="${installPaths#install: }"
223 library_path+=($(
224 IFS=:
225 for f in $installPaths; do
226 # strip optional leading "="
227 f=${f#=}
228
229 echo $f
230 done
231 ))
232 fi
233
234 if [[ "$triple" == *"-darwin"* ]]; then
235 library_path+=(
236 /usr/lib
237 /usr/lib/system
238 )
239 fi
240
241 # Add /lib when missing but likely required
242 haveUsrLib=0
243 haveLib=0
244 for f in ${library_path[@]}; do
245 if [[ "$f" == "/usr/lib" ]]; then
246 haveUsrLib=1
247 elif [[ "$f" == "/lib" ]]; then
248 haveLib=1
249 fi
250 done
251 if [[ $haveUsrLib -eq 1 && $haveLib -eq 0 ]]; then
252 library_path+=(
253 /lib
254 )
255 fi
256
257 # fallback defaults
258 if [ ${#include_path[@]} -le $include_path_count_initial ]; then
259 include_path+=(
260 /usr/lib/gcc/$triple/*/include
261 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/*.sdk/usr/include
262 /usr/include
263 /usr/include/linux
264 )
265 fi
266
267 if [ ${#include_cpp_path[@]} -le $include_cpp_path_count_initial ]; then
268 include_cpp_path+=(
269 /usr/include/c++/*
270 /usr/include/c++/*/$triple
271 /usr/include/c++/*/backward
272 )
273 fi
274
275 if [ ${#library_path[@]} -le $library_path_count_initial ]; then
276 library_path+=(
277 /lib
278 /usr/lib
279 )
280 fi
281
282 if [ ${#framework_path[@]} -le $framework_path_count_initial ]; then
283 framework_path+=(
284 )
285 fi
286
287 declare -a arrIn
288 declare -a arrOut
289 function checkPaths() {
290 arrOut=()
291 for path in ${arrIn[@]}; do
292 if [ "${path:0:1}" != "/" ]; then
293 echo "Path must be absolute: $path" >&2
294 continue
295 fi
296 if [ ! -d "$path" ]; then
297 continue
298 fi
299 path=$(cd "$path"; pwd -P)
300
301 skip=0
302 for p in ${arrOut[@]}; do
303 if [ "$p" == "$path" ]; then
304 skip=1
305 break
306 fi
307 done
308
309 if [[ "$path" == "/usr/local/"* && $allPaths -eq 0 ]]; then
310 # skip "/usr/local/..." paths by default
311 echo "Skipping path (run with --all-paths to force): $path" >&2
312 continue
313 fi
314 if [[ "$path" == *".xctoolchain/"* && $allPaths -eq 0 ]]; then
315 # skip Xcode toolchain paths by default
316 echo "Skipping path (run with --all-paths to force): $path" >&2
317 continue
318 fi
319
320 if [ $skip -eq 0 ]; then
321 arrOut+=("$path")
322 fi
323 done
324 }
325
326 arrIn=(${include_path[@]})
327 checkPaths
328 include_path=(${arrOut[@]})
329
330 arrIn=(${include_cpp_path[@]})
331 checkPaths
332 include_cpp_path=(${arrOut[@]})
333
334 arrIn=(${library_path[@]})
335 checkPaths
336 library_path=(${arrOut[@]})
337
338 arrIn=(${framework_path[@]})
339 checkPaths
340 framework_path=(${arrOut[@]})
341
342 linker=
343 if [ -n "$($linkerBinary -help 2>/dev/null| grep ld64)" ]; then
344 linker=ld64
345 elif [ -n "$($linkerBinary -version 2>/dev/null| grep GNU)" ]; then
346 linker=ld
347 fi
348
349 targetConf="$targetroot/target.conf"
350 targetH="$targetroot/target.h"
351
352 cat >"$targetConf" <<EOT
353 # crossclang target configuration file
354 # created: $(date)
355 # on host: $(hostname)
356 # by user: $(whoami)
357 target_triple=$triple
358 target_linker=$linker
359 target_include_path=( $(for f in ${include_path[@]}; do printf "\n %q" $f; done )
360 )
361 target_include_cpp_path=( $(for f in ${include_cpp_path[@]}; do printf "\n %q" $f; done )
362 )
363 target_library_path=( $(for f in ${library_path[@]}; do printf "\n %q" $f; done )
364 )
365 target_framework_path=( $(for f in ${framework_path[@]}; do printf "\n %q" $f; done )
366 )
367 EOT
368
369 cat >"$targetH" <<EOT
370 /* crossclang target.h -- This header will automatically be included during compilation */
371 EOT
372
373 targetroot=$(cd "$targetroot" ; pwd)
374
375 cat <<EOT
376
377 Now synchronize the SDK with the following command:
378 $(dirname $0)/sync-target-sdk "$targetroot"
379
380 If you do not wish to synchronize libraries at this point, enter this instead:
381 $(dirname $0)/sync-target-sdk -L "$targetroot"
382 EOT