diff options
author | Daan De Meyer <daan.j.demeyer@gmail.com> | 2019-10-12 16:49:04 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2019-10-18 18:37:33 (GMT) |
commit | 8065a686ddcb54b10fd6758eca24280f3e68926b (patch) | |
tree | 26160936788f23291802c1639be94a9b4a9cd463 | |
parent | 493c4e781a35a92c5c912b22493b84de750b1fe1 (diff) | |
download | CMake-8065a686ddcb54b10fd6758eca24280f3e68926b.zip CMake-8065a686ddcb54b10fd6758eca24280f3e68926b.tar.gz CMake-8065a686ddcb54b10fd6758eca24280f3e68926b.tar.bz2 |
Initialize CMAKE_EXPORT_COMPILE_COMMANDS from envvar of the same name
Fixes: #18386
11 files changed, 58 insertions, 4 deletions
diff --git a/Help/envvar/CMAKE_EXPORT_COMPILE_COMMANDS.rst b/Help/envvar/CMAKE_EXPORT_COMPILE_COMMANDS.rst new file mode 100644 index 0000000..e9e0810 --- /dev/null +++ b/Help/envvar/CMAKE_EXPORT_COMPILE_COMMANDS.rst @@ -0,0 +1,9 @@ +CMAKE_EXPORT_COMPILE_COMMANDS +----------------------------- + +.. include:: ENV_VAR.txt + +The default value for :variable:`CMAKE_EXPORT_COMPILE_COMMANDS` when there +is no explicit configuration given on the first run while creating a new +build tree. On later runs in an existing build tree the value persists in +the cache as :variable:`CMAKE_EXPORT_COMPILE_COMMANDS`. diff --git a/Help/manual/cmake-env-variables.7.rst b/Help/manual/cmake-env-variables.7.rst index 96ceb94..c98f18f 100644 --- a/Help/manual/cmake-env-variables.7.rst +++ b/Help/manual/cmake-env-variables.7.rst @@ -23,6 +23,7 @@ Environment Variables that Control the Build /envvar/CMAKE_BUILD_PARALLEL_LEVEL /envvar/CMAKE_CONFIG_TYPE + /envvar/CMAKE_EXPORT_COMPILE_COMMANDS /envvar/CMAKE_GENERATOR /envvar/CMAKE_GENERATOR_INSTANCE /envvar/CMAKE_GENERATOR_PLATFORM diff --git a/Help/release/dev/export-compile-commands-environment-variable.rst b/Help/release/dev/export-compile-commands-environment-variable.rst new file mode 100644 index 0000000..da9d66b --- /dev/null +++ b/Help/release/dev/export-compile-commands-environment-variable.rst @@ -0,0 +1,6 @@ +export-compile-commands-environment-variable +-------------------------------------------- + +* The :variable:`CMAKE_EXPORT_COMPILE_COMMANDS` variable now takes its + initial value from the :envvar:`CMAKE_EXPORT_COMPILE_COMMANDS` environment + variable if no explicit configuration is given. diff --git a/Help/variable/CMAKE_EXPORT_COMPILE_COMMANDS.rst b/Help/variable/CMAKE_EXPORT_COMPILE_COMMANDS.rst index 4548abc..6d2450b 100644 --- a/Help/variable/CMAKE_EXPORT_COMPILE_COMMANDS.rst +++ b/Help/variable/CMAKE_EXPORT_COMPILE_COMMANDS.rst @@ -25,6 +25,9 @@ form. The format of the JSON file looks like: } ] +This is initialized by the :envvar:`CMAKE_EXPORT_COMPILE_COMMANDS` environment +variable. + .. note:: This option is implemented only by :ref:`Makefile Generators` and the :generator:`Ninja`. It is ignored on other generators. diff --git a/Modules/CMakeGenericSystem.cmake b/Modules/CMakeGenericSystem.cmake index 77d8cfd..7efe5c4 100644 --- a/Modules/CMakeGenericSystem.cmake +++ b/Modules/CMakeGenericSystem.cmake @@ -51,16 +51,16 @@ if(CMAKE_GENERATOR MATCHES "Make") set_property(GLOBAL PROPERTY TARGET_MESSAGES ${CMAKE_TARGET_MESSAGES}) endif() if(CMAKE_GENERATOR MATCHES "Unix Makefiles") - set(CMAKE_EXPORT_COMPILE_COMMANDS OFF CACHE BOOL - "Enable/Disable output of compile commands during generation." + 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") - set(CMAKE_EXPORT_COMPILE_COMMANDS OFF CACHE BOOL - "Enable/Disable output of compile commands during generation." + 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() diff --git a/Tests/EnforceConfig.cmake.in b/Tests/EnforceConfig.cmake.in index b7587aa..7781ded 100644 --- a/Tests/EnforceConfig.cmake.in +++ b/Tests/EnforceConfig.cmake.in @@ -33,5 +33,6 @@ unset(ENV{CMAKE_GENERATOR}) unset(ENV{CMAKE_GENERATOR_INSTANCE}) unset(ENV{CMAKE_GENERATOR_PLATFORM}) unset(ENV{CMAKE_GENERATOR_TOOLSET}) +unset(ENV{CMAKE_EXPORT_COMPILE_COMMANDS}) @TEST_HOME_ENV_CODE@ diff --git a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake index b608d33..f903c3d 100644 --- a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake +++ b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake @@ -254,6 +254,24 @@ function(run_EnvironmentGenerator) endfunction() run_EnvironmentGenerator() +function(run_EnvironmentExportCompileCommands) + set(RunCMake_TEST_SOURCE_DIR ${RunCMake_SOURCE_DIR}/env-export-compile-commands) + + run_cmake(env-export-compile-commands-unset) + + set(ENV{CMAKE_EXPORT_COMPILE_COMMANDS} ON) + run_cmake(env-export-compile-commands-set) + + set(RunCMake_TEST_OPTIONS -DCMAKE_EXPORT_COMPILE_COMMANDS=OFF) + run_cmake(env-export-compile-commands-override) + + unset(ENV{CMAKE_EXPORT_COMPILE_COMMANDS}) +endfunction(run_EnvironmentExportCompileCommands) + +if(RunCMake_GENERATOR MATCHES "Unix Makefiles" OR RunCMake_GENERATOR MATCHES "Ninja") + run_EnvironmentExportCompileCommands() +endif() + if(RunCMake_GENERATOR STREQUAL "Ninja") # Use a single build tree for a few tests without cleaning. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/Build-build) diff --git a/Tests/RunCMake/CommandLine/env-export-compile-commands-override-check.cmake b/Tests/RunCMake/CommandLine/env-export-compile-commands-override-check.cmake new file mode 100644 index 0000000..032a1ae --- /dev/null +++ b/Tests/RunCMake/CommandLine/env-export-compile-commands-override-check.cmake @@ -0,0 +1,3 @@ +if(EXISTS "${RunCMake_TEST_BINARY_DIR}/compile_commands.json") + set(RunCMake_TEST_FAILED "compile_commands.json generated with CMAKE_EXPORT_COMPILE_COMMANDS overridden") +endif() diff --git a/Tests/RunCMake/CommandLine/env-export-compile-commands-set-check.cmake b/Tests/RunCMake/CommandLine/env-export-compile-commands-set-check.cmake new file mode 100644 index 0000000..a749a55 --- /dev/null +++ b/Tests/RunCMake/CommandLine/env-export-compile-commands-set-check.cmake @@ -0,0 +1,3 @@ +if(NOT EXISTS "${RunCMake_TEST_BINARY_DIR}/compile_commands.json") + set(RunCMake_TEST_FAILED "compile_commands.json not generated with CMAKE_EXPORT_COMPILE_COMMANDS set") +endif() diff --git a/Tests/RunCMake/CommandLine/env-export-compile-commands-unset-check.cmake b/Tests/RunCMake/CommandLine/env-export-compile-commands-unset-check.cmake new file mode 100644 index 0000000..c5878f0 --- /dev/null +++ b/Tests/RunCMake/CommandLine/env-export-compile-commands-unset-check.cmake @@ -0,0 +1,3 @@ +if(EXISTS "${RunCMake_TEST_BINARY_DIR}/compile_commands.json") + set(RunCMake_TEST_FAILED "compile_commands.json generated with CMAKE_EXPORT_COMPILE_COMMANDS unset") +endif() diff --git a/Tests/RunCMake/CommandLine/env-export-compile-commands/CMakeLists.txt b/Tests/RunCMake/CommandLine/env-export-compile-commands/CMakeLists.txt new file mode 100644 index 0000000..aa6fbfd --- /dev/null +++ b/Tests/RunCMake/CommandLine/env-export-compile-commands/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.14) +project(env-export-compile-commands C) + +# Add target with a source file to make sure compile_commands.json gets +# generated. +file(TOUCH ${CMAKE_CURRENT_BINARY_DIR}/main.c) +add_executable(env-export-compile-commands ${CMAKE_CURRENT_BINARY_DIR}/main.c) |