From 5de14c61b0449d9d58e03f054f24553def229fa9 Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Sun, 3 Apr 2022 09:37:54 +0000 Subject: [PATCH] cwrappers: Add cross-compilation support. - New option `sysroot=': . Wrapper will add `--sysroot=' as first argument. . For every rpath argument, e.g. -Wl,-R to cc, wrapper will pass `-rpath-link ' to the linker. This matches the old mk/wrapper/cmd-sink-cross-* logic. - Create wrappers for the ${MACHINE_GNU_ARCH}-cc style of command, as in ${CC}, ${CXX}, ${LD}. - Bump version. - Use TOOL_DEPENDS, not BUILD_DEPENDS, for cwrappers. --- mk/cwrappers.mk | 11 ++++++- pkgtools/cwrappers/Makefile | 2 +- pkgtools/cwrappers/files/bin/base-wrapper.c | 13 ++++++++ pkgtools/cwrappers/files/bin/common.c | 6 ++++ pkgtools/cwrappers/files/bin/common.h | 1 + pkgtools/cwrappers/files/bin/normalise-cc.c | 36 +++++++++++++++++---- pkgtools/cwrappers/files/bin/normalise-ld.c | 31 ++++++++++++++++++ 7 files changed, 91 insertions(+), 9 deletions(-) diff --git a/mk/cwrappers.mk b/mk/cwrappers.mk index dab8af0aeac9..cb1cf14d4741 100644 --- a/mk/cwrappers.mk +++ b/mk/cwrappers.mk @@ -5,7 +5,7 @@ .include "../../mk/wrapper/wrapper-defs.mk" .include "../../mk/buildlink3/bsd.buildlink3.mk" -BUILD_DEPENDS+= cwrappers>=20150314:../../pkgtools/cwrappers +TOOL_DEPENDS+= cwrappers>=20220403:../../pkgtools/cwrappers # XXX This should be PREFIX, but USE_CROSSBASE overrides it. CWRAPPERS_SRC_DIR= ${LOCALBASE}/libexec/cwrappers @@ -37,6 +37,12 @@ CWRAPPERS_ALIASES.ld= ld CWRAPPERS_ALIASES.libtool= libtool CWRAPPERS_ALIASES.shlibtool= shlibtool +.if !empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +CWRAPPERS_ALIASES.cc+= ${CC:T} +CWRAPPERS_ALIASES.cxx+= ${CXX:T} +CWRAPPERS_ALIASES.ld+= ${LD:T} +.endif + CWRAPPERS_WRAPPEE.as= ${AS:Ufalse} CWRAPPERS_WRAPPEE.cxx= ${PKG_CXX:Ufalse} CWRAPPERS_WRAPPEE.cc= ${PKG_CC:Ufalse} @@ -95,6 +101,9 @@ generate-cwrappers: . endfor . endif . endif +. if !empty(USE_CROSS_COMPILE:M[yY][eE][sS]) + ${RUN}echo sysroot=${_CROSS_DESTDIR:Q} >> ${CWRAPPERS_CONFIG_DIR}/${CWRAPPERS_CONFIG.${wrappee}} +. endif .endfor PREPEND_PATH+= ${WRAPPER_BINDIR} diff --git a/pkgtools/cwrappers/Makefile b/pkgtools/cwrappers/Makefile index 3eda51ab105e..8b3dcca347f2 100644 --- a/pkgtools/cwrappers/Makefile +++ b/pkgtools/cwrappers/Makefile @@ -1,6 +1,6 @@ # $NetBSD: Makefile,v 1.26 2020/06/02 19:46:00 rillig Exp $ -PKGNAME= cwrappers-20180325 +PKGNAME= cwrappers-20220403 CATEGORIES= pkgtools sysutils MAINTAINER= joerg@NetBSD.org diff --git a/pkgtools/cwrappers/files/bin/base-wrapper.c b/pkgtools/cwrappers/files/bin/base-wrapper.c index 8ec08379a45b..66ddd4d22f0d 100644 --- a/pkgtools/cwrappers/files/bin/base-wrapper.c +++ b/pkgtools/cwrappers/files/bin/base-wrapper.c @@ -99,6 +99,18 @@ libtool_mode(struct arglist *args) } #endif +static void +apply_sysroot(struct arglist *args) +{ + struct argument *arg; + + if (sysroot == NULL) + return; + + arg = argument_new(concat("--sysroot=", sysroot)); + TAILQ_INSERT_HEAD(args, arg, link); +} + int main(int argc, char **argv) { @@ -136,6 +148,7 @@ main(int argc, char **argv) operation_mode_cc(&args); #endif arglist_apply_config(&args); + apply_sysroot(&args); #if defined(WRAPPER_LD) normalise_ld(&args); #else diff --git a/pkgtools/cwrappers/files/bin/common.c b/pkgtools/cwrappers/files/bin/common.c index 3d2b8266e4cc..4d0fa121a147 100644 --- a/pkgtools/cwrappers/files/bin/common.c +++ b/pkgtools/cwrappers/files/bin/common.c @@ -48,6 +48,7 @@ static char *real_path; char *exec_path; char *exec_name; char *wrksrc; +char *sysroot; int debug; enum operation_mode current_operation_mode = mode_unknown; @@ -294,6 +295,11 @@ parse_config(const char *wrapper) wrksrc = xstrdup(line + 7); continue; } + if (strncmp(line, "sysroot=", 8) == 0) { + free(sysroot); + sysroot = xstrdup(line + 8); + continue; + } if (strncmp(line, "unwrap=", 7) == 0) { register_unwrap(line + 7); continue; diff --git a/pkgtools/cwrappers/files/bin/common.h b/pkgtools/cwrappers/files/bin/common.h index 5348f0234808..c36770db5582 100644 --- a/pkgtools/cwrappers/files/bin/common.h +++ b/pkgtools/cwrappers/files/bin/common.h @@ -42,6 +42,7 @@ extern const char library_name_chars[]; extern char *exec_path; extern char *exec_name; extern char *wrksrc; +extern char *sysroot; extern int debug; enum operation_mode { diff --git a/pkgtools/cwrappers/files/bin/normalise-cc.c b/pkgtools/cwrappers/files/bin/normalise-cc.c index 7c884cbba993..64ded916725c 100644 --- a/pkgtools/cwrappers/files/bin/normalise-cc.c +++ b/pkgtools/cwrappers/files/bin/normalise-cc.c @@ -38,7 +38,8 @@ static void normalise_path_list(struct arglist *args, struct argument *arg, - const char *prefix, const char *val, int strip_relative) + const char *prefix, const char *sysroot_prefix, const char *val, + int strip_relative) { const char *sep; struct argument *arg2; @@ -51,6 +52,14 @@ normalise_path_list(struct arglist *args, struct argument *arg, arg2 = argument_new(concat2(prefix, val, sep - val)); TAILQ_INSERT_AFTER(args, arg, arg2, link); arg = arg2; + if (sysroot_prefix && sysroot) { + char *sysroot_val = concat2(sysroot, val, sep - val); + arg2 = argument_new(concat(sysroot_prefix, + sysroot_val)); + free(sysroot_val); + TAILQ_INSERT_AFTER(args, arg, arg2, link); + arg = arg2; + } val = sep + 1; } if (val[0] == '\0' || (strip_relative && !isabs(val[0]))) @@ -58,6 +67,13 @@ normalise_path_list(struct arglist *args, struct argument *arg, arg2 = argument_new(concat(prefix, val)); TAILQ_INSERT_AFTER(args, arg, arg2, link); arg = arg2; + if (sysroot_prefix && sysroot) { + char *sysroot_val = concat(sysroot, val); + arg2 = argument_new(concat(sysroot_prefix, sysroot_val)); + free(sysroot_val); + TAILQ_INSERT_AFTER(args, arg, arg2, link); + arg = arg2; + } } void @@ -201,7 +217,8 @@ normalise_cc(struct arglist *args) if (strcmp(arg->val, "-Wl,-rpath-link") == 0) { if (arg2 == NULL || strncmp(arg2->val, "-Wl,", 4)) errx(255, "Missing argument for %s", arg->val); - normalise_path_list(args, arg, "-Wl,-rpath-link,", + normalise_path_list(args, arg, + "-Wl,-rpath-link,", NULL, arg2->val + 4, 0); argument_unlink(args, &arg); argument_unlink(args, &arg2); @@ -210,7 +227,8 @@ normalise_cc(struct arglist *args) if (strcmp(arg->val, "-R") == 0) { if (arg2 == NULL || arg2->val[0] == '-') errx(255, "Missing argument for %s", arg->val); - normalise_path_list(args, arg, "-Wl,-rpath,", + normalise_path_list(args, arg, + "-Wl,-rpath,", "-Wl,-rpath-link"/*sysroot*/, arg2->val, 1); argument_unlink(args, &arg); argument_unlink(args, &arg2); @@ -221,26 +239,30 @@ normalise_cc(struct arglist *args) strcmp(arg->val, "-Wl,--rpath") == 0) { if (arg2 == NULL || strncmp(arg2->val, "-Wl,", 4)) errx(255, "Missing argument for %s", arg->val); - normalise_path_list(args, arg, "-Wl,-rpath,", + normalise_path_list(args, arg, + "-Wl,-rpath,", "-Wl,-rpath-link"/*sysroot*/, arg2->val + 4, 1); argument_unlink(args, &arg); argument_unlink(args, &arg2); continue; } if (strncmp(arg->val, "-Wl,-R", 6) == 0) { - normalise_path_list(args, arg, "-Wl,-rpath,", + normalise_path_list(args, arg, + "-Wl,-rpath,", "-Wl,-rpath-link"/*sysroot*/, arg->val + 6, 1); argument_unlink(args, &arg); continue; } if (strncmp(arg->val, "-R", 2) == 0) { - normalise_path_list(args, arg, "-Wl,-rpath,", + normalise_path_list(args, arg, + "-Wl,-rpath,", "-Wl,-rpath-link"/*sysroot*/, arg->val + 2, 1); argument_unlink(args, &arg); continue; } if (strncmp(arg->val, "-Wl,-rpath,", 10) == 0) { - normalise_path_list(args, arg, "-Wl,-rpath,", + normalise_path_list(args, arg, + "-Wl,-rpath,", "-Wl,-rpath-link"/*sysroot*/, arg->val + 10, 1); argument_unlink(args, &arg); continue; diff --git a/pkgtools/cwrappers/files/bin/normalise-ld.c b/pkgtools/cwrappers/files/bin/normalise-ld.c index 53421d2669d2..476efa548fa3 100644 --- a/pkgtools/cwrappers/files/bin/normalise-ld.c +++ b/pkgtools/cwrappers/files/bin/normalise-ld.c @@ -62,6 +62,29 @@ operation_mode_ld(struct arglist *args) } } +/* + * append_rpath_link(args, path, &lastarg) + * + * If sysroot is enabled, append `-rpath-link ${sysroot}${path}' + * to the argument list after lastarg, and update lastarg to be + * the last argument appended. + */ +static void +append_rpath_link(struct arglist *args, const char *path, + struct argument **lastargp) +{ + struct argument *arg_opt, *arg_val; + + if (sysroot == NULL) + return; + + arg_opt = argument_new(xstrdup("-rpath-link")); + arg_val = argument_new(concat(sysroot, path)); + TAILQ_INSERT_AFTER(args, *lastargp, arg_opt, link); + TAILQ_INSERT_AFTER(args, arg_opt, arg_val, link); + *lastargp = arg_val; +} + void normalise_ld(struct arglist *args) { @@ -83,12 +106,20 @@ normalise_ld(struct arglist *args) if (arg2 == NULL || arg2->val[0] == '-') errx(255, "Missing argument for %s", arg->val); argument_update(arg, xstrdup("-rpath")); + append_rpath_link(args, arg2->val, &arg2); + continue; + } + if (sysroot && strcmp(arg->val, "-rpath") == 0) { + if (arg2 == NULL || arg2->val[0] == '-') + errx(255, "Missing argument for %s", arg->val); + append_rpath_link(args, arg2->val, &arg2); continue; } if (strncmp(arg->val, "-R", 2) == 0) { argument_update(arg, xstrdup(arg->val + 2)); arg3 = argument_copy("-rpath"); TAILQ_INSERT_BEFORE(arg, arg3, link); + append_rpath_link(args, arg->val, &arg2); continue; } }