Skip to content

Commit 29a8c9c

Browse files
committed
libraries: honour SPC_DEFAULT_CFLAGS/CXXFLAGS/LDFLAGS and bug fixes
bzip2, fastlz, jbig, qdbm: thread SPC_DEFAULT_CFLAGS into the hand-rolled Makefile patches and shell compile commands. Backward compatible when the env var is empty. icu: append SPC_DEFAULT_CXXFLAGS/LDFLAGS to runConfigureICU's CXXFLAGS and LDFLAGS so user flags reach the bundled cxx invocation. openssl: append user CFLAGS/LDFLAGS after the target name on Configure so options like -flto and -fprofile-* reach the openssl build. libheif: rewrite libheif 1.22+'s C-incompatible `struct heif_bad_pixel { uint32_t row; uint32_t column; };` as a typedef so C consumers compile. ncurses: filter the clang/zig-cc "N warning(s) generated." stdout line out of MKlib_gen.sh's preprocessor pipe before sed turns it into invalid C in lib_gen.c. libaom: detect target CPU from SystemTarget instead of hard-coding generic, fall back to generic only when neither nasm nor yasm is available, and turn off examples/tests/tools/docs.
1 parent 582a88e commit 29a8c9c

9 files changed

Lines changed: 75 additions & 7 deletions

File tree

src/Package/Library/bzip2.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ class bzip2
1717
#[PatchBeforeBuild]
1818
public function patchBeforeBuild(LibraryPackage $lib): void
1919
{
20-
FileSystem::replaceFileStr($lib->getSourceDir() . '/Makefile', 'CFLAGS=-Wall', 'CFLAGS=-fPIC -Wall');
20+
// Makefile pins -O2 -fPIC; inject SPC_DEFAULT_CFLAGS
21+
$extra = deduplicate_flags(trim((string) getenv('SPC_DEFAULT_CFLAGS')) . ' -fPIC -Wall');
22+
FileSystem::replaceFileStr($lib->getSourceDir() . '/Makefile', 'CFLAGS=-Wall', "CFLAGS={$extra}");
2123
}
2224

2325
#[BuildFor('Windows')]

