-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathMakePath.php
More file actions
67 lines (53 loc) · 1.87 KB
/
Copy pathMakePath.php
File metadata and controls
67 lines (53 loc) · 1.87 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
<?php
declare(strict_types=1);
namespace Php\Pie\Platform;
use Symfony\Component\Process\Process;
use function assert;
use function file_exists;
use function is_executable;
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 */
final class MakePath
{
private static function looksLikeValidGnuMake(string $makePathToCheck): bool
{
if ($makePathToCheck === '') {
return false;
}
if (! file_exists($makePathToCheck) || ! is_executable($makePathToCheck)) {
return false;
}
$makeVersionProcess = new Process([$makePathToCheck, '--version']);
if ($makeVersionProcess->run() !== 0) {
return false;
}
return preg_match('/GNU Make/i', $makeVersionProcess->getOutput()) === 1;
}
/**
* PHP extension Makefiles (generated by phpize/configure) rely on GNU
* Make syntax. On some platforms (e.g. FreeBSD, NetBSD, OpenBSD), the
* `make` found on the PATH is the native BSD make, which cannot parse
* these Makefiles; GNU Make is available there as `gmake` instead.
*
* @return non-empty-string
*/
public static function guess(): string
{
foreach (['make', 'gmake'] as $candidate) {
// Note: depends on `which` existing, which doesn't always...
$which = new Process(['which', $candidate]);
if ($which->run() !== 0) {
continue;
}
$candidatePath = trim($which->getOutput());
if (! self::looksLikeValidGnuMake($candidatePath)) {
continue;
}
assert($candidatePath !== '');
return $candidatePath;
}
// Fall back to assuming/hoping make is on the path
return 'make';
}
}