Skip to content

Commit 29dc5e4

Browse files
committed
Chore
1 parent 08fa49b commit 29dc5e4

File tree

8 files changed

+127
-105
lines changed

8 files changed

+127
-105
lines changed

src/SPC/builder/windows/library/WindowsLibraryBase.php

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
use SPC\builder\BuilderBase;
88
use SPC\builder\LibraryBase;
99
use SPC\builder\windows\WindowsBuilder;
10-
use SPC\exception\FileSystemException;
11-
use SPC\exception\RuntimeException;
12-
use SPC\exception\WrongUsageException;
13-
use SPC\store\FileSystem;
1410

1511
abstract class WindowsLibraryBase extends LibraryBase
1612
{
@@ -23,50 +19,4 @@ public function getBuilder(): BuilderBase
2319
{
2420
return $this->builder;
2521
}
26-
27-
/**
28-
* @throws RuntimeException
29-
* @throws FileSystemException
30-
* @throws WrongUsageException
31-
*/
32-
public function getStaticLibFiles(string $style = 'autoconf', bool $recursive = true, bool $include_self = true): string
33-
{
34-
$libs = $include_self ? [$this] : [];
35-
if ($recursive) {
36-
array_unshift($libs, ...array_values($this->getDependencies(recursive: true)));
37-
}
38-
39-
$sep = match ($style) {
40-
'autoconf' => ' ',
41-
'cmake' => ';',
42-
default => throw new RuntimeException('style only support autoconf and cmake'),
43-
};
44-
$ret = [];
45-
foreach ($libs as $lib) {
46-
$libFiles = [];
47-
foreach ($lib->getStaticLibs() as $name) {
48-
$name = str_replace(' ', '\ ', FileSystem::convertPath(BUILD_LIB_PATH . "/{$name}"));
49-
$name = str_replace('"', '\"', $name);
50-
$libFiles[] = $name;
51-
}
52-
array_unshift($ret, implode($sep, $libFiles));
53-
}
54-
return implode($sep, $ret);
55-
}
56-
57-
/**
58-
* Create a nmake wrapper file.
59-
*
60-
* @param string $content nmake wrapper content
61-
* @param string $default_filename default nmake wrapper filename
62-
* @throws FileSystemException
63-
*/
64-
public function makeNmakeWrapper(string $content, string $default_filename = ''): string
65-
{
66-
if ($default_filename === '') {
67-
$default_filename = $this->source_dir . '\nmake_wrapper.bat';
68-
}
69-
FileSystem::writeFile($default_filename, $content);
70-
return 'nmake_wrapper.bat';
71-
}
7222
}

src/SPC/builder/windows/library/libffi_win.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,42 @@
55
namespace SPC\builder\windows\library;
66

77
use SPC\builder\windows\SystemUtil;
8-
use SPC\exception\RuntimeException;
8+
use SPC\exception\EnvironmentException;
99
use SPC\store\FileSystem;
1010

