Genex: Enable COMPILE_LANGUAGE for INCLUDE_DIRECTORIES 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
`INCLUDE_DIRECTORIES` 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 include directories for C and C++ is frequently similar but may
be distinct from those for other languages like CUDA.

Fixes: #17435
This commit is contained in:
Brad King
2018-01-11 14:32:13 -05:00
parent c2f79c9867
commit 506fda1cf0
21 changed files with 112 additions and 144 deletions

View File

@@ -97,27 +97,32 @@ 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 may be used to specify compile options
and compile definitions for source files of a
otherwise ``0``. This expression may be used to specify compile options,
compile definitions, and include directories for source files of a
particular language in a target. For example:
.. code-block:: cmake
add_executable(myapp main.cpp foo.c bar.cpp)
add_executable(myapp main.cpp foo.c bar.cpp zot.cu)
target_compile_options(myapp
PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
)
target_compile_definitions(myapp
PRIVATE $<$<COMPILE_LANGUAGE:CXX>:COMPILING_CXX>
$<$<COMPILE_LANGUAGE:CUDA>:COMPILING_CUDA>
)
target_include_directories(myapp
PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/opt/foo/cxx_headers>
)
This specifies the use of the ``-fno-exceptions`` compile option
and ``COMPILING_CXX`` compile definition for C++ only
(compiler id checks elided).
This specifies the use of the ``-fno-exceptions`` compile option,
``COMPILING_CXX`` compile definition, and ``cxx_headers`` include
directory for C++ only (compiler id checks elided). It also specifies
a ``COMPILING_CUDA`` compile definition for CUDA.
Note that with :ref:`Visual Studio Generators` and :generator:`Xcode` there
is no way to represent target-wide compile definitions separately for
``C`` and ``CXX`` languages.
is no way to represent target-wide compile definitions or include directories
separately for ``C`` and ``CXX`` languages.
Also, with :ref:`Visual Studio Generators` there is no way to represent
target-wide flags separately for ``C`` and ``CXX`` languages. Under these
generators, expressions for both C and C++ sources will be evaluated
@@ -133,16 +138,6 @@ Available logical expressions are:
add_executable(myapp main.cpp)
target_link_libraries(myapp myapp_c myapp_cxx)
The ``Makefile`` and ``Ninja`` based generators can also use this
expression to specify compile-language specific include directories:
.. code-block:: cmake
add_executable(myapp main.cpp foo.c bar.cpp)
target_include_directories(myapp
PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/opt/foo/cxx_headers>
)
Informational Expressions
=========================

View File

@@ -4,10 +4,11 @@ extend-compile-language-genex
* :ref:`Visual Studio Generators` learned to support the ``COMPILE_LANGUAGE``
:manual:`generator expression <cmake-generator-expressions(7)>` in
target-wide :prop_tgt:`COMPILE_DEFINITIONS`,
:prop_tgt:`COMPILE_OPTIONS`, and :command:`file(GENERATE)`.
:prop_tgt:`INCLUDE_DIRECTORIES`, :prop_tgt:`COMPILE_OPTIONS`, and
:command:`file(GENERATE)`.
* The :generator:`Xcode` generator learned to support the ``COMPILE_LANGUAGE``
:manual:`generator expression <cmake-generator-expressions(7)>` in
target-wide :prop_tgt:`COMPILE_DEFINITIONS`.
It previously supported only :prop_tgt:`COMPILE_OPTIONS` and
:command:`file(GENERATE)`.
target-wide :prop_tgt:`COMPILE_DEFINITIONS` and
:prop_tgt:`INCLUDE_DIRECTORIES`. It previously supported only
:prop_tgt:`COMPILE_OPTIONS` and :command:`file(GENERATE)`.

View File

