summaryrefslogtreecommitdiff
blob: 50df33f0b0740416e69a07c7aa2c412c268d6986 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
# Copyright 1999-2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# @ECLASS: apache-2.eclass
# @MAINTAINER:
# apache-bugs@gentoo.org
# @AUTHOR:
# polynomial-c@gentoo.org
# @SUPPORTED_EAPIS: 7
# @BLURB: Provides a common set of functions for apache-2.x ebuilds
# @DESCRIPTION:
# This eclass handles apache-2.x ebuild functions such as LoadModule generation
# and inter-module dependency checking.

LUA_COMPAT=( lua5-{1..4} )
inherit autotools flag-o-matic lua-single multilib ssl-cert toolchain-funcs

[[ ${CATEGORY}/${PN} != www-servers/apache ]] \
	&& die "Do not use this eclass with anything else than www-servers/apache ebuilds!"

case ${EAPI} in
	7) ;;
	*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac

# settings which are version specific go in here:
case $(ver_cut 1-2) in
	2.4)
		DEFAULT_MPM_THREADED="event" #509922
	;;
	*)
		die "Unknown MAJOR.MINOR apache version."
	;;
esac

# ==============================================================================
# INTERNAL VARIABLES
# ==============================================================================

# @ECLASS_VARIABLE: GENTOO_USE_PCRE1
# @DESCRIPTION:
# Temporary variable to allow older Apache versions to force using legacy
# dev-libs/libpcre.  This will be removed once ebuilds using dev-libs/libpcre2
# ar stable.

# @ECLASS_VARIABLE: GENTOO_PATCHNAME
# @DESCRIPTION:
# This internal variable contains the prefix for the patch tarball.
# Defaults to the full name and version (including revision) of the package.
# If you want to override this in an ebuild, use:
# ORIG_PR="(revision of Gentoo stuff you want)"
# GENTOO_PATCHNAME="gentoo-${PN}-${PV}${ORIG_PR:+-${ORIG_PR}}"
[[ -n "${GENTOO_PATCHNAME}" ]] || GENTOO_PATCHNAME="gentoo-${PF}"

# @ECLASS_VARIABLE: GENTOO_PATCHDIR
# @DESCRIPTION:
# This internal variable contains the working directory where patches and config
# files are located.
# Defaults to the patchset name appended to the working directory.
[[ -n "${GENTOO_PATCHDIR}" ]] || GENTOO_PATCHDIR="${WORKDIR}/${GENTOO_PATCHNAME}"

# @VARIABLE: GENTOO_DEVELOPER
# @DESCRIPTION:
# This variable needs to be set in the ebuild and contains the name of the
# gentoo developer who created the patch tarball

# @VARIABLE: GENTOO_PATCHSTAMP
# @DESCRIPTION:
# This variable needs to be set in the ebuild and contains the date the patch
# tarball was created at in YYYYMMDD format

# @VARIABLE: GENTOO_PATCH_A
# @DESCRIPTION:
# This variable should contain the entire filename of patch tarball.
# Defaults to the name of the patchset, with a datestamp.
[[ -n "${GENTOO_PATCH_A}" ]] || GENTOO_PATCH_A="${GENTOO_PATCHNAME}-${GENTOO_PATCHSTAMP}.tar.bz2"

SRC_URI="mirror://apache/httpd/httpd-${PV}.tar.bz2
	https://dev.gentoo.org/~${GENTOO_DEVELOPER}/dist/apache/${GENTOO_PATCH_A}"

# @VARIABLE: IUSE_MPMS_FORK
# @DESCRIPTION:
# This variable needs to be set in the ebuild and contains a list of forking
# (i.e.  non-threaded) MPMs

# @VARIABLE: IUSE_MPMS_THREAD
# @DESCRIPTION:
# This variable needs to be set in the ebuild and contains a list of threaded
# MPMs

# @VARIABLE: IUSE_MODULES
# @DESCRIPTION:
# This variable needs to be set in the ebuild and contains a list of available
# built-in modules

IUSE_MPMS="${IUSE_MPMS_FORK} ${IUSE_MPMS_THREAD}"
IUSE="${IUSE} debug doc gdbm ldap selinux ssl static suexec +suexec-caps suexec-syslog split-usr threads"