1111
class libffi_win extends WindowsLibraryBase
1212
{
1313
public const NAME = 'libffi-win';
1414

15-
protected function build()
15+
private string $vs_ver_dir;
16+
17+
public function validate(): void
1618
{
17-
$vs_ver_dir = match (SystemUtil::findVisualStudio()['version']) {
18-
'vs17' => '/win32/vs17_x64',
19-
'vs16' => '/win32/vs16_x64',
20-
default => throw new RuntimeException('Current VS version is not supported yet!'),
19+
$this->vs_ver_dir = match ($ver = SystemUtil::findVisualStudio()['version']) {
20+
'vs17' => '\win32\vs17_x64',
21+
'vs16' => '\win32\vs16_x64',
22+
default => throw new EnvironmentException("Current VS version {$ver} is not supported !"),
2123
};
24+
}
2225

26+
protected function build(): void
27+
{
2328
// start build
24-
cmd()->cd($this->source_dir . $vs_ver_dir)
29+
cmd()->cd($this->source_dir . $this->vs_ver_dir)
2530
->execWithWrapper(
2631
$this->builder->makeSimpleWrapper('msbuild'),
2732
'libffi-msvc.sln /t:Rebuild /p:Configuration=Release /p:Platform=x64'
2833
);
2934
FileSystem::createDir(BUILD_LIB_PATH);
3035
FileSystem::createDir(BUILD_INCLUDE_PATH);
31-
copy($this->source_dir . $vs_ver_dir . '\x64\Release\libffi.lib', BUILD_LIB_PATH . '\libffi.lib');
32-
copy($this->source_dir . $vs_ver_dir . '\x64\Release\libffi.pdb', BUILD_LIB_PATH . '\libffi.pdb');
33-
copy($this->source_dir . '\include\ffi.h', BUILD_INCLUDE_PATH . '\ffi.h');
36+
37+
FileSystem::copy("{$this->source_dir}{$this->vs_ver_dir}\\x64\\Release\\libffi.lib", BUILD_LIB_PATH . '\libffi.lib');
38+
FileSystem::copy("{$this->source_dir}{$this->vs_ver_dir}\\x64\\Release\\libffi.pdb", BUILD_LIB_PATH . '\libffi.pdb');
39+
FileSystem::copy($this->source_dir . '\include\ffi.h', BUILD_INCLUDE_PATH . '\ffi.h');
3440

3541
FileSystem::replaceFileStr(BUILD_INCLUDE_PATH . '\ffi.h', '#define LIBFFI_H', "#define LIBFFI_H\n#define FFI_BUILDING");
36-
copy($this->source_dir . '\src\x86\ffitarget.h', BUILD_INCLUDE_PATH . '\ffitarget.h');
37-
copy($this->source_dir . '\fficonfig.h', BUILD_INCLUDE_PATH . '\fficonfig.h');
42+
FileSystem::copy($this->source_dir . '\src\x86\ffitarget.h', BUILD_INCLUDE_PATH . '\ffitarget.h');
43+
FileSystem::copy($this->source_dir . '\fficonfig.h', BUILD_INCLUDE_PATH . '\fficonfig.h');
3844

3945
// copy($this->source_dir . '\msvc_build\out\static-Release\X64\libffi.lib', BUILD_LIB_PATH . '\libffi.lib');
4046
// copy($this->source_dir . '\msvc_build\include\ffi.h', BUILD_INCLUDE_PATH . '\ffi.h');

src/SPC/builder/windows/library/libiconv_win.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,36 @@
55
namespace SPC\builder\windows\library;
66

77
use SPC\builder\windows\SystemUtil;
8-
use SPC\exception\RuntimeException;
8+
use SPC\exception\EnvironmentException;
99
use SPC\store\FileSystem;
1010

1111
class libiconv_win extends WindowsLibraryBase
1212
{
1313
public const NAME = 'libiconv-win';
1414

15-
protected function build()
15+
private string $vs_ver_dir = '';
16+
17+
public function validate(): void
1618
{
17-
$vs_ver_dir = match (SystemUtil::findVisualStudio()['version']) {
18-
'vs17' => '/MSVC17',
19-
'vs16' => '/MSVC16',
20-
default => throw new RuntimeException('Current VS version is not supported yet!'),
19+
$this->vs_ver_dir = match ($ver = SystemUtil::findVisualStudio()['version']) {
20+
'vs17' => '\MSVC17',
21+
'vs16' => '\MSVC16',
22+
default => throw new EnvironmentException("Current VS version {$ver} is not supported yet!"),
2123
};
24+
}
2225

26+
protected function build(): void
27+
{
2328
// start build
24-
cmd()->cd($this->source_dir . $vs_ver_dir)
29+
cmd()->cd($this->source_dir . $this->vs_ver_dir)
2530
->execWithWrapper(
2631
$this->builder->makeSimpleWrapper('msbuild'),
2732
'libiconv.sln /t:Rebuild /p:Configuration=Release /p:Platform=x64'
2833
);
2934
FileSystem::createDir(BUILD_LIB_PATH);
3035
FileSystem::createDir(BUILD_INCLUDE_PATH);
31-
copy($this->source_dir . $vs_ver_dir . '\x64\lib\libiconv.lib', BUILD_LIB_PATH . '\libiconv.lib');
32-
copy($this->source_dir . $vs_ver_dir . '\x64\lib\libiconv_a.lib', BUILD_LIB_PATH . '\libiconv_a.lib');
36+
copy("{$this->source_dir}{$this->vs_ver_dir}\\x64\\lib\\libiconv.lib", BUILD_LIB_PATH . '\libiconv.lib');
37+
copy("{$this->source_dir}{$this->vs_ver_dir}\\x64\\lib\\libiconv_a.lib", BUILD_LIB_PATH . '\libiconv_a.lib');
3338
copy($this->source_dir . '\source\include\iconv.h', BUILD_INCLUDE_PATH . '\iconv.h');
3439
}
3540
}

src/SPC/doctor/item/BSDToolCheckList.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use SPC\doctor\AsCheckItem;
99
use SPC\doctor\AsFixItem;
1010
use SPC\doctor\CheckResult;
11-
use SPC\exception\RuntimeException;
1211

1312
class BSDToolCheckList
1413
{
@@ -56,11 +55,8 @@ public function fixBuildTools(array $missing): bool
5655
} else {
5756
$prefix = '';
5857
}
59-
try {
60-
shell(true)->exec("ASSUME_ALWAYS_YES=yes {$prefix}pkg install -y " . implode(' ', $missing));
61-
} catch (RuntimeException) {
62-
return false;
63-
}
58+
shell(true)->exec("ASSUME_ALWAYS_YES=yes {$prefix}pkg install -y " . implode(' ', $missing));
59+
6460
return true;
6561
}
6662
}

src/SPC/util/executor/UnixAutoconfExecutor.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use SPC\builder\freebsd\library\BSDLibraryBase;
88
use SPC\builder\linux\library\LinuxLibraryBase;
99
use SPC\builder\macos\library\MacOSLibraryBase;
10-
use SPC\exception\RuntimeException;
10+
use SPC\exception\SPCException;
1111
use SPC\util\shell\UnixShell;
1212

1313
class UnixAutoconfExecutor extends Executor
@@ -34,8 +34,7 @@ public function configure(...$args): static
3434
$args = array_diff($args, $this->ignore_args);
3535
$configure_args = implode(' ', $args);
3636

37-
$this->shell->exec("./configure {$configure_args}");
38-
return $this;
37+
return $this->seekLogFileOnException(fn () => $this->shell->exec("./configure {$configure_args}"));
3938
}
4039

4140
public function getConfigureArgsString(): string
@@ -53,15 +52,17 @@ public function getConfigureArgsString(): string
5352
*/
5453
public function make(string $target = '', false|string $with_install = 'install', bool $with_clean = true, array $after_env_vars = []): static
5554
{
56-
if ($with_clean) {
57-
$this->shell->exec('make clean');
58-
}
59-
$after_env_vars_str = $after_env_vars !== [] ? shell()->setEnv($after_env_vars)->getEnvString() : '';
60-
$this->shell->exec("make -j{$this->library->getBuilder()->concurrency} {$target} {$after_env_vars_str}");
61-
if ($with_install !== false) {
62-
$this->shell->exec("make {$with_install}");
63-
}
64-
return $this;
55+
return $this->seekLogFileOnException(function () use ($target, $with_install, $with_clean, $after_env_vars) {
56+
if ($with_clean) {
57+
$this->shell->exec('make clean');
58+
}
59+
$after_env_vars_str = $after_env_vars !== [] ? shell()->setEnv($after_env_vars)->getEnvString() : '';
60+
$this->shell->exec("make -j{$this->library->getBuilder()->concurrency} {$target} {$after_env_vars_str}");
61+
if ($with_install !== false) {
62+
$this->shell->exec("make {$with_install}");
63+
}
64+
return $this->shell;
65+
});
6566
}
6667

6768
public function exec(string $cmd): static
@@ -141,4 +142,24 @@ private function initShell(): void
141142
'LDFLAGS' => "-L{$this->library->getLibDir()}",
142143
]);
143144
}
145+
146+
/**
147+
* When an exception occurs, this method will check if the config log file exists.
148+
*/
149+
private function seekLogFileOnException(mixed $callable): static
150+
{
151+
try {
152+
$callable();
153+
return $this;
154+
} catch (SPCException $e) {
155+
if (file_exists("{$this->library->getSourceDir()}/config.log")) {
156+
logger()->debug("Config log file found: {$this->library->getSourceDir()}/config.log");
157+
$log_file = "lib.{$this->library->getName()}.console.log";
158+
logger()->debug('Saved config log file to: ' . SPC_LOGS_DIR . "/{$log_file}");
159+
$e->addExtraLogFile("{$this->library->getName()} library config.log", $log_file);
160+
copy("{$this->library->getSourceDir()}/config.log", SPC_LOGS_DIR . "/{$log_file}");
161+
}
162+
throw $e;
163+
}
164+
}
144165
}

