Skip to content

Commit 3bb9a7b

Browse files
Feat: UnixCMakeExecutor (#750)
* Add cmake executor and default library path var wrapper * Add cmake tool functions * Use cmake executor instead of raw command * Fix UnixCMakeExecutor.php missing return * Fix UnixCMakeExecutor.php * Refactor remaining cmake libs * Add reset function * Remove unused cmake things * Remove unused cmake things * Whoops * Update src/SPC/builder/unix/library/libzip.php Co-authored-by: Marc <[email protected]> * Update src/SPC/builder/unix/library/libzip.php Co-authored-by: Marc <[email protected]> * For tomorrow windows executor, move it * Rename setCMakeBuildDir to setBuildDir --------- Co-authored-by: Marc <[email protected]>
2 parents a7e48a8 + 9babe7f commit 3bb9a7b

35 files changed

+450
-569
lines changed

src/SPC/builder/LibraryBase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,21 @@ public function patchBeforeMake(): bool
328328
return false;
329329
}
330330

331+
public function getIncludeDir(): string
332+
{
333+
return BUILD_INCLUDE_PATH;
334+
}
335+
336+
public function getBuildRootPath(): string
337+
{
338+
return BUILD_ROOT_PATH;
339+
}
340+
341+
public function getLibDir(): string
342+
{
343+
return BUILD_LIB_PATH;
344+
}
345+
331346
/**
332347
* Build this library.
333348
*

src/SPC/builder/freebsd/BSDBuilder.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public function __construct(array $options = [])
4747
// cflags
4848
$this->arch_c_flags = SystemUtil::getArchCFlags($this->getOption('arch'));
4949
$this->arch_cxx_flags = SystemUtil::getArchCFlags($this->getOption('arch'));
50-
// cmake toolchain
51-
$this->cmake_toolchain_file = SystemUtil::makeCmakeToolchainFile('BSD', $this->getOption('arch'), $this->arch_c_flags);
5250

5351
// create pkgconfig and include dir (some libs cannot create them automatically)
5452
f_mkdir(BUILD_LIB_PATH . '/pkgconfig', recursive: true);

src/SPC/builder/linux/LinuxBuilder.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,6 @@ public function __construct(array $options = [])
4848
// cflags
4949
$this->arch_c_flags = getenv('SPC_DEFAULT_C_FLAGS');
5050
$this->arch_cxx_flags = getenv('SPC_DEFAULT_CXX_FLAGS');
51-
// cmake toolchain
52-
$this->cmake_toolchain_file = SystemUtil::makeCmakeToolchainFile(
53-
'Linux',
54-
$arch,
55-
$this->arch_c_flags,
56-
getenv('CC'),
57-
getenv('CXX'),
58-
);
5951

6052
// cross-compiling is not supported yet
6153
/*if (php_uname('m') !== $this->arch) {

src/SPC/builder/linux/library/libxml2.php

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,9 @@
44

55
namespace SPC\builder\linux\library;
66

7-
use SPC\exception\FileSystemException;
8-
use SPC\exception\RuntimeException;
9-
use SPC\store\FileSystem;
10-
117
class libxml2 extends LinuxLibraryBase
128
{
13-
public const NAME = 'libxml2';
14-
15-
/**
16-
* @throws RuntimeException
17-
* @throws FileSystemException
18-
*/
19-
public function build(): void
20-
{
21-
$enable_zlib = $this->builder->getLib('zlib') ? ('ON -DZLIB_LIBRARY=' . BUILD_LIB_PATH . '/libz.a -DZLIB_INCLUDE_DIR=' . BUILD_INCLUDE_PATH) : 'OFF';
22-
$enable_icu = $this->builder->getLib('icu') ? 'ON' : 'OFF';
23-
$enable_xz = $this->builder->getLib('xz') ? 'ON' : 'OFF';
9+
use \SPC\builder\unix\library\libxml2;
2410

25-
FileSystem::resetDir($this->source_dir . '/build');
26-
shell()->cd($this->source_dir . '/build')
27-
->exec(
28-
'cmake ' .
29-
'-DCMAKE_BUILD_TYPE=Release ' .
30-
'-DCMAKE_INSTALL_PREFIX=' . BUILD_ROOT_PATH . ' ' .
31-
'-DCMAKE_INSTALL_LIBDIR=' . BUILD_LIB_PATH . ' ' .
32-
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
33-
'-DBUILD_SHARED_LIBS=OFF ' .
34-
'-DIconv_IS_BUILT_IN=OFF ' .
35-
'-DLIBXML2_WITH_ICONV=ON ' .
36-
"-DLIBXML2_WITH_ZLIB={$enable_zlib} " .
37-
"-DLIBXML2_WITH_ICU={$enable_icu} " .
38-
"-DLIBXML2_WITH_LZMA={$enable_xz} " .
39-
'-DLIBXML2_WITH_PYTHON=OFF ' .
40-
'-DLIBXML2_WITH_PROGRAMS=OFF ' .
41-
'-DLIBXML2_WITH_TESTS=OFF ' .
42-
'..'
43-
)
44-
->exec("cmake --build . -j {$this->builder->concurrency}")
45-
->exec('make install');
46-
47-
FileSystem::replaceFileStr(
48-
BUILD_LIB_PATH . '/pkgconfig/libxml-2.0.pc',
49-
'-licudata -licui18n -licuuc',
50-
'-licui18n -licuuc -licudata'
51-
);
52-
}
11+
public const NAME = 'libxml2';
5312
}

