diff --git a/CMakeLists.txt b/CMakeLists.txt index e08967e..a7da6e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,18 +1,19 @@ cmake_minimum_required(VERSION 3.19...3.26) -project(kaitai_awkward_runtime CXX) +project(${SKBUILD_PROJECT_NAME} CXX) set(KSY "" CACHE STRING "Specify the KSY file path") - -string(REGEX MATCH "([^/]+)$" package_name ${KSY}) -string(REGEX REPLACE "\\.ksy$" "" package_name ${package_name}) -set(PACKAGE_NAME ${package_name}) - set(BUILD_SHARED_LIBS ON) -set(BUILD_STATIC_LIBS OFF) find_package(pybind11 CONFIG REQUIRED) -add_subdirectory(kaitai_struct_cpp_stl_runtime) +# Setup the RPATH for built libraries +if(APPLE) + set(CMAKE_INSTALL_RPATH "@loader_path") +else() + SET(CMAKE_INSTALL_RPATH "$ORIGIN") +endif() + +add_subdirectory(kaitai_struct_cpp_stl_runtime EXCLUDE_FROM_ALL) # Download awkward headers include(FetchContent) @@ -36,13 +37,24 @@ execute_process( ) execute_process( - COMMAND ./kaitai-struct-compiler -t awkward --outdir src-${PACKAGE_NAME} ${KSY} #package name should be declared as a string + COMMAND ./kaitai-struct-compiler -t awkward --outdir src-${SKBUILD_PROJECT_NAME} ${KSY} #package name should be declared as a string WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND_ERROR_IS_FATAL ANY ) -pybind11_add_module(awkward_${PACKAGE_NAME} src-${PACKAGE_NAME}/${PACKAGE_NAME}_main.cpp src-${PACKAGE_NAME}/${PACKAGE_NAME}.cpp) -target_link_libraries(awkward_${PACKAGE_NAME} PRIVATE awkward::layout-builder kaitai_struct_cpp_stl_runtime) +string(REGEX MATCH "([^/]+)$" KSY_MODULE ${KSY}) +string(REGEX REPLACE "\\.ksy$" "" KSY_MODULE ${KSY_MODULE}) + +pybind11_add_module(awkward_${KSY_MODULE} src-${SKBUILD_PROJECT_NAME}/${KSY_MODULE}_main.cpp src-${SKBUILD_PROJECT_NAME}/${KSY_MODULE}.cpp) +target_link_libraries(awkward_${KSY_MODULE} PRIVATE awkward::layout-builder kaitai_struct_cpp_stl_runtime) + +# Install pure-python files +file(GLOB_RECURSE PYTHON_SOURCES "src/${SKBUILD_PROJECT_NAME}/*.py") + +install( + TARGETS awkward_${KSY_MODULE} kaitai_struct_cpp_stl_runtime + LIBRARY DESTINATION "${SKBUILD_PROJECT_NAME}/lib" + RUNTIME DESTINATION "${SKBUILD_PROJECT_NAME}/lib" + ARCHIVE DESTINATION "${SKBUILD_PROJECT_NAME}/lib") -# Install into wheel -install(TARGETS awkward_${PACKAGE_NAME} DESTINATION .) +install(FILES ${PYTHON_SOURCES} DESTINATION ${SKBUILD_PROJECT_NAME}) diff --git a/README.md b/README.md index 66f2601..d15f217 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # kaitai_awkward_runtime -# awkward-kaitai Building Awkward Arrays using Kaitai binary file descriptors. Steps for reproducing the environment: @@ -16,9 +15,9 @@ cd kaitai_awkward_runtime ``` -3. Install the library, specifying the schema, and open Python: +3. Install the library, and open Python: ``` -pip install . --config-settings 'cmake.define.KSY=schemas/animal.ksy' +pip install . python ``` @@ -29,3 +28,6 @@ awkward_array = awkward_animal.load("data/animal.raw") print(awkward_array) ``` + +> **Info** +> `kaitai_awkward_runtime` depends upon `sbt`, and `gtest` dependencies. diff --git a/src/kaitai_awkward_runtime/__init__.py b/src/kaitai_awkward_runtime/__init__.py index 252fa70..8c2070e 100644 --- a/src/kaitai_awkward_runtime/__init__.py +++ b/src/kaitai_awkward_runtime/__init__.py @@ -10,3 +10,6 @@ from ._version import version as __version__ __all__ = ("__version__",) + + +from .lib.awkward_animal import * diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index cd3f59d..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include -#include -#include -#include -#include "../build/_deps/awkward-headers-src/layout-builder/awkward/LayoutBuilder.h" -#include "../src-animal/animal.h" - -namespace py = pybind11; - -/** - * Create a snapshot of the given builder, and return an `ak.Array` pyobject - * @tparam T type of builder - * @param builder builder - * @return pyobject of Awkward Array - */ -template -py::object snapshot_builder(const T &builder) { - // How much memory to allocate? - std::map names_nbytes = {}; - builder.buffer_nbytes(names_nbytes); - - // Allocate memory - std::map buffers = {}; - for (auto it: names_nbytes) { - uint8_t *ptr = new uint8_t[it.second]; - buffers[it.first] = (void *) ptr; - } - - // Write non-contiguous contents to memory - builder.to_buffers(buffers); - auto from_buffers = py::module::import("awkward").attr("from_buffers"); - - // Build Python dictionary containing arrays - py::dict container; - for (auto it: buffers) { - - py::capsule free_when_done(it.second, [](void *data) { - uint8_t *dataPtr = reinterpret_cast(data); - delete[] dataPtr; - }); - - uint8_t *data = reinterpret_cast(it.second); - container[py::str(it.first)] = py::array_t( - {names_nbytes[it.first]}, - {sizeof(uint8_t)}, - data, - free_when_done - ); - } - return from_buffers(builder.form(), builder.length(), container); -} - -/** - * Create array, and return its snapshot - * @return pyobject of Awkward Array - */ -py::object load(std::string file_path) { - std::ifstream infile(file_path, std::ifstream::binary); - kaitai::kstream ks(&infile); - animal_t zoo = animal_t(&ks); - return snapshot_builder(zoo.animal_builder); -} - -PYBIND11_MODULE(awkward_animal, m) { - m.def("load", &load); -} \ No newline at end of file