Skip to content

Commit

Permalink
modernized CMake, enabled support for CMake's FetContent-API
Browse files Browse the repository at this point in the history
  • Loading branch information
jkriege2 committed May 21, 2024
1 parent 28c0034 commit 1c29633
Show file tree
Hide file tree
Showing 14 changed files with 428 additions and 266 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/build_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ jobs:
cd extcmake_tinymat_test
mkdir build
cd build
cmake -G "${{matrix.gen}}" "-DCMAKE_PREFIX_PATH=${{github.workspace}}/install/" -DCMAKE_CXX_COMPILER=${{matrix.cxxcompiler}} -DCMAKE_C_COMPILER=${{matrix.ccompiler}} -B . -S ..
cmake -G "${{matrix.gen}}" "-DCMAKE_PREFIX_PATH=${{github.workspace}}/install/" -DCMAKE_CXX_COMPILER=${{matrix.cxxcompiler}} -B . -S ..
cmake --build . --config Release --verbose
- name: Test CMake-build against TinyMAT, using FetchContent-API
if: success() || failure() # always run even if the previous step fails
run: |
cd examples
cd extcmake_fetchcontent_tinymat_test
mkdir build
cd build
cmake -G "${{matrix.gen}}" -DCMAKE_CXX_COMPILER=${{matrix.cxxcompiler}} -B . -S ..
cmake --build . --config Release --verbose
# - name: Test CMake-build against TinyMAT, using FetchContent-API
# if: success() || failure() # always run even if the previous step fails
# run: |
# cd examples
# cd extcmake_fetchcontent_tinytiff_test
# mkdir build
# cd build
# cmake -G "${{matrix.gen}}" -DCMAKE_CXX_COMPILER=${{matrix.cxxcompiler}} -DCMAKE_C_COMPILER=${{matrix.ccompiler}} -B . -S ..
# cmake --build . --config Release --verbose
# - name: Run Release tests
# run: |
Expand Down
18 changes: 9 additions & 9 deletions .github/workflows/build_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ jobs:
echo ${{github.workspace}}
cmake -G "${{matrix.gen}}" -A ${{matrix.arch}} "-DCMAKE_PREFIX_PATH=${{github.workspace}}/install/" -B . -S ..
cmake --build . --config Release --verbose
# - name: Test CMake-build against TinyMAT, using FetchContent-API
# if: success() || failure() # always run even if the previous step fails
# run: |
# cd examples
# cd extcmake_tinymat_test
# mkdir build
# cd build
# cmake -G "${{matrix.gen}}" -A ${{matrix.arch}} -B . -S ..
# cmake --build . --config Release --verbose
- name: Test CMake-build against TinyMAT, using FetchContent-API
if: success() || failure() # always run even if the previous step fails
run: |
cd examples
cd extcmake_fetchcontent_tinymat_test
mkdir build
cd build
cmake -G "${{matrix.gen}}" -A ${{matrix.arch}} -B . -S ..
cmake --build . --config Release --verbose
# - name: Run Release tests
# run: |
Expand Down
47 changes: 33 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@ include(tinymat_build_type)
if(NOT DEFINED TinyMAT_FILEBACKEND_USE_MEMORY_CACHE)
option(TinyMAT_FILEBACKEND_USE_MEMORY_CACHE "Build with a file-backend, that caches the output in memory, before writing to disk (sets TINYMAT_WRITE_VIA_MEMORY when building)" ON)
endif()
if(NOT DEFINED BUILD_SHARED_LIBS)
option(BUILD_SHARED_LIBS "Build as shared library" ON)
endif()
if(NOT DEFINED TinyMAT_OPENCV_SUPPORT)
find_package(OpenCV)
option(TinyMAT_OPENCV_SUPPORT "Build with Support for OpenCV" ${OpenCV_FOUND})
endif()
if(NOT DEFINED TinyMAT_QT5_SUPPORT)
find_package(Qt5 COMPONENTS Core)
option(TinyMAT_QT5_SUPPORT "Build with Support for Qt5" ${Qt5_FOUND})
endif()
if(NOT DEFINED TinyMAT_BUILD_SHARED_LIBS)
option(TinyMAT_BUILD_SHARED_LIBS "Build as shared library" ON)
endif()
if(NOT DEFINED TinyMAT_BUILD_STATIC_LIBS)
option(TinyMAT_BUILD_STATIC_LIBS "Build as static library" OFF)
if(NOT DEFINED TinyMAT_QT_SUPPORT)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core)
if(${Qt5_FOUND})
set(TinyMAT_QT_SUPPORT ON)
else()
if(${Qt6_FOUND})
set(TinyMAT_QT_SUPPORT ON)
endif()
endif()
option(TinyMAT_QT_SUPPORT "Build with Support for Qt5/6" ${TinyMAT_QT_SUPPORT})
endif()
if(NOT DEFINED TinyMAT_BUILD_DECORATE_LIBNAMES_WITH_BUILDTYPE)
option(TinyMAT_BUILD_DECORATE_LIBNAMES_WITH_BUILDTYPE "If set, the build-type (debug/release/...) is appended to the library name" ON)
Expand All @@ -40,6 +44,11 @@ endif()
if(NOT DEFINED CMAKE_INSTALL_PREFIX)
option(CMAKE_INSTALL_PREFIX "Install directory" ${CMAKE_CURRENT_SOURCE_DIR}/install)
endif()
if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
# We're in the root, define additional targets for developers.
# This prepares the library to be used with CMake's FetchContent
set(TinyMAT_BUILD_EXAMPLES OFF)
endif()


