Skip to content

Commit

Permalink
Further modernize CMakefile
Browse files Browse the repository at this point in the history
Increase the minimum required CMake version to 3.16.

Generate config version file.

Export wxAutoExcel target with wxAutoExcel:: namespace and also add wxAutoExcel::wxAutoExcel alias for the library.

Many other small changes.
  • Loading branch information
PBfordev committed Apr 26, 2024
1 parent 5b5b633 commit 4396ab0
Showing 1 changed file with 119 additions and 101 deletions.
220 changes: 119 additions & 101 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
######################################################################
######################################################################
# Author: PB
# Purpose: Primary CMake for wxAutoExcel
# Copyright: (c) 2017 PB <[email protected]>
# Licence: wxWindows licence
######################################################################

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
cmake_minimum_required(VERSION 3.16 FATAL_ERROR)

# parses the wxAutoExcel version from include/wx/wxAutoExcel_version.h
# into ${wxAutoExcel_MAJOR_VER}, ${wxAutoExcel_MINOR_VER}, and ${wxAutoExcel_REL_NUM}
Expand All @@ -19,17 +18,22 @@ project(wxAutoExcel
LANGUAGES CXX
)

if(NOT (CMAKE_SYSTEM_NAME STREQUAL Windows))
if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows)
message(FATAL_ERROR "wxAutoExcel is available only for Microsoft Windows")
endif()

# wxAutoExcel requires wxWidgets 3.1 and higher
find_package(wxWidgets 3.1 COMPONENTS core base REQUIRED)
if(wxWidgets_USE_FILE)
include(${wxWidgets_USE_FILE})
endif()

# list of .h and .cpp files for the wxAutoExcel library
include("${wxAutoExcel_SOURCE_DIR}/build/CMake/files.cmake")

######################################################################
#
# wxAutoExcel library and samples can be built in Debug and/or Release configurations
#
######################################################################

get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(isMultiConfig)
set(CMAKE_CONFIGURATION_TYPES Debug Release)
Expand All @@ -45,29 +49,55 @@ else()
endif()


#################################
######################################################################
#
# Global user-customizable options
#
#################################
######################################################################

option(wxAutoExcel_BUILD_SHARED "Build wxAutoExcel libraries as shared libs" ${BUILD_SHARED_LIBS})
option(wxAutoExcel_BUILD_LINK_WX_SHARED "Link with wxWidgets dynamically" ${BUILD_SHARED_LIBS})
option(wxAutoExcel_BUILD_USE_STATIC_RUNTIME "Link using the static runtime library" OFF)
option(wxAutoExcel_BUILD_USE_PRECOMPILED "Use precompiled headers (requires CMake v3.16+)" ON)
option(wxAutoExcel_BUILD_USE_PRECOMPILED "Use precompiled headers" ON)
set(wxAutoExcel_BUILD_VENDOR "custom" CACHE STRING "Vendor")
option(wxAutoExcel_BUILD_INSTALL "Create install target for the library" OFF)

option(wxAutoExcel_BUILD_SAMPLES "Build samples" ${PROJECT_IS_TOP_LEVEL})

#################################

######################################################################
#
# Compiler and linker options
#
#################################
######################################################################

# projects for the samples are grouped under one folder
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
function(wxAutoExcel_adjust_target_flags target)

target_compile_definitions(${target} PRIVATE __WXMSW__ wxNO_UNSAFE_WXSTRING_CONV $<$<CONFIG:Release>:NDEBUG>)

if(wxAutoExcel_BUILD_LINK_WX_SHARED)
target_compile_definitions(${target} PRIVATE WXUSINGDLL)
endif()

