Skip to content

Commit

Permalink
mingw-w64-cross-clang: wrappers for cross compilation
Browse files Browse the repository at this point in the history
This allows using clang to target other Windows arches using prefixed
cross tools (ie, aarch64-w64-mingw32-clang).
  • Loading branch information
jeremyd2019 committed May 25, 2024
1 parent c4a1025 commit c1f7ecd
Show file tree
Hide file tree
Showing 7 changed files with 701 additions and 0 deletions.
94 changes: 94 additions & 0 deletions mingw-w64-cross-clang/PKGBUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
declare -g -A _cross_arches=(
["/clang64"]="x86_64-w64-mingw32"
["/clang32"]="i686-w64-mingw32"
["/clangarm64"]="aarch64-w64-mingw32"
)
pkgbase=mingw-w64-cross-clang
pkgname=($(for _pfx in "${!_cross_arches[@]}"; do
if [[ "${_cross_arches[$_pfx]%%-*}" != "${CARCH}" ]]; then
echo "${MINGW_PACKAGE_PREFIX}-cross-clang-${_cross_arches[$_pfx]%%-*}"
fi
done))
pkgver=18.1.6
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('ISC')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
# this is unused in the packages but necessary to make CI happy
depends=("${MINGW_PACKAGE_PREFIX}-clang=${pkgver}"
"${MINGW_PACKAGE_PREFIX}-cross-compiler-rt=${pkgver}")
source=("native-wrapper.h"
"llvm-wrapper.c"
"clang-target-wrapper.c"
"clang-scan-deps-wrapper.c"
"dlltool-wrapper.c"
"windres-wrapper.c")
sha256sums=('58d066367c55a2300fd8205955fc77f726df596c77cb0906c3e05b93a0ae6aef'
'b807f9b7ce3c205afc61f917a19d18e007b994b4bb2e8042f0db65976e96a4ec'
'f7dad39312d3b2cdf42f6d8968f624cc9e3d2af4d5d0dfdae8e807a887fccb18'
'f9c8223a1b9813de4d09f914121ec874568296cf0bf65569e1802b66e784de51'
'2adee7d81d71a167d28fc508e8b3427f63e7c064f08ca63fe330476e8ae4a2c8'
'b121e52bc752396f887e8e6a97138117fc9c2ee4f7efbe9f5ca2be4f451b6f45')
build() {
CC=${CC:-cc}
cd "${srcdir}"

[[ -d "build-${MSYSTEM}" ]] && rm -rf "build-${MSYSTEM}"
mkdir "build-${MSYSTEM}" && cd "build-${MSYSTEM}"

for _pfx in "${!_cross_arches[@]}"; do
if [[ "${_cross_arches[$_pfx]%%-*}" != "${CARCH}" ]]; then
MSYS2_ARG_CONV_EXCL="-DSYSROOT=" \
$CC $CPPFLAGS $CFLAGS $LDFLAGS -municode -DDEFAULT_TARGET="\"${_cross_arches[$_pfx]}\"" -DSYSROOT="\"${_pfx}\"" ../llvm-wrapper.c -o ${_cross_arches[$_pfx]}-llvm-wrapper.exe
MSYS2_ARG_CONV_EXCL="-DSYSROOT=" \
$CC $CPPFLAGS $CFLAGS $LDFLAGS -municode -DDEFAULT_TARGET="\"${_cross_arches[$_pfx]}\"" -DSYSROOT="\"${_pfx}\"" ../clang-target-wrapper.c -o ${_cross_arches[$_pfx]}-clang.exe
MSYS2_ARG_CONV_EXCL="-DSYSROOT=" \
$CC $CPPFLAGS $CFLAGS $LDFLAGS -municode -DDEFAULT_TARGET="\"${_cross_arches[$_pfx]}\"" -DSYSROOT="\"${_pfx}\"" ../clang-scan-deps-wrapper.c -o ${_cross_arches[$_pfx]}-clang-scan-deps.exe
MSYS2_ARG_CONV_EXCL="-DSYSROOT=" \
$CC $CPPFLAGS $CFLAGS $LDFLAGS -municode -DDEFAULT_TARGET="\"${_cross_arches[$_pfx]}\"" -DSYSROOT="\"${_pfx}\"" ../dlltool-wrapper.c -o ${_cross_arches[$_pfx]}-dlltool.exe
MSYS2_ARG_CONV_EXCL="-DSYSROOT=" \
$CC $CPPFLAGS $CFLAGS $LDFLAGS -municode -DDEFAULT_TARGET="\"${_cross_arches[$_pfx]}\"" -DSYSROOT="\"${_pfx}\"" ../windres-wrapper.c -o ${_cross_arches[$_pfx]}-windres.exe
fi
done
}
_real_package() {
local _pfx="@@@PREFIX@@@"
depends=("${MINGW_PACKAGE_PREFIX}-clang=${pkgver}"
"${MINGW_PACKAGE_PREFIX}-cross-compiler-rt=${pkgver}"
"${MINGW_PACKAGE_PREFIX}-llvm=${pkgver}"
"${MINGW_PACKAGE_PREFIX}-lld=${pkgver}"
"mingw-w64-clang-${_cross_arches[@@@PREFIX@@@]%%-*}-crt"
"mingw-w64-clang-${_cross_arches[@@@PREFIX@@@]%%-*}-headers"
"mingw-w64-clang-${_cross_arches[@@@PREFIX@@@]%%-*}-libc++"
"mingw-w64-clang-${_cross_arches[@@@PREFIX@@@]%%-*}-libunwind"
"mingw-w64-clang-${_cross_arches[@@@PREFIX@@@]%%-*}-winpthreads-git")

cd "${srcdir}/build-${MSYSTEM}"

mkdir -p "${pkgdir}${MINGW_PREFIX}/bin"
local _tool
# c11 c99 ?
for _tool in as c++ cc clang clang++ gcc g++; do
cp -f ${_cross_arches[$_pfx]}-clang.exe "${pkgdir}${MINGW_PREFIX}/bin/${_cross_arches[$_pfx]}-${_tool}.exe"
done
for _tool in addr2line ar ranlib nm objcopy readelf strings strip llvm-ar llvm-ranlib; do
cp -f ${_cross_arches[$_pfx]}-llvm-wrapper.exe "${pkgdir}${MINGW_PREFIX}/bin/${_cross_arches[$_pfx]}-${_tool}.exe"
done
# windres and dlltool can't use llvm-wrapper, as that loses the original
# target arch prefix
for _tool in clang-scan-deps dlltool windres; do
cp -f ${_cross_arches[$_pfx]}-${_tool}.exe "${pkgdir}${MINGW_PREFIX}/bin/${_cross_arches[$_pfx]}-${_tool}.exe"
done

mkdir -p "${pkgdir}${MINGW_PREFIX}/share/licenses/${_cross_arches[${_pfx}]}-cross-clang"
sed -ne '1,/^ \*\//p' "${srcdir}/native-wrapper.h" > "${pkgdir}${MINGW_PREFIX}/share/licenses/${_cross_arches[${_pfx}]}-cross-clang/LICENSE"
}

