phpenv で 8.0.x インストールするときのエラー回避

# phpenv で 8.0.x インストールするときのエラー回避

macOS Big Sur 11.2.3

## まずは、良く引っかかる BZip2 の問題

```
$ phpenv install 8.0.3
[Info]: Loaded extension plugin
[Info]: Loaded apc Plugin.
[Info]: Loaded composer Plugin.
[Info]: Loaded github Plugin.
[Info]: Loaded uprofiler Plugin.
[Info]: Loaded xdebug Plugin.
[Info]: Loaded xhprof Plugin.
[Info]: Loaded zendopcache Plugin.
[Info]: php.ini-production gets used as php.ini
[Info]: Building 8.0.3 into /Users/kaihei777/.phpenv/versions/8.0.3
[Skipping]: Already downloaded and extracted https://secure.php.net/distributions/php-8.0.3.tar.bz2
[Preparing]: /var/tmp/php-build/source/8.0.3

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
./configure: line 3700: config.nice: Interrupted system call
configure: error: Please reinstall the BZip2 distribution
-----------------------------------------

The full Log is available at '/tmp/php-build.8.0.3.20210403002410.log'.
[Warn]: Aborting build.
```

## PHP_BUILD_CONFIGURE_OPTSで回避できます。

しかし、今度はfileinfo.cでエラーが・・・

```
$ PHP_BUILD_CONFIGURE_OPTS="--with-bz2=/usr/local/opt/bzip2 --with-iconv=/usr/local/opt/libiconv"  phpenv install 8.0.3
[Info]: Loaded extension plugin
[Info]: Loaded apc Plugin.
[Info]: Loaded composer Plugin.
[Info]: Loaded github Plugin.
[Info]: Loaded uprofiler Plugin.
[Info]: Loaded xdebug Plugin.
[Info]: Loaded xhprof Plugin.
[Info]: Loaded zendopcache Plugin.
[Info]: php.ini-production gets used as php.ini
[Info]: Building 8.0.3 into /Users/kaihei777/.phpenv/versions/8.0.3
[Skipping]: Already downloaded and extracted https://secure.php.net/distributions/php-8.0.3.tar.bz2
[Preparing]: /var/tmp/php-build/source/8.0.3
[Compiling]: /var/tmp/php-build/source/8.0.3

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
                                          ^
/var/tmp/php-build/source/8.0.3/ext/fileinfo/fileinfo.c:481:25: error: implicit declaration of function 'magic_stream' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
                                                ret_val = (char *)magic_stream(magic, stream);
                                                                  ^
/var/tmp/php-build/source/8.0.3/ext/fileinfo/fileinfo.c:481:17: warning: cast to 'char *' from smaller integer type 'int' [-Wint-to-pointer-cast]
                                                ret_val = (char *)magic_stream(magic, stream);
                                                          ^
2 warnings and 2 errors generated.
make: *** [ext/fileinfo/fileinfo.lo] Error 1
make: *** Waiting for unfinished jobs....
-----------------------------------------

The full Log is available at '/tmp/php-build.8.0.3.20210403003154.log'.
[Warn]: Aborting build.
```

## 色々、調査した結果、ヘッダの参照が問題のようなので、CPPFLAGSに、isystemオプションでヘッダファイルがある場所を設定する

私の環境だとこの様なものが入ってます。

