|
| 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 | +} |
0 commit comments