Skip to content

Commit

Permalink
Merge pull request #231 from DubbleClick/cplus
Browse files Browse the repository at this point in the history
Linux c++ support using musl-cross-make toolchains
  • Loading branch information
crazywhalecc authored Oct 31, 2023
2 parents 7625f58 + 0b37080 commit 9fb5173
Show file tree
Hide file tree
Showing 70 changed files with 795 additions and 565 deletions.
350 changes: 215 additions & 135 deletions composer.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions config/ext.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@
},
"intl": {
"type": "builtin",
"cpp-extension": true,
"lib-depends": [
"icu"
]
Expand Down Expand Up @@ -233,7 +232,7 @@
},
"openssl": {
"type": "builtin",
"arg-type": "with",
"arg-type": "custom",
"lib-depends": [
"openssl"
],
Expand Down
15 changes: 9 additions & 6 deletions config/lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
},
"icu": {
"source": "icu",
"cpp-library": true,
"static-libs-unix": [
"libicui18n.a",
"libicuio.a",
Expand All @@ -122,20 +123,20 @@
"source": "imagemagick",
"static-libs-unix": [
"libMagick++-7.Q16HDRI.a",
"libMagickCore-7.Q16HDRI.a",
"libMagickWand-7.Q16HDRI.a"
"libMagickWand-7.Q16HDRI.a",
"libMagickCore-7.Q16HDRI.a"
],
"lib-depends": [
"zlib",
"libpng",
"libjpeg",
"bzip2",
"libwebp",
"freetype"
],
"lib-suggests": [
"zstd",
"xz",
"bzip2",
"libzip",
"libxml2"
]
Expand Down Expand Up @@ -434,7 +435,10 @@
"readline"
],
"lib-suggests": [
"icu"
"icu",
"libxslt",
"ldap",
"zstd"
]
},
"pthreads4w": {
Expand Down Expand Up @@ -464,10 +468,9 @@
"libsnappy.a"
],
"headers-unix": [
"snappy.h",
"snappy-c.h",
"snappy-sinksource.h",
"snappy.h",
"snappy-stubs-internal.h",
"snappy-stubs-public.h"
],
"lib-depends": [
Expand Down
2 changes: 1 addition & 1 deletion config/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@
},
"sqlite": {
"type": "url",
"url": "https://www.sqlite.org/2023/sqlite-autoconf-3410100.tar.gz",
"url": "https://www.sqlite.org/2023/sqlite-autoconf-3430200.tar.gz",
"license": {
"type": "text",
"text": "The author disclaims copyright to this source code. In place of\na legal notice, here is a blessing:\n\n * May you do good and not evil.\n * May you find forgiveness for yourself and forgive others.\n * May you share freely, never taking more than you give."
Expand Down
2 changes: 1 addition & 1 deletion src/SPC/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
final class ConsoleApplication extends Application
{
public const VERSION = '2.0-rc7';
public const VERSION = '2.0.0-rc8';

public function __construct()
{
Expand Down
32 changes: 30 additions & 2 deletions src/SPC/builder/BuilderBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ public function getLib(string $name): ?LibraryBase
return $this->libs[$name] ?? null;
}

/**
* Get all library objects.
*
* @return LibraryBase[]
*/
public function getLibs(): array
{
return $this->libs;
}

/**
* Add extension to build.
*/
Expand Down Expand Up @@ -139,12 +149,12 @@ public function getExts(): array
}

/**
* Check if there is a cpp extension.
* Check if there is a cpp extensions or libraries.
*
* @throws FileSystemException
* @throws WrongUsageException
*/
public function hasCppExtension(): bool
public function hasCpp(): bool
{
// judge cpp-extension
$exts = array_keys($this->getExts());
Expand All @@ -153,6 +163,12 @@ public function hasCppExtension(): bool
return true;
}
}
$libs = array_keys($this->getLibs());
foreach ($libs as $lib) {
if (Config::getLib($lib, 'cpp-library', false) === true) {
return true;
}
}
return false;
}

Expand Down Expand Up @@ -303,6 +319,18 @@ public function setOption(string $key, mixed $value): void
$this->options[$key] = $value;
}

public function getEnvString(array $vars = ['cc', 'cxx', 'ar', 'ld']): string
{
$env = [];
foreach ($vars as $var) {
$var = strtoupper($var);
if (getenv($var) !== false) {
$env[] = "{$var}=" . getenv($var);
}
}
return implode(' ', $env);
}