src/globals/defines.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,11 @@
9090
const SPC_SOURCE_GIT = 'git'; // download as git repository
9191
const SPC_SOURCE_LOCAL = 'local'; // download as local directory
9292

93+
// spc logs dir
94+
const SPC_LOGS_DIR = WORKING_DIR . DIRECTORY_SEPARATOR . 'log';
95+
const SPC_OUTPUT_LOG = SPC_LOGS_DIR . DIRECTORY_SEPARATOR . 'spc.output.log';
96+
const SPC_SHELL_LOG = SPC_LOGS_DIR . DIRECTORY_SEPARATOR . 'spc.shell.log';
97+
const SPC_ENV_LOG = SPC_LOGS_DIR . DIRECTORY_SEPARATOR . 'spc.env.log';
98+
9399
ConsoleLogger::$date_format = 'H:i:s';
94100
ConsoleLogger::$format = '[%date%] [%level_short%] %body%';

src/globals/functions.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use Psr\Log\LoggerInterface;
66
use SPC\builder\BuilderBase;
77
use SPC\builder\BuilderProvider;
8+
use SPC\exception\ExecutionException;
89
use SPC\exception\InterruptException;
9-
use SPC\exception\RuntimeException;
1010
use SPC\exception\WrongUsageException;
1111
use SPC\util\shell\UnixShell;
1212
use SPC\util\shell\WindowsCmd;
@@ -165,7 +165,7 @@ function f_passthru(string $cmd): ?bool
165165
}
166166
$ret = passthru($cmd, $code);
167167
if ($code !== 0) {
168-
throw new RuntimeException('Command run failed with code[' . $code . ']: ' . $cmd, $code);
168+
throw new ExecutionException($cmd, "Direct command run failed with code: {$code}", $code);
169169
}
170170
return $ret;
171171
}
@@ -203,7 +203,7 @@ function f_putenv(string $env): bool
203203
function get_cmake_version(): ?string
204204
{
205205
try {
206-
[,$output] = shell()->execWithResult('cmake --version', false);
206+
[,$output] = shell(false)->execWithResult('cmake --version', false);
207207
if (preg_match('/cmake version ([\d.]+)/i', $output[0], $matches)) {
208208
return $matches[1];
209209
}
@@ -244,3 +244,42 @@ function clean_spaces(string $string): string
244244
{
245245
return trim(preg_replace('/\s+/', ' ', $string));
246246
}
247+
248+
/**
249+
* Register a callback function to handle keyboard interrupts (Ctrl+C).
250+
*
251+
* @param callable $callback callback function to handle keyboard interrupts
252+
*/
253+
function keyboard_interrupt_register(callable $callback): void
254+
{
255+
if (PHP_OS_FAMILY === 'Windows') {
256+
sapi_windows_set_ctrl_handler($callback);
257+
} elseif (extension_loaded('pcntl')) {
258+
pcntl_signal(SIGINT, $callback);
259+
}
260+
}
261+
262+
/**
263+
* Unregister the keyboard interrupt handler.
264+
*
265+
* This function is used to remove the previously registered keyboard interrupt handler.
266+
* It should be called when you no longer need to handle keyboard interrupts.
267+
*/
268+
function keyboard_interrupt_unregister(): void
269+
{
270+
if (PHP_OS_FAMILY === 'Windows') {
271+
sapi_windows_set_ctrl_handler(null);
272+
} elseif (extension_loaded('pcntl')) {
273+
pcntl_signal(SIGINT, SIG_IGN);
274+
}
275+
}
276+
277+
/**
278+
* Strip ANSI color codes from a string.
279+
*/
280+
function strip_ansi_colors(string $text): string
281+
{
282+
// Regular expression to match ANSI escape sequences
283+
// Including color codes, cursor control, clear screen and other control sequences
284+
return preg_replace('/\e\[[0-9;]*[a-zA-Z]/', '', $text);
285+
}

src/globals/internal-env.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use SPC\builder\windows\SystemUtil as WindowsSystemUtil;
99
use SPC\ConsoleApplication;
1010
use SPC\store\FileSystem;
11-
use SPC\util\GlobalEnvManager;
1211

1312
// static-php-cli version string
1413
const SPC_VERSION = ConsoleApplication::VERSION;
@@ -47,15 +46,15 @@
4746
]);
4847

