Skip to content

Commit efdd2a7

Browse files
committed
update libraries to honour user flags
1 parent 6e32672 commit efdd2a7

13 files changed

Lines changed: 117 additions & 12 deletions

File tree

src/Package/Extension/opcache.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public function getUnixConfigureArg(bool $shared, PackageBuilder $builder): stri
7272
) {
7373
$opcache_jit = ' --disable-opcache-jit';
7474
}
75-
return '--enable-opcache' . ($shared ? '=shared' : '') . $opcache_jit;
75+
// PHP 8.5+ has opcache built-in
76+
if ($phpVersionID < 80500) {
77+
return '--enable-opcache' . ($shared ? '=shared' : '') . $opcache_jit;
78+
}
79+
return trim($opcache_jit);
7680
}
7781
}

src/Package/Extension/spx.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,27 @@
66

77
use Package\Target\php;
88
use StaticPHP\Attribute\Package\BeforeStage;
9+
use StaticPHP\Attribute\Package\CustomPhpConfigureArg;
910
use StaticPHP\Attribute\Package\Extension;
1011
use StaticPHP\Attribute\PatchDescription;
12+
use StaticPHP\Package\PackageInstaller;
1113
use StaticPHP\Package\PhpExtensionPackage;
1214
use StaticPHP\Util\FileSystem;
1315

1416
#[Extension('spx')]
1517
class spx extends PhpExtensionPackage
1618
{
19+
#[CustomPhpConfigureArg('Linux')]
20+
#[CustomPhpConfigureArg('Darwin')]
21+
public function getUnixConfigureArg(bool $shared, PackageInstaller $installer): string
22+
{
23+
$arg = '--enable-SPX' . ($shared ? '=shared' : '');
24+
if ($installer->getLibraryPackage('zlib') !== null) {
25+
$arg .= ' --with-zlib-dir=' . BUILD_ROOT_PATH;
26+
}
27+
return $arg;
28+
}
29+
1730
#[BeforeStage('php', [php::class, 'buildconfForUnix'], 'ext-spx')]
1831
#[PatchDescription('Fix spx extension compile error when building as static')]
1932
public function patchBeforeBuildconf(): bool

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/liblz4.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@
1717
class liblz4
1818
{
1919
#[PatchBeforeBuild]
20-
#[PatchDescription('Fix Makefile install target for static liblz4')]
20+
#[PatchDescription('Compile lib sources individually so -flto -c with multiple inputs works under zig-cc/clang')]
2121
public function patchBeforeBuild(LibraryPackage $lib): void
2222
{
23-
FileSystem::replaceFileStr($lib->getSourceDir() . '/programs/Makefile', 'install: lz4', "install: lz4\n\ninstallewfwef: lz4");
23+
// `-flto -c` with multiple input files only writes a .o for the
24+
// first source — the others are silently dropped, leaving liblz4.a with a
25+
// single object. Compile each source individually so all .o files exist.
26+
FileSystem::replaceFileStr(
27+
$lib->getSourceDir() . '/lib/Makefile',
28+
"liblz4.a: \$(SRCFILES)\nifeq (\$(BUILD_STATIC),yes) # can be disabled on command line\n\t@echo compiling static library\n\t\$(COMPILE.c) \$^\n\t\$(AR) rcs \$@ *.o\nendif",
29+
"liblz4.a: \$(SRCFILES:.c=.o)\nifeq (\$(BUILD_STATIC),yes) # can be disabled on command line\n\t@echo compiling static library\n\t\$(AR) rcs \$@ \$^\nendif"
30+
);
2431
}
2532

2633
#[BuildFor('Windows')]

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)