Skip to content

Commit

Permalink
build: stop relying on CMAKE_BUILD_TYPE to determine the build type (n…
Browse files Browse the repository at this point in the history
…eovim#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.
  • Loading branch information
dundargoc authored and yesean committed Mar 25, 2023
1 parent c5229e2 commit 081a805
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 100 deletions.
23 changes: 0 additions & 23 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 13 additions & 5 deletions cmake.config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions cmake.config/versiondef-old.h.in
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions cmake.config/versiondef.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
57 changes: 0 additions & 57 deletions cmake/GetCompileFlags.cmake

This file was deleted.

31 changes: 18 additions & 13 deletions src/nvim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -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}
Expand All @@ -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()

Expand Down Expand Up @@ -942,6 +950,3 @@ add_custom_target(generated-sources DEPENDS
)

add_subdirectory(po)

include(GetCompileFlags)
get_compile_flags(NVIM_VERSION_CFLAGS)

0 comments on commit 081a805

Please sign in to comment.