From e333695e56f0a29f31631c84d81d2f54381680c3 Mon Sep 17 00:00:00 2001 From: tbeu Date: Sun, 19 Nov 2023 22:09:44 +0100 Subject: [PATCH 1/2] Add CMake option to link MSVC runtime library statically --- CMakeLists.txt | 3 +++ README | 2 ++ README.md | 2 ++ cmake/options.cmake | 16 ++++++++++++++++ documentation/build.texi | 2 ++ 5 files changed, 25 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 745f549e..40ba4858 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,9 @@ cmake_minimum_required(VERSION 3.7) if(POLICY CMP0074) cmake_policy(SET CMP0074 NEW) endif() +if(POLICY CMP0091) + cmake_policy(SET CMP0091 NEW) +endif() project(matio VERSION 1.5.26 diff --git a/README b/README index c658d64b..4fc76cf6 100644 --- a/README +++ b/README @@ -191,6 +191,8 @@ Table of Contents * 'MATIO_SHARED:BOOL=ON' This option builds the matio library as shared object (i.e., a dynamic link library on Windows). + * 'MATIO_STATIC_LINK_CRT:BOOL=OFF' + This option links the MSVC runtime library statically. * 'MATIO_WITH_HDF5:BOOL=ON' This option enables CMake to check for availability of the HDF5 library (see section 2.1.2 for information about HDF5). diff --git a/README.md b/README.md index 90224c0d..af986934 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,8 @@ This flag enables the support for version 7.3 MAT files. This option enables position-independent code (PIC), i.e., compilation with the `-fPIC` flag. It is ignored for Visual Studio builds. * `MATIO_SHARED:BOOL=ON` This option builds the matio library as shared object (i.e., a dynamic link library on Windows). +* `MATIO_STATIC_LINK_CRT:BOOL=OFF` +This option links the MSVC runtime library statically. * `MATIO_WITH_HDF5:BOOL=ON` This option enables CMake to check for availability of the HDF5 library (see section [2.1.2](#212-hdf5) for information about HDF5). * `MATIO_WITH_ZLIB:BOOL=ON` diff --git a/cmake/options.cmake b/cmake/options.cmake index f2e36459..f28ec42d 100644 --- a/cmake/options.cmake +++ b/cmake/options.cmake @@ -17,6 +17,22 @@ option(MATIO_SHARED "Build shared matio library, disable for static library" ON) # Option to enable position-independent code (PIC) option(MATIO_PIC "Enable position-independent code (PIC), i.e., compilation with the -fPIC flag" ON) +# Option to link the MSVC runtime library statically +option(MATIO_STATIC_LINK_CRT "Link the MSVC runtime library statically" OFF) + +if(POLICY CMP0091) + # CMake >= 3.15 has CMAKE_MSVC_RUNTIME_LIBRARY to set the MSVCC runtime library + if(WIN32 AND CMAKE_GENERATOR MATCHES "Visual Studio .*|NMake .*") + if(MATIO_STATIC_LINK_CRT) + message(STATUS "Configuring to link the MSVC runtime library statically") + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + else() + message(STATUS "Configuring to link the MSVC runtime library dynamically") + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") + endif() + endif() +endif() + # Build with hdf5 support option(MATIO_WITH_HDF5 "Check for HDF5 library" ON) diff --git a/documentation/build.texi b/documentation/build.texi index 1306bc62..63ec6565 100644 --- a/documentation/build.texi +++ b/documentation/build.texi @@ -103,6 +103,8 @@ Visual Studio builds. @item MATIO_SHARED:BOOL=ON This option builds the matio library as shared object (i.e., a dynamic link library on Windows). +@item MATIO_STATIC_LINK_CRT:BOOL=OFF +This option links the MSVC runtime library statically. @item MATIO_WITH_HDF5:BOOL=ON This option enables CMake to check for availability of the HDF5 library (see section 2.1.2 for information about HDF5). From 48fe47acb77539d828d7b8d8a60403be7ae17a2b Mon Sep 17 00:00:00 2001 From: tbeu Date: Sat, 10 Feb 2024 19:17:05 +0100 Subject: [PATCH 2/2] Rename to MATIO_STATIC_LINK_MSVC_RT --- .github/workflows/cmake.yml | 10 +++++----- README | 2 +- README.md | 2 +- cmake/options.cmake | 13 ++++++------- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index a3ec48cf..e7264ddc 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -59,11 +59,11 @@ jobs: echo "/Users/runner/Library/Python/${{ matrix.python-version }}/bin" >> $GITHUB_PATH fi if [ "${{ matrix.compiler }}" == "msvc" ]; then - cmake -S . -B build -DMATIO_USE_CONAN=ON -DMATIO_SHARED=OFF + cmake -S . -B build -DMATIO_USE_CONAN=ON -DMATIO_SHARED=OFF -DMATIO_STATIC_LINK_MSVC_RT=ON --loglevel VERBOSE elif [ "${{ matrix.compiler }}" == "mingw" ]; then - cmake -S . -B build -DCMAKE_BUILD_TYPE=${{ matrix.configuration }} -DMATIO_USE_CONAN=ON -DMATIO_SHARED=OFF -G "MinGW Makefiles" + cmake -S . -B build -DCMAKE_BUILD_TYPE=${{ matrix.configuration }} -DMATIO_USE_CONAN=ON -DMATIO_SHARED=OFF -G "MinGW Makefiles" --loglevel VERBOSE else - cmake -S . -B build -DCMAKE_BUILD_TYPE=${{ matrix.configuration }} -DMATIO_USE_CONAN=ON -DMATIO_SHARED=OFF + cmake -S . -B build -DCMAKE_BUILD_TYPE=${{ matrix.configuration }} -DMATIO_USE_CONAN=ON -DMATIO_SHARED=OFF --loglevel VERBOSE fi - name: Build with ${{ matrix.compiler }} run: | @@ -105,7 +105,7 @@ jobs: pkg_add cmake run: | set -e - cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DMATIO_SHARED=OFF -DMATIO_MAT73=OFF + cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DMATIO_SHARED=OFF -DMATIO_MAT73=OFF --loglevel VERBOSE cmake --build build -- -j8 ./build/test_snprintf ./build/test_mat -H @@ -137,7 +137,7 @@ jobs: run: | set -e mkdir build - cmake -S . -Bbuild -DCMAKE_BUILD_TYPE=Release -DMATIO_SHARED=OFF -DMATIO_MAT73=OFF + cmake -S . -Bbuild -DCMAKE_BUILD_TYPE=Release -DMATIO_SHARED=OFF -DMATIO_MAT73=OFF --loglevel VERBOSE cmake --build build -- -j8 ./build/test_snprintf ./build/test_mat -H diff --git a/README b/README index 4fc76cf6..5af2ddee 100644 --- a/README +++ b/README @@ -191,7 +191,7 @@ Table of Contents * 'MATIO_SHARED:BOOL=ON' This option builds the matio library as shared object (i.e., a dynamic link library on Windows). - * 'MATIO_STATIC_LINK_CRT:BOOL=OFF' + * 'MATIO_STATIC_LINK_MSVC_RT:BOOL=OFF' This option links the MSVC runtime library statically. * 'MATIO_WITH_HDF5:BOOL=ON' This option enables CMake to check for availability of the diff --git a/README.md b/README.md index af986934..30cae574 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ This flag enables the support for version 7.3 MAT files. This option enables position-independent code (PIC), i.e., compilation with the `-fPIC` flag. It is ignored for Visual Studio builds. * `MATIO_SHARED:BOOL=ON` This option builds the matio library as shared object (i.e., a dynamic link library on Windows). -* `MATIO_STATIC_LINK_CRT:BOOL=OFF` +* `MATIO_STATIC_LINK_MSVC_RT:BOOL=OFF` This option links the MSVC runtime library statically. * `MATIO_WITH_HDF5:BOOL=ON` This option enables CMake to check for availability of the HDF5 library (see section [2.1.2](#212-hdf5) for information about HDF5). diff --git a/cmake/options.cmake b/cmake/options.cmake index f28ec42d..17c56405 100644 --- a/cmake/options.cmake +++ b/cmake/options.cmake @@ -17,17 +17,16 @@ option(MATIO_SHARED "Build shared matio library, disable for static library" ON) # Option to enable position-independent code (PIC) option(MATIO_PIC "Enable position-independent code (PIC), i.e., compilation with the -fPIC flag" ON) -# Option to link the MSVC runtime library statically -option(MATIO_STATIC_LINK_CRT "Link the MSVC runtime library statically" OFF) - if(POLICY CMP0091) # CMake >= 3.15 has CMAKE_MSVC_RUNTIME_LIBRARY to set the MSVCC runtime library - if(WIN32 AND CMAKE_GENERATOR MATCHES "Visual Studio .*|NMake .*") - if(MATIO_STATIC_LINK_CRT) - message(STATUS "Configuring to link the MSVC runtime library statically") + if(MSVC) + # Option to link the MSVC runtime library statically + option(MATIO_STATIC_LINK_MSVC_RT "Link the MSVC runtime library statically" OFF) + if(MATIO_STATIC_LINK_MSVC_RT) + message(VERBOSE "Configuring to link the MSVC runtime library statically") set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") else() - message(STATUS "Configuring to link the MSVC runtime library dynamically") + message(VERBOSE "Configuring to link the MSVC runtime library dynamically") set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") endif() endif()