for module in ${IUSE_MODULES} ; do
	case ${module} in
		# Enable http2 by default (bug #563452)
		http2)
			IUSE+=" +apache2_modules_${module}"
		;;
		systemd)
			IUSE+=" systemd"
		;;
		*)
			IUSE+=" apache2_modules_${module}"
		;;
	esac
done

_apache2_set_mpms() {
	local mpm
	local ompm

	for mpm in ${IUSE_MPMS} ; do
		IUSE="${IUSE} apache2_mpms_${mpm}"

		REQUIRED_USE+=" apache2_mpms_${mpm}? ("
		for ompm in ${IUSE_MPMS} ; do
			if [[ "${mpm}" != "${ompm}" ]] ; then
				REQUIRED_USE+=" !apache2_mpms_${ompm}"
			fi
		done

		if has ${mpm} ${IUSE_MPMS_FORK} ; then
			REQUIRED_USE+=" !threads"
		else
			REQUIRED_USE+=" threads"
		fi
		REQUIRED_USE+=" )"
	done

	REQUIRED_USE+=" apache2_mpms_prefork? ( !apache2_modules_http2 )"
}
_apache2_set_mpms
unset -f _apache2_set_mpms

# Dependencies
RDEPEND="
	acct-group/apache
	acct-user/apache
	dev-lang/perl
	>=dev-libs/apr-1.5.1:=
	=dev-libs/apr-util-1*:=[gdbm=,ldap?]
	virtual/libcrypt:=
	apache2_modules_brotli? ( >=app-arch/brotli-0.6.0:= )
	apache2_modules_deflate? ( sys-libs/zlib )
	apache2_modules_http2? (
		>=net-libs/nghttp2-1.2.1
		kernel_linux? ( sys-apps/util-linux )
	)
	apache2_modules_lua? ( ${LUA_DEPS} )
	apache2_modules_md? ( >=dev-libs/jansson-2.10 )
	apache2_modules_mime? ( app-misc/mime-types )
	apache2_modules_proxy_http2? (
		>=net-libs/nghttp2-1.2.1
		kernel_linux? ( sys-apps/util-linux )
	)
	apache2_modules_session_crypto? (
		dev-libs/apr-util[openssl]
	)
	gdbm? ( sys-libs/gdbm:= )
	ldap? ( =net-nds/openldap-2* )
	selinux? ( sec-policy/selinux-apache )
	ssl? (
		>=dev-libs/openssl-1.0.2:0=
		kernel_linux? ( sys-apps/util-linux )
	)
	systemd? ( sys-apps/systemd )
"

if [[ -n ${GENTOO_USE_PCRE1} ]] ; then
	RDEPEND+=" dev-libs/libpcre"
else
	RDEPEND+=" dev-libs/libpcre2"
fi

DEPEND="${RDEPEND}"
BDEPEND="
	virtual/pkgconfig
	suexec? ( suexec-caps? ( sys-libs/libcap ) )
"
PDEPEND="~app-admin/apache-tools-${PV}"

REQUIRED_USE+="
	apache2_modules_http2? ( ssl )
	apache2_modules_lua? ( ${LUA_REQUIRED_USE} )
	apache2_modules_md? ( ssl )
"

S="${WORKDIR}/httpd-${PV}"

# @VARIABLE: MODULE_DEPENDS
# @DESCRIPTION:
# This variable needs to be set in the ebuild and contains a space-separated
# list of dependency tokens each with a module and the module it depends on
# separated by a colon

# now extend REQUIRED_USE to reflect the module dependencies to portage
_apache2_set_module_depends() {
	local dep

	for dep in ${MODULE_DEPENDS} ; do
		REQUIRED_USE+=" apache2_modules_${dep%:*}? ( apache2_modules_${dep#*:} )"
	done
}
_apache2_set_module_depends
unset -f _apache2_set_module_depends

# ==============================================================================
# INTERNAL FUNCTIONS
# ==============================================================================

# @ECLASS_VARIABLE: MY_MPM
# @DESCRIPTION:
# This internal variable contains the selected MPM after a call to setup_mpm()

