Skip to content

Commit 5d19cfa

Browse files
authored
PHPC-2647, PHPC-2715: Fix all possible build configurations (standalone vs. in-tree, in-source vs. out-of-source) (#2014)
* PHPC-2647: Write generated config headers to extension build directory Use PHP_EXT_BUILDDIR as the base for all AC_CONFIG_FILES paths so that generated config headers (common-config.h, bson/config.h, mongoc-config.h, etc.) are written to the extension's build directory rather than its source directory, keeping the source tree clean for out-of-source builds. Add PHP_MONGODB_ADD_BUILD_INCLUDE to php_mongodb.m4 as the counterpart to PHP_MONGODB_ADD_INCLUDE: it adds a compiler include path relative to the extension build directory (PHP_EXT_BUILDDIR), which is needed so the compiler can find the generated config headers regardless of whether the build is in-source or out-of-source. Call PHP_MONGODB_ADD_BUILD_INCLUDE for each directory that receives a generated header via AC_CONFIG_FILES: common/src, libbson/src/bson, libmongoc/src/mongoc, and conditionally zlib-1.3.1 and libmongocrypt/src. * Test both in-source and out-of-source for in-tree builds * Correctly expand build directory before adding as include * Fail on unsupported modes * Fix comment to match matrix value (in-source, not from-source)
1 parent 8f28ee4 commit 5d19cfa

3 files changed

Lines changed: 76 additions & 40 deletions

File tree

.github/workflows/tests.yml

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,21 @@ jobs:
240240
# --enable-mongodb instead of --enable-mongodb=shared), as done by projects
241241
# like StaticPHP. This exercises the PHP in-tree build path where configure
242242
# is invoked from the PHP source root rather than the extension directory.
243-
test-static-php-build:
244-
name: "Static PHP Build"
243+
# Two modes are tested:
244+
# in-source — configure and build run from the PHP source directory
245+
# (PHP source dir = PHP build dir, layout 3)
246+
# out-of-source — configure and build run from a separate build directory
247+
# (PHP source dir ≠ PHP build dir, layout 4)
248+
test-in-tree-build:
249+
name: "Static PHP Build (${{ matrix.mode }})"
245250
runs-on: "ubuntu-latest"
246251
env:
247252
PHP_VERSION: "8.5.5"
248253

254+
strategy:
255+
matrix:
256+
mode: ['in-source', 'out-of-source']
257+
249258
steps:
250259
- name: "Checkout"
251260
uses: "actions/checkout@v6"
@@ -276,24 +285,38 @@ jobs:
276285
- name: "Generate PHP build scripts"
277286
run: cd "$GITHUB_WORKSPACE/php-src" && ./buildconf --force
278287

288+
- name: "Set build directory"
289+
run: |
290+
if [ "${{ matrix.mode }}" = "out-of-source" ]; then
291+
mkdir -p /tmp/php-build
292+
echo "PHP_BUILD_DIR=/tmp/php-build" >> "$GITHUB_ENV"
293+
elif [ "${{ matrix.mode }}" = "in-source" ]; then
294+
echo "PHP_BUILD_DIR=$GITHUB_WORKSPACE/php-src" >> "$GITHUB_ENV"
295+
else
296+
echo "Invalid mode"
297+
exit 1
298+
fi
299+
279300
# Note: --with-openssl is needed for the bundled libmongoc's TLS support.
280301
- name: "Configure PHP with MongoDB extension (static)"
281302
run: |
282-
cd "$GITHUB_WORKSPACE/php-src"
283-
./configure \
303+
[ -n "$PHP_BUILD_DIR" ] || { echo "PHP_BUILD_DIR is not set"; exit 1; }
304+
"$GITHUB_WORKSPACE/php-src/configure" \
284305
--enable-debug \
285306
--enable-cli \
286307
--enable-mongodb \
287308
--enable-mongodb-developer-flags \
288309
--with-openssl
310+
working-directory: ${{ env.PHP_BUILD_DIR }}
289311

290312
- name: "Build PHP"
291-
run: cd "$GITHUB_WORKSPACE/php-src" && make -j$(nproc)
313+
run: make -j$(nproc)
314+
working-directory: ${{ env.PHP_BUILD_DIR }}
292315

293316
- name: "Verify MongoDB extension is compiled in"
294317
run: |
295-
"$GITHUB_WORKSPACE/php-src/sapi/cli/php" -m | grep -ix mongodb
296-
"$GITHUB_WORKSPACE/php-src/sapi/cli/php" --ri mongodb
318+
"$PHP_BUILD_DIR/sapi/cli/php" -m | grep -ix mongodb
319+
"$PHP_BUILD_DIR/sapi/cli/php" --ri mongodb
297320
298321
test-out-of-source-build:
299322
name: "Out-of-source Build"

config.m4

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -374,27 +374,6 @@ if test "$PHP_MONGODB" != "no"; then
374374
PHP_MONGODB_ADD_SOURCES([src/libmongoc/src/libbson/src/jsonsl/], $PHP_MONGODB_JSONSL_SOURCES, $PHP_MONGODB_BUNDLED_CFLAGS)
375375
PHP_MONGODB_ADD_SOURCES([src/libmongoc/src/libmongoc/src/mongoc/], $PHP_MONGODB_MONGOC_SOURCES, $PHP_MONGODB_BUNDLED_CFLAGS)
376376

377-
dnl TODO: Use $ext_builddir/$ext_srcdir once this block can move after PHP_NEW_EXTENSION.
378-
dnl $ext_builddir and $ext_srcdir are set by PHP_NEW_EXTENSION, but PHP_ADD_INCLUDE
379-
dnl and PHP_MONGODB_ADD_SOURCES calls must precede PHP_NEW_EXTENSION, so the whole
380-
dnl block cannot be deferred. PHP_EXT_BUILDDIR/PHP_EXT_SRCDIR are M4 macros that
381-
dnl are always available and provide equivalent values without that constraint.
382-
dnl
383-
dnl PHP_EXT_BUILDDIR expands to the extension's directory relative to the build
384-
dnl root: "ext/mongodb" for PHP in-tree builds, "." for phpize builds. Pairing
385-
dnl it with $abs_builddir (the absolute path to wherever configure was invoked)
386-
dnl produces the correct absolute path to the generated config headers regardless
387-
dnl of build layout. Using $PWD here breaks PHP in-tree builds because $PWD is
388-
dnl the PHP source/build root, not the extension's subdirectory within it.
389-
php_mongodb_ext_builddir=PHP_EXT_BUILDDIR(mongodb)
390-
php_mongodb_ext_srcdir=PHP_EXT_SRCDIR(mongodb)
391-
392-
dnl Add the build directories as include paths so the compiler finds generated
393-
dnl config headers (common-config.h, bson/config.h, mongoc-config.h, etc.).
394-
PHP_ADD_INCLUDE([$abs_builddir/$php_mongodb_ext_builddir/src/libmongoc/src/common/src])
395-
PHP_ADD_INCLUDE([$abs_builddir/$php_mongodb_ext_builddir/src/libmongoc/src/libbson/src])
396-
PHP_ADD_INCLUDE([$abs_builddir/$php_mongodb_ext_builddir/src/libmongoc/src/libmongoc/src])
397-
398377
PHP_MONGODB_ADD_INCLUDE([src/libmongoc/src/common/src/])
399378
PHP_MONGODB_ADD_INCLUDE([src/libmongoc/src/uthash/])
400379
PHP_MONGODB_ADD_INCLUDE([src/libmongoc/src/libbson/src/])
@@ -415,17 +394,26 @@ if test "$PHP_MONGODB" != "no"; then
415394
PHP_MONGODB_ADD_BUILD_DIR([src/libmongoc/src/kms-message/src/])
416395
fi
417396

418-
dnl Write generated config headers into the extension's build directory
419-
dnl (${php_mongodb_ext_builddir}/... relative to the configure invocation directory).
420-
dnl For standalone out-of-source builds this stays in the build tree; for
421-
dnl PHP in-tree builds it lands under ext/mongodb/ rather than the PHP root.
397+
dnl PHP_EXT_BUILDDIR expands to "ext/mongodb" for PHP in-tree builds and
398+
dnl "." for phpize builds. Captured as a shell variable here because
399+
dnl AC_CONFIG_FILES paths are expanded at configure run time, not at M4
400+
dnl processing time.
401+
mongodb_builddir=PHP_EXT_BUILDDIR(mongodb)
402+
403+
dnl Generated config headers are written into the extension build directory.
404+
dnl For standalone out-of-source builds they stay in the build tree; for PHP
405+
dnl in-tree builds they land under ext/mongodb/ rather than the PHP root.
406+
PHP_MONGODB_ADD_BUILD_INCLUDE([src/libmongoc/src/common/src/])
407+
PHP_MONGODB_ADD_BUILD_INCLUDE([src/libmongoc/src/libbson/src/])
408+
PHP_MONGODB_ADD_BUILD_INCLUDE([src/libmongoc/src/libmongoc/src/])
409+
422410
AC_CONFIG_FILES([
423-
${php_mongodb_ext_srcdir}/src/libmongoc/src/common/src/common-config.h
424-
${php_mongodb_ext_srcdir}/src/libmongoc/src/libbson/src/bson/config.h
425-
${php_mongodb_ext_srcdir}/src/libmongoc/src/libbson/src/bson/version.h
426-
${php_mongodb_ext_srcdir}/src/libmongoc/src/libmongoc/src/mongoc/mongoc-config.h
427-
${php_mongodb_ext_srcdir}/src/libmongoc/src/libmongoc/src/mongoc/mongoc-config-private.h
428-
${php_mongodb_ext_srcdir}/src/libmongoc/src/libmongoc/src/mongoc/mongoc-version.h
411+
${mongodb_builddir}/src/libmongoc/src/common/src/common-config.h
412+
${mongodb_builddir}/src/libmongoc/src/libbson/src/bson/config.h
413+
${mongodb_builddir}/src/libmongoc/src/libbson/src/bson/version.h
414+
${mongodb_builddir}/src/libmongoc/src/libmongoc/src/mongoc/mongoc-config.h
415+
${mongodb_builddir}/src/libmongoc/src/libmongoc/src/mongoc/mongoc-config-private.h
416+
${mongodb_builddir}/src/libmongoc/src/libmongoc/src/mongoc/mongoc-version.h
429417
])
430418

431419
if test "x$bundled_utf8proc" = "xyes"; then
@@ -440,7 +428,8 @@ if test "$PHP_MONGODB" != "no"; then
440428
PHP_MONGODB_ADD_SOURCES([src/libmongoc/src/zlib-1.3.1/], $PHP_MONGODB_ZLIB_SOURCES, $PHP_MONGODB_ZLIB_CFLAGS)
441429
PHP_MONGODB_ADD_INCLUDE([src/libmongoc/src/zlib-1.3.1/])
442430
PHP_MONGODB_ADD_BUILD_DIR([src/libmongoc/src/zlib-1.3.1/])
443-
AC_CONFIG_FILES([${php_mongodb_ext_srcdir}/src/libmongoc/src/zlib-1.3.1/zconf.h])
431+
PHP_MONGODB_ADD_BUILD_INCLUDE([src/libmongoc/src/zlib-1.3.1/])
432+
AC_CONFIG_FILES([${mongodb_builddir}/src/libmongoc/src/zlib-1.3.1/zconf.h])
444433
fi
445434

446435
if test "$PHP_MONGODB_CLIENT_SIDE_ENCRYPTION" = "yes"; then
@@ -478,8 +467,10 @@ if test "$PHP_MONGODB" != "no"; then
478467
PHP_MONGODB_ADD_BUILD_DIR([src/libmongocrypt/src/unicode/])
479468
PHP_MONGODB_ADD_BUILD_DIR([src/libmongocrypt/kms-message/src/])
480469

470+
PHP_MONGODB_ADD_BUILD_INCLUDE([src/libmongocrypt/src/])
471+
481472
AC_CONFIG_FILES([
482-
${php_mongodb_ext_srcdir}/src/libmongocrypt/src/mongocrypt-config.h
473+
${mongodb_builddir}/src/libmongocrypt/src/mongocrypt-config.h
483474
])
484475
fi
485476
fi

scripts/autotools/m4/php_mongodb.m4

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ AC_DEFUN([PHP_MONGODB_ADD_INCLUDE],[
4040
PHP_ADD_INCLUDE(PHP_EXT_SRCDIR(mongodb)[/][$1])
4141
])
4242

43+
dnl
44+
dnl PHP_MONGODB_ADD_BUILD_INCLUDE(path)
45+
dnl
46+
dnl Adds an include path relative to the extension build directory (i.e.
47+
dnl PHP_EXT_BUILDDIR). Use this for directories containing generated files
48+
dnl (e.g. config headers written by AC_CONFIG_FILES).
49+
dnl
50+
dnl PHP_EXT_BUILDDIR returns a path relative to the configure invocation
51+
dnl directory (e.g. "." for phpize builds, "ext/mongodb" for in-tree builds).
52+
dnl The subdirectory passed as $1 does not exist at configure time (build dirs
53+
dnl are created in AC_CONFIG_COMMANDS_PRE), so passing the full relative path
54+
dnl to PHP_ADD_INCLUDE would cause PHP_EXPAND_PATH to silently discard it when
55+
dnl the "cd $path && pwd" probe fails. To avoid this, we resolve only the base
56+
dnl (PHP_EXT_BUILDDIR, which always exists at configure time) to an absolute
57+
dnl path first, then append $1. PHP_ADD_INCLUDE skips the cd-probe for paths
58+
dnl that already start with "/".
59+
dnl
60+
AC_DEFUN([PHP_MONGODB_ADD_BUILD_INCLUDE],[
61+
PHP_EXPAND_PATH(PHP_EXT_BUILDDIR(mongodb), php_mongodb_abs_builddir)
62+
PHP_ADD_INCLUDE([$php_mongodb_abs_builddir/$1])
63+
])
64+
4365
dnl
4466
dnl PHP_MONGODB_ADD_BUILD_DIR(path)
4567
dnl

0 commit comments

Comments
 (0)