Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ jobs:
libexif-dev \
libgd-dev \
libpng-dev \
libjpeg-dev \
libtiff-dev \
libfreetype-dev \
libfreetype6 \
Expand Down
5 changes: 5 additions & 0 deletions src/Building/UnixBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ private function buildFromSource(
$io->write('<info>phpize complete</info>.');

$configureOptions = $this->withDetectedLibdirOption($configureOptions, $targetPlatform);
$configureOptions = BundledPhpExtensionsRepository::augmentConfigureOptionsForPhpBundledExtensions(
$configureOptions,
$downloadedPackage,
$targetPlatform,
);

$phpConfigPath = $targetPlatform->phpBinaryPath->phpConfigPath();
if ($phpConfigPath !== null) {
Expand Down
92 changes: 91 additions & 1 deletion src/ComposerIntegration/BundledPhpExtensionsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
use function count;
use function implode;
use function in_array;
use function Safe\preg_match;
use function Safe\realpath;
use function sprintf;
use function str_starts_with;

/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
class BundledPhpExtensionsRepository extends ArrayRepository
Expand All @@ -38,6 +40,7 @@ class BundledPhpExtensionsRepository extends ArrayRepository
* os-families?: non-empty-list<OperatingSystemFamily>,
* type?: ExtensionType,
* priority?: int,
* configure-options?: list<array{name: non-empty-string, description: string, needs-value: bool}>,
* }>
*/
private static array $bundledPhpExtensions = [
Expand Down Expand Up @@ -69,7 +72,37 @@ class BundledPhpExtensionsRepository extends ArrayRepository
'lib-ffi' => '*',
],
],
// ['name' => 'gd'], // build failure - ext/gd/gd.c:79:11: fatal error: ft2build.h: No such file or directory
[
'name' => 'gd',
'require' => ['php' => '>= 7.4.0'],
'configure-options' => [
[
'name' => 'with-jpeg',
'description' => 'Enable JPEG support in bundled GD',
'needs-value' => false,
],
[
'name' => 'with-freetype',
'description' => 'Enable FreeType support in bundled GD',
'needs-value' => false,
],
[
'name' => 'with-xpm',
'description' => 'Enable XPM support in bundled GD',
'needs-value' => false,
],
[
'name' => 'with-webp',
'description' => 'Enable WebP support in bundled GD',
'needs-value' => false,
],
[
'name' => 'with-avif',
'description' => 'Enable AVIF support in bundled GD',
'needs-value' => false,
],
],
],
['name' => 'gettext'],
['name' => 'gmp'],
['name' => 'iconv'],
Expand Down Expand Up @@ -259,6 +292,10 @@ static function (string $target, string $constraint) use ($extension, $versionPa
$phpExt['priority'] = $extension['priority'];
}

if (array_key_exists('configure-options', $extension)) {
$phpExt['configure-options'] = $extension['configure-options'];
}

$package->setPhpExt($phpExt);

return $package;
Expand All @@ -276,6 +313,59 @@ private static function findRe2c(): string
}
}

/**
* @param list<non-empty-string> $configureOptions
*
* @return list<non-empty-string>
*/
public static function augmentConfigureOptionsForPhpBundledExtensions(
array $configureOptions,
DownloadedPackage $downloadedPackage,
TargetPlatform $targetPlatform,
): array {
if ($downloadedPackage->package->name() !== 'php/gd') {
return $configureOptions;
}

$phpConfigHeader = $targetPlatform->phpBinaryPath->phpConfigHeader();
if ($phpConfigHeader === null) {
return $configureOptions;
}

$gdFeatureDefinesToConfigureOptions = [
'HAVE_GD_FREETYPE' => '--with-freetype',
'HAVE_GD_JPG' => '--with-jpeg',
'HAVE_GD_XPM' => '--with-xpm',
'HAVE_GD_WEBP' => '--with-webp',
'HAVE_GD_AVIF' => '--with-avif',
];

foreach ($gdFeatureDefinesToConfigureOptions as $define => $configureOption) {
if (
! preg_match('/^#define ' . $define . ' 1$/m', $phpConfigHeader)
|| self::hasConfigureOption($configureOptions, $configureOption)
) {
continue;
}

$configureOptions[] = $configureOption;
}

return $configureOptions;
}

/** @param list<non-empty-string> $configureOptions */
private static function hasConfigureOption(array $configureOptions, string $configureOption): bool
{
foreach ($configureOptions as $existingConfigureOption) {
if ($existingConfigureOption === $configureOption || str_starts_with($existingConfigureOption, $configureOption . '=')) {
return true;
}
}

return false;
}