if(DEFINED wxAutoExcel_BUILD_USE_STATIC_RUNTIME AND wxAutoExcel_BUILD_USE_STATIC_RUNTIME)
if(MSVC)
set_target_properties(${target} PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
elseif(MINGW)
target_link_options(${target} PRIVATE -static)
else() # clang
target_link_options(${target} PRIVATE -static-libgcc -static-libstdc++)
endif()
endif()

if(MSVC)
# suppress security warnings
target_compile_definitions(${target} PRIVATE _CRT_SECURE_NO_DEPRECATE _CRT_NON_CONFORMING_SWPRINTFS _SCL_SECURE_NO_WARNINGS)
target_compile_options(${target} PRIVATE /MP /W4)
else() # GCC or clang
target_compile_options(${target} PRIVATE -Wno-deprecated-declarations)
endif()

endfunction()

if(wxAutoExcel_BUILD_SHARED)
set(wxAutoExcel_BUILD_LIB_TYPE SHARED)
Expand All @@ -78,91 +108,62 @@ else()
endif()

if(wxAutoExcel_BUILD_LINK_WX_SHARED)
if(NOT "${wxWidgets_LIB_DIR}" MATCHES "_dll")
if(NOT "${wxWidgets_LIB_DIR}" STREQUAL "wxWidgets_LIB_DIR-NOTFOUND" AND NOT "${wxWidgets_LIB_DIR}" MATCHES "_dll")
MESSAGE(WARNING "When linking with wxWidgets dynamically you should probably replace \"_lib\" with \"_dll\" in the wxWidgets\\wxWidgets_LIB_DIR variable")
endif()
endif()

if(DEFINED wxAutoExcel_BUILD_USE_STATIC_RUNTIME AND wxAutoExcel_BUILD_USE_STATIC_RUNTIME)
if(wxAutoExcel_BUILD_SHARED OR wxAutoExcel_BUILD_LINK_WX_SHARED)
MESSAGE(FATAL_ERROR "The static runtime cannot be used when creating wxAutoExcel DLLs or linking with wxWidgets dynamically")
endif()
if(MSVC)
string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
else()
if(MINGW)
set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} -static")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
else() # clang
set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} -static-libgcc -static-libstdc++")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++")
endif()
endif()
endif()

if(MSVC)
# suppress security warnings
add_compile_definitions(_CRT_SECURE_NO_DEPRECATE _CRT_NON_CONFORMING_SWPRINTFS _SCL_SECURE_NO_WARNINGS)
# set warnings to level 4
string(REGEX REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})

# turn on multi-processor complication
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
else() # GCC or clang
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
if(DEFINED wxAutoExcel_BUILD_USE_STATIC_RUNTIME AND wxAutoExcel_BUILD_USE_STATIC_RUNTIME
AND (wxAutoExcel_BUILD_SHARED OR wxAutoExcel_BUILD_LINK_WX_SHARED))
MESSAGE(FATAL_ERROR "The static runtime cannot be used when creating wxAutoExcel DLLs or linking with wxWidgets dynamically")
endif()

include(${wxWidgets_USE_FILE})

# Initialize variables for quick access to wxAutoExcel dirs
set(wxAutoExcel_RUNTIME_DIR "${wxAutoExcel_BINARY_DIR}/bin")
set(wxAutoExcel_ARCHIVE_DIR "${wxAutoExcel_BINARY_DIR}/lib")
set(wxAutoExcel_RUNTIME_DIR "${CMAKE_BINARY_DIR}/bin")
set(wxAutoExcel_ARCHIVE_DIR "${CMAKE_BINARY_DIR}/lib")

#################################

######################################################################
#
# wxAutoExcel library
#
#################################
######################################################################

# list of .cpp files for the wxAutoExcel library
include("${wxAutoExcel_SOURCE_DIR}/build/CMake/files.cmake")

if(wxAutoExcel_BUILD_SHARED)
list(APPEND SRCS "${wxAutoExcel_SOURCE_DIR}/src/wxAutoExcel_version.rc")
list(APPEND SRCS "${CMAKE_CURRENT_SOURCE_DIR}/src/wxAutoExcel_version.rc")
endif()

