From 87e5a4131666e44354f280538cbc6bbe52225092 Mon Sep 17 00:00:00 2001 From: Florian Walch Date: Mon, 28 Sep 2015 12:14:38 +0200 Subject: [PATCH 1/4] CMake: Add custom Dev build type. Introduce new build type Dev that replaces RelWithDebInfo for development builds off master and has optimizations, debug info, and logging enabled. Keep assertions enabled for RelWithDebInfo. --- CMakeLists.txt | 62 ++++++++++++++++++++++++++++------------ contrib/local.mk.example | 11 ++++--- 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3db32f1966..dcc5edb52a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,12 +38,12 @@ endif() # Set available build types for CMake GUIs. # A different build type can still be set by -DCMAKE_BUILD_TYPE=... set_property(CACHE CMAKE_BUILD_TYPE PROPERTY - STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo") + STRINGS "Debug" "Dev" "Release" "MinSizeRel" "RelWithDebInfo") # Set default build type. if(NOT CMAKE_BUILD_TYPE) - message(STATUS "CMAKE_BUILD_TYPE not given; setting to 'RelWithDebInfo'.") - set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose the type of build." FORCE) + message(STATUS "CMAKE_BUILD_TYPE not given, defaulting to 'Dev'.") + set(CMAKE_BUILD_TYPE "Dev" CACHE STRING "Choose the type of build." FORCE) endif() # Version tokens @@ -74,6 +74,46 @@ if(CMAKE_C_FLAGS_RELEASE MATCHES "-O3") string(REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}") endif() +# Enable assertions for RelWithDebInfo. +if(CMAKE_C_FLAGS_RELWITHDEBINFO MATCHES DNDEBUG) + string(REPLACE "-DNDEBUG" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}") +endif() + +# Set build flags for custom Dev build type. +# -DNDEBUG purposely omitted because we want assertions. +if(MSVC) + SET(CMAKE_C_FLAGS_DEV "" + CACHE STRING "Flags used by the compiler during development (optimized, but with debug info and logging) builds." + FORCE) +else() + if(CMAKE_COMPILER_IS_GNUCC) + check_c_compiler_flag(-Og HAS_OG_FLAG) + else() + set(HAS_OG_FLAG 0) + endif() + + if(HAS_OG_FLAG) + set(CMAKE_C_FLAGS_DEV "-Og -g" + CACHE STRING "Flags used by the compiler during development (optimized, but with debug info and logging) builds." + FORCE) + else() + set(CMAKE_C_FLAGS_DEV "-O2 -g" + CACHE STRING "Flags used by the compiler during development (optimized, but with debug info and logging) builds." + FORCE) + endif() +endif() +SET(CMAKE_EXE_LINKER_FLAGS_DEV "" + CACHE STRING "Flags used for linking binaries during development (optimized, but with debug info and logging) builds." + FORCE) +SET(CMAKE_SHARED_LINKER_FLAGS_DEV "" + CACHE STRING "Flags used by the shared libraries linker during development (optimized, but with debug info and logging) builds." + FORCE) + +MARK_AS_ADVANCED( + CMAKE_C_FLAGS_DEV + CMAKE_EXE_LINKER_FLAGS_DEV + CMAKE_SHARED_LINKER_FLAGS_DEV) + # Enable -Wconversion. if(NOT MSVC) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion") @@ -177,22 +217,6 @@ if(TRAVIS_CI_BUILD) add_definitions(-Werror) endif() -if(CMAKE_COMPILER_IS_GNUCC) - check_c_compiler_flag(-Og HAS_OG_FLAG) -else() - set(HAS_OG_FLAG 0) -endif() - -# Set custom build flags for RelWithDebInfo. -# -DNDEBUG purposely omitted because we want assertions. -if(HAS_OG_FLAG) - set(CMAKE_C_FLAGS_RELWITHDEBINFO "-Og -g" - CACHE STRING "Flags used by the compiler during release builds with debug info." FORCE) -elseif(NOT MSVC) - set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g" - CACHE STRING "Flags used by the compiler during release builds with debug info." FORCE) -endif() - if(CMAKE_BUILD_TYPE MATCHES Debug) set(DEBUG 1) else() diff --git a/contrib/local.mk.example b/contrib/local.mk.example index 4e7b01a39f..22aac4fda7 100644 --- a/contrib/local.mk.example +++ b/contrib/local.mk.example @@ -13,16 +13,19 @@ # Sets the build type; defaults to Debug. Valid values: # -# - Debug: Disables optimizations (-O0), enables debug information. +# - Debug: Disables optimizations (-O0), enables debug information and logging. # -# - RelWithDebInfo: Enables all optimizations that do not interfere with +# - Dev: Enables all optimizations that do not interfere with # debugging (-Og if available, -O2 and -g if not). -# Enables debug information. +# Enables debug information and logging. +# +# - RelWithDebInfo: Enables optimizations (-O2) and debug information. +# Disables logging. # # - MinSizeRel: Enables all -O2 optimization that do not typically # increase code size, and performs further optimizations # designed to reduce code size (-Os). -# Disables debug information. +# Disables debug information and logging. # # - Release: Same as RelWithDebInfo, but disables debug information. # From cb87ea7e82f656711de27121bd67cea9afeab40b Mon Sep 17 00:00:00 2001 From: Florian Walch Date: Mon, 28 Sep 2015 12:15:55 +0200 Subject: [PATCH 2/4] CMake: Disable logging for release build types. These are: Release, MinSizeRel, and RelWithDebInfo. Closes #2913. --- CMakeLists.txt | 11 +++++++++++ src/nvim/log.h | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dcc5edb52a..f7dd444105 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,6 +74,17 @@ if(CMAKE_C_FLAGS_RELEASE MATCHES "-O3") string(REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}") endif() +# Disable logging for release-type builds. +if(NOT CMAKE_C_FLAGS_RELEASE MATCHES DDISABLE_LOG) + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -DDISABLE_LOG") +endif() +if(NOT CMAKE_C_FLAGS_MINSIZEREL MATCHES DDISABLE_LOG) + set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} -DDISABLE_LOG") +endif() +if(NOT CMAKE_C_FLAGS_RELWITHDEBINFO MATCHES DDISABLE_LOG) + set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -DDISABLE_LOG") +endif() + # Enable assertions for RelWithDebInfo. if(CMAKE_C_FLAGS_RELWITHDEBINFO MATCHES DNDEBUG) string(REPLACE "-DNDEBUG" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}") diff --git a/src/nvim/log.h b/src/nvim/log.h index 152e90760e..32b7276f14 100644 --- a/src/nvim/log.h +++ b/src/nvim/log.h @@ -19,7 +19,7 @@ #define ELOGN(...) // Logging is disabled if NDEBUG or DISABLE_LOG is defined. -#ifdef NDEBUG +#if !defined(DISABLE_LOG) && defined(NDEBUG) # define DISABLE_LOG #endif From a83020922d0dcdde4ca29394342901ea4bc71e8f Mon Sep 17 00:00:00 2001 From: Florian Walch Date: Mon, 28 Sep 2015 12:17:19 +0200 Subject: [PATCH 3/4] version: Prepare for releases. * Hide commit information from --version if we can't find any (e.g. when building from tarball). To define a release in CMake, set NVIM_VERSION_PRERELEASE to "". This will modify --version output to: * Show annotated Git tag instead of commit hash (NVIM_VERSION_COMMIT). * Hide commit date (NVIM_VERSION_BUILD). --- CMakeLists.txt | 27 ++++++++++++++++----------- config/versiondef.h.in | 2 +- src/nvim/version.c | 4 ++++ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f7dd444105..f4e5e51bcb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,22 +47,27 @@ if(NOT CMAKE_BUILD_TYPE) endif() # Version tokens -include(GetGitRevisionDescription) -file(TO_NATIVE_PATH ${CMAKE_CURRENT_LIST_DIR}/.git GIT_DIR) -get_git_head_revision(GIT_REFSPEC NVIM_VERSION_COMMIT) -if(NOT NVIM_VERSION_COMMIT) - set(NVIM_VERSION_COMMIT "?") -endif() set(NVIM_VERSION_MAJOR 0) set(NVIM_VERSION_MINOR 0) set(NVIM_VERSION_PATCH 0) set(NVIM_VERSION_PRERELEASE "-alpha") -# TODO(justinmk): UTC time would be nice here #1071 -git_timestamp(GIT_TIMESTAMP) -# TODO(justinmk): do not set this for "release" builds #1071 -if(GIT_TIMESTAMP) - set(NVIM_VERSION_BUILD "+${GIT_TIMESTAMP}") + +include(GetGitRevisionDescription) +file(TO_NATIVE_PATH ${CMAKE_CURRENT_LIST_DIR}/.git GIT_DIR) +if(NVIM_VERSION_PRERELEASE) + get_git_head_revision(GIT_REFSPEC NVIM_VERSION_COMMIT) + + # TODO(justinmk): UTC time would be nice here #1071 + git_timestamp(GIT_TIMESTAMP) + if(GIT_TIMESTAMP) + set(NVIM_VERSION_BUILD "+${GIT_TIMESTAMP}") + endif() +else() + # If possible, get the Git tag for the current revision. + git_get_exact_tag(NVIM_VERSION_COMMIT) + set(NVIM_VERSION_BUILD "") endif() + set(NVIM_VERSION_BUILD_TYPE "${CMAKE_BUILD_TYPE}") # NVIM_VERSION_CFLAGS set further below. diff --git a/config/versiondef.h.in b/config/versiondef.h.in index a177e599ba..bef099e55f 100644 --- a/config/versiondef.h.in +++ b/config/versiondef.h.in @@ -6,7 +6,7 @@ #define NVIM_VERSION_PATCH @NVIM_VERSION_PATCH@ #define NVIM_VERSION_PRERELEASE "@NVIM_VERSION_PRERELEASE@" #define NVIM_VERSION_BUILD "@NVIM_VERSION_BUILD@" -#define NVIM_VERSION_COMMIT "@NVIM_VERSION_COMMIT@" +#cmakedefine NVIM_VERSION_COMMIT "@NVIM_VERSION_COMMIT@" #define NVIM_VERSION_CFLAGS "@NVIM_VERSION_CFLAGS@" #define NVIM_VERSION_BUILD_TYPE "@NVIM_VERSION_BUILD_TYPE@" diff --git a/src/nvim/version.c b/src/nvim/version.c index 7cc72705b6..c8cbcf2439 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -36,7 +36,9 @@ char *Version = VIM_VERSION_SHORT; char *longVersion = NVIM_VERSION_LONG; char *longVersionWithDate = NVIM_VERSION_LONG " (compiled " __DATE__ " " __TIME__ ")"; char *mediumVersion = NVIM_VERSION_MEDIUM; +#ifdef NVIM_VERSION_COMMIT char *version_commit = "Commit: " NVIM_VERSION_COMMIT; +#endif char *version_buildtype = "Build type: " NVIM_VERSION_BUILD_TYPE; char *version_cflags = "Compilation: " NVIM_VERSION_CFLAGS; @@ -1024,7 +1026,9 @@ void list_version(void) // When adding features here, don't forget to update the list of // internal variables in eval.c! MSG(longVersionWithDate); +#ifdef NVIM_VERSION_COMMIT MSG(version_commit); +#endif MSG(version_buildtype); MSG(version_cflags); From 90909e9362dacb5ece17c7baaffe33b16d106ce7 Mon Sep 17 00:00:00 2001 From: Florian Walch Date: Tue, 29 Sep 2015 21:22:55 +0200 Subject: [PATCH 4/4] CMake: Force use of project directory to look for Git data. Before this change, building Neovim would recursively search parent directories for a .git directory. If Neovim was downloaded as a tarball (i.e. without a .git directory), but placed in a subdirectory of a Git repository, this caused a CMake error. Such a situation could occur when packaging Neovim, for example. Unfortunately, the previous attempt in #3317 did not fix this problem. --- CMakeLists.txt | 2 +- cmake/GetGitRevisionDescription.cmake | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f4e5e51bcb..70be0be6d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,8 +52,8 @@ set(NVIM_VERSION_MINOR 0) set(NVIM_VERSION_PATCH 0) set(NVIM_VERSION_PRERELEASE "-alpha") +file(TO_CMAKE_PATH ${CMAKE_CURRENT_LIST_DIR}/.git FORCED_GIT_DIR) include(GetGitRevisionDescription) -file(TO_NATIVE_PATH ${CMAKE_CURRENT_LIST_DIR}/.git GIT_DIR) if(NVIM_VERSION_PRERELEASE) get_git_head_revision(GIT_REFSPEC NVIM_VERSION_COMMIT) diff --git a/cmake/GetGitRevisionDescription.cmake b/cmake/GetGitRevisionDescription.cmake index 1e0968ec3b..5044c682e4 100644 --- a/cmake/GetGitRevisionDescription.cmake +++ b/cmake/GetGitRevisionDescription.cmake @@ -42,7 +42,13 @@ set(__get_git_revision_description YES) get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH) function(get_git_dir _gitdir) - # check GIT_DIR in environment first + # check FORCED_GIT_DIR first + if(FORCED_GIT_DIR) + set(${_gitdir} ${FORCED_GIT_DIR} PARENT_SCOPE) + return() + endif() + + # check GIT_DIR in environment set(GIT_DIR $ENV{GIT_DIR}) if(NOT GIT_DIR) set(GIT_PARENT_DIR ${CMAKE_CURRENT_SOURCE_DIR})