######################################################################################################
Expand Down Expand Up @@ -75,20 +84,30 @@ endif()

######################################################################################################
# check for dependency libs
if (TinyMAT_QT5_SUPPORT)
if (TinyMAT_QT_SUPPORT)
# check for Qt
find_package(Qt5 COMPONENTS Core REQUIRED)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
if(QT_VERSION_MAJOR LESS 5)
message(FATAL_ERROR "Minimum supported Qt version is 5, but you are trying to compile against Qt${QT_VERSION_MAJOR}")
endif()

find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED)
if (${Qt5_FOUND})
message(NOTICE "compiling ${PROJECT_NAME} width Qt5-support")
message(NOTICE "compiling ${PROJECT_NAME} with Qt5-support")
else()
message(FATAL_ERROR "could not find Qt5 on your system")
if (${Qt6_FOUND})
message(NOTICE "compiling ${PROJECT_NAME} with Qt6-support")
else()
message(FATAL_ERROR "could not find Qt5 on your system")
endif()
endif()
endif()
if (TinyMAT_OPENCV_SUPPORT)
# check for Qt
find_package(OpenCV REQUIRED)
if (${OpenCV_FOUND})
message(NOTICE "compiling ${PROJECT_NAME} width OpenCV-support")
message(NOTICE "compiling ${PROJECT_NAME} with OpenCV-support")
else()
message(FATAL_ERROR "could not find OpenCV on your system")
endif()
Expand Down
7 changes: 3 additions & 4 deletions doc/dox/buildinstructions.dox
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ You can use (MinGW) Makefiles by calling:
\code{.sh}
$ mkdir build
$ cd build
$ cmake .. -G "MinGW Makefiles" "-DCMAKE_PREFIX_PATH=<path_to_your_qt_sources>" -DTinyMAT_QT5_SUPPORT=ON -DTinyMAT_OPENCV_SUPPORT=ON -DTinyMAT_BUILD_SHARED_LIBS=ON -DTinyMAT_BUILD_STATIC_LIBS=ON
$ cmake .. -G "MinGW Makefiles" "-DCMAKE_PREFIX_PATH=<path_to_your_qt_sources>" -DTinyMAT_QT_SUPPORT=ON -DTinyMAT_OPENCV_SUPPORT=ON -DBUILD_SHARED_LIBS=ON
$ cmake --build . --config "Debug"
$ cmake --build . --config "Debug" --target install
\endcode
Expand Down Expand Up @@ -47,10 +47,9 @@ Afterwards you can install the library by

