Skip to content

Commit 5583677

Browse files
authored
Merge pull request #869 from crazywhalecc/pkg-config-pkg
turn pkg-config into a package
2 parents 0c9130a + 3b9efcb commit 5583677

File tree

8 files changed

+122
-25
lines changed

8 files changed

+122
-25
lines changed

config/lib.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
{
22
"lib-base": {
3-
"type": "root",
4-
"lib-depends-unix": [
5-
"pkg-config"
6-
]
3+
"type": "root"
74
},
85
"php": {
96
"type": "root",

config/pkg.json

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
{
2+
"go-xcaddy-aarch64-linux": {
3+
"type": "custom"
4+
},
5+
"go-xcaddy-aarch64-macos": {
6+
"type": "custom"
7+
},
8+
"go-xcaddy-x86_64-linux": {
9+
"type": "custom"
10+
},
11+
"go-xcaddy-x86_64-macos": {
12+
"type": "custom"
13+
},
214
"musl-toolchain-aarch64-linux": {
315
"type": "url",
416
"url": "https://dl.static-php.dev/static-php-cli/deps/musl-toolchain/aarch64-musl-toolchain.tgz"
@@ -15,6 +27,38 @@
1527
"nasm-2.16.01/ndisasm.exe": "{php_sdk_path}/bin/ndisasm.exe"
1628
}
1729
},
30+
"pkg-config-aarch64-linux": {
31+
"type": "ghrel",
32+
"repo": "static-php/static-php-cli-hosted",
33+
"match": "pkg-config-aarch64-linux-musl-1.2.5.txz",
34+
"extract-files": {
35+
"bin/pkg-config": "{pkg_root_path}/bin/pkg-config"
36+
}
37+
},
38+
"pkg-config-aarch64-macos": {
39+
"type": "ghrel",
40+
"repo": "static-php/static-php-cli-hosted",
41+
"match": "pkg-config-aarch64-darwin.txz",
42+
"extract-files": {
43+
"bin/pkg-config": "{pkg_root_path}/bin/pkg-config"
44+
}
45+
},
46+
"pkg-config-x86_64-linux": {
47+
"type": "ghrel",
48+
"repo": "static-php/static-php-cli-hosted",
49+
"match": "pkg-config-x86_64-linux-musl-1.2.5.txz",
50+
"extract-files": {
51+
"bin/pkg-config": "{pkg_root_path}/bin/pkg-config"
52+
}
53+
},
54+
"pkg-config-x86_64-macos": {
55+
"type": "ghrel",
56+
"repo": "static-php/static-php-cli-hosted",
57+
"match": "pkg-config-x86_64-darwin.txz",
58+
"extract-files": {
59+
"bin/pkg-config": "{pkg_root_path}/bin/pkg-config"
60+
}
61+
},
1862
"strawberry-perl-x86_64-win": {
1963
"type": "url",
2064
"url": "https://github.com/StrawberryPerl/Perl-Dist-Strawberry/releases/download/SP_5380_5361/strawberry-perl-5.38.0.1-64bit-portable.zip"
@@ -43,30 +87,18 @@
4387
"upx-*-win64/upx.exe": "{pkg_root_path}/bin/upx.exe"
4488
}
4589
},
46-
"go-xcaddy-x86_64-linux": {
47-
"type": "custom"
48-
},
49-
"go-xcaddy-aarch64-linux": {
50-
"type": "custom"
51-
},
52-
"go-xcaddy-x86_64-macos": {
90+
"zig-aarch64-linux": {
5391
"type": "custom"
5492
},
55-
"go-xcaddy-aarch64-macos": {
93+
"zig-aarch64-macos": {
5694
"type": "custom"
5795
},
5896
"zig-x86_64-linux": {
5997
"type": "custom"
6098
},
61-
"zig-aarch64-linux": {
62-
"type": "custom"
63-
},
6499
"zig-x86_64-macos": {
65100
"type": "custom"
66101
},
67-
"zig-aarch64-macos": {
68-
"type": "custom"
69-
},
70102
"zig-x86_64-win": {
71103
"type": "custom"
72104
}

src/SPC/builder/freebsd/BSDBuilder.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ public function __construct(array $options = [])
2525
f_putenv('CXX=' . $this->getOption('cxx', 'clang++'));
2626
// set PATH
2727
f_putenv('PATH=' . BUILD_ROOT_PATH . '/bin:' . getenv('PATH'));
28-
// set PKG_CONFIG
29-
f_putenv('PKG_CONFIG=' . BUILD_ROOT_PATH . '/bin/pkg-config');
30-
// set PKG_CONFIG_PATH
31-
f_putenv('PKG_CONFIG_PATH=' . BUILD_LIB_PATH . '/pkgconfig/');
3228

3329
// set arch (default: current)
3430
$this->setOptionIfNotExist('arch', php_uname('m'));
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\doctor\item;
6+
7+
use SPC\doctor\AsCheckItem;
8+
use SPC\doctor\AsFixItem;
9+
use SPC\doctor\CheckResult;
10+
use SPC\doctor\OptionalCheck;
11+
use SPC\store\PackageManager;
12+
use SPC\util\PkgConfigUtil;
13+
14+
#[OptionalCheck([self::class, 'optionalCheck'])]
15+
class PkgConfigCheck
16+
{
17+
public static function optionalCheck(): bool
18+
{
19+
return PHP_OS_FAMILY !== 'Windows';
20+
}
21+
22+
/** @noinspection PhpUnused */
23+
#[AsCheckItem('if pkg-config is installed or built', level: 800)]
24+
public function checkPkgConfig(): CheckResult
25+
{
26+
if (!($pkgconf = PkgConfigUtil::findPkgConfig())) {
27+
return CheckResult::fail('pkg-config is not installed', 'install-pkgconfig');
28+
}
29+
return CheckResult::ok($pkgconf);
30+
}
31+
32+
#[AsFixItem('install-pkgconfig')]
33+
public function installPkgConfig(): bool
34+
{
35+
PackageManager::installPackage('pkg-config');
36+
return true;
37+
}
38+
}

src/SPC/toolchain/ToolchainManager.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use SPC\builder\linux\SystemUtil;
88
use SPC\exception\WrongUsageException;
99
use SPC\util\GlobalEnvManager;
10+
use SPC\util\PkgConfigUtil;
1011
use SPC\util\SPCTarget;
1112

1213
class ToolchainManager
@@ -56,6 +57,15 @@ public static function afterInitToolchain(): void
5657
if (SPCTarget::getLibc() === 'glibc' && SystemUtil::isMuslDist()) {
5758
throw new WrongUsageException('You are linking against glibc dynamically, which is only supported on glibc distros.');
5859
}
60+
61+
// init pkg-config for unix
62+
if (is_unix()) {
63+
if (($found = PkgConfigUtil::findPkgConfig()) === null) {
64+
throw new WrongUsageException('Cannot find pkg-config executable. Please run `doctor` to fix this.');
65+
}
66+
GlobalEnvManager::putenv("PKG_CONFIG={$found}");
67+
}
68+
5969
$toolchain = getenv('SPC_TOOLCHAIN');
6070
/* @var ToolchainInterface $toolchain */
6171
$instance = new $toolchain();

src/SPC/util/GlobalEnvManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public static function init(): void
3939
// Define env vars for unix
4040
if (is_unix()) {
4141
self::addPathIfNotExists(BUILD_BIN_PATH);
42-
self::putenv('PKG_CONFIG=' . BUILD_BIN_PATH . '/pkg-config');
43-
self::putenv('PKG_CONFIG_PATH=' . BUILD_ROOT_PATH . '/lib/pkgconfig');
42+
self::addPathIfNotExists(PKG_ROOT_PATH . '/bin');
43+
self::putenv('PKG_CONFIG_PATH=' . BUILD_LIB_PATH . '/pkgconfig');
4444
}
4545

4646
$ini = self::readIniFile();

src/SPC/util/PkgConfigUtil.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,28 @@
1414
*/
1515
class PkgConfigUtil
1616
{
17+
/**
18+
* Find the pkg-config executable which is compatible with static builds.
19+
*
20+
* @return null|string Path to pkg-config executable, or null if not found
21+
*/
22+
public static function findPkgConfig(): ?string
23+
{
24+
// Find pkg-config executable
25+
$find_list = [
26+
PKG_ROOT_PATH . '/bin/pkg-config',
27+
BUILD_BIN_PATH . '/pkg-config',
28+
];
29+
$found = null;
30+
foreach ($find_list as $file) {
31+
if (file_exists($file) && is_executable($file)) {
32+
$found = $file;
33+
break;
34+
}
35+
}
36+
return $found;
37+
}
38+
1739
/**
1840
* Returns the version of a module.
1941
* This method uses `pkg-config --modversion` to get the version of the specified module.

src/SPC/util/executor/UnixCMakeExecutor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use SPC\builder\linux\library\LinuxLibraryBase;
99
use SPC\builder\macos\library\MacOSLibraryBase;
1010
use SPC\store\FileSystem;
11+
use SPC\util\PkgConfigUtil;
1112
use SPC\util\shell\UnixShell;
1213

1314
/**
@@ -184,6 +185,7 @@ private function makeCmakeToolchainFile(): string
184185
$cxx = getenv('CCX');
185186
logger()->debug("making cmake tool chain file for {$os} {$target_arch} with CFLAGS='{$cflags}'");
186187
$root = BUILD_ROOT_PATH;
188+
$pkgConfigExecutable = PkgConfigUtil::findPkgConfig();
187189
$ccLine = '';
188190
if ($cc) {
189191
$ccLine = 'SET(CMAKE_C_COMPILER ' . $cc . ')';
@@ -202,7 +204,7 @@ private function makeCmakeToolchainFile(): string
202204
SET(CMAKE_INSTALL_PREFIX "{$root}")
203205
SET(CMAKE_INSTALL_LIBDIR "lib")
204206
205-
set(PKG_CONFIG_EXECUTABLE "{$root}/bin/pkg-config")
207+
set(PKG_CONFIG_EXECUTABLE "{$pkgConfigExecutable}")
206208
list(APPEND PKG_CONFIG_EXECUTABLE "--static")
207209
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
208210
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)

0 commit comments

Comments
 (0)