src/Package/Library/fastlz.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ public function build(LibraryPackage $lib): void
1818
{
1919
$cc = getenv('CC') ?: 'cc';
2020
$ar = getenv('AR') ?: 'ar';
21+
$extra = trim((string) getenv('SPC_DEFAULT_CFLAGS'));
22+
$extra = $extra !== '' ? $extra . ' -fPIC' : '-O3 -fPIC';
2123

2224
shell()->cd($lib->getSourceDir())->initializeEnv($lib)
23-
->exec("{$cc} -c -O3 -fPIC fastlz.c -o fastlz.o")
25+
->exec("{$cc} -c {$extra} fastlz.c -o fastlz.o")
2426
->exec("{$ar} rcs libfastlz.a fastlz.o");
2527

2628
// Copy header file

src/Package/Library/icu.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ public function beforePack(LibraryPackage $lib): void
2424
#[BuildFor('Linux')]
2525
public function buildLinux(LibraryPackage $lib, ToolchainInterface $toolchain, PackageBuilder $builder): void
2626
{
27+
// runConfigureICU bakes CXXFLAGS/LDFLAGS, apply user flags too
28+
$userCxxFlags = trim((string) getenv('SPC_DEFAULT_CXXFLAGS'));
29+
$userLdFlags = trim((string) getenv('SPC_DEFAULT_LDFLAGS'));
2730
$cppflags = 'CPPFLAGS="-DU_CHARSET_IS_UTF8=1 -DU_USING_ICU_NAMESPACE=1 -DU_STATIC_IMPLEMENTATION=1 -DPIC -fPIC"';
28-
$cxxflags = 'CXXFLAGS="-std=c++17 -DPIC -fPIC -fno-ident"';
29-
$ldflags = $toolchain->isStatic() ? 'LDFLAGS="-static"' : '';
31+
$cxxflags = "CXXFLAGS=\"-std=c++17 -DPIC -fPIC -fno-ident {$userCxxFlags}\"";
32+
$ldflags = $toolchain->isStatic() ? "LDFLAGS=\"-static {$userLdFlags}\"" : "LDFLAGS=\"{$userLdFlags}\"";
3033
shell()->cd($lib->getSourceDir() . '/source')->initializeEnv($lib)
3134
->exec(
3235
"{$cppflags} {$cxxflags} {$ldflags} " .

src/Package/Library/jbig.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ class jbig
1717
#[PatchBeforeBuild]
1818
public function patchBeforeBuild(LibraryPackage $lib): void
1919
{
20-
FileSystem::replaceFileStr($lib->getSourceDir() . '/Makefile', 'CFLAGS = -O2 -W -Wno-unused-result', 'CFLAGS = -O2 -W -Wno-unused-result -fPIC');
20+
$extra = trim((string) getenv('SPC_DEFAULT_CFLAGS'));
21+
$cflags = ($extra !== '' ? $extra : '-O2') . ' -W -Wno-unused-result -fPIC';
22+
FileSystem::replaceFileStr($lib->getSourceDir() . '/Makefile', 'CFLAGS = -O2 -W -Wno-unused-result', "CFLAGS = {$cflags}");
2123
}
2224

2325
#[BuildFor('Darwin')]

src/Package/Library/libaom.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
use StaticPHP\Package\LibraryPackage;
1010
use StaticPHP\Runtime\Executor\UnixCMakeExecutor;
1111
use StaticPHP\Runtime\Executor\WindowsCMakeExecutor;
12+
use StaticPHP\Runtime\SystemTarget;
1213
use StaticPHP\Toolchain\Interface\ToolchainInterface;
1314
use StaticPHP\Toolchain\ZigToolchain;
15+
use StaticPHP\Util\System\UnixUtil;
1416

1517
#[Library('libaom')]
1618
class libaom extends LibraryPackage
@@ -39,9 +41,23 @@ public function buildUnix(ToolchainInterface $toolchain): void
3941
$new = trim($extra . ' -D_GNU_SOURCE');
4042
f_putenv("SPC_COMPILER_EXTRA={$new}");
4143
}
44+
$targetCpu = SystemTarget::getTargetArch();
45+
if (str_starts_with($targetCpu, 'aarch')) {
46+
$targetCpu = str_replace('aarch', 'arm', $targetCpu);
47+
}
48+
if (!UnixUtil::findCommand('nasm') && !UnixUtil::findCommand('yasm')) {
49+
$targetCpu = 'generic';
50+
}
4251
UnixCMakeExecutor::create($this)
4352
->setBuildDir("{$this->getSourceDir()}/builddir")
44-
->addConfigureArgs('-DAOM_TARGET_CPU=generic')
53+
->addConfigureArgs(
54+
"-DAOM_TARGET_CPU={$targetCpu}",
55+
'-DCONFIG_RUNTIME_CPU_DETECT=1',
56+
'-DENABLE_EXAMPLES=OFF',
57+
'-DENABLE_TESTS=OFF',
58+
'-DENABLE_TOOLS=OFF',
59+
'-DENABLE_DOCS=OFF',
60+
)
4561
->build();
4662
f_putenv("SPC_COMPILER_EXTRA={$extra}");
4763
$this->patchPkgconfPrefix(['aom.pc']);

src/Package/Library/libheif.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ public function patchBeforeBuild(LibraryPackage $lib): void
2424
'list(APPEND REQUIRES_PRIVATE "libbrotlidec")' . "\n" . ' list(APPEND REQUIRES_PRIVATE "libbrotlienc")'
2525
);
2626
}
27+
// libheif 1.22+ ships a C-incompatible header: `struct heif_bad_pixel`
28+
$heif_properties = $lib->getSourceDir() . '/libheif/api/libheif/heif_properties.h';
29+
if (file_exists($heif_properties)
30+
&& str_contains(file_get_contents($heif_properties), 'struct heif_bad_pixel { uint32_t row; uint32_t column; };')
31+
) {
32+
FileSystem::replaceFileStr(
33+
$heif_properties,
34+
'struct heif_bad_pixel { uint32_t row; uint32_t column; };',
35+
'typedef struct heif_bad_pixel { uint32_t row; uint32_t column; } heif_bad_pixel;'
36+
);
37+
}
2738
}
2839

2940
#[BuildFor('Darwin')]