src/SPC/builder/macos/MacOSBuilder.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public function __construct(array $options = [])
3636
// cflags
3737
$this->arch_c_flags = getenv('SPC_DEFAULT_C_FLAGS');
3838
$this->arch_cxx_flags = getenv('SPC_DEFAULT_CXX_FLAGS');
39-
// cmake toolchain
40-
$this->cmake_toolchain_file = SystemUtil::makeCmakeToolchainFile('Darwin', getenv('SPC_ARCH'), $this->arch_c_flags);
4139

4240
// create pkgconfig and include dir (some libs cannot create them automatically)
4341
f_mkdir(BUILD_LIB_PATH . '/pkgconfig', recursive: true);

src/SPC/builder/macos/library/glfw.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use SPC\exception\FileSystemException;
88
use SPC\exception\RuntimeException;
9+
use SPC\util\executor\UnixCMakeExecutor;
910

1011
class glfw extends MacOSLibraryBase
1112
{
@@ -17,11 +18,14 @@ class glfw extends MacOSLibraryBase
1718
*/
1819
protected function build(): void
1920
{
20-
// compile!
21-
shell()->cd(SOURCE_PATH . '/ext-glfw/vendor/glfw')
22-
->exec("cmake . {$this->builder->makeCmakeArgs()} -DBUILD_SHARED_LIBS=OFF -DGLFW_BUILD_EXAMPLES=OFF -DGLFW_BUILD_TESTS=OFF")
23-
->exec("make -j{$this->builder->concurrency}")
24-
->exec('make install');
21+
UnixCMakeExecutor::create($this)
22+
->setBuildDir("{$this->source_dir}/vendor/glfw")
23+
->setReset(false)
24+
->addConfigureArgs(
25+
'-DGLFW_BUILD_EXAMPLES=OFF',
26+
'-DGLFW_BUILD_TESTS=OFF',
27+
)
28+
->build('.');
2529
// patch pkgconf
2630
$this->patchPkgconfPrefix(['glfw3.pc']);
2731
}

src/SPC/builder/macos/library/libxml2.php

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,9 @@
44

55
namespace SPC\builder\macos\library;
66

7-
use SPC\exception\FileSystemException;
8-
use SPC\exception\RuntimeException;
9-
use SPC\store\FileSystem;
10-
117
class libxml2 extends MacOSLibraryBase
128
{
13-
public const NAME = 'libxml2';
9+
use \SPC\builder\unix\library\libxml2;
1410

15-
/**
16-
* @throws RuntimeException
17-
* @throws FileSystemException
18-
*/
19-
protected function build(): void
20-
{
21-
$enable_zlib = $this->builder->getLib('zlib') ? ('ON -DZLIB_LIBRARY=' . BUILD_LIB_PATH . '/libz.a -DZLIB_INCLUDE_DIR=' . BUILD_INCLUDE_PATH) : 'OFF';
22-
$enable_icu = $this->builder->getLib('icu') ? 'ON' : 'OFF';
23-
$enable_xz = $this->builder->getLib('xz') ? 'ON' : 'OFF';
24-
25-
FileSystem::resetDir($this->source_dir . '/build');
26-
shell()->cd($this->source_dir . '/build')
27-
->exec(
28-
'cmake ' .
29-
// '--debug-find ' .
30-
'-DCMAKE_BUILD_TYPE=Release ' .
31-
'-DCMAKE_INSTALL_PREFIX=' . BUILD_ROOT_PATH . ' ' .
32-
'-DCMAKE_INSTALL_LIBDIR=' . BUILD_LIB_PATH . ' ' .
33-
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
34-
'-DBUILD_SHARED_LIBS=OFF ' .
35-
'-DLIBXML2_WITH_ICONV=ON ' .
36-
"-DLIBXML2_WITH_ZLIB={$enable_zlib} " .
37-
"-DLIBXML2_WITH_ICU={$enable_icu} " .
38-
"-DLIBXML2_WITH_LZMA={$enable_xz} " .
39-
'-DLIBXML2_WITH_PYTHON=OFF ' .
40-
'-DLIBXML2_WITH_PROGRAMS=OFF ' .
41-
'-DLIBXML2_WITH_TESTS=OFF ' .
42-
'..'
43-
)
44-
->exec("cmake --build . -j {$this->builder->concurrency}")
45-
->exec('make install');
46-
}
11+
public const NAME = 'libxml2';
4712
}

src/SPC/builder/traits/UnixSystemUtilTrait.php

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,11 @@
44

55
namespace SPC\builder\traits;
66

7-
use SPC\exception\FileSystemException;
8-
use SPC\store\FileSystem;
9-
107
/**
118
* Unix 系统的工具函数 Trait,适用于 Linux、macOS
129
*/
1310
trait UnixSystemUtilTrait
1411
{
15-
/**
16-
* 生成 toolchain.cmake,用于 cmake 构建
17-
*
18-
* @param string $os 操作系统代号
19-
* @param string $target_arch 目标架构
20-
* @param string $cflags CFLAGS 参数
21-
* @param null|string $cc CC 参数(默认空)
22-
* @param null|string $cxx CXX 参数(默认空)
23-
* @throws FileSystemException
24-
*/
25-
public static function makeCmakeToolchainFile(
26-
string $os,
27-
string $target_arch,
28-
string $cflags,
29-
?string $cc = null,
30-
?string $cxx = null
31-
): string {
32-
logger()->debug("making cmake tool chain file for {$os} {$target_arch} with CFLAGS='{$cflags}'");
33-
$root = BUILD_ROOT_PATH;
34-
$ccLine = '';
35-
if ($cc) {
36-
$ccLine = 'SET(CMAKE_C_COMPILER ' . $cc . ')';
37-
}
38-
$cxxLine = '';
39-
if ($cxx) {
40-
$cxxLine = 'SET(CMAKE_CXX_COMPILER ' . $cxx . ')';
41-
}
42-
$toolchain = <<<CMAKE
43-
{$ccLine}
44-
{$cxxLine}
45-
SET(CMAKE_C_FLAGS "{$cflags}")
46-
SET(CMAKE_CXX_FLAGS "{$cflags}")
47-
SET(CMAKE_FIND_ROOT_PATH "{$root}")
48-
SET(CMAKE_PREFIX_PATH "{$root}")
49-
SET(CMAKE_INSTALL_PREFIX "{$root}")
50-
SET(CMAKE_INSTALL_LIBDIR "lib")
51-
52-
set(PKG_CONFIG_EXECUTABLE "{$root}/bin/pkg-config")
53-
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
54-
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
55-
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
56-
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
57-
set(CMAKE_EXE_LINKER_FLAGS "-ldl -lpthread -lm -lutil")
58-
CMAKE;
59-
// 有时候系统的 cmake 找不到 ar 命令,真奇怪
60-
if (PHP_OS_FAMILY === 'Linux') {
61-
$toolchain .= "\nSET(CMAKE_AR \"ar\")";
62-
}
63-
FileSystem::writeFile(SOURCE_PATH . '/toolchain.cmake', $toolchain);
64-
return realpath(SOURCE_PATH . '/toolchain.cmake');
65-
}
66-
6712
/**
6813
* @param string $name 命令名称
6914
* @param array $paths 寻找的目标路径(如果不传入,则使用环境变量 PATH)

src/SPC/builder/unix/UnixBuilderBase.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use SPC\builder\BuilderBase;
88
use SPC\builder\freebsd\library\BSDLibraryBase;
99
use SPC\builder\linux\library\LinuxLibraryBase;
10-
use SPC\builder\linux\LinuxBuilder;
1110
use SPC\builder\macos\library\MacOSLibraryBase;
1211
use SPC\exception\FileSystemException;
1312
use SPC\exception\RuntimeException;
@@ -25,9 +24,6 @@ abstract class UnixBuilderBase extends BuilderBase
2524
/** @var string C++ flags */
2625
public string $arch_cxx_flags;
2726

28-
/** @var string cmake toolchain file */
29-
public string $cmake_toolchain_file;
30-
3127
/**
3228
* @throws WrongUsageException
3329
* @throws FileSystemException
@@ -56,21 +52,6 @@ public function getAllStaticLibFiles(): array
5652
return array_map(fn ($x) => realpath(BUILD_LIB_PATH . "/{$x}"), $libFiles);
5753
}
5854

59-
/**
60-
* Return generic cmake options when configuring cmake projects
61-
*/
62-
public function makeCmakeArgs(): string
63-
{
64-
$extra = $this instanceof LinuxBuilder ? '-DCMAKE_C_COMPILER=' . getenv('CC') . ' ' : '';
65-
return $extra .
66-
'-DCMAKE_BUILD_TYPE=Release ' .
67-
'-DCMAKE_INSTALL_PREFIX=' . BUILD_ROOT_PATH . ' ' .
68-
'-DCMAKE_INSTALL_BINDIR=bin ' .
69-
'-DCMAKE_INSTALL_LIBDIR=lib ' .
70-
'-DCMAKE_INSTALL_INCLUDEDIR=include ' .
71-
"-DCMAKE_TOOLCHAIN_FILE={$this->cmake_toolchain_file}";
72-
}
73-
7455
/**
7556
* Generate configure flags
7657
*/

src/SPC/builder/unix/library/brotli.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use SPC\exception\FileSystemException;
88
use SPC\exception\RuntimeException;
99
use SPC\store\FileSystem;
10+
use SPC\util\executor\UnixCMakeExecutor;
1011

1112
trait brotli
1213
{
@@ -16,21 +17,10 @@ trait brotli
1617
*/
1718
protected function build(): void
1819
{
19-
FileSystem::resetDir($this->source_dir . '/build-dir');
20-
shell()->cd($this->source_dir . '/build-dir')
21-
->setEnv(['CFLAGS' => $this->getLibExtraCFlags(), 'LDFLAGS' => $this->getLibExtraLdFlags(), 'LIBS' => $this->getLibExtraLibs()])
22-
->execWithEnv(
23-
'cmake ' .
24-
'-DCMAKE_BUILD_TYPE=Release ' .
25-
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
26-
'-DCMAKE_INSTALL_PREFIX=' . BUILD_ROOT_PATH . ' ' .
27-
'-DCMAKE_INSTALL_LIBDIR=lib ' .
28-
'-DSHARE_INSTALL_PREFIX=' . BUILD_ROOT_PATH . ' ' .
29-
'-DBUILD_SHARED_LIBS=OFF ' .
30-
'..'
31-
)
32-
->execWithEnv("cmake --build . -j {$this->builder->concurrency}")
33-
->execWithEnv('make install');
20+
UnixCMakeExecutor::create($this)
21+
->setBuildDir("{$this->getSourceDir()}/build-dir")
22+
->build();
23+
3424
$this->patchPkgconfPrefix(['libbrotlicommon.pc', 'libbrotlidec.pc', 'libbrotlienc.pc']);
3525
shell()->cd(BUILD_ROOT_PATH . '/lib')->exec('ln -sf libbrotlicommon.a libbrotli.a');
3626
foreach (FileSystem::scanDirFiles(BUILD_ROOT_PATH . '/lib/', false, true) as $filename) {

0 commit comments

Comments
 (0)