From 6ad9b04c923cc2d5eab01c918a5559a8672c315b Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Sun, 3 Apr 2022 15:05:01 +0000 Subject: [PATCH] mk: Cross-eyed hacks to support cross-libtool. For a long time, when cross-building, say from native=amd64 to target=powerpc, it was necessary to: 1. cross-build a _target_ powerpc package called cross-libtool-base-powerpc, and then 2. install the powerpc _natively_ with `pkg_add -m x86_64' to override the architecture check that normally forbids this kind of shenanigans, in order to cross-build anything that uses libtool as a tool. This is partly because libtool doesn't follow the normal GNU convention of `./configure --build= --host= --target=' -- in this example, build=amd64, host=amd64, target=powerpc. Instead, libtool expects to be cross-built itself, even if it's going to run as a tool. This is not as bonkers as it sounds: libtool is just a shell script, and it caches various information about the (cross-building!) toolchain it is built with so it can use that information later when it is run as a tool itself to cross-compile other software. To make this work, we need to create the toolchain wrappers for libtool _as if_ we were cross-building even if we are building a native package. So mk/tools uses a new flag TOOLS_USE_CROSS_COMPILE instead of USE_CROSS_COMPILE, and libtool internally sets MACHINE_ARCH=${TARGET_ARCH} (in the example above, powerpc) to make it look like we're cross-building. The new TOOLS_CROSS_DESTDIR is an alias for the (defaulted) CROSS_DESTDIR, which must now be set unconditionally in mk.conf in order for libtool to know where the cross-destdir will be; _CROSS_DESTDIR remains empty when building any native packages (including the native cross-libtool package). Finally, we need to make the resulting package be a native package, with MACHINE_ARCH set to the one that it will be installed on (in the example above, amd64), so I added an indirection _BUILD_DEFS.${var} to replace var on its own in the build definitions that get baked into the package, shown by `pkg_info -B'. Setting _BUILD_DEFS.MACHINE_ARCH=${NATIVE_MACHINE_ARCH} ensures that this mutant hybrid cross-built libtool still produces a native package. All of this logic is gated on setting USE_CROSS_COMPILE in mk.conf or LIBTOOL_CROSS_COMPILE in the package makefile, so it should be safe for non-cross-builds -- when USE_CROSS_COMPILE=no and you're not building cross-libtool, everything is as before. --- cross/cross-libtool-base/Makefile | 4 +++- doc/HOWTO-use-crosscompile | 33 +++++++++---------------------- mk/bsd.prefs.mk | 31 +++++++++++++++++++++++++++++ mk/compiler/gcc.mk | 2 +- mk/configure/gnu-configure.mk | 2 +- mk/cwrappers.mk | 6 +++--- mk/pkgformat/pkg/metadata.mk | 2 +- mk/tools/tools.FreeMiNT.mk | 8 ++++---- mk/tools/tools.Minix.mk | 8 ++++---- mk/tools/tools.NetBSD.mk | 8 ++++---- mk/tools/tools.QNX.mk | 6 +++--- x11/libdrm/Makefile | 2 +- 12 files changed, 65 insertions(+), 47 deletions(-) diff --git a/cross/cross-libtool-base/Makefile b/cross/cross-libtool-base/Makefile index 1d1571ac464d..742b3098f50e 100644 --- a/cross/cross-libtool-base/Makefile +++ b/cross/cross-libtool-base/Makefile @@ -31,12 +31,14 @@ ########################################################################### ########################################################################### +LIBTOOL_CROSS_COMPILE= yes + .include "../../devel/libtool/Makefile.common" # XXX Tweaked for cross-compilation. #PKGNAME= ${DISTNAME:S/-/-base-/} PKGNAME= ${DISTNAME:S/^libtool-/cross-libtool-base-${MACHINE_ARCH}-/} -PKGREVISION= 6 +PKGREVISION= 7 COMMENT= Generic shared library support script (the script itself) diff --git a/doc/HOWTO-use-crosscompile b/doc/HOWTO-use-crosscompile index de1be95d4231..9a472dc2c55c 100644 --- a/doc/HOWTO-use-crosscompile +++ b/doc/HOWTO-use-crosscompile @@ -40,14 +40,8 @@ In addition to whatever else you want in your mk.conf for pkgsrc, add: # which is a bug. USE_CROSS_COMPILE?= yes - .if !empty(USE_CROSS_COMPILE:M[yY][eE][sS]) - # Specify the machine architecture of target packages. - # - # XXX This currently can't be set on the command line, which is a - # bug. - MACHINE_ARCH= powerpc - - # Point pkgsrc at the NetBSD tooldir and destdir. + # Point pkgsrc at the NetBSD tooldir and destdir. These are used + # only for cross-builds. # # XXX There is no obvious variable that is set to amd64 so that we # could use @@ -59,6 +53,13 @@ In addition to whatever else you want in your mk.conf for pkgsrc, add: TOOLDIR= /usr/obj/tooldir.NetBSD-6.1-amd64 CROSS_DESTDIR= /usr/obj/destdir.evbppc + .if !empty(USE_CROSS_COMPILE:M[yY][eE][sS]) + # Specify the machine architecture of target packages. + # + # XXX This currently can't be set on the command line, which is a + # bug. + MACHINE_ARCH= powerpc + # Put target work and packages in separate directories. (You might # use OBJMACHINE=yes or WRKOBJDIR=/tmp/work.${MACHINE_ARCH} instead # for the work directories.) @@ -76,22 +77,6 @@ XXX Some variables, notably LOCALBASE and other paths that get baked into packages, cannot currently be set differently for native and target packages. -* Work around libtool - -Libtool's build system is broken -- it misuses the GNU build system -build/host/target settings. For now we need to kludge around it -manually: - - $ cd /usr/pkgsrc/cross/cross-libtool-base - $ make package - $ pkg_add -m powerpc /usr/pkgsrc/packages.powerpc/All/cross-libtool-base-powerpc-2.4.2.tgz - -(This builds a libtool package for the target, and then installs the -target package natively. This *shouldn't* work, and pkg_add normally -refuses this, but it does work because libtool is a shell script, and -`pkg_add -m powerpc' makes pkg_add pretend we are powerpc to suppress -its refusal.) - * Make some packages Now packages you build normally will be cross-compiled for the target: diff --git a/mk/bsd.prefs.mk b/mk/bsd.prefs.mk index eaf4dff2b631..ce9939cf6606 100644 --- a/mk/bsd.prefs.mk +++ b/mk/bsd.prefs.mk @@ -337,6 +337,32 @@ MACHINE_PLATFORM?= ${OPSYS}-${OS_VERSION}-${MACHINE_ARCH} NATIVE_MACHINE_GNU_PLATFORM?= ${NATIVE_MACHINE_GNU_ARCH}-${LOWER_VENDOR}-${LOWER_OPSYS:C/[0-9]//g}${NATIVE_APPEND_ELF}${LOWER_OPSYS_VERSUFFIX}${NATIVE_APPEND_ABI} MACHINE_GNU_PLATFORM?= ${MACHINE_GNU_ARCH}-${LOWER_VENDOR}-${LOWER_OPSYS:C/[0-9]//g}${APPEND_ELF}${LOWER_OPSYS_VERSUFFIX}${APPEND_ABI} +# +# cross-libtool is special -- it is built as a native package, but it +# needs tools set up as if for a cross-compiled package because it +# remembers the paths. +# +# So normally TOOLS_USE_CROSS_COMPILE is the same as USE_CROSS_COMPILE, +# but for cross-libtool, we set TOOLS_USE_CROSS_COMPILE=yes while doing +# the rest of the native package build with USE_CROSS_COMPILE=no. +# +# This can't live inside the cross-libtool makefile because the +# TARGET_ARCH / MACHINE_ARCH / NATIVE_MACHINE_ARCH switcheroo has to +# happen in the middle of this file -- after NATIVE_MACHINE_ARCH is +# determined, before MACHINE_ARCH is used for anything else. +# +.if !empty(LIBTOOL_CROSS_COMPILE:M[yY][eE][sS]) +TOOLS_USE_CROSS_COMPILE= yes +. if !defined(TARGET_ARCH) +PKG_FAIL_REASON+= "Must set TARGET_ARCH for cross-libtool." +. endif +MACHINE_ARCH:= ${TARGET_ARCH} +_BUILD_DEFS.MACHINE_ARCH= ${NATIVE_MACHINE_ARCH} +_BUILD_DEFS.MACHINE_GNU_ARCH= ${NATIVE_MACHINE_GNU_ARCH} +.else +TOOLS_USE_CROSS_COMPILE= ${USE_CROSS_COMPILE:Uno} +.endif + # Needed to prevent an "install:" target from being created in bsd.own.mk. NEED_OWN_INSTALL_TARGET=no @@ -520,6 +546,11 @@ _CROSS_DESTDIR= ${CROSS_DESTDIR} . endif .endif +# TOOLS_CROSS_DESTDIR is used for the libtool build to make a wrapper +# that points at the cross-destdir as sysroot, without setting +# _CROSS_DESTDIR because we're actually building a native package. +TOOLS_CROSS_DESTDIR= ${CROSS_DESTDIR} + # Depends on MACHINE_ARCH override above .if ${OPSYS} == "NetBSD" # XXX NATIVE_OBJECT_FMT is a cop-out -- but seriously, who is going to diff --git a/mk/compiler/gcc.mk b/mk/compiler/gcc.mk index e3aef2b4430a..6006b7ff07e9 100644 --- a/mk/compiler/gcc.mk +++ b/mk/compiler/gcc.mk @@ -668,7 +668,7 @@ _GCCBINDIR= ${_GCC_PREFIX}bin .elif !empty(_IS_BUILTIN_GCC:M[yY][eE][sS]) _GCCBINDIR= ${_CC:H} .endif -.if !empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if !empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) _GCC_BIN_PREFIX= ${MACHINE_GNU_PLATFORM}- .endif _GCC_BIN_PREFIX?= # empty diff --git a/mk/configure/gnu-configure.mk b/mk/configure/gnu-configure.mk index 6caf076b4f61..835bd8ef6914 100644 --- a/mk/configure/gnu-configure.mk +++ b/mk/configure/gnu-configure.mk @@ -80,7 +80,7 @@ CONFIGURE_ARGS+= --libdir=${GNU_CONFIGURE_LIBDIR} USE_GNU_CONFIGURE_HOST?= yes .if !empty(USE_GNU_CONFIGURE_HOST:M[yY][eE][sS]) -. if !empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +. if !empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) CONFIGURE_ARGS+= --build=${NATIVE_MACHINE_GNU_PLATFORM:Q} . else CONFIGURE_ARGS+= --build=${MACHINE_GNU_PLATFORM:Q} diff --git a/mk/cwrappers.mk b/mk/cwrappers.mk index cb1cf14d4741..acc92c4e3c60 100644 --- a/mk/cwrappers.mk +++ b/mk/cwrappers.mk @@ -37,7 +37,7 @@ CWRAPPERS_ALIASES.ld= ld CWRAPPERS_ALIASES.libtool= libtool CWRAPPERS_ALIASES.shlibtool= shlibtool -.if !empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if !empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) CWRAPPERS_ALIASES.cc+= ${CC:T} CWRAPPERS_ALIASES.cxx+= ${CXX:T} CWRAPPERS_ALIASES.ld+= ${LD:T} @@ -101,8 +101,8 @@ 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}} +. if !empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) + ${RUN}echo sysroot=${TOOLS_CROSS_DESTDIR:Q} >> ${CWRAPPERS_CONFIG_DIR}/${CWRAPPERS_CONFIG.${wrappee}} . endif .endfor diff --git a/mk/pkgformat/pkg/metadata.mk b/mk/pkgformat/pkg/metadata.mk index cca49504aa71..c10ea3f5eb61 100644 --- a/mk/pkgformat/pkg/metadata.mk +++ b/mk/pkgformat/pkg/metadata.mk @@ -36,7 +36,7 @@ DARWIN_REQUIRES_FILTER= ${CAT} ${_BUILD_INFO_FILE}: ${_PLIST_NOKEYWORDS} ${RUN}${MKDIR} ${.TARGET:H} ${RUN}${RM} -f ${.TARGET}.tmp - ${RUN} (${_BUILD_DEFS:NPATH:@v@${ECHO} ${v}=${${v}:Q} ;@}) \ + ${RUN} (${_BUILD_DEFS:NPATH:@v@${ECHO} ${v}=${_BUILD_DEFS.${v}:Q:U${${v}:Q}} ;@}) \ > ${.TARGET}.tmp .if !empty(USE_LANGUAGES) ${RUN}${ECHO} "CC_VERSION=${CC_VERSION}" >> ${.TARGET}.tmp diff --git a/mk/tools/tools.FreeMiNT.mk b/mk/tools/tools.FreeMiNT.mk index 2a93943ae380..df4fa6657bb1 100644 --- a/mk/tools/tools.FreeMiNT.mk +++ b/mk/tools/tools.FreeMiNT.mk @@ -37,7 +37,7 @@ TOOLS_PLATFORM.gzip?= /usr/bin/gzip -nf ${GZIP} TOOLS_PLATFORM.head?= /usr/bin/head TOOLS_PLATFORM.hostname?= /bin/hostname TOOLS_PLATFORM.id?= /usr/bin/id -.if empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) TOOLS_PLATFORM.install?= /usr/bin/install .else TOOLS_PLATFORM.install?= ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-install @@ -58,7 +58,7 @@ TOOLS_PLATFORM.openssl?= /usr/bin/openssl TOOLS_PLATFORM.patch?= /usr/bin/patch TOOLS_PLATFORM.printf?= /usr/bin/printf TOOLS_PLATFORM.pwd?= /bin/pwd -.if empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) TOOLS_PLATFORM.readelf?= /usr/bin/readelf .else TOOLS_PLATFORM.readelf?= ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-readelf @@ -71,7 +71,7 @@ TOOLS_PLATFORM.sh?= /bin/sh TOOLS_PLATFORM.sleep?= /bin/sleep TOOLS_PLATFORM.soelim?= /usr/bin/soelim TOOLS_PLATFORM.sort?= /bin/sort -.if empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) TOOLS_PLATFORM.strip?= /usr/bin/strip .else TOOLS_PLATFORM.strip?= ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-strip @@ -92,7 +92,7 @@ TOOLS_PLATFORM.xargs?= /usr/bin/xargs -r TOOLS_PLATFORM.xgettext?= /usr/bin/xgettext TOOLS_PLATFORM.yacc?= /usr/bin/yacc -.if !empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if !empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) . for _t_ in ar as ld nm objdump ranlib readelf strip TOOLS_PATH.${MACHINE_GNU_PLATFORM}-${_t_}?= \ ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-${_t_} diff --git a/mk/tools/tools.Minix.mk b/mk/tools/tools.Minix.mk index 5bb0c774682f..e323425b1912 100644 --- a/mk/tools/tools.Minix.mk +++ b/mk/tools/tools.Minix.mk @@ -43,7 +43,7 @@ TOOLS_PLATFORM.gzip?= /usr/bin/gzip -nf ${GZIP} TOOLS_PLATFORM.head?= /usr/bin/head TOOLS_PLATFORM.hostname?= /bin/hostname TOOLS_PLATFORM.id?= /usr/bin/id -.if empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) TOOLS_PLATFORM.install?= /usr/bin/install .else TOOLS_PLATFORM.install?= ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-install @@ -67,7 +67,7 @@ TOOLS_PLATFORM.patch?= /usr/bin/patch TOOLS_PLATFORM.pax?= /bin/pax TOOLS_PLATFORM.printf?= /usr/bin/printf TOOLS_PLATFORM.pwd?= /bin/pwd -.if empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) TOOLS_PLATFORM.readelf?= /usr/bin/readelf .else TOOLS_PLATFORM.readelf?= ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-readelf @@ -82,7 +82,7 @@ TOOLS_PLATFORM.shlock?= /usr/bin/shlock TOOLS_PLATFORM.sleep?= /bin/sleep TOOLS_PLATFORM.soelim?= /usr/bin/soelim TOOLS_PLATFORM.sort?= /usr/bin/sort -.if empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) TOOLS_PLATFORM.strip?= /usr/bin/strip .else TOOLS_PLATFORM.strip?= ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-strip @@ -103,7 +103,7 @@ TOOLS_PLATFORM.xz?= /usr/bin/xz TOOLS_PLATFORM.xzcat?= /usr/bin/xzcat TOOLS_PLATFORM.yacc?= /usr/bin/yacc -.if !empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if !empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) . for _t_ in ar as ld nm objcopy objdump ranlib readelf strip TOOLS_PATH.${MACHINE_GNU_PLATFORM}-${_t_}?= \ ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-${_t_} diff --git a/mk/tools/tools.NetBSD.mk b/mk/tools/tools.NetBSD.mk index e3afbfba69c7..0c49f008ab19 100644 --- a/mk/tools/tools.NetBSD.mk +++ b/mk/tools/tools.NetBSD.mk @@ -58,7 +58,7 @@ TOOLS_PLATFORM.head?= /usr/bin/head TOOLS_PLATFORM.hostname?= /bin/hostname TOOLS_PLATFORM.id?= /usr/bin/id TOOLS_PLATFORM.ident?= /usr/bin/ident -.if empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) TOOLS_PLATFORM.install?= /usr/bin/install .else TOOLS_PLATFORM.install?= ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-install @@ -101,7 +101,7 @@ TOOLS_PLATFORM.paxctl?= /usr/sbin/paxctl .endif TOOLS_PLATFORM.printf?= /usr/bin/printf TOOLS_PLATFORM.pwd?= /bin/pwd -.if empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) TOOLS_PLATFORM.readelf?= /usr/bin/readelf .else TOOLS_PLATFORM.readelf?= ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-readelf @@ -119,7 +119,7 @@ TOOLS_PLATFORM.shlock?= /usr/bin/shlock TOOLS_PLATFORM.sleep?= /bin/sleep TOOLS_PLATFORM.soelim?= /usr/bin/soelim TOOLS_PLATFORM.sort?= /usr/bin/sort -.if empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) TOOLS_PLATFORM.strip?= /usr/bin/strip .else TOOLS_PLATFORM.strip?= ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-strip @@ -155,7 +155,7 @@ TOOLS_PLATFORM.xzcat?= /usr/bin/xzcat .endif TOOLS_PLATFORM.yacc?= /usr/bin/yacc -.if !empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if !empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) . for _t_ in ar as ld nm objcopy objdump ranlib readelf strip TOOLS_PATH.${MACHINE_GNU_PLATFORM}-${_t_}?= \ ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-${_t_} diff --git a/mk/tools/tools.QNX.mk b/mk/tools/tools.QNX.mk index 1a2d288e3ac2..83d3e4ac6272 100644 --- a/mk/tools/tools.QNX.mk +++ b/mk/tools/tools.QNX.mk @@ -55,7 +55,7 @@ TOOLS_PLATFORM.patch?= /usr/bin/patch TOOLS_PLATFORM.pax?= /bin/pax TOOLS_PLATFORM.printf?= /usr/bin/printf TOOLS_PLATFORM.pwd?= /bin/pwd -.if empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) TOOLS_PLATFORM.readelf?= ${QNX_HOST}/usr/bin/readelf .endif TOOLS_PLATFORM.rm?= /bin/rm @@ -65,7 +65,7 @@ TOOLS_PLATFORM.sh?= /bin/sh TOOLS_PLATFORM.sleep?= /usr/bin/sleep TOOLS_PLATFORM.soelim?= ${QNX_HOST}/usr/bin/soelim TOOLS_PLATFORM.sort?= /usr/bin/sort -.if empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) TOOLS_PLATFORM.strip?= ${QNX_HOST}/usr/bin/strip .endif TOOLS_PLATFORM.tail?= /usr/bin/tail @@ -85,7 +85,7 @@ TOOLS_PLATFORM.yacc?= ${QNX_HOST}/usr/bin/bison -y -.if !empty(USE_CROSS_COMPILE:M[yY][eE][sS]) +.if !empty(TOOLS_USE_CROSS_COMPILE:M[yY][eE][sS]) . for _t_ in ar as ld nm objcopy objdump ranlib readelf strip TOOLS_PATH.${MACHINE_GNU_PLATFORM}-${_t_}?= \ ${QNX_HOST}/usr/bin/nto${MACHINE_GNU_PLATFORM}-${_t_} diff --git a/x11/libdrm/Makefile b/x11/libdrm/Makefile index c792eab6aad1..e9c534b231a5 100644 --- a/x11/libdrm/Makefile +++ b/x11/libdrm/Makefile @@ -38,7 +38,7 @@ SUBST_STAGE.atomic= pre-configure SUBST_VARS.atomic= ATOMIC_OPS_CHECK ATOMIC_OPS_CHECK= 0 -.if exists(${CROSS_DESTDIR}/usr/include/sys/atomic.h) +.if exists(${_CROSS_DESTDIR}/usr/include/sys/atomic.h) ATOMIC_OPS_CHECK= 1 .else . include "../../devel/libatomic_ops/buildlink3.mk"