build: stop relying on CMAKE_BUILD_TYPE to determine the build type (#22051)

Any logic involving CMAKE_BUILD_TYPE is automatically broken as it won't
work with multi-config generators. The only exception is if we
explicitly check whether the current generator is single-config as well.
Instead, use generator expressions or cmake variables that allows to set
options for certain build types only such as
INTERPROCEDURAL_OPTIMIZATION_<CONFIG>.

Opt to generate all headers with optimization level O2 with no debug
information for all build types as that is the simplest way to make it
behave the same for all generators.
This commit is contained in:
dundargoc 2023-02-03 00:00:15 +01:00 committed by GitHub
parent ebd2372f92
commit f9826e1dff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 100 deletions

View File

@ -130,28 +130,6 @@ set(NVIM_API_LEVEL 11) # Bump this after any API change.
set(NVIM_API_LEVEL_COMPAT 0) # Adjust this after a _breaking_ API change.
set(NVIM_API_PRERELEASE true)
set(NVIM_VERSION_BUILD_TYPE "${CMAKE_BUILD_TYPE}")
# NVIM_VERSION_CFLAGS set further below.
# Log level (MIN_LOG_LEVEL in log.h)
if("${MIN_LOG_LEVEL}" MATCHES "^$")
# Minimize logging for release-type builds.
if(CMAKE_BUILD_TYPE STREQUAL "Release"
OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo"
OR CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
message(STATUS "MIN_LOG_LEVEL not specified, default is 3 (ERROR) for release builds")
set(MIN_LOG_LEVEL 3)
else()
message(STATUS "MIN_LOG_LEVEL not specified, default is 1 (INFO)")
set(MIN_LOG_LEVEL 1)
endif()
else()
if(NOT MIN_LOG_LEVEL MATCHES "^[0-3]$")
message(FATAL_ERROR "invalid MIN_LOG_LEVEL: " ${MIN_LOG_LEVEL})
endif()
message(STATUS "MIN_LOG_LEVEL=${MIN_LOG_LEVEL}")
endif()
# Default to -O2 on release builds.
if(CMAKE_C_FLAGS_RELEASE MATCHES "-O3")
message(STATUS "Replacing -O3 in CMAKE_C_FLAGS_RELEASE with -O2")
@ -333,7 +311,6 @@ install_helper(
#
add_subdirectory(src/nvim)
get_directory_property(NVIM_VERSION_CFLAGS DIRECTORY src/nvim DEFINITION NVIM_VERSION_CFLAGS)
add_subdirectory(cmake.config)
add_subdirectory(test/functional/fixtures) # compile test programs
add_subdirectory(runtime)

View File

@ -162,11 +162,19 @@ configure_file (
"${PROJECT_BINARY_DIR}/cmake.config/auto/config.h"
)
# generate version definitions
configure_file (
"${PROJECT_SOURCE_DIR}/cmake.config/versiondef.h.in"
"${PROJECT_BINARY_DIR}/cmake.config/auto/versiondef.h"
)
# Improved :version output on newer cmake versions
if(${CMAKE_VERSION} VERSION_LESS 3.14)
configure_file(
"${PROJECT_SOURCE_DIR}/cmake.config/versiondef-old.h.in"
"${PROJECT_BINARY_DIR}/cmake.config/auto/versiondef.h.gen")
else()
configure_file(
"${PROJECT_SOURCE_DIR}/cmake.config/versiondef.h.in"
"${PROJECT_BINARY_DIR}/cmake.config/auto/versiondef.h.gen")
endif()
file(GENERATE
OUTPUT "${PROJECT_BINARY_DIR}/cmake.config/auto/versiondef.h"
INPUT "${PROJECT_BINARY_DIR}/cmake.config/auto/versiondef.h.gen")
# generate pathdef.c
find_program(WHOAMI_PROG whoami)

View File

@ -0,0 +1,21 @@
#ifndef AUTO_VERSIONDEF_H
#define AUTO_VERSIONDEF_H
#define NVIM_VERSION_MAJOR @NVIM_VERSION_MAJOR@
#define NVIM_VERSION_MINOR @NVIM_VERSION_MINOR@
#define NVIM_VERSION_PATCH @NVIM_VERSION_PATCH@
#define NVIM_VERSION_PRERELEASE "@NVIM_VERSION_PRERELEASE@"
#cmakedefine NVIM_VERSION_MEDIUM "@NVIM_VERSION_MEDIUM@"
#ifndef NVIM_VERSION_MEDIUM
# include "auto/versiondef_git.h"
#endif
#define NVIM_API_LEVEL @NVIM_API_LEVEL@
#define NVIM_API_LEVEL_COMPAT @NVIM_API_LEVEL_COMPAT@
#define NVIM_API_PRERELEASE @NVIM_API_PRERELEASE@
#define NVIM_VERSION_CFLAGS "${CMAKE_C_COMPILER} $<JOIN:$<TARGET_PROPERTY:nvim,COMPILE_OPTIONS>, > -D$<JOIN:$<TARGET_PROPERTY:nvim,COMPILE_DEFINITIONS>, -D> -I$<JOIN:$<TARGET_PROPERTY:nvim,INCLUDE_DIRECTORIES>, -I>"
#define NVIM_VERSION_BUILD_TYPE "$<CONFIG>"
#endif // AUTO_VERSIONDEF_H

View File

@ -15,7 +15,7 @@
#define NVIM_API_LEVEL_COMPAT @NVIM_API_LEVEL_COMPAT@
#define NVIM_API_PRERELEASE @NVIM_API_PRERELEASE@
#define NVIM_VERSION_CFLAGS "@NVIM_VERSION_CFLAGS@"
#define NVIM_VERSION_BUILD_TYPE "@NVIM_VERSION_BUILD_TYPE@"
#define NVIM_VERSION_CFLAGS "${CMAKE_C_COMPILER} $<JOIN:$<TARGET_PROPERTY:nvim,COMPILE_OPTIONS>, > -D$<JOIN:$<TARGET_PROPERTY:nvim,COMPILE_DEFINITIONS>, -D> -I$<JOIN:$<REMOVE_DUPLICATES:$<TARGET_PROPERTY:nvim,INCLUDE_DIRECTORIES>>, -I>"
#define NVIM_VERSION_BUILD_TYPE "$<CONFIG>"
#endif // AUTO_VERSIONDEF_H

View File

@ -1,57 +0,0 @@
function(get_compile_flags _compile_flags)
string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type)
set(compile_flags ${CMAKE_C_COMPILER} ${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${build_type}})
# Get flags set by target_compile_options().
get_target_property(opt main_lib INTERFACE_COMPILE_OPTIONS)
if(opt)
list(APPEND compile_flags ${opt})
endif()
get_target_property(opt nvim COMPILE_OPTIONS)
if(opt)
list(APPEND compile_flags ${opt})
endif()
# Get flags set by target_compile_definitions().
get_target_property(defs main_lib INTERFACE_COMPILE_DEFINITIONS)
if(defs)
foreach(def ${defs})
list(APPEND compile_flags "-D${def}")
endforeach()
endif()
get_target_property(defs nvim COMPILE_DEFINITIONS)
if(defs)
foreach(def ${defs})
list(APPEND compile_flags "-D${def}")
endforeach()
endif()
# Get include directories.
get_target_property(dirs main_lib INTERFACE_INCLUDE_DIRECTORIES)
if(dirs)
foreach(dir ${dirs})
list(APPEND compile_flags "-I${dir}")
endforeach()
endif()
get_target_property(dirs main_lib INTERFACE_SYSTEM_INCLUDE_DIRECTORIES)
if(dirs)
foreach(dir ${dirs})
list(APPEND compile_flags "-I${dir}")
endforeach()
endif()
get_target_property(dirs nvim INCLUDE_DIRECTORIES)
if(dirs)
foreach(dir ${dirs})
list(APPEND compile_flags "-I${dir}")
endforeach()
endif()
list(REMOVE_DUPLICATES compile_flags)
string(REPLACE ";" " " compile_flags "${compile_flags}")
set(${_compile_flags} "${compile_flags}" PARENT_SCOPE)
endfunction()

View File

@ -412,7 +412,15 @@ else()
${EXTERNAL_SOURCES} PROPERTIES COMPILE_FLAGS "${COMPILE_FLAGS} -Wno-conversion -Wno-missing-noreturn -Wno-missing-format-attribute -Wno-double-promotion -Wno-strict-prototypes")
endif()
if(NOT "${MIN_LOG_LEVEL}" MATCHES "^$")
# Log level (MIN_LOG_LEVEL in log.h)
if("${MIN_LOG_LEVEL}" MATCHES "^$")
# Minimize logging for release-type builds.
target_compile_definitions(main_lib INTERFACE MIN_LOG_LEVEL=$<IF:$<CONFIG:Debug>,1,3>)
else()
if(NOT MIN_LOG_LEVEL MATCHES "^[0-3]$")
message(FATAL_ERROR "invalid MIN_LOG_LEVEL: " ${MIN_LOG_LEVEL})
endif()
message(STATUS "MIN_LOG_LEVEL=${MIN_LOG_LEVEL}")
target_compile_definitions(main_lib INTERFACE MIN_LOG_LEVEL=${MIN_LOG_LEVEL})
endif()
@ -435,10 +443,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_OSX_SYSROOT)
list(APPEND gen_cflags "-isysroot")
list(APPEND gen_cflags "${CMAKE_OSX_SYSROOT}")
endif()
string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type)
separate_arguments(C_FLAGS_ARRAY UNIX_COMMAND ${CMAKE_C_FLAGS})
separate_arguments(C_FLAGS_${build_type}_ARRAY UNIX_COMMAND ${CMAKE_C_FLAGS_${build_type}})
set(gen_cflags ${gen_cflags} ${C_FLAGS_${build_type}_ARRAY} ${C_FLAGS_ARRAY})
set(gen_cflags ${gen_cflags} -O2)
set(NVIM_VERSION_GIT_H ${PROJECT_BINARY_DIR}/cmake.config/auto/versiondef_git.h)
add_custom_target(update_version_stamp
@ -653,10 +658,10 @@ endif()
if(NOT LUAJIT_FOUND)
message(STATUS "luajit not found, skipping unit tests")
elseif(CMAKE_BUILD_TYPE MATCHES Debug)
else()
glob_wrapper(UNIT_TEST_FIXTURES ${PROJECT_SOURCE_DIR}/test/unit/fixtures/*.c)
list(APPEND NVIM_SOURCES ${UNIT_TEST_FIXTURES})
target_compile_definitions(main_lib INTERFACE UNIT_TESTING)
target_sources(nvim PRIVATE $<$<CONFIG:Debug>:${UNIT_TEST_FIXTURES}>)
target_compile_definitions(nvim PRIVATE $<$<CONFIG:Debug>:UNIT_TESTING>)
endif()
target_sources(nvim PRIVATE ${NVIM_GENERATED_FOR_SOURCES} ${NVIM_GENERATED_FOR_HEADERS}
@ -681,8 +686,11 @@ endif()
if(ENABLE_LTO)
include(CheckIPOSupported)
check_ipo_supported(RESULT IPO_SUPPORTED)
if(IPO_SUPPORTED AND (NOT CMAKE_BUILD_TYPE MATCHES Debug))
set_target_properties(nvim PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)
if(IPO_SUPPORTED)
set_target_properties(nvim PROPERTIES
INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE
INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL TRUE)
endif()
endif()
@ -942,6 +950,3 @@ add_custom_target(generated-sources DEPENDS
)
add_subdirectory(po)
include(GetCompileFlags)
get_compile_flags(NVIM_VERSION_CFLAGS)