-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckAllBuildTools.php
More file actions
200 lines (171 loc) · 7.93 KB
/
Copy pathCheckAllBuildTools.php
File metadata and controls
200 lines (171 loc) · 7.93 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
<?php
declare(strict_types=1);
namespace Php\Pie\SelfManage\BuildTools;
use Composer\IO\IOInterface;
use Php\Pie\Platform\PackageManager;
use Php\Pie\Platform\TargetPlatform;
use Throwable;
use function array_unique;
use function array_values;
use function count;
use function implode;
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
class CheckAllBuildTools
{
public static function buildToolsFactory(): self
{
return new self([
new BinaryBuildToolFinder(
'gcc',
[
PackageManager::Apt->value => 'gcc',
PackageManager::Apk->value => 'build-base',
PackageManager::Dnf->value => 'gcc',
PackageManager::Microdnf->value => 'gcc',
PackageManager::Yum->value => 'gcc',
PackageManager::Brew->value => 'gcc',
],
),
new BinaryBuildToolFinder(
'make',
[
PackageManager::Apt->value => 'make',
PackageManager::Apk->value => 'build-base',
PackageManager::Dnf->value => 'make',
PackageManager::Microdnf->value => 'make',
PackageManager::Yum->value => 'make',
PackageManager::Brew->value => 'make',
],
),
new BinaryBuildToolFinder(
'autoconf',
[
PackageManager::Apt->value => 'autoconf',
PackageManager::Apk->value => 'autoconf',
PackageManager::Dnf->value => 'autoconf',
PackageManager::Microdnf->value => 'autoconf',
PackageManager::Yum->value => 'autoconf',
PackageManager::Brew->value => 'autoconf',
],
),
new BinaryBuildToolFinder(
'pkg-config',
[
PackageManager::Apt->value => 'pkg-config',
PackageManager::Apk->value => 'pkgconfig',
PackageManager::Dnf->value => 'pkgconf-pkg-config',
PackageManager::Microdnf->value => 'pkgconf-pkg-config',
PackageManager::Yum->value => 'pkgconf-pkg-config',
PackageManager::Brew->value => 'pkgconf',
],
),
new BinaryBuildToolFinder(
['libtoolize', 'glibtoolize'],
[
PackageManager::Apt->value => 'libtool',
PackageManager::Apk->value => 'libtool',
PackageManager::Dnf->value => 'libtool',
PackageManager::Microdnf->value => 'libtool',
PackageManager::Yum->value => 'libtool',
PackageManager::Brew->value => 'libtool',
],
),
// Composer's archive downloader uses /usr/bin/unzip first
// and falls back to git-source-cloning when it isn't
// present (not to PHP's ZipArchive). Without unzip, a
// pre-packaged-binary dist URL is silently swapped for a
// git clone of the source tree and the .so the user paid
// for in download time is never extracted, surfacing as
// ExtensionBinaryNotFound when PIE's prePackagedBinary
// check looks for it in the vendor dir. Bare php:X.Y-cli
// Debian images do not ship /usr/bin/unzip.
new BinaryBuildToolFinder(
'unzip',
[
PackageManager::Apt->value => 'unzip',
PackageManager::Apk->value => 'unzip',
PackageManager::Dnf->value => 'unzip',
PackageManager::Microdnf->value => 'unzip',
PackageManager::Yum->value => 'unzip',
PackageManager::Brew->value => 'unzip',
],
),
new PhpizeBuildToolFinder(
[
PackageManager::Apt->value => 'php-dev',
PackageManager::Apk->value => 'php{major}{minor}-dev',
PackageManager::Dnf->value => '{php-config-path}',
PackageManager::Microdnf->value => '{php-config-path}',
PackageManager::Yum->value => '{php-config-path}',
PackageManager::Brew->value => 'php',
],
),
]);
}
/** @param list<BinaryBuildToolFinder> $buildTools */
public function __construct(
private readonly array $buildTools,
) {
}
public function check(IOInterface $io, PackageManager|null $packageManager, TargetPlatform $targetPlatform, bool $autoInstallIfMissing): void
{
$io->write('<info>Checking if all build tools are installed.</info>', verbosity: IOInterface::VERBOSE);
/** @var list<string> $packagesToInstall */
$packagesToInstall = [];
$missingTools = [];
$allFound = true;
foreach ($this->buildTools as $buildTool) {
if ($buildTool->check($targetPlatform) !== false) {
$io->write('Build tool ' . $buildTool->toolNames() . ' is installed.', verbosity: IOInterface::VERY_VERBOSE);
continue;
}
$allFound = false;
$missingTools[] = $buildTool->toolNames();
if ($packageManager === null) {
continue;
}
$packageName = $buildTool->packageNameFor($packageManager, $targetPlatform);
if ($packageName === null) {
$io->writeError('<warning>Could not find package name for build tool ' . $buildTool->toolNames() . '.</warning>', verbosity: IOInterface::VERBOSE);
continue;
}
$packagesToInstall[] = $packageName;
}
if ($allFound) {
$io->write('<info>All build tools found.</info>', verbosity: IOInterface::VERBOSE);
return;
}
$io->write('<comment>The following build tools are missing: ' . implode(', ', $missingTools) . '</comment>');
if ($packageManager === null) {
$io->write('<warning>Could not find a package manager to install the missing build tools.</warning>');
return;
}
if (! count($packagesToInstall)) {
$io->write('<warning>Could not determine packages to install.</warning>');
return;
}
$proposedInstallCommand = implode(' ', $packageManager->installCommand(array_values(array_unique($packagesToInstall))));
if (! $io->isInteractive() && ! $autoInstallIfMissing) {
$io->writeError('<warning>You are not running in interactive mode, and you did not provide the --auto-install-build-tools flag.');
$io->writeError('You may need to run: ' . $proposedInstallCommand . '</warning>');
$io->writeError('');
return;
}
$io->write('The following command will be run: ' . $proposedInstallCommand, verbosity: IOInterface::VERBOSE);
if ($io->isInteractive() && ! $autoInstallIfMissing) {
if (! $io->askConfirmation('<question>Would you like to install them now? [y/N]</question>', false)) {
$io->write('<comment>Ok, but things might not work. Just so you know.</comment>');
return;
}
}
try {
$packageManager->install($io, array_values(array_unique($packagesToInstall)));
$io->write('<info>Missing build tools have been installed.</info>');
} catch (Throwable $throwable) {
$io->writeError('<error>Could not install the missing build tools. You may need to install them manually.</error>');
$io->writeError($throwable->__toString(), verbosity: IOInterface::VERBOSE);
$io->writeError('');
return;
}
}
}