# @FUNCTION: setup_mpm
# @DESCRIPTION:
# This internal function makes sure that only one of APACHE2_MPMS was selected
# or a default based on USE=threads is selected if APACHE2_MPMS is empty
setup_mpm() {
	MY_MPM=""
	for x in ${IUSE_MPMS} ; do
		if use apache2_mpms_${x} ; then
			# there can at most be one MPM selected because of REQUIRED_USE constraints
			MY_MPM=${x}
			elog
			elog "Selected MPM: ${MY_MPM}"
			elog
			break
		fi
	done

	if [[ -z "${MY_MPM}" ]] ; then
		if use threads ; then
			MY_MPM=${DEFAULT_MPM_THREADED}
			elog
			elog "Selected default threaded MPM: ${MY_MPM}"
			elog
		else
			MY_MPM=prefork
			elog
			elog "Selected default MPM: ${MY_MPM}"
			elog
		fi
	fi
}

# @VARIABLE: MODULE_CRITICAL
# @DESCRIPTION:
# This variable needs to be set in the ebuild and contains a space-separated
# list of modules critical for the default apache. A user may still
# disable these modules for custom minimal installation at their own risk.

# @FUNCTION: check_module_critical
# @DESCRIPTION:
# This internal function warns the user about modules critical for the default
# apache configuration.
check_module_critical() {
	local unsupported=0

	for m in ${MODULE_CRITICAL} ; do
		if ! has ${m} ${MY_MODS[@]} ; then
			ewarn "Module '${m}' is required in the default apache configuration."
			unsupported=1
		fi
	done

	if [[ ${unsupported} -ne 0 ]] ; then
		ewarn
		ewarn "You have disabled one or more required modules"
		ewarn "for the default apache configuration."
		ewarn "Although this is not an error, please be"
		ewarn "aware that this setup is UNSUPPORTED."
		ewarn
	fi
}

# @ECLASS_VARIABLE: MY_CONF
# @DESCRIPTION:
# This internal variable contains the econf options for the current module
# selection after a call to setup_modules()

# @ECLASS_VARIABLE: MY_MODS
# @DESCRIPTION:
# This internal variable contains a sorted, space separated list of currently
# selected modules after a call to setup_modules()

# @FUNCTION: setup_modules
# @DESCRIPTION:
# This internal function selects all built-in modules based on USE flags and
# APACHE2_MODULES USE_EXPAND flags
setup_modules() {
	local mod_type= x=

	if use static ; then
		mod_type="static"
	else
		mod_type="shared"
	fi

	MY_CONF=( --enable-so=static --disable-static )
	MY_MODS=()

	if use ldap ; then
		MY_CONF+=(
			--enable-authnz_ldap=${mod_type}
			--enable-ldap=${mod_type}
		)
		MY_MODS+=( ldap authnz_ldap )
	else
		MY_CONF+=( --disable-authnz_ldap --disable-ldap )
	fi

	if use ssl ; then
		MY_CONF+=( --with-ssl --enable-ssl=${mod_type} )
		MY_MODS+=( ssl )
	else
		MY_CONF+=( --without-ssl --disable-ssl )
	fi

	if use suexec ; then
		elog "You can manipulate several configure options of suexec"
		elog "through the following environment variables:"
		elog
		elog " SUEXEC_SAFEPATH: Default PATH for suexec (default: '${EPREFIX}/usr/local/bin:${EPREFIX}/usr/bin:${EPREFIX}/bin')"
		if ! use suexec-syslog ; then
			elog "  SUEXEC_LOGFILE: Path to the suexec logfile (default: '${EPREFIX}/var/log/apache2/suexec_log')"
		fi
		elog "   SUEXEC_CALLER: Name of the user Apache is running as (default: apache)"
		elog "  SUEXEC_DOCROOT: Directory in which suexec will run scripts (default: '${EPREFIX}/var/www')"
		elog "   SUEXEC_MINUID: Minimum UID, which is allowed to run scripts via suexec (default: 1000)"
		elog "   SUEXEC_MINGID: Minimum GID, which is allowed to run scripts via suexec (default: 100)"
		elog "  SUEXEC_USERDIR: User subdirectories (like /home/user/html) (default: public_html)"
		elog "    SUEXEC_UMASK: Umask for the suexec process (default: 077)"
		elog

		MY_CONF+=( --with-suexec-safepath="${SUEXEC_SAFEPATH:-${EPREFIX}/usr/local/bin:${EPREFIX}/usr/bin:${EPREFIX}/bin}" )
		MY_CONF+=( $(use_with !suexec-syslog suexec-logfile "${SUEXEC_LOGFILE:-${EPREFIX}/var/log/apache2/suexec_log}") )
		MY_CONF+=( $(use_with suexec-syslog) )
		if use suexec-syslog && use suexec-caps ; then
			MY_CONF+=( --enable-suexec-capabilities )
		fi
		MY_CONF+=( --with-suexec-bin="${EPREFIX}/usr/sbin/suexec" )
		MY_CONF+=( --with-suexec-userdir=${SUEXEC_USERDIR:-public_html} )
		MY_CONF+=( --with-suexec-caller=${SUEXEC_CALLER:-apache} )
		MY_CONF+=( --with-suexec-docroot="${SUEXEC_DOCROOT:-${EPREFIX}/var/www}" )
		MY_CONF+=( --with-suexec-uidmin=${SUEXEC_MINUID:-1000} )
		MY_CONF+=( --with-suexec-gidmin=${SUEXEC_MINGID:-100} )
		MY_CONF+=( --with-suexec-umask=${SUEXEC_UMASK:-077} )
		MY_CONF+=( --enable-suexec=${mod_type} )
		MY_MODS+=( suexec )
	else
		MY_CONF+=( --disable-suexec )
	fi

	if use systemd ; then
		MY_CONF+=( --enable-systemd=${mod_type} )
		MY_MODS+=( systemd )
	fi

	for x in ${IUSE_MODULES/ systemd} ; do
		if use apache2_modules_${x} ; then
			MY_CONF+=( --enable-${x}=${mod_type} )
			MY_MODS+=( ${x} )
		else
			MY_CONF+=( --disable-${x} )
		fi
	done

	# sort and uniquify MY_MODS
	MY_MODS=( $(echo ${MY_MODS[@]} | tr ' ' '\n' | sort -u) )
	check_module_critical
}

