1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: apache-2.eclass
7 # polynomial-c@gentoo.org
8 # @BLURB: Provides a common set of functions for apache-2.x ebuilds
10 # This eclass handles apache-2.x ebuild functions such as LoadModule generation
11 # and inter-module dependency checking.
13 inherit autotools eutils flag-o-matic multilib ssl-cert user toolchain-funcs versionator
15 [[ ${CATEGORY}/${PN} != www-servers/apache ]] \
16 && die "Do not use this eclass with anything else than www-servers/apache ebuilds!"
20 die "This eclass requires >=EAPI-5"
24 # settings which are version specific go in here:
25 case $(get_version_component_range 1-2) in
27 DEFAULT_MPM_THREADED="event" #509922
28 RDEPEND=">=dev-libs/apr-1.5.1
29 !www-apache/mod_macro" #492578 #477702
32 DEFAULT_MPM_THREADED="worker"
33 RDEPEND=">=dev-libs/apr-1.4.5" #368651
37 # ==============================================================================
39 # ==============================================================================
41 # @ECLASS-VARIABLE: GENTOO_PATCHNAME
43 # This internal variable contains the prefix for the patch tarball.
44 # Defaults to the full name and version (including revision) of the package.
45 # If you want to override this in an ebuild, use:
46 # ORIG_PR="(revision of Gentoo stuff you want)"
47 # GENTOO_PATCHNAME="gentoo-${PN}-${PV}${ORIG_PR:+-${ORIG_PR}}"
48 [[ -n "$GENTOO_PATCHNAME" ]] || GENTOO_PATCHNAME="gentoo-${PF}"
50 # @ECLASS-VARIABLE: GENTOO_PATCHDIR
52 # This internal variable contains the working directory where patches and config
54 # Defaults to the patchset name appended to the working directory.
55 [[ -n "$GENTOO_PATCHDIR" ]] || GENTOO_PATCHDIR="${WORKDIR}/${GENTOO_PATCHNAME}"
57 # @VARIABLE: GENTOO_DEVELOPER
59 # This variable needs to be set in the ebuild and contains the name of the
60 # gentoo developer who created the patch tarball
62 # @VARIABLE: GENTOO_PATCHSTAMP
64 # This variable needs to be set in the ebuild and contains the date the patch
65 # tarball was created at in YYYYMMDD format
67 # @VARIABLE: GENTOO_PATCH_A
69 # This variable should contain the entire filename of patch tarball.
70 # Defaults to the name of the patchset, with a datestamp.
71 [[ -n "$GENTOO_PATCH_A" ]] || GENTOO_PATCH_A="${GENTOO_PATCHNAME}-${GENTOO_PATCHSTAMP}.tar.bz2"
73 SRC_URI="mirror://apache/httpd/httpd-${PV}.tar.bz2
74 https://dev.gentoo.org/~${GENTOO_DEVELOPER}/dist/apache/${GENTOO_PATCH_A}"
76 # @VARIABLE: IUSE_MPMS_FORK
78 # This variable needs to be set in the ebuild and contains a list of forking
79 # (i.e. non-threaded) MPMs
81 # @VARIABLE: IUSE_MPMS_THREAD
83 # This variable needs to be set in the ebuild and contains a list of threaded
86 # @VARIABLE: IUSE_MODULES
88 # This variable needs to be set in the ebuild and contains a list of available
91 IUSE_MPMS="${IUSE_MPMS_FORK} ${IUSE_MPMS_THREAD}"
92 IUSE="${IUSE} debug doc ldap libressl selinux ssl static suexec threads"
94 for module in ${IUSE_MODULES} ; do
95 IUSE="${IUSE} apache2_modules_${module}"
98 for mpm in ${IUSE_MPMS} ; do
99 IUSE="${IUSE} apache2_mpms_${mpm}"
102 DEPEND="dev-lang/perl
104 =dev-libs/apr-util-1*[ldap?]
106 apache2_modules_deflate? ( sys-libs/zlib )
107 apache2_modules_mime? ( app-misc/mime-types )
108 ldap? ( =net-nds/openldap-2* )
110 !libressl? ( >=dev-libs/openssl-1.0.2:0= )
111 libressl? ( dev-libs/libressl:= )
113 !=www-servers/apache-1*"
115 selinux? ( sec-policy/selinux-apache )"
116 PDEPEND="~app-admin/apache-tools-${PV}"
118 S="${WORKDIR}/httpd-${PV}"
120 # ==============================================================================
122 # ==============================================================================
124 # @ECLASS-VARIABLE: MY_MPM
126 # This internal variable contains the selected MPM after a call to setup_mpm()
128 # @FUNCTION: setup_mpm
130 # This internal function makes sure that only one of APACHE2_MPMS was selected
131 # or a default based on USE=threads is selected if APACHE2_MPMS is empty
134 for x in ${IUSE_MPMS} ; do
135 if use apache2_mpms_${x} ; then
136 if [[ -z "${MY_MPM}" ]] ; then
139 elog "Selected MPM: ${MY_MPM}"
142 eerror "You have selected more then one mpm USE-flag."
143 eerror "Only one MPM is supported."
144 die "more then one mpm was specified"
149 if [[ -z "${MY_MPM}" ]] ; then
150 if use threads ; then
151 MY_MPM=${DEFAULT_MPM_THREADED}
153 elog "Selected default threaded MPM: ${MY_MPM}"
158 elog "Selected default MPM: ${MY_MPM}"
163 if has ${MY_MPM} ${IUSE_MPMS_THREAD} && ! use threads ; then
164 eerror "You have selected a threaded MPM but USE=threads is disabled"
165 die "invalid use flag combination"
168 if has ${MY_MPM} ${IUSE_MPMS_FORK} && use threads ; then
169 eerror "You have selected a non-threaded MPM but USE=threads is enabled"
170 die "invalid use flag combination"
174 # @VARIABLE: MODULE_CRITICAL
176 # This variable needs to be set in the ebuild and contains a space-separated
177 # list of modules critical for the default apache. A user may still
178 # disable these modules for custom minimal installation at their own risk.
180 # @FUNCTION: check_module_critical
182 # This internal function warns the user about modules critical for the default
183 # apache configuration.
184 check_module_critical() {
187 for m in ${MODULE_CRITICAL} ; do
188 if ! has ${m} ${MY_MODS[@]} ; then
189 ewarn "Module '${m}' is required in the default apache configuration."
194 if [[ ${unsupported} -ne 0 ]] ; then
196 ewarn "You have disabled one or more required modules"
197 ewarn "for the default apache configuration."
198 ewarn "Although this is not an error, please be"
199 ewarn "aware that this setup is UNSUPPORTED."
204 # @VARIABLE: MODULE_DEPENDS
206 # This variable needs to be set in the ebuild and contains a space-separated
207 # list of dependency tokens each with a module and the module it depends on
208 # separated by a colon
210 # @FUNCTION: check_module_depends
212 # This internal function makes sure that all inter-module dependencies are
213 # satisfied with the current module selection
214 check_module_depends() {
217 for m in ${MY_MODS[@]} ; do
218 for dep in ${MODULE_DEPENDS} ; do
219 if [[ "${m}" == "${dep%:*}" ]] ; then
220 if ! use apache2_modules_${dep#*:} ; then
221 eerror "Module '${m}' depends on '${dep#*:}'"
228 if [[ ${err} -ne 0 ]] ; then
229 die "invalid use flag combination"
233 # @ECLASS-VARIABLE: MY_CONF
235 # This internal variable contains the econf options for the current module
236 # selection after a call to setup_modules()
238 # @ECLASS-VARIABLE: MY_MODS
240 # This internal variable contains a sorted, space separated list of currently
241 # selected modules after a call to setup_modules()
243 # @FUNCTION: setup_modules
245 # This internal function selects all built-in modules based on USE flags and
246 # APACHE2_MODULES USE_EXPAND flags
256 MY_CONF=( --enable-so=static )
260 MY_CONF+=( --enable-authnz_ldap=${mod_type} --enable-ldap=${mod_type} )
261 MY_MODS+=( ldap authnz_ldap )
263 MY_CONF+=( --disable-authnz_ldap --disable-ldap )
267 MY_CONF+=( --with-ssl="${EPREFIX}"/usr --enable-ssl=${mod_type} )
270 MY_CONF+=( --without-ssl --disable-ssl )
274 elog "You can manipulate several configure options of suexec"
275 elog "through the following environment variables:"
277 elog " SUEXEC_SAFEPATH: Default PATH for suexec (default: '${EPREFIX}/usr/local/bin:${EPREFIX}/usr/bin:${EPREFIX}/bin')"
278 elog " SUEXEC_LOGFILE: Path to the suexec logfile (default: '${EPREFIX}/var/log/apache2/suexec_log')"
279 elog " SUEXEC_CALLER: Name of the user Apache is running as (default: apache)"
280 elog " SUEXEC_DOCROOT: Directory in which suexec will run scripts (default: '${EPREFIX}/var/www')"
281 elog " SUEXEC_MINUID: Minimum UID, which is allowed to run scripts via suexec (default: 1000)"
282 elog " SUEXEC_MINGID: Minimum GID, which is allowed to run scripts via suexec (default: 100)"
283 elog " SUEXEC_USERDIR: User subdirectories (like /home/user/html) (default: public_html)"
284 elog " SUEXEC_UMASK: Umask for the suexec process (default: 077)"
287 MY_CONF+=( --with-suexec-safepath="${SUEXEC_SAFEPATH:-${EPREFIX}/usr/local/bin:${EPREFIX}/usr/bin:${EPREFIX}/bin}" )
288 MY_CONF+=( --with-suexec-logfile="${SUEXEC_LOGFILE:-${EPREFIX}/var/log/apache2/suexec_log}" )
289 MY_CONF+=( --with-suexec-bin="${EPREFIX}/usr/sbin/suexec" )
290 MY_CONF+=( --with-suexec-userdir=${SUEXEC_USERDIR:-public_html} )
291 MY_CONF+=( --with-suexec-caller=${SUEXEC_CALLER:-apache} )
292 MY_CONF+=( --with-suexec-docroot="${SUEXEC_DOCROOT:-${EPREFIX}/var/www}" )
293 MY_CONF+=( --with-suexec-uidmin=${SUEXEC_MINUID:-1000} )
294 MY_CONF+=( --with-suexec-gidmin=${SUEXEC_MINGID:-100} )
295 MY_CONF+=( --with-suexec-umask=${SUEXEC_UMASK:-077} )
296 MY_CONF+=( --enable-suexec=${mod_type} )
299 MY_CONF+=( --disable-suexec )
302 for x in ${IUSE_MODULES} ; do
303 if use apache2_modules_${x} ; then
304 MY_CONF+=( --enable-${x}=${mod_type} )
307 MY_CONF+=( --disable-${x} )
311 # sort and uniquify MY_MODS
312 MY_MODS=( $(echo ${MY_MODS[@]} | tr ' ' '\n' | sort -u) )
314 check_module_critical
317 # @VARIABLE: MODULE_DEFINES
319 # This variable needs to be set in the ebuild and contains a space-separated
320 # list of tokens each mapping a module to a runtime define which can be
321 # specified in APACHE2_OPTS in /etc/conf.d/apache2 to enable this particular
324 # @FUNCTION: generate_load_module
326 # This internal function generates the LoadModule lines for httpd.conf based on
327 # the current module selection and MODULE_DEFINES
328 generate_load_module() {
329 local endit=0 mod_lines= mod_dir="${ED}/usr/$(get_libdir)/apache2/modules"
332 sed -i -e "/%%LOAD_MODULE%%/d" \
333 "${GENTOO_PATCHDIR}"/conf/httpd.conf
337 for m in ${MY_MODS[@]} ; do
338 if [[ -e "${mod_dir}/mod_${m}.so" ]] ; then
339 for def in ${MODULE_DEFINES} ; do
340 if [[ "${m}" == "${def%:*}" ]] ; then
341 mod_lines="${mod_lines}\n<IfDefine ${def#*:}>"
346 mod_lines="${mod_lines}\nLoadModule ${m}_module modules/mod_${m}.so"
348 if [[ ${endit} -ne 0 ]] ; then
349 mod_lines="${mod_lines}\n</IfDefine>"
355 sed -i -e "s:%%LOAD_MODULE%%:${mod_lines}:" \
356 "${GENTOO_PATCHDIR}"/conf/httpd.conf
359 # @FUNCTION: check_upgrade
361 # This internal function checks if the previous configuration file for built-in
362 # modules exists in ROOT and prevents upgrade in this case. Users are supposed
363 # to convert this file to the new APACHE2_MODULES USE_EXPAND variable and remove
366 if [[ -e "${EROOT}"etc/apache2/apache2-builtin-mods ]]; then
367 eerror "The previous configuration file for built-in modules"
368 eerror "(${EROOT}etc/apache2/apache2-builtin-mods) exists on your"
371 eerror "Please read https://wiki.gentoo.org/wiki/Project:Apache/Upgrading"
372 eerror "for detailed information how to convert this file to the new"
373 eerror "APACHE2_MODULES USE_EXPAND variable."
375 die "upgrade not possible with existing ${ROOT}etc/apache2/apache2-builtin-mods"
379 # ==============================================================================
381 # ==============================================================================
383 # @FUNCTION: apache-2_pkg_setup
385 # This function selects built-in modules, the MPM and other configure options,
386 # creates the apache user and group and informs about CONFIG_SYSVIPC being
387 # needed (we don't depend on kernel sources and therefore cannot check).
388 apache-2_pkg_setup() {
391 # setup apache user and group
393 enewuser apache 81 -1 /var/www apache
399 MY_CONF+=( --enable-maintainer-mode --enable-exception-hook )
402 elog "Please note that you need SysV IPC support in your kernel."
403 elog "Make sure CONFIG_SYSVIPC=y is set."
406 if use userland_BSD; then
407 elog "On BSD systems you need to add the following line to /boot/loader.conf:"
408 elog " accf_http_load=\"YES\""
413 # @FUNCTION: apache-2_src_prepare
415 # This function applies patches, configures a custom file-system layout and
416 # rebuilds the configure scripts.
417 apache-2_src_prepare() {
418 #fix prefix in conf files etc (bug #433736)
419 use !prefix || sed -e "s@/\(usr\|var\|etc\|run\)/@${EPREFIX}&@g" \
420 -i "${GENTOO_PATCHDIR}"/conf/httpd.conf "${GENTOO_PATCHDIR}"/scripts/* \
421 "${GENTOO_PATCHDIR}"/docs/*.example "${GENTOO_PATCHDIR}"/patches/*.layout \
422 "${GENTOO_PATCHDIR}"/init/* "${GENTOO_PATCHDIR}"/conf/vhosts.d/* \
423 "${GENTOO_PATCHDIR}"/conf/modules.d/* || die
425 # 03_all_gentoo-apache-tools.patch injects -Wl,-z,now, which is not a good
428 *-linux-gnu|*-solaris*|*-freebsd*)
429 # do nothing, these use GNU binutils
433 sed -i -e 's/-Wl,-z,now/-Wl,-bind_at_load/g' \
434 "${GENTOO_PATCHDIR}"/patches/03_all_gentoo_apache-tools.patch
437 # patch it out to be like upstream
438 sed -i -e 's/-Wl,-z,now//g' \
439 "${GENTOO_PATCHDIR}"/patches/03_all_gentoo_apache-tools.patch
443 # Use correct multilib libdir in gentoo patches
444 sed -i -e "s:/usr/lib:/usr/$(get_libdir):g" \
445 "${GENTOO_PATCHDIR}"/{conf/httpd.conf,init/*,patches/config.layout} \
446 || die "libdir sed failed"
448 epatch "${GENTOO_PATCHDIR}"/patches/*.patch
450 # setup the filesystem layout config
451 cat "${GENTOO_PATCHDIR}"/patches/config.layout >> "${S}"/config.layout || \
452 die "Failed preparing config.layout!"
453 sed -i -e "s:version:${PF}:g" "${S}"/config.layout
455 # apache2.8 instead of httpd.8 (bug #194828)
456 mv docs/man/{httpd,apache2}.8
457 sed -i -e 's/httpd\.8/apache2.8/g' Makefile.in
459 # patched-in MPMs need the build environment rebuilt
460 sed -i -e '/sinclude/d' configure.in
461 AT_M4DIR=build eautoreconf
463 # ${T} must be not group-writable, else grsec TPE will block it
466 # This package really should upgrade to using pcre's .pc file.
467 cat <<-\EOF >"${T}"/pcre-config
469 [ "${flag}" = "--version" ] && set -- --modversion
470 exec ${PKG_CONFIG} libpcre "$@"
472 chmod a+x "${T}"/pcre-config
475 # @FUNCTION: apache-2_src_configure
477 # This function adds compiler flags and runs econf and emake based on MY_MPM and
479 apache-2_src_configure() {
482 # Sanity check in case people have bad mounts/TPE settings. #500928
483 if ! "${T}"/pcre-config --help >/dev/null ; then
484 eerror "Could not execute ${T}/pcre-config; do you have bad mount"
485 eerror "permissions in ${T} or have TPE turned on in your kernel?"
486 die "check your runtime settings #500928"
489 # Instead of filtering --as-needed (bug #128505), append --no-as-needed
490 # Thanks to Harald van Dijk
491 append-ldflags $(no-as-needed)
493 # peruser MPM debugging with -X is nearly impossible
494 if has peruser ${IUSE_MPMS} && use apache2_mpms_peruser ; then
495 use debug && append-flags -DMPM_PERUSER_DEBUG
498 # econf overwrites the stuff from config.layout, so we have to put them into
499 # our myconf line too
500 ac_cv_path_PKGCONFIG=${PKG_CONFIG} \
502 --includedir="${EPREFIX}"/usr/include/apache2 \
503 --libexecdir="${EPREFIX}"/usr/$(get_libdir)/apache2/modules \
504 --datadir="${EPREFIX}"/var/www/localhost \
505 --sysconfdir="${EPREFIX}"/etc/apache2 \
506 --localstatedir="${EPREFIX}"/var \
507 --with-mpm=${MY_MPM} \
508 --with-apr="${SYSROOT}${EPREFIX}"/usr \
509 --with-apr-util="${SYSROOT}${EPREFIX}"/usr \
510 --with-pcre="${T}"/pcre-config \
511 --with-z="${EPREFIX}"/usr \
513 --with-program-name=apache2 \
514 --enable-layout=Gentoo \
517 sed -i -e 's:apache2\.conf:httpd.conf:' include/ap_config_auto.h
520 # @FUNCTION: apache-2_src_install
522 # This function runs `emake install' and generates, installs and adapts the gentoo
523 # specific configuration files found in the tarball
524 apache-2_src_install() {
525 emake DESTDIR="${D}" MKINSTALLDIRS="mkdir -p" install
527 # install our configuration files
528 keepdir /etc/apache2/vhosts.d
529 keepdir /etc/apache2/modules.d
533 doins -r "${GENTOO_PATCHDIR}"/conf/*
534 use apache2_modules_mime_magic && doins docs/conf/magic
536 insinto /etc/logrotate.d
537 newins "${GENTOO_PATCHDIR}"/scripts/apache2-logrotate apache2
539 # generate a sane default APACHE2_OPTS
540 APACHE2_OPTS="-D DEFAULT_VHOST -D INFO"
541 use doc && APACHE2_OPTS="${APACHE2_OPTS} -D MANUAL"
542 use ssl && APACHE2_OPTS="${APACHE2_OPTS} -D SSL -D SSL_DEFAULT_VHOST"
543 use suexec && APACHE2_OPTS="${APACHE2_OPTS} -D SUEXEC"
544 if has negotiation ${APACHE2_MODULES} && use apache2_modules_negotiation; then
545 APACHE2_OPTS="${APACHE2_OPTS} -D LANGUAGE"
548 sed -i -e "s:APACHE2_OPTS=\".*\":APACHE2_OPTS=\"${APACHE2_OPTS}\":" \
549 "${GENTOO_PATCHDIR}"/init/apache2.confd || die "sed failed"
551 newconfd "${GENTOO_PATCHDIR}"/init/apache2.confd apache2
552 newinitd "${GENTOO_PATCHDIR}"/init/apache2.initd apache2
554 # install apache2ctl wrapper for our init script if available
555 if test -e "${GENTOO_PATCHDIR}"/scripts/apache2ctl; then
557 doexe "${GENTOO_PATCHDIR}"/scripts/apache2ctl
559 dosym /etc/init.d/apache2 /usr/sbin/apache2ctl
562 # provide legacy symlink for apxs, bug 177697
563 dosym apxs /usr/sbin/apxs2
565 # install some documentation
566 dodoc ABOUT_APACHE CHANGES LAYOUT README README.platforms VERSIONING
567 dodoc "${GENTOO_PATCHDIR}"/docs/*
569 # drop in a convenient link to the manual
571 sed -i -e "s:VERSION:${PVR}:" "${ED}/etc/apache2/modules.d/00_apache_manual.conf"
572 docompress -x /usr/share/doc/${PF}/manual # 503640
574 rm -f "${ED}/etc/apache2/modules.d/00_apache_manual.conf"
575 rm -Rf "${ED}/usr/share/doc/${PF}/manual"
578 # the default icons and error pages get stored in
579 # /usr/share/apache2/{error,icons}
580 dodir /usr/share/apache2
581 mv -f "${ED}/var/www/localhost/error" "${ED}/usr/share/apache2/error"
582 mv -f "${ED}/var/www/localhost/icons" "${ED}/usr/share/apache2/icons"
583 rm -rf "${ED}/var/www/localhost/"
586 # set some sane permissions for suexec
588 fowners 0:${SUEXEC_CALLER:-apache} /usr/sbin/suexec
589 fperms 4710 /usr/sbin/suexec
590 # provide legacy symlink for suexec, bug 177697
591 dosym /usr/sbin/suexec /usr/sbin/suexec2
595 for i in /var/lib/dav /var/log/apache2 /var/cache/apache2 ; do
597 fowners apache:apache ${i}
602 # @FUNCTION: apache-2_pkg_postinst
604 # This function creates test certificates if SSL is enabled and installs the
605 # default index.html to /var/www/localhost if it does not exist. We do this here
606 # because the default webroot is a copy of the files that exist elsewhere and we
607 # don't want them to be managed/removed by portage when apache is upgraded.
608 apache-2_pkg_postinst() {
609 if use ssl && [[ ! -e "${EROOT}/etc/ssl/apache2/server.pem" ]]; then
610 SSL_ORGANIZATION="${SSL_ORGANIZATION:-Apache HTTP Server}"
611 install_cert /etc/ssl/apache2/server
613 ewarn "The location of SSL certificates has changed. If you are"
614 ewarn "upgrading from ${CATEGORY}/${PN}-2.2.13 or earlier (or remerged"
615 ewarn "*any* apache version), you might want to move your old"
616 ewarn "certificates from /etc/apache2/ssl/ to /etc/ssl/apache2/ and"
617 ewarn "update your config files."
621 if [[ ! -e "${EROOT}/var/www/localhost" ]] ; then
622 mkdir -p "${EROOT}/var/www/localhost/htdocs"
623 echo "<html><body><h1>It works!</h1></body></html>" > "${EROOT}/var/www/localhost/htdocs/index.html"
627 elog "Attention: cgi and cgid modules are now handled via APACHE2_MODULES flags"
628 elog "in make.conf. Make sure to enable those in order to compile them."
629 elog "In general, you should use 'cgid' with threaded MPMs and 'cgi' otherwise."
634 EXPORT_FUNCTIONS pkg_setup src_prepare src_configure src_install pkg_postinst