Skip to content

Commit

Permalink
Clean up CMake + PackageConfig (#39)
Browse files Browse the repository at this point in the history
- Cleaned up old and obsolete stuff from the CMake files
- Modernized the interface library target
- Added PackageConfig.cmake
  • Loading branch information
petiaccja authored Jun 22, 2024
1 parent f64eb78 commit 0959532
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test_no_xsimd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
cmake -E make_directory "${{github.workspace}}/build"
conan install "${{github.workspace}}" --build=missing -pr $PR -pr:b $PR
conan cache clean
cmake -S "${{github.workspace}}" --preset conan-${{ matrix.conan_preset }} -DMATHTER_USE_XSIMD:BOOL=OFF
cmake -S "${{github.workspace}}" --preset conan-${{ matrix.conan_preset }} -DMATHTER_ENABLE_SIMD:BOOL=OFF
- name: Build
run: |
Expand Down
95 changes: 54 additions & 41 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,54 +1,30 @@
cmake_minimum_required(VERSION 3.24.0)

## Mathter CMake
project(Mathter)
# Project version
if (NOT DEFINED MATHTER_VERSION)
set(MATHTER_VERSION 0.0.1)
endif()

# Cpp flags
# Project
enable_language(CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
project(Mathter VERSION ${MATHTER_VERSION})

# Options
option(ENABLE_LLVM_COV "Adds compiler flags to generate LLVM source-based code coverage. Only works with Clang." OFF)
option(MATHTER_USE_XSIMD "Uses XSimd for vectorization of math routines. Uses scalar fallback if turned off." ON)
# Project options
option(MATHTER_ENABLE_SIMD "Enables hand-rolled vectorization. Requires the XSimd library." ON)
set(MATHTER_INIT_MODE "DEFAULT" CACHE STRING "Set default initialization of Mathter types.")
set_property(CACHE MATHTER_INIT_MODE PROPERTY STRINGS DEFAULT NULL INVALID UNINITIALIZED)
option(MATHTER_BUILD_TESTS "Include or exclude tests from the generated project." ON)
option(MATHTER_BUILD_BENCHMARKS "Include or exclude bechmarks from the generated project." ON)
option(ENABLE_LLVM_COV "Adds compiler flags to generate LLVM source-based code coverage. Only works with Clang." OFF)

# Necessary compiler flags
# Global compiler flags
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
if (ENABLE_LLVM_COV)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping -mllvm -enable-name-compression=false")
endif()
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse2 -lpthread")
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj")
message("Using source-based coverage")
add_compile_options("-fprofile-instr-generate" "-fcoverage-mapping" "-mllvm" "-enable-name-compression=false")
add_link_options("-fprofile-instr-generate" "-fcoverage-mapping")
endif()
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Intel")

elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
add_compile_options("/MP")
set(RELWITHDEBINFO_OPTIONS "/Ob2")
set(RELWITHDEBINFO_REMOVE "/Ob1")
foreach(option ${RELWITHDEBINFO_REMOVE})
string(REPLACE ${option} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
endforeach()
add_compile_options("$<$<CONFIG:RELWITHDEBINFO>:${RELEASE_OPTIONS}>")
endif()

# Optional compiler flags
if (MATHTER_INIT_MODE STREQUAL "NULL")
add_compile_definitions(MATHTER_NULL_INITIALIZE=1)
elseif(MATHTER_INIT_MODE STREQUAL "INVALID")
add_compile_definitions(MATHTER_INVALID_INITIALIZE=1)
elseif(MATHTER_INIT_MODE STREQUAL "UNINITIALIZED")
add_compile_definitions(MATHTER_DONT_INITIALIZE=1)
endif()

if (${MATHTER_USE_XSIMD})
message("Vectorization is enabled via the XSimd library.")
add_compile_definitions(MATHTER_USE_XSIMD=1)
else()
message("Vectorization is disabled.")
endif()

# Output directories
Expand All @@ -58,6 +34,43 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# Subdirectories
add_subdirectory(include/Mathter)
add_subdirectory(test)
add_subdirectory(benchmark)
if (${MATHTER_BUILD_TESTS})
add_subdirectory(test)
endif()
if (${MATHTER_BUILD_BENCHMARKS})
add_subdirectory(benchmark)
endif()

# Installation
install(TARGETS Mathter EXPORT MathterTargets
FILE_SET headers DESTINATION "include"
FILE_SET natvis DESTINATION "include"
)

install(EXPORT MathterTargets
FILE MathterTargets.cmake
NAMESPACE Mathter::
DESTINATION lib/cmake/${PROJECT_NAME})

include(CMakePackageConfigHelpers)

set(INCLUDE_INSTALL_DIR "include" CACHE PATH "Location of header files" )

configure_package_config_file(
${PROJECT_NAME}Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION lib/cmake/${PROJECT_NAME}
PATH_VARS INCLUDE_INSTALL_DIR
)

write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
COMPATIBILITY SameMajorVersion
)

install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION lib/cmake/${PROJECT_NAME}
)
15 changes: 15 additions & 0 deletions MathterConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
set(Mathter_VERSION @PROJECT_VERSION@)
set(MATHTER_ENABLE_SIMD @MATHTER_ENABLE_SIMD@)

@PACKAGE_INIT@

set_and_check(Mathter_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@")

include(CMakeFindDependencyMacro)
if (${MATHTER_ENABLE_SIMD})
find_dependency(xsimd REQUIRED)
endif()

include("${CMAKE_CURRENT_LIST_DIR}/MathterTargets.cmake")

check_required_components(Mathter)
70 changes: 64 additions & 6 deletions include/Mathter/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,74 @@
add_library(Mathter INTERFACE)

message("${CMAKE_CURRENT_SOURCE_DIR}/..")
target_include_directories(Mathter INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/..")

target_sources(Mathter
INTERFACE
INTERFACE FILE_SET natvis TYPE HEADERS BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/.." FILES
"Mathter.natvis"
INTERFACE FILE_SET headers TYPE HEADERS BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/.." FILES
"Geometry.hpp"
"IoStream.hpp"
"Matrix.hpp"
"Quaternion.hpp"
"Utility.hpp"
"Vector.hpp"
"Common/Approx.hpp"
"Common/Definitions.hpp"
"Common/DeterministicInitializer.hpp"
"Common/MathUtil.hpp"
"Common/Range.hpp"
"Common/Traits.hpp"
"Decompositions/DecomposeLU.hpp"
"Decompositions/DecomposeQR.hpp"
"Decompositions/DecomposeSVD.hpp"
"Matrix/MatrixArithmetic.hpp"
"Matrix/MatrixCast.hpp"
"Matrix/MatrixCompare.hpp"
"Matrix/MatrixFunction.hpp"
"Matrix/MatrixImpl.hpp"
"Matrix/MatrixVectorArithmetic.hpp"
"Quaternion/QuaternionArithmetic.hpp"
"Quaternion/QuaternionCompare.hpp"
"Quaternion/QuaternionFunction.hpp"
"Quaternion/QuaternionImpl.hpp"
"Quaternion/QuaternionLiterals.hpp"
"Quaternion/QuaternionVectorArithmetic.hpp"
"Swizzle/Swizzle_1.inc.hpp"
"Swizzle/Swizzle_2.inc.hpp"
"Swizzle/Swizzle_3.inc.hpp"
"Swizzle/Swizzle_4.inc.hpp"
"Transforms/IdentityBuilder.hpp"
"Transforms/OrthographicBuilder.hpp"
"Transforms/PerspectiveBuilder.hpp"
"Transforms/Rotation2DBuilder.hpp"
"Transforms/Rotation3DBuilder.hpp"
"Transforms/ScaleBuilder.hpp"
"Transforms/ShearBuilder.hpp"
"Transforms/TranslationBuilder.hpp"
"Transforms/ViewBuilder.hpp"
"Transforms/ZeroBuilder.hpp"
"Vector/VectorArithmetic.hpp"
"Vector/VectorCompare.hpp"
"Vector/VectorConcat.hpp"
"Vector/VectorFunction.hpp"
"Vector/VectorImpl.hpp"
)

set_target_properties(Mathter PROPERTIES VERIFY_INTERFACE_HEADER_SETS ON)

target_compile_features(Mathter INTERFACE cxx_std_17)

if (${MATHTER_USE_XSIMD})
find_package(xsimd)
if (MATHTER_INIT_MODE STREQUAL "NULL")
target_compile_definitions(Mathter INTERFACE MATHTER_NULL_INITIALIZE=1)
elseif(MATHTER_INIT_MODE STREQUAL "INVALID")
target_compile_definitions(Mathter INTERFACE MATHTER_INVALID_INITIALIZE=1)
elseif(MATHTER_INIT_MODE STREQUAL "UNINITIALIZED")
target_compile_definitions(Mathter INTERFACE MATHTER_DONT_INITIALIZE=1)
endif()

if (${MATHTER_ENABLE_SIMD})
message("Vectorization: ON (using XSimd).")
find_package(xsimd REQUIRED)
target_link_libraries(Mathter INTERFACE xsimd)
target_compile_definitions(Mathter INTERFACE MATHTER_ENABLE_SIMD=1)
else()
message("Vectorization: OFF.")
endif()
6 changes: 3 additions & 3 deletions include/Mathter/Vector/VectorFunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void Fill(Vector<T, Dim, Packed>& lhs, U all) {
/// <summary> Calculates the scalar product (dot product) of the two arguments. </summary>
template <class T, int Dim, bool Packed>
T Dot(const Vector<T, Dim, Packed>& lhs, const Vector<T, Dim, Packed>& rhs) {
#if MATHTER_USE_XSIMD
#if MATHTER_ENABLE_SIMD
if constexpr (IsBatched<T, Dim, Packed>()) {
struct G {
static constexpr bool get(unsigned idx, unsigned size) noexcept {
Expand Down Expand Up @@ -175,7 +175,7 @@ Vector<T, 3, Packed> Cross(const std::array<const Vector<T, 3, Packed>*, 2>& arg
/// <summary> Returns the element-wise minimum of arguments </summary>
template <class T, int Dim, bool Packed>
Vector<T, Dim, Packed> Min(const Vector<T, Dim, Packed>& lhs, const Vector<T, Dim, Packed>& rhs) {
#if MATHTER_USE_XSIMD
#if MATHTER_ENABLE_SIMD
if constexpr (IsBatched<T, Dim, Packed>()) {
using B = Batch<T, Dim, Packed>;
const auto lhsv = B::load_unaligned(lhs.data());
Expand All @@ -194,7 +194,7 @@ Vector<T, Dim, Packed> Min(const Vector<T, Dim, Packed>& lhs, const Vector<T, Di
/// <summary> Returns the element-wise maximum of arguments </summary>
template <class T, int Dim, bool Packed>
Vector<T, Dim, Packed> Max(const Vector<T, Dim, Packed>& lhs, const Vector<T, Dim, Packed>& rhs) {
#if MATHTER_USE_XSIMD
#if MATHTER_ENABLE_SIMD
if constexpr (IsBatched<T, Dim, Packed>()) {
using B = Batch<T, Dim, Packed>;
const auto lhsv = B::load_unaligned(lhs.data());
Expand Down
8 changes: 4 additions & 4 deletions include/Mathter/Vector/VectorImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <type_traits>
#include <utility>

#if MATHTER_USE_XSIMD
#if MATHTER_ENABLE_SIMD
#include <xsimd/xsimd.hpp>
#endif

Expand Down Expand Up @@ -117,7 +117,7 @@ constexpr int ExtendedDim() {

template <class T, int Dim, bool Packed>
constexpr int IsBatched() {
#if MATHTER_USE_XSIMD
#if MATHTER_ENABLE_SIMD
if constexpr (!Packed) {
constexpr auto extendedSize = ExtendedDim<T, Dim, Packed>();
using BatchT = typename xsimd::make_sized_batch<T, extendedSize>::type;
Expand All @@ -132,7 +132,7 @@ template <class T, int Dim, bool Packed>
struct BatchTHelper {
static auto GetType() {
if constexpr (IsBatched<T, Dim, Packed>()) {
#if MATHTER_USE_XSIMD
#if MATHTER_ENABLE_SIMD
using B = typename xsimd::make_sized_batch<T, ExtendedDim<T, Dim, Packed>()>::type;
return static_cast<B*>(nullptr);
#else
Expand Down Expand Up @@ -474,7 +474,7 @@ Swizzle<T, Dim, Packed, Indices...>::operator Vector<T2, sizeof...(Indices), Pac
constexpr auto Dim2 = int(sizeof...(Indices));
using V = Vector<T2, Dim2, Packed2>;

#if MATHTER_USE_XSIMD
#if MATHTER_ENABLE_SIMD
if constexpr (IsBatched<T, Dim, Packed>() && Dim2 <= Dim) {
using TI = traits::same_size_int_t<T>;
static_assert(!std::is_void_v<TI> && sizeof(TI) == sizeof(T));
Expand Down

0 comments on commit 0959532

Please sign in to comment.