# @VARIABLE: MODULE_DEFINES
# @DESCRIPTION:
# This variable needs to be set in the ebuild and contains a space-separated
# list of tokens each mapping a module to a runtime define which can be
# specified in APACHE2_OPTS in /etc/conf.d/apache2 to enable this particular
# module.

# @FUNCTION: generate_load_module
# @DESCRIPTION:
# This internal function generates the LoadModule lines for httpd.conf based on
# the current module selection and MODULE_DEFINES
generate_load_module() {
	local def= endit=0 m= mod_lines= mod_dir="${ED}/usr/$(get_libdir)/apache2/modules"

	if use static; then
		sed -i -e "/%%LOAD_MODULE%%/d" \
			"${GENTOO_PATCHDIR}"/conf/httpd.conf
		return
	fi

	for m in ${MY_MODS[@]} ; do
		if [[ -e "${mod_dir}/mod_${m}.so" ]] ; then
			for def in ${MODULE_DEFINES} ; do
				if [[ "${m}" == "${def%:*}" ]] ; then
					mod_lines="${mod_lines}\n<IfDefine ${def#*:}>"
					endit=1
				fi
			done

			mod_lines="${mod_lines}\nLoadModule ${m}_module modules/mod_${m}.so"

			if [[ ${endit} -ne 0 ]] ; then
				mod_lines="${mod_lines}\n</IfDefine>"
				endit=0
			fi
		fi
	done

	sed -i -e "s:%%LOAD_MODULE%%:${mod_lines}:" \
		"${GENTOO_PATCHDIR}"/conf/httpd.conf
}

# @FUNCTION: check_upgrade
# @DESCRIPTION:
# This internal function checks if the previous configuration file for built-in
# modules exists in ROOT and prevents upgrade in this case. Users are supposed
# to convert this file to the new APACHE2_MODULES USE_EXPAND variable and remove
# it afterwards.
check_upgrade() {
	if [[ -e "${EROOT}"etc/apache2/apache2-builtin-mods ]]; then
		eerror "The previous configuration file for built-in modules"
		eerror "(${EROOT}etc/apache2/apache2-builtin-mods) exists on your"
		eerror "system."
		eerror
		eerror "Please read https://wiki.gentoo.org/wiki/Project:Apache/Upgrading"
		eerror "for detailed information how to convert this file to the new"
		eerror "APACHE2_MODULES USE_EXPAND variable."
		eerror
		die "upgrade not possible with existing ${ROOT}etc/apache2/apache2-builtin-mods"
	fi
}

