2020-09-03 16:35:18 +00:00
|
|
|
/* Copyright 2020 Zhuowei Zhang, Oliver Smith
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
*
|
|
|
|
* This program gets built to a set of wrapper executables, which launch native
|
|
|
|
* cross compilers inside foreign arch chroots. Speeds up cross compilation a
|
|
|
|
* lot, compared to using just qemu user mode emulation or using the previous
|
|
|
|
* distcc-based methods.
|
|
|
|
*
|
|
|
|
* How this program gets called:
|
|
|
|
* - pmbootstrap creates one native arch chroot and one foreign arch chroot.
|
|
|
|
* - The native chroot gets mounted as /native in the foreign chroot.
|
|
|
|
* - When calling "abuild", pmbootstrap sets PATH to:
|
|
|
|
* /native/usr/lib/crossdirect/<ARCH>:$PATH
|
|
|
|
* - That crossdirect directory contains a crossdirect-<ARCH> binary built with
|
|
|
|
* the matching HOSTSPEC (e.g. crossdirect-aarch64 binary with HOSTSPEC
|
|
|
|
* aarch64-alpine-linux-musl). This binary is symlinked to:
|
|
|
|
* - <HOSTSPEC>-c++
|
|
|
|
* - <HOSTSPEC>-cc
|
|
|
|
* - <HOSTSPEC>-clang
|
|
|
|
* - <HOSTSPEC>-clang++
|
|
|
|
* - <HOSTSPEC>-cpp
|
|
|
|
* - <HOSTSPEC>-g++
|
|
|
|
* - <HOSTSPEC>-gcc
|
|
|
|
* - c++
|
|
|
|
* - cc
|
|
|
|
* - clang
|
|
|
|
* - clang++
|
|
|
|
* - cpp
|
|
|
|
* - g++
|
|
|
|
* - gcc
|
|
|
|
*/
|
2019-02-16 05:20:14 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
2019-06-25 06:29:37 +00:00
|
|
|
#include <libgen.h>
|
2019-02-16 05:20:14 +00:00
|
|
|
#include <limits.h>
|
2019-06-25 06:29:37 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-02-16 05:20:14 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-04-03 18:45:16 +00:00
|
|
|
#define NATIVE_BIN_DIR "/native/usr/lib/ccache/bin"
|
|
|
|
|
2019-06-25 06:29:37 +00:00
|
|
|
void exit_userfriendly()
|
|
|
|
{
|
2019-02-16 05:20:14 +00:00
|
|
|
fprintf(stderr, "Please report this at: https://gitlab.com/postmarketOS/pmaports/issues\n");
|
|
|
|
fprintf(stderr, "As a workaround, you can compile without crossdirect.\n");
|
|
|
|
fprintf(stderr, "See 'pmbootstrap -h' for related options.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-06-25 06:29:37 +00:00
|
|
|
bool argv_has_arg(int argc, char **argv, const char *arg)
|
|
|
|
{
|
2019-04-14 01:40:49 +00:00
|
|
|
size_t arg_len = strlen(arg);
|
|
|
|
|
2019-06-25 06:29:37 +00:00
|
|
|
for (int i = 1; i < argc; i++) {
|
2019-04-14 01:40:49 +00:00
|
|
|
if (strlen(argv[i]) < arg_len)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!memcmp(argv[i], arg, arg_len))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-25 06:29:37 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2019-02-16 05:20:14 +00:00
|
|
|
// we have a max of four extra args ("-target", "HOSTSPEC", "--sysroot=/", "-Wl,-rpath-link=/lib:/usr/lib"), plus one ending null
|
2019-06-25 06:29:37 +00:00
|
|
|
char *newargv[argc + 5];
|
|
|
|
char *executableName = basename(argv[0]);
|
2019-02-16 05:20:14 +00:00
|
|
|
char newExecutable[PATH_MAX];
|
|
|
|
bool isClang = (strcmp(executableName, "clang") == 0 || strcmp(executableName, "clang++") == 0);
|
2019-06-25 06:29:37 +00:00
|
|
|
bool startsWithHostSpec = (strncmp(HOSTSPEC, executableName, sizeof(HOSTSPEC) - 1) == 0);
|
2019-02-16 05:20:14 +00:00
|
|
|
|
2019-04-14 01:40:49 +00:00
|
|
|
// linker is involved: just use qemu binary (to avoid broken cross-ld, pmaports#227)
|
|
|
|
if (!argv_has_arg(argc, argv, "-c")) {
|
|
|
|
snprintf(newExecutable, sizeof(newExecutable), "/usr/bin/%s", executableName);
|
|
|
|
if (execv(newExecutable, argv) == -1) {
|
|
|
|
fprintf(stderr, "ERROR: crossdirect: failed to execute %s: %s\n", newExecutable, strerror(errno));
|
|
|
|
fprintf(stderr, "NOTE: this is a foreign arch binary that would run with qemu (linker is involved).\n");
|
|
|
|
exit_userfriendly();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 16:35:18 +00:00
|
|
|
// prepend the HOSTSPEC to GCC binaries
|
2019-02-16 05:20:14 +00:00
|
|
|
if (isClang || startsWithHostSpec) {
|
2019-06-25 06:29:37 +00:00
|
|
|
snprintf(newExecutable, sizeof(newExecutable), NATIVE_BIN_DIR "/%s", executableName);
|
2019-02-16 05:20:14 +00:00
|
|
|
} else {
|
2019-06-25 06:29:37 +00:00
|
|
|
snprintf(newExecutable, sizeof(newExecutable), NATIVE_BIN_DIR "/" HOSTSPEC "-%s", executableName);
|
2019-02-16 05:20:14 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 16:35:18 +00:00
|
|
|
// prepare new arguments for GCC / clang
|
2019-06-25 06:29:37 +00:00
|
|
|
char **newArgsPtr = newargv;
|
2019-02-16 05:20:14 +00:00
|
|
|
*newArgsPtr++ = newExecutable;
|
|
|
|
if (isClang) {
|
2020-09-03 16:35:18 +00:00
|
|
|
// clang does not use a HOSTSPEC prefix for the cross compiler
|
|
|
|
// binary, but instead have a -target argument
|
2019-02-16 05:20:14 +00:00
|
|
|
*newArgsPtr++ = "-target";
|
|
|
|
*newArgsPtr++ = HOSTSPEC;
|
|
|
|
}
|
|
|
|
*newArgsPtr++ = "--sysroot=/";
|
2019-04-14 01:40:49 +00:00
|
|
|
|
2020-09-03 16:35:18 +00:00
|
|
|
// add all arguments passed to this executable to GCC / clang and set
|
|
|
|
// the last arg in newargv to NULL to mark its end
|
2019-06-25 06:29:37 +00:00
|
|
|
memcpy(newArgsPtr, argv + 1, sizeof(char *) * (argc - 1));
|
2019-02-16 05:20:14 +00:00
|
|
|
newArgsPtr += (argc - 1);
|
|
|
|
*newArgsPtr = NULL;
|
|
|
|
|
|
|
|
// new arguments prepared; now setup environmental vars
|
2019-06-25 06:29:37 +00:00
|
|
|
char *env[] = { "LD_PRELOAD=",
|
2019-04-03 18:45:16 +00:00
|
|
|
"LD_LIBRARY_PATH=/native/lib:/native/usr/lib",
|
|
|
|
"CCACHE_PATH=/native/usr/bin",
|
2019-06-25 06:29:37 +00:00
|
|
|
NULL };
|
|
|
|
char *ldPreload = getenv("LD_PRELOAD");
|
2019-02-16 05:20:14 +00:00
|
|
|
if (ldPreload) {
|
|
|
|
if (strcmp(ldPreload, "/usr/lib/libfakeroot.so") == 0) {
|
2019-04-03 18:45:16 +00:00
|
|
|
env[0] = "LD_PRELOAD=/native/usr/lib/libfakeroot.so";
|
2019-02-16 05:20:14 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "ERROR: crossdirect: can't handle LD_PRELOAD: %s\n", ldPreload);
|
|
|
|
exit_userfriendly();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 16:35:18 +00:00
|
|
|
// finally exec GCC / clang
|
2019-04-03 18:45:16 +00:00
|
|
|
if (execve(newExecutable, newargv, env) == -1) {
|
2019-02-16 05:20:14 +00:00
|
|
|
fprintf(stderr, "ERROR: crossdirect: failed to execute %s: %s\n", newExecutable, strerror(errno));
|
2019-04-03 18:45:16 +00:00
|
|
|
fprintf(stderr, "Maybe the target arch is missing in the ccache-cross-symlinks package?\n");
|
2019-02-16 05:20:14 +00:00
|
|
|
exit_userfriendly();
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|