diff --git a/.gitignore b/.gitignore index 222511e..d1394d2 100644 --- a/.gitignore +++ b/.gitignore @@ -181,6 +181,7 @@ fabric.properties .libs/ build/ configure.in +configure.ac mkinstalldirs run-tests.php Makefile.global diff --git a/Makefile.frag b/Makefile.frag index 2daa2ae..ea9eb3b 100644 --- a/Makefile.frag +++ b/Makefile.frag @@ -40,16 +40,20 @@ $(BUILD_DIR)/aws-crt-ffi/CMakeCache.txt: $(INT_DIR)/lib/libcrypto.a $(BUILD_DIR)/aws-crt-ffi/libaws-crt-ffi.so: $(BUILD_DIR)/aws-crt-ffi/CMakeCache.txt $(CMAKE_BUILD) build/aws-crt-ffi $(CMAKE_TARGET) -# copy the lib into the lib folder -$(INSTALL_DIR)/lib/libaws-crt-ffi.so: $(BUILD_DIR)/aws-crt-ffi/libaws-crt-ffi.so $(INSTALL_DIR)/lib/api.h - cp -v $(BUILD_DIR)/aws-crt-ffi/libaws-crt-ffi.so $(INSTALL_DIR)/lib/libaws-crt-ffi.so +# copy the lib into the src folder +$(INSTALL_DIR)/src/libaws-crt-ffi.so: $(BUILD_DIR)/aws-crt-ffi/libaws-crt-ffi.so $(INSTALL_DIR)/src/api.h + cp -v $(BUILD_DIR)/aws-crt-ffi/libaws-crt-ffi.so $(INSTALL_DIR)/src/libaws-crt-ffi.so # install api.h from FFI lib -$(INSTALL_DIR)/lib/api.h: crt/aws-crt-ffi/src/api.h - cat crt/aws-crt-ffi/src/api.h | grep -v AWS_EXTERN_C | sed -e 's/AWS_CRT_API //' | grep -ve '^#' > $(INSTALL_DIR)/lib/api.h +$(INSTALL_DIR)/src/api.h: crt/aws-crt-ffi/src/api.h + cat crt/aws-crt-ffi/src/api.h | grep -v AWS_EXTERN_C | sed -e 's/AWS_CRT_API //' | grep -ve '^#' > $(INSTALL_DIR)/src/api.h + +# copy the FFI lib from src to ext for the extension to use +$(INSTALL_DIR)/ext/libaws-crt-ffi.so: $(INSTALL_DIR)/src/libaws-crt-ffi.so + cp -v $(INSTALL_DIR)/src/libaws-crt-ffi.so $(INSTALL_DIR)/ext/libaws-crt-ffi.so # Force the crt object target to depend on the FFI library -src/crt.lo: $(INSTALL_DIR)/lib/libaws-crt-ffi.so +ext/crt.lo: $(INSTALL_DIR)/ext/libaws-crt-ffi.so ifeq ($(TEST_FFI),1) test-ci: test-ffi @@ -58,6 +62,6 @@ test-ci: test endif # Test the FFI interface -test-ffi: $(INSTALL_DIR)/lib/libaws-crt-ffi.so +test-ffi: $(INSTALL_DIR)/src/libaws-crt-ffi.so composer update composer run test diff --git a/composer.json b/composer.json index 856b366..9131452 100644 --- a/composer.json +++ b/composer.json @@ -12,19 +12,16 @@ }, "autoload": { "classmap": [ - "lib/" - ], - "psr-4": { - "AWS\\": "lib/AWS" - } + "src/" + ] }, "autoload-dev": { "classmap": [ - "lib/tests/" + "src/tests/" ] }, "scripts": { - "test": "phpunit lib/tests" + "test": "phpunit src/tests" }, "license": "Apache-2.0", "authors": [ diff --git a/config.m4 b/config.m4 index eeb4759..61cc655 100644 --- a/config.m4 +++ b/config.m4 @@ -17,7 +17,7 @@ if test "$PHP_AWSCRT" != "no"; then PHP_SUBST(AWSCRT_SHARED_LIBADD) # Sources for the PHP extension itself - AWSCRT_SOURCES=src/*.c + AWSCRT_SOURCES=ext/*.c PHP_NEW_EXTENSION(awscrt, $AWSCRT_SOURCES, $ext_shared) fi diff --git a/ext/.gitignore b/ext/.gitignore new file mode 100644 index 0000000..2973c97 --- /dev/null +++ b/ext/.gitignore @@ -0,0 +1,2 @@ +*.so +api.h diff --git a/src/crt.c b/ext/crt.c similarity index 100% rename from src/crt.c rename to ext/crt.c diff --git a/src/php_aws_crt.h b/ext/php_aws_crt.h similarity index 100% rename from src/php_aws_crt.h rename to ext/php_aws_crt.h diff --git a/format-check.sh b/format-check.sh index ca5af09..9783241 100755 --- a/format-check.sh +++ b/format-check.sh @@ -10,7 +10,7 @@ if NOT type $CLANG_FORMAT 2> /dev/null ; then fi FAIL=0 -SOURCE_FILES=`find src -type f \( -name '*.h' -o -name '*.c' \)` +SOURCE_FILES=`find ext -type f \( -name '*.h' -o -name '*.c' \)` for i in $SOURCE_FILES do $CLANG_FORMAT -output-replacements-xml $i | grep -c " /dev/null diff --git a/lib/.gitignore b/src/.gitignore similarity index 100% rename from lib/.gitignore rename to src/.gitignore diff --git a/lib/AWS/CRT.php b/src/AWS/CRT/CRT.php similarity index 52% rename from lib/AWS/CRT.php rename to src/AWS/CRT/CRT.php index 9535e3b..b4bde63 100644 --- a/lib/AWS/CRT.php +++ b/src/AWS/CRT/CRT.php @@ -1,21 +1,33 @@ getMessage(), "\n"; + self::$impl = new Extension(); + } catch (RuntimeException $rex) { + try { + self::$impl = new FFI(); + } catch (RuntimeException $frex) { + throw new RuntimeException('Unable to initialize AWS CRT via extension or FFI', -1, $frex); + } } } ++self::$refcount; @@ -23,24 +35,15 @@ final class CRT { function __destruct() { if (--self::$refcount == 0) { - self::clean_up(); - self::$ffi = null; + self::$impl = null; } } - private static function init() { - return self::$ffi->aws_crt_init(); - } - - private static function clean_up() { - return self::$ffi->aws_crt_clean_up(); - } - /** * @return integer last error code reported within the CRT */ public static function last_error() { - return self::$ffi->aws_crt_last_error(); + return self::$impl->aws_crt_last_error(); } /** @@ -48,7 +51,7 @@ final class CRT { * @return string Human-readable description of the provided error code */ public static function error_str($error) { - return self::$ffi->aws_crt_error_str((int) $error); + return self::$impl->aws_crt_error_str((int) $error); } /** @@ -56,14 +59,14 @@ final class CRT { * @return string Name/enum identifier for the provided error code */ public static function error_name($error) { - return self::$ffi->aws_crt_error_name((int) $error); + return self::$impl->aws_crt_error_name((int) $error); } function event_loop_group_new($num_threads) { - return self::$ffi->aws_crt_event_loop_group_new($num_threads); + return self::$impl->aws_crt_event_loop_group_new($num_threads); } function event_loop_group_release($elg) { - return self::$ffi->aws_crt_event_loop_group_release($elg); + return self::$impl->aws_crt_event_loop_group_release($elg); } } diff --git a/lib/AWS/CRT/IO/EventLoopGroup.php b/src/AWS/CRT/IO/EventLoopGroup.php similarity index 85% rename from lib/AWS/CRT/IO/EventLoopGroup.php rename to src/AWS/CRT/IO/EventLoopGroup.php index 38e74a9..cccf919 100644 --- a/lib/AWS/CRT/IO/EventLoopGroup.php +++ b/src/AWS/CRT/IO/EventLoopGroup.php @@ -1,5 +1,9 @@ event_loop_group_release($this->native); - $this->release(); + self::$crt->event_loop_group_release($this->release()); parent::__destruct(); } } diff --git a/src/AWS/CRT/Internal/Extension.php b/src/AWS/CRT/Internal/Extension.php new file mode 100644 index 0000000..0defbc5 --- /dev/null +++ b/src/AWS/CRT/Internal/Extension.php @@ -0,0 +1,26 @@ +aws_crt_init(); + } + + private static function clean_up() { + return self::$ffi->aws_crt_clean_up(); + } +} diff --git a/lib/AWS/CRT/NativeResource.php b/src/AWS/CRT/NativeResource.php similarity index 80% rename from lib/AWS/CRT/NativeResource.php rename to src/AWS/CRT/NativeResource.php index f47de36..6fc5bb4 100644 --- a/lib/AWS/CRT/NativeResource.php +++ b/src/AWS/CRT/NativeResource.php @@ -1,5 +1,9 @@ getMessage(), "\n"; - } + self::$crt = new CRT(); } self::$resources[spl_object_hash($this)] = 1; @@ -26,7 +26,9 @@ abstract class NativeResource { } protected function release() { + $native = $this->native; $this->native = null; + return $native; } function __destruct() { diff --git a/lib/php.ini b/src/php.ini similarity index 100% rename from lib/php.ini rename to src/php.ini diff --git a/lib/tests/ErrorTest.php b/src/tests/ErrorTest.php similarity index 97% rename from lib/tests/ErrorTest.php rename to src/tests/ErrorTest.php index 021d3ad..5027987 100644 --- a/lib/tests/ErrorTest.php +++ b/src/tests/ErrorTest.php @@ -1,5 +1,7 @@ assertNotNull($elg, "Failed to create default EventLoopGroup"); $elg = null; } @@ -11,6 +14,7 @@ final class EventLoopGroupTest extends PHPUnit_Framework_TestCase { $options = EventLoopGroup::defaults(); $options['num_threads'] = 1; $elg = new EventLoopGroup($options); + $this->assertNotNull($elg, "Failed to create EventLoopGroup with 1 thread"); $elg = null; } }