# ==============================================================================
# EXPORTED FUNCTIONS
# ==============================================================================

# @FUNCTION: apache-2_pkg_setup
# @DESCRIPTION:
# This function selects built-in modules, the MPM and other configure options,
# creates the apache user and group and informs about CONFIG_SYSVIPC being
# needed (we don't depend on kernel sources and therefore cannot check).
apache-2_pkg_setup() {
	check_upgrade

	setup_mpm
	setup_modules

	if use debug; then
		MY_CONF+=( --enable-exception-hook )
	fi

	elog "Please note that you need SysV IPC support in your kernel."
	elog "Make sure CONFIG_SYSVIPC=y is set."
	elog

	if use apache2_modules_lua ; then
		lua-single_pkg_setup
	fi
}

# @FUNCTION: apache-2_src_prepare
# @DESCRIPTION:
# This function applies patches, configures a custom file-system layout and
# rebuilds the configure scripts.
apache-2_src_prepare() {
	#fix prefix in conf files etc (bug #433736)
	use !prefix || sed -e "s@/\(usr\|var\|etc\|run\)/@${EPREFIX}&@g" \
		-i "${GENTOO_PATCHDIR}"/conf/httpd.conf "${GENTOO_PATCHDIR}"/scripts/* \
		"${GENTOO_PATCHDIR}"/docs/*.example "${GENTOO_PATCHDIR}"/patches/*.layout \
		"${GENTOO_PATCHDIR}"/init/* "${GENTOO_PATCHDIR}"/conf/vhosts.d/* \
		"${GENTOO_PATCHDIR}"/conf/modules.d/* || die

	# 03_all_gentoo-apache-tools.patch injects -Wl,-z,now, which is not a good
	# idea for everyone
	case ${CHOST} in
		*-linux-gnu|*-solaris*|*-freebsd*)
			# do nothing, these use GNU binutils
			:
		;;
		*-darwin*)
			sed -i -e 's/-Wl,-z,now/-Wl,-bind_at_load/g' \
				"${GENTOO_PATCHDIR}"/patches/03_all_gentoo_apache-tools.patch \
				|| die
		;;
		*)
			# patch it out to be like upstream
			sed -i -e 's/-Wl,-z,now//g' \
				"${GENTOO_PATCHDIR}"/patches/03_all_gentoo_apache-tools.patch \
				|| die
		;;
	esac

	# Use correct multilib libdir in gentoo patches
	sed -i -e "s:/usr/lib:/usr/$(get_libdir):g" \
		"${GENTOO_PATCHDIR}"/{conf/httpd.conf,init/*,patches/config.layout} \
		|| die "libdir sed failed"

	eapply "${GENTOO_PATCHDIR}"/patches/*.patch
	default

	# Don't rename configure.in _before_ any possible user patches!
	if [[ -f "configure.in" ]] ; then
		einfo "Renaming configure.in to configure.ac"
		mv configure.{in,ac} || die
	fi

	# setup the filesystem layout config
	cat "${GENTOO_PATCHDIR}"/patches/config.layout >> "${S}"/config.layout || \
		die "Failed preparing config.layout!"
	sed -i -e "s:version:${PF}:g" "${S}"/config.layout || die

	# apache2.8 instead of httpd.8 (bug #194828)
	mv docs/man/{httpd,apache2}.8 || die
	sed -i -e 's/httpd\.8/apache2.8/g' Makefile.in || die

	# patched-in MPMs need the build environment rebuilt
	sed -i -e '/sinclude/d' configure.ac || die
	AT_M4DIR=build eautoreconf

	# ${T} must be not group-writable, else grsec TPE will block it
	chmod g-w "${T}" || die

	if [[ -n ${GENTOO_USE_PCRE1} ]] ; then
		# This package really should upgrade to using pcre's .pc file.
		cat <<-\EOF > "${T}"/pcre-config
		#!/usr/bin/env bash
		flags=()
		for flag; do
			if [[ ${flag} == "--version" ]]; then
				flags+=( --modversion )
			else
				flags+=( "${flag}" )
			fi
		done
		exec ${PKG_CONFIG} libpcre "${flags[@]}"
		EOF
		chmod a+x "${T}"/pcre-config || die
	else
			# This package really should upgrade to using pcre's .pc file.
		cat <<-\EOF > "${T}"/pcre2-config
		#!/usr/bin/env bash
		flags=()
		for flag; do
			if [[ ${flag} == "--version" ]]; then
				flags+=( --modversion )
			else
				flags+=( "${flag}" )
			fi
		done
		exec ${PKG_CONFIG} libpcre2-8 "${flags[@]}"
		EOF
		chmod a+x "${T}"/pcre2-config || die
	fi
}

# @FUNCTION: apache-2_src_configure
# @DESCRIPTION:
# This function adds compiler flags and runs econf and emake based on MY_MPM and
# MY_CONF
apache-2_src_configure() {
	tc-export PKG_CONFIG
	export ac_cv_path_PKGCONFIG="${PKG_CONFIG}"

	# Sanity check in case people have bad mounts/TPE settings. #500928
	if ! "${T}"/pcre-config --help >/dev/null && ! "${T}"/pcre2-config --help >/dev/null ; then
		eerror "Could not execute ${T}/pcre-config (or pcre2-config); do you have bad mount"
		eerror "permissions in ${T} or have TPE turned on in your kernel?"
		die "check your runtime settings #500928"
	fi

	# Instead of filtering --as-needed (bug #128505), append --no-as-needed
	# Thanks to Harald van Dijk
	append-ldflags $(no-as-needed)

	# peruser MPM debugging with -X is nearly impossible
	if has peruser ${IUSE_MPMS} && use apache2_mpms_peruser ; then
		use debug && append-flags -DMPM_PERUSER_DEBUG
	fi

	# econf overwrites the stuff from config.layout, so we have to put them into
	# our myconf line too
	MY_CONF+=(
		--includedir="${EPREFIX}"/usr/include/apache2
		--libexecdir="${EPREFIX}"/usr/$(get_libdir)/apache2/modules
		--datadir="${EPREFIX}"/var/www/localhost
		--sysconfdir="${EPREFIX}"/etc/apache2
		--localstatedir="${EPREFIX}"/var
		--with-mpm=${MY_MPM}
		--with-apr="${SYSROOT}${EPREFIX}"/usr
		--with-apr-util="${SYSROOT}${EPREFIX}"/usr
		--with-z="${EPREFIX}"/usr
		--with-port=80
		--with-program-name=apache2
		--enable-layout=Gentoo
	)

	if [[ -n ${GENTOO_USE_PCRE1} ]] ; then
		export ac_cv_prog_ac_ct_PCRE_CONFIG="${T}"/pcre-config

		MY_CONF+=(
			--without-pcre2
			--with-pcre="${T}"/pcre-config
		)
	else
		export ac_cv_prog_ac_ct_PCRE_CONFIG="${T}"/pcre2-config

		MY_CONF+=(
			--without-pcre
			--with-pcre2="${T}"/pcre2-config
		)
	fi

	econf "${MY_CONF[@]}"

	sed -i -e 's:apache2\.conf:httpd.conf:' include/ap_config_auto.h || die
}

# @FUNCTION: apache-2_src_install
# @DESCRIPTION:
# This function runs `emake install' and generates, installs and adapts the gentoo
# specific configuration files found in the tarball
apache-2_src_install() {
	emake DESTDIR="${D}" MKINSTALLDIRS="mkdir -p" install

	# install our configuration files
	keepdir /etc/apache2/vhosts.d
	keepdir /etc/apache2/modules.d

	generate_load_module
	insinto /etc/apache2
	doins -r "${GENTOO_PATCHDIR}"/conf/*
	use apache2_modules_mime_magic && doins docs/conf/magic

	insinto /etc/logrotate.d
	newins "${GENTOO_PATCHDIR}"/scripts/apache2-logrotate apache2

	# generate a sane default APACHE2_OPTS
	APACHE2_OPTS="-D DEFAULT_VHOST -D INFO"
	use doc && APACHE2_OPTS+=" -D MANUAL"
	use ssl && APACHE2_OPTS+=" -D SSL -D SSL_DEFAULT_VHOST"
	use suexec && APACHE2_OPTS+=" -D SUEXEC"
	if has negotiation ${APACHE2_MODULES} && use apache2_modules_negotiation; then
		APACHE2_OPTS+=" -D LANGUAGE"
	fi

	sed -i -e "s:APACHE2_OPTS=\".*\":APACHE2_OPTS=\"${APACHE2_OPTS}\":" \
		"${GENTOO_PATCHDIR}"/init/apache2.confd || die

	newconfd "${GENTOO_PATCHDIR}"/init/apache2.confd apache2
	newinitd "${GENTOO_PATCHDIR}"/init/apache2.initd apache2

	# install apache2ctl wrapper for our init script if available
	if test -e "${GENTOO_PATCHDIR}"/scripts/apache2ctl; then
		exeinto /usr/sbin
		doexe "${GENTOO_PATCHDIR}"/scripts/apache2ctl
	else
		dosym /etc/init.d/apache2 /usr/sbin/apache2ctl
	fi

	# provide legacy symlink for apxs, bug 177697
	dosym apxs /usr/sbin/apxs2

	# install some documentation
	dodoc ABOUT_APACHE CHANGES LAYOUT README README.platforms VERSIONING
	dodoc "${GENTOO_PATCHDIR}"/docs/*

	# drop in a convenient link to the manual
	if use doc ; then
		sed -i -e "s:VERSION:${PVR}:" \
			"${ED}/etc/apache2/modules.d/00_apache_manual.conf" \
			|| die
		docompress -x /usr/share/doc/${PF}/manual # 503640
	else
		rm -f "${ED}/etc/apache2/modules.d/00_apache_manual.conf" \
			|| die
		rm -rf "${ED}/usr/share/doc/${PF}/manual" || die
	fi

	# the default icons and error pages get stored in
	# /usr/share/apache2/{error,icons}
	dodir /usr/share/apache2
	mv -f "${ED}/var/www/localhost/error" \
		"${ED}/usr/share/apache2/error" || die
	mv -f "${ED}/var/www/localhost/icons" \
		"${ED}/usr/share/apache2/icons" || die
	rm -rf "${ED}/var/www/localhost/" || die

	# set some sane permissions for suexec
	if use suexec ; then
		if ! use suexec-syslog || ! use suexec-caps ; then
			fowners 0:${SUEXEC_CALLER:-apache} /usr/sbin/suexec
			fperms 4710 /usr/sbin/suexec
			# provide legacy symlink for suexec, bug 177697
			dosym /usr/sbin/suexec /usr/sbin/suexec2
		fi
	fi

	# empty dirs
	local i
	for i in /var/lib/dav /var/log/apache2 /var/cache/apache2 ; do
		keepdir ${i}
		fowners apache:apache ${i}
		fperms 0750 ${i}
	done
}

# @FUNCTION: apache-2_pkg_postinst
# @DESCRIPTION:
# This function creates test certificates if SSL is enabled and installs the
# default index.html to /var/www/localhost if it does not exist. We do this here
# because the default webroot is a copy of the files that exist elsewhere and we
# don't want them to be managed/removed by portage when apache is upgraded.
apache-2_pkg_postinst() {
	if use ssl && [[ ! -e "${EROOT}/etc/ssl/apache2/server.pem" ]]; then
		SSL_ORGANIZATION="${SSL_ORGANIZATION:-Apache HTTP Server}"
		install_cert /etc/ssl/apache2/server
		ewarn
		ewarn "The location of SSL certificates has changed. If you are"
		ewarn "upgrading from ${CATEGORY}/${PN}-2.2.13 or earlier (or remerged"
		ewarn "*any* apache version), you might want to move your old"
		ewarn "certificates from /etc/apache2/ssl/ to /etc/ssl/apache2/ and"
		ewarn "update your config files."
		ewarn
	fi

	if [[ ! -e "${EROOT}/var/www/localhost" ]] ; then
		mkdir -p "${EROOT}/var/www/localhost/htdocs"
		echo "<html><body><h1>It works!</h1></body></html>" > "${EROOT}/var/www/localhost/htdocs/index.html"
	fi

	echo
	elog "Attention: cgi and cgid modules are now handled via APACHE2_MODULES flags"
	elog "in make.conf. Make sure to enable those in order to compile them."
	elog "In general, you should use 'cgid' with threaded MPMs and 'cgi' otherwise."
	echo

}

EXPORT_FUNCTIONS pkg_setup src_prepare src_configure src_install pkg_postinst