PORTNAME=	emacs
DISTVERSION=	30.1
PORTREVISION=	5
PORTEPOCH=	3
CATEGORIES=	editors
MASTER_SITES=	GNU

MAINTAINER=	emacs@FreeBSD.org
COMMENT=	GNU editing macros ${COMMENT_${FLAVOR}}
WWW=		https://www.gnu.org/software/emacs/
COMMENT_nox=	(No X flavor)

LICENSE=	GPLv3+
LICENSE_FILE=	${WRKSRC}/COPYING

LIB_DEPENDS=	libgmp.so:math/gmp

FLAVORS=			nox
nox_PKGNAMESUFFIX=		-nox

USES=		cpe gmake localbase:ldflags ncurses pkgconfig tar:xz
CPE_VENDOR=	gnu

GNU_CONFIGURE=		yes
CONFIGURE_ARGS=		--disable-build-details \
			--localstatedir=/var \
			--without-gconf \
			--without-libsystemd \
			--without-selinux
CONFIGURE_ARGS+=	--with-sound=no \
			--with-x-toolkit=no \
			--without-cairo \
			--without-dbus \
			--without-gconf \
			--without-gif \
			--without-gsettings \
			--without-imagemagick \
			--without-jpeg \
			--without-lcms2 \
			--without-libotf \
			--without-m17n-flt \
			--without-pgtk \
			--without-png \
			--without-rsvg \
			--without-tiff \
			--without-toolkit-scroll-bars \
			--without-webp \
			--without-x \
			--without-xim \
			--without-xpm \
			--without-xwidgets
# See r468320 to determine if/when the next line can be removed
CONFIGURE_ENV=		ac_cv_header_sys_sysinfo_h=no

BINARY_ALIAS=	makeinfo=true
SUB_FILES=	pkg-message sources.el
SUB_LIST=	EMACS_VER=${DISTVERSION} \
		NATIVECOMP_COMMENT_END=${NATIVECOMP_COMMENT_END} \
		NATIVECOMP_COMMENT_START=${NATIVECOMP_COMMENT_START}

INFO=		auth autotype bovine calc ccmode cl dbus dired-x ebrowse ede \
		ediff edt efaq eglot eieio eintr elisp emacs emacs-gnutls \
		emacs-mime epa erc ert eshell eudc eww flymake forms gnus \
		htmlfontify idlwave ido info mairix-el message mh-e \
		modus-themes newsticker nxml-mode octave-mode org pcl-cvs pgg \
		rcirc reftex remember sasl sc semantic ses sieve smtpmail \
		speedbar srecode todo-mode tramp transient url use-package \
		vhdl-mode vip viper vtable widget wisent woman
INFO_PATH=	${DATADIR_REL}/info
PLIST_SUB=	EMACS_VER=${DISTVERSION} \
		GNU_HOST=${CONFIGURE_TARGET}

OPTIONS_DEFINE=			ACL GNUTLS MAILUTILS MODULES NATIVECOMP \
				SOURCES SQLITE3 THREADS TREESITTER XML
OPTIONS_DEFAULT=		ACL GNUTLS KQUEUE MAILUTILS MODULES \
				SOURCES SQLITE3 THREADS TREESITTER XML
OPTIONS_RADIO=			FILENOTIFY
OPTIONS_RADIO_FILENOTIFY=	KQUEUE
OPTIONS_SUB=			YES

ACL_DESC=		Access control list support
FILENOTIFY_DESC=	File notification support
KQUEUE_DESC=		File notification via kqueue
MAILUTILS_DESC=		Mail framework via GNU Mailutils
MODULES_DESC=		Dynamic modules support
NATIVECOMP_DESC=	Emacs Lisp native compiler support
SOURCES_DESC=		Install sources
TREESITTER_DESC=	tree-sitter support

ACL_CONFIGURE_ENABLE=		acl
GNUTLS_LIB_DEPENDS=		libgnutls.so:security/gnutls
GNUTLS_CONFIGURE_WITH=		gnutls
KQUEUE_CONFIGURE_ON=		--with-file-notification=kqueue
#LTO_CONFIGURE_ON=		--enable-link-time-optimization
MAILUTILS_RUN_DEPENDS=		mailutils:mail/mailutils
MAILUTILS_CONFIGURE_WITH=	mailutils
MAILUTILS_CONFIGURE_ENV_OFF=	ac_cv_lib_lockfile_maillock=no \
				ac_cv_prog_liblockfile=no
MODULES_CONFIGURE_WITH=		modules
NATIVECOMP_USE=			GCC=yes
NATIVECOMP_CONFIGURE_WITH=	native-compilation=aot
SQLITE3_USES=			sqlite:3
SQLITE3_CONFIGURE_WITH=		sqlite3
THREADS_CONFIGURE_WITH=		threads
TREESITTER_IMPLIES=		MODULES
TREESITTER_LIB_DEPENDS=		libtree-sitter.so:devel/tree-sitter
TREESITTER_CONFIGURE_WITH=	tree-sitter
XML_USES=			gnome
XML_USE=			GNOME=libxml2
XML_CONFIGURE_WITH=		xml2

.include <bsd.port.options.mk>

.if ${PORT_OPTIONS:MNATIVECOMP}
NATIVECOMP_COMMENT_END=
NATIVECOMP_COMMENT_START=
.else
NATIVECOMP_COMMENT_END=		'*/'
NATIVECOMP_COMMENT_START=	'/*'
.endif

CONFIGURE_ARGS+=	--with-x-toolkit=no

post-install:
	@${RM} -r ${STAGEDIR}${PREFIX}/lib/systemd
	@${FIND} ${STAGEDIR}${PREFIX}/ -type f -name '*.desktop' -exec ${RM} {} +
	@(cd ${STAGEDIR}${PREFIX} && ${FIND} libexec/emacs/${DISTVERSION}/${CONFIGURE_TARGET}/ -name emacs-*.pdmp >> ${TMPPLIST})

post-install-MAILUTILS-on:
	${LN} -sf ${PREFIX}/bin/movemail \
		${STAGEDIR}${PREFIX}/libexec/emacs/${DISTVERSION}/${CONFIGURE_TARGET}

# Natively compiled elisp files names include build checksum information, so add
# these files to the plist after the build.
post-install-NATIVECOMP-on:
	@(cd ${WRKSRC} && ${FIND} native-lisp -type f | ${SED} -e 's,^,lib/emacs/${DISTVERSION}/,') >> ${TMPPLIST}
	@${FIND} ${STAGEDIR}${PREFIX}/lib/emacs/${DISTVERSION}/native-lisp/ -name '*.eln*' -exec ${STRIP_CMD} {} +

post-install-SOURCES-on:
	@${MKDIR} ${STAGEDIR}${DATADIR}/${DISTVERSION}/src
	${INSTALL_DATA} ${WRKSRC}/src/*.[ch] ${STAGEDIR}${DATADIR}/${DISTVERSION}/src
	${INSTALL_DATA} ${WRKDIR}/sources.el ${STAGEDIR}${DATADIR}/${DISTVERSION}/site-lisp/site-start.el

.include <bsd.port.mk>