/**
* @param list<string> $makeCommand
*
Expand Down
17 changes: 17 additions & 0 deletions src/Platform/TargetPhp/PhpBinaryPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use function is_executable;
use function ltrim;
use function rtrim;
use function Safe\file_get_contents;
use function Safe\mkdir;
use function Safe\preg_match;
use function Safe\preg_replace;
Expand Down Expand Up @@ -213,6 +214,22 @@ public function phpConfigLibdir(): string|null
return self::libdirFromConfigureOptions($configureOptions);
}

public function phpConfigHeader(): string|null
{
if ($this->phpConfigPath === null) {
return null;
}

$includeDir = self::cleanWarningAndDeprecationsFromOutput(Process::run([$this->phpConfigPath, '--include-dir']));

$phpConfigHeaderPath = $includeDir . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'php_config.h';
if ($includeDir === '' || ! file_exists($phpConfigHeaderPath)) {
return null;
}

return file_get_contents($phpConfigHeaderPath);
}

/** @return non-empty-string|null */
private static function libdirFromConfigureOptions(string $configureOptions): string|null
{
Expand Down
12 changes: 12 additions & 0 deletions test/end-to-end/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ FROM homebrew_base_with_pie AS test_pie_installs_extension_using_pcre_with_brew
RUN pie install --auto-install-build-tools -v apcu/apcu
RUN php -m | grep -i apcu

FROM homebrew_base_with_pie AS test_pie_installs_bundled_gd_against_php_with_gd_features_with_brew
RUN grep -x '#define HAVE_GD_FREETYPE 1' "$(php-config --include-dir)/main/php_config.h"
RUN pie install --auto-install-build-tools --force -v php/gd

FROM ubuntu_base AS test_pie_installs_system_deps_on_ubuntu
RUN apt-get update && apt install -y unzip curl wget gcc make autoconf libtool bison re2c pkg-config libzip-dev libssl-dev libonig-dev
RUN mkdir -p /opt/php \
Expand All @@ -80,6 +84,14 @@ RUN apk add php php-phar php-mbstring php-iconv php-openssl bzip2-dev libbz2 bui
COPY --from=build_pie_phar /app/pie.phar /usr/local/bin/pie
RUN pie install -v --auto-install-system-dependencies --force php/sodium

FROM alpine_base AS test_pie_installs_bundled_gd_against_php_with_gd_features_on_alpine
RUN apk add php php-phar php-mbstring php-iconv php-openssl build-base autoconf libtool pkgconf php85-dev \
freetype-dev libjpeg-turbo-dev libpng-dev libwebp-dev libavif-dev libxpm-dev
RUN grep -x '#define HAVE_GD_FREETYPE 1' "$(php-config --include-dir)/main/php_config.h"
COPY --from=build_pie_phar /app/pie.phar /usr/local/bin/pie
RUN pie install -v --force php/gd
RUN LD_BIND_NOW=1 php -m | grep -x gd

FROM fedora_base AS test_pie_installs_system_deps_on_fedora
RUN dnf install -y php php-pecl-zip unzip gcc make autoconf bison re2c libtool php-devel
COPY --from=build_pie_phar /app/pie.phar /usr/local/bin/pie
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Composer\Package\CompletePackageInterface;
use Php\Pie\ComposerIntegration\BundledPhpExtensionsRepository;
use Php\Pie\ConfigureOption;
use Php\Pie\DependencyResolver\Package;
use Php\Pie\Downloading\DownloadedPackage;
use Php\Pie\ExtensionName;
Expand All @@ -22,8 +23,10 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Exception\ProcessFailedException;

use function array_map;
use function Safe\mkdir;
use function Safe\realpath;
use function substr;
use function sys_get_temp_dir;
use function uniqid;

Expand All @@ -46,6 +49,7 @@ public static function bundledRepositoryPackageNames(): array
['php/enchant'],
['php/exif'],
['php/ffi'],
['php/gd'],
['php/gettext'],
['php/gmp'],
['php/iconv'],
Expand Down Expand Up @@ -111,6 +115,38 @@ public function testBundledRepository(string $packageName): void
self::assertSame('8.1.0', $package->getPrettyVersion());
}

public function testGdExposesOptionalFeatureFlags(): void
{
$phpBinary = $this->createMock(PhpBinaryPath::class);
$phpBinary->method('version')->willReturn('8.1.0');

$repository = BundledPhpExtensionsRepository::forTargetPlatform(
new TargetPlatform(
OperatingSystem::NonWindows,
OperatingSystemFamily::Linux,
$phpBinary,
Architecture::x86_64,
ThreadSafetyMode::NonThreadSafe,
1,
null,
null,
),
);

$composerPackage = $repository->findPackage('php/gd', '8.1.0');
self::assertInstanceOf(CompletePackageInterface::class, $composerPackage);

$package = Package::fromComposerCompletePackage($composerPackage);

self::assertSame(
[['with-jpeg', false], ['with-freetype', false], ['with-xpm', false], ['with-webp', false], ['with-avif', false]],
array_map(
static fn (ConfigureOption $option): array => [$option->name, $option->needsValue],
$package->configureOptions(),
),
);
}

public function testMakeCommandForXmlReader(): void
{
$phpPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('pie_test_bundled_', true);
Expand Down Expand Up @@ -163,6 +199,67 @@ public function testMakeCommandForDom(): void
);
}

