diff --git a/.gitignore b/.gitignore index a62ad82..f603f73 100644 --- a/.gitignore +++ b/.gitignore @@ -15,7 +15,9 @@ /c/configure /c/configure.ac /c/libtool +/c/main.dep /c/main.lo +/c/Makefile /c/Makefile.fragments /c/Makefile.objects /c/my_module.la diff --git a/Makefile b/Makefile index 56d0189..6602e14 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,51 @@ +CONFIGURE_ARGS := + +all: cgo c + +.PHONY: clean +clean: + /bin/rm -rfv \ + c/.libs \ + c/autom4te.cache \ + c/build \ + c/include \ + c/modules + + /bin/rm -fv \ + c/config.h \ + c/config.h.in \ + c/config.log \ + c/config.nice \ + c/config.status \ + c/configure \ + c/configure.ac \ + c/libtool \ + c/main.dep \ + c/main.lo \ + c/Makefile \ + c/Makefile.fragments \ + c/Makefile.objects \ + c/my_module.la \ + c/run-tests.php \ + cgo/libmymodule.h \ + cgo/libmymodule.so \ + .PHONY: cgo cgo: cd ./cgo && go build -o libmymodule.so -buildmode=c-shared ./main.go +c/configure: + cd ./c && phpize + +c/Makefile: c/configure + cd ./c && ./configure $(CONFIGURE_ARGS) + .PHONY: c -c: +c: c/configure c/Makefile cd ./c && EXTRA_CFLAGS="-I`pwd`/../cgo -L`pwd`/../cgo -lmymodule" make - cp ./c/.libs/my_module.so /usr/local/lib/php/extensions/no-debug-non-zts-20200930/ + +install: cgo c + cd ./c && make install .PHONY: test test: diff --git a/README.md b/README.md index 7c61899..07276c1 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,7 @@ $ go mod tidy ## Build ``` -$ make cgo -$ make c +$ make ``` ## Run diff --git a/c/Makefile b/c/Makefile deleted file mode 100644 index 3041c00..0000000 --- a/c/Makefile +++ /dev/null @@ -1,214 +0,0 @@ -srcdir = /usr/src/workspace/c -builddir = /usr/src/workspace/c -top_srcdir = /usr/src/workspace/c -top_builddir = /usr/src/workspace/c -EGREP = /bin/grep -E -SED = /bin/sed -CONFIGURE_COMMAND = './configure' '--enable-my-module' -CONFIGURE_OPTIONS = '--enable-my-module' -SHLIB_SUFFIX_NAME = so -SHLIB_DL_SUFFIX_NAME = so -AWK = nawk -shared_objects_my_module = main.lo -PHP_PECL_EXTENSION = my_module -PHP_MODULES = $(phplibdir)/my_module.la -PHP_ZEND_EX = -all_targets = $(PHP_MODULES) $(PHP_ZEND_EX) -install_targets = install-modules install-headers -prefix = /usr/local -exec_prefix = $(prefix) -libdir = ${exec_prefix}/lib -prefix = /usr/local -phplibdir = /usr/src/workspace/c/modules -phpincludedir = /usr/local/include/php -CC = cc -CFLAGS = -g -O2 -CFLAGS_CLEAN = $(CFLAGS) -CPP = cc -E -CPPFLAGS = -DHAVE_CONFIG_H -CXX = -CXXFLAGS = -CXXFLAGS_CLEAN = $(CXXFLAGS) -EXTENSION_DIR = /usr/local/lib/php/extensions/no-debug-non-zts-20200930 -PHP_EXECUTABLE = /usr/local/bin/php -EXTRA_LDFLAGS = -EXTRA_LIBS = -INCLUDES = -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib -LFLAGS = -LDFLAGS = -SHARED_LIBTOOL = -LIBTOOL = $(SHELL) $(top_builddir)/libtool -SHELL = /bin/bash -INSTALL_HEADERS = -mkinstalldirs = $(top_srcdir)/build/shtool mkdir -p -INSTALL = $(top_srcdir)/build/shtool install -c -INSTALL_DATA = $(INSTALL) -m 644 - -DEFS = -I$(top_builddir)/include -I$(top_builddir)/main -I$(top_srcdir) -COMMON_FLAGS = $(DEFS) $(INCLUDES) $(EXTRA_INCLUDES) $(CPPFLAGS) $(PHP_FRAMEWORKPATH) - -all: $(all_targets) - @echo - @echo "Build complete." - @echo "Don't forget to run 'make test'." - @echo - -build-modules: $(PHP_MODULES) $(PHP_ZEND_EX) - -build-binaries: $(PHP_BINARIES) - -libphp.la: $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS) - $(LIBTOOL) --mode=link $(CC) $(LIBPHP_CFLAGS) $(CFLAGS) $(EXTRA_CFLAGS) -rpath $(phptempdir) $(EXTRA_LDFLAGS) $(LDFLAGS) $(PHP_RPATHS) $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS) $(EXTRA_LIBS) $(ZEND_EXTRA_LIBS) -o $@ - -@$(LIBTOOL) --silent --mode=install cp $@ $(phptempdir)/$@ >/dev/null 2>&1 - -libs/libphp.bundle: $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS) - $(CC) $(MH_BUNDLE_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) $(PHP_GLOBAL_OBJS:.lo=.o) $(PHP_SAPI_OBJS:.lo=.o) $(PHP_FRAMEWORKS) $(EXTRA_LIBS) $(ZEND_EXTRA_LIBS) -o $@ && cp $@ libs/libphp.so - -install: $(all_targets) $(install_targets) - -install-sapi: $(OVERALL_TARGET) - @echo "Installing PHP SAPI module: $(PHP_SAPI)" - -@$(mkinstalldirs) $(INSTALL_ROOT)$(bindir) - -@if test ! -r $(phptempdir)/libphp.$(SHLIB_DL_SUFFIX_NAME); then \ - for i in 0.0.0 0.0 0; do \ - if test -r $(phptempdir)/libphp.$(SHLIB_DL_SUFFIX_NAME).$$i; then \ - $(LN_S) $(phptempdir)/libphp.$(SHLIB_DL_SUFFIX_NAME).$$i $(phptempdir)/libphp.$(SHLIB_DL_SUFFIX_NAME); \ - break; \ - fi; \ - done; \ - fi - @$(INSTALL_IT) - -install-binaries: build-binaries $(install_binary_targets) - -install-modules: build-modules - @test -d modules && \ - $(mkinstalldirs) $(INSTALL_ROOT)$(EXTENSION_DIR) - @echo "Installing shared extensions: $(INSTALL_ROOT)$(EXTENSION_DIR)/" - @rm -f modules/*.la >/dev/null 2>&1 - @$(INSTALL) modules/* $(INSTALL_ROOT)$(EXTENSION_DIR) - -install-headers: - -@if test "$(INSTALL_HEADERS)"; then \ - for i in `echo $(INSTALL_HEADERS)`; do \ - i=`$(top_srcdir)/build/shtool path -d $$i`; \ - paths="$$paths $(INSTALL_ROOT)$(phpincludedir)/$$i"; \ - done; \ - $(mkinstalldirs) $$paths && \ - echo "Installing header files: $(INSTALL_ROOT)$(phpincludedir)/" && \ - for i in `echo $(INSTALL_HEADERS)`; do \ - if test "$(PHP_PECL_EXTENSION)"; then \ - src=`echo $$i | $(SED) -e "s#ext/$(PHP_PECL_EXTENSION)/##g"`; \ - else \ - src=$$i; \ - fi; \ - if test -f "$(top_srcdir)/$$src"; then \ - $(INSTALL_DATA) $(top_srcdir)/$$src $(INSTALL_ROOT)$(phpincludedir)/$$i; \ - elif test -f "$(top_builddir)/$$src"; then \ - $(INSTALL_DATA) $(top_builddir)/$$src $(INSTALL_ROOT)$(phpincludedir)/$$i; \ - else \ - (cd $(top_srcdir)/$$src && $(INSTALL_DATA) *.h $(INSTALL_ROOT)$(phpincludedir)/$$i; \ - cd $(top_builddir)/$$src && $(INSTALL_DATA) *.h $(INSTALL_ROOT)$(phpincludedir)/$$i) 2>/dev/null || true; \ - fi \ - done; \ - fi - -PHP_TEST_SETTINGS = -d 'open_basedir=' -d 'output_buffering=0' -d 'memory_limit=-1' -PHP_TEST_SHARED_EXTENSIONS = ` \ - if test "x$(PHP_MODULES)" != "x"; then \ - for i in $(PHP_MODULES)""; do \ - . $$i; $(top_srcdir)/build/shtool echo -n -- " -d extension=$$dlname"; \ - done; \ - fi; \ - if test "x$(PHP_ZEND_EX)" != "x"; then \ - for i in $(PHP_ZEND_EX)""; do \ - . $$i; $(top_srcdir)/build/shtool echo -n -- " -d zend_extension=$(top_builddir)/modules/$$dlname"; \ - done; \ - fi` -PHP_DEPRECATED_DIRECTIVES_REGEX = '^(magic_quotes_(gpc|runtime|sybase)?|(zend_)?extension(_debug)?(_ts)?)[\t\ ]*=' - -test: all - @if test ! -z "$(PHP_EXECUTABLE)" && test -x "$(PHP_EXECUTABLE)"; then \ - INI_FILE=`$(PHP_EXECUTABLE) -d 'display_errors=stderr' -r 'echo php_ini_loaded_file();' 2> /dev/null`; \ - if test "$$INI_FILE"; then \ - $(EGREP) -h -v $(PHP_DEPRECATED_DIRECTIVES_REGEX) "$$INI_FILE" > $(top_builddir)/tmp-php.ini; \ - else \ - echo > $(top_builddir)/tmp-php.ini; \ - fi; \ - INI_SCANNED_PATH=`$(PHP_EXECUTABLE) -d 'display_errors=stderr' -r '$$a = explode(",\n", trim(php_ini_scanned_files())); echo $$a[0];' 2> /dev/null`; \ - if test "$$INI_SCANNED_PATH"; then \ - INI_SCANNED_PATH=`$(top_srcdir)/build/shtool path -d $$INI_SCANNED_PATH`; \ - $(EGREP) -h -v $(PHP_DEPRECATED_DIRECTIVES_REGEX) "$$INI_SCANNED_PATH"/*.ini >> $(top_builddir)/tmp-php.ini; \ - fi; \ - TEST_PHP_EXECUTABLE=$(PHP_EXECUTABLE) \ - TEST_PHP_SRCDIR=$(top_srcdir) \ - CC="$(CC)" \ - $(PHP_EXECUTABLE) -n -c $(top_builddir)/tmp-php.ini $(PHP_TEST_SETTINGS) $(top_srcdir)/run-tests.php -n -c $(top_builddir)/tmp-php.ini -d extension_dir=$(top_builddir)/modules/ $(PHP_TEST_SHARED_EXTENSIONS) $(TESTS); \ - TEST_RESULT_EXIT_CODE=$$?; \ - rm $(top_builddir)/tmp-php.ini; \ - exit $$TEST_RESULT_EXIT_CODE; \ - else \ - echo "ERROR: Cannot run tests without CLI sapi."; \ - fi - -clean: - find . -name \*.gcno -o -name \*.gcda | xargs rm -f - find . -name \*.lo -o -name \*.o | xargs rm -f - find . -name \*.la -o -name \*.a | xargs rm -f - find . -name \*.so | xargs rm -f - find . -name .libs -a -type d|xargs rm -rf - rm -f libphp.la $(SAPI_CLI_PATH) $(SAPI_CGI_PATH) $(SAPI_LITESPEED_PATH) $(SAPI_FPM_PATH) $(OVERALL_TARGET) modules/* libs/* - -distclean: clean - rm -f Makefile config.cache config.log config.status Makefile.objects Makefile.fragments libtool main/php_config.h main/internal_functions_cli.c main/internal_functions.c Zend/zend_dtrace_gen.h Zend/zend_dtrace_gen.h.bak Zend/zend_config.h - rm -f main/build-defs.h scripts/phpize - rm -f ext/date/lib/timelib_config.h ext/mbstring/libmbfl/config.h ext/oci8/oci8_dtrace_gen.h ext/oci8/oci8_dtrace_gen.h.bak - rm -f scripts/man1/phpize.1 scripts/php-config scripts/man1/php-config.1 sapi/cli/php.1 sapi/cgi/php-cgi.1 sapi/phpdbg/phpdbg.1 ext/phar/phar.1 ext/phar/phar.phar.1 - rm -f sapi/fpm/php-fpm.conf sapi/fpm/init.d.php-fpm sapi/fpm/php-fpm.service sapi/fpm/php-fpm.8 sapi/fpm/status.html - rm -f ext/phar/phar.phar ext/phar/phar.php - rm -f ext/opcache/jit/zend_jit_x86.c - if test "$(srcdir)" != "$(builddir)"; then \ - rm -f ext/phar/phar/phar.inc; \ - fi - $(EGREP) define'.*include/php' $(top_srcdir)/configure | $(SED) 's/.*>//'|xargs rm -f - -prof-gen: - CCACHE_DISABLE=1 $(MAKE) PROF_FLAGS=-fprofile-generate all - -prof-clean: - find . -name \*.lo -o -name \*.o | xargs rm -f - find . -name \*.la -o -name \*.a | xargs rm -f - find . -name \*.so | xargs rm -f - rm -f libphp.la $(SAPI_CLI_PATH) $(SAPI_CGI_PATH) $(SAPI_LITESPEED_PATH) $(SAPI_FPM_PATH) $(OVERALL_TARGET) modules/* libs/* - -prof-use: - CCACHE_DISABLE=1 $(MAKE) PROF_FLAGS=-fprofile-use all - -# only php above 7.1.0 supports nullable return type -%_arginfo.h: %.stub.php - @if test -e "$(top_srcdir)/build/gen_stub.php"; then \ - if test ! -z "$(PHP_EXECUTABLE)" && test -x "$(PHP_EXECUTABLE)"; then \ - echo Parse $< to generate $@;\ - $(PHP_EXECUTABLE) $(top_srcdir)/build/gen_stub.php $<; \ - elif type php >/dev/null 2>/dev/null; then \ - if test `php -v | head -n1 | cut -d" " -f 2 | sed "s/$$/\n7.0.99/" | sort -rV | head -n1` != "7.0.99"; then \ - echo Parse $< to generate $@;\ - php $(top_srcdir)/build/gen_stub.php $<; \ - fi; \ - fi; \ - fi; - -# As we don't track includes, this is just a heuristic -%.c: %_arginfo.h - @touch $@ - -.PHONY: all clean install distclean test prof-gen prof-clean prof-use -.NOEXPORT: -main.lo: /usr/src/workspace/c/main.c - $(LIBTOOL) --mode=compile $(CC) -I. -I/usr/src/workspace/c $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) -c /usr/src/workspace/c/main.c -o main.lo -$(phplibdir)/my_module.la: ./my_module.la - $(LIBTOOL) --mode=install cp ./my_module.la $(phplibdir) - -./my_module.la: $(shared_objects_my_module) $(MY_MODULE_SHARED_DEPENDENCIES) - $(LIBTOOL) --mode=link $(CC) -shared $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(LDFLAGS) -o $@ -export-dynamic -avoid-version -prefer-pic -module -rpath $(phplibdir) $(EXTRA_LDFLAGS) $(shared_objects_my_module) $(MY_MODULE_SHARED_LIBADD) -