src/Package/Library/ncurses.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use StaticPHP\Attribute\Package\BuildFor;
88
use StaticPHP\Attribute\Package\Library;
9+
use StaticPHP\Attribute\Package\PatchBeforeBuild;
10+
use StaticPHP\Attribute\PatchDescription;
911
use StaticPHP\Package\LibraryPackage;
1012
use StaticPHP\Runtime\Executor\UnixAutoconfExecutor;
1113
use StaticPHP\Toolchain\Interface\ToolchainInterface;
@@ -16,6 +18,24 @@
1618
#[Library('ncursesw')]
1719
class ncurses
1820
{
21+
#[PatchBeforeBuild]
22+
#[PatchDescription('Filter clang/zig "N warning(s) generated." line out of MKlib_gen.sh preprocessor pipe')]
23+
public function patchBeforeBuild(LibraryPackage $lib): void
24+
{
25+
// MKlib_gen.sh feeds the C preprocessor's stdout through a sed/awk
26+
// pipeline into lib_gen.c. zig-cc/clang emits "N warning(s) generated."
27+
// on stdout (not stderr), and that line ends up as invalid C in the
28+
// generated source. Filter it out of the pipe before sed sees it.
29+
$mklibGen = $lib->getSourceDir() . '/ncurses/base/MKlib_gen.sh';
30+
if (is_file($mklibGen) && !str_contains((string) file_get_contents($mklibGen), "| grep -v ' generated")) {
31+
FileSystem::replaceFileStr(
32+
$mklibGen,
33+
'$preprocessor $TMP 2>/dev/null \\',
34+
"\$preprocessor \$TMP 2>/dev/null \\\n| grep -v ' generated\\.\$' \\",
35+
);
36+
}
37+
}
38+
1939
#[BuildFor('Darwin')]
2040
#[BuildFor('Linux')]
2141
public function build(LibraryPackage $package, ToolchainInterface $toolchain): void

src/Package/Library/openssl.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ public function build(LibraryPackage $lib): void
111111
$openssl_dir ??= LinuxUtil::getOSRelease()['dist'] === 'redhat' ? '/etc/pki/tls' : '/etc/ssl';
112112
$ex_lib = trim($ex_lib);
113113

114+
// anything we want included (PGO -fprofile-*, LTO, custom hardening)
115+
// has to be appended on the command line *after* the target name.
116+
$userCFlags = trim((string) getenv('SPC_DEFAULT_CFLAGS'));
117+
$userLdFlags = trim((string) getenv('SPC_DEFAULT_LDFLAGS'));
118+
$userExtra = trim($userCFlags . ' ' . $userLdFlags);
119+
114120
shell()->cd($lib->getSourceDir())->initializeEnv($lib)
115121
->exec(
116122
"{$env} ./Configure no-shared zlib " .
@@ -121,7 +127,8 @@ public function build(LibraryPackage $lib): void
121127
'enable-pie ' .
122128
'no-legacy ' .
123129
'no-tests ' .
124-
"linux-{$arch}"
130+
"linux-{$arch} " .
131+
$userExtra
125132
)
126133
->exec('make clean')
127134
->exec("make -j{$lib->getBuilder()->concurrency} CNF_EX_LIBS=\"{$ex_lib}\"")

src/Package/Library/qdbm.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public function buildUnix(LibraryPackage $lib): void
2020
{
2121
$ac = UnixAutoconfExecutor::create($lib)->configure();
2222
FileSystem::replaceFileRegex($lib->getSourceDir() . '/Makefile', '/MYLIBS = libqdbm.a.*/m', 'MYLIBS = libqdbm.a');
23+
// Makefile pins -O3, replace with SPC_DEFAULT_CFLAGS
24+
$extra = trim((string) getenv('SPC_DEFAULT_CFLAGS'));
25+
if ($extra !== '') {
26+
FileSystem::replaceFileRegex($lib->getSourceDir() . '/Makefile', '/^CFLAGS = .*$/m', "CFLAGS = -Wall {$extra}");
27+
}
2328
$ac->make(SystemTarget::getTargetOS() === 'Darwin' ? 'mac' : '');
2429
$lib->patchPkgconfPrefix(['qdbm.pc']);
2530
}

0 commit comments

Comments
 (0)