Genex: Enable COMPILE_LANGUAGE for COMPILE_DEFINITIONS with VS and Xcode

The set of compile flags used for a target's C and C++ sources is based
on the linker language.  By default this is always the C++ flags if any
C++ sources appear in the target, and otherwise the C flags.  Therefore
we can define the `COMPILE_LANGUAGE` generator expression in
`COMPILE_DEFINITIONS` to match the selected language.

This is not exactly the same as for other generators, but is the best VS
and Xcode can do.  It is also sufficient for many use cases since the
set of definitions for C and C++ is frequently similar but may be
distinct from those for other languages like CUDA.

Issue: #17435
This commit is contained in:
Brad King
2018-01-11 11:23:06 -05:00
parent 0795d25b78
commit c2f79c9867
24 changed files with 107 additions and 115 deletions

View File

@@ -26,18 +26,18 @@ target_compile_definitions(consumer
PRIVATE
)
if (CMAKE_GENERATOR MATCHES "Makefiles" OR CMAKE_GENERATOR MATCHES "Ninja")
target_sources(consumer PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/consumer.c"
)
target_sources(consumer PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/consumer.c"
)
target_compile_definitions(consumer
PRIVATE
CONSUMER_LANG_$<COMPILE_LANGUAGE>
LANG_IS_CXX=$<COMPILE_LANGUAGE:CXX>
LANG_IS_C=$<COMPILE_LANGUAGE:C>
)
if(CMAKE_GENERATOR MATCHES "Visual Studio|Xcode")
target_compile_definitions(consumer
PRIVATE
CONSUMER_LANG_$<COMPILE_LANGUAGE>
LANG_IS_CXX=$<COMPILE_LANGUAGE:CXX>
LANG_IS_C=$<COMPILE_LANGUAGE:C>
)
target_compile_definitions(consumer
PRIVATE -DTEST_LANG_DEFINES
PRIVATE TEST_LANG_DEFINES_FOR_VISUAL_STUDIO_OR_XCODE
)
endif()

View File

@@ -1,5 +1,23 @@
#ifdef TEST_LANG_DEFINES
// Visual Studio allows only one set of flags for C and C++.
// In a target using C++ we pick the C++ flags even for C sources.
#ifdef TEST_LANG_DEFINES_FOR_VISUAL_STUDIO_OR_XCODE
#ifndef CONSUMER_LANG_CXX
#error Expected CONSUMER_LANG_CXX
#endif
#ifdef CONSUMER_LANG_C
#error Unexpected CONSUMER_LANG_C
#endif
#if !LANG_IS_CXX
#error Expected LANG_IS_CXX
#endif
#if LANG_IS_C
#error Unexpected LANG_IS_C
#endif
#else
#ifdef CONSUMER_LANG_CXX
#error Unexpected CONSUMER_LANG_CXX
#endif

View File

@@ -15,7 +15,6 @@
#error Expected DASH_D_DEFINE
#endif
#ifdef TEST_LANG_DEFINES
#ifndef CONSUMER_LANG_CXX
#error Expected CONSUMER_LANG_CXX
#endif
@@ -31,7 +30,6 @@
#if LANG_IS_C
#error Unexpected LANG_IS_C
#endif
#endif
int main()
{