Genex: Enable COMPILE_LANGUAGE for COMPILE_OPTIONS with Visual Studio

Since commit v3.9.0-rc4~3^2~1 (VS: Fix target_compile_options for CUDA,
2017-06-21), the evaluation of `COMPILE_LANGUAGE` receives the proper
language.  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_OPTIONS` to match the selected language.

This is not exactly the same as for other generators, but is the best VS
can do.  It is also sufficient for many use cases since the set of
allowed flags for C and C++ is almost the same in Visual Studio.
Furthermore, since the VS generator moves many of the flags to
declarative `.vcxproj` elements, it will automatically avoid passing
C++ flags for C sources.

Issue: #17435
This commit is contained in:
Brad King
2017-11-17 14:46:53 -05:00
parent 2b7d59f310
commit 2ae880fa8f
14 changed files with 65 additions and 45 deletions
@@ -97,7 +97,7 @@ Available logical expressions are:
compile features and a list of supported compilers.
``$<COMPILE_LANGUAGE:lang>``
``1`` when the language used for compilation unit matches ``lang``,
otherwise ``0``. This expression used to specify compile options for
otherwise ``0``. This expression may be used to specify compile options for
source files of a particular language in a target. For example, to specify
the use of the ``-fno-exceptions`` compile option (compiler id checks
elided):
@@ -109,10 +109,12 @@ Available logical expressions are:
PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
)
This generator expression has limited use because it is not possible to
use it with the Visual Studio generators. Portable buildsystems would
not use this expression, and would create separate libraries for each
source file language instead:
Note that with :ref:`Visual Studio Generators` there is no way to represent
target-wide flags separately for ``C`` and ``CXX`` languages. Under these
generators, target-wide flags for both C and C++ sources will be evaluated
using ``CXX`` if there are any C++ sources and otherwise using ``C``.
A workaround is to create separate libraries for each source file language
instead:
.. code-block:: cmake
@@ -0,0 +1,7 @@
extend-compile-language-genex
-----------------------------
* The ``COMPILE_LANGUAGE`` :manual:`generator expression
<cmake-generator-expressions(7)>` may now be used with
:ref:`Visual Studio Generators` in :prop_tgt:`COMPILE_OPTIONS`
and :command:`file(GENERATE)`.