Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure cmake lists #50

Merged
merged 6 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 30 additions & 31 deletions .github/workflows/cmake.yml → .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CMake
name: Run Tests

on:
push:
Expand All @@ -11,61 +11,60 @@ env:
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
test:
runs-on: ${{ matrix.os }}

steps:
- name: Checkout Lazperf
uses: actions/checkout@v2
with:
path: lazperf
repository: hobu/laz-perf

- name: Build lazperf
shell: bash
working-directory: lazperf
run: |
mkdir build
cd build
cmake ..
make
sudo make install
strategy:
fail-fast: false
matrix:
os: [ubuntu-18.04, macos-10.15, windows-latest]
python-version: [3.6, 3.7, 3.8, 3.9]

steps:
- name: Checkout Copclib
uses: actions/checkout@v2
with:
submodules: true

- name: Set up Python 3.8
uses: actions/setup-python@v2
- name: Set up Conda for Python ${{ matrix.python-version }}
uses: conda-incubator/setup-miniconda@v2
with:
python-version: 3.8
python-version: ${{ matrix.python-version }}

- name: Free up disk space
if: ${{ runner.os == 'Linux' }}
run: |
sudo rm -rf /usr/share/dotnet

- name: Install dependencies
run: |
conda install -c conda-forge "laz-perf>=2.1.0" Catch2 pybind11 --yes
python -m pip install --upgrade pip
pip install pytest wheel
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
shell:
bash -l {0}

- name: Build Copclib Python Bindings
run: |
pip install .
shell:
bash -l {0}

- name: Configure CMake
# 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 -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
run: cmake -B "${{github.workspace}}/build" -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DWITH_TESTS_AND_EXAMPLES=ON -DWITH_PYTHON=OFF
shell:
bash -l {0}

- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
run: cmake --build "${{github.workspace}}/build" --config ${{env.BUILD_TYPE}}
shell:
bash -l {0}

- name: Test
working-directory: ${{github.workspace}}/build
# 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}} --output-on-failure
shell:
bash -l {0}
219 changes: 116 additions & 103 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,118 +1,131 @@
cmake_minimum_required(VERSION 3.0.0)
project(copc-lib VERSION 1.2.0)
cmake_minimum_required(VERSION 3.15)

find_package(lazperf REQUIRED)
# ref https://github.com/robotology/how-to-export-cpp-library/blob/master/CMakeLists.txt
project(COPCLIB
LANGUAGES CXX C
VERSION 1.2.0)

set (COPC_SHARED_LIB copc-lib)
set (COPC_STATIC_LIB copc-lib-s)
set (COPC_PYTHON_LIB copclib)
# Defines the CMAKE_INSTALL_LIBDIR, CMAKE_INSTALL_BINDIR and many other useful macros.
# See https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html
include(GNUInstallDirs)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

include_directories(include)
### Options
# Shared/Dynamic or Static library?
option(BUILD_SHARED_LIBS "Build libraries as shared as opposed to static" ON)