/**
* Check if all libs are downloaded.
* If not, throw exception.
Expand Down
4 changes: 4 additions & 0 deletions src/SPC/builder/extension/iconv.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace SPC\builder\extension;

use SPC\builder\Extension;
use SPC\builder\macos\MacOSBuilder;
use SPC\util\CustomExt;

#[CustomExt('iconv')]
Expand All @@ -13,6 +14,9 @@ class iconv extends Extension
public function patchBeforeConfigure(): bool
{
// macOS need to link iconv dynamically, we add it to extra-libs
if (!$this->builder instanceof MacOSBuilder) {
return false;
}
$extra_libs = $this->builder->getOption('extra-libs', '');
if (!str_contains($extra_libs, '-liconv')) {
$extra_libs .= ' -liconv';
Expand Down
8 changes: 3 additions & 5 deletions src/SPC/builder/extension/imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
#[CustomExt('imagick')]
class imagick extends Extension
{
public function patchBeforeBuildconf(): bool
public function patchBeforeMake(): bool
{
// linux need to link library manually, we add it to extra-libs
// imagick may call omp_pause_all which requires -lgomp
$extra_libs = $this->builder->getOption('extra-libs', '');
if (!str_contains($extra_libs, 'libMagickCore')) {
$extra_libs .= ' /usr/lib/libMagick++-7.Q16HDRI.a /usr/lib/libMagickCore-7.Q16HDRI.a /usr/lib/libMagickWand-7.Q16HDRI.a';
}
$extra_libs .= ' -lgomp ';
$this->builder->setOption('extra-libs', $extra_libs);
return true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/SPC/builder/extension/openssl.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public function patchBeforeMake(): bool

return false;
}

public function getUnixConfigureArg(): string
{
return '--with-openssl=' . BUILD_ROOT_PATH . ' --with-openssl-dir=' . BUILD_ROOT_PATH;
}
}
37 changes: 20 additions & 17 deletions src/SPC/builder/freebsd/BSDBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ public function __construct(array $options = [])

// ---------- set necessary options ----------
// set C Compiler (default: clang)
$this->setOptionIfNotExist('cc', 'clang');
f_putenv('CC=' . $this->getOption('cc', 'clang'));
// set C++ Composer (default: clang++)
$this->setOptionIfNotExist('cxx', 'clang++');
f_putenv('CXX=' . $this->getOption('cxx', 'clang++'));
// set PATH
f_putenv('PATH=' . BUILD_ROOT_PATH . '/bin:' . getenv('PATH'));
// set PKG_CONFIG
f_putenv('PKG_CONFIG=' . BUILD_ROOT_PATH . '/bin/pkg-config');
// set PKG_CONFIG_PATH
f_putenv('PKG_CONFIG_PATH=' . BUILD_LIB_PATH . '/pkgconfig/');

// set arch (default: current)
$this->setOptionIfNotExist('arch', php_uname('m'));
$this->setOptionIfNotExist('gnu-arch', arch2gnu($this->getOption('arch')));
Expand All @@ -46,16 +53,6 @@ public function __construct(array $options = [])
$this->arch_cxx_flags = SystemUtil::getArchCFlags($this->getOption('arch'));
// cmake toolchain
$this->cmake_toolchain_file = SystemUtil::makeCmakeToolchainFile('BSD', $this->getOption('arch'), $this->arch_c_flags);
// configure environment
$this->configure_env = SystemUtil::makeEnvVarString([
'PKG_CONFIG' => BUILD_ROOT_PATH . '/bin/pkg-config',
'PKG_CONFIG_PATH' => BUILD_LIB_PATH . '/pkgconfig/',
'CC' => $this->getOption('cc'),
'CXX' => $this->getOption('cxx'),
'CFLAGS' => "{$this->arch_c_flags} -Wimplicit-function-declaration -Os",
'LIBS' => '-ldl -lpthread',
'PATH' => BUILD_ROOT_PATH . '/bin:' . getenv('PATH'),
]);

// create pkgconfig and include dir (some libs cannot create them automatically)
f_mkdir(BUILD_LIB_PATH . '/pkgconfig', recursive: true);
Expand All @@ -75,7 +72,7 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
// ---------- Update extra-libs ----------
$extra_libs = $this->getOption('extra-libs', '');
// add libc++, some extensions or libraries need it (C++ cannot be linked statically)
$extra_libs .= (empty($extra_libs) ? '' : ' ') . ($this->hasCppExtension() ? '-lc++ ' : '');
$extra_libs .= (empty($extra_libs) ? '' : ' ') . ($this->hasCpp() ? '-lc++ ' : '');
if (!$this->getOption('bloat', false)) {
$extra_libs .= (empty($extra_libs) ? '' : ' ') . implode(' ', $this->getAllStaticLibFiles());
} else {
Expand Down Expand Up @@ -115,8 +112,7 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
($enableMicro ? '--enable-micro ' : '--disable-micro ') .
$json_74 .
$zts .
$this->makeExtensionArgs() . ' ' .
$this->configure_env
$this->makeExtensionArgs()
);

SourcePatcher::patchBeforeMake($this);
Expand Down Expand Up @@ -173,12 +169,14 @@ public function buildCli(): void
/**
* Build phpmicro sapi
*
* @throws FileSystemException|RuntimeException
* @throws FileSystemException
* @throws RuntimeException
* @throws WrongUsageException
*/
public function buildMicro(): void
{
if ($this->getPHPVersionID() < 80000) {
throw new RuntimeException('phpmicro only support PHP >= 8.0!');
throw new WrongUsageException('phpmicro only support PHP >= 8.0!');
}
if ($this->getExt('phar')) {
$this->phar_patched = true;
Expand Down Expand Up @@ -228,6 +226,11 @@ public function buildFpm(): void
$this->deployBinary(BUILD_TARGET_FPM);
}

/**
* Build embed sapi
*
* @throws RuntimeException
*/
public function buildEmbed(): void
{
$vars = SystemUtil::makeEnvVarString([
Expand Down
2 changes: 1 addition & 1 deletion src/SPC/builder/freebsd/library/openssl.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function build(): void

shell()->cd($this->source_dir)
->exec(
"{$this->builder->configure_env} ./Configure no-shared {$extra} " .
"./Configure no-shared {$extra} " .
'--prefix=/ ' . // use prefix=/
"--libdir={$lib} " .
'--openssldir=/etc/ssl ' .
Expand Down
Loading

0 comments on commit 9fb5173

Please sign in to comment.