```
$ ls /usr/local/include
ares.h				lua-5.1
ares_build.h			lua5.1
ares_dns.h			lua5.4
ares_rules.h			lz4.h
ares_version.h			lz4frame.h
argon2.h			lz4frame_static.h
aspell.h			lz4hc.h
autosprintf.h			lzma
autotest.h			lzma.h
bezctx.h			lzo
bezctx_intf.h			magic.h
bkpublic.h			mcrypt.h
blake2.h			metalink
brotli				mhash.h
cairo				mutils
cspublic.h			nettle
cstypes.h			nghttp2
ctpublic.h			odbcinst.h
db.h				odbcinstext.h
db_185.h			odbcss.h
db_cxx.h			omp-tools.h
dbm.h				omp.h
dbsql.h				ompt.h
dbstl_base_iterator.h		oniggnu.h
dbstl_common.h			oniguruma.h
dbstl_container.h		openvpn-msg.h
dbstl_dbc.h			openvpn-plugin.h
dbstl_dbt.h			p11-kit-1
dbstl_element_ref.h		pango-1.0
dbstl_exception.h		pcre.h
dbstl_inner_utility.h		pcre2.h
dbstl_map.h			pcre2posix.h
dbstl_resource_manager.h	pcre_scanner.h
dbstl_set.h			pcre_stringpiece.h
dbstl_utility.h			pcrecpp.h
dbstl_vector.h			pcrecpparg.h
entities.h			pcreposix.h
ev++.h				pixman-1
ev.h				pkcs11-helper-1.0
evdns.h				png.h
event.h				pngconf.h
event2				pnglibconf.h
evhttp.h			popt.h
evrpc.h				pr29.h
evutil.h			pskc
fontconfig			pspell
freetype2			punycode.h
fribidi				rpm
gc				sodium
gc.h				sodium.h
gc_cpp.h			spiro.h
gcrypt.h			spiroentrypoints.h
gd.h				sql.h
gd_color_map.h			sqldb.h
gd_errors.h			sqlext.h
gd_io.h				sqlfront.h
gdbm-ndbm.h			sqlspi.h
gdbm.h				sqltypes.h
gdcache.h			sqlucode.h
gdfontg.h			stringprep.h
gdfontl.h			sybdb.h
gdfontmb.h			syberror.h
gdfonts.h			sybfront.h
gdfontt.h			tds_sysdep_public.h
gdfx.h				textstyle
gdpp.h				textstyle.h
gettext-po.h			tidy.h
gif_lib.h			tidybuffio.h
gio-unix-2.0			tidyenum.h
glib-2.0			tidyplatform.h
gmp.h				tiff.h
gmpxx.h				tiffconf.h
gnutls				tiffio.h
gobject-introspection-1.0	tiffio.hxx
gpg-error.h			tiffvers.h
gpgrt.h				tld.h
graphite2			unbound-event.h
guile				unbound.h
harfbuzz			unicase.h
httpd				uniconv.h
idn-free.h			unictype.h
idn-int.h			unigbrk.h
idn2.h				unilbrk.h
idna.h				uniname.h
jansson.h			uninameslist.h
jansson_config.h		uninorm.h
jconfig.h			unistdio.h
jemalloc			unistr.h
jerror.h			unistring
jmorecfg.h			unitypes.h
jpeglib.h			uniwbrk.h
jq.h				uniwidth.h
jv.h				unixodbc_conf.h
libintl.h			uodbc_extras.h
libltdl				uodbc_stats.h
liboath				webp
libpng16			xmlsec1
librtmp				yaml.h
libssh2.h			zbuff.h
libssh2_publickey.h		zdict.h
libssh2_sftp.h			zip.h
libtasn1.h			zipconf.h
ltdl.h				zstd.h
lua				zstd_errors.h
```

## 無事インストール成功

```
$ PHP_BUILD_CONFIGURE_OPTS="--with-bz2=/usr/local/opt/bzip2 --with-iconv=/usr/local/opt/libiconv" CPPFLAGS="-isystem /usr/local/include" phpenv install 8.0.3
[Info]: Loaded extension plugin
[Info]: Loaded apc Plugin.
[Info]: Loaded composer Plugin.
[Info]: Loaded github Plugin.
[Info]: Loaded uprofiler Plugin.
[Info]: Loaded xdebug Plugin.
[Info]: Loaded xhprof Plugin.
[Info]: Loaded zendopcache Plugin.
[Info]: php.ini-production gets used as php.ini
[Info]: Building 8.0.3 into /Users/kaihei777/.phpenv/versions/8.0.3
[Skipping]: Already downloaded and extracted https://secure.php.net/distributions/php-8.0.3.tar.bz2
[Preparing]: /var/tmp/php-build/source/8.0.3
[Compiling]: /var/tmp/php-build/source/8.0.3

[xdebug]: Installing version 3.0.3
[Downloading]: http://xdebug.org/files/xdebug-3.0.3.tgz
[xdebug]: Compiling xdebug in /var/tmp/php-build/source/xdebug-3.0.3
[xdebug]: Installing xdebug configuration in /Users/kaihei777/.phpenv/versions/8.0.3/etc/conf.d/xdebug.ini
[xdebug]: Cleaning up.
Makefile:245: warning: overriding commands for target `test'
Makefile:132: warning: ignoring old commands for target `test'
[Info]: Enabling Opcache...
[Info]: Done
[Info]: The Log File is not empty, but the Build did not fail. Maybe just warnings got logged. You can review the log in /tmp/php-build.8.0.3.20210403082249.log or rebuild with '--verbose' option
[Success]: Built 8.0.3 successfully.
```

## 2022/03/09 追記
gdのエラー

```
/var/tmp/php-build/source/8.0.2/ext/gd/libgd/gd_crop.c:43:12: error: conflicting types for 'gdImageCrop'
gdImagePtr gdImageCrop(gdImagePtr src, const gdRectPtr crop)
```

PHP_BUILD_CONFIGURE_OPTSに、「--with-external-gd=$(brew --prefix gd)」を追加する