- Add support for 'lib' build mode that creates shared libraries with correct extensions
- Implement automatic 'lib' prefix for library targets on Unix systems unless explicit output is set
- Add build mode aliases normalization ('library', 'extension', 'cli' map to standard modes)
- Update target filename generation to handle explicit output paths with custom extensions
- Add platform-specific target extensions: .so for Linux, .dylib for macOS, .dll for Windows
- Include smoke test for validating library compilation and linking
- Update documentation with library build instructions and usage examples
- Add linker flag -Wl,-z,defs for Linux library builds to ensure self-contained libraries
- Modify command line help to document new build modes and clarify existing options
- Add C API header generation for exported
pull/16/head
parent
b4a2de7ace
commit
88139f4da7
11 changed files with 179 additions and 30 deletions
@ -0,0 +1,21 @@ |
||||
# lib-demo |
||||
|
||||
This example compiles TypePHP code into a shared library and exposes a stable C ABI. |
||||
|
||||
Build it from the repository root: |
||||
|
||||
```sh |
||||
php bin/compiler.php examples/lib-demo/project.yml --no-progress |
||||
``` |
||||
|
||||
The resulting library uses the configured project name: currently `libdemo.so` on Linux, `libdemo.dylib` on macOS, and `demo.dll` on Windows. Set `output: demo.so` in `project.yml` to use an exact output filename. Its public API is declared in `include/typephp_lib_demo.h`. |
||||
|
||||
On Linux, validate the library can be linked and called: |
||||
|
||||
```sh |
||||
g++ -std=c++17 examples/lib-demo/smoke-test.cc -Iexamples/lib-demo \ |
||||
./libdemo.so -Wl,-rpath,'$ORIGIN' -o lib-demo-smoke |
||||
./lib-demo-smoke |
||||
``` |
||||
|
||||
`typephp_lib_demo_add()` initializes the embedded PHP runtime on its first call. The runtime remains process-global for the lifetime of the library; do not call it concurrently with the first invocation. |
||||
@ -0,0 +1,22 @@ |
||||
#ifndef TYPEPHP_LIB_DEMO_H |
||||
#define TYPEPHP_LIB_DEMO_H |
||||
|
||||
#if defined(_WIN32) && defined(TYPEPHP_LIB_DEMO_BUILD) |
||||
#define TYPEPHP_LIB_DEMO_API __declspec(dllexport) |
||||
#elif defined(_WIN32) |
||||
#define TYPEPHP_LIB_DEMO_API __declspec(dllimport) |
||||
#else |
||||
#define TYPEPHP_LIB_DEMO_API |
||||
#endif |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
TYPEPHP_LIB_DEMO_API int typephp_lib_demo_add(int a, int b); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif |
||||
@ -0,0 +1,14 @@ |
||||
#include "include/typephp_lib_demo.h" |
||||
|
||||
#include <iostream> |
||||
|
||||
int main() |
||||
{ |
||||
const int first = typephp_lib_demo_add(20, 22); |
||||
const int second = typephp_lib_demo_add(-10, 7); |
||||
if (first != 42 || second != -3) { |
||||
std::cerr << "unexpected results: " << first << ", " << second << '\n'; |
||||
return 1; |
||||
} |
||||
return 0; |
||||
} |
||||
Loading…
Reference in new issue