Skip to content
Open
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
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ parameters:
-
message: '#^Call to static method PHPUnit\\Framework\\Assert\:\:assertNotEmpty\(\) with Php\\Pie\\File\\BinaryFile will always evaluate to true\.$#'
identifier: staticMethod.alreadyNarrowedType
count: 2
count: 1
path: test/integration/Building/UnixBuildTest.php

-
Expand Down
35 changes: 28 additions & 7 deletions src/Building/UnixBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use function count;
use function file_exists;
use function implode;
use function max;
use function Safe\rename;
use function sprintf;
use function str_starts_with;
Expand Down Expand Up @@ -80,6 +81,14 @@ private function buildFromSource(
): BinaryFile {
$outputCallback = Process::outputCallbackForVerbosity($io, IOInterface::VERBOSE);

$sourceDateEpoch = Platform::getEnv('SOURCE_DATE_EPOCH');
if ($sourceDateEpoch === false) {
$releaseDate = $downloadedPackage->package->composerPackage()->getReleaseDate();
$sourceDateEpoch = (string) max(0, $releaseDate?->getTimestamp() ?? 0);
}

$buildEnv = ['SOURCE_DATE_EPOCH' => $sourceDateEpoch];

$phpizePath = $targetPlatform->phpizePath ?? PhpizePath::guessFrom($targetPlatform->phpBinaryPath);

/**
Expand All @@ -95,6 +104,7 @@ private function buildFromSource(
$downloadedPackage,
$io,
$outputCallback,
$buildEnv,
);

$io->write('<info>phpize complete</info>.');
Expand All @@ -111,13 +121,13 @@ private function buildFromSource(
$configureOptions[] = '--with-php-config=' . $phpConfigPath;
}

$this->configure($downloadedPackage, $targetPlatform, $configureOptions, $io, $outputCallback);
$this->configure($downloadedPackage, $targetPlatform, $configureOptions, $io, $outputCallback, $buildEnv);

$optionsOutput = count($configureOptions) ? ' with options: ' . implode(' ', $configureOptions) : '.';
$io->write('<info>Configure complete</info>' . $optionsOutput);

try {
$this->make($targetPlatform, $downloadedPackage, $io, $outputCallback);
$this->make($targetPlatform, $downloadedPackage, $io, $outputCallback, $buildEnv);
} catch (ProcessFailedException $p) {
throw ProcessFailedWithLimitedOutput::fromProcessFailedException($p);
}
Expand Down Expand Up @@ -186,12 +196,16 @@ private function renamesToConfigM4(DownloadedPackage $downloadedPackage, IOInter
}
}

/** @param callable(SymfonyProcess::ERR|SymfonyProcess::OUT, string): void|null $outputCallback */
/**
* @param callable(SymfonyProcess::ERR|SymfonyProcess::OUT, string): void|null $outputCallback
* @param array<string, string> $buildEnv
*/
private function phpize(
PhpizePath $phpize,
DownloadedPackage $downloadedPackage,
IOInterface $io,
callable|null $outputCallback,
array $buildEnv,
): void {
$phpizeCommand = [$phpize->phpizeBinaryPath];

Expand All @@ -206,26 +220,28 @@ private function phpize(
$phpizeCommand,
$downloadedPackage->extractedSourcePath,
outputCallback: $outputCallback,
env: $buildEnv,
);
}

/**
* @param list<non-empty-string> $configureOptions
* @param callable(SymfonyProcess::ERR|SymfonyProcess::OUT, string): void|null $outputCallback
* @param array<string, string> $buildEnv
*/
private function configure(
DownloadedPackage $downloadedPackage,
TargetPlatform $targetPlatform,
array $configureOptions,
IOInterface $io,
callable|null $outputCallback,
array $buildEnv,
): void {
$configureCommand = ['./configure', ...$configureOptions];
$configureEnv = null;

$pcreCompilerFlags = PcreCompilerFlags::forTargetPlatform($targetPlatform);
if ($pcreCompilerFlags !== null) {
$configureEnv = ['CPPFLAGS' => trim(Platform::getEnv('CPPFLAGS') . ' ' . $pcreCompilerFlags)];
$buildEnv['CPPFLAGS'] = trim(Platform::getEnv('CPPFLAGS') . ' ' . $pcreCompilerFlags);

$io->write(
'<comment>PHP uses an external PCRE library, adding to CPPFLAGS: ' . $pcreCompilerFlags . '</comment>',
Expand All @@ -242,16 +258,20 @@ private function configure(
$configureCommand,
$downloadedPackage->extractedSourcePath,
outputCallback: $outputCallback,
env: $configureEnv,
env: $buildEnv,
);
}

/** @param callable(SymfonyProcess::ERR|SymfonyProcess::OUT, string): void|null $outputCallback */
/**
* @param callable(SymfonyProcess::ERR|SymfonyProcess::OUT, string): void|null $outputCallback
* @param array<string, string> $buildEnv
*/
private function make(
TargetPlatform $targetPlatform,
DownloadedPackage $downloadedPackage,
IOInterface $io,
callable|null $outputCallback,
array $buildEnv,
): void {
$makeCommand = [MakePath::guess()];

Expand All @@ -275,6 +295,7 @@ private function make(
$makeCommand,
$downloadedPackage->extractedSourcePath,
outputCallback: $outputCallback,
env: $buildEnv,
);
}

Expand Down
1 change: 1 addition & 0 deletions test/assets/pie_test_ext/pie_test_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ PHP_MINFO_FUNCTION(pie_test_ext)
{
php_info_print_table_start();
php_info_print_table_row(2, "pie_test_ext support", "enabled");
php_info_print_table_row(2, "Build time", __TIME__);
php_info_print_table_end();
}
/* }}} */
Expand Down
55 changes: 46 additions & 9 deletions test/integration/Building/UnixBuildTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
use Php\Pie\Platform\TargetPlatform;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Safe\DateTimeImmutable;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

use function dirname;

use const PHP_BINARY;

#[CoversClass(UnixBuild::class)]
final class UnixBuildTest extends TestCase
{
Expand All @@ -32,7 +36,18 @@ final class UnixBuildTest extends TestCase
private const TEST_PREBUILT_PATH_VALID = __DIR__ . '/../../assets/pre-packaged-binary-examples/valid';
private const TEST_PREBUILT_PATH_INVALID = __DIR__ . '/../../assets/pre-packaged-binary-examples/invalid';

public function testUnixSourceBuildCanBuildExtension(): void
/** @return array<string, array{string|null, string|null, string}> */
public static function sourceDateEpochCases(): array
{
return [
'release timestamp' => [null, '2023-11-14T22:13:20+00:00', '22:13:20'],
'missing release timestamp' => [null, null, '00:00:00'],
'caller override' => ['1', '2023-11-14T22:13:20+00:00', '00:00:01'],
];
}

#[DataProvider('sourceDateEpochCases')]
public function testUnixSourceBuildCanBuildExtension(string|null $sourceDateEpoch, string|null $releaseDate, string $expectedBuildTime): void
{
if (Platform::isWindows()) {
self::markTestSkipped('Unix build test cannot be run on Windows');
Expand All @@ -44,6 +59,9 @@ public function testUnixSourceBuildCanBuildExtension(): void
$composerPackage
->method('getExtra')
->willReturn([self::COMPOSER_PACKAGE_EXTRA_KEY => DownloadUrlMethod::ComposerDefaultDownload->value]);
$composerPackage
->method('getReleaseDate')
->willReturn($releaseDate === null ? null : new DateTimeImmutable($releaseDate));

$downloadedPackage = DownloadedPackage::fromPackageAndExtractedPath(
new Package(
Expand All @@ -57,15 +75,34 @@ public function testUnixSourceBuildCanBuildExtension(): void
self::TEST_EXTENSION_PATH,
);

$unixBuilder = new UnixBuild();
$builtBinary = $unixBuilder->__invoke(
$downloadedPackage,
TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromCurrentProcess(), null, null),
['--enable-pie_test_ext'],
$output,
);
$originalSourceDateEpoch = Platform::getEnv('SOURCE_DATE_EPOCH');
try {
if ($sourceDateEpoch === null) {
Platform::clearEnv('SOURCE_DATE_EPOCH');
} else {
Platform::putEnv('SOURCE_DATE_EPOCH', $sourceDateEpoch);
}

self::assertNotEmpty($builtBinary);
$builtBinary = (new UnixBuild())->__invoke(
$downloadedPackage,
TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromCurrentProcess(), null, null),
['--enable-pie_test_ext'],
$output,
);

self::assertSame($sourceDateEpoch ?? false, Platform::getEnv('SOURCE_DATE_EPOCH'));
} finally {
if ($originalSourceDateEpoch === false) {
Platform::clearEnv('SOURCE_DATE_EPOCH');
} else {
Platform::putEnv('SOURCE_DATE_EPOCH', $originalSourceDateEpoch);
}
}

$extensionInfo = (new Process([PHP_BINARY, '-n', '-d', 'extension=' . $builtBinary->filePath, '--ri', 'pie_test_ext']))
->mustRun()
->getOutput();
self::assertStringContainsString('Build time => ' . $expectedBuildTime, $extensionInfo);

$outputString = $output->getOutput();

Expand Down