Skip to content

Commit 06bd759

Browse files
committed
wrappers: Add a wrapper for clang-scan-deps
This picks the architecture by looking for a triple prefix in the nested command, and passes it as an explicit flag, to let clang-scan-deps find it. We also pass -stdlib=libc++, to let it find the libc++ headers as expected.
1 parent 5ba9e1e commit 06bd759

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

install-wrappers.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ if [ -n "$HOST" ] && [ -n "$EXEEXT" ]; then
126126
done
127127
fi
128128
$CC wrappers/clang-target-wrapper.c -o "$PREFIX/bin/clang-target-wrapper$EXEEXT" -O2 -Wl,-s $WRAPPER_FLAGS
129+
$CC wrappers/clang-scan-deps-wrapper.c -o "$PREFIX/bin/clang-scan-deps-wrapper$EXEEXT" -O2 -Wl,-s $WRAPPER_FLAGS
129130
$CC wrappers/llvm-wrapper.c -o "$PREFIX/bin/llvm-wrapper$EXEEXT" -O2 -Wl,-s $WRAPPER_FLAGS
130131
if [ -n "$EXEEXT" ]; then
131132
# For Windows, we should prefer the executable wrapper, which also works
@@ -141,6 +142,7 @@ for arch in $ARCHS; do
141142
for exec in clang clang++ gcc g++ c++ as; do
142143
ln -sf clang-target-wrapper$CTW_SUFFIX $arch-w64-$target_os-$exec$CTW_LINK_SUFFIX
143144
done
145+
ln -sf clang-scan-deps-wrapper$CTW_SUFFIX $arch-w64-$target_os-clang-scan-deps$CTW_LINK_SUFFIX
144146
for exec in addr2line ar ranlib nm objcopy readelf size strings strip llvm-ar llvm-ranlib; do
145147
if [ -n "$EXEEXT" ]; then
146148
link_target=llvm-wrapper