add_library(wxAutoExcel ${wxAutoExcel_BUILD_LIB_TYPE} ${SRCS})
add_library(wxAutoExcel::wxAutoExcel ALIAS wxAutoExcel)
target_include_directories(wxAutoExcel PUBLIC
$<BUILD_INTERFACE:${wxAutoExcel_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(wxAutoExcel PUBLIC ${wxWidgets_LIBRARIES})
if(wxAutoExcel_BUILD_SHARED)
target_link_libraries(wxAutoExcel PUBLIC ${wxWidgets_LIBRARIES})
endif()

# Make the library the startup project in MSVS. If the samples are built,
# it will be changed to minimal sample.
if(MSVC)
set_directory_properties(PROPERTIES VS_STARTUP_PROJECT wxAutoExcel)
wxAutoExcel_adjust_target_flags(wxAutoExcel)

if(wxAutoExcel_BUILD_SHARED)
target_compile_definitions(wxAutoExcel PRIVATE WXMAKINGDLL_WXAUTOEXCEL)
# WXUSINGDLL_WXAUTOEXCEL must be defined when building against shared wxAutoExcel build
target_compile_definitions(wxAutoExcel INTERFACE WXUSINGDLL_WXAUTOEXCEL)
endif()

set(CMAKE_DEBUG_POSTFIX d)
if(wxAutoExcel_BUILD_USE_PRECOMPILED)
target_precompile_headers(wxAutoExcel PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include/wx/wxAutoExcel_prec.h")
endif()

set(wxAutoExcel_OUTPUT_NAME_MAIN "wxAutoExcel${wxAutoExcel_VERSION_MAJOR}${wxAutoExcel_VERSION_MINOR}")
set_target_properties(wxAutoExcel PROPERTIES
OUTPUT_NAME "${wxAutoExcel_OUTPUT_NAME_MAIN}"
OUTPUT_NAME_DEBUG "${wxAutoExcel_OUTPUT_NAME_MAIN}${CMAKE_DEBUG_POSTFIX}"
OUTPUT_NAME_DEBUG "${wxAutoExcel_OUTPUT_NAME_MAIN}d"
)

target_compile_definitions(wxAutoExcel PUBLIC __WXMSW__ PRIVATE wxNO_UNSAFE_WXSTRING_CONV)
target_compile_definitions(wxAutoExcel PRIVATE $<$<CONFIG:Release>:NDEBUG>)

if(wxAutoExcel_BUILD_LINK_WX_SHARED)
target_compile_definitions(wxAutoExcel PUBLIC WXUSINGDLL)
endif()

# WXUSINGDLL_WXAUTOEXCEL must be defined when building against shared wxAutoExcel build
if(wxAutoExcel_BUILD_SHARED)
target_compile_definitions(wxAutoExcel INTERFACE WXUSINGDLL_WXAUTOEXCEL)
endif()

set (wxAutoExcel_DLL_NAME_MAIN "wxAutoExcel${wxAutoExcel_VERSION_MAJOR}${wxAutoExcel_VERSION_MINOR}${wxAutoExcel_VERSION_PATCH}")

if(MSVC)
Expand All @@ -179,7 +180,6 @@ else()
endif()
endif()


if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8") # 64-bit target
string(APPEND wxAutoExcel_DLL_NAME_FLAVOR "${wxAutoExcel_COMPILER_ID_STR}_x64")
else()
Expand All @@ -204,7 +204,7 @@ endif()

set_target_properties(wxAutoExcel PROPERTIES
RUNTIME_OUTPUT_NAME "${wxAutoExcel_DLL_NAME_MAIN}_${wxAutoExcel_DLL_NAME_FLAVOR}${VENDOR_SUFFIX}"
RUNTIME_OUTPUT_NAME_DEBUG "${wxAutoExcel_DLL_NAME_MAIN}${CMAKE_DEBUG_POSTFIX}_${wxAutoExcel_DLL_NAME_FLAVOR}${VENDOR_SUFFIX}"
RUNTIME_OUTPUT_NAME_DEBUG "${wxAutoExcel_DLL_NAME_MAIN}d_${wxAutoExcel_DLL_NAME_FLAVOR}${VENDOR_SUFFIX}"
)

# for static and import libraries
Expand All @@ -217,34 +217,42 @@ set_target_properties(wxAutoExcel PROPERTIES
# for DLLs
set_target_properties(wxAutoExcel PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${wxAutoExcel_RUNTIME_DIR}")

if(wxAutoExcel_BUILD_SHARED)
target_compile_definitions(wxAutoExcel PRIVATE WXMAKINGDLL_WXAUTOEXCEL)
endif()
# Make the library the startup project. If the samples are built,
# it will be changed to minimal sample.
set_target_properties(wxAutoExcel PROPERTIES PROJECT_LABEL "wxAutoExcel Library")
# projects for the samples are grouped under one folder
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set_directory_properties(PROPERTIES VS_STARTUP_PROJECT "wxAutoExcel Library")

if(wxAutoExcel_BUILD_USE_PRECOMPILED)
if(${CMAKE_VERSION} VERSION_GREATER "3.15")
target_precompile_headers(wxAutoExcel PRIVATE "${wxAutoExcel_SOURCE_DIR}/include/wx/wxAutoExcel_prec.h")
else()
message(WARNING "Precompiled headers require CMake v3.16 and newer.")
endif()
endif()

######################################################################
#
# wxAutoExcel packaging and installing
#
######################################################################

include(CMakePackageConfigHelpers)

# create configs also in the build dir, so find_package() can use that without installing
export(TARGETS wxAutoExcel NAMESPACE wxAutoExcel:: FILE wxAutoExcelConfig.cmake)
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/wxAutoExcelConfigVersion.cmake" COMPATIBILITY SameMajorVersion)

if(wxAutoExcel_BUILD_INSTALL)
install(DIRECTORY ${wxAutoExcel_SOURCE_DIR}/include/wx DESTINATION include)
install(TARGETS wxAutoExcel EXPORT wxAutoExcelTargets)
install(EXPORT wxAutoExcelTargets FILE wxAutoExcelConfig.cmake DESTINATION lib/cmake/wxAutoExcel)
include(GNUInstallDirs)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/wx DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(TARGETS wxAutoExcel EXPORT wxAutoExcelTargets)
install(EXPORT wxAutoExcelTargets NAMESPACE wxAutoExcel:: FILE wxAutoExcelConfig.cmake DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/wxAutoExcel")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/wxAutoExcelConfigVersion.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/wxAutoExcel")
if(MSVC AND wxAutoExcel_BUILD_SHARED)
install(FILES $<$<CONFIG:Debug>:$<TARGET_PDB_FILE:${PROJECT_NAME}>> DESTINATION bin)
install(FILES $<$<CONFIG:Debug>:$<TARGET_PDB_FILE:${PROJECT_NAME}>> DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
endif()
# create config also in the build dir, so find_package() can use that as well
export(TARGETS wxAutoExcel FILE wxAutoExcelConfig.cmake)

#################################
######################################################################
#
# wxAutoExcel samples
#
#################################
######################################################################

# function wxAutoExcel_add_sample has the following parameters:
# name = name of the sample, must match the name of the sample folder
Expand All @@ -254,25 +262,33 @@ export(TARGETS wxAutoExcel FILE wxAutoExcelConfig.cmake)
function(wxAutoExcel_add_sample name sourceFiles copyFiles)

foreach(file ${sourceFiles})
list(APPEND src "${wxAutoExcel_SOURCE_DIR}/samples/${name}/${file}")
list(APPEND src "${CMAKE_CURRENT_SOURCE_DIR}/samples/${name}/${file}")
endforeach(file)

list(APPEND src "${wxAutoExcel_SOURCE_DIR}/samples/samples.rc")
list(APPEND src "${CMAKE_CURRENT_SOURCE_DIR}/samples/samples.rc")

add_executable(${name} WIN32 ${src})
target_compile_definitions(${name} PRIVATE $<$<CONFIG:Release>:NDEBUG>)
set_target_properties(${name} PROPERTIES FOLDER "Samples")
set_target_properties(${name} PROPERTIES FOLDER "wxAutoExcel Samples")
set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${wxAutoExcel_RUNTIME_DIR})
target_link_libraries(${name} wxAutoExcel)
target_link_libraries(${name} wxAutoExcel::wxAutoExcel ${wxWidgets_LIBRARIES})

# we are using the application manifest from the resource file
if(MSVC)
target_link_options(${name} PRIVATE "/MANIFEST:NO")
endif()
wxAutoExcel_adjust_target_flags(${name})

if(wxAutoExcel_BUILD_USE_PRECOMPILED)
if (${name} STREQUAL "minimal")
target_precompile_headers(${name} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include/wx/wxAutoExcel_prec.h")
else()
target_precompile_headers(${name} REUSE_FROM "minimal")
endif()
endif()

foreach(copyFile ${copyFiles})
add_custom_command(TARGET ${name} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "${wxAutoExcel_SOURCE_DIR}/samples/${name}/${copyFile}" ${wxAutoExcel_RUNTIME_DIR})
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/samples/${name}/${copyFile}" ${wxAutoExcel_RUNTIME_DIR})
endforeach()

endfunction()
Expand All @@ -292,13 +308,15 @@ if(wxAutoExcel_BUILD_SAMPLES)
wxAutoExcel_add_sample(purewin32 "usewxAutoExcel.h;usewxAutoExcel.cpp;purewin32.cpp" "")
endif()

#################################

######################################################################
#
# Configuration report
#
#################################
message(STATUS "Configured wxAutoExcel ${wxAutoExcel_VERSION} for ${CMAKE_GENERATOR} (${CMAKE_CXX_COMPILER}, v${CMAKE_CXX_COMPILER_VERSION})
Build type: ${wxAutoExcel_BUILD_LIB_TYPE}")
######################################################################

message(STATUS "Configured wxAutoExcel ${wxAutoExcel_VERSION} for ${CMAKE_GENERATOR} (${CMAKE_CXX_COMPILER}, v${CMAKE_CXX_COMPILER_VERSION})")
message(STATUS " Build type: ${wxAutoExcel_BUILD_LIB_TYPE}")
if(CMAKE_CONFIGURATION_TYPES)
message(STATUS " Build configuration: ${CMAKE_CONFIGURATION_TYPES}")
else()
Expand All @@ -314,7 +332,7 @@ else()
endif()
message(STATUS " Compiler id string: ${wxAutoExcel_COMPILER_ID_STR}")

message(STATUS " Include folder: ${wxAutoExcel_SOURCE_DIR}/include")
message(STATUS " Include folder: ${CMAKE_CURRENT_SOURCE_DIR}/include")
message(STATUS " Static/Import library folder: ${wxAutoExcel_ARCHIVE_DIR}")
message(STATUS " Runtime folder: ${wxAutoExcel_RUNTIME_DIR}")

Expand Down

0 comments on commit 4396ab0

Please sign in to comment.