Skip to content

Commit 9eb3202

Browse files
committed
Add --enable-embed=static support on Windows
PHP's Unix build already documents and accepts --enable-embed=static (see sapi/embed/config.m4) for building php<N>embed as a static library with no DLL at runtime. The Windows config.w32 ignored the static value, leaving php<N>embed.lib as a thin wrapper that still depended on php<N>.dll at runtime. When --enable-embed=static is passed on Windows: * win32/build/confutils.js generates a Makefile rule for php<N>embed .lib that links in PHP core (PHP_GLOBAL_OBJS), statically built extensions (STATIC_EXT_OBJS + STATIC_EXT_LIBS), the embed SAPI objects, and ASM_OBJS in place of the import lib (BUILD_DIR/PHPLIB). The resulting php<N>embed.lib is a self-contained static library with no runtime DLL dependency. * sapi/embed/config.w32 adds /D PHP_EXPORTS /D LIBZEND_EXPORTS /D SAPI_EXPORTS /D TSRM_EXPORTS to CFLAGS_EMBED so php_embed.c references PHP/Zend/SAPI/TSRM symbols directly instead of through __declspec(dllimport) thunks (which would produce LNK2019 with no DLL to import from), and defines PHP_EMBED_STATIC as a gate. * sapi/embed/php_embed.c skips its ZEND_TSRMLS_CACHE_DEFINE() when PHP_EMBED_STATIC is set. In static mode zend.c is in the same link unit and already defines _tsrm_ls_cache; the duplicate from php_embed.c produced LNK4006 and a corrupt binary.
1 parent 7260b27 commit 9eb3202

4 files changed

Lines changed: 30 additions & 4 deletions

File tree

NEWS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.5.8
44

5+
- Embed:
6+
. Added --enable-embed=static support on Windows: php<N>embed.lib is now
7+
produced as a self-contained static library (no php<N>.dll dependency)
8+
by linking PHP core, statically built extensions, and the embed SAPI
9+
into a single .lib, with the corresponding source-side adjustments
10+
(PHP/Zend/SAPI/TSRM export defines and TSRMLS cache deduplication).
11+
(Luther Monson)
12+
513
- GD:
614
. Fixed bug GH-22121 (Double free in gdImageSetStyle() after
715
overflow-triggered early return). (iliaal)

sapi/embed/config.w32

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,15 @@ var PHP_EMBED_PGO = false;
66

77
if (PHP_EMBED != "no") {
88
SAPI('embed', 'php_embed.c', 'php' + PHP_VERSION + 'embed.lib', '/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1');
9+
if (PHP_EMBED == "static") {
10+
// Static embed mode: php<N>embed.lib is intended to contain PHP
11+
// core, extensions and the embed SAPI in a single static library
12+
// (no php<N>.dll at runtime). php_embed.c must reach PHP/Zend/
13+
// SAPI/TSRM symbols directly rather than through dllimport thunks.
14+
ADD_FLAG('CFLAGS_EMBED',
15+
'/D PHP_EMBED_STATIC' +
16+
' /D PHP_EXPORTS /D LIBZEND_EXPORTS' +
17+
' /D SAPI_EXPORTS /D TSRM_EXPORTS');
18+
}
919
PHP_INSTALL_HEADERS("sapi/embed", "php_embed.h");
1020
}

sapi/embed/php_embed.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static const char HARDCODED_INI[] =
3030
"max_execution_time=0\n"
3131
"max_input_time=-1\n\0";
3232

33-
#if defined(PHP_WIN32) && defined(ZTS)
33+
#if defined(PHP_WIN32) && defined(ZTS) && !defined(PHP_EMBED_STATIC)
3434
ZEND_TSRMLS_CACHE_DEFINE()
3535
#endif
3636

