Skip to content

Commit

Permalink
Update the CMake build.
Browse files Browse the repository at this point in the history
Specify the C++ language version.
Don't let MSVC get misled by user code pages.
Get the CMake build to behave more like the automake build.
  • Loading branch information
henricj authored and mywave82 committed Jul 12, 2024
1 parent 6ae0ad7 commit 110fb91
Show file tree
Hide file tree
Showing 8 changed files with 599 additions and 22 deletions.
148 changes: 130 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,137 @@
cmake_minimum_required(VERSION 3.13)
cmake_minimum_required(VERSION 3.16..3.25)

project(libbinio)
list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

include(CheckTypeSize)
check_type_size("long long" SIZEOF_LONG_LONG)
if(HAVE_SIZEOF_LONG_LONG)
set(TYPE_INT "long long")
else()
set(TYPE_INT "long")
###
### Project
###

project(libbinio VERSION 1.5)

if(CMAKE_VERSION VERSION_LESS 3.21)
# Set "top level" if the current CMake is too old to do so in project().
string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" libbinio_IS_TOP_LEVEL)
endif()

###
### C++ Language
###

if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()
if(NOT DEFINED CMAKE_CXX_STANDARD_REQUIRED)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD OFF)
endif()
check_type_size("long double" SIZEOF_LONG_DOUBLE)
if(HAVE_SIZEOF_LONG_DOUBLE)
set(TYPE_FLOAT "long double")
else()
set(TYPE_FLOAT "double")

add_compile_definitions($<$<CONFIG:Debug>:DEBUG>)

if(MSVC)
# Tell MSVC to use UTF-8 when reading the source code and when generating the
# object file. Otherwise, both the compiler's input and output can vary
# depending on the current user code page. (MSVC always uses UTF-8
# internally.)
# https://learn.microsoft.com/en-us/cpp/build/reference/utf-8-set-source-and-executable-character-sets-to-utf-8
add_compile_options("/utf-8")
endif()

###
### Library type
###

if(DEFINED libbinio_BUILD_SHARED_LIBS)
set(BUILD_SHARED_LIBS "${libbinio_BUILD_SHARED_LIBS}")
endif ()

if(BUILD_SHARED_LIBS AND NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION)
message(STATUS "Enabling interprocedural optimization")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_DEBUG OFF)
endif()

# TODO: check library exists
set(ENABLE_IOSTREAM 1)
set(ENABLE_STRING 1)
set(ISO_STDLIB 1)
set(WITH_MATH 1)

if(WIN32)
if(BUILD_SHARED_LIBS)
set(CMAKE_DEBUG_POSTFIX "d")
else()
set(CMAKE_RELEASE_POSTFIX "_static")
set(CMAKE_RELWITHDEBINFO_POSTFIX "_static")
set(CMAKE_DEBUG_POSTFIX "_staticd")
endif()
endif()

###
### Configuration checks
###

include(CheckTypeSize)
include(CheckCXXSourceCompiles)
include(CheckLibraryExists)

# Use a function for the configuration to keep the any variables in their own
# local scope (e.g., CMAKE_REQUIRED_LIBRARIES).
function(configure_libbinio)
if(UNIX)
check_library_exists(m sin "" HAVE_LIB_M)
if(HAVE_LIB_M)
list(APPEND CMAKE_REQUIRED_LIBRARIES m)
set(HAVE_LIB_M 1 PARENT_SCOPE)
endif()
endif()

check_type_size("long long" SIZEOF_LONG_LONG)
if(HAVE_SIZEOF_LONG_LONG)
set(TYPE_INT "long long" CACHE STRING "Integer type")
else()
set(TYPE_INT "long" CACHE STRING "Integer type")
endif()

check_type_size("long double" SIZEOF_LONG_DOUBLE)
if(HAVE_SIZEOF_LONG_DOUBLE)
set(TYPE_FLOAT "long double" CACHE STRING "Floating point type")
else()
set(TYPE_FLOAT "double" CACHE STRING "Floating point type")
endif()

check_cxx_source_compiles(
"
#include <math.h>
int main(int, char**)
{ (void)ldexp(1.0, 5); }
"
HAVE_LDEXP)
if(NOT HAVE_LDEXP)
set(HAVE_LDEXP 0)
endif()

include(TestForANSIStreamHeaders)

if(${CMAKE_NO_ANSI_STREAM_HEADERS})
set(HAVE_STREAMS 0)
else()
set(HAVE_STREAMS 1)
endif()

