From fe19df49d48680cbfd8e1023d4a93b39f12d4b64 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 12 May 2020 07:48:17 -0400 Subject: [PATCH] Initialize CMAKE_EXPORT_COMPILE_COMMANDS only if not set already Also de-duplicate its initialization code. Fixes: #16588 --- Modules/CMakeGenericSystem.cmake | 8 +------- Tests/RunCMake/CMakeLists.txt | 3 +++ .../ExportCompileCommands/BeforeProject-check.cmake | 4 ++++ Tests/RunCMake/ExportCompileCommands/BeforeProject.cmake | 3 +++ .../ExportCompileCommands/BeforeProjectBEFORE.cmake | 1 + Tests/RunCMake/ExportCompileCommands/CMakeLists.txt | 3 +++ Tests/RunCMake/ExportCompileCommands/RunCMakeTest.cmake | 3 +++ Tests/RunCMake/ExportCompileCommands/empty.c | 0 8 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 Tests/RunCMake/ExportCompileCommands/BeforeProject-check.cmake create mode 100644 Tests/RunCMake/ExportCompileCommands/BeforeProject.cmake create mode 100644 Tests/RunCMake/ExportCompileCommands/BeforeProjectBEFORE.cmake create mode 100644 Tests/RunCMake/ExportCompileCommands/CMakeLists.txt create mode 100644 Tests/RunCMake/ExportCompileCommands/RunCMakeTest.cmake create mode 100644 Tests/RunCMake/ExportCompileCommands/empty.c diff --git a/Modules/CMakeGenericSystem.cmake b/Modules/CMakeGenericSystem.cmake index f539b46581..000fba199b 100644 --- a/Modules/CMakeGenericSystem.cmake +++ b/Modules/CMakeGenericSystem.cmake @@ -50,15 +50,9 @@ if(CMAKE_GENERATOR MATCHES "Make") if(DEFINED CMAKE_TARGET_MESSAGES) set_property(GLOBAL PROPERTY TARGET_MESSAGES ${CMAKE_TARGET_MESSAGES}) endif() - if(CMAKE_GENERATOR MATCHES "Unix Makefiles") - set(CMAKE_EXPORT_COMPILE_COMMANDS "$ENV{CMAKE_EXPORT_COMPILE_COMMANDS}" - CACHE BOOL "Enable/Disable output of compile commands during generation." - ) - mark_as_advanced(CMAKE_EXPORT_COMPILE_COMMANDS) - endif() endif() -if(CMAKE_GENERATOR MATCHES "Ninja") +if(NOT DEFINED CMAKE_EXPORT_COMPILE_COMMANDS AND CMAKE_GENERATOR MATCHES "Ninja|Unix Makefiles") set(CMAKE_EXPORT_COMPILE_COMMANDS "$ENV{CMAKE_EXPORT_COMPILE_COMMANDS}" CACHE BOOL "Enable/Disable output of compile commands during generation." ) diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index cc888682f7..8aeb412280 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -197,6 +197,9 @@ endif() add_RunCMake_test(CompilerNotFound) add_RunCMake_test(Configure -DMSVC_IDE=${MSVC_IDE}) add_RunCMake_test(DisallowedCommands) +if("${CMAKE_GENERATOR}" MATCHES "Unix Makefiles|Ninja") + add_RunCMake_test(ExportCompileCommands) +endif() add_RunCMake_test(ExternalData) add_RunCMake_test(FeatureSummary) add_RunCMake_test(FPHSA) diff --git a/Tests/RunCMake/ExportCompileCommands/BeforeProject-check.cmake b/Tests/RunCMake/ExportCompileCommands/BeforeProject-check.cmake new file mode 100644 index 0000000000..87058e2fd1 --- /dev/null +++ b/Tests/RunCMake/ExportCompileCommands/BeforeProject-check.cmake @@ -0,0 +1,4 @@ +if(NOT EXISTS "${RunCMake_TEST_BINARY_DIR}/compile_commands.json") + set(RunCMake_TEST_FAILED "compile_commands.json not generated") + return() +endif() diff --git a/Tests/RunCMake/ExportCompileCommands/BeforeProject.cmake b/Tests/RunCMake/ExportCompileCommands/BeforeProject.cmake new file mode 100644 index 0000000000..b8cbdefca7 --- /dev/null +++ b/Tests/RunCMake/ExportCompileCommands/BeforeProject.cmake @@ -0,0 +1,3 @@ +enable_language(C) +add_library(empty STATIC empty.c) +message(STATUS "CMAKE_EXPORT_COMPILE_COMMANDS='${CMAKE_EXPORT_COMPILE_COMMANDS}'") diff --git a/Tests/RunCMake/ExportCompileCommands/BeforeProjectBEFORE.cmake b/Tests/RunCMake/ExportCompileCommands/BeforeProjectBEFORE.cmake new file mode 100644 index 0000000000..87f9c87930 --- /dev/null +++ b/Tests/RunCMake/ExportCompileCommands/BeforeProjectBEFORE.cmake @@ -0,0 +1 @@ +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) diff --git a/Tests/RunCMake/ExportCompileCommands/CMakeLists.txt b/Tests/RunCMake/ExportCompileCommands/CMakeLists.txt new file mode 100644 index 0000000000..b7117bd630 --- /dev/null +++ b/Tests/RunCMake/ExportCompileCommands/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.17) +project(${RunCMake_TEST} NONE) +include(${RunCMake_TEST}.cmake) diff --git a/Tests/RunCMake/ExportCompileCommands/RunCMakeTest.cmake b/Tests/RunCMake/ExportCompileCommands/RunCMakeTest.cmake new file mode 100644 index 0000000000..b540a04505 --- /dev/null +++ b/Tests/RunCMake/ExportCompileCommands/RunCMakeTest.cmake @@ -0,0 +1,3 @@ +include(RunCMake) + +run_cmake_with_options(BeforeProject -DCMAKE_PROJECT_INCLUDE_BEFORE=BeforeProjectBEFORE.cmake) diff --git a/Tests/RunCMake/ExportCompileCommands/empty.c b/Tests/RunCMake/ExportCompileCommands/empty.c new file mode 100644 index 0000000000..e69de29bb2