From Fedora Project Wiki

m (Packaging/PHP moved to Packaging:PHP: Moving Packaging Pages to Packaging Namespace)
(19 intermediate revisions by 3 users not shown)
Line 1: Line 1:
= Guidelines for packaging PHP addon modules =
= Guidelines for packaging PHP addon modules =




== Different kinds of packages ==
== Different kinds of packages ==


There are basically two different kinds of php modules, which are packaged for Fedora Extras:
There are basically 4 different kinds of php modules, which are packaged for Fedora:


* [http://pecl.php.net PECL]  (PHP Extention Community Library), which are PHP modules usually written in C, which are dynamically loaded by the PHP interpreter on startup.
* [http://pecl.php.net PECL]  (PHP Extention Community Library), which are PHP modules usually written in C, which are dynamically loaded by the PHP interpreter on startup.
* [http://pear.php.net PEAR]  (PHP Extension and Application Repository), which are reusable components written in PHP, usually classes, which can be used in your own PHP applications and scripts by using e.g. the include() directive.
* [http://pear.php.net PEAR]  (PHP Extension and Application Repository), which are reusable components written in PHP, usually classes, which can be used in your own PHP applications and scripts by using e.g. the include() directive.
* CHANNEL : a package which register a channel. A channel is a repository which provides php extensions
* Other package providing php extension not handled by pear/pecl mechanisms


While upstream used the same package and distribution format for both, creating RPMs has to take some differences into account.
While upstream used the same package and distribution format for PECL and PEAR, creating RPMs has to take some differences into account.
 
3 channels are defined on installation of php-pear
* <code>pear.php.net</code> (alias pear) : the default channel for PHP Extension and Application Repository
* <code>pecl.php.net</code> (alias pecl) : the default channel for PHP Extension Community Library
* <code>__uri</code> : Pseudo-channel for static packages
 
Other channels must be configured at RPM build time and at at RPM installation time.


{{Anchor|NamingScheme}}
{{Anchor|NamingScheme}}
== Naming scheme ==
== Naming scheme ==


* PECL packages should be named ''php-pecl-PECLPackageName-%{version}-%{release}.%{arch}.rpm''.
* PECL packages from standard pecl channel should be named php-pecl-PECLPackageName-%{version}-%{release}.%{arch}.rpm.
* PEAR packages should be named ''php-pear-PEARPackageName-%{version}-%{release}.noarch.rpm''.
* PEAR packages from standard pear channel should be named php-pear-PEARPackageName-%{version}-%{release}.noarch.rpm.
* CHANNEL packages should be named php-channel-ChannelAlias-%{version}-%{release}.noarch.rpm
* Packages from another channel  should be named php-ChannelAlias-PackageName-%{version}-%{release}.noarch.rpm.
* Other packages should be named ''php-PackageName-%{version}-%{release}.%{arch}.rpm''; %{arch} can be "noarch" where appropriate.
* Other packages should be named ''php-PackageName-%{version}-%{release}.%{arch}.rpm''; %{arch} can be "noarch" where appropriate.


Line 28: Line 39:
== File Placement ==
== File Placement ==


Non-PEAR PHP extensions should put their Class files in /usr/share/php.
Non-PEAR PHP software which provides shared libraries should put its PHP source files for such shared libraries in a subfolder of /usr/share/php, named according to the name of the software. For example, a library called "Whizz_Bang" (with a RPM called php-something-Whizz-Bang) would put the PHP source files for its shared libraries in /usr/share/php/Whizz_Bang.
 
A PSR-0 <ref>[https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md PSR-0]</ref>
compliant library would put its PHP files in /usr/share/php/<Vendor Name>
 
PEAR documentation provided by upstream are installed in %{pear_docdir}, should stay there, and must be marked as %doc.


== Requires and Provides ==
== Requires and Provides ==


=== PEAR Packages ===
=== PEAR Packages from the standard channel/repository ===


A PEAR package '''MUST''' have:
A PEAR package '''MUST''' have:
Line 43: Line 59:
Provides:    php-pear(foo) = %{version}
Provides:    php-pear(foo) = %{version}
</pre>
</pre>
=== Packages for CHANNEL (repository) configuration  ===
A CHANNEL package ''''MUST''' have :
<pre>
Requires: php-pear(PEAR)
Requires(post): %{__pear}
Requires(postun): %{__pear}
Provides: php-channel(channelname)
</pre>
=== PEAR Packages from a non standard channel/repository ===
A PEAR package '''MUST''' have:
<pre>
BuildRequires: php-channel(channelname)
BuildRequires: php-pear(PEAR)
Requires: php-pear(PEAR)
Requires(post): %{__pear}
Requires(postun): %{__pear}
Requires: php-channel(channelname)
Provides:    php-pear(channelname/foo) = %{version}
</pre>
=== C extensions (PECL and others) ===
To be certain that a binary extension will run correctly with a particular version of PHP, it is necessary to check that a particular package has both API and ABIs matching the installed version of PHP. The mechanism for doing this has evolved over time and is as follows:
For '''Fedora''' (all current versions):
<pre>
BuildRequires: php-devel
Requires:      php(zend-abi) = %{php_zend_api}
Requires:      php(api) = %{php_core_api}
</pre>
{{admon/important|Packaging note|Details on what to do for EPEL branches EL-4 and EL-5 can be found here: [[Packaging:EPEL#PHP_ABI_Check_Handling]]}}


=== PECL Packages ===
=== PECL Packages ===


A PECL package '''MUST''' have:
PECL extension '''MUST''' have ABI check (see previous)
 
A PECL package '''MUST''' also have:


<pre>
<pre>
BuildRequires: php-devel, php-pear
BuildRequires: php-pear
Requires(post): %{__pecl}
Requires(post): %{__pecl}
Requires(postun): %{__pecl}
Requires(postun): %{__pecl}


%if %{?php_zend_api}0
Provides:    php-pecl(foo) = %{version}
Requires:    php(zend-abi) = %{php_zend_api}
Provides:    php-pecl(foo)%{?_isa} = %{version}
Requires:    php(api) = %{php_core_api}
</pre>
%else
Requires:    php-api = %{php_apiver}
%endif


Provides:     php-pecl(foo) = %{version}
=== PECL Packages from a non standard channel/repository ===
 
A PECL package from a non standard channel MUST have (instead of previous provides)
 
<pre>
Requires: php-channel(channelname)
Provides: php-pecl(channelname/foo) = %{version}
Provides: php-pecl(channelname/foo)%{?_isa} = %{version}
</pre>
</pre>


=== Other Packages ===
=== Other Packages ===


PHP addons which are neither PEAR nor PECL should require what makes sense (either a base PHP version or a php-api as necessary).
PHP addons which are neither PEAR nor PECL should require what makes sense (either a base PHP version or a php-api, php(zend-abi) as necessary).
 
=== Apache requirement ===
 
A PHP library must not have an explicit Requires on php or httpd, since these libraries could be used with any webserver or any SAPI (php-cli, php-cgi, php-fpm, ...).
 
Only a PHP web application, which provides a specific Apache httpd configuration, should have a Requires on httpd and mod_php.
 
=== Extensions Requires ===
 
PHP extensions must have a Requires on all of the dependent extensions (php-date, php-gd, php-mbstring, ...). These extensions are virtual Provides of the php sub-packages.
 
=== Requiring a Minimum PHP version ===
 
If you need to specify a minimum PHP version, the recommended method is to add a Requires: php(language) >= $VERSION (where $VERSION is the minimum PHP version). This works for all released versions of Fedora and RHEL/EPEL-6, but does NOT work for RHEL/EPEL-5. For RHEL/EPEL-5 packages, you will need to use Requires: php-common >= $VERSION.


== Macros and scriptlets ==
== Macros and scriptlets ==
=== PHP ZTS extension ===
When the Apache HTTPD is run in worker mode (instead of prefork mode), the ZTS (Zend Thread Safe) version of PHP is used.
If an extension maintainer wants to provide a ZTS version of this extension, the maintainer must ensure that:
* the extension is thread safe
* the libraries used by the extension are thread safe
The php-devel package in fedora >= 17 (5.4.0) provides the necessary files to build ZTS modules and provides several new helper macros:
For standard (NTS) extensions
<pre>
%{__php}          %{_bindir}/php
%{php_extdir}    %{_libdir}/php/modules
%{php_inidir}    %{_sysconfdir}/php.d
%{php_incldir    %{_includedir}/php
</pre>
For ZTS extensions
<pre>
%{__ztsphp}      %{_bindir}/zts-php
%{php_ztsextdir}  %{_libdir}/php-zts/modules
%{php_ztsinidir}  %{_sysconfdir}/php-zts.d
%{php_ztsincldir  %{_includedir}/php-zts/php
</pre>
php-devel provides the executables needed during the build of a ZTS extension, which are:
* zts-phpize
* zts-php-config
* zts-php (which is only useful to run the test suite during build)
=== Packages for CHANNEL (repository) configuration  ===
Here are some recommended scriptlets for properly registering and unregistering the channel:
<pre>
%post
if [ $1 -eq  1 ] ; then
  %{__pear} channel-add %{pear_xmldir}/%{name}.xml > /dev/null || :
else
  %{__pear} channel-update %{pear_xmldir}/%{name}.xml > /dev/null ||:
fi
%postun
if [ $1 -eq 0 ] ; then
  %{__pear} channel-delete %{channelname} > /dev/null || :
fi
</pre>


=== PEAR Modules ===
=== PEAR Modules ===


The php-pear package in Fedora Core 5 and above (version 1:1.4.9-1.2) provides several useful macros:
The php-pear package provides several useful macros:
* %{pear_phpdir}
* %{pear_phpdir}
* %{pear_docdir}
* %{pear_docdir} (This evaluates to %{_docdir}/pear.)
* %{pear_testdir}
* %{pear_testdir}
* %{pear_datadir}
* %{pear_datadir}
* %{pear_xmldir}
* %{pear_xmldir}
* %{pear_metadir} (This evaluates to %{pear_phpdir}, except in Fedora 19+, where it evaluates to /var/lib/pear.)


These defintions for the .spec should be of interest:
These definitions for the .spec should be of interest:
<pre>
<pre>
BuildRequires:    php-pear >= 1:1.4.9-1.2
BuildRequires:    php-pear >= 1:1.4.9-1.2
Provides:        php-pear(PackageName) = %{version}
Provides:        php-pear(PackageName) = %{version}
Requires:        php >= 4.3, php-pear(PEAR)
Requires:        php-common >= 4.3, php-pear(PEAR)
Requires(post):  %{_bindir}/pear
Requires(post):  %{_bindir}/pear
Requires(postun): %{_bindir}/pear
Requires(postun): %{_bindir}/pear
</pre>
</pre>


And here are some recommended scriptlets for properly registering and unregistering the module:
Be sure you delete any PEAR metadata files at the end of %install:
<pre>
rm -rf %{buildroot}/%{pear_metadir}/.??*
</pre>
 
{{admon/important|Packaging note|The %{pear_metadir} macro is not present on EPEL, please see: [[EPEL:Packaging#PHP_PEAR_Macros]]}}
 
Here are some recommended scriptlets for properly registering the module:
<pre>
<pre>
%post
%post
%{_bindir}/pear install --nodeps --soft --force --register-only %{pear_xmldir}/Foo_Bar.xml >/dev/null ||:
%{_bindir}/pear install --nodeps --soft --force --register-only %{pear_xmldir}/%{name}.xml >/dev/null ||:
</pre>


And here are some recommended scriptlets for properly unregistering the module, from the standard channel:
<pre>
%postun
%postun
if [ "$1" -eq "0" ] ; then
if [ "$1" -eq "0" ] ; then
%{_bindir}/pear uninstall --nodeps --ignore-errors --register-only Foo_Bar >/dev/null ||:
%{_bindir}/pear uninstall --nodeps --ignore-errors --register-only Foo_Bar >/dev/null ||:
fi
</pre>
From a non standard channel (pear command requires the channel):
<pre>
%postun
if [ "$1" -eq "0" ] ; then
%{_bindir}/pear uninstall --nodeps --ignore-errors --register-only Foo_channel/Foo_Bar >/dev/null ||:
fi
fi
</pre>
</pre>
Line 116: Line 255:
And here are some recommended scriptlets for properly registering and unregistering the module:
And here are some recommended scriptlets for properly registering and unregistering the module:
<pre>
<pre>
%if 0%{?pecl_install:1}
%post
%post
%{pecl_install} %{pecl_xmldir}/%{name}.xml >/dev/null || :
%{pecl_install} %{pecl_xmldir}/%{name}.xml >/dev/null || :
%endif




%if 0%{?pecl_uninstall:1}
%postun
%postun
if [ $1 -eq 0 ]  ; then
if [ $1 -eq 0 ]  ; then
%{pecl_uninstall} %{pecl_name} >/dev/null || :
%{pecl_uninstall} %{pecl_name} >/dev/null || :
fi
fi
%endif
</pre>
</pre>
{{admon/important|Packaging note|These scriptlets are not correct on EPEL, please see: [[Packaging:EPEL#PHP_PECL_Module_Scriptlets]]}}


=== Other Modules ===
=== Other Modules ===
Line 154: Line 291:


<pre>
<pre>
fedora-newrpmspec -t php-pear php-pear-Foo
rpmdev-newspec -t php-pear php-pear-Foo
</pre>
</pre>


Line 162: Line 299:
pear make-rpm-spec Foo.tgz
pear make-rpm-spec Foo.tgz
</pre>
</pre>
== References to the Fedora PHP Packaging Guidelines ==
<references/>
[[Category:PHP]]
[[Category:Packaging guidelines]]

Revision as of 06:04, 18 October 2013

Guidelines for packaging PHP addon modules

Different kinds of packages

There are basically 4 different kinds of php modules, which are packaged for Fedora:

  • PECL (PHP Extention Community Library), which are PHP modules usually written in C, which are dynamically loaded by the PHP interpreter on startup.
  • PEAR (PHP Extension and Application Repository), which are reusable components written in PHP, usually classes, which can be used in your own PHP applications and scripts by using e.g. the include() directive.
  • CHANNEL : a package which register a channel. A channel is a repository which provides php extensions
  • Other package providing php extension not handled by pear/pecl mechanisms

While upstream used the same package and distribution format for PECL and PEAR, creating RPMs has to take some differences into account.

3 channels are defined on installation of php-pear

  • pear.php.net (alias pear) : the default channel for PHP Extension and Application Repository
  • pecl.php.net (alias pecl) : the default channel for PHP Extension Community Library
  • __uri : Pseudo-channel for static packages

Other channels must be configured at RPM build time and at at RPM installation time.

Naming scheme

  • PECL packages from standard pecl channel should be named php-pecl-PECLPackageName-%{version}-%{release}.%{arch}.rpm.
  • PEAR packages from standard pear channel should be named php-pear-PEARPackageName-%{version}-%{release}.noarch.rpm.
  • CHANNEL packages should be named php-channel-ChannelAlias-%{version}-%{release}.noarch.rpm
  • Packages from another channel should be named php-ChannelAlias-PackageName-%{version}-%{release}.noarch.rpm.
  • Other packages should be named php-PackageName-%{version}-%{release}.%{arch}.rpm; %{arch} can be "noarch" where appropriate.

Please make sure that the PEAR package is correctly being built for noarch.

The PECLPackageName and the PEARPackageName should be consistent with the upstream naming scheme. The Crack PHP Extension would thus be named php-pecl-crack with the resulting packages being php-pecl-crack-0.4-1.i386.rpm and php-pecl-crack-0.4-1.src.rpm.

Note that web applications that happen to be written in PHP do not belong under the php-* namespace.

File Placement

Non-PEAR PHP software which provides shared libraries should put its PHP source files for such shared libraries in a subfolder of /usr/share/php, named according to the name of the software. For example, a library called "Whizz_Bang" (with a RPM called php-something-Whizz-Bang) would put the PHP source files for its shared libraries in /usr/share/php/Whizz_Bang.

A PSR-0 [1] compliant library would put its PHP files in /usr/share/php/<Vendor Name>

PEAR documentation provided by upstream are installed in %{pear_docdir}, should stay there, and must be marked as %doc.

Requires and Provides

PEAR Packages from the standard channel/repository

A PEAR package MUST have:

BuildRequires: php-pear(PEAR)
Requires: php-pear(PEAR)
Requires(post): %{__pear}
Requires(postun): %{__pear}
Provides:     php-pear(foo) = %{version}

Packages for CHANNEL (repository) configuration

A CHANNEL package 'MUST have :

Requires: php-pear(PEAR)
Requires(post): %{__pear}
Requires(postun): %{__pear}
Provides: php-channel(channelname)

PEAR Packages from a non standard channel/repository

A PEAR package MUST have:

BuildRequires: php-channel(channelname)
BuildRequires: php-pear(PEAR)
Requires: php-pear(PEAR)
Requires(post): %{__pear}
Requires(postun): %{__pear}
Requires: php-channel(channelname)
Provides:     php-pear(channelname/foo) = %{version}

C extensions (PECL and others)

To be certain that a binary extension will run correctly with a particular version of PHP, it is necessary to check that a particular package has both API and ABIs matching the installed version of PHP. The mechanism for doing this has evolved over time and is as follows:

For Fedora (all current versions):

BuildRequires: php-devel
Requires:      php(zend-abi) = %{php_zend_api}
Requires:      php(api) = %{php_core_api}
Important.png
Packaging note
Details on what to do for EPEL branches EL-4 and EL-5 can be found here: Packaging:EPEL#PHP_ABI_Check_Handling

PECL Packages

PECL extension MUST have ABI check (see previous)

A PECL package MUST also have:

BuildRequires: php-pear
Requires(post): %{__pecl}
Requires(postun): %{__pecl}

Provides:     php-pecl(foo) = %{version}
Provides:     php-pecl(foo)%{?_isa}  = %{version}

PECL Packages from a non standard channel/repository

A PECL package from a non standard channel MUST have (instead of previous provides)

Requires: php-channel(channelname)
Provides: php-pecl(channelname/foo) = %{version}
Provides: php-pecl(channelname/foo)%{?_isa} = %{version}

Other Packages

PHP addons which are neither PEAR nor PECL should require what makes sense (either a base PHP version or a php-api, php(zend-abi) as necessary).

Apache requirement

A PHP library must not have an explicit Requires on php or httpd, since these libraries could be used with any webserver or any SAPI (php-cli, php-cgi, php-fpm, ...).

Only a PHP web application, which provides a specific Apache httpd configuration, should have a Requires on httpd and mod_php.

Extensions Requires

PHP extensions must have a Requires on all of the dependent extensions (php-date, php-gd, php-mbstring, ...). These extensions are virtual Provides of the php sub-packages.

Requiring a Minimum PHP version

If you need to specify a minimum PHP version, the recommended method is to add a Requires: php(language) >= $VERSION (where $VERSION is the minimum PHP version). This works for all released versions of Fedora and RHEL/EPEL-6, but does NOT work for RHEL/EPEL-5. For RHEL/EPEL-5 packages, you will need to use Requires: php-common >= $VERSION.

Macros and scriptlets

PHP ZTS extension

When the Apache HTTPD is run in worker mode (instead of prefork mode), the ZTS (Zend Thread Safe) version of PHP is used.

If an extension maintainer wants to provide a ZTS version of this extension, the maintainer must ensure that:

  • the extension is thread safe
  • the libraries used by the extension are thread safe

The php-devel package in fedora >= 17 (5.4.0) provides the necessary files to build ZTS modules and provides several new helper macros:

For standard (NTS) extensions

%{__php}          %{_bindir}/php
%{php_extdir}     %{_libdir}/php/modules
%{php_inidir}     %{_sysconfdir}/php.d
%{php_incldir     %{_includedir}/php

For ZTS extensions

%{__ztsphp}       %{_bindir}/zts-php
%{php_ztsextdir}  %{_libdir}/php-zts/modules
%{php_ztsinidir}  %{_sysconfdir}/php-zts.d
%{php_ztsincldir  %{_includedir}/php-zts/php

php-devel provides the executables needed during the build of a ZTS extension, which are:

  • zts-phpize
  • zts-php-config
  • zts-php (which is only useful to run the test suite during build)

Packages for CHANNEL (repository) configuration

Here are some recommended scriptlets for properly registering and unregistering the channel:

%post
if [ $1 -eq  1 ] ; then
   %{__pear} channel-add %{pear_xmldir}/%{name}.xml > /dev/null || :
else
   %{__pear} channel-update %{pear_xmldir}/%{name}.xml > /dev/null ||:
fi

%postun
if [ $1 -eq 0 ] ; then
   %{__pear} channel-delete %{channelname} > /dev/null || :
fi

PEAR Modules

The php-pear package provides several useful macros:

  • %{pear_phpdir}
  • %{pear_docdir} (This evaluates to %{_docdir}/pear.)
  • %{pear_testdir}
  • %{pear_datadir}
  • %{pear_xmldir}
  • %{pear_metadir} (This evaluates to %{pear_phpdir}, except in Fedora 19+, where it evaluates to /var/lib/pear.)

These definitions for the .spec should be of interest:

BuildRequires:    php-pear >= 1:1.4.9-1.2
Provides:         php-pear(PackageName) = %{version}
Requires:         php-common >= 4.3, php-pear(PEAR)
Requires(post):   %{_bindir}/pear
Requires(postun): %{_bindir}/pear

Be sure you delete any PEAR metadata files at the end of %install:

rm -rf %{buildroot}/%{pear_metadir}/.??* 
Important.png
Packaging note
The %{pear_metadir} macro is not present on EPEL, please see: EPEL:Packaging#PHP_PEAR_Macros

Here are some recommended scriptlets for properly registering the module:

%post
%{_bindir}/pear install --nodeps --soft --force --register-only %{pear_xmldir}/%{name}.xml >/dev/null ||:

And here are some recommended scriptlets for properly unregistering the module, from the standard channel:

%postun
if [ "$1" -eq "0" ] ; then
%{_bindir}/pear uninstall --nodeps --ignore-errors --register-only Foo_Bar >/dev/null ||:
fi

From a non standard channel (pear command requires the channel):

%postun
if [ "$1" -eq "0" ] ; then
%{_bindir}/pear uninstall --nodeps --ignore-errors --register-only Foo_channel/Foo_Bar >/dev/null ||:
fi

PECL Modules

The php-pear package in Fedora Core 5 and above (version 1:1.4.9-1.2) provides several useful macros:

  • %{pecl_phpdir}
  • %{pecl_docdir}
  • %{pecl_testdir}
  • %{pecl_datadir}
  • %{pecl_xmldir}

You may need to define a few additional macros to extract some information from PHP. It is recommended that you use the following:

%global php_apiver  %((echo 0; php -i 2>/dev/null | sed -n 's/^PHP API => //p') | tail -1)
%{!?__pecl:     %{expand: %%global __pecl     %{_bindir}/pecl}}
%{!?php_extdir: %{expand: %%global php_extdir %(php-config --extension-dir)}}

And here are some recommended scriptlets for properly registering and unregistering the module:

%post
%{pecl_install} %{pecl_xmldir}/%{name}.xml >/dev/null || :


%postun
if [ $1 -eq 0 ]  ; then
%{pecl_uninstall} %{pecl_name} >/dev/null || :
fi
Important.png
Packaging note
These scriptlets are not correct on EPEL, please see: Packaging:EPEL#PHP_PECL_Module_Scriptlets

Other Modules

If your module includes compiled code, you may need to define some macros to extract some information from PHP. It is recommended that you user the following:

%global php_apiver  %((echo 0; php -i 2>/dev/null | sed -n 's/^PHP API => //p') | tail -1)
%global php_extdir  %(php-config --extension-dir 2>/dev/null || echo "undefined")
%global php_version %(php-config --version 2>/dev/null || echo 0)

Additional Hints for Packagers

PEAR & PECL Packages

The source archive contains a package.xml outside any directory, so you have to use use

%setup -q -c

in your %prep section to avoid writing files to the build root.

PEAR Packages

To create your initial specfile, you can use the default template provided by the rpmdevtools package:

rpmdev-newspec -t php-pear php-pear-Foo

Or you can generate one; make sure you have the php-pear-PEAR-Command-Packaging package installed:

pear make-rpm-spec Foo.tgz

References to the Fedora PHP Packaging Guidelines