-
-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathUnixAutoconfExecutor.php
More file actions
167 lines (149 loc) · 5.59 KB
/
UnixAutoconfExecutor.php
File metadata and controls
167 lines (149 loc) · 5.59 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
<?php
declare(strict_types=1);
namespace SPC\util\executor;
use SPC\builder\freebsd\library\BSDLibraryBase;
use SPC\builder\linux\library\LinuxLibraryBase;
use SPC\builder\macos\library\MacOSLibraryBase;
use SPC\exception\SPCException;
use SPC\util\shell\UnixShell;
class UnixAutoconfExecutor extends Executor
{
protected UnixShell $shell;
protected array $configure_args = [];
public function __construct(protected BSDLibraryBase|LinuxLibraryBase|MacOSLibraryBase $library)
{
parent::__construct($library);
$this->initShell();
$this->configure_args = $this->getDefaultConfigureArgs();
}
/**
* Run ./configure
*/
public function configure(...$args): static
{
$args = array_merge($args, $this->configure_args);
$configure_args = implode(' ', $args);
return $this->seekLogFileOnException(fn () => $this->shell->exec("./configure {$configure_args}"));
}
/**
* Run make
*
* @param string $target Build target
* @param false|string $with_install Run `make install` after building, or false to skip
* @param bool $with_clean Whether to clean before building
* @param array $after_env_vars Environment variables postfix
*/
public function make(string $target = '', false|string $with_install = 'install', bool $with_clean = true, array $after_env_vars = [], ?string $dir = null): static
{
return $this->seekLogFileOnException(function () use ($target, $with_install, $with_clean, $after_env_vars, $dir) {
$shell = $this->shell;
if ($dir) {
$shell = $shell->cd($dir);
}
if ($with_clean) {
$shell->exec('make clean');
}
$after_env_vars_str = $after_env_vars !== [] ? shell()->setEnv($after_env_vars)->getEnvString() : '';
$shell->exec("make -j{$this->library->getBuilder()->concurrency} {$target} {$after_env_vars_str}");
if ($with_install !== false) {
$shell->exec("make {$with_install}");
}
return $shell;
});
}
public function exec(string $cmd): static
{
$this->shell->exec($cmd);
return $this;
}
/**
* Add optional library configuration.
* This method checks if a library is available and adds the corresponding arguments to the CMake configuration.
*
* @param string $name library name to check
* @param \Closure|string $true_args arguments to use if the library is available (allow closure, returns string)
* @param string $false_args arguments to use if the library is not available
* @return $this
*/
public function optionalLib(string $name, \Closure|string $true_args, string $false_args = ''): static
{
if ($get = $this->library->getBuilder()->getLib($name)) {
logger()->info("Building library [{$this->library->getName()}] with {$name} support");
$args = $true_args instanceof \Closure ? $true_args($get) : $true_args;
} else {
logger()->info("Building library [{$this->library->getName()}] without {$name} support");
$args = $false_args;
}
$this->addConfigureArgs($args);
return $this;
}
/**
* Add configure args.
*/
public function addConfigureArgs(...$args): static
{
$this->configure_args = [...$this->configure_args, ...$args];
return $this;
}
/**
* Remove some configure args, to bypass the configure option checking for some libs.
*/
public function removeConfigureArgs(...$args): static
{
$this->configure_args = array_diff($this->configure_args, $args);
return $this;
}
public function setEnv(array $env): static
{
$this->shell->setEnv($env);
return $this;
}
public function appendEnv(array $env): static
{
$this->shell->appendEnv($env);
return $this;
}
/**
* Returns the default autoconf ./configure arguments
*/
private function getDefaultConfigureArgs(): array
{
return [
'--enable-static',
'--disable-shared',
"--prefix={$this->library->getBuildRootPath()}",
'--with-pic',
'--enable-pic',
];
}
/**
* Initialize UnixShell class.
*/
private function initShell(): void
{
$this->shell = shell()->cd($this->library->getSourceDir())->initializeEnv($this->library)->appendEnv([
'CFLAGS' => "-I{$this->library->getIncludeDir()}",
'CXXFLAGS' => "-I{$this->library->getIncludeDir()}",
'LDFLAGS' => "-L{$this->library->getLibDir()}",
]);
}
/**
* When an exception occurs, this method will check if the config log file exists.
*/
private function seekLogFileOnException(mixed $callable): static
{
try {
$callable();
return $this;
} catch (SPCException $e) {
if (file_exists("{$this->library->getSourceDir()}/config.log")) {
logger()->debug("Config log file found: {$this->library->getSourceDir()}/config.log");
$log_file = "lib.{$this->library->getName()}.console.log";
logger()->debug('Saved config log file to: ' . SPC_LOGS_DIR . "/{$log_file}");
$e->addExtraLogFile("{$this->library->getName()} library config.log", $log_file);
copy("{$this->library->getSourceDir()}/config.log", SPC_LOGS_DIR . "/{$log_file}");
}
throw $e;
}
}
}