4948
// add these to env vars with same name
50-
GlobalEnvManager::putenv('SPC_VERSION=' . SPC_VERSION);
51-
GlobalEnvManager::putenv('BUILD_ROOT_PATH=' . BUILD_ROOT_PATH);
52-
GlobalEnvManager::putenv('BUILD_INCLUDE_PATH=' . BUILD_INCLUDE_PATH);
53-
GlobalEnvManager::putenv('BUILD_LIB_PATH=' . BUILD_LIB_PATH);
54-
GlobalEnvManager::putenv('BUILD_BIN_PATH=' . BUILD_BIN_PATH);
55-
GlobalEnvManager::putenv('PKG_ROOT_PATH=' . PKG_ROOT_PATH);
56-
GlobalEnvManager::putenv('SOURCE_PATH=' . SOURCE_PATH);
57-
GlobalEnvManager::putenv('DOWNLOAD_PATH=' . DOWNLOAD_PATH);
58-
GlobalEnvManager::putenv('CPU_COUNT=' . CPU_COUNT);
59-
GlobalEnvManager::putenv('SPC_ARCH=' . php_uname('m'));
60-
GlobalEnvManager::putenv('GNU_ARCH=' . GNU_ARCH);
61-
GlobalEnvManager::putenv('MAC_ARCH=' . MAC_ARCH);
49+
putenv('SPC_VERSION=' . SPC_VERSION);
50+
putenv('BUILD_ROOT_PATH=' . BUILD_ROOT_PATH);
51+
putenv('BUILD_INCLUDE_PATH=' . BUILD_INCLUDE_PATH);
52+
putenv('BUILD_LIB_PATH=' . BUILD_LIB_PATH);
53+
putenv('BUILD_BIN_PATH=' . BUILD_BIN_PATH);
54+
putenv('PKG_ROOT_PATH=' . PKG_ROOT_PATH);
55+
putenv('SOURCE_PATH=' . SOURCE_PATH);
56+
putenv('DOWNLOAD_PATH=' . DOWNLOAD_PATH);
57+
putenv('CPU_COUNT=' . CPU_COUNT);
58+
putenv('SPC_ARCH=' . php_uname('m'));
59+
putenv('GNU_ARCH=' . GNU_ARCH);
60+
putenv('MAC_ARCH=' . MAC_ARCH);

0 commit comments

Comments
 (0)