set(ENABLE_IOSTREAM ${HAVE_STREAMS} CACHE STRING "Enable IOStream")
set(ENABLE_STRING 1 CACHE STRING "Enable string")
set(ISO_STDLIB ${HAVE_STREAMS} CACHE STRING "Use ISO stdlib")
set(WITH_MATH ${HAVE_LDEXP} CACHE STRING "Use math.h")
endfunction()

configure_libbinio()

###
### Core
###

add_subdirectory(doc)
add_subdirectory(src)

option(libbinio_INCLUDE_PACKAGING "Include packaging rules for libbinio" "${libbinio_IS_TOP_LEVEL}")

if(libbinio_INCLUDE_PACKAGING)
add_subdirectory(packaging)
endif()
219 changes: 219 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
{
"version": 4,
"cmakeMinimumRequired": {
"major": 3,
"minor": 23,
"patch": 0
},
"configurePresets": [
{
"name": "default",
"displayName": "Default",
"description": "Default build using Ninja generator",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
}
},
{
"name": "static-lto",
"displayName": "Static LTO",
"inherits": "default",
"cacheVariables": {
"CMAKE_INTERPROCEDURAL_OPTIMIZATION": true
}
},
{
"name": "shared",
"displayName": "Shared",
"inherits": "default",
"cacheVariables": {
"libbinio_BUILD_SHARED_LIBS": true
}
},
{
"name": "windows-base",
"hidden": true,
"inherits": "default",
"displayName": "Windows Static",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"vendor": {
"microsoft.com/VisualStudioSettings/CMake/1.0": {
"hostOS": [
"Windows"
]
}
}
},
{
"name": "windows-lto-base",
"hidden": true,
"inherits": "static-lto",
"displayName": "Windows Static LTO",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"vendor": {
"microsoft.com/VisualStudioSettings/CMake/1.0": {
"hostOS": [
"Windows"
]
}
}
},
{
"name": "windows-shared-base",
"hidden": true,
"inherits": "shared",
"displayName": "Windows Shared",
"description": "Default Windows shared library build",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"vendor": {
"microsoft.com/VisualStudioSettings/CMake/1.0": {
"hostOS": [
"Windows"
]
}
}
},
{
"name": "windows-x64",
"inherits": "windows-base",
"displayName": "Windows x64 Static Release",
"architecture": {
"value": "x64",
"strategy": "external"
}
},
{
"name": "windows-shared-x64",
"inherits": "windows-shared-base",
"displayName": "Windows x64 Shared Release",
"architecture": {
"value": "x64",
"strategy": "external"
}
},
{
"name": "windows-x64-debug",
"inherits": "windows-x64",
"displayName": "Windows x64 Debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "windows-arm64",
"inherits": "windows-base",
"displayName": "Windows arm64 Static Release",
"architecture": {
"value": "arm64",
"strategy": "external"
}
},
{
"name": "windows-shared-arm64",
"inherits": "windows-shared-base",
"displayName": "Windows arm64 Shared Release",
"architecture": {
"value": "arm64",
"strategy": "external"
}
},
{
"name": "windows-arm64-debug",
"inherits": "windows-arm64",
"displayName": "Windows arm64 Debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "windows-x86",
"inherits": "windows-base",
"displayName": "Windows x86 Release",
"architecture": {
"value": "x86",
"strategy": "external"
}
},
{
"name": "windows-x86-debug",
"displayName": "Windows x86 Debug",
"inherits": "windows-x86",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "windows-clang-x64",
"inherits": "windows-base",
"displayName": "Windows Clang x64 Release",
"cacheVariables": {
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++"
},
"architecture": {
"value": "x64",
"strategy": "external"
},
"vendor": {
"microsoft.com/VisualStudioSettings/CMake/1.0": {
"intelliSenseMode": "windows-clang-x64"
}
}
},
{
"name": "windows-clang-x64-debug",
"inherits": "windows-clang-x64",
"displayName": "Windows Clang x64 Debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "linux-base",
"inherits": "default",
"displayName": "Linux Static Release",
"vendor": {
"microsoft.com/VisualStudioSettings/CMake/1.0": {
"hostOS": [ "Linux" ]
},
"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
}
}
},
{
"name": "linux-shared-base",
"inherits": "shared",
"displayName": "Linux Shared Release",
"vendor": {
"microsoft.com/VisualStudioSettings/CMake/1.0": {
"hostOS": [ "Linux" ]
},
"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
}
}
},
{
"name": "linux-debug",
"inherits": "linux-base",
"displayName": "Linux Debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
}
],
"buildPresets": [
{
"name": "default",
"configurePreset": "default"
},
{
"name": "shared",
"configurePreset": "shared"
}
]
}
Loading

0 comments on commit 110fb91

Please sign in to comment.