_func="$(declare -f "_real_package")"
for _pfx in "${!_cross_arches[@]}"; do
if [[ "${_cross_arches[$_pfx]%%-*}" != "${CARCH}" ]]; then
_func2="${_func//@@@PREFIX@@@/${_pfx}}"
eval "${_func2/#_real_package/package_${MINGW_PACKAGE_PREFIX}-cross-clang-${_cross_arches[$_pfx]%%-*}}"
fi
done
114 changes: 114 additions & 0 deletions mingw-w64-cross-clang/clang-scan-deps-wrapper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2024 Martin Storsjo
*
* This file is part of llvm-mingw.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include "native-wrapper.h"

#ifndef CLANG_SCAN_DEPS
#define CLANG_SCAN_DEPS "clang-scan-deps"
#endif
#ifndef DEFAULT_TARGET
#define DEFAULT_TARGET "x86_64-w64-mingw32"
#endif
#ifndef SYSROOT
#define SYSROOT "/clang64"
#endif

int _tmain(int argc, TCHAR *argv[]) {
const TCHAR *dir;
split_argv(argv[0], &dir, NULL, NULL, NULL);

int max_arg = argc + 5;
const TCHAR **exec_argv = malloc((max_arg + 1) * sizeof(*exec_argv));
int arg = 0;
exec_argv[arg++] = concat(dir, _T(CLANG_SCAN_DEPS));

// If changing this wrapper, change clang-scan-deps-wrapper.sh accordingly.
int i = 1;
int got_flags = 0;
TCHAR *cmd_exe = NULL;
for (; i < argc; i++) {
if (got_flags) {
cmd_exe = _tcsdup(argv[i]);
exec_argv[arg++] = argv[i];
i++;
break;
} else if (!_tcscmp(argv[i], _T("--"))) {
got_flags = 1;
exec_argv[arg++] = argv[i];
} else {
exec_argv[arg++] = argv[i];
}
}

if (cmd_exe) {
// If cmd_exe is a <triple>-<exe> style command, prefer the
// target triple from there, rather than from what we might have
// had in our name.
TCHAR *sep = _tcsrchrs(cmd_exe, '/', '\\');
if (sep)
cmd_exe = sep + 1;
sep = _tcsrchr(cmd_exe, '.');
if (sep)
*sep = '\0';
TCHAR *dash = _tcsrchr(cmd_exe, '-');
const TCHAR *target = NULL;
if (dash) {
*dash = '\0';
const TCHAR *cmd_exe_suffix = dash + 1;
if (!_tcscmp(cmd_exe_suffix, _T("clang")) ||
!_tcscmp(cmd_exe_suffix, _T("clang++")) ||
!_tcscmp(cmd_exe_suffix, _T("gcc")) ||
!_tcscmp(cmd_exe_suffix, _T("g++")) ||
!_tcscmp(cmd_exe_suffix, _T("c++")) ||
!_tcscmp(cmd_exe_suffix, _T("as")) ||
!_tcscmp(cmd_exe_suffix, _T("cc")) ||
!_tcscmp(cmd_exe_suffix, _T("c99")) ||
!_tcscmp(cmd_exe_suffix, _T("c11"))) {
target = cmd_exe;
}
}
#ifdef _WIN32
// On Windows, we want to set our default target even if no target
// was found in cmd_exe, as we want to support running with a foreign
// clang-scan-deps-real.exe binary, that could have any default.
if (!target)
target = _T(DEFAULT_TARGET);
#endif

if (target) {
// If we did find a cmd_exe and have figured out a target, add
// -target after cmd_exe.
exec_argv[arg++] = _T("-target");
exec_argv[arg++] = target;
exec_argv[arg++] = _T("-stdlib=libc++");
exec_argv[arg++] = _T("--sysroot");
exec_argv[arg++] = concat(dir, _T("../..") _T(SYSROOT));
}
}

for (; i < argc; i++)
exec_argv[arg++] = argv[i];

exec_argv[arg] = NULL;
if (arg > max_arg) {
fprintf(stderr, "Too many options added\n");
abort();
}

return run_final(exec_argv[0], exec_argv);
}
117 changes: 117 additions & 0 deletions mingw-w64-cross-clang/clang-target-wrapper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) 2018 Martin Storsjo
*
* This file is part of llvm-mingw.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include "native-wrapper.h"

