Skip to content

Commit b6be207

Browse files
committed
fix a few accidental array_reverse calls, pkg-config --static instead of pkg-config --libs
1 parent 9c4a6b4 commit b6be207

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/SPC/util/PkgConfigUtil.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,23 @@ public static function getLibsArray(string $pkg_config_str, bool $force_short_na
3939
$libs = explode(' ', trim($result));
4040

4141
// get other things
42-
$result = self::execWithResult("pkg-config --static --libs --libs-only-other {$pkg_config_str}");
42+
$result = self::execWithResult("pkg-config --static --libs-only-other {$pkg_config_str}");
4343
// convert libxxx.a to -L{path} -lxxx
4444
$exp = explode(' ', trim($result));
4545
foreach ($exp as $item) {
46+
if (str_starts_with($item, '-L')) {
47+
$libs[] = $item;
48+
continue;
49+
}
4650
// if item ends with .a, convert it to -lxxx
47-
if (str_ends_with($item, '.a') && str_starts_with($item, 'lib') && $force_short_name) {
51+
if (str_ends_with($item, '.a') && (str_starts_with($item, 'lib') || str_starts_with($item, BUILD_LIB_PATH))) {
4852
$name = pathinfo($item, PATHINFO_BASENAME);
4953
$name = substr($name, 3, -2); // remove 'lib' prefix and '.a' suffix
50-
$libs[] = "-l{$name}";
51-
} else {
52-
// if item starts with -L, keep it as is
53-
// if item starts with -l, keep it as is
54+
$shortlib = "-l{$name}";
55+
if (!in_array($shortlib, $libs)) {
56+
$libs[] = $shortlib;
57+
}
58+
} elseif (!in_array($item, $libs)) {
5459
$libs[] = $item;
5560
}
5661
}

src/SPC/util/SPCConfigUtil.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ public function config(array $extensions = [], array $libraries = [], bool $incl
8787
$libs .= " {$this->getFrameworksString($extensions)}";
8888
}
8989
if ($this->builder->hasCpp()) {
90-
$libs .= $this->builder instanceof MacOSBuilder ? ' -lc++' : ' -lstdc++';
90+
$libs .= SPCTarget::getTargetOS() === 'Darwin' ? ' -lc++' : ' -lstdc++';
9191
}
9292
if ($this->libs_only_deps) {
93+
$libs = '-L' . BUILD_LIB_PATH . ' ' . $libs;
9394
return [
9495
'cflags' => trim(getenv('CFLAGS') . ' ' . $cflags),
9596
'ldflags' => trim(getenv('LDFLAGS') . ' ' . $ldflags),
@@ -105,6 +106,7 @@ public function config(array $extensions = [], array $libraries = [], bool $incl
105106
if (str_contains($libs, BUILD_LIB_PATH . '/mimalloc.o')) {
106107
$libs = BUILD_LIB_PATH . '/mimalloc.o ' . str_replace(BUILD_LIB_PATH . '/mimalloc.o', '', $libs);
107108
}
109+
$libs = '-L' . BUILD_LIB_PATH . ' ' . $libs;
108110
return [
109111
'cflags' => trim(getenv('CFLAGS') . ' ' . $cflags),
110112
'ldflags' => trim(getenv('LDFLAGS') . ' ' . $ldflags),
@@ -138,8 +140,7 @@ private function getIncludesString(array $libraries): string
138140
}
139141
}
140142
$pc_cflags = implode(' ', $pc);
141-
if ($pc_cflags !== '') {
142-
$pc_cflags = PkgConfigUtil::getCflags($pc_cflags);
143+
if ($pc_cflags !== '' && ($pc_cflags = PkgConfigUtil::getCflags($pc_cflags)) !== '') {
143144
$includes[] = $pc_cflags;
144145
}
145146
}
@@ -165,7 +166,7 @@ private function getLibsString(array $libraries, bool $use_short_libs = true): s
165166
if (!file_exists(BUILD_LIB_PATH . "/{$lib}")) {
166167
throw new WrongUsageException("Library file '{$lib}' for lib [{$library}] does not exist in '" . BUILD_LIB_PATH . "'. Please build it first.");
167168
}
168-
$lib_names[] = $use_short_libs ? $this->getShortLibName($lib) : (BUILD_LIB_PATH . "/{$lib}");
169+
$lib_names[] = $this->getShortLibName($lib);
169170
}
170171
// add frameworks for macOS
171172
if (SPCTarget::getTargetOS() === 'Darwin') {
@@ -180,13 +181,13 @@ private function getLibsString(array $libraries, bool $use_short_libs = true): s
180181
}
181182
$pkg_configs = implode(' ', $pkg_configs);
182183
if ($pkg_configs !== '') {
183-
$pc_libs = array_reverse(PkgConfigUtil::getLibsArray($pkg_configs, $use_short_libs));
184+
$pc_libs = PkgConfigUtil::getLibsArray($pkg_configs);
184185
$lib_names = [...$lib_names, ...$pc_libs];
185186
}
186187
}
187188

188189
// post-process
189-
$lib_names = array_reverse(array_unique($lib_names));
190+
$lib_names = array_reverse(array_unique(array_reverse($lib_names)));
190191
$frameworks = array_unique($frameworks);
191192

192193
// process frameworks to short_name

0 commit comments

Comments
 (0)