git: 2d27a0b0097c - main - Uses/zig.mk: Introduce Zig language support to the framework

From: Gleb Popov <arrowd_at_FreeBSD.org>
Date: Fri, 29 Aug 2025 17:31:13 UTC
The branch main has been updated by arrowd:

URL: https://cgit.FreeBSD.org/ports/commit/?id=2d27a0b0097ca2ddd1e24cb5c656126d64b092a4

commit 2d27a0b0097ca2ddd1e24cb5c656126d64b092a4
Author:     Gleb Popov <arrowd@FreeBSD.org>
AuthorDate: 2025-07-04 18:19:19 +0000
Commit:     Gleb Popov <arrowd@FreeBSD.org>
CommitDate: 2025-08-29 17:31:07 +0000

    Uses/zig.mk: Introduce Zig language support to the framework
    
    Approved by:    mat (portmgr)
    Differential Revision:  https://reviews.freebsd.org/D51165
---
 Mk/Uses/zig.mk    | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 Mk/bsd.options.mk |   7 ++++
 2 files changed, 109 insertions(+)

diff --git a/Mk/Uses/zig.mk b/Mk/Uses/zig.mk
new file mode 100644
index 000000000000..5727a812fede
--- /dev/null
+++ b/Mk/Uses/zig.mk
@@ -0,0 +1,102 @@
+# Provide support for building Zig software.
+#
+# Feature:	zig
+# Usage:	USES=zig
+
+# Variables, which can be set by the port:
+#
+#  ZIG_TUPLE		List of Zig dependencies required to build the port.
+#			Each item is triplet of an arbitrary name, an URL without
+#			scheme part (https://) and a directory name where the
+#			expects this dependency to be placed.
+#			This knob can be generated by running make make-zig-tuple
+#			You might need to run this multiple time until all
+#			transitive dependencies get pulled in.
+#
+# MAINTAINER: ports@FreeBSD.org
+
+.if !defined(_INCLUDE_USES_ZIG_MK)
+_INCLUDE_USES_ZIG_MK=    yes
+
+.  if !empty(zig_ARGS)
+IGNORE=		USES=zig does not accept arguments
+.    endif
+
+.sinclude "${MASTERDIR}/Makefile.zig"
+
+ZIG_CMD?=	zig
+ZIG_PORT?=	lang/zig
+ZIG_DEPSDIR=	${WRKDIR}/zig-packages
+ZIG_TMPDEPSDIR=	${WRKDIR}/zig-packages-tmp
+
+ZIG_CPUTYPE_DEFAULT=	${ARCH:S/amd64/x86_64/}
+ZIG_CPUTYPE?=		${CPUTYPE:U${ZIG_CPUTYPE_DEFAULT}}
+
+_ZIG2TUPLE_CMD=	zig2tuple
+
+BUILD_DEPENDS+=	${ZIG_CMD}:${ZIG_PORT}
+
+DIST_SUBDIR?=	zig
+
+.  for z in ${ZIG_TUPLE}
+.    for group url dir in ${z:S/:/ /g:tw}
+MASTER_SITES+=	https://${url:H}/:${group}
+DISTFILES+=	${url:T}:${group}
+.    endfor
+.  endfor
+
+_USES_extract=	299:zig-pre-extract
+
+# Generates ZIG_TUPLE= ... line ready to be pasted into the port based on
+# build.zig.zon files found in ${WRKSRC}
+make-zig-tuple: check-zig2tuple
+	@${ECHO_MSG} "===> Processing build.zig.zon files"
+	${_ZIG2TUPLE_CMD} ${WRKDIR}
+
+check-zig2tuple:
+	@if ! type ${_ZIG2TUPLE_CMD} > /dev/null 2>&1; then \
+		${ECHO_MSG} "===> zig2tuple executable not found, install \"ports-mgmt/zig2tuple\""; exit 1; \
+	fi
+
+# Main targets implementation.
+
+zig-pre-extract:
+	@${ECHO_CMD} "===> Extracting zig dependencies"
+# We don't know the name of the directory that is contained in the archive,
+# but we need to rename it into the last component of the tuple
+# To solve this we extract into an empty directory and then do
+# mv * /path/where/we/want/it
+	@${MKDIR} ${ZIG_DEPSDIR} ${ZIG_TMPDEPSDIR}
+.  for z in ${ZIG_TUPLE}
+.    for group url dir in ${z:S/:/ /g:tw}
+	${MAKE} -C ${.CURDIR} do-extract EXTRACT_ONLY=${url:T} WRKDIR=${ZIG_TMPDEPSDIR}
+	${MV} ${ZIG_TMPDEPSDIR}/* ${ZIG_DEPSDIR}/${dir}
+.    endfor
+.  endfor
+	@${RMDIR} ${ZIG_TMPDEPSDIR}
+
+ZIG_ENV+=	DESTDIR=${STAGEDIR}
+ZIG_ARGS+=	--prefix ${PREFIX} --system ${ZIG_DEPSDIR} --verbose \
+		-Dcpu=${ZIG_CPUTYPE} \
+		${"${WITH_DEBUG}" != "":?:--release=fast} \
+		${"${WITH_DEBUG}" != "":?-Doptimize=Debug:-Doptimize=ReleaseSmall} \
+		${ZIG_ARGS_${FLAVOR}}
+DO_MAKE_BUILD?=	${SETENVI} ${WRK_ENV} ${ZIG_ENV} ${ZIG_CMD} build \
+				${_MAKE_JOBS} ${ZIG_ARGS}
+.  if !target(do-build)
+do-build:
+	@${DO_NADA}
+.  endif
+
+.  if !target(do-install)
+do-install:
+	@(cd ${BUILD_WRKSRC}; if ! ${DO_MAKE_BUILD}; then \
+		if [ -n "${BUILD_FAIL_MESSAGE}" ] ; then \
+			${ECHO_MSG} "===> Compilation failed unexpectedly."; \
+			(${ECHO_CMD} "${BUILD_FAIL_MESSAGE}") | ${FMT_80} ; \
+			fi; \
+		${FALSE}; \
+		fi)
+.  endif
+
+.endif
diff --git a/Mk/bsd.options.mk b/Mk/bsd.options.mk
index 3b4b2dd4d300..38f1492f0dbd 100644
--- a/Mk/bsd.options.mk
+++ b/Mk/bsd.options.mk
@@ -198,6 +198,7 @@ _ALL_OPTIONS_HELPERS=	${_OPTIONS_DEPENDS:S/$/_DEPENDS/} \
 			CONFIGURE_WITH IMPLIES MESON_ARGS MESON_DISABLED \
 			MESON_ENABLED MESON_FALSE MESON_OFF MESON_ON MESON_TRUE \
 			PREVENTS PREVENTS_MSG QMAKE_OFF QMAKE_ON \
+			ZIG_BOOL ZIG_BOOL_OFF \
 			SUBPACKAGES SUBPACKAGES_OFF USE USE_OFF VARS VARS_OFF
 
 # The format here is target_family:priority:target-type
@@ -544,6 +545,9 @@ MESON_ARGS+=		${${opt}_MESON_DISABLED:C/.*/-D&=disabled/}
 .      if defined(${opt}_CABAL_FLAGS)
 CABAL_FLAGS+=	${${opt}_CABAL_FLAGS}
 .      endif
+.      if defined(${opt}_ZIG_BOOL)
+ZIG_ARGS+=	${${opt}_ZIG_BOOL:C/.*/-D&=true/}
+.      endif
 .      for configure in CONFIGURE CMAKE MESON QMAKE
 .        if defined(${opt}_${configure}_ON)
 ${configure}_ARGS+=	${${opt}_${configure}_ON}
@@ -620,6 +624,9 @@ MESON_ARGS+=		${${opt}_MESON_DISABLED:C/.*/-D&=enabled/}
 .      if defined(${opt}_CABAL_FLAGS)
 CABAL_FLAGS+=	-${${opt}_CABAL_FLAGS}
 .      endif
+.      if defined(${opt}_ZIG_BOOL)
+ZIG_ARGS+=	${${opt}_ZIG_BOOL:C/.*/-D&=false/}
+.      endif
 .      for configure in CONFIGURE CMAKE MESON QMAKE
 .        if defined(${opt}_${configure}_OFF)
 ${configure}_ARGS+=	${${opt}_${configure}_OFF}