High version mac phpize is the only feasible method in the whole network

Reprint address: https://l1905.github.io/php/2020/02/28/macos-pecl-xdebug-mongodb/

Error in installing php extension for macos higher version
Published on February 28, 2020 | classified in php
Install php extension xdebug, mongodb
Analysis of error reporting in installing php extension on mac
background
Machine: mac os catalina
Version: 10.15.2
PHP: 7.3.9 (installed by default)
A PHP project relies on the mongodb extension. The prompt of composer update indicates that the mongodb extension needs to be installed through pecl. When installing the extension through pecl, it indicates that the PHP header file cannot be found. The following error is reported.

phpize 
grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:        
Zend Module Api No:     
Zend Extension Api No:

Finally, the problem is solved smoothly, and the problem analysis is recorded here

problem analysis
There are very few Chinese materials and the contents are outdated. Most of them are for the solutions under versions 10.14 and 10.13:

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

Not applicable for our version 10.15.

Some of them modify phpize directly. The phpize of the system is changed here, which is not desirable( https://bbqsoftwares.com/blog/xdebug-catalina-issue)

Here is a clear-cut solution for xdebug, which is applicable to pecl extensions that need to be compiled. Translate this article and take notes.

How to install xdebug on MacOS 10.15 Catalina
On macOS Catalina, when you try to build xdebug, the following error will be reported:

phpize 
grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:        
Zend Module Api No:     
Zend Extension Api No:
xdebug-2.9.0/xdebug.c:25:10: fatal error: 'php.h' file not found
#include "php.h"
         ^~~~~~~
1 error generated.
make: *** [xdebug.lo] Error 1
/usr/include/php/main/php.h:33:10: fatal error: 'zend.h' file
      not found
#include "zend.h"
         ^~~~~~~~
1 error generated.
make: *** [xdebug.lo] Error 1
/usr/include/php/Zend/../TSRM/TSRM.h:20:11: error: 
      'tsrm_config.h' file not found with <angled> include; use "quotes" instead
# include <tsrm_config.h>
          ^~~~~~~~~~~~~~~
          "tsrm_config.h"
....
/usr/include/php/Zend/zend_virtual_cwd.h:24:10: fatal error: 
      'TSRM.h' file not found
#include "TSRM.h"
         ^~~~~~~~
2 errors generated.
make: *** [xdebug.lo] Error 1

Configure on macOS Catalina and build xdebug
The reason for the above error is that the directory / usr/include was removed when the latest xcode11 was released on macos.

We can bypass this problem and continue compiling xdebug

First, we need to confirm that Xcode and command line tools have been properly installed, open the command line terminal, run the following commands and print the sdk path

xcrun --show-sdk-path

Theoretically, it will output

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk

If the output does not match the following command line

xcode-select --install

After installation, open xcode to ensure correct installation and reprint the sdk path

xcrun --show-sdk-path

The include directory that needs to be deleted to compile PHP extensions, so we can modify phpize and PHP config programs to use the include directory of mac os sdk. First, we copy a copy of phpize and PHP config. We modify the copy by patching (mainly without affecting the original function)

Create a new PHP private in our personal directory

mkdir ~/php-private/

Copy the programs phpize and PHP config to the PHP private directory.

cp /usr/bin/phpize ~/php-private/
cp /usr/bin/php-config ~/php-private/

View PHP version

grep version= ~/php-private/php-config

There may be the following outputs

version="7.3.11

I have patched the program in advance and downloaded these two files.

Download the phpize patch. The details of the patch are as follows:

--- /usr/bin/phpize	2019-09-11 02:46:18.000000000 +0200
+++ ./phpize	2019-12-26 23:10:32.000000000 +0100
@@ -1,11 +1,12 @@
 #!/bin/sh
 
 # Variable declaration
+XCODE_SDK_ROOT=$(/usr/bin/xcrun --show-sdk-path)
 prefix='/usr'
 datarootdir='/usr/php'
 exec_prefix="`eval echo ${prefix}`"
 phpdir="`eval echo ${exec_prefix}/lib/php`/build"
-includedir="`eval echo ${prefix}/include`/php"
+includedir="`eval echo ${XCODE_SDK_ROOT}${prefix}/include`/php"
 builddir="`pwd`"
 SED="/usr/bin/sed"

For php 7.3.9, download the PHP config patch

For 7.3.11, download the patch

The patch for PHP conf G is as follows

--- /usr/bin/php-config	2019-09-11 02:48:43.000000000 +0200
+++ ./php-config	2019-12-26 23:10:19.000000000 +0100
@@ -1,12 +1,14 @@
 #! /bin/sh
 
+XCODE_SDK_ROOT=$(/usr/bin/xcrun --show-sdk-path)
+
 SED="/usr/bin/sed"
 prefix="/usr"
 datarootdir="/usr/php"
 exec_prefix="${prefix}"
 version="7.3.9"
 vernum="70309"
-include_dir="${prefix}/include/php"
+include_dir="${XCODE_SDK_ROOT}${prefix}/include/php"
 includes="-I$include_dir -I$include_dir/main -I$include_dir/TSRM -I$include_dir/Zend -I$include_dir/ext -I$include_dir/ext/date/lib"
 ldflags=" -L$SDKROOT/usr/lib -L$SDKROOT/usr/local/libressl/lib  -L/usr/local/lib"
 libs="-lresolv  -lcrypto -lssl -lcrypto -lexslt -ltidy -lresolv -ledit -lncurses -lpq -lpq -lldap -llber -liconv -liconv -lpng -lz -ljpeg -lcrypto -lssl -lcrypto -lbz2 -lz -lcrypto -lssl -lcrypto -lm  -lxml2 -lz -licucore -lm -lkrb5 -lcurl -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lnetsnmp -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxslt -lxml2 -lz -licucore -lm "

Unzip the package

PHP 7.3.9

unzip ~/Downloads/phpize-catalina.patch.zip -d ~/Downloads/
unzip ~/Downloads/php-config-7.3.9-catalina.patch.zip -d ~/Downloads/

PHP 7.3.11

unzip ~/Downloads/phpize-catalina.patch.zip -d ~/Downloads/
unzip ~/Downloads/php-config-7.3.11-catalina.patch.zip -d ~/Downloads/

Now let's patch our copy phpize and PHP config

PHP 7.3.9

patch ~/php-private/phpize < ~/Downloads/phpize-catalina.patch
patch ~/php-private/php-config < ~/Downloads/php-config-7.3.9-catalina.patch

PHP 7.3.11

patch ~/php-private/phpize < ~/Downloads/phpize-catalina.patch
patch ~/php-private/php-config < ~/Downloads/php-config-7.3.11-catalina.patch

So far, we have prepared the preliminary work of compiling xdebug.

In the home directory, the build folder of common xdebug

mkdir ~/xdebug-install

Download Xdebug from the Xdebug official website to ~ / Downloads

cp ~/Downloads/xdebug-2.9.0.tgz ~/xdebug-install
cd ~/xdebug-install
tar -xvzf xdebug-2.9.0.tgz
cd xdebug-2.9.0

Now let's run our patch version of phpize in the xdebug directory

~/php-private/phpize

Output content, everything is normal.

Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731'

If there are no other errors reported during the execution, you can skip Zhang Jie below and go directly to the configuration and installation xdebug chapter

During the execution, you may encounter the following errors, prompting you to install some programs

Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

If phpize outputs the following, it means we need to install autoconf

Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
Execute the following command to install autoconf

#change back to our working directory
cd ~/xdebug-install

#dowload autoconf
curl -OL http://ftpmirror.gnu.org/autoconf/autoconf-2.69.tar.gz

#extract it, and change into the folder
tar xzf autoconf-2.69.tar.gz
cd autoconf-2.69

#now build and install it
./configure --prefix=/usr/local
make
sudo make install

#change back to the xdebug folder
cd ...
cd xdebug-2.9.0
Re run the patch phpize in the xdebug folder

~/php-private/phpize
Review the output again. If everything is normal, we will continue the installation process

Configuring for:
PHP Api Version: 20180731
Zend Module Api No: 20180731
Zend Extension Api No: 320180731
Configuration, build xdebug program
We can see the absolute path of the patch PHP config

echo ~/php-private/php-config
The output is as follows:

/Users/YOUR-USERNAME/php-private/php-config
Configure the above path into the following command

./configure --enable-xdebug --with-php-config=/Users/YOUR-USERNAME/php-private/php-config
Command output results, you will find that sdk path has been used correctly

...
checking for PHP prefix... /usr
checking for PHP includes... -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/php -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/php/main -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/php/TSRM -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/php/Zend -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/php/ext -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/php/ext/date/lib
checking for PHP extension directory... /usr/lib/php/extensions/no-debug-non-zts-20180731
checking for PHP installed headers prefix... /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/php
checking if debug is enabled... no
...
Now we can build the extension

make
We can't install xdebug directly by making install on macos system So, mainly because the macos sip protection system does not allow us to install xdebug in the / usr/lib/extensions directory, so we install the extension in the / usr/local directory

sudo mkdir -p /usr/local/php/extensions
sudo cp modules/xdebug.so /usr/local/php/extensions
Now let's compile PHP Ini (usually in / etc/php.ini) to load our packaged xdebug. PHP extensions default to a special extension directory to search for extensions to be installed. Because our xdebug is outside this directory, we need to specify an absolute path.

zend_extension=/usr/local/php/extensions/xdebug.so
Let's do a simple test and execute it

php -i | grep xdebug
Expected output status:

xdebug
xdebug support => enabled
...
Then we re Apache Web services to make our changes take effect

sudo apachectl restart
Reference articles
https://profilingviewer.com/installing-xdebug-on-catalina.html
https://stackoverflow.com/questions/52592548/unable-to-use-phpize-after-update-to-macos-mojave
patch command
https://bbqsoftwares.com/blog/xdebug-catalina-issue

Added by hjunw on Thu, 10 Feb 2022 08:51:32 +0200