/** @return array<non-empty-string, array{non-empty-string, string|null, list<non-empty-string>, list<non-empty-string>}> */
public static function configureOptionsForPhpConfigHeader(): array
{
$allGdFeatures = "#define HAVE_GD_AVIF 1\n#define HAVE_GD_FREETYPE 1\n#define HAVE_GD_JPG 1\n#define HAVE_GD_PNG 1\n#define HAVE_GD_WEBP 1\n#define HAVE_GD_XPM 1\n";

return [
'gd with all features' => ['php/gd', $allGdFeatures, [], ['--with-freetype', '--with-jpeg', '--with-xpm', '--with-webp', '--with-avif']],
'gd with only freetype' => ['php/gd', "#define HAVE_GD_FREETYPE 1\n", [], ['--with-freetype']],
'gd with undefined freetype' => ['php/gd', "/* #undef HAVE_GD_FREETYPE */\n", [], []],
'gd keeps existing options first' => ['php/gd', "#define HAVE_GD_FREETYPE 1\n", ['--with-libdir=lib64'], ['--with-libdir=lib64', '--with-freetype']],
'gd does not duplicate user flag' => ['php/gd', "#define HAVE_GD_FREETYPE 1\n#define HAVE_GD_JPG 1\n", ['--with-freetype'], ['--with-freetype', '--with-jpeg']],
'gd does not override user flag value' => ['php/gd', "#define HAVE_GD_JPG 1\n", ['--with-jpeg=/opt/jpeg'], ['--with-jpeg=/opt/jpeg']],
'gd without php_config.h' => ['php/gd', null, [], []],
'other bundled extension' => ['php/exif', $allGdFeatures, [], []],
];
}

/**
* @param non-empty-string $packageName
* @param list<non-empty-string> $configureOptions
* @param list<non-empty-string> $expectedConfigureOptions
*/
#[DataProvider('configureOptionsForPhpConfigHeader')]
public function testConfigureOptionsAreDetectedFromPhpConfigHeader(
string $packageName,
string|null $phpConfigHeader,
array $configureOptions,
array $expectedConfigureOptions,
): void {
$phpBinary = $this->createMock(PhpBinaryPath::class);
$phpBinary->method('phpConfigHeader')->willReturn($phpConfigHeader);

self::assertSame(
$expectedConfigureOptions,
BundledPhpExtensionsRepository::augmentConfigureOptionsForPhpBundledExtensions(
$configureOptions,
DownloadedPackage::fromPackageAndExtractedPath(
new Package(
$this->createMock(CompletePackageInterface::class),
ExtensionType::PhpModule,
ExtensionName::normaliseFromString(substr($packageName, 4)),
$packageName,
'1.2.3',
null,
),
'/path/to/ext',
),
new TargetPlatform(
OperatingSystem::NonWindows,
OperatingSystemFamily::Linux,
$phpBinary,
Architecture::x86_64,
ThreadSafetyMode::NonThreadSafe,
1,
null,
null,
),
),
);
}

/** @return list<array{0: non-empty-string}> */
public static function dependantsOnRe2c(): array
{
Expand Down
41 changes: 41 additions & 0 deletions test/unit/Platform/TargetPhp/PhpBinaryPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,47 @@ public function testPhpConfigLibdirFromConfigureOptions(string $configureOptions
unlink($tmpSh);
}

public function testPhpConfigHeaderIsReadFromPhpConfigIncludeDir(): void
{
if (Platform::isWindows()) {
self::markTestSkipped('Bash script does not run on Windows.');
}

$includeDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('pie_php_config_header_test', true);
mkdir($includeDir . DIRECTORY_SEPARATOR . 'main', recursive: true);
file_put_contents($includeDir . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'php_config.h', "#define HAVE_GD_FREETYPE 1\n");

$tmpSh = tempnam(sys_get_temp_dir(), uniqid('pie_php_config_header_test'));
file_put_contents($tmpSh, "#!/usr/bin/env bash\necho \"" . $includeDir . "\";\n");
chmod($tmpSh, 0777);

$phpBinary = $this->createPartialMock(PhpBinaryPath::class, []);
(new ReflectionMethod($phpBinary, '__construct'))->invoke($phpBinary, $tmpSh, $tmpSh, PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION, PHP_VERSION);

self::assertSame("#define HAVE_GD_FREETYPE 1\n", $phpBinary->phpConfigHeader());
unlink($tmpSh);
}

public function testPhpConfigHeaderIsNullWhenHeaderDoesNotExist(): void
{
if (Platform::isWindows()) {
self::markTestSkipped('Bash script does not run on Windows.');
}

$includeDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('pie_php_config_header_test', true);
mkdir($includeDir, recursive: true);

$tmpSh = tempnam(sys_get_temp_dir(), uniqid('pie_php_config_header_test'));
file_put_contents($tmpSh, "#!/usr/bin/env bash\necho \"" . $includeDir . "\";\n");
chmod($tmpSh, 0777);

$phpBinary = $this->createPartialMock(PhpBinaryPath::class, []);
(new ReflectionMethod($phpBinary, '__construct'))->invoke($phpBinary, $tmpSh, $tmpSh, PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION, PHP_VERSION);

self::assertNull($phpBinary->phpConfigHeader());
unlink($tmpSh);
}

public function testVersionDetailsAreReadCorrectlyWhenSurroundedByWarningsAndDeprecations(): void
{
if (Platform::isWindows()) {
Expand Down
Loading