file(GLOB_RECURSE SRCS
src/*.cpp
)
option(WITH_TESTS_AND_EXAMPLES "Build test and example files." OFF)
option(WITH_PYTHON "Build python bindings." OFF)
option(ONLY_PYTHON "Build only python bindings, for setup.py env" OFF)

if (NOT EMSCRIPTEN)
add_library(${COPC_SHARED_LIB} SHARED ${SRCS})
target_link_libraries(${COPC_SHARED_LIB} LAZPERF::lazperf)
target_include_directories(${COPC_SHARED_LIB} INTERFACE $<INSTALL_INTERFACE:include>)
if (ONLY_PYTHON)
set(WITH_PYTHON ON)
set(WITH_TESTS_AND_EXAMPLES OFF)
set(BUILD_SHARED_LIBS ON)
endif()

add_library(${COPC_STATIC_LIB} STATIC ${SRCS})
target_link_libraries(${COPC_STATIC_LIB} LAZPERF::lazperf)

# Choose package components
set(WITH_TESTS TRUE CACHE BOOL "Choose if unit tests should be built")
set(WITH_EXAMPLES TRUE CACHE BOOL "Choose if examples should be built")
set(WITH_PYTHON TRUE CACHE BOOL "Choose if python bindings should be built")

if (WITH_TESTS OR WITH_EXAMPLES OR WITH_PYTHON)
set(AutzenFilePath "${CMAKE_BINARY_DIR}/test/data/autzen-classified.copc.laz")
if(NOT EXISTS ${AutzenFilePath})
message(STATUS "Downloading test files")
file (DOWNLOAD
"https://github.com/PDAL/data/raw/62e514b6484ec59cd48bb48d5c6fe8a00216a6ac/autzen/autzen-classified.copc.laz"
${AutzenFilePath}
)
endif()

add_executable(example_reader "example/example-reader.cpp")
target_link_libraries(example_reader ${COPC_SHARED_LIB})
add_executable(example_writer "example/example-writer.cpp")
target_link_libraries(example_writer ${COPC_SHARED_LIB})
# Control where libraries and executables are placed during the build.
# With the following settings executables are placed in <the top level of the
# build tree>/bin and libraries/archives in <top level of the build tree>/lib.
# This is particularly useful to run ctests on libraries built on Windows
# machines: tests, which are executables, are placed in the same folders of
# dlls, which are treated as executables as well, so that they can properly
# find the libraries to run. This is a because of missing RPATH on Windows.

# setup.py already sets CMAKE_LIBRARY_OUTPUT_DIRECTORY, we don't want to override it
if (NOT ONLY_PYTHON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")

# For windows, remove the Release/ and Debug/ pathes
foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY} )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} )
endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )
endif()

if(WITH_TESTS)
add_subdirectory(lib/Catch2)

file(GLOB TEST_SRCS
test/*.cpp
)

add_executable(tests ${TEST_SRCS})
target_link_libraries(tests ${COPC_SHARED_LIB} Catch2::Catch2)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/lib/Catch2/contrib")
include(CTest)
include(Catch)
catch_discover_tests(tests)

add_test(NAME example_reader COMMAND example_reader)
add_test(NAME example_writer COMMAND example_writer)
# To build shared libraries in Windows, we set CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS to TRUE.
# See https://cmake.org/cmake/help/v3.4/variable/CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS.html
# See https://blog.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

# Under MSVC, we set CMAKE_DEBUG_POSTFIX to "d" to add a trailing "d" to library
# built in debug mode. In this Windows user can compile, build and install the
# library in both Release and Debug configuration avoiding naming clashes in the
# installation directories.
if(MSVC)
set(CMAKE_DEBUG_POSTFIX "d")
endif()

if(WITH_PYTHON)
if(NOT EXISTS "${CMAKE_SOURCE_DIR}/test/data/autzen-classified.copc.laz")
message(STATUS "Copying test files")
file(COPY "${AutzenFilePath}"
DESTINATION "${CMAKE_SOURCE_DIR}/test/data")
endif()

add_subdirectory(lib/pybind11)
pybind11_add_module(${COPC_PYTHON_LIB} ${SRCS} python/bindings.cpp)
target_link_libraries(${COPC_PYTHON_LIB} PRIVATE ${COPC_STATIC_LIB})
target_compile_definitions(${COPC_PYTHON_LIB} PRIVATE VERSION_INFO=${EXAMPLE_VERSION_INFO})
if(WITH_TESTS)
add_test(NAME python-tests
COMMAND ${PYTHON_EXECUTABLE} -m pytest "${CMAKE_SOURCE_DIR}/test"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/test"
)
add_test(NAME example_reader_py
COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_SOURCE_DIR}/example/example-reader.py"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/example"
)
add_test(NAME example_writer_py
COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_SOURCE_DIR}/example/example-writer.py"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/example"
)
endif()
# Required C++ versioning
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)

# We use
# - InstallBasicPackageFiles (http://robotology.github.io/ycm/gh-pages/v0.8/module/InstallBasicPackageFiles.html)
# - AddUninstallTarget (http://robotology.github.io/ycm/gh-pages/v0.8/module/AddUninstallTarget.html)
# - AddInstallRPATHSupport (http://robotology.github.io/ycm/gh-pages/v0.8/module/AddInstallRPATHSupport.html)
# from YCM. Files are under the cmake subdirectory.
# See https://github.com/robotology/ycm/
# If you don't want to ship these files with your code (recommended), you can
# instead depend on YCM.
# In this case replace the following line with
# find_package(YCM REQUIRED)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

if(WITH_TESTS_AND_EXAMPLES)
enable_testing()
endif()

# Install
set(include_dest "include/copc-lib")
set(lib_dest "lib/cmake/copc-lib")

if (NOT EMSCRIPTEN)

## Targets
install(TARGETS ${COPC_SHARED_LIB} EXPORT ${COPC_SHARED_LIB} LIBRARY DESTINATION lib)
## Export
install(FILES cmake/copc-lib-config.cmake DESTINATION ${lib_dest})
install(EXPORT ${COPC_SHARED_LIB} DESTINATION "${lib_dest}")
## Headers
file(GLOB_RECURSE HDRS
include/*.hpp
)
### Reproduce the folder hierarchy of include/* in destination
foreach(HDR IN LISTS HDRS)
get_filename_component(FILE_DIR ${HDR} DIRECTORY)
file(RELATIVE_PATH REL_PATH "${CMAKE_CURRENT_SOURCE_DIR}/include" "${FILE_DIR}")
install(FILES ${HDR} DESTINATION "include/${REL_PATH}")
endforeach()
# Find required packages
find_package(LAZPERF 2.1.0 REQUIRED)

# Enable RPATH support for installed binaries and libraries
include(AddInstallRPATHSupport)
add_install_rpath_support(BIN_DIRS "${CMAKE_INSTALL_FULL_BINDIR}"
LIB_DIRS "${CMAKE_INSTALL_FULL_LIBDIR}"
INSTALL_NAME_DIR "${CMAKE_INSTALL_FULL_LIBDIR}"
USE_LINK_PATH)

### Compile- and install-related commands.
add_subdirectory(cpp)

# Create and install CMake configuration files for your project that are
# necessary to for other projects to call find_package(LibTemplateCMake).
#
# Note that it is extremely important to use exactly the project name while
# installing configuration files (you can use PROJECT_NAME variable to avoid
# any possible error). This is required to allow find_package() to properly
# look for the installed library in system path, in particular in Windows when
# the installation is performed in the default path.
#
# install_basic_package_files() comes with many input parameters to customize
# the configuration files. The parameters used in the following call provide
# basic versions of CMake configuration files.
# See install_basic_package_files() documentation found in ./cmake folder.
#
# Note that if your library depends from other libraries, you are probably
# required to used the install_basic_package_files() DEPENDENCIES option.
include(InstallBasicPackageFiles)
install_basic_package_files(${PROJECT_NAME}
VERSION ${${PROJECT_NAME}_VERSION}
COMPATIBILITY AnyNewerVersion
VARS_PREFIX ${PROJECT_NAME}
DEPENDENCIES "LAZPERF 2.1.0 REQUIRED"
FIRST_TARGET copc-lib
NO_CHECK_REQUIRED_COMPONENTS_MACRO)
# Add the uninstall target
include(AddUninstallTarget)

# Build Submodules
if (WITH_TESTS_AND_EXAMPLES)
add_subdirectory(test)
add_subdirectory(example)
endif()

if(WITH_PYTHON)
add_subdirectory(python)
endif()
Loading