-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathTargetPlatform.php
More file actions
122 lines (109 loc) · 4.1 KB
/
Copy pathTargetPlatform.php
File metadata and controls
122 lines (109 loc) · 4.1 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
<?php
declare(strict_types=1);
namespace Php\Pie\Platform;
use Fidry\CpuCoreCounter\CpuCoreCounter;
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
use Php\Pie\Platform\TargetPhp\PhpizePath;
use function explode;
use function function_exists;
use function posix_getuid;
use function Safe\preg_match;
use function trim;
/**
* @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks
*
* @immutable
*/
class TargetPlatform
{
private static LibcFlavour $libcFlavour;
public function __construct(
public readonly OperatingSystem $operatingSystem,
public readonly OperatingSystemFamily $operatingSystemFamily,
public readonly PhpBinaryPath $phpBinaryPath,
public readonly Architecture $architecture,
public readonly ThreadSafetyMode $threadSafety,
public readonly int $makeParallelJobs,
public readonly WindowsCompiler|null $windowsCompiler,
public readonly PhpizePath|null $phpizePath,
) {
}
public function libcFlavour(): LibcFlavour
{
if (! isset(self::$libcFlavour)) {
self::$libcFlavour = LibcFlavour::detect();
}
return self::$libcFlavour;
}
public static function isRunningAsRoot(): bool
{
return function_exists('posix_getuid') && posix_getuid() === 0;
}
public static function fromPhpBinaryPath(PhpBinaryPath $phpBinaryPath, int|null $makeParallelJobs, PhpizePath|null $phpizePath): self
{
$os = $phpBinaryPath->operatingSystem();
$osFamily = $phpBinaryPath->operatingSystemFamily();
$phpinfo = $phpBinaryPath->phpinfo();
$architecture = $phpBinaryPath->machineType();
$windowsCompiler = null;
$threadSafety = ThreadSafetyMode::ThreadSafe;
/**
* Based on xdebug.org wizard, copyright Derick Rethans, used under MIT licence
*
* @link https://github.com/xdebug/xdebug.org/blob/aff649f2c3ca303ad471e6ed9dd29c0db16d3e22/src/XdebugVersion.php#L276-L299
*/
if (preg_match('/PHP Extension Build([ =>\t]+)(API.*)/', $phpinfo, $m)) {
$parts = explode(',', trim($m[2]));
foreach ($parts as $part) {
switch ($part) {
case 'NTS':
$threadSafety = ThreadSafetyMode::NonThreadSafe;
break;
case 'TS':
$threadSafety = ThreadSafetyMode::ThreadSafe;
break;
case 'VC6':
$windowsCompiler = WindowsCompiler::VC6;
break;
case 'VC8':
$windowsCompiler = WindowsCompiler::VC8;
break;
case 'VC9':
$windowsCompiler = WindowsCompiler::VC9;
break;
case 'VC11':
$windowsCompiler = WindowsCompiler::VC11;
break;
case 'VC14':
$windowsCompiler = WindowsCompiler::VC14;
break;
case 'VC15':
$windowsCompiler = WindowsCompiler::VC15;
break;
case 'VS16':
$windowsCompiler = WindowsCompiler::VS16;
break;
case 'VS17':
$windowsCompiler = WindowsCompiler::VS17;
break;
case 'VS18':
$windowsCompiler = WindowsCompiler::VS18;
break;
}
}
}
if ($makeParallelJobs === null) {
$makeParallelJobs = (new CpuCoreCounter())->getAvailableForParallelisation()->availableCpus;
}
return new self(
$os,
$osFamily,
$phpBinaryPath,
$architecture,
$threadSafety,
$makeParallelJobs,
$windowsCompiler,
$phpizePath,
);
}
}