CMake Integration

For some lib target already defined:

add_library(lib header.h source.cpp)

Add Scapix:

include(FetchContent)
FetchContent_Declare(
  cmodule
  URL "https://github.com/scapix-com/cmodule/archive/refs/tags/v2.0.0.tar.gz"
  URL_HASH SHA256=58695a9d73dc95a2c214097729917c7d9c366bf511e162d566cf55dd2b9cd7be
)
FetchContent_MakeAvailable(cmodule)

find_package(Scapix REQUIRED)
scapix_bridge_headers(lib "com.example.lib" header.h)

The same CMake project will build C++ library with bindings for different languages, depending on SCAPIX_BRIDGE parameter:

$ cmake -B build -DSCAPIX_BRIDGE=java

Supported values for SCAPIX_BRIDGE parameter:

  • cpp (default, no bridge)
  • java
  • objc
  • python
  • js
  • cs

scapix_add_target() / scapix_bridge_headers() parameters:

  • target - your C++ library target
  • domain - reverse domain name for your application (example: com.companyname.appname)
  • bridge_headers - C++ headers to parse.

For Python bridge, the order of headers passed to scapix_bridge_headers() matters: headers with base classes should come before headers with derived classes.

scapix_fix_sources()

Cosmetic: call scapix_fix_sources(lib) to organize sources in IDEs like Visual Studio and XCode (calls CMake source_group() function).

Configuration options

When using Java Bridge (SCAPIX_BRIDGE=java), you can specify the following options:

  • SCAPIX_JNI_CACHE_CLASS_LOADER, default: ON
  • SCAPIX_JNI_AUTO_ATTACH_THREAD, default: ON

See Scapix JNI documentation.

  • SCAPIX_CUSTOM_JNI_ONLOAD

By default, Scapix generates JNI_OnLoad() function for target library. You can also use your own JNI_OnLoad(), which then needs to call scapix_JNI_OnLoad().

target_compile_definitions(scapix PUBLIC SCAPIX_CUSTOM_JNI_ONLOAD)
#include <scapix/bridge/java/on_load.h>

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
{
	// ... your additional JNI init code ...

	return scapix_JNI_OnLoad(vm, reserved);
}

C++ dependency management

Scapix uses cmodule for CMake dependency management.

You can also add additional dependencies to your project and cmodule will automatically download and build these libraries for any target platform like iOS, Android, WebAssembly, etc.

include(FetchContent)
FetchContent_Declare(
  cmodule
  URL "https://github.com/scapix-com/cmodule/archive/refs/tags/v2.0.0.tar.gz"
  URL_HASH SHA256=58695a9d73dc95a2c214097729917c7d9c366bf511e162d566cf55dd2b9cd7be
)
FetchContent_MakeAvailable(cmodule)

find_package(Boost REQUIRED COMPONENTS filesystem iostreams context)
target_link_libraries(mylib PUBLIC Boost::filesystem Boost::iostreams Boost::context)

find_package(ZLIB REQUIRED)
target_link_libraries(mylib PRIVATE ZLIB::ZLIB)

find_package(BZip2 REQUIRED)
target_link_libraries(mylib PRIVATE BZip2::BZip2)

find_package(CURL REQUIRED)
target_link_libraries(mylib PRIVATE CURL::libcurl)