-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnixBuild.php
More file actions
272 lines (226 loc) · 9.55 KB
/
UnixBuild.php
File metadata and controls
272 lines (226 loc) · 9.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
declare(strict_types=1);
namespace Php\Pie\Building;
use Composer\IO\IOInterface;
use LogicException;
use Php\Pie\ComposerIntegration\BundledPhpExtensionsRepository;
use Php\Pie\Downloading\DownloadedPackage;
use Php\Pie\Downloading\DownloadUrlMethod;
use Php\Pie\File\BinaryFile;
use Php\Pie\Platform\TargetPhp\PhpizePath;
use Php\Pie\Platform\TargetPlatform;
use Php\Pie\Util\Process;
use Php\Pie\Util\ProcessFailedWithLimitedOutput;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process as SymfonyProcess;
use function count;
use function file_exists;
use function implode;
use function Safe\rename;
use function sprintf;
use const DIRECTORY_SEPARATOR;
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
final class UnixBuild implements Build
{
/** {@inheritDoc} */
public function __invoke(
DownloadedPackage $downloadedPackage,
TargetPlatform $targetPlatform,
array $configureOptions,
IOInterface $io,
): BinaryFile {
$selectedDownloadMethod = DownloadUrlMethod::fromDownloadedPackage($downloadedPackage);
switch ($selectedDownloadMethod) {
case DownloadUrlMethod::PrePackagedBinary:
return $this->prePackagedBinary($downloadedPackage, $io);
case DownloadUrlMethod::ComposerDefaultDownload:
case DownloadUrlMethod::PrePackagedSourceDownload:
return $this->buildFromSource($downloadedPackage, $targetPlatform, $configureOptions, $io);
default:
throw new LogicException('Unsupported download method: ' . $selectedDownloadMethod->value);
}
}
private function prePackagedBinary(
DownloadedPackage $downloadedPackage,
IOInterface $io,
): BinaryFile {
$expectedSoFile = $downloadedPackage->extractedSourcePath . '/' . $downloadedPackage->package->extensionName()->name() . '.so';
if (! file_exists($expectedSoFile)) {
throw ExtensionBinaryNotFound::fromPrePackagedBinary($expectedSoFile);
}
$io->write(sprintf(
'<info>Pre-packaged binary found:</info> %s',
$expectedSoFile,
));
return BinaryFile::fromFileWithSha256Checksum($expectedSoFile);
}
/** @param list<non-empty-string> $configureOptions */
private function buildFromSource(
DownloadedPackage $downloadedPackage,
TargetPlatform $targetPlatform,
array $configureOptions,
IOInterface $io,
): BinaryFile {
$outputCallback = null;
if ($io->isVerbose()) {
$outputCallback = static function (string $type, string $outputMessage) use ($io): void {
$io->write(sprintf(
'%s%s%s',
$type === SymfonyProcess::ERR ? '<comment>' : '',
$outputMessage,
$type === SymfonyProcess::ERR ? '</comment>' : '',
));
};
}
$phpizePath = $targetPlatform->phpizePath ?? PhpizePath::guessFrom($targetPlatform->phpBinaryPath);
/**
* Call a cleanup first; most of the time, we expect to be changing a
* version (e.g. upgrade, downgrade), in which case the source is
* already clean anyway; however, sometimes we want to rebuild the
* current ext, so this will perform a clean first
*/
$this->cleanup($phpizePath, $downloadedPackage, $io, $outputCallback);
$this->phpize(
$phpizePath,
$downloadedPackage,
$io,
$outputCallback,
);
$io->write('<info>phpize complete</info>.');
$phpConfigPath = $targetPlatform->phpBinaryPath->phpConfigPath();
if ($phpConfigPath !== null) {
$configureOptions[] = '--with-php-config=' . $phpConfigPath;
}
$this->configure($downloadedPackage, $configureOptions, $io, $outputCallback);
$optionsOutput = count($configureOptions) ? ' with options: ' . implode(' ', $configureOptions) : '.';
$io->write('<info>Configure complete</info>' . $optionsOutput);
try {
$this->make($targetPlatform, $downloadedPackage, $io, $outputCallback);
} catch (ProcessFailedException $p) {
throw ProcessFailedWithLimitedOutput::fromProcessFailedException($p);
}
$expectedSoFile = $downloadedPackage->extractedSourcePath . '/modules/' . $downloadedPackage->package->extensionName()->name() . '.so';
if (! file_exists($expectedSoFile)) {
throw ExtensionBinaryNotFound::fromExpectedBinary($expectedSoFile);
}
$io->write(sprintf(
'<info>Build complete:</info> %s',
$expectedSoFile,
));
return BinaryFile::fromFileWithSha256Checksum($expectedSoFile);
}
private function renamesToConfigM4(DownloadedPackage $downloadedPackage, IOInterface $io): void
{
$configM4 = $downloadedPackage->extractedSourcePath . DIRECTORY_SEPARATOR . 'config.m4';
if (file_exists($configM4)) {
return;
}
$io->write('config.m4 does not exist; checking alternatives', verbosity: IOInterface::VERY_VERBOSE);
foreach (['config0.m4', 'config9.m4'] as $alternateConfigM4) {
$fullPathToAlternate = $downloadedPackage->extractedSourcePath . DIRECTORY_SEPARATOR . $alternateConfigM4;
if (file_exists($fullPathToAlternate)) {
$io->write(sprintf('Renaming %s to config.m4', $alternateConfigM4), verbosity: IOInterface::VERY_VERBOSE);
rename($fullPathToAlternate, $configM4);
return;
}
}
}
/** @param callable(SymfonyProcess::ERR|SymfonyProcess::OUT, string): void|null $outputCallback */
private function phpize(
PhpizePath $phpize,
DownloadedPackage $downloadedPackage,
IOInterface $io,
callable|null $outputCallback,
): void {
$phpizeCommand = [$phpize->phpizeBinaryPath];
$io->write(
'<comment>Running phpize step using: ' . implode(' ', $phpizeCommand) . '</comment>',
verbosity: IOInterface::VERBOSE,
);
$this->renamesToConfigM4($downloadedPackage, $io);
Process::run(
$phpizeCommand,
$downloadedPackage->extractedSourcePath,
outputCallback: $outputCallback,
);
}
/**
* @param list<non-empty-string> $configureOptions
* @param callable(SymfonyProcess::ERR|SymfonyProcess::OUT, string): void|null $outputCallback
*/
private function configure(
DownloadedPackage $downloadedPackage,
array $configureOptions,
IOInterface $io,
callable|null $outputCallback,
): void {
$configureCommand = ['./configure', ...$configureOptions];
$io->write(
'<comment>Running configure step with: ' . implode(' ', $configureCommand) . '</comment>',
verbosity: IOInterface::VERBOSE,
);
Process::run(
$configureCommand,
$downloadedPackage->extractedSourcePath,
outputCallback: $outputCallback,
);
}
/** @param callable(SymfonyProcess::ERR|SymfonyProcess::OUT, string): void|null $outputCallback */
private function make(
TargetPlatform $targetPlatform,
DownloadedPackage $downloadedPackage,
IOInterface $io,
callable|null $outputCallback,
): void {
$makeCommand = ['make'];
if ($targetPlatform->makeParallelJobs === 1) {
$io->write('Running make without parallelization - try providing -jN to PIE where N is the number of cores you have.');
} else {
$makeCommand[] = sprintf('-j%d', $targetPlatform->makeParallelJobs);
}
$makeCommand = BundledPhpExtensionsRepository::augmentMakeCommandForPhpBundledExtensions(
$makeCommand,
$downloadedPackage,
);
$io->write(
'<comment>Running make step with: ' . implode(' ', $makeCommand) . '</comment>',
verbosity: IOInterface::VERBOSE,
);
Process::run(
$makeCommand,
$downloadedPackage->extractedSourcePath,
outputCallback: $outputCallback,
);
}
/** @param callable(SymfonyProcess::ERR|SymfonyProcess::OUT, string): void|null $outputCallback */
private function cleanup(
PhpizePath $phpize,
DownloadedPackage $downloadedPackage,
IOInterface $io,
callable|null $outputCallback,
): void {
/**
* A basic, but fallible check to see if we should clean first. This
* should work "most" of the time, unless someone has removed the
* configure script manually...
*/
if (! file_exists($downloadedPackage->extractedSourcePath . '/configure')) {
$io->write(
'<comment>Skipping phpize --clean, configure does not exist</comment>',
verbosity: IOInterface::VERBOSE,
);
return;
}
$phpizeCleanCommand = [$phpize->phpizeBinaryPath, '--clean'];
$io->write(
'<comment>Running phpize --clean step using: ' . implode(' ', $phpizeCleanCommand) . '</comment>',
verbosity: IOInterface::VERBOSE,
);
Process::run(
$phpizeCleanCommand,
$downloadedPackage->extractedSourcePath,
outputCallback: $outputCallback,
);
$io->write('<info>Build files cleaned up.</info>');
}
}