#ifndef CLANG
#define CLANG "clang"
#endif
#ifndef DEFAULT_TARGET
#define DEFAULT_TARGET "x86_64-w64-mingw32"
#endif
#ifndef SYSROOT
#define SYSROOT "/clang64"
#endif

int _tmain(int argc, TCHAR* argv[]) {
const TCHAR *dir;
const TCHAR *target;
const TCHAR *exe;
split_argv(argv[0], &dir, NULL, &target, &exe);
if (!target)
target = _T(DEFAULT_TARGET);
TCHAR *arch = _tcsdup(target);
TCHAR *dash = _tcschr(arch, '-');
if (dash)
*dash = '\0';
TCHAR *target_os = _tcsrchr(target, '-');
if (target_os)
target_os++;

// Check if trying to compile Ada; if we try to do this, invoking clang
// would end up invoking <triplet>-gcc with the same arguments, which ends
// up in an infinite recursion.
for (int i = 1; i < argc - 1; i++) {
if (!_tcscmp(argv[i], _T("-x")) && !_tcscmp(argv[i + 1], _T("ada"))) {
fprintf(stderr, "Ada is not supported\n");
return 1;
}
}

int max_arg = argc + 22;
const TCHAR **exec_argv = malloc((max_arg + 1) * sizeof(*exec_argv));
int arg = 0;
if (getenv("CCACHE"))
exec_argv[arg++] = _T("ccache");
exec_argv[arg++] = concat(dir, _T(CLANG));
exec_argv[arg++] = _T("--start-no-unused-arguments");

// If changing this wrapper, change clang-target-wrapper.sh accordingly.
if (!_tcscmp(exe, _T("clang++")) || !_tcscmp(exe, _T("g++")) || !_tcscmp(exe, _T("c++")))
exec_argv[arg++] = _T("--driver-mode=g++");
else if (!_tcscmp(exe, _T("c99")))
exec_argv[arg++] = _T("-std=c99");
else if (!_tcscmp(exe, _T("c11")))
exec_argv[arg++] = _T("-std=c11");

if (target_os && !_tcscmp(target_os, _T("mingw32uwp"))) {
// the UWP target is for Windows 10
exec_argv[arg++] = _T("-D_WIN32_WINNT=0x0A00");
exec_argv[arg++] = _T("-DWINVER=0x0A00");
// the UWP target can only use Windows Store APIs
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");
// force the user of Universal C Runtime
exec_argv[arg++] = _T("-D_UCRT");
}

exec_argv[arg++] = _T("-target");
exec_argv[arg++] = target;
exec_argv[arg++] = _T("--sysroot");
exec_argv[arg++] = concat(dir, _T("../..") _T(SYSROOT));
exec_argv[arg++] = _T("-rtlib=compiler-rt");
exec_argv[arg++] = _T("-unwindlib=libunwind");
exec_argv[arg++] = _T("-stdlib=libc++");
exec_argv[arg++] = _T("-fuse-ld=lld");
exec_argv[arg++] = _T("--end-no-unused-arguments");

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");
abort();
}

