diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c3fc147 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,24 @@ +cmake_minimum_required(VERSION 3.11) +FIND_PACKAGE(SWIG 3.0.11 REQUIRED) +INCLUDE(${SWIG_USE_FILE}) + +if(WIN32) +add_definitions(/D_WIN32 /D_WINDOWS /DWINDOWS=1 /DZEND_WIN32=1 /DPHP_WIN32=1 /DWIN32 /DNDebug /DNDEBUG /DZEND_DEBUG=0 /DZTS=1) +if(NOT CMAKE_CL_64) +add_definitions(-D_USE_32BIT_TIME_T) +endif(NOT CMAKE_CL_64) +ENDIF(WIN32) + +include_directories (C:/php-sdk/phpmaster/vc${VSCMD_VER}/$ENV{VSCMD_ARG_TGT_ARCH}/php-src) +include_directories (C:/php-sdk/phpmaster/vc${VSCMD_VER}/$ENV{VSCMD_ARG_TGT_ARCH}/php-src/main) +include_directories (C:/php-sdk/phpmaster/vc${VSCMD_VER}/$ENV{VSCMD_ARG_TGT_ARCH}/php-src/Zend) +include_directories (C:/php-sdk/phpmaster/vc${VSCMD_VER}/$ENV{VSCMD_ARG_TGT_ARCH}/php-src/TSRM) +include_directories (${CMAKE_CURRENT_SOURCE_DIR}) + +add_library(example STATIC example.c) +swig_add_library(php_example TYPE MODULE LANGUAGE php7 SOURCES example.i) +target_compile_options(php_example PRIVATE /D_WIN32 /D_WINDOWS /DWINDOWS=1 /DZEND_WIN32=1 /DPHP_WIN32=1 /DWIN32 /DNDebug /DNDEBUG /DZEND_DEBUG=0 /DZTS=1) + +string(SUBSTRING $ENV{VSCMD_VER} 0 2 VSCMD_VER) +target_link_libraries(php_example "C:/php-sdk/phpmaster/vc${VSCMD_VER}/$ENV{VSCMD_ARG_TGT_ARCH}/php-src/x64/Release_TS/php7ts.lib") +target_link_libraries(php_example example) diff --git a/README.md b/README.md index 8a9707e..2ef39ce 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,20 @@ # SWIGCmakePHPExampleWindows -Example on how to build a SWIG Extension for PHP with CMake on Windows +Example on how to build a SWIG Extension for PHP with CMake on Windows. +Couldn't find any working example for Windows so i created this one ;) +Hope it helps you. + +To build this Example, build PHP as descriped here: https://github.com/Microsoft/php-sdk-binary-tools +You need SWIG to make this work. +Current SWIG (3.0.12) has a Problem with building Extensions on Windows with Thread-Safe PHP. +So until SWIG 4.0.0 is released, you need to build SWIG, too. +I have a fork of swig in my github account which enables you to build it with cmake: https://github.com/AlexanderGabriel/swig +Build it and change the path SWIG_EXECUTABLE and SWIG_DIR to your environment. + +You can stay inside PHP-SDK-Shell if you want, change directory back to where you checked out this and then execute the following commands. + +mkdir build +cd build +cmake .. -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -Wno-dev -DSWIG_EXECUTABLE=C:\dev\work\swig\swig.exe -DSWIG_DIR=C:\dev\work\swig +cmake --build . + +Thats it. diff --git a/example.c b/example.c new file mode 100644 index 0000000..25044d0 --- /dev/null +++ b/example.c @@ -0,0 +1,14 @@ +/* File : example.c */ + +double My_variable = 3.0; + +/* Compute factorial of n */ +int fact(int n) { + if (n <= 1) return 1; + else return n*fact(n-1); +} + +/* Compute n mod m */ +int my_mod(int n, int m) { + return(n % m); +} \ No newline at end of file diff --git a/example.i b/example.i new file mode 100644 index 0000000..45a6933 --- /dev/null +++ b/example.i @@ -0,0 +1,12 @@ +/* File : example.i */ +%module example +%{ +/* Put headers and other declarations here */ +extern double My_variable; +extern int fact(int); +extern int my_mod(int n, int m); +%} + +extern double My_variable; +extern int fact(int); +extern int my_mod(int n, int m); \ No newline at end of file