Skip to content

Commit e7d6f37

Browse files
authored
Fix windows xlswriter duplicate md5 symbol bug (crazywhalecc#719)
* Add missing SOURCE_PATH before making cmake toolchain * Fix windows xlswriter duplicate md5 symbol bug * Add detection for patchFile to prevent duplicate patches * Add tests
1 parent f755d66 commit e7d6f37

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

src/SPC/builder/extension/xlswriter.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SPC\builder\extension;
66

77
use SPC\builder\Extension;
8+
use SPC\store\SourcePatcher;
89
use SPC\util\CustomExt;
910

1011
#[CustomExt('xlswriter')]
@@ -27,13 +28,14 @@ public function getWindowsConfigureArg(): string
2728
public function patchBeforeMake(): bool
2829
{
2930
if (PHP_OS_FAMILY === 'Windows') {
31+
// fix windows build with openssl extension duplicate symbol bug
32+
SourcePatcher::patchFile('spc_fix_xlswriter_win32.patch', $this->source_dir);
3033
$content = file_get_contents($this->source_dir . '/library/libxlsxwriter/src/theme.c');
3134
$bom = pack('CCC', 0xEF, 0xBB, 0xBF);
3235
if (substr($content, 0, 3) !== $bom) {
33-
file_put_contents($this->source_dir . '/library/libxlsxwriter/src/theme.c', $content);
34-
return true;
36+
file_put_contents($this->source_dir . '/library/libxlsxwriter/src/theme.c', $bom . $content);
3537
}
36-
return false;
38+
return true;
3739
}
3840
return false;
3941
}

src/SPC/builder/windows/SystemUtil.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ public static function makeCmakeToolchainFile(?string $cflags = null, ?string $l
9999
SET(CMAKE_FIND_ROOT_PATH "{$buildroot}")
100100
SET(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
101101
CMAKE;
102+
if (!is_dir(SOURCE_PATH)) {
103+
FileSystem::createDir(SOURCE_PATH);
104+
}
102105
FileSystem::writeFile(SOURCE_PATH . '\toolchain.cmake', $toolchain);
103106
return realpath(SOURCE_PATH . '\toolchain.cmake');
104107
}

src/SPC/store/SourcePatcher.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,30 @@ public static function patchFile(string $patch_name, string $cwd, bool $reverse
181181
$patch_file = ROOT_DIR . "/src/globals/patch/{$patch_name}";
182182
$patch_str = str_replace('/', DIRECTORY_SEPARATOR, $patch_file);
183183

184-
// copy patch from phar
184+
// Copy patch from phar
185185
if (\Phar::running() !== '') {
186186
file_put_contents(SOURCE_PATH . '/' . $patch_name, file_get_contents($patch_file));
187187
$patch_str = str_replace('/', DIRECTORY_SEPARATOR, SOURCE_PATH . '/' . $patch_name);
188188
}
189189

190-
f_passthru(
191-
'cd ' . $cwd . ' && ' .
192-
(PHP_OS_FAMILY === 'Windows' ? 'type' : 'cat') . ' ' . $patch_str . ' | patch -p1 ' . ($reverse ? '-R' : '')
193-
);
190+
// detect
191+
$detect_reverse = !$reverse;
192+
$detect_cmd = 'cd ' . escapeshellarg($cwd) . ' && '
193+
. (PHP_OS_FAMILY === 'Windows' ? 'type' : 'cat') . ' ' . escapeshellarg($patch_str)
194+
. ' | patch --dry-run -p1 -s -f ' . ($detect_reverse ? '-R' : '')
195+
. ' > ' . (PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null') . ' 2>&1';
196+
exec($detect_cmd, $output, $detect_status);
197+
198+
if ($detect_status === 0) {
199+
return true;
200+
}
201+
202+
// apply patch
203+
$apply_cmd = 'cd ' . escapeshellarg($cwd) . ' && '
204+
. (PHP_OS_FAMILY === 'Windows' ? 'type' : 'cat') . ' ' . escapeshellarg($patch_str)
205+
. ' | patch -p1 ' . ($reverse ? '-R' : '');
206+
207+
f_passthru($apply_cmd);
194208
return true;
195209
}
196210

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
diff --git a/library/libxlsxwriter/third_party/md5/md5.c b/library/libxlsxwriter/third_party/md5/md5.c
2+
index b235e17..ce98e18 100644
3+
--- a/library/libxlsxwriter/third_party/md5/md5.c
4+
+++ b/library/libxlsxwriter/third_party/md5/md5.c
5+
@@ -35,7 +35,11 @@
6+
* compile-time configuration.
7+
*/
8+
9+
-#ifndef HAVE_OPENSSL
10+
+#ifdef PHP_WIN32
11+
+#include "config.w32.h"
12+
+#endif
13+
+
14+
+#ifndef HAVE_OPENSSL_EXT
15+
16+
#include <string.h>
17+
18+
diff --git a/library/libxlsxwriter/third_party/md5/md5.h b/library/libxlsxwriter/third_party/md5/md5.h
19+
index 2da44bf..3cb0a98 100644
20+
--- a/library/libxlsxwriter/third_party/md5/md5.h
21+
+++ b/library/libxlsxwriter/third_party/md5/md5.h
22+
@@ -23,7 +23,7 @@
23+
* See md5.c for more information.
24+
*/
25+
26+
-#ifdef HAVE_OPENSSL
27+
+#ifdef HAVE_OPENSSL_EXT
28+
#include <openssl/md5.h>
29+
#elif !defined(_MD5_H)
30+
#define _MD5_H

src/globals/test-extensions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
// If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`).
4747
$extensions = match (PHP_OS_FAMILY) {
4848
'Linux', 'Darwin' => 'pgsql',
49-
'Windows' => 'xlswriter',
49+
'Windows' => 'xlswriter,openssl',
5050
};
5151

5252
// If you want to test shared extensions, add them below (comma separated, example `bcmath,openssl`).

0 commit comments

Comments
 (0)