return run_final(exec_argv[0], exec_argv);
}
67 changes: 67 additions & 0 deletions mingw-w64-cross-clang/dlltool-wrapper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2018 Martin Storsjo
*
* This file is part of llvm-mingw.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include "native-wrapper.h"

#ifndef DEFAULT_TARGET
#define DEFAULT_TARGET "x86_64-w64-mingw32"
#endif

int _tmain(int argc, TCHAR* argv[]) {
const TCHAR *dir;
const TCHAR *target;
split_argv(argv[0], &dir, NULL, &target, NULL);
if (!target)
target = _tcsdup(_T(DEFAULT_TARGET));
TCHAR *dash = _tcschr(target, '-');
if (dash)
*dash = '\0';

int max_arg = argc + 2;
const TCHAR **exec_argv = malloc((max_arg + 1) * sizeof(*exec_argv));
int arg = 0;
exec_argv[arg++] = concat(dir, _T("llvm-dlltool"));

if (!_tcscmp(target, _T("i686"))) {
exec_argv[arg++] = _T("-m");
exec_argv[arg++] = _T("i386");
} else if (!_tcscmp(target, _T("x86_64"))) {
exec_argv[arg++] = _T("-m");
exec_argv[arg++] = _T("i386:x86-64");
} else if (!_tcscmp(target, _T("armv7"))) {
exec_argv[arg++] = _T("-m");
exec_argv[arg++] = _T("arm");
} else if (!_tcscmp(target, _T("aarch64"))) {
exec_argv[arg++] = _T("-m");
exec_argv[arg++] = _T("arm64");
} else {
_ftprintf(stderr, _T("Arch "TS" unsupported\n"), target);
return 1;
}

for (int i = 1; i < argc; i++)
exec_argv[arg++] = argv[i];

exec_argv[arg] = NULL;
if (arg > max_arg) {
fprintf(stderr, "Too many options added\n");
abort();
}

return run_final(exec_argv[0], exec_argv);
}
Loading

0 comments on commit c1f7ecd

Please sign in to comment.