From b53766b2058160853a29c2bbf8dbfef7345552a4 Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Mon, 11 Mar 2019 12:33:43 -0400 Subject: CUDA: Support compiler id and version generator expressions Introduce the CUDA_COMPILER_ID and CUDA_COMPILER_VERSION generator expressions. --- Help/manual/cmake-generator-expressions.7.rst | 12 +++++++ Source/cmGeneratorExpressionNode.cxx | 46 +++++++++++++++++++++++++++ Tests/CudaOnly/WithDefs/CMakeLists.txt | 2 ++ Tests/CudaOnly/WithDefs/main.notcu | 8 +++++ 4 files changed, 68 insertions(+) diff --git a/Help/manual/cmake-generator-expressions.7.rst b/Help/manual/cmake-generator-expressions.7.rst index 7f484a4..47409ab 100644 --- a/Help/manual/cmake-generator-expressions.7.rst +++ b/Help/manual/cmake-generator-expressions.7.rst @@ -121,6 +121,9 @@ Variable Queries ``$`` ``1`` if the CMake-id of the CXX compiler matches ``compiler_id``, otherwise ``0``. +``$`` + ``1`` if the CMake-id of the CUDA compiler matches ``compiler_id``, + otherwise ``0``. See also the :variable:`CMAKE__COMPILER_ID` variable. ``$`` ``1`` if the CMake-id of the Fortran compiler matches ``compiler_id``, @@ -132,6 +135,9 @@ Variable Queries ``$`` ``1`` if the version of the CXX compiler matches ``version``, otherwise ``0``. See also the :variable:`CMAKE__COMPILER_VERSION` variable. +``$`` + ``1`` if the version of the CXX compiler matches ``version``, otherwise ``0``. + See also the :variable:`CMAKE__COMPILER_VERSION` variable. ``$`` ``1`` if the version of the Fortran compiler matches ``version``, otherwise ``0``. See also the :variable:`CMAKE__COMPILER_VERSION` variable. @@ -346,6 +352,9 @@ Variable Queries ``$`` The CMake-id of the CXX compiler used. See also the :variable:`CMAKE__COMPILER_ID` variable. +``$`` + The CMake-id of the CUDA compiler used. + See also the :variable:`CMAKE__COMPILER_ID` variable. ``$`` The CMake-id of the Fortran compiler used. See also the :variable:`CMAKE__COMPILER_ID` variable. @@ -355,6 +364,9 @@ Variable Queries ``$`` The version of the CXX compiler used. See also the :variable:`CMAKE__COMPILER_VERSION` variable. +``$`` + The version of the CUDA compiler used. + See also the :variable:`CMAKE__COMPILER_VERSION` variable. ``$`` The version of the Fortran compiler used. See also the :variable:`CMAKE__COMPILER_VERSION` variable. diff --git a/Source/cmGeneratorExpressionNode.cxx b/Source/cmGeneratorExpressionNode.cxx index 6ee7fa5..0ba662d 100644 --- a/Source/cmGeneratorExpressionNode.cxx +++ b/Source/cmGeneratorExpressionNode.cxx @@ -690,6 +690,28 @@ static const struct CXXCompilerIdNode : public CompilerIdNode } } cxxCompilerIdNode; +static const struct CUDACompilerIdNode : public CompilerIdNode +{ + CUDACompilerIdNode() {} // NOLINT(modernize-use-equals-default) + + std::string Evaluate( + const std::vector& parameters, + cmGeneratorExpressionContext* context, + const GeneratorExpressionContent* content, + cmGeneratorExpressionDAGChecker* dagChecker) const override + { + if (!context->HeadTarget) { + reportError( + context, content->GetOriginalExpression(), + "$ may only be used with binary targets. It may " + "not be used with add_custom_command or add_custom_target."); + return std::string(); + } + return this->EvaluateWithLanguage(parameters, context, content, dagChecker, + "CUDA"); + } +} cudaCompilerIdNode; + static const struct FortranCompilerIdNode : public CompilerIdNode { FortranCompilerIdNode() {} // NOLINT(modernize-use-equals-default) @@ -793,6 +815,28 @@ static const struct CXXCompilerVersionNode : public CompilerVersionNode } } cxxCompilerVersionNode; +static const struct CUDACompilerVersionNode : public CompilerVersionNode +{ + CUDACompilerVersionNode() {} // NOLINT(modernize-use-equals-default) + + std::string Evaluate( + const std::vector& parameters, + cmGeneratorExpressionContext* context, + const GeneratorExpressionContent* content, + cmGeneratorExpressionDAGChecker* dagChecker) const override + { + if (!context->HeadTarget) { + reportError( + context, content->GetOriginalExpression(), + "$ may only be used with binary targets. It " + "may not be used with add_custom_command or add_custom_target."); + return std::string(); + } + return this->EvaluateWithLanguage(parameters, context, content, dagChecker, + "CUDA"); + } +} cudaCompilerVersionNode; + static const struct FortranCompilerVersionNode : public CompilerVersionNode { FortranCompilerVersionNode() {} // NOLINT(modernize-use-equals-default) @@ -2066,6 +2110,7 @@ const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode( { "NOT", ¬Node }, { "C_COMPILER_ID", &cCompilerIdNode }, { "CXX_COMPILER_ID", &cxxCompilerIdNode }, + { "CUDA_COMPILER_ID", &cudaCompilerIdNode }, { "Fortran_COMPILER_ID", &fortranCompilerIdNode }, { "VERSION_GREATER", &versionGreaterNode }, { "VERSION_GREATER_EQUAL", &versionGreaterEqNode }, @@ -2074,6 +2119,7 @@ const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode( { "VERSION_EQUAL", &versionEqualNode }, { "C_COMPILER_VERSION", &cCompilerVersionNode }, { "CXX_COMPILER_VERSION", &cxxCompilerVersionNode }, + { "CUDA_COMPILER_VERSION", &cudaCompilerVersionNode }, { "Fortran_COMPILER_VERSION", &fortranCompilerVersionNode }, { "PLATFORM_ID", &platformIdNode }, { "COMPILE_FEATURES", &compileFeaturesNode }, diff --git a/Tests/CudaOnly/WithDefs/CMakeLists.txt b/Tests/CudaOnly/WithDefs/CMakeLists.txt index e58204d..00fd7d2 100644 --- a/Tests/CudaOnly/WithDefs/CMakeLists.txt +++ b/Tests/CudaOnly/WithDefs/CMakeLists.txt @@ -37,6 +37,8 @@ target_compile_definitions(CudaOnlyWithDefs $<$:$> -DDEF_COMPILE_LANG_$ -DDEF_LANG_IS_CUDA=$ + -DDEF_CUDA_COMPILER=$ + -DDEF_CUDA_COMPILER_VERSION=$ ) target_include_directories(CudaOnlyWithDefs diff --git a/Tests/CudaOnly/WithDefs/main.notcu b/Tests/CudaOnly/WithDefs/main.notcu index 98f73ce..68a296b 100644 --- a/Tests/CudaOnly/WithDefs/main.notcu +++ b/Tests/CudaOnly/WithDefs/main.notcu @@ -39,6 +39,14 @@ # error "Expected DEF_LANG_IS_CUDA" #endif +#ifndef DEF_CUDA_COMPILER +# error "DEF_CUDA_COMPILER not defined!" +#endif + +#ifndef DEF_CUDA_COMPILER_VERSION +# error "DEF_CUDA_COMPILER_VERSION not defined!" +#endif + static __global__ void DetermineIfValidCudaDevice() { } -- cgit v0.12