The CMake build system offers several configuration variables that you may set/change to modify the outcome of the build:
- \c CMAKE_PREFIX_PATH : add the path to your Qt installatrion to this variable, so the \c find_package(Qt5...) commands find the libraries you want to use
- \c TinyMAT_BUILD_SHARED_LIBS : Build as shared library (default: \c ON )
- \c TinyMAT_BUILD_STATIC_LIBS : Build as static library (default: \c OFF )
- \c BUILD_SHARED_LIBS : Build as static library (default: \c OFF )
- \c TinyMAT_BUILD_DECORATE_LIBNAMES_WITH_BUILDTYPE : If set, the build-type is appended to the library name (default: \c ON )
- \c TinyMAT_QT5_SUPPORT : build with support for Qt5 datatypes ... you'll need to make sure that Qt5 can be found on your system, e.g. by providing \c CMAKE_PREFIX_PATH=<path_to_your_qt_sources>
- \c TinyMAT_QT_SUPPORT : build with support for Qt5/6 datatypes ... you'll need to make sure that Qt5/6 can be found on your system, e.g. by providing \c CMAKE_PREFIX_PATH=<path_to_your_qt_sources>
- \c TinyMAT_OPENCV_SUPPORT : enables support for OpenCV ... you'll need to make sure that Open can be found on your system, e.g. by providing \c CMAKE_PREFIX_PATH=<path_to_your_opencv_sources>
- \c TinyMAT_BUILD_EXAMPLES : Build examples (default: \c ON )
- \c CMAKE_INSTALL_PREFIX : Install directory for the library
Expand Down
68 changes: 45 additions & 23 deletions doc/dox/useinstructions.dox
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,66 @@
\page page_useinstructions Usage/Linking Instructions

This page explains how to link against TinyMAT.


After building and installing TinyMAT you have all files that you need inside the instal directory:
- \c <INSTALLDIR>/include contains all required header files
- \c <INSTALLDIR>/bin contains the shared libraries
- \c <INSTALLDIR>/lib contains the link libraries
- \c <INSTALLDIR>/lib/cmake contains files necessary for CMake's \c find_package() to work
.

\tableofcontents


\section page_buildinstructions_CMAKE Use TinyMAT with CMake
When using CMake to build you application, you can simply use \c target_link_libraries() to link against TinyMAT and use CMake's \c find_package() to enable that (see https://github.com/jkriege2/TinyMAT/tree/master/examples/extcmake_tinymat_test for an example):
\section page_useinstructions_CMAKE Use TinyMAT with CMake
When using CMake to build your application, you can simply use \c target_link_libraries() to link against TinyMAT and use CMake's \c find_package() to enable that (see https://github.com/jkriege2/TinyMAT/tree/master/tests/extcmake_tinyMAT_test for an example):

\code{.cmake}
project(extcmake_tinymat_test CXX)
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.10)

project(extcmake_tinyMAT_test CXX)

# find TinyMAT library
find_package(TinyMAT)

set(EXAMPLE_NAME extcmake_tinymat_test)
# add an executable
add_executable(${PROJECT_NAME}
extcmake_tinyMAT_test.cpp
)

# if you built with CMake and Qt5 support, you also need to include these packages
find_package(Qt5 COMPONENTS Core REQUIRED)
find_package(OpenCV)
# link against TinyMAT
target_link_libraries(${PROJECT_NAME} TinyMAT::TinyMAT)
\endcode

\note Note that you may have to provide CMake with search pathes to find the library, e.g. set \c TinyMAT_DIR to \c <INSTALLDIR>/lib/cmake/TinyMAT so CMake finds the \c TinyMATConfig.cmake file there.

# now find TinyMAT
find_package(TinyMAT REQUIRED)

# add an executable
add_executable(${EXAMPLE_NAME} extcmake_tinymat_test.cpp)

# link against TinyMAT
target_link_libraries(${EXAMPLE_NAME} TinyMAT)
\section page_useinstructions_CMAKE_FETCHCONTENT Use TinyMAT with CMake & FetchCotent-API
When using CMake to build your application, you can also use CMake's FetchContent-API to download and include TinyMAT (see https://github.com/jkriege2/TinyMAT/tree/master/tests/extcmake_fetchcontent_tinyMAT_test for an example and https://cmake.org/cmake/help/latest/module/FetchContent.html for documentation on FetchContent):

\code{.cmake}
# set minimum required CMake-Version
cmake_minimum_required(VERSION 3.23)

# set Project name
project(extcmake_fetchcontent_tinyMAT_test LANGUAGES CXX)

# include TinyMAT via FetchContent-API:
# ... first load the FetchContent-API:
include(FetchContent) # once in the project to include the module
# ... now declare TinyMAT
FetchContent_Declare(TinyMAT
GIT_REPOSITORY https://github.com/jkriege2/TinyMAT.git
# GIT_TAG 3.1.0.0
)

# ... finally make TinyMAT available
FetchContent_MakeAvailable(TinyMAT)

add_executable(${PROJECT_NAME}
extcmake_fetchcontent_tinyMAT_test.cpp
)

