From bde2fa027ba61546b69f5b00ca3e3f154a50bce8 Mon Sep 17 00:00:00 2001 From: tbeu Date: Sun, 19 Nov 2023 22:09:44 +0100 Subject: [PATCH] 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 3fd67660..1c57767f 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).