diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index b68467b9..76f3ee0c 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -141,6 +141,7 @@ jobs: libexif-dev \ libgd-dev \ libpng-dev \ + libjpeg-dev \ libtiff-dev \ libfreetype-dev \ libfreetype6 \ diff --git a/src/Building/UnixBuild.php b/src/Building/UnixBuild.php index 1d19371f..32620247 100644 --- a/src/Building/UnixBuild.php +++ b/src/Building/UnixBuild.php @@ -100,6 +100,11 @@ private function buildFromSource( $io->write('phpize complete.'); $configureOptions = $this->withDetectedLibdirOption($configureOptions, $targetPlatform); + $configureOptions = BundledPhpExtensionsRepository::augmentConfigureOptionsForPhpBundledExtensions( + $configureOptions, + $downloadedPackage, + $targetPlatform, + ); $phpConfigPath = $targetPlatform->phpBinaryPath->phpConfigPath(); if ($phpConfigPath !== null) { diff --git a/src/ComposerIntegration/BundledPhpExtensionsRepository.php b/src/ComposerIntegration/BundledPhpExtensionsRepository.php index f285b90a..0e3b3d2f 100644 --- a/src/ComposerIntegration/BundledPhpExtensionsRepository.php +++ b/src/ComposerIntegration/BundledPhpExtensionsRepository.php @@ -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 @@ -38,6 +40,7 @@ class BundledPhpExtensionsRepository extends ArrayRepository * os-families?: non-empty-list, * type?: ExtensionType, * priority?: int, + * configure-options?: list, * }> */ private static array $bundledPhpExtensions = [ @@ -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'], @@ -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; @@ -276,6 +313,59 @@ private static function findRe2c(): string } } + /** + * @param list $configureOptions + * + * @return list + */ + 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 $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 $makeCommand * diff --git a/src/Platform/TargetPhp/PhpBinaryPath.php b/src/Platform/TargetPhp/PhpBinaryPath.php index 644ade68..f2e6273f 100644 --- a/src/Platform/TargetPhp/PhpBinaryPath.php +++ b/src/Platform/TargetPhp/PhpBinaryPath.php @@ -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; @@ -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 { diff --git a/test/end-to-end/Dockerfile b/test/end-to-end/Dockerfile index ad8c1b0e..ea3df962 100644 --- a/test/end-to-end/Dockerfile +++ b/test/end-to-end/Dockerfile @@ -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 \ @@ -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 diff --git a/test/unit/ComposerIntegration/BundledPhpExtensionsRepositoryTest.php b/test/unit/ComposerIntegration/BundledPhpExtensionsRepositoryTest.php index e66b926f..b38d7472 100644 --- a/test/unit/ComposerIntegration/BundledPhpExtensionsRepositoryTest.php +++ b/test/unit/ComposerIntegration/BundledPhpExtensionsRepositoryTest.php @@ -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; @@ -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; @@ -46,6 +49,7 @@ public static function bundledRepositoryPackageNames(): array ['php/enchant'], ['php/exif'], ['php/ffi'], + ['php/gd'], ['php/gettext'], ['php/gmp'], ['php/iconv'], @@ -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); @@ -163,6 +199,67 @@ public function testMakeCommandForDom(): void ); } + /** @return array, list}> */ + 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 $configureOptions + * @param list $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 */ public static function dependantsOnRe2c(): array { diff --git a/test/unit/Platform/TargetPhp/PhpBinaryPathTest.php b/test/unit/Platform/TargetPhp/PhpBinaryPathTest.php index 8b7fcbc2..999de270 100644 --- a/test/unit/Platform/TargetPhp/PhpBinaryPathTest.php +++ b/test/unit/Platform/TargetPhp/PhpBinaryPathTest.php @@ -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()) {