Unverified Commit 6cdee17d authored by John Ericson's avatar John Ericson Committed by GitHub
Browse files

Fix static binaries for FreeBSD (#353567)

parents a9089570 2cb5d446
Loading
Loading
Loading
Loading
+81 −0
Original line number Diff line number Diff line
From 197b10de54b53a089ad549f2e00787b4fa719210 Mon Sep 17 00:00:00 2001
From: Artemis Tosini <me@artem.ist>
Date: Sat, 2 Nov 2024 07:50:13 +0000
Subject: [PATCH] HACK: bsd.lib.mk: Treat empty SHLIB_NAME as nonexistant

Unsetting SHLIB_NAME in nix package definitions is a pain
but we can easily set it to be empty. This is useful when
building static libraries without unneeded static libraries.
---
 share/mk/bsd.lib.mk | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/share/mk/bsd.lib.mk b/share/mk/bsd.lib.mk
index 5f328d5378ca..89d16dc6fa41 100644
--- a/share/mk/bsd.lib.mk
+++ b/share/mk/bsd.lib.mk
@@ -242,7 +242,7 @@ PO_FLAG=-pg
 _LIBDIR:=${LIBDIR}
 _SHLIBDIR:=${SHLIBDIR}
 
-.if defined(SHLIB_NAME)
+.if defined(SHLIB_NAME) && !empty(SHLIB_NAME)
 .if ${MK_DEBUG_FILES} != "no"
 SHLIB_NAME_FULL=${SHLIB_NAME}.full
 # Use ${DEBUGDIR} for base system debug files, else .debug subdirectory
@@ -277,7 +277,7 @@ LDFLAGS+=	-Wl,--undefined-version
 .endif
 .endif
 
-.if defined(LIB) && !empty(LIB) || defined(SHLIB_NAME)
+.if defined(LIB) && !empty(LIB) || (defined(SHLIB_NAME) && !empty(SHLIB_NAME))
 OBJS+=		${SRCS:N*.h:${OBJS_SRCS_FILTER:ts:}:S/$/.o/}
 BCOBJS+=	${SRCS:N*.[hsS]:N*.asm:${OBJS_SRCS_FILTER:ts:}:S/$/.bco/g}
 LLOBJS+=	${SRCS:N*.[hsS]:N*.asm:${OBJS_SRCS_FILTER:ts:}:S/$/.llo/g}
@@ -320,14 +320,14 @@ lib${LIB_PRIVATE}${LIB}.ll: ${LLOBJS}
 CLEANFILES+=	lib${LIB_PRIVATE}${LIB}.bc lib${LIB_PRIVATE}${LIB}.ll
 .endif
 
-.if defined(SHLIB_NAME) || \
+.if (defined(SHLIB_NAME) && !empty(SHLIB_NAME)) || \
     defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
 SOBJS+=		${OBJS:.o=.pico}
 DEPENDOBJS+=	${SOBJS}
 CLEANFILES+=	${SOBJS}
 .endif
 
-.if defined(SHLIB_NAME)
+.if defined(SHLIB_NAME) && !empty(SHLIB_NAME)
 _LIBS+=		${SHLIB_NAME}
 
 SOLINKOPTS+=	-shared -Wl,-x
@@ -435,7 +435,7 @@ all: all-man
 CLEANFILES+=	${_LIBS}
 
 _EXTRADEPEND:
-.if !defined(NO_EXTRADEPEND) && defined(SHLIB_NAME)
+.if !defined(NO_EXTRADEPEND) && defined(SHLIB_NAME) && !empty(SHLIB_NAME)
 .if defined(DPADD) && !empty(DPADD)
 	echo ${SHLIB_NAME_FULL}: ${DPADD} >> ${DEPENDFILE}
 .endif
@@ -501,7 +501,7 @@ _libinstall:
 	    ${_INSTALLFLAGS} lib${LIB_PRIVATE}${LIB}_p.a ${DESTDIR}${_LIBDIR}/
 .endif
 .endif
-.if defined(SHLIB_NAME)
+.if defined(SHLIB_NAME) && !empty(SHLIB_NAME)
 	${INSTALL} ${TAG_ARGS} ${STRIP} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
 	    ${_INSTALLFLAGS} ${_SHLINSTALLFLAGS} \
 	    ${SHLIB_NAME} ${DESTDIR}${_SHLIBDIR}/
@@ -588,7 +588,7 @@ OBJS_DEPEND_GUESS+= ${SRCS:M*.h}
 OBJS_DEPEND_GUESS.${_S:${OBJS_SRCS_FILTER:ts:}}.po+=	${_S}
 .endfor
 .endif
-.if defined(SHLIB_NAME) || \
+.if (defined(SHLIB_NAME) && !empty(SHLIB_NAME)) || \
     defined(INSTALL_PIC_ARCHIVE) && defined(LIB) && !empty(LIB)
 .for _S in ${SRCS:N*.[hly]}
 OBJS_DEPEND_GUESS.${_S:${OBJS_SRCS_FILTER:ts:}}.pico+=	${_S}
-- 
2.46.1
+3 −1
Original line number Diff line number Diff line
{ mkDerivation }:
{ mkDerivation, stdenv }:
mkDerivation {
  path = "sbin/init";
  extraPaths = [ "sbin/mount" ];
  NO_FSCHG = "yes";
  MK_TESTS = "no";

  meta.broken = !stdenv.hostPlatform.isStatic;
}
+15 −1
Original line number Diff line number Diff line
@@ -116,6 +116,13 @@ mkDerivation {
        --replace '/usr/share/i18n' '${builtins.placeholder "out"}/share/i18n'
  '';

  # NIX_CFLAGS_LINK is empty at this point except when building static,
  # in which case the stdenv adapter adds the `-static` flag.
  # Building with `-static` set causes linker errors.
  postConfigure = ''
    export NIX_CFLAGS_LINK=
  '';

  nativeBuildInputs = [
    bsdSetupHook
    freebsdSetupHook
@@ -194,7 +201,7 @@ mkDerivation {
      make -C $BSDSRCDIR/lib/libgcc_eh $makeFlags
      make -C $BSDSRCDIR/lib/libgcc_eh $makeFlags install

      ln -s $BSDSRCDIR/lib/libc/libc.so.7 $BSDSRCDIR/lib/libc/libc.so  # not sure
      ln -s $BSDSRCDIR/lib/libc/libc.so.7 $BSDSRCDIR/lib/libc/libc.so  # otherwise these dynamic libraries try to link with libc.a
      mkdir $BSDSRCDIR/lib/libgcc_s/i386 $BSDSRCDIR/lib/libgcc_s/cpu_model
      make -C $BSDSRCDIR/lib/libgcc_s $makeFlags
      make -C $BSDSRCDIR/lib/libgcc_s $makeFlags install
@@ -266,6 +273,13 @@ mkDerivation {
      make -C $BSDSRCDIR/libexec/rtld-elf $makeFlags install
      rm -f $out/libexec/ld-elf.so.1
      mv $out/bin/ld-elf.so.1 $out/libexec
    ''
    + lib.optionalString (!stdenv.hostPlatform.isStatic) ''
      mkdir $out/lib/keep_static
      mv $out/lib/*_nonshared.a $out/lib/libgcc*.a $out/lib/libcompiler_rt.a $out/lib/keep_static
      rm $out/lib/*.a
      mv $out/lib/keep_static/* $out/lib
      rmdir $out/lib/keep_static
    '';

  # libc should not be allowed to refer to anything other than itself
+13 −6
Original line number Diff line number Diff line
{ mkDerivation }:
{
  mkDerivation,
  stdenv,
  lib,
}:
# this package is quite different from stock libcxxrt.
# as of FreeBSD 14.0, it is vendored from APPROXIMATELY libcxxrt
# 5d8a15823a103bbc27f1bfdcf2b5aa008fab57dd, though the vendoring mechanism is
@@ -8,9 +12,12 @@ mkDerivation {
  pname = "libcxxrt";
  path = "lib/libcxxrt";
  extraPaths = [ "contrib/libcxxrt" ];
  outputs = [
  outputs =
    [
      "out"
      "dev"
    ]
    ++ lib.optionals (!stdenv.hostPlatform.isStatic) [
      "debug"
    ];
  noLibcxx = true;
+6 −3
Original line number Diff line number Diff line
@@ -58,9 +58,12 @@ lib.makeOverridable (

      HOST_SH = stdenv'.shell;

      makeFlags = [
      makeFlags =
        [
          "STRIP=-s" # flag to install, not command
      ] ++ lib.optional (!stdenv'.hostPlatform.isFreeBSD) "MK_WERROR=no";
        ]
        ++ lib.optional (!stdenv'.hostPlatform.isFreeBSD) "MK_WERROR=no"
        ++ lib.optional stdenv.hostPlatform.isStatic "SHLIB_NAME=";

      # amd64 not x86_64 for this on unlike NetBSD
      MACHINE_ARCH = freebsd-lib.mkBsdArch stdenv';
Loading