wrappers/clang-scan-deps-wrapper.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (c) 2024 Martin Storsjo
3+
*
4+
* This file is part of llvm-mingw.
5+
*
6+
* Permission to use, copy, modify, and/or distribute this software for any
7+
* purpose with or without fee is hereby granted, provided that the above
8+
* copyright notice and this permission notice appear in all copies.
9+
*
10+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17+
*/
18+
19+
#include "native-wrapper.h"
20+
21+
#ifndef DEFAULT_TARGET
22+
#define DEFAULT_TARGET "x86_64-w64-mingw32"
23+
#endif
24+
25+
int _tmain(int argc, TCHAR *argv[]) {
26+
const TCHAR *dir;
27+
split_argv(argv[0], &dir, NULL, NULL, NULL);
28+
29+
int max_arg = argc + 3;
30+
const TCHAR **exec_argv = malloc((max_arg + 1) * sizeof(*exec_argv));
31+
int arg = 0;
32+
exec_argv[arg++] = concat(dir, _T("clang-scan-deps"));
33+
34+
// If changing this wrapper, change clang-scan-deps-wrapper.sh accordingly.
35+
int i = 1;
36+
int got_flags = 0;
37+
TCHAR *cmd_exe = NULL;
38+
for (; i < argc; i++) {
39+
if (got_flags) {
40+
cmd_exe = _tcsdup(argv[i]);
41+
exec_argv[arg++] = argv[i];
42+
i++;
43+
break;
44+
} else if (!_tcscmp(argv[i], _T("--"))) {
45+
got_flags = 1;
46+
exec_argv[arg++] = argv[i];
47+
} else {
48+
exec_argv[arg++] = argv[i];
49+
}
50+
}
51+
52+
if (cmd_exe) {
53+
// If cmd_exe is a <triple>-<exe> style command, prefer the
54+
// target triple from there, rather than from what we might have
55+
// had in our name.
56+
TCHAR *sep = _tcsrchrs(cmd_exe, '/', '\\');
57+
if (sep)
58+
cmd_exe = sep + 1;
59+
sep = _tcsrchr(cmd_exe, '.');
60+
if (sep)
61+
*sep = '\0';
62+
TCHAR *dash = _tcsrchr(cmd_exe, '-');
63+
const TCHAR *target = NULL;
64+
if (dash) {
65+
*dash = '\0';
66+
const TCHAR *cmd_exe_suffix = dash + 1;
67+
if (!_tcscmp(cmd_exe_suffix, _T("clang")) ||
68+
!_tcscmp(cmd_exe_suffix, _T("clang++")) ||
69+
!_tcscmp(cmd_exe_suffix, _T("gcc")) ||
70+
!_tcscmp(cmd_exe_suffix, _T("g++")) ||
71+
!_tcscmp(cmd_exe_suffix, _T("c++")) ||
72+
!_tcscmp(cmd_exe_suffix, _T("as")) ||
73+
!_tcscmp(cmd_exe_suffix, _T("cc")) ||
74+
!_tcscmp(cmd_exe_suffix, _T("c99")) ||
75+
!_tcscmp(cmd_exe_suffix, _T("c11"))) {
76+
target = cmd_exe;
77+
}
78+
}
79+
#ifdef _WIN32
80+
// On Windows, we want to set our default target even if no target
81+
// was found in cmd_exe, as we want to support running with a foreign
82+
// clang-scan-deps-real.exe binary, that could have any default.
83+
if (!target)
84+
target = _T(DEFAULT_TARGET);
85+
#endif
86+
87+
if (target) {
88+
// If we did find a cmd_exe and have figured out a target, add
89+
// -target after cmd_exe.
90+
exec_argv[arg++] = _T("-target");
91+
exec_argv[arg++] = target;
92+
exec_argv[arg++] = _T("-stdlib=libc++");
93+
}
94+
}
95+
96+
for (; i < argc; i++)
97+
exec_argv[arg++] = argv[i];
98+
99+
exec_argv[arg] = NULL;
100+
if (arg > max_arg) {
101+
fprintf(stderr, "Too many options added\n");
102+
abort();
103+
}
104+
105+
return run_final(exec_argv[0], exec_argv);
106+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
#
3+
# Copyright (c) 2024 Martin Storsjo
4+
#
5+
# Permission to use, copy, modify, and/or distribute this software for any
6+
# purpose with or without fee is hereby granted, provided that the above
7+
# copyright notice and this permission notice appear in all copies.
8+
#
9+
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16+
17+
get_dir() {
18+
target="$1"
19+
while [ -L "$target" ]; do
20+
cd "$(dirname "$target")"
21+
target="$(readlink "$(basename "$target")")"
22+
done
23+
cd "$(dirname "$target")"
24+
pwd
25+
}
26+
27+
DIR="$(get_dir "$0")"
28+
29+
# If changing this wrapper, change clang-scan-deps-wrapper.c accordingly.
30+
EXE="$DIR/clang-scan-deps"
31+
FLAGS=()
32+
while [ $# -gt 0 ]; do
33+
if [ -n "$got_flags" ]; then
34+
CMD_EXE="$1"
35+
shift
36+
break
37+
elif [ "$1" = "--" ]; then
38+
got_flags=1
39+
FLAGS+=("$1")
40+
else
41+
FLAGS+=("$1")
42+
fi
43+
shift
44+
done
45+
46+
if [ -n "$CMD_EXE" ]; then
47+
# If CMD_EXE is a <triple>-<exe> style command, pick up the
48+
# target triple from there.
49+
CMD_BASENAME="$(basename "$CMD_EXE")"
50+
CMD_BASENAME="${CMD_BASENAME%.*}"
51+
CMD_TARGET="${CMD_BASENAME%-*}"
52+
CMD_EXE_SUFFIX="${CMD_BASENAME##*-}"
53+
if [ "$CMD_TARGET" != "$CMD_BASENAME" ]; then
54+
case $CMD_EXE_SUFFIX in
55+
clang|clang++|gcc|g++|c++|as|cc|c99|c11)
56+
TARGET="$CMD_TARGET"
57+
;;
58+
esac
59+
fi
60+
fi
61+
62+
if [ -n "$TARGET" ]; then
63+
"$EXE" "${FLAGS[@]}" "$CMD_EXE" -target $TARGET -stdlib=libc++ "$@"
64+
else
65+
"$EXE" "${FLAGS[@]}" "$@"
66+
fi

0 commit comments

Comments
 (0)