win32/build/confutils.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,14 @@ function SAPI(sapiname, file_list, makefiletarget, cflags, obj_dir, duplicate_so
11971197
var ld;
11981198
var manifest;
11991199

1200+
// In --enable-embed=static, php<N>embed.lib must contain PHP core, all
1201+
// statically built extensions, and the embed SAPI itself - no runtime
1202+
// dependency on php<N>.dll. Substitute the PHP object groups for the
1203+
// import lib in both the dependency line and the link command.
1204+
var is_static_embed = (sapiname == "embed" && PHP_EMBED == "static");
1205+
var dep_phplib_deps = is_static_embed ? "$(PHP_GLOBAL_OBJS) $(STATIC_EXT_OBJS) $(ASM_OBJS)" : "$(BUILD_DIR)\\$(PHPLIB)";
1206+
var link_phplib_args = is_static_embed ? "$(PHP_GLOBAL_OBJS_RESP) $(STATIC_EXT_OBJS_RESP) $(ASM_OBJS) $(STATIC_EXT_LIBS)" : "$(BUILD_DIR)\\$(PHPLIB)";
1207+
12001208
if (typeof(obj_dir) == "undefined") {
12011209
sapiname_for_printing = configure_module_dirname;
12021210
} else {
@@ -1228,7 +1236,7 @@ function SAPI(sapiname, file_list, makefiletarget, cflags, obj_dir, duplicate_so
12281236
if (MODE_PHPIZE) {
12291237
MFO.WriteLine("$(BUILD_DIR)\\" + makefiletarget + ": $(DEPS_" + SAPI + ") $(" + SAPI + "_GLOBAL_OBJS) $(PHPLIB) $(BUILD_DIR)\\" + resname + " $(BUILD_DIR)\\" + manifest_name);
12301238
} else {
1231-
MFO.WriteLine("$(BUILD_DIR)\\" + makefiletarget + ": $(DEPS_" + SAPI + ") $(" + SAPI + "_GLOBAL_OBJS) $(BUILD_DIR)\\$(PHPLIB) $(BUILD_DIR)\\" + resname + " $(BUILD_DIR)\\" + manifest_name);
1239+
MFO.WriteLine("$(BUILD_DIR)\\" + makefiletarget + ": $(DEPS_" + SAPI + ") $(" + SAPI + "_GLOBAL_OBJS) " + dep_phplib_deps + " $(BUILD_DIR)\\" + resname + " $(BUILD_DIR)\\" + manifest_name);
12321240
}
12331241

12341242
var is_lib = makefiletarget.match(new RegExp("\\.lib$"));
@@ -1276,10 +1284,10 @@ function SAPI(sapiname, file_list, makefiletarget, cflags, obj_dir, duplicate_so
12761284
}
12771285
} else {
12781286
if (ld) {
1279-
MFO.WriteLine("\t" + ld + " /nologo /out:$(BUILD_DIR)\\" + makefiletarget + " " + ldflags + " $(" + SAPI + "_GLOBAL_OBJS_RESP) $(BUILD_DIR)\\$(PHPLIB) $(ARFLAGS_" + SAPI + ") $(LIBS_" + SAPI + ") $(BUILD_DIR)\\" + resname);
1287+
MFO.WriteLine("\t" + ld + " /nologo /out:$(BUILD_DIR)\\" + makefiletarget + " " + ldflags + " $(" + SAPI + "_GLOBAL_OBJS_RESP) " + link_phplib_args + " $(ARFLAGS_" + SAPI + ") $(LIBS_" + SAPI + ") $(BUILD_DIR)\\" + resname);
12801288
} else {
12811289
ld = CMD_MOD1 + '"$(LINK)"';
1282-
MFO.WriteLine("\t" + ld + " /nologo " + " $(" + SAPI + "_GLOBAL_OBJS_RESP) $(BUILD_DIR)\\$(PHPLIB) $(LIBS_" + SAPI + ") $(BUILD_DIR)\\" + resname + " /out:$(BUILD_DIR)\\" + makefiletarget + " " + ldflags + " $(LDFLAGS_" + SAPI + ")");
1290+
MFO.WriteLine("\t" + ld + " /nologo " + " $(" + SAPI + "_GLOBAL_OBJS_RESP) " + link_phplib_args + " $(LIBS_" + SAPI + ") $(BUILD_DIR)\\" + resname + " /out:$(BUILD_DIR)\\" + makefiletarget + " " + ldflags + " $(LDFLAGS_" + SAPI + ")");
12831291
}
12841292
}
12851293

0 commit comments

Comments
 (0)