Commit 8065a686 authored by Daan De Meyer's avatar Daan De Meyer Committed by Brad King

Initialize CMAKE_EXPORT_COMPILE_COMMANDS from envvar of the same name

Fixes: #18386
parent 493c4e78
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`.
......@@ -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
......
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.
......@@ -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.
......
......@@ -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()
......
......@@ -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@
......@@ -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)
......
if(EXISTS "${RunCMake_TEST_BINARY_DIR}/compile_commands.json")
set(RunCMake_TEST_FAILED "compile_commands.json generated with CMAKE_EXPORT_COMPILE_COMMANDS overridden")
endif()
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()
if(EXISTS "${RunCMake_TEST_BINARY_DIR}/compile_commands.json")
set(RunCMake_TEST_FAILED "compile_commands.json generated with CMAKE_EXPORT_COMPILE_COMMANDS unset")
endif()
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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment