From c96029fa1bcff13bfa46e4f034d4cb1e44650bdb Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Wed, 18 Oct 2023 12:31:38 -0400 Subject: [PATCH] initial implementation --- CMakeLists.txt | 44 +++++++++++++++++++++++ README.md | 46 ++++++++++++++++++++++++ cmake/xo_pyprintjsonConfig.cmake.in | 4 +++ include/xo/pyprintjson/pyprintjson.hpp | 25 +++++++++++++ src/pyprintjson/CMakeLists.txt | 8 +++++ src/pyprintjson/EXAMPLES | 2 ++ src/pyprintjson/pyprintjson.cpp | 50 ++++++++++++++++++++++++++ src/pyprintjson/pyprintjson.hpp.in | 25 +++++++++++++ 8 files changed, 204 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 cmake/xo_pyprintjsonConfig.cmake.in create mode 100644 include/xo/pyprintjson/pyprintjson.hpp create mode 100644 src/pyprintjson/CMakeLists.txt create mode 100644 src/pyprintjson/EXAMPLES create mode 100644 src/pyprintjson/pyprintjson.cpp create mode 100644 src/pyprintjson/pyprintjson.hpp.in diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..6f326e13 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,44 @@ +# xo-pyprintjson/CMakeLists.txt + +cmake_minimum_required(VERSION 3.10) + +project(xo_pyprintjson VERSION 1.0) +enable_language(CXX) + +# common XO cmake macros (see github.com:Rconybea/xo-cmake) +include(xo_macros/xo_cxx) +include(xo_macros/code-coverage) + +# ---------------------------------------------------------------- +# unit test setup + +enable_testing() +# activate code coverage for all executables + libraries (when configured with -DCODE_COVERAGE=ON) +add_code_coverage() +# 1. assuming that /nix/store/ prefixes .hpp files belonging to gcc, catch2 etc. +# we're not interested in code coverage for these sources. +# 2. exclude the utest/ subdir, we don't need coverage on the unit tests themselves; +# rather, want coverage on the code that the unit tests exercise. +# +# NOTE: this seems to work only with the 'ccov-all' target. In particular, doesn't seem to do anything with the 'ccov' target +# +add_code_coverage_all_targets(EXCLUDE /nix/store/* ${PROJECT_SOURCE_DIR}/utest/* ${PROJECT_BINARY_DIR}/local/* ${PROJECT_SOURCE_DIR}/repo/*) + +# ---------------------------------------------------------------- +# c++ settings (usually temporary) + +set(PROJECT_CXX_FLAGS "") +add_definitions(${PROJECT_CXX_FLAGS}) + +xo_toplevel_compile_options() + +# ---------------------------------------------------------------- +# sources + +add_subdirectory(src/pyprintjson) +#add_subdirectory(utest) + +# ---------------------------------------------------------------- +# provide find_package() support + +xo_export_cmake_config(${PROJECT_NAME} ${PROJECT_VERSION} ${PROJECT_NAME}Targets) diff --git a/README.md b/README.md new file mode 100644 index 00000000..68fbdce3 --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +# python bindings for c++ printjson library (xo-printjson) + +## Getting Started + +### build + install dependencies + +- [github/Rconybea/xo-pyutil](https://github.com/Rconybea/xo-pyutil) +- [github/Rconybea/xo-printjson](https://github.com/Rconybea/xo-printjson) + +### build + install + +``` +$ cd xo-pyprintjson +$ mkdir build +$ cd build +$ INSTALL_PREFIX=/usr/local # or wherever you prefer, e.g. ~/local +$ cmake \ + -DCMAKE_MODULE_PATH=${INSTALL_PREFIX}/share/cmake \ + -DCMAKE_PREFIX_PATH=${INSTALL_PREFIX} \ + -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} .. +$ make +$ make install +``` +(also see .github/workflows/main.yml) + +### build for unit test coverage +``` +$ cd xo-pyprintjson +$ mkdir build-ccov +$ cd build-ccov +$ cmake \ + -DCMAKE_MODULE_PATH=${INSTALL_PREFIX}/share/cmake \ + -DCMAKE_PREFIX_PATH=${INSTALL_PREFIX} \ + -DCODE_COVERAGE=ON \ + -DCMAKE_BUILD_TYPE=Debug .. +``` + +### LSP (language server) support + +LSP looks for compile commands in the root of the source tree; +while Cmake creates them in the root of its build directory. + +``` +$ cd xo-pyprintjson +$ ln -s build/compile_commands.json # supply compile commands to LSP +``` diff --git a/cmake/xo_pyprintjsonConfig.cmake.in b/cmake/xo_pyprintjsonConfig.cmake.in new file mode 100644 index 00000000..9c15f36a --- /dev/null +++ b/cmake/xo_pyprintjsonConfig.cmake.in @@ -0,0 +1,4 @@ +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") +check_required_components("@PROJECT_NAME@") diff --git a/include/xo/pyprintjson/pyprintjson.hpp b/include/xo/pyprintjson/pyprintjson.hpp new file mode 100644 index 00000000..c5fa007d --- /dev/null +++ b/include/xo/pyprintjson/pyprintjson.hpp @@ -0,0 +1,25 @@ +/* @file pyprintjson.hpp + * + * automatically generated from src/pyprintjson/pyprintjson.hpp.in + * see src/pyprintjson/CMakeLists.txt + */ + +/* python requires module name = library name + * example: + * PYBIND11_MODULE(PYPRINTJSON_MODULE_NAME(), m) { ... } + */ +#define PYPRINTJSON_MODULE_NAME() pyprintjson + +/* example: + * py::module_::import(PYPRINTJSON_MODULE_NAME_STR) + */ +#define PYPRINTJSON_MODULE_NAME_STR "pyprintjson" + +/* example: + * PYPRINTJSON_IMPORT_MODULE() + * replaces + * py::module_::import("pyprintjson") + */ +#define PYPRINTJSON_IMPORT_MODULE() py::module_::import("pyprintjson") + +/* end pyprintjson.hpp */ diff --git a/src/pyprintjson/CMakeLists.txt b/src/pyprintjson/CMakeLists.txt new file mode 100644 index 00000000..5e77c995 --- /dev/null +++ b/src/pyprintjson/CMakeLists.txt @@ -0,0 +1,8 @@ +# xo_pyprintjson/src/pyprintjson/CMakeLists.txt + +set(SELF_LIB pyprintjson) +set(SELF_SRCS pyprintjson.cpp) + +xo_pybind11_library(${SELF_LIB} ${PROJECT_NAME}Targets ${SELF_SRCS}) + +xo_pybind11_dependency(${SELF_LIB} printjson) diff --git a/src/pyprintjson/EXAMPLES b/src/pyprintjson/EXAMPLES new file mode 100644 index 00000000..5965b9b6 --- /dev/null +++ b/src/pyprintjson/EXAMPLES @@ -0,0 +1,2 @@ +import pyprintjson +pj=pyprintjson.PrintJson() diff --git a/src/pyprintjson/pyprintjson.cpp b/src/pyprintjson/pyprintjson.cpp new file mode 100644 index 00000000..f49d4b41 --- /dev/null +++ b/src/pyprintjson/pyprintjson.cpp @@ -0,0 +1,50 @@ +/* @file pyprintjson.cpp */ + +// note: need pyreflect/ here bc pyreflect.hpp is generated, located in build directory +#include "pyprintjson.hpp" +#include "xo/pyreflect/pyreflect.hpp" + +#include "xo/printjson/PrintJson.hpp" +#include "xo/reflect/TaggedRcptr.hpp" +//#include "reflect/SelfTagging.hpp" +//#include "refcnt/Refcounted.hpp" +//#include "refcnt/Unowned.hpp" +#include "xo/pyutil/pyutil.hpp" +//#include +//#include +//#include +//#include + +namespace xo { + namespace py = pybind11; + + namespace json { + using xo::reflect::SelfTagging; + using xo::reflect::TaggedRcptr; + using xo::ref::rp; + using xo::ref::unowned_ptr; + + PYBIND11_MODULE(PYPRINTJSON_MODULE_NAME(), m) { + PYREFLECT_IMPORT_MODULE(); + + py::class_>(m, "PrintJson") + .def_static("instance", &PrintJsonSingleton::instance) + .def("print", + [](PrintJson & pj, TaggedRcptr p) + { + pj.print_tp(p, &std::cout); std::cout << "\n"; + }, + py::arg("value")) + .def("print", + [](PrintJson & pj, rp const & p) + { + pj.print_obj(p, &std::cout); std::cout << "\n"; + }, + py::arg("value")); + + //m.def("print_json", [](){ return PrintJsonSingleton::instance_ptr(); }); + } /*pyprintjson*/ + } /*namespace json*/ +} /*namespace xo*/ + +/* end pyprintjson.cpp */ diff --git a/src/pyprintjson/pyprintjson.hpp.in b/src/pyprintjson/pyprintjson.hpp.in new file mode 100644 index 00000000..d80e5aba --- /dev/null +++ b/src/pyprintjson/pyprintjson.hpp.in @@ -0,0 +1,25 @@ +/* @file pyprintjson.hpp + * + * automatically generated from src/pyprintjson/pyprintjson.hpp.in + * see src/pyprintjson/CMakeLists.txt + */ + +/* python requires module name = library name + * example: + * PYBIND11_MODULE(PYPRINTJSON_MODULE_NAME(), m) { ... } + */ +#define PYPRINTJSON_MODULE_NAME() @SELF_LIB@ + +/* example: + * py::module_::import(PYPRINTJSON_MODULE_NAME_STR) + */ +#define PYPRINTJSON_MODULE_NAME_STR "@SELF_LIB@" + +/* example: + * PYPRINTJSON_IMPORT_MODULE() + * replaces + * py::module_::import("pyprintjson") + */ +#define PYPRINTJSON_IMPORT_MODULE() py::module_::import("@SELF_LIB@") + +/* end pyprintjson.hpp */