Skip to content
This repository has been archived by the owner on Nov 2, 2023. It is now read-only.

Commit

Permalink
Switch to CPM
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Ferguson committed Oct 26, 2023
1 parent d77d82c commit 70c1341
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 142 deletions.
55 changes: 36 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ else()
endif()

# Check required CMake version
set(REQUIRED_CMAKE_VERSION "3.14.0")
set(REQUIRED_CMAKE_VERSION "3.18.0")
if(SIMPLE_BVH_TOPLEVEL_PROJECT)
cmake_minimum_required(VERSION ${REQUIRED_CMAKE_VERSION})
else()
Expand All @@ -24,13 +24,33 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/SimpleBVHOptions.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/SimpleBVHOptions.cmake)
endif()

# Enable ccache if available
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
option(SIMPLE_BVH_WITH_CCACHE "Enable ccache when building IPC Toolkit" ${SIMPLE_BVH_TOPLEVEL_PROJECT})
else()
option(SIMPLE_BVH_WITH_CCACHE "Enable ccache when building IPC Toolkit" OFF)
endif()
if(SIMPLE_BVH_WITH_CCACHE AND CCACHE_PROGRAM)
message(STATUS "Enabling Ccache support (${CCACHE_PROGRAM})")
set(ccacheEnv
CCACHE_BASEDIR=${CMAKE_BINARY_DIR}
CCACHE_SLOPPINESS=clang_index_store,include_file_ctime,include_file_mtime,locale,pch_defines,time_macros
)
foreach(lang IN ITEMS C CXX)
set(CMAKE_${lang}_COMPILER_LAUNCHER
${CMAKE_COMMAND} -E env ${ccacheEnv} ${CCACHE_PROGRAM}
)
endforeach()
endif()

################################################################################

project(SimpleBVH
DESCRIPTION "A simple BVH data structure."
LANGUAGES CXX)

option(SIMPLE_BVH_WITH_UNIT_TESTS "Build unit-tests" ${SIMPLE_BVH_TOPLEVEL_PROJECT})
option(SIMPLE_BVH_BUILD_TESTS "Build unit-tests" ${SIMPLE_BVH_TOPLEVEL_PROJECT})

# Set default minimum C++ standard
if(SIMPLE_BVH_TOPLEVEL_PROJECT)
Expand All @@ -42,13 +62,16 @@ endif()
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/simple_bvh/")
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/recipes/")

# General CMake utils
include(simple_bvh_cpm_cache)

################################################################################
# Main library
################################################################################

add_library(simple_bvh
src/BVH.cpp
src/Morton.cpp
src/SimpleBVH/BVH.cpp
src/SimpleBVH/Morton.cpp
)
target_include_directories(simple_bvh PUBLIC src)
add_library(simple_bvh::simple_bvh ALIAS simple_bvh)
Expand All @@ -61,13 +84,13 @@ add_library(simple_bvh::simple_bvh ALIAS simple_bvh)
# Required Libraries
################################################################################

# Extra warnings
include(simple_bvh_warnings)
target_link_libraries(simple_bvh PRIVATE simple_bvh::warnings)

include(eigen)
target_link_libraries(simple_bvh PUBLIC Eigen3::Eigen)

# Extra warnings (link last for highest priority)
include(simple_bvh_warnings)
target_link_libraries(simple_bvh PRIVATE simple_bvh::warnings)

################################################################################
# Compiler options
################################################################################
Expand All @@ -80,14 +103,8 @@ target_compile_features(simple_bvh PUBLIC cxx_std_14)
################################################################################

# Enable unit testing at the root level
if(SIMPLE_BVH_WITH_UNIT_TESTS)
include(CTest)
enable_testing()

# Include Catch2 and provide function `catch_discover_tests` to register tests.
include(catch2)
FetchContent_GetProperties(catch2)
include("${catch2_SOURCE_DIR}/contrib/Catch.cmake")