target_link_libraries(${PROJECT_NAME} TinyMAT::TinyMAT)
\endcode

Note that you may have to provide the CMake variables \c TinyMAT_DIR (or \c TinyMATShared_DIR\c ) to \c <INSTALLDIR>/lib/cmake/TinyMAT so CMake finds the \c TinyMATConfig.cmake file there.

\section page_buildinstructions_NOCMAKE Use TinyMAT without CMake
\section page_useinstructions_NOCMAKE Use TinyMAT without CMake
You can also link against TinyMAT without using CMake. For this you simply have to supply the library as a parameter to your compile/link run, e.g. for GCC:

\code{.sh}
Expand All @@ -53,4 +74,5 @@ The \c -I -option provides the search directory for include-files (i.e. headers)
Note that you might also have to provide additional libraries, depending on your system.



*/
6 changes: 1 addition & 5 deletions examples/basic_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ set(EXAMPLE_NAME ${PROJECT_NAME}_basic_test)
add_executable(${EXAMPLE_NAME}
test_tinymat.cpp
)
if(TinyMAT_BUILD_STATIC_LIBS)
target_link_libraries(${EXAMPLE_NAME} TinyMAT)
elseif(TinyMAT_BUILD_SHARED_LIBS)
target_link_libraries(${EXAMPLE_NAME} TinyMATShared)
endif()
target_link_libraries(${EXAMPLE_NAME} TinyMAT::TinyMAT)

# Installation
install(TARGETS ${EXAMPLE_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
Expand Down
23 changes: 23 additions & 0 deletions examples/extcmake_fetchcontent_tinymat_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# set minimum required CMake-Version
cmake_minimum_required(VERSION 3.23)

# set Project name
project(extcmake_fetchcontent_tinymat_test LANGUAGES CXX)

# include TinyMAT via FetchContent-API:
# ... first load the FetchContent-API:
include(FetchContent) # once in the project to include the module
# ... now declare TinyMAT
FetchContent_Declare(TinyMAT
GIT_REPOSITORY https://github.com/jkriege2/TinyMAT.git
# GIT_TAG 3.1.0.0
)

# ... finally make TinyMAT available
FetchContent_MakeAvailable(TinyMAT)

add_executable(${PROJECT_NAME}
extcmake_fetchcontent_tinymat_test.cpp
)

target_link_libraries(${PROJECT_NAME} TinyMAT::TinyMAT)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "tinymatwriter.h"


using namespace std;


int main() {
TinyMATWriterFile* mat=TinyMATWriter_open("extcmake_tinymat_test.mat");
if (mat) {
// a matrix in row-major form
double mat1[6]={
1,2,
3,4,
5,6
};

TinyMATWriter_writeMatrix2D_rowmajor(mat, "matrix1", mat1, 2,3);

TinyMATWriter_close(mat);
}
return 0;
}
4 changes: 2 additions & 2 deletions examples/extcmake_tinymat_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ cmake_minimum_required(VERSION 3.0)
set(EXAMPLE_NAME extcmake_tinymat_test)

# now find libTinyMAT
find_package(libTinyMAT REQUIRED)
find_package(TinyMAT REQUIRED)

# add an executable
add_executable(${EXAMPLE_NAME} extcmake_tinymat_test.cpp)

# link against libTinyMAT
target_link_libraries(${EXAMPLE_NAME} libTinyMAT)
target_link_libraries(${EXAMPLE_NAME} TinyMAT::TinyMAT)
12 changes: 3 additions & 9 deletions examples/test_qt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.10)

set(EXAMPLE_NAME ${PROJECT_NAME}_qt_test)

find_package(Qt5 COMPONENTS Core REQUIRED)

add_executable(${EXAMPLE_NAME}
test_tinymat.cpp
)

if(TinyMAT_BUILD_STATIC_LIBS)
target_link_libraries(${EXAMPLE_NAME} TinyMAT)
elseif(TinyMAT_BUILD_SHARED_LIBS)
target_link_libraries(${EXAMPLE_NAME} TinyMATShared)
endif()
target_link_libraries(${EXAMPLE_NAME} Qt5::Core)
target_link_libraries(${EXAMPLE_NAME} TinyMAT::TinyMAT)
target_link_libraries(${EXAMPLE_NAME} Qt${QT_VERSION_MAJOR}::Core)


# Installation
Expand Down
Loading

0 comments on commit 1c29633

Please sign in to comment.