@@ -805,7 +805,7 @@ static const struct CompileLanguageNode : public cmGeneratorExpressionNode
const std::vector<std::string>& parameters,
cmGeneratorExpressionContext* context,
const GeneratorExpressionContent* content,
cmGeneratorExpressionDAGChecker* dagChecker) const override
cmGeneratorExpressionDAGChecker* /*dagChecker*/) const override
{
if (context->Language.empty()) {
reportError(
@@ -827,33 +827,14 @@ static const struct CompileLanguageNode : public cmGeneratorExpressionNode
return std::string();
}
std::string genName = gg->GetName();
if (genName.find("Visual Studio") != std::string::npos) {
if (dagChecker && dagChecker->EvaluatingIncludeDirectories()) {
reportError(
context, content->GetOriginalExpression(),
"$<COMPILE_LANGUAGE:...> may only be used for COMPILE_OPTIONS, "
"COMPILE_DEFINITIONS, "
"and file(GENERATE) with the Visual Studio generator.");
return std::string();
}
} else if (genName.find("Xcode") != std::string::npos) {
if (dagChecker && dagChecker->EvaluatingIncludeDirectories()) {
reportError(
context, content->GetOriginalExpression(),
"$<COMPILE_LANGUAGE:...> may only be used for COMPILE_OPTIONS, "
"COMPILE_DEFINITIONS, "
"and file(GENERATE) with the Xcode generator.");
return std::string();
}
} else {
if (genName.find("Makefiles") == std::string::npos &&
genName.find("Ninja") == std::string::npos &&
genName.find("Watcom WMake") == std::string::npos) {
reportError(
context, content->GetOriginalExpression(),
"$<COMPILE_LANGUAGE:...> not supported for this generator.");
return std::string();
}
if (genName.find("Makefiles") == std::string::npos &&
genName.find("Ninja") == std::string::npos &&
genName.find("Visual Studio") == std::string::npos &&
genName.find("Xcode") == std::string::npos &&
genName.find("Watcom WMake") == std::string::npos) {
reportError(context, content->GetOriginalExpression(),
"$<COMPILE_LANGUAGE:...> not supported for this generator.");
return std::string();
}
if (parameters.empty()) {
return context->Language;

View File

@@ -2000,8 +2000,10 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmGeneratorTarget* gtgt,
const bool emitSystemIncludes = this->XcodeVersion >= 83;
std::vector<std::string> includes;
this->CurrentLocalGenerator->GetIncludeDirectories(includes, gtgt, "C",
configName);
if (!langForPreprocessor.empty()) {
this->CurrentLocalGenerator->GetIncludeDirectories(
includes, gtgt, langForPreprocessor, configName);
}
std::set<std::string> emitted;
emitted.insert("/System/Library/Frameworks");

View File

@@ -796,10 +796,13 @@ void cmLocalVisualStudio7Generator::WriteConfiguration(
<< "\\$(ConfigurationName)\"\n";
}
fout << "\t\t\t\tAdditionalIncludeDirectories=\"";
std::vector<std::string> includes;
this->GetIncludeDirectories(includes, target, "C", configName);
std::vector<std::string>::iterator i = includes.begin();
for (; i != includes.end(); ++i) {
std::vector<std::string> includes_cl;
if (!langForClCompile.empty()) {
this->GetIncludeDirectories(includes_cl, target, langForClCompile,
configName);
}
std::vector<std::string>::iterator i = includes_cl.begin();
for (; i != includes_cl.end(); ++i) {
// output the include path
std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
fout << ipath << ";";
@@ -834,9 +837,12 @@ void cmLocalVisualStudio7Generator::WriteConfiguration(
"\t\t\t\tName=\"MASM\"\n"
"\t\t\t\tIncludePaths=\""
;
std::vector<std::string> includes_masm;
this->GetIncludeDirectories(includes_masm, target, "ASM_MASM",
configName);
/* clang-format on */
const char* sep = "";
for (i = includes.begin(); i != includes.end(); ++i) {
for (i = includes_masm.begin(); i != includes_masm.end(); ++i) {
std::string inc = *i;
cmConvertToWindowsSlash(inc);
fout << sep << this->EscapeForXML(inc);
@@ -864,7 +870,9 @@ void cmLocalVisualStudio7Generator::WriteConfiguration(
}
fout << "\t\t\t<Tool\n\t\t\t\tName=\"" << tool << "\"\n"
<< "\t\t\t\tAdditionalIncludeDirectories=\"";
for (i = includes.begin(); i != includes.end(); ++i) {
std::vector<std::string> includes_rc;
this->GetIncludeDirectories(includes_rc, target, "RC", configName);
for (i = includes_rc.begin(); i != includes_rc.end(); ++i) {
std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
fout << ipath << ";";
}
@@ -878,7 +886,9 @@ void cmLocalVisualStudio7Generator::WriteConfiguration(
}
fout << "\t\t\t<Tool\n\t\t\t\tName=\"" << tool << "\"\n";
fout << "\t\t\t\tAdditionalIncludeDirectories=\"";
for (i = includes.begin(); i != includes.end(); ++i) {
std::vector<std::string> includes_midl;
this->GetIncludeDirectories(includes_midl, target, "MIDL", configName);
for (i = includes_midl.begin(); i != includes_midl.end(); ++i) {
std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
fout << ipath << ";";
}

View File

@@ -2502,7 +2502,7 @@ bool cmVisualStudio10TargetGenerator::ComputeClOptions(
}
void cmVisualStudio10TargetGenerator::WriteClOptions(
std::string const& configName, std::vector<std::string> const& includes)
std::string const& configName)
{
Options& clOptions = *(this->ClOptions[configName]);
if (this->ProjectType == csproj) {
@@ -2510,7 +2510,11 @@ void cmVisualStudio10TargetGenerator::WriteClOptions(
}
this->WriteString("<ClCompile>\n", 2);
clOptions.PrependInheritedString("AdditionalOptions");
clOptions.AppendFlag("AdditionalIncludeDirectories", includes);
if (!this->LangForClCompile.empty()) {
std::vector<std::string> const includes =
this->GetIncludes(configName, this->LangForClCompile);
clOptions.AppendFlag("AdditionalIncludeDirectories", includes);
}
clOptions.AppendFlag("AdditionalIncludeDirectories",
"%(AdditionalIncludeDirectories)");
clOptions.OutputFlagMap(*this->BuildFileStream, " ");
@@ -2596,7 +2600,7 @@ bool cmVisualStudio10TargetGenerator::ComputeRcOptions(
}
void cmVisualStudio10TargetGenerator::WriteRCOptions(
std::string const& configName, std::vector<std::string> const& includes)
std::string const& configName)
{
if (!this->MSTools) {
return;
@@ -2606,6 +2610,8 @@ void cmVisualStudio10TargetGenerator::WriteRCOptions(
Options& rcOptions = *(this->RcOptions[configName]);
rcOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
"\n", "RC");
std::vector<std::string> const includes =
this->GetIncludes(configName, "RC");
rcOptions.AppendFlag("AdditionalIncludeDirectories", includes);
rcOptions.AppendFlag("AdditionalIncludeDirectories",
"%(AdditionalIncludeDirectories)");
@@ -2710,7 +2716,7 @@ bool cmVisualStudio10TargetGenerator::ComputeCudaOptions(
}
void cmVisualStudio10TargetGenerator::WriteCudaOptions(
std::string const& configName, std::vector<std::string> const& includes)
std::string const& configName)
{
if (!this->MSTools || !this->GlobalGenerator->IsCudaEnabled()) {
return;
@@ -2718,6 +2724,8 @@ void cmVisualStudio10TargetGenerator::WriteCudaOptions(
this->WriteString("<CudaCompile>\n", 2);
Options& cudaOptions = *(this->CudaOptions[configName]);
std::vector<std::string> const includes =
this->GetIncludes(configName, "CUDA");
cudaOptions.AppendFlag("Include", includes);
cudaOptions.AppendFlag("Include", "%(Include)");
cudaOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
@@ -2832,7 +2840,7 @@ bool cmVisualStudio10TargetGenerator::ComputeMasmOptions(
}
void cmVisualStudio10TargetGenerator::WriteMasmOptions(
std::string const& configName, std::vector<std::string> const& includes)
std::string const& configName)
{
if (!this->MSTools || !this->GlobalGenerator->IsMasmEnabled()) {
return;
@@ -2845,6 +2853,8 @@ void cmVisualStudio10TargetGenerator::WriteMasmOptions(
"\n", "ASM_MASM");
Options& masmOptions = *(this->MasmOptions[configName]);
std::vector<std::string> const includes =
this->GetIncludes(configName, "ASM_MASM");
masmOptions.AppendFlag("IncludePaths", includes);
masmOptions.AppendFlag("IncludePaths", "%(IncludePaths)");
masmOptions.PrependInheritedString("AdditionalOptions");
@@ -2889,13 +2899,15 @@ bool cmVisualStudio10TargetGenerator::ComputeNasmOptions(
}
void cmVisualStudio10TargetGenerator::WriteNasmOptions(
std::string const& configName, std::vector<std::string> includes)
std::string const& configName)
{
if (!this->GlobalGenerator->IsNasmEnabled()) {
return;
}
this->WriteString("<NASM>\n", 2);
std::vector<std::string> includes =
this->GetIncludes(configName, "ASM_NASM");
Options& nasmOptions = *(this->NasmOptions[configName]);
for (size_t i = 0; i < includes.size(); i++) {
includes[i] += "\\";
@@ -3443,7 +3455,7 @@ void cmVisualStudio10TargetGenerator::AddTargetsFileAndConfigPair(
}
void cmVisualStudio10TargetGenerator::WriteMidlOptions(
std::string const& /*config*/, std::vector<std::string> const& includes)
std::string const& configName)
{
if (!this->MSTools) {
return;
@@ -3469,6 +3481,8 @@ void cmVisualStudio10TargetGenerator::WriteMidlOptions(
// on the CMake side?
this->WriteString("<Midl>\n", 2);
this->WriteString("<AdditionalIncludeDirectories>", 3);
std::vector<std::string> const includes =
this->GetIncludes(configName, "MIDL");
for (std::string const& i : includes) {
*this->BuildFileStream << cmVS10EscapeXML(i) << ";";
}
@@ -3493,20 +3507,19 @@ void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups()
return;
}
for (std::string const& i : this->Configurations) {
std::vector<std::string> const includes = this->GetIncludes(i, "C");
this->WritePlatformConfigTag("ItemDefinitionGroup", i, 1);
*this->BuildFileStream << "\n";
// output cl compile flags <ClCompile></ClCompile>
if (this->GeneratorTarget->GetType() <= cmStateEnums::OBJECT_LIBRARY) {
this->WriteClOptions(i, includes);
this->WriteClOptions(i);
// output rc compile flags <ResourceCompile></ResourceCompile>
this->WriteRCOptions(i, includes);
this->WriteCudaOptions(i, includes);
this->WriteMasmOptions(i, includes);
this->WriteNasmOptions(i, includes);
this->WriteRCOptions(i);
this->WriteCudaOptions(i);
this->WriteMasmOptions(i);
this->WriteNasmOptions(i);
}
// output midl flags <Midl></Midl>
this->WriteMidlOptions(i, includes);
this->WriteMidlOptions(i);
// write events
if (this->ProjectType != csproj) {
this->WriteEvents(i);

View File

@@ -95,16 +95,13 @@ private:
bool ComputeClOptions();
bool ComputeClOptions(std::string const& configName);
void WriteClOptions(std::string const& config,
std::vector<std::string> const& includes);
void WriteClOptions(std::string const& config);
bool ComputeRcOptions();
bool ComputeRcOptions(std::string const& config);
void WriteRCOptions(std::string const& config,
std::vector<std::string> const& includes);
void WriteRCOptions(std::string const& config);
bool ComputeCudaOptions();
bool ComputeCudaOptions(std::string const& config);
void WriteCudaOptions(std::string const& config,
std::vector<std::string> const& includes);
void WriteCudaOptions(std::string const& config);
bool ComputeCudaLinkOptions();
bool ComputeCudaLinkOptions(std::string const& config);
@@ -112,20 +109,17 @@ private:
bool ComputeMasmOptions();
bool ComputeMasmOptions(std::string const& config);
void WriteMasmOptions(std::string const& config,
std::vector<std::string> const& includes);
void WriteMasmOptions(std::string const& config);
bool ComputeNasmOptions();
bool ComputeNasmOptions(std::string const& config);
void WriteNasmOptions(std::string const& config,
std::vector<std::string> includes);
void WriteNasmOptions(std::string const& config);
bool ComputeLinkOptions();
bool ComputeLinkOptions(std::string const& config);
bool ComputeLibOptions();
bool ComputeLibOptions(std::string const& config);
void WriteLinkOptions(std::string const& config);
void WriteMidlOptions(std::string const& config,
std::vector<std::string> const& includes);
void WriteMidlOptions(std::string const& config);
void WriteAntBuildOptions(std::string const& config);
void OutputLinkIncremental(std::string const& configName);
void WriteCustomRule(cmSourceFile const* source,

View File

@@ -42,17 +42,17 @@ add_executable(consumer
"${CMAKE_CURRENT_SOURCE_DIR}/consumer.cpp"
)
if (CMAKE_GENERATOR MATCHES "Makefiles" OR CMAKE_GENERATOR MATCHES "Ninja")
target_sources(consumer PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/consumer.c"
)
target_include_directories(consumer
PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/cxx_only>
$<$<COMPILE_LANGUAGE:C>:${CMAKE_CURRENT_SOURCE_DIR}/c_only>
)
target_sources(consumer PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/consumer.c"
)
target_include_directories(consumer
PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/cxx_only>
$<$<COMPILE_LANGUAGE:C>:${CMAKE_CURRENT_SOURCE_DIR}/c_only>
)
if(CMAKE_GENERATOR MATCHES "Visual Studio|Xcode")
target_compile_definitions(consumer
PRIVATE -DTEST_LANG_DEFINES
PRIVATE TEST_LANG_DEFINES_FOR_VISUAL_STUDIO_OR_XCODE
)
endif()

View File

@@ -1,5 +1,13 @@
#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
#include "cxx_only.h"
#ifndef CXX_ONLY_DEFINE
#error Expected CXX_ONLY_DEFINE
#endif
#else
#include "c_only.h"
#ifndef C_ONLY_DEFINE

View File

@@ -1,12 +1,10 @@
#include "consumer.h"
#include "common.h"
#include "cxx_only.h"
#include "interfaceinclude.h"
#include "publicinclude.h"
#include "relative_dir.h"
#ifdef TEST_LANG_DEFINES
#include "cxx_only.h"
#endif
#ifdef PRIVATEINCLUDE_DEFINE
#error Unexpected PRIVATEINCLUDE_DEFINE
@@ -32,11 +30,9 @@
#error Expected CONSUMER_DEFINE
#endif
#ifdef TEST_LANG_DEFINES
#ifndef CXX_ONLY_DEFINE
#error Expected CXX_ONLY_DEFINE
#endif
#endif
int main()
{

View File

@@ -45,6 +45,11 @@ target_compile_definitions(CudaOnlyWithDefs
-DDEF_LANG_IS_CUDA=$<COMPILE_LANGUAGE:CUDA>
)
target_include_directories(CudaOnlyWithDefs
PRIVATE
$<$<COMPILE_LANGUAGE:CUDA>:${CMAKE_CURRENT_SOURCE_DIR}/inc_cuda>
)
if(APPLE)
# Help the static cuda runtime find the driver (libcuda.dyllib) at runtime.
set_property(TARGET CudaOnlyWithDefs PROPERTY BUILD_RPATH ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES})

View File

@@ -0,0 +1 @@
#define INC_CUDA

View File

@@ -2,6 +2,11 @@
#include <cuda_runtime.h>
#include <iostream>
#include <inc_cuda.h>
#ifndef INC_CUDA
#error "INC_CUDA not defined!"
#endif
#ifndef HOST_DEFINE
#error "HOST_DEFINE not defined!"
#endif

View File

@@ -351,8 +351,6 @@ add_RunCMake_test(IfacePaths_INCLUDE_DIRECTORIES TEST_DIR IfacePaths)
set(IfacePaths_SOURCES_ARGS -DTEST_PROP=SOURCES)
add_RunCMake_test(IfacePaths_SOURCES TEST_DIR IfacePaths)
add_RunCMake_test(COMPILE_LANGUAGE-genex)
# Matlab module related tests
if(CMake_TEST_FindMatlab)
add_RunCMake_test(FindMatlab)

View File

@@ -1,3 +0,0 @@
cmake_minimum_required(VERSION 3.1)
project(${RunCMake_TEST} NONE)
include(${RunCMake_TEST}.cmake)

View File

@@ -1,9 +0,0 @@
CMake Error at IncludeDirectories.cmake:5 \(target_include_directories\):
Error evaluating generator expression:
\$<COMPILE_LANGUAGE:CXX>
\$<COMPILE_LANGUAGE:...> may only be used for COMPILE_OPTIONS,
COMPILE_DEFINITIONS, and file\(GENERATE\) with the Visual Studio generator.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@@ -1,9 +0,0 @@
CMake Error at IncludeDirectories.cmake:5 \(target_include_directories\):
Error evaluating generator expression:
\$<COMPILE_LANGUAGE:CXX>
\$<COMPILE_LANGUAGE:...> may only be used for COMPILE_OPTIONS,
COMPILE_DEFINITIONS, and file\(GENERATE\) with the Xcode generator.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@@ -1,5 +0,0 @@
enable_language(CXX)
add_executable(main main.cpp)
target_include_directories(main PRIVATE $<$<COMPILE_LANGUAGE:CXX>:anydir>)

View File

@@ -1,9 +0,0 @@
include(RunCMake)
if (RunCMake_GENERATOR STREQUAL "Xcode")
set(RunCMake-stderr-file IncludeDirectories-stderr-Xcode.txt)
run_cmake(IncludeDirectories)
elseif (RunCMake_GENERATOR MATCHES "Visual Studio")
set(RunCMake-stderr-file IncludeDirectories-stderr-VS.txt)
run_cmake(IncludeDirectories)
endif()

View File

@@ -1,5 +0,0 @@
int main()
{
return 0;
}