add_subdirectory(tests)
endif()
if(SIMPLE_BVH_TOPLEVEL_PROJECT AND SIMPLE_BVH_BUILD_TESTS)
include(CTest)
enable_testing()
add_subdirectory(tests)
endif()
33 changes: 33 additions & 0 deletions cmake/recipes/CPM.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
set(CPM_DOWNLOAD_VERSION 0.38.1)

if(CPM_SOURCE_CACHE)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()

# Expand relative path. This is important if the provided path contains a tilde (~)
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)

function(download_cpm)
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION}
)
endfunction()

if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
download_cpm()
else()
# resume download if it previously failed
file(READ ${CPM_DOWNLOAD_LOCATION} check)
if("${check}" STREQUAL "")
download_cpm()
endif()
unset(check)
endif()

include(${CPM_DOWNLOAD_LOCATION})
27 changes: 8 additions & 19 deletions cmake/recipes/catch2.cmake
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
#
# Copyright 2020 Adobe. All rights reserved.
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. You may obtain a copy
# of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.
#
# Catch2 (https://github.com/catchorg/Catch2)
# License: BSL-1.0
if(TARGET Catch2::Catch2)
return()
endif()

message(STATUS "Third-party: creating target 'Catch2::Catch2'")

include(FetchContent)
FetchContent_Declare(
catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.13.6
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(catch2)
option(CATCH_CONFIG_CPP17_STRING_VIEW "Enable support for std::string_view" ON)
option(CATCH_INSTALL_DOCS "Install documentation alongside library" OFF)
option(CATCH_INSTALL_EXTRAS "Install extras alongside library" OFF)

include(CPM)
CPMAddPackage("gh:catchorg/Catch2@3.3.2")
60 changes: 23 additions & 37 deletions cmake/recipes/eigen.cmake
Original file line number Diff line number Diff line change
@@ -1,53 +1,39 @@
#
# Copyright 2020 Adobe. All rights reserved.
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. You may obtain a copy
# of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.
#
# Eigen (https://gitlab.com/libeigen/eigen)
# License: MPL 2.0
if(TARGET Eigen3::Eigen)
return()
endif()

option(EIGEN_WITH_MKL "Use Eigen with MKL" OFF)
option(EIGEN_DONT_VECTORIZE "Disable Eigen vectorization" OFF)
option(EIGEN_MPL2_ONLY "Enable Eigen MPL2 license only" OFF)

if(EIGEN_ROOT)
message(STATUS "Third-party: creating target 'Eigen3::Eigen' for external path: ${EIGEN_ROOT}")
set(EIGEN_INCLUDE_DIRS ${EIGEN_ROOT})
else()
message(STATUS "Third-party: creating target 'Eigen3::Eigen'")
message(STATUS "Third-party: creating target 'Eigen3::Eigen'")

include(FetchContent)
FetchContent_Declare(
eigen
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
GIT_TAG tags/3.3.7
GIT_SHALLOW TRUE
)
FetchContent_GetProperties(eigen)
if(NOT eigen_POPULATED)
FetchContent_Populate(eigen)
endif()
set(EIGEN_INCLUDE_DIRS ${eigen_SOURCE_DIR})

install(DIRECTORY ${EIGEN_INCLUDE_DIRS}/Eigen
DESTINATION include
)
endif()
include(CPM)
CPMAddPackage(
NAME eigen
GITLAB_REPOSITORY libeigen/eigen
GIT_TAG 3.4.0
DOWNLOAD_ONLY ON
)

add_library(Eigen3_Eigen INTERFACE)
add_library(Eigen3::Eigen ALIAS Eigen3_Eigen)

include(GNUInstallDirs)
target_include_directories(Eigen3_Eigen SYSTEM INTERFACE
$<BUILD_INTERFACE:${EIGEN_INCLUDE_DIRS}>
$<BUILD_INTERFACE:${eigen_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
# target_compile_definitions(Eigen3_Eigen INTERFACE EIGEN_MPL2_ONLY)

if(EIGEN_MPL2_ONLY)
target_compile_definitions(Eigen3_Eigen INTERFACE EIGEN_MPL2_ONLY)
endif()

if(EIGEN_DONT_VECTORIZE)
target_compile_definitions(Eigen3_Eigen INTERFACE EIGEN_DONT_VECTORIZE)
endif()

if(EIGEN_WITH_MKL)
# TODO: Checks that, on 64bits systems, `mkl::mkl` is using the LP64 interface
Expand All @@ -68,6 +54,6 @@ endif()
# Install rules
set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME eigen)
set_target_properties(Eigen3_Eigen PROPERTIES EXPORT_NAME Eigen)
install(DIRECTORY ${EIGEN_INCLUDE_DIRS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(DIRECTORY ${eigen_SOURCE_DIR} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(TARGETS Eigen3_Eigen EXPORT Eigen_Targets)
install(EXPORT Eigen_Targets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/eigen NAMESPACE Eigen3::)
install(EXPORT Eigen_Targets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/eigen NAMESPACE Eigen3::)
24 changes: 24 additions & 0 deletions cmake/simple_bvh/simple_bvh_cpm_cache.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# Copyright 2021 Adobe. All rights reserved.
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. You may obtain a copy
# of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.
#

if(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_SOURCE_CACHE_DEFAULT $ENV{CPM_SOURCE_CACHE})
else()
# Set CPM cache folder if unset
file(REAL_PATH "~/.cache/CPM" CPM_SOURCE_CACHE_DEFAULT EXPAND_TILDE)
endif()

set(CPM_SOURCE_CACHE
${CPM_SOURCE_CACHE_DEFAULT}
CACHE PATH "Directory to download CPM dependencies"
)
message(STATUS "Using CPM cache folder: ${CPM_SOURCE_CACHE}")
6 changes: 3 additions & 3 deletions cmake/simple_bvh/simple_bvh_warnings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if(TARGET simple_bvh::warnings)
return()
endif()

set(SIMPLE_BVH_FLAGS
set(SIMPLE_BVH_WARNING_FLAGS
-Wall
-Wextra
-pedantic
Expand Down Expand Up @@ -147,15 +147,15 @@ set(SIMPLE_BVH_FLAGS

# Flags above don't make sense for MSVC
if(MSVC)
set(SIMPLE_BVH_FLAGS)
set(SIMPLE_BVH_WARNING_FLAGS)
endif()

include(CheckCXXCompilerFlag)

add_library(simple_bvh_warnings INTERFACE)
add_library(simple_bvh::warnings ALIAS simple_bvh_warnings)

foreach(FLAG IN ITEMS ${SIMPLE_BVH_FLAGS})
foreach(FLAG IN ITEMS ${SIMPLE_BVH_WARNING_FLAGS})
string(REPLACE "=" "-" FLAG_VAR "${FLAG}")
if(NOT DEFINED IS_SUPPORTED_${FLAG_VAR})
check_cxx_compiler_flag("${FLAG}" IS_SUPPORTED_${FLAG_VAR})
Expand Down
8 changes: 4 additions & 4 deletions src/BVH.cpp → src/SimpleBVH/BVH.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <BVH.hpp>
#include <Morton.hpp>
#include "BVH.hpp"
#include <SimpleBVH/Morton.hpp>

#include <iostream>

namespace BVH {
namespace SimpleBVH {
namespace {
bool box_box_intersection(
const Eigen::Vector3d& min1,
Expand Down Expand Up @@ -201,4 +201,4 @@ bool BVH::box_intersects_box(

return box_box_intersection(bbd0, bbd1, bmin, bmax);
}
} // namespace BVH
} // namespace SimpleBVH
Loading

0 comments on commit 70c1341

Please sign in to comment.