From 56fec2622f77f3fa5f875c2ffacee37c86c015a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Thu, 29 Sep 2022 14:51:31 +0300 Subject: [PATCH] wrappers: For UWP, pass the default -lwindowsapp after user specified options This matches how the compiler driver normally includes default linked libraries; the user provided -l flags are passed first, followed by the compiler's default ones. That allows a user specified library to override the default ones. This makes sure that if a user provides -lwindowsappcompat, it gets linked before -lwindowsapp, which makes a difference for symbols that are provided in both (such as e.g. LoadLibraryW). --- wrappers/clang-target-wrapper.c | 20 ++++++++++++++------ wrappers/clang-target-wrapper.sh | 14 +++++++++++--- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/wrappers/clang-target-wrapper.c b/wrappers/clang-target-wrapper.c index 48143a8..6ea609e 100644 --- a/wrappers/clang-target-wrapper.c +++ b/wrappers/clang-target-wrapper.c @@ -50,7 +50,7 @@ int _tmain(int argc, TCHAR* argv[]) { } } - int max_arg = argc + 20; + int max_arg = argc + 22; const TCHAR **exec_argv = malloc((max_arg + 1) * sizeof(*exec_argv)); int arg = 0; if (getenv("CCACHE")) @@ -84,11 +84,6 @@ int _tmain(int argc, TCHAR* argv[]) { exec_argv[arg++] = _T("-DWINAPI_FAMILY=WINAPI_FAMILY_APP"); // the Windows Store API only supports Windows Unicode (some rare ANSI ones are available) exec_argv[arg++] = _T("-DUNICODE"); - // add the minimum runtime to use for UWP targets - exec_argv[arg++] = _T("-Wl,-lwindowsapp"); - // This still requires that the toolchain (in particular, libc++.a) has - // been built targeting UCRT originally. - exec_argv[arg++] = _T("-Wl,-lucrtapp"); // force the user of Universal C Runtime exec_argv[arg++] = _T("-D_UCRT"); } @@ -104,6 +99,19 @@ int _tmain(int argc, TCHAR* argv[]) { for (int i = 1; i < argc; i++) exec_argv[arg++] = argv[i]; + if (target_os && !_tcscmp(target_os, _T("mingw32uwp"))) { + // Default linker flags; passed after any user specified -l options, + // to let the user specified libraries take precedence over these. + + exec_argv[arg++] = _T("--start-no-unused-arguments"); + // add the minimum runtime to use for UWP targets + exec_argv[arg++] = _T("-Wl,-lwindowsapp"); + // This still requires that the toolchain (in particular, libc++.a) has + // been built targeting UCRT originally. + exec_argv[arg++] = _T("-Wl,-lucrtapp"); + exec_argv[arg++] = _T("--end-no-unused-arguments"); + } + exec_argv[arg] = NULL; if (arg >= max_arg) { fprintf(stderr, "Too many options added\n"); diff --git a/wrappers/clang-target-wrapper.sh b/wrappers/clang-target-wrapper.sh index 682c987..f980e46 100755 --- a/wrappers/clang-target-wrapper.sh +++ b/wrappers/clang-target-wrapper.sh @@ -71,6 +71,7 @@ aarch64) # SEH is the default for aarch64. ;; esac +LINKER_FLAGS="" case $TARGET_OS in mingw32uwp) # the UWP target is for Windows 10 @@ -79,11 +80,18 @@ mingw32uwp) FLAGS="$FLAGS -DWINAPI_FAMILY=WINAPI_FAMILY_APP" # the Windows Store API only supports Windows Unicode (some rare ANSI ones are available) FLAGS="$FLAGS -DUNICODE" + + # Default linker flags; passed after any user specified -l options, + # to let the user specified libraries take precedence over these. + # add the minimum runtime to use for UWP targets - FLAGS="$FLAGS -Wl,-lwindowsapp" + LINKER_FLAGS="$LINKER_FLAGS --start-no-unused-arguments" + LINKER_FLAGS="$LINKER_FLAGS -Wl,-lwindowsapp" # This still requires that the toolchain (in particular, libc++.a) has # been built targeting UCRT originally. - FLAGS="$FLAGS -Wl,-lucrtapp" + LINKER_FLAGS="$LINKER_FLAGS -Wl,-lucrtapp" + LINKER_FLAGS="$LINKER_FLAGS --end-no-unused-arguments" + # Force the Universal C Runtime FLAGS="$FLAGS -D_UCRT" ;; @@ -96,4 +104,4 @@ FLAGS="$FLAGS -stdlib=libc++" FLAGS="$FLAGS -fuse-ld=lld" FLAGS="$FLAGS --end-no-unused-arguments" -$CCACHE "$CLANG" $FLAGS "$@" +$CCACHE "$CLANG" $FLAGS "$@" $LINKER_FLAGS