From 65bd9178faf31bb3fb3fc596277e18206a2d33b0 Mon Sep 17 00:00:00 2001 From: ifilot Date: Mon, 25 Dec 2023 13:35:23 +0100 Subject: [PATCH] Adding shared library example --- .github/workflows/build.yml | 22 +++++++++ examples/.gitignore | 1 + examples/README.md | 71 ++++++++++++++++++++++++++++ examples/shared/CMakeLists.txt | 84 +++++++++++++++++++++++++++++++++ examples/shared/test_shared.cpp | 16 +++++++ src/CMakeLists.txt | 6 +-- 6 files changed, 195 insertions(+), 5 deletions(-) create mode 100644 examples/.gitignore create mode 100644 examples/README.md create mode 100644 examples/shared/CMakeLists.txt create mode 100644 examples/shared/test_shared.cpp diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7989122..1e264a8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -36,3 +36,25 @@ jobs: with: token: ${{ secrets.CODECOV_TOKEN }} files: ./build/coverage.xml + + test-shared: + + runs-on: ubuntu-latest + needs: build + + steps: + - uses: actions/checkout@v3 + - name: Install dependencies + run: sudo apt install -y build-essential cmake libglm-dev libtclap-dev libboost-all-dev libopenvdb-dev libtbb-dev libcppunit-dev libeigen3-dev liblzma-dev zlib1g-dev libbz2-dev gcovr + - name: Configure CMake + run: mkdir build && cd build && cmake ../src + - name: Build + run: cd build && make -j + - name: Perform unit tests + run: cd build && sudo make install + - name: Produce compilation using shared library + run: | + cd examples && mkdir build && cd build + cmake ../shared + make -j + ./den2obj-shared-example diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..a007fea --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1 @@ +build/* diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..90a8ce1 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,71 @@ +# Examples + +This folder contains a number of scripts how to use Den2Obj as a (shared) +library in your own programs and applications. + +## Requirements + +To use Den2Obj as a shared library in your applications, you need to dynamically +link against `libden2obj.so` as well as against a number of required libraries: + +* Boost (regex, iostreams and filesystem) +* GZIP +* LZMA +* BZ2 + +Besides these libraries, there is also a header-only dependency on the Eigen3 +library. + +A convenient way to ensure this in your application is by making use of +`CMake` and using the following settings in your `CmakeLists.txt file` + +```cmake +# use Boost +SET(BOOST_INCLUDEDIR "/usr/include") +SET(BOOST_LIBRARYDIR "/usr/lib/x86_64-linux-gnu") + +# set Boost +set (Boost_NO_SYSTEM_PATHS ON) +set (Boost_USE_MULTITHREADED ON) +set (Boost_USE_STATIC_LIBS OFF) +set (Boost_USE_STATIC_RUNTIME OFF) +set (BOOST_ALL_DYN_LINK OFF) + +# use OpenMP +find_package(OpenMP) +if (OPENMP_FOUND) + option(HAS_OPENMP "OpenMP enabled" ON) + set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") + set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") +endif() + +# include libraries +find_package(PkgConfig REQUIRED) +find_package(Boost COMPONENTS regex iostreams filesystem REQUIRED) +find_package(LibLZMA REQUIRED) +find_package(ZLIB REQUIRED) +find_package(BZip2 REQUIRED) +pkg_check_modules(DEN2OBJ den2obj REQUIRED) +pkg_check_modules(EIGEN eigen3 REQUIRED) +``` + +and finally add the required libraries to your executable + +```cmake +target_link_libraries(den2obj-shared-example + ${DEN2OBJ_LIBRARIES} + ${Boost_LIBRARIES} + ${LIBLZMA_LIBRARIES} + ${ZLIB_LIBRARIES} + ${BZIP2_LIBRARIES}) +``` + +An example of this is provided in [shared/CMakeLists.txt](shared/CMakeLists.txt). + +## Compilation instructions + +```bash +mkdir build && cd build +cmake ../shared +make -j +``` \ No newline at end of file diff --git a/examples/shared/CMakeLists.txt b/examples/shared/CMakeLists.txt new file mode 100644 index 0000000..478d45a --- /dev/null +++ b/examples/shared/CMakeLists.txt @@ -0,0 +1,84 @@ + #************************************************************************* + # CMakeLists.txt -- This file is part of DEN2OBJ. * + # * + # Author: Ivo Filot * + # * + # DEN2OBJ is free software: you can redistribute it and/or modify * + # it under the terms of the GNU General Public License as published * + # by the Free Software Foundation, either version 3 of the License, * + # or (at your option) any later version. * + # * + # DEN2OBJ is distributed in the hope that it will be useful, * + # but WITHOUT ANY WARRANTY; without even the implied warranty * + # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + # See the GNU General Public License for more details. * + # * + # You should have received a copy of the GNU General Public License * + # along with this program. If not, see http://www.gnu.org/licenses/. * + # * + #*************************************************************************/ + +# set minimum cmake requirements +cmake_minimum_required(VERSION 2.8.12) +project (den2obj-shared-example) + +# add custom directory to look for .cmake files +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake/modules ) + +# Enable release build +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "Setting build type to 'Release' as none was specified.") + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE) + # Set the possible values of build type for cmake-gui + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo") +endif() + +# add OS specific +SET(BOOST_INCLUDEDIR "/usr/include") +SET(BOOST_LIBRARYDIR "/usr/lib/x86_64-linux-gnu") + +# set Boost +set (Boost_NO_SYSTEM_PATHS ON) +set (Boost_USE_MULTITHREADED ON) +set (Boost_USE_STATIC_LIBS OFF) +set (Boost_USE_STATIC_RUNTIME OFF) +set (BOOST_ALL_DYN_LINK OFF) + +# use OpenMP +find_package(OpenMP) +if (OPENMP_FOUND) + option(HAS_OPENMP "OpenMP enabled" ON) + set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") + set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") +endif() + +# include libraries +find_package(PkgConfig REQUIRED) +find_package(Boost COMPONENTS regex iostreams filesystem REQUIRED) +find_package(LibLZMA REQUIRED) +find_package(ZLIB REQUIRED) +find_package(BZip2 REQUIRED) +pkg_check_modules(DEN2OBJ den2obj REQUIRED) +pkg_check_modules(EIGEN eigen3 REQUIRED) + +# Set include folders +include_directories(${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_BINARY_DIR} + ${EIGEN_INCLUDE_DIRS} + ${DEN2OBJ_INCLUDE_DIR} + ${Boost_INCLUDE_DIRS}) + +# Add sources +file(GLOB SOURCES "*.cpp") +add_executable(den2obj-shared-example ${SOURCES}) + +# Set C++17 +add_definitions(-std=c++17 -march=native) + +# Link libraries +target_link_libraries(den2obj-shared-example + ${DEN2OBJ_LIBRARIES} + ${Boost_LIBRARIES} + ${LIBLZMA_LIBRARIES} + ${ZLIB_LIBRARIES} + ${BZIP2_LIBRARIES}) \ No newline at end of file diff --git a/examples/shared/test_shared.cpp b/examples/shared/test_shared.cpp new file mode 100644 index 0000000..4653729 --- /dev/null +++ b/examples/shared/test_shared.cpp @@ -0,0 +1,16 @@ +#include +#include +#include +#include + +int main() { + // construct scalar field + Generator gen; + const std::string filename = "genus2.d2o"; + gen.build_dataset("genus2", filename, D2OFormat::CompressionAlgo::BZIP2); + + auto sf = std::make_unique(filename, + ScalarFieldInputFileType::SFF_D2O); + + return 0; +} \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5256b57..64562c1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -116,11 +116,7 @@ add_library(den2objsources STATIC ${SOURCES}) add_library(den2obj SHARED ${SOURCES}) add_executable(den2obj_exec ${CMAKE_CURRENT_SOURCE_DIR}/den2obj.cpp) set_property(TARGET den2obj_exec PROPERTY OUTPUT_NAME den2obj) -set(DEN2OBJ_HEADERS - isosurface_mesh.h - isosurface.h - scalar_field.h -) +file(GLOB DEN2OBJ_HEADERS "*.h") set_target_properties(den2obj PROPERTIES PUBLIC_HEADER "${DEN2OBJ_HEADERS}") # Set C++17