From c96029fa1bcff13bfa46e4f034d4cb1e44650bdb Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Wed, 18 Oct 2023 12:31:38 -0400 Subject: [PATCH 01/13] 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 */ From b7c2e1d15eded2aba2ff64e110ccdf2d0f82e96a Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Wed, 18 Oct 2023 12:32:03 -0400 Subject: [PATCH 02/13] + .gitignore --- .gitignore | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..8ea1f615 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# lsp keep state here +.cache +# typical build directories +build +ccov +# for lsp: manual symlink to chosen build directory +compile_commands.json From dc253cd388bc53218c01fc2f88fbebf984a61790 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Wed, 18 Oct 2023 12:33:10 -0400 Subject: [PATCH 03/13] doc: minor README improvements --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 68fbdce3..34b77909 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ $ make install ``` (also see .github/workflows/main.yml) +## Development + ### build for unit test coverage ``` $ cd xo-pyprintjson @@ -44,3 +46,14 @@ 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 ``` + +### display cmake variables + +- `-L` list variables +- `-A` include 'advanced' variables +- `-H` include help text + +``` +$ cd xo-pyprintjson/build +$ cmake -LAH +``` From c4ed2e7f22fa983913122ac31d936a3f8e8e277a Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Wed, 18 Oct 2023 12:54:16 -0400 Subject: [PATCH 04/13] github: + workflow --- .github/workflows/main.yml | 177 +++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 00000000..eb51827e --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,177 @@ +name: build xo-pyprintjson + dependencies + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Release + +jobs: + build: + # The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac. + # You can convert this to a matrix build if you need cross-platform coverage. + # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix + runs-on: ubuntu-latest + + steps: + - name: checkout source + uses: actions/checkout@v3 + + - name: Install catch2 + # install catch2. see [[https://stackoverflow.com/questions/57982945/how-to-apt-get-install-in-a-github-actions-workflow]] + run: sudo apt-get install -y catch2 + + - name: Install pybind11-dev + run: sudo apt-get install -y pybind11-dev + + # ---------------------------------------------------------------- + + - name: Clone xo-cmake + uses: actions/checkout@v3 + with: + repository: Rconybea/xo-cmake + path: repo/xo-cmake + + - name: Configure xo-cmake + run: cmake -B ${{github.workspace}}/build_xo-cmake -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/local repo/xo-cmake + + - name: Build xo-cmake (trivial) + run: cmake --build ${{github.workspace}}/build_xo-cmake --config ${{env.BUILD_TYPE}} + + - name: Install xo-cmake + run: cmake --install ${{github.workspace}}/build_xo-cmake + + # ---------------------------------------------------------------- + + - name: Clone indentlog + uses: actions/checkout@v3 + with: + repository: Rconybea/indentlog + path: repo/indentlog + + - name: Configure indentlog + # configure cmake for indentlog in dedicated build directory. + run: cmake -B ${{github.workspace}}/build_indentlog -DCMAKE_MODULE_PATH=${{github.workspace}}/local/share/cmake -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/local repo/indentlog + + - name: Build indentlog + run: cmake --build ${{github.workspace}}/build_indentlog --config ${{env.BUILD_TYPE}} + + - name: Install indentlog + # install into ${{github.workspace}}/local + run: cmake --install ${{github.workspace}}/build_indentlog + + # ---------------------------------------------------------------- + + - name: Clone subsys + uses: actions/checkout@v3 + with: + repository: Rconybea/subsys + path: repo/subsys + + - name: Configure subsys + # configure cmake for subsys in dedicated build directory. + run: cmake -B ${{github.workspace}}/build_subsys -DCMAKE_MODULE_PATH=${{github.workspace}}/local/share/cmake -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/local repo/subsys + + - name: Build subsys + run: cmake --build ${{github.workspace}}/build_subsys --config ${{env.BUILD_TYPE}} + + - name: Install subsys + # install into ${{github.workspace}}/local + run: cmake --install ${{github.workspace}}/build_subsys + + # ---------------------------------------------------------------- + + - name: Clone refcnt + uses: actions/checkout@v3 + with: + repository: Rconybea/refcnt + path: repo/refcnt + + - name: Configure refcnt + # configure cmake for refcnt in dedicated build directory. + run: cmake -B ${{github.workspace}}/build_refcnt -DCMAKE_MODULE_PATH=${{github.workspace}}/local/share/cmake -DCMAKE_PREFIX_PATH=${{github.workspace}}/local -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/local repo/refcnt + + - name: Build refcnt + run: cmake --build ${{github.workspace}}/build_refcnt --config ${{env.BUILD_TYPE}} + + - name: Install refcnt + # install into ${{github.workspace}}/local + run: cmake --install ${{github.workspace}}/build_refcnt + + # ---------------------------------------------------------------- + + - name: Clone reflect + uses: actions/checkout@v3 + with: + repository: Rconybea/reflect + path: repo/reflect + + - name: Configure reflect + # configure cmake for reflect in dedicated build directory. + run: cmake -B ${{github.workspace}}/build_reflect -DCMAKE_MODULE_PATH=${{github.workspace}}/local/share/cmake -DCMAKE_PREFIX_PATH=${{github.workspace}}/local -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/local repo/reflect + + - name: Build reflect + run: cmake --build ${{github.workspace}}/build_reflect --config ${{env.BUILD_TYPE}} + + - name: Install reflect + # install into ${{github.workspace}}/local + run: cmake --install ${{github.workspace}}/build_reflect + + # ---------------------------------------------------------------- + + - name: Clone printjson + uses: actions/checkout@v3 + with: + repository: Rconybea/xo-printjson + path: repo/printjson + + - name: Configure printjson + # configure cmake for printjson in dedicated build directory. + run: cmake -B ${{github.workspace}}/build_printjson -DCMAKE_MODULE_PATH=${{github.workspace}}/local/share/cmake -DCMAKE_PREFIX_PATH=${{github.workspace}}/local -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/local repo/printjson + + - name: Build printjson + run: cmake --build ${{github.workspace}}/build_printjson --config ${{env.BUILD_TYPE}} + + - name: Install printjson + # install into ${{github.workspace}}/local + run: cmake --install ${{github.workspace}}/build_printjson + + # ---------------------------------------------------------------- + + - name: Clone pyutil + uses: actions/checkout@v3 + with: + repository: Rconybea/xo-pyutil + path: repo/pyutil + + - name: Configure pyutil + # configure cmake for pyutil in dedicated build directory. + run: cmake -B ${{github.workspace}}/build_pyutil -DCMAKE_MODULE_PATH=${{github.workspace}}/local/share/cmake -DCMAKE_PREFIX_PATH=${{github.workspace}}/local -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/local repo/pyutil + + - name: Build pyutil + run: cmake --build ${{github.workspace}}/build_pyutil --config ${{env.BUILD_TYPE}} + + - name: Install pyutil + # install into ${{github.workspace}}/local + run: cmake --install ${{github.workspace}}/build_pyutil + + # ---------------------------------------------------------------- + + - name: Configure self (pyprintjson) + # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. + # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type + run: cmake -B ${{github.workspace}}/build_pyprintjson -DCMAKE_MODULE_PATH=${{github.workspace}}/local/share/cmake -DCMAKE_PREFIX_PATH=${{github.workspace}}/local -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/local -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} + + - name: Build self (pyprintjson) + # Build your program with the given configuration + run: cmake --build ${{github.workspace}}/build_pyprintjson --config ${{env.BUILD_TYPE}} + + - name: Test self (pyprintjson) + working-directory: ${{github.workspace}}/build_pyprintjson + # Execute tests defined by the CMake configuration. + # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail + run: ctest -C ${{env.BUILD_TYPE}} From 36e012934856062dfb8c6f5511ec00dcfcc01819 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Wed, 18 Oct 2023 13:34:39 -0400 Subject: [PATCH 05/13] github: + pyreflect dep --- .github/workflows/main.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index eb51827e..af5526f6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -161,6 +161,25 @@ jobs: # ---------------------------------------------------------------- + - name: Clone pyreflect + uses: actions/checkout@v3 + with: + repository: Rconybea/xo-pyreflect + path: repo/pyreflect + + - name: Configure pyreflect + # configure cmake for pyreflect in dedicated build directory. + run: cmake -B ${{github.workspace}}/build_pyreflect -DCMAKE_MODULE_PATH=${{github.workspace}}/local/share/cmake -DCMAKE_PREFIX_PATH=${{github.workspace}}/local -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/local repo/pyreflect + + - name: Build pyreflect + run: cmake --build ${{github.workspace}}/build_pyreflect --config ${{env.BUILD_TYPE}} + + - name: Install pyreflect + # install into ${{github.workspace}}/local + run: cmake --install ${{github.workspace}}/build_pyreflect + + # ---------------------------------------------------------------- + - name: Configure self (pyprintjson) # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type From 66a668703951dda9abcd6ecbba45e8b47ecbef22 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sun, 22 Oct 2023 14:54:40 -0400 Subject: [PATCH 06/13] pyprintjson: + pyreflect dep --- src/pyprintjson/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pyprintjson/CMakeLists.txt b/src/pyprintjson/CMakeLists.txt index 5e77c995..d73baad1 100644 --- a/src/pyprintjson/CMakeLists.txt +++ b/src/pyprintjson/CMakeLists.txt @@ -6,3 +6,4 @@ set(SELF_SRCS pyprintjson.cpp) xo_pybind11_library(${SELF_LIB} ${PROJECT_NAME}Targets ${SELF_SRCS}) xo_pybind11_dependency(${SELF_LIB} printjson) +xo_pybind11_header_dependency(${SELF_LIB} pyreflect) From a55be719146df74e3bf27bdb21750f91e2eb432d Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sun, 22 Oct 2023 15:54:01 -0400 Subject: [PATCH 07/13] build: pyreflect -> xo_pyreflect --- src/pyprintjson/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pyprintjson/CMakeLists.txt b/src/pyprintjson/CMakeLists.txt index d73baad1..ad68a137 100644 --- a/src/pyprintjson/CMakeLists.txt +++ b/src/pyprintjson/CMakeLists.txt @@ -6,4 +6,4 @@ set(SELF_SRCS pyprintjson.cpp) xo_pybind11_library(${SELF_LIB} ${PROJECT_NAME}Targets ${SELF_SRCS}) xo_pybind11_dependency(${SELF_LIB} printjson) -xo_pybind11_header_dependency(${SELF_LIB} pyreflect) +xo_pybind11_header_dependency(${SELF_LIB} xo_pyreflect) From 9dc0c6e25695d4d67e87e7ee4a504e5a86661f98 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Tue, 24 Oct 2023 22:13:04 -0400 Subject: [PATCH 08/13] pyprintjson -> xo_pyprintjson --- src/pyprintjson/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pyprintjson/CMakeLists.txt b/src/pyprintjson/CMakeLists.txt index 5e77c995..2370e6a2 100644 --- a/src/pyprintjson/CMakeLists.txt +++ b/src/pyprintjson/CMakeLists.txt @@ -1,6 +1,6 @@ # xo_pyprintjson/src/pyprintjson/CMakeLists.txt -set(SELF_LIB pyprintjson) +set(SELF_LIB xo_pyprintjson) set(SELF_SRCS pyprintjson.cpp) xo_pybind11_library(${SELF_LIB} ${PROJECT_NAME}Targets ${SELF_SRCS}) From a96142ffea81ef2ce8da64e18d03b8de4f929dfa Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Fri, 15 Mar 2024 19:33:37 -0400 Subject: [PATCH 09/13] build: streamline CMAKE_MODULE_PATH interaction + README.md xpand --- .gitignore | 9 ++++----- CMakeLists.txt | 3 +-- README.md | 12 ++++++++++++ cmake/xo-bootstrap-macros.cmake | 12 ++++++++++++ include/README.md | 1 + include/xo/pyprintjson/pyprintjson.hpp | 25 ------------------------- 6 files changed, 30 insertions(+), 32 deletions(-) create mode 100644 cmake/xo-bootstrap-macros.cmake create mode 100644 include/README.md delete mode 100644 include/xo/pyprintjson/pyprintjson.hpp diff --git a/.gitignore b/.gitignore index 8ea1f615..13c0afb7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ -# lsp keep state here +# clangd working space (see emacs+lsp) .cache -# typical build directories -build -ccov -# for lsp: manual symlink to chosen build directory +# typical cmake build directory (source-tree-nephew) +.build* +# symlink to builddir/compile_commands.json; should be set manually in dev sandbox compile_commands.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f326e13..603f7253 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,8 +6,7 @@ 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) +include(cmake/xo-bootstrap-macros.cmake) # ---------------------------------------------------------------- # unit test setup diff --git a/README.md b/README.md index 34b77909..e85bc227 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,18 @@ $ make install ``` (also see .github/workflows/main.yml) +## Examples + +``` +PYTHONPATH=~/local2/lib:$PYTHONPATH python +>>> import xo_pyprintjson +>>> dir(xo_pyprintjson) +['PrintJson', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__'] +>>> dir(xo_pyprintjson.PrintJson) +['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'instance', 'print'] +>>> +``` + ## Development ### build for unit test coverage diff --git a/cmake/xo-bootstrap-macros.cmake b/cmake/xo-bootstrap-macros.cmake new file mode 100644 index 00000000..16644435 --- /dev/null +++ b/cmake/xo-bootstrap-macros.cmake @@ -0,0 +1,12 @@ +if (("${CMAKE_MODULE_PATH}" STREQUAL "") OR ("${CMAKE_MODULE_PATH}" STREQUAL "prefix")) + # default to typical install location for xo-project-macros + set(CMAKE_MODULE_PATH ${CMAKE_INSTALL_PREFIX}/share/cmake) +endif() + +message("-- CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}") +message("-- CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}") + +# needs to have been installed somewhere on CMAKE_MODULE_PATH, +# (e.g. from xo-cmake with the same value for CMAKE_INSTALL_PREFIX) +# +include(xo_macros/xo-project-macros) diff --git a/include/README.md b/include/README.md new file mode 100644 index 00000000..5d2b4c67 --- /dev/null +++ b/include/README.md @@ -0,0 +1 @@ +placeholder for future pyprintjson #include files diff --git a/include/xo/pyprintjson/pyprintjson.hpp b/include/xo/pyprintjson/pyprintjson.hpp deleted file mode 100644 index c5fa007d..00000000 --- a/include/xo/pyprintjson/pyprintjson.hpp +++ /dev/null @@ -1,25 +0,0 @@ -/* @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 */ From 0bce4cdc91bfd3d6023beb67ea6932252c792839 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Fri, 29 Mar 2024 14:33:41 -0400 Subject: [PATCH 10/13] build: suppress bootstrap message in submodule build --- cmake/xo-bootstrap-macros.cmake | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cmake/xo-bootstrap-macros.cmake b/cmake/xo-bootstrap-macros.cmake index 16644435..96592216 100644 --- a/cmake/xo-bootstrap-macros.cmake +++ b/cmake/xo-bootstrap-macros.cmake @@ -3,8 +3,10 @@ if (("${CMAKE_MODULE_PATH}" STREQUAL "") OR ("${CMAKE_MODULE_PATH}" STREQUAL "pr set(CMAKE_MODULE_PATH ${CMAKE_INSTALL_PREFIX}/share/cmake) endif() -message("-- CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}") -message("-- CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}") +if (NOT XO_SUBMODULE_BUILD) + message("-- CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}") + message("-- CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}") +endif() # needs to have been installed somewhere on CMAKE_MODULE_PATH, # (e.g. from xo-cmake with the same value for CMAKE_INSTALL_PREFIX) From e4088392b8b45e4224cdaed100c337db7d3ec4e8 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sat, 14 Sep 2024 12:20:27 -0500 Subject: [PATCH 11/13] xo-pyprintjson: build: update to latest xo-cmake macros --- CMakeLists.txt | 22 ++++------------------ src/pyprintjson/CMakeLists.txt | 3 ++- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 603f7253..dd29fbc9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,25 +3,11 @@ 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(GNUInstallDirs) include(cmake/xo-bootstrap-macros.cmake) -# ---------------------------------------------------------------- -# 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/*) +xo_cxx_toplevel_options3() # ---------------------------------------------------------------- # c++ settings (usually temporary) @@ -29,8 +15,6 @@ add_code_coverage_all_targets(EXCLUDE /nix/store/* ${PROJECT_SOURCE_DIR}/utest/* set(PROJECT_CXX_FLAGS "") add_definitions(${PROJECT_CXX_FLAGS}) -xo_toplevel_compile_options() - # ---------------------------------------------------------------- # sources @@ -41,3 +25,5 @@ add_subdirectory(src/pyprintjson) # provide find_package() support xo_export_cmake_config(${PROJECT_NAME} ${PROJECT_VERSION} ${PROJECT_NAME}Targets) + +# end CMakeLists.txt diff --git a/src/pyprintjson/CMakeLists.txt b/src/pyprintjson/CMakeLists.txt index a45f1b82..defff223 100644 --- a/src/pyprintjson/CMakeLists.txt +++ b/src/pyprintjson/CMakeLists.txt @@ -3,7 +3,8 @@ set(SELF_LIB xo_pyprintjson) set(SELF_SRCS pyprintjson.cpp) -xo_pybind11_library(${SELF_LIB} ${PROJECT_NAME}Targets ${SELF_SRCS}) +# ---------------------------------------------------------------- +xo_pybind11_library(${SELF_LIB} ${PROJECT_NAME}Targets ${SELF_SRCS}) xo_pybind11_dependency(${SELF_LIB} printjson) xo_pybind11_header_dependency(${SELF_LIB} xo_pyreflect) From 2ccc717579e5cf3afeb2c260d1dccff8aeb476a7 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sat, 14 Sep 2024 12:20:44 -0500 Subject: [PATCH 12/13] xo-pyprintjson: bugfix: track ns change xo::ref::rp -> xo::rp --- src/pyprintjson/pyprintjson.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pyprintjson/pyprintjson.cpp b/src/pyprintjson/pyprintjson.cpp index f49d4b41..1e107f31 100644 --- a/src/pyprintjson/pyprintjson.cpp +++ b/src/pyprintjson/pyprintjson.cpp @@ -21,7 +21,6 @@ namespace xo { 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) { From 36797a419a695f8f0e9d526002fe50e7978a12d7 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sat, 14 Sep 2024 12:26:41 -0500 Subject: [PATCH 13/13] xo-pyprintjson: build: use xo-cmake-config from xo-cmake --- cmake/xo-bootstrap-macros.cmake | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/cmake/xo-bootstrap-macros.cmake b/cmake/xo-bootstrap-macros.cmake index 96592216..aba31169 100644 --- a/cmake/xo-bootstrap-macros.cmake +++ b/cmake/xo-bootstrap-macros.cmake @@ -1,14 +1,35 @@ -if (("${CMAKE_MODULE_PATH}" STREQUAL "") OR ("${CMAKE_MODULE_PATH}" STREQUAL "prefix")) - # default to typical install location for xo-project-macros - set(CMAKE_MODULE_PATH ${CMAKE_INSTALL_PREFIX}/share/cmake) +# ---------------------------------------------------------------- +# for example: +# $ PREFIX=/usr/local # for example +# $ cmake -DCMAKE_MODULE_PATH=prefix -DCMAKE_INSTALL_PREFIX=$PREFIX -B .build +# +# will get +# CMAKE_MODULE_PATH +# from xo-cmake-config --cmake-module-path +# +# and expect .cmake macros in +# CMAKE_MODULE_PATH/xo_macros/xo_cxx.cmake +# ---------------------------------------------------------------- + +find_program(XO_CMAKE_CONFIG_EXECUTABLE NAMES xo-cmake-config REQUIRED) + +if ("${XO_CMAKE_CONFIG_EXECUTABLE}" STREQUAL "XO_CMAKE_CONFIG_EXECUTABLE-NOT_FOUND") + message(FATAL "could not find xo-cmake-config executable") endif() +message(STATUS "XO_CMAKE_CONFIG_EXECUTABLE=${XO_CMAKE_CONFIG_EXECUTABLE}") + if (NOT XO_SUBMODULE_BUILD) - message("-- CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}") - message("-- CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}") + if (("${CMAKE_MODULE_PATH}" STREQUAL "") OR ("${CMAKE_MODULE_PATH}" STREQUAL prefix)) + # default to typical install location for xo-project-macros + execute_process(COMMAND ${XO_CMAKE_CONFIG_EXECUTABLE} --cmake-module-path OUTPUT_VARIABLE CMAKE_MODULE_PATH) + message(STATUS "CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}") + endif() endif() # needs to have been installed somewhere on CMAKE_MODULE_PATH, # (e.g. from xo-cmake with the same value for CMAKE_INSTALL_PREFIX) # -include(xo_macros/xo-project-macros) +include(xo_macros/xo_cxx) + +xo_cxx_bootstrap_message()