Skip to content

Commit

Permalink
Workaround lack of dead code elimination
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed Nov 26, 2024
1 parent b0ababb commit ff07807
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 20 deletions.
75 changes: 75 additions & 0 deletions Sun/vendor/Soup/build_common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
// Config
$clang = $argv[1] ?? "clang";
$clang .= " -std=c++17 -fno-rtti -O3 -ffunction-sections -fdata-sections";
if (defined("PHP_WINDOWS_VERSION_MAJOR"))
{
$clang .= " -D_CRT_SECURE_NO_WARNINGS";
}
else if (PHP_OS_FAMILY != "Darwin")
{
$clang .= " -fPIC";
}

$clanglink = $clang;
if (!defined("PHP_WINDOWS_VERSION_MAJOR"))
{
$clanglink .= " -lstdc++ -pthread -lm -ldl";
if (!getenv("ANDROID_ROOT"))
{
$clanglink .= " -lresolv";
}
if (PHP_OS_FAMILY != "Darwin")
{
$clanglink .= " -fuse-ld=lld";
if (!getenv("ANDROID_ROOT"))
{
$clanglink .= " -lstdc++fs";
}
}
}

// Utilities
$procs = [];

function run_command_async($cmd)
{
global $procs;
echo ".";
$file = tmpfile();
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("file", stream_get_meta_data($file)["uri"], "a"),
2 => array("file", stream_get_meta_data($file)["uri"], "a"),
);
$proc = proc_open($cmd, $descriptorspec, $pipes);
array_push($procs, [ $proc, $file ]);
if (count($procs) >= (getenv("ANDROID_ROOT") ? 16 : 32))
{
await_commands();
}
}

function await_commands()
{
global $procs;
echo "\r";
$output = "";
while (count($procs) != 0)
{
foreach ($procs as $i => $proc)
{
if (!proc_get_status($proc[0])["running"])
{
echo "";
$output .= stream_get_contents($proc[1]);
fclose($proc[1]);
proc_close($proc[0]);
unset($procs[$i]);
}
}
usleep(50000);
}
echo "\n";
echo $output;
}
7 changes: 0 additions & 7 deletions Sun/vendor/Soup/build_config.php

This file was deleted.

28 changes: 15 additions & 13 deletions Sun/vendor/Soup/build_lib.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php
require "build_config.php";
require "build_common.php";

$cd = getcwd();
chdir(__DIR__);

// Setup folders
if(!is_dir(__DIR__."/bin"))
Expand All @@ -11,29 +14,28 @@
mkdir(__DIR__."/bin/int");
}

// Find work
echo "Compiling...\n";
$files = [];
$objects = [];
foreach(scandir(__DIR__."/soup") as $file)
{
if(substr($file, -4) == ".cpp")
{
array_push($files, substr($file, 0, -4));
$file = substr($file, 0, -4);
run_command_async("$clang -c ".__DIR__."/soup/$file.cpp -o ".__DIR__."/bin/int/$file.o");
array_push($objects, escapeshellarg("bin/int/$file.o"));
}
}

echo "Compiling...\n";
$objects = [];
foreach($files as $file)
{
echo $file."\n";
passthru("$clang -c ".__DIR__."/soup/$file.cpp -o ".__DIR__."/bin/int/$file.o");
array_push($objects, escapeshellarg(__DIR__."/bin/int/$file.o"));
}
await_commands();

echo "Bundling static lib...\n";
$archiver = "ar";
$libname = "libsoup.a";
if (defined("PHP_WINDOWS_VERSION_MAJOR"))
{
$archiver = "llvm-ar";
$libname = "soup.lib";
}
passthru("$archiver rc libsoup.a ".join(" ", $objects));
passthru("$archiver rc $libname ".join(" ", $objects));

chdir($cd);
1 change: 1 addition & 0 deletions Sun/vendor/Soup/soup/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ NAMESPACE_SOUP
#if SOUP_WINDOWS
"-D_CRT_SECURE_NO_WARNINGS",
#endif
"-ffunction-sections", "-fdata-sections", // needed for dead code elimination
"-std="
};
args.back().append(lang);
Expand Down

0 comments on commit ff07807

Please sign in to comment.