From fe3c7fdf52e4bfd6e736a54c20eb12bb5cfd1f30 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Sat, 24 Aug 2013 17:38:05 +0200 Subject: initial setup for library --- Makefile | 17 ++++ include/extension.h | 76 ++++++++++++++++++ phpcpp.h | 15 ++++ src/.deps | 0 src/config.m4 | 0 src/configure.in | 202 ++++++++++++++++++++++++++++++++++++++++++++++++ src/entry.cpp | 24 ++++++ src/extension.cpp | 56 ++++++++++++++ src/includes.h | 21 +++++ tests/Makefile | 5 ++ tests/simple/Makefile | 22 ++++++ tests/simple/simple.cpp | 35 +++++++++ 12 files changed, 473 insertions(+) create mode 100644 Makefile create mode 100644 include/extension.h create mode 100644 phpcpp.h create mode 100644 src/.deps create mode 100644 src/config.m4 create mode 100644 src/configure.in create mode 100644 src/entry.cpp create mode 100644 src/extension.cpp create mode 100644 src/includes.h create mode 100644 tests/Makefile create mode 100644 tests/simple/Makefile create mode 100644 tests/simple/simple.cpp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2eb652a --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +PREFIX = /usr +INCLUDE_DIR = ${PREFIX}/include +LIBRARY_DIR = ${PREFIX}/lib + +all: + cd src; $(MAKE) + +clean: + cd src; $(MAKE) clean + +install: + mkdir -p ${INCLUDE_DIR}/phpcpp + mkdir -p ${LIBRARY_DIR} + cp -f phpcpp.h ${INCLUDE_DIR} + cp -f include/*.h ${INCLUDE_DIR}/phpcpp + cp -f src/libphpcpp.so ${LIBRARY_DIR} + diff --git a/include/extension.h b/include/extension.h new file mode 100644 index 0000000..69d5d3c --- /dev/null +++ b/include/extension.h @@ -0,0 +1,76 @@ +/** + * Extension.h + * + * The extension class is the starting point of your PHP extension. This class + * is instantiated the moment the PHP engine starts - for example when the + * apache process starts - and will be used for all subsequent requests that + * are handled by Apache. + * + * For some environments (for example CLI scripts and FastCGI calls) only one + * request is handled by an extension instance, but for others (when PHP runs + * as module in a webserver) many requests are handled by the same extension + * instance. + */ + +/** + * Set up namespace + */ +namespace PhpCpp +{ + +/** + * Class definition + */ +class Extension +{ +public: + /** + * Constructor + */ + Extension(); + + /** + * Destructor + */ + virtual ~Extension() + { + } + + /** + * Initialize the extension. + * + * This method is called after the extension has been loaded, constructed + * and after the compatibility has been checked, but before the requests + * are handled. You can override this method to add your own initialization. + * + * The method should return true on success, and false on failure (in which + * case the extension will not be used) + * + * @return boolean + */ + virtual bool initialize() + { + return true; + } + + /** + * Finalize the extension + * + * This method gets called after all requests were handled, and right before + * the Apache module or CLI script will exit. You can override it to add + * your own cleanup code. + */ + virtual void finalize() + { + } + +private: + +}; + +/** + * End of namespace + */ +} + + diff --git a/phpcpp.h b/phpcpp.h new file mode 100644 index 0000000..439b51f --- /dev/null +++ b/phpcpp.h @@ -0,0 +1,15 @@ +/** + * phpcpp.h + * + * Library to build PHP extensions with CPP + * + * @copyright 2013 CopernicA BV + * @author Emiel Bruijntjes + */ + +/** + * Include all headers files that are related to this library + */ +#include + + diff --git a/src/.deps b/src/.deps new file mode 100644 index 0000000..e69de29 diff --git a/src/config.m4 b/src/config.m4 new file mode 100644 index 0000000..e69de29 diff --git a/src/configure.in b/src/configure.in new file mode 100644 index 0000000..d745ca7 --- /dev/null +++ b/src/configure.in @@ -0,0 +1,202 @@ +dnl This file becomes configure.in for self-contained extensions. + +AC_PREREQ(2.59) +AC_INIT(config.m4) +ifdef([AC_PRESERVE_HELP_ORDER], [AC_PRESERVE_HELP_ORDER], []) + +PHP_CONFIG_NICE(config.nice) + +dnl +AC_DEFUN([PHP_EXT_BUILDDIR],[.])dnl +AC_DEFUN([PHP_EXT_DIR],[""])dnl +AC_DEFUN([PHP_EXT_SRCDIR],[$abs_srcdir])dnl +AC_DEFUN([PHP_ALWAYS_SHARED],[ + ext_output="yes, shared" + ext_shared=yes + test "[$]$1" = "no" && $1=yes +])dnl +dnl + +test -z "$CFLAGS" && auto_cflags=1 + +abs_srcdir=`(cd $srcdir && pwd)` +abs_builddir=`pwd` + +AC_PROG_CC([cc gcc]) +PHP_DETECT_ICC +PHP_DETECT_SUNCC +AC_PROG_CC_C_O + +dnl Support systems with system libraries in e.g. /usr/lib64 +PHP_ARG_WITH(libdir, for system library directory, +[ --with-libdir=NAME Look for libraries in .../NAME rather than .../lib], lib, no) + +PHP_RUNPATH_SWITCH +PHP_SHLIB_SUFFIX_NAMES + +dnl Find php-config script +PHP_ARG_WITH(php-config,, +[ --with-php-config=PATH Path to php-config [php-config]], php-config, no) + +dnl For BC +PHP_CONFIG=$PHP_PHP_CONFIG +prefix=`$PHP_CONFIG --prefix 2>/dev/null` +phpincludedir=`$PHP_CONFIG --include-dir 2>/dev/null` +INCLUDES=`$PHP_CONFIG --includes 2>/dev/null` +EXTENSION_DIR=`$PHP_CONFIG --extension-dir 2>/dev/null` +PHP_EXECUTABLE=`$PHP_CONFIG --php-binary 2>/dev/null` + +if test -z "$prefix"; then + AC_MSG_ERROR([Cannot find php-config. Please use --with-php-config=PATH]) +fi + +php_shtool=$srcdir/build/shtool +PHP_INIT_BUILD_SYSTEM + +AC_MSG_CHECKING([for PHP prefix]) +AC_MSG_RESULT([$prefix]) +AC_MSG_CHECKING([for PHP includes]) +AC_MSG_RESULT([$INCLUDES]) +AC_MSG_CHECKING([for PHP extension directory]) +AC_MSG_RESULT([$EXTENSION_DIR]) +AC_MSG_CHECKING([for PHP installed headers prefix]) +AC_MSG_RESULT([$phpincludedir]) + +dnl Checks for PHP_DEBUG / ZEND_DEBUG / ZTS +AC_MSG_CHECKING([if debug is enabled]) +old_CPPFLAGS=$CPPFLAGS +CPPFLAGS="-I$phpincludedir" +AC_EGREP_CPP(php_debug_is_enabled,[ +#include
+#if ZEND_DEBUG +php_debug_is_enabled +#endif +],[ + PHP_DEBUG=yes +],[ + PHP_DEBUG=no +]) +CPPFLAGS=$old_CPPFLAGS +AC_MSG_RESULT([$PHP_DEBUG]) + +AC_MSG_CHECKING([if zts is enabled]) +old_CPPFLAGS=$CPPFLAGS +CPPFLAGS="-I$phpincludedir" +AC_EGREP_CPP(php_zts_is_enabled,[ +#include
+#if ZTS +php_zts_is_enabled +#endif +],[ + PHP_THREAD_SAFETY=yes +],[ + PHP_THREAD_SAFETY=no +]) +CPPFLAGS=$old_CPPFLAGS +AC_MSG_RESULT([$PHP_DEBUG]) + +dnl Support for building and testing Zend extensions +ZEND_EXT_TYPE="zend_extension" +PHP_SUBST(ZEND_EXT_TYPE) + +dnl Discard optimization flags when debugging is enabled +if test "$PHP_DEBUG" = "yes"; then + PHP_DEBUG=1 + ZEND_DEBUG=yes + changequote({,}) + CFLAGS=`echo "$CFLAGS" | $SED -e 's/-O[0-9s]*//g'` + CXXFLAGS=`echo "$CXXFLAGS" | $SED -e 's/-O[0-9s]*//g'` + changequote([,]) + dnl add -O0 only if GCC or ICC is used + if test "$GCC" = "yes" || test "$ICC" = "yes"; then + CFLAGS="$CFLAGS -O0" + CXXFLAGS="$CXXFLAGS -g -O0" + fi + if test "$SUNCC" = "yes"; then + if test -n "$auto_cflags"; then + CFLAGS="-g" + CXXFLAGS="-g" + else + CFLAGS="$CFLAGS -g" + CXXFLAGS="$CFLAGS -g" + fi + fi +else + PHP_DEBUG=0 + ZEND_DEBUG=no +fi + +dnl Always shared +PHP_BUILD_SHARED + +dnl Required programs +PHP_PROG_RE2C +PHP_PROG_AWK + +sinclude(config.m4) + +enable_static=no +enable_shared=yes + +dnl Only allow AC_PROG_CXX and AC_PROG_CXXCPP if they are explicitly called (by PHP_REQUIRE_CXX). +dnl Otherwise AC_PROG_LIBTOOL fails if there is no working C++ compiler. +AC_PROVIDE_IFELSE([PHP_REQUIRE_CXX], [], [ + undefine([AC_PROG_CXX]) + AC_DEFUN([AC_PROG_CXX], []) + undefine([AC_PROG_CXXCPP]) + AC_DEFUN([AC_PROG_CXXCPP], [php_prog_cxxcpp=disabled]) +]) +AC_PROG_LIBTOOL + +all_targets='$(PHP_MODULES) $(PHP_ZEND_EX)' +install_targets="install-modules install-headers" +phplibdir="`pwd`/modules" +CPPFLAGS="$CPPFLAGS -DHAVE_CONFIG_H" +CFLAGS_CLEAN='$(CFLAGS)' +CXXFLAGS_CLEAN='$(CXXFLAGS)' + +test "$prefix" = "NONE" && prefix="/usr/local" +test "$exec_prefix" = "NONE" && exec_prefix='$(prefix)' + +PHP_SUBST(PHP_MODULES) +PHP_SUBST(PHP_ZEND_EX) + +PHP_SUBST(all_targets) +PHP_SUBST(install_targets) + +PHP_SUBST(prefix) +PHP_SUBST(exec_prefix) +PHP_SUBST(libdir) +PHP_SUBST(prefix) +PHP_SUBST(phplibdir) +PHP_SUBST(phpincludedir) + +PHP_SUBST(CC) +PHP_SUBST(CFLAGS) +PHP_SUBST(CFLAGS_CLEAN) +PHP_SUBST(CPP) +PHP_SUBST(CPPFLAGS) +PHP_SUBST(CXX) +PHP_SUBST(CXXFLAGS) +PHP_SUBST(CXXFLAGS_CLEAN) +PHP_SUBST(EXTENSION_DIR) +PHP_SUBST(PHP_EXECUTABLE) +PHP_SUBST(EXTRA_LDFLAGS) +PHP_SUBST(EXTRA_LIBS) +PHP_SUBST(INCLUDES) +PHP_SUBST(LFLAGS) +PHP_SUBST(LDFLAGS) +PHP_SUBST(SHARED_LIBTOOL) +PHP_SUBST(LIBTOOL) +PHP_SUBST(SHELL) +PHP_SUBST(INSTALL_HEADERS) + +PHP_GEN_BUILD_DIRS +PHP_GEN_GLOBAL_MAKEFILE + +test -d modules || $php_shtool mkdir modules +touch .deps + +AC_CONFIG_HEADER(config.h) + +AC_OUTPUT() diff --git a/src/entry.cpp b/src/entry.cpp new file mode 100644 index 0000000..66fa244 --- /dev/null +++ b/src/entry.cpp @@ -0,0 +1,24 @@ +/** + * Entry.cpp + * + * The kickstart function that loads the library. When PHP loads an extension, + * if runs a dlopen() call to open the .so file, and the it locates the + * get_module()' function - which it then calls. + * + * In this file we have implemented this function. + * + * @author Emiel Bruijntjes + * @documentation private + */ +#include "includes.h" + +/** + * The get_entry function + * @return zend_module_entry_t + */ +DLL_EXPORT zend_module_entry_t *get_entry() +{ + + +} + diff --git a/src/extension.cpp b/src/extension.cpp new file mode 100644 index 0000000..1677ec1 --- /dev/null +++ b/src/extension.cpp @@ -0,0 +1,56 @@ +/** + * Extension.cpp + * + * @author Emiel Bruijntjes + * @copyright 2013 Copernica BV + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace PhpCpp { + +/** + * Constructor + * @param name Name of the extension + * @param version Version number + */ +Extension::Extension(const char *name, const char *version) +{ + // initialize the entry + _entry.size = sizeof(zend_module_entry); // size of the data + _entry.zend_api = ZEND_MODULE_API_NO; // api number + _entry.zend_debug = ZEND_DEBUG; // debug mode enabled? + _entry.zts = USING_ZTS; // is thread safety enabled? + _entry.ini_entry = NULL; // the php.ini record + _entry.deps = NULL; // dependencies on other modules + _entry.name = name; // extension name + _entry.functions = NULL; // functions supported by this module + _entry.module_startup_func = NULL; // startup function for the whole extension + _entry.module_shutdown_func = NULL; // shutdown function for the whole extension + _entry.request_startup_func = NULL; // startup function per request + _entry.request_shutdown_func = NULL; // shutdown function per request + _entry.info_func = NULL; // information for retrieving info + _entry.version = version; // version string + _entry.globals_size = 0; // number of global variables +#ifdef ZTS + _entry.globals_id_ptr = NULL; // pointer to the globals, thread safe +#else + _entry.globals_ptr = NULL; // pointer to the globals + _entry.globals_ctor = NULL; // constructor for global variables + _entry.globals_dtor = NULL; // destructor for global variables + _entry.post_deactivate_func = NULL; // unknown function + _entry.module_started = 0; // module is not yet started + _entry.type = 0; // temporary or persistent module, will be filled by Zend engine + _entry.handle = NULL; // dlopen() handle, will be filled by Zend engine + _entry.module_number = 0; // module number will be filled in by Zend engine + _entry.build_id = ZEND_MODULE_BUILD_ID; // check if extension and zend engine are compatible +} + +/** + * End of namespace + */ +} + + diff --git a/src/includes.h b/src/includes.h new file mode 100644 index 0000000..385d033 --- /dev/null +++ b/src/includes.h @@ -0,0 +1,21 @@ +/** + * Includes.h + * + * Startup include file to compile the phpcpp library + * + * @author Emiel Bruijntjes + * @copyright 2013 Copernica BV + */ + +/** + * Include standard C and C++ libraries + */ +#include +#include +#include + +/** + * Include other files from this library + */ +#include "../include/extension.h" + diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..2f74b32 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,5 @@ +all: + cd simple; $(MAKE) + +clean: + cd simple; $(MAKE) clean diff --git a/tests/simple/Makefile b/tests/simple/Makefile new file mode 100644 index 0000000..e3bd7c7 --- /dev/null +++ b/tests/simple/Makefile @@ -0,0 +1,22 @@ +CPP = g++ +RM = rm -f +CPP_FLAGS = -Wall -c -I. -O2 + +LD = g++ +LD_FLAGS = -Wall -shared -O2 +RESULT = simple.so + +SOURCES = $(wildcard *.cpp) +OBJECTS = $(SOURCES:%.cpp=%.o) + +all: ${OBJECTS} ${RESULT} + +${RESULT}: ${OBJECTS} + ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp + +clean: + ${RM} *.obj *~* ${OBJECTS} ${RESULT} + +${OBJECTS}: + ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp} + diff --git a/tests/simple/simple.cpp b/tests/simple/simple.cpp new file mode 100644 index 0000000..36db294 --- /dev/null +++ b/tests/simple/simple.cpp @@ -0,0 +1,35 @@ +/** + * Simple.h + * + * A very simple extension that does almost nothing + * + * @author Emiel Bruijntjes + * @copyright 2013 Copernica BV + */ +#include + +/** + * Override the extension class + */ +class SimpleExtension : public PhpCpp::Extension +{ +public: + /** + * Constructor + */ + SimpleExtension() : Extension( + "simple", + "1.0", + "Emiel Bruijntjes ", + "http://www.copernica.com", + "Copyright 2013 Copernica BV") + { + } +}; + +extern "C" { + +// create the object for the PHP extension +PHP_CPP_EXTENSION(SimpleExtension); + +} -- cgit v1.2.3