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 Apr 5, 2024
1 parent b42877c commit 29128a2
Show file tree
Hide file tree
Showing 6 changed files with 583 additions and 0 deletions.
90 changes: 90 additions & 0 deletions mingw-w64-cross-clang/PKGBUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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.3
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"
"dlltool-wrapper.c"
"windres-wrapper.c")
sha256sums=('30f22023059f76113de774aa3b708df6a5b604f51d2ef1452a82bcff1b9c3fca'
'b807f9b7ce3c205afc61f917a19d18e007b994b4bb2e8042f0db65976e96a4ec'
'9eb8bc50327e3194688179fd217d6f00e46c0f167e962db7549bef9d3fde2808'
'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}\"" ../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 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
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);
}
31 changes: 31 additions & 0 deletions mingw-w64-cross-clang/llvm-wrapper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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"

int _tmain(int argc, TCHAR* argv[]) {
const TCHAR *dir;
const TCHAR *exe;
const TCHAR *basename, *target;
split_argv(argv[0], &dir, &basename, &target, &exe);
if (_tcsncmp(exe, _T("llvm-"), 5))
exe = concat(_T("llvm-"), exe);
TCHAR *exe_path = concat(dir, exe);

return run_final(exe_path, (const TCHAR *const *) argv);
}
Loading

0 comments on commit 29128a2

Please sign in to comment.