git: 37b66193453c - main - Uses/cabal.mk: Add support for LTO feature.

From: Gleb Popov <arrowd_at_FreeBSD.org>
Date: Mon, 27 Feb 2023 18:18:42 UTC
The branch main has been updated by arrowd:

URL: https://cgit.FreeBSD.org/ports/commit/?id=37b66193453c2ed2c103385aebf7e9f034a58ac1

commit 37b66193453c2ed2c103385aebf7e9f034a58ac1
Author:     Gleb Popov <arrowd@FreeBSD.org>
AuthorDate: 2023-02-27 10:54:00 +0000
Commit:     Gleb Popov <arrowd@FreeBSD.org>
CommitDate: 2023-02-27 18:11:10 +0000

    Uses/cabal.mk: Add support for LTO feature.
    
    Haskell code benefits greatly from splitting functions into distinct ELF
    sections and then letting the linker to garbage-collect them.
    
    devel/hs-haskell-language-server port goes from 277MiB to 176MiB in size.
    net-p2p/cardano-node ports goes 302MiB -> 76.2MiB
    
    Relevant blog post: http://brandon.si/code/linking-smaller-haskell-binaries/
    
    Reported by:    Greg Steuck <greg@nest.cx>
---
 Mk/Features/lto.mk | 4 ++++
 Mk/Uses/cabal.mk   | 6 +++---
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/Mk/Features/lto.mk b/Mk/Features/lto.mk
index d17484e05f53..aadd27b5d61f 100644
--- a/Mk/Features/lto.mk
+++ b/Mk/Features/lto.mk
@@ -16,6 +16,10 @@ LTO_Include_MAINTAINER=	pkubaj@FreeBSD.org
 		CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1
 .    elif defined(_INCLUDE_USES_MESON_MK)
    MESON_ARGS+=	-Db_lto=true
+.    elif defined(_INCLUDE_USES_CABAL_MK)
+   CABAL_LTO_ARGS=	--ghc-options=-split-sections \
+			--gcc-options="-fdata-sections -ffunction-sections" \
+			--ld-options=-Wl,--gc-sections,--build-id,--icf=all
 .    else
 # Overridable as a user may want to use -flto
    LTO_FLAGS?=	-flto=thin
diff --git a/Mk/Uses/cabal.mk b/Mk/Uses/cabal.mk
index 27237bde3974..23d0b0fa6ebb 100644
--- a/Mk/Uses/cabal.mk
+++ b/Mk/Uses/cabal.mk
@@ -215,12 +215,12 @@ cabal-extract: check-cabal
 # This pulls in all source dependencies, resolves them and generates build plan
 cabal-configure: check-cabal
 	cd ${WRKSRC} && \
-		${SETENV} ${MAKE_ENV} HOME=${CABAL_HOME} ${CABAL_CMD} build --dry-run --disable-benchmarks --disable-tests --flags="${CABAL_FLAGS}" ${CABAL_WITH_ARGS} ${BUILD_ARGS} ${BUILD_TARGET}
+		${SETENV} ${MAKE_ENV} HOME=${CABAL_HOME} ${CABAL_CMD} build --dry-run --disable-benchmarks --disable-tests --flags="${CABAL_FLAGS}" ${CABAL_WITH_ARGS} ${CABAL_LTO_ARGS} ${BUILD_ARGS} ${BUILD_TARGET}
 
 # Calls cabal build on the Haskell package located in ${WRKSRC}
 cabal-build: check-cabal
 	cd ${WRKSRC} && \
-		${SETENV} ${MAKE_ENV} HOME=${CABAL_HOME} ${CABAL_CMD} build --disable-benchmarks --disable-tests ${CABAL_WITH_ARGS} ${BUILD_ARGS} ${BUILD_TARGET}
+		${SETENV} ${MAKE_ENV} HOME=${CABAL_HOME} ${CABAL_CMD} build --disable-benchmarks --disable-tests ${CABAL_WITH_ARGS} ${CABAL_LTO_ARGS} ${BUILD_ARGS} ${BUILD_TARGET}
 
 # Generates USE_CABAL= ... line ready to be pasted into the port based on the plan.json file generated by cabal configure.
 make-use-cabal: check-cabal2tuple
@@ -306,7 +306,7 @@ cabal-pre-configure:
 .  if !target(do-build)
 do-build:
 	cd ${WRKSRC} && \
-		${SETENV} ${MAKE_ENV} HOME=${CABAL_HOME} ${CABAL_CMD} build --offline --disable-benchmarks --disable-tests ${CABAL_WITH_ARGS} --flags "${CABAL_FLAGS}" ${BUILD_ARGS} ${BUILD_TARGET}
+		${SETENV} ${MAKE_ENV} HOME=${CABAL_HOME} ${CABAL_CMD} build --offline --disable-benchmarks --disable-tests ${CABAL_WITH_ARGS} ${CABAL_LTO_ARGS} --flags "${CABAL_FLAGS}" ${BUILD_ARGS} ${BUILD_TARGET}
 .  endif
 
 .  if !target(do-install)