Skip to content

Commit 6108433

Browse files
authored
Merge pull request #681 from crazywhalecc/fix/remove-libgomp
Remove openmp support for imagemagick
2 parents 2f3c71e + 615e680 commit 6108433

File tree

9 files changed

+45
-17
lines changed

9 files changed

+45
-17
lines changed

config/ext.json

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@
253253
"Windows": "wip",
254254
"BSD": "wip"
255255
},
256+
"notes": true,
256257
"type": "external",
257258
"source": "ext-imagick",
258259
"arg-type": "custom",

config/lib.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,12 @@
210210
"libwebp",
211211
"freetype",
212212
"libtiff",
213-
"libheif"
213+
"libheif",
214+
"bzip2"
214215
],
215216
"lib-suggests": [
216217
"zstd",
217218
"xz",
218-
"bzip2",
219219
"libzip",
220220
"libxml2"
221221
]

docs/en/guide/extension-notes.md

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ This extension contains an implementation of the coroutine environment for `pdo_
4848

4949
1. Only PHP 8.0 ~ 8.4 is supported.
5050

51+
## imagick
52+
53+
1. The imagick extension currently only has openmp support on musl libc. This means that multithreading is disabled on glibc or other operating systems. The extension is still fully functional.
54+
5155
## imap
5256

5357
1. Kerberos is not supported

docs/zh/guide/extension-notes.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ swoole-hook-sqlite 与 `pdo_sqlite` 扩展冲突。如需使用 Swoole 和 `pdo_
4545

4646
1. swow 仅支持 PHP 8.0 ~ 8.4 版本。
4747

48+
## imagick
49+
50+
imagick 扩展目前仅在 musl libc 上支持 OpenMP(libgomp)。使用 glibc 方式构建的 imagick 扩展无法支持多线程特性。
51+
4852
## imap
4953

5054
1. 该扩展目前不支持 Kerberos。
@@ -141,4 +145,4 @@ parallel 扩展只支持 PHP 8.0 及以上版本,并只支持 ZTS 构建(`--
141145

142146
1. 从技术上讲,这不是扩展,而是一个库。
143147
2. 在 Linux 或 macOS 上使用 `--with-libs="mimalloc"` 进行构建将覆盖默认分配器。
144-
3. 目前,这还处于实验阶段,但建议在线程环境中使用。
148+
3. 目前,这还处于实验阶段,但建议在线程环境中使用。

src/SPC/builder/extension/imagick.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55
namespace SPC\builder\extension;
66

77
use SPC\builder\Extension;
8-
use SPC\builder\linux\LinuxBuilder;
98
use SPC\util\CustomExt;
109

1110
#[CustomExt('imagick')]
1211
class imagick extends Extension
1312
{
1413
public function patchBeforeMake(): bool
1514
{
16-
// imagick may call omp_pause_all which requires -lgomp
17-
$extra_libs = getenv('SPC_EXTRA_LIBS') ?: '';
18-
if ($this->builder instanceof LinuxBuilder) {
19-
$extra_libs .= (empty($extra_libs) ? '' : ' ') . '-lgomp ';
15+
if (getenv('SPC_LIBC') !== 'musl') {
16+
return false;
2017
}
18+
// imagick with calls omp_pause_all which requires -lgomp, on non-musl we build imagick without openmp
19+
$extra_libs = trim(getenv('SPC_EXTRA_LIBS') . ' -lgomp');
2120
f_putenv('SPC_EXTRA_LIBS=' . $extra_libs);
2221
return true;
2322
}
2423

2524
public function getUnixConfigureArg(): string
2625
{
27-
return '--with-imagick=' . BUILD_ROOT_PATH;
26+
$disable_omp = getenv('SPC_LIBC') === 'musl' ? '' : ' ac_cv_func_omp_pause_resource_all=no';
27+
return '--with-imagick=' . BUILD_ROOT_PATH . $disable_omp;
2828
}
2929
}

src/SPC/builder/unix/library/imagemagick.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ trait imagemagick
1818
*/
1919
protected function build(): void
2020
{
21-
// TODO: imagemagick build with bzip2 failed with bugs, we need to fix it in the future
22-
$extra = '--without-jxl --without-x --enable-openmp --without-bzlib ';
21+
// TODO: glibc rh 10 toolset's libgomp.a was built without -fPIC -fPIE so we can't use openmp without depending on libgomp.so
22+
$openmp = getenv('SPC_LIBC') === 'musl' ? '--enable-openmp' : '--disable-openmp';
23+
$extra = "--without-jxl --without-x {$openmp} ";
2324
$required_libs = '';
2425
$optional_libs = [
2526
'libzip' => 'zip',
@@ -31,6 +32,7 @@ protected function build(): void
3132
'xz' => 'lzma',
3233
'zstd' => 'zstd',
3334
'freetype' => 'freetype',
35+
'bzip2' => 'bzlib',
3436
];
3537
foreach ($optional_libs as $lib => $option) {
3638
$extra .= $this->builder->getLib($lib) ? "--with-{$option} " : "--without-{$option} ";

src/SPC/store/FileSystem.php

+17
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,23 @@ public static function removeFileIfExists(string $string): void
461461
}
462462
}
463463

464+
/**
465+
* @throws FileSystemException
466+
*/
467+
public static function replaceFileLineContainsString(string $file, string $find, string $line): false|int
468+
{
469+
$lines = file($file);
470+
if ($lines === false) {
471+
throw new FileSystemException('Cannot read file: ' . $file);
472+
}
473+
foreach ($lines as $key => $value) {
474+
if (str_contains($value, $find)) {
475+
$lines[$key] = $line . PHP_EOL;
476+
}
477+
}
478+
return file_put_contents($file, implode('', $lines));
479+
}
480+
464481
/**
465482
* @throws RuntimeException
466483
* @throws FileSystemException

src/SPC/util/SPCConfigUtil.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ private function getLibsString(array $libraries): string
9090
}
9191
}
9292
}
93-
// patch: imagick (imagemagick wrapper) for linux needs -lgomp
94-
if (in_array('imagemagick', $libraries) && PHP_OS_FAMILY === 'Linux') {
93+
// patch: imagick (imagemagick wrapper) for linux needs libgomp
94+
if (in_array('imagemagick', $libraries) && PHP_OS_FAMILY === 'Linux' && getenv('SPC_LIBC') === 'musl') {
9595
$short_name[] = '-lgomp';
9696
}
9797
return implode(' ', $short_name);

src/globals/test-extensions.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
// test os (macos-13, macos-14, ubuntu-latest, windows-latest are available)
2323
$test_os = [
24-
// 'macos-13',
25-
// 'macos-14',
24+
'macos-13',
25+
'macos-14',
2626
'ubuntu-latest',
2727
'ubuntu-22.04',
2828
'ubuntu-22.04-arm',
@@ -35,10 +35,10 @@
3535
$no_strip = false;
3636

3737
// compress with upx
38-
$upx = true;
38+
$upx = false;
3939

4040
// prefer downloading pre-built packages to speed up the build process
41-
$prefer_pre_built = true;
41+
$prefer_pre_built = false;
4242

4343
// If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`).
4444
$extensions = match (PHP_OS_FAMILY) {

0 commit comments

Comments
 (0)