LANGUAGE property is not set for source files added with target_sources
CMake version: 3.22.3
When a new source file is added to a target using the target_sources
function, the LANGUAGE
property is not correctly set. For example:
project(test)
add_executable(test)
target_sources(test PRIVATE test.c)
# Uncomment either of the following line to make the test work:
#get_source_file_property(file_loc test.c LOCATION)
#set_source_files_properties(test.c PROPERTIES COMPILE_DEFINITIONS foo)
get_source_file_property(file_lang test.c LANGUAGE)
if("${file_lang}" STREQUAL "NOTFOUND")
message(FATAL_ERROR "LANGUAGE not set")
else()
message("LANGUAGE: ${file_lang}")
endif()
Expected result:
LANGUAGE: C
Actual Result:
CMake Error at CMakeLists.txt:10 (message):
LANGUAGE not set
There was a previous issue for this exact same problem here: #19504 (closed)
It is claimed to have been fixed in version 3.16 and there is a test demonstrating this: https://gitlab.kitware.com/cmake/cmake/-/blob/v3.26.2/Tests/SourceFileProperty/CMakeLists.txt
However, the problem still seems to remain and the test seems to only work by chance due to the presence of a previous call to set the COMPILE_DEFINITIONS
property on the source file.
Edited by Brad King