Skip to content

Commit b0538c0

Browse files
committed
deduplicate those to make it more readable
1 parent 40f89d1 commit b0538c0

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/SPC/builder/Extension.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,23 @@ public function patchBeforeSharedMake(): bool
222222
{
223223
$config = (new SPCConfigUtil($this->builder))->config([$this->getName()], array_map(fn ($l) => $l->getName(), $this->builder->getLibs()));
224224
[$staticLibs, $sharedLibs] = $this->splitLibsIntoStaticAndShared($config['libs']);
225-
FileSystem::replaceFileRegex(
226-
$this->source_dir . '/Makefile',
227-
'/^(.*_SHARED_LIBADD\s*=.*)$/m',
228-
clean_spaces("$1 {$staticLibs} {$sharedLibs}")
229-
);
225+
$lstdcpp = str_contains($sharedLibs, '-lstdc++') ? '-lstdc++' : '';
226+
227+
$makefileContent = file_get_contents($this->source_dir . '/Makefile');
228+
229+
if (preg_match('/^(.*_SHARED_LIBADD\s*=\s*)(.*)$/m', $makefileContent, $matches)) {
230+
$prefix = $matches[1];
231+
$currentLibs = trim($matches[2]);
232+
$newLibs = trim("{$currentLibs} {$staticLibs} {$lstdcpp}");
233+
$deduplicatedLibs = deduplicate_flags($newLibs);
234+
235+
FileSystem::replaceFileRegex(
236+
$this->source_dir . '/Makefile',
237+
'/^(.*_SHARED_LIBADD\s*=.*)$/m',
238+
$prefix . $deduplicatedLibs
239+
);
240+
}
241+
230242
if ($objs = getenv('SPC_EXTRA_RUNTIME_OBJECTS')) {
231243
FileSystem::replaceFileRegex(
232244
$this->source_dir . '/Makefile',

src/globals/functions.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,16 @@ function clean_spaces(string $string): string
245245
return trim(preg_replace('/\s+/', ' ', $string));
246246
}
247247

248+
function deduplicate_flags(string $flags): string
249+
{
250+
$tokens = preg_split('/\s+/', trim($flags));
251+
252+
// Reverse, unique, reverse back - keeps last occurrence of duplicates
253+
$deduplicated = array_reverse(array_unique(array_reverse($tokens)));
254+
255+
return implode(' ', $deduplicated);
256+
}
257+
248258
/**
249259
* Register a callback function to handle keyboard interrupts (Ctrl+C).
250260
*

0 commit comments

Comments
 (0)