From fff812db95cea0844833e1cde2942ee52bffe911 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 20 Aug 2008 11:45:16 -0400 Subject: [PATCH] ENH: Add if(TARGET) command It is useful to be able to test if a target has been created. Often targets are created only inside conditions. Rather than storing the result of the condition manually for testing by other parts of the project, it is much easier for the other parts to just test for the target's existence. This will also be useful when find-modules start reporting results with IMPORTED targets and projects want to test if a certain target is available. --- Source/cmIfCommand.cxx | 7 +++++++ Source/cmIfCommand.h | 2 ++ Tests/Complex/Executable/CMakeLists.txt | 17 +++++++++++++++++ Tests/Complex/Library/CMakeLists.txt | 4 ++++ .../ComplexOneConfig/Executable/CMakeLists.txt | 17 +++++++++++++++++ Tests/ComplexOneConfig/Library/CMakeLists.txt | 4 ++++ .../Executable/CMakeLists.txt | 17 +++++++++++++++++ .../ComplexRelativePaths/Library/CMakeLists.txt | 4 ++++ 8 files changed, 72 insertions(+) diff --git a/Source/cmIfCommand.cxx b/Source/cmIfCommand.cxx index 2806dd4026..e82a8fb11c 100644 --- a/Source/cmIfCommand.cxx +++ b/Source/cmIfCommand.cxx @@ -408,6 +408,13 @@ namespace makefile->GetPolicies()->GetPolicyID((argP1)->c_str(), pid), reducible, arg, newArgs, argP1, argP2); } + // does a target exist + if (*arg == "TARGET" && argP1 != newArgs.end()) + { + HandlePredicate( + makefile->FindTargetToUse((argP1)->c_str())? true:false, + reducible, arg, newArgs, argP1, argP2); + } // is a variable defined if (*arg == "DEFINED" && argP1 != newArgs.end()) { diff --git a/Source/cmIfCommand.h b/Source/cmIfCommand.h index cc83919b35..6c23aa0f5a 100644 --- a/Source/cmIfCommand.h +++ b/Source/cmIfCommand.h @@ -142,6 +142,8 @@ public: " if(POLICY policy-id)\n" "True if the given name is an existing policy " "(of the form CMP).\n" + " if(TARGET target-name)\n" + "True if the given name is an existing target, built or imported.\n" " if(EXISTS file-name)\n" " if(EXISTS directory-name)\n" "True if the named file or directory exists. " diff --git a/Tests/Complex/Executable/CMakeLists.txt b/Tests/Complex/Executable/CMakeLists.txt index 1c406b90e1..9f64cbb152 100644 --- a/Tests/Complex/Executable/CMakeLists.txt +++ b/Tests/Complex/Executable/CMakeLists.txt @@ -16,6 +16,23 @@ IF(COMPLEX_TEST_CMAKELIB) LINK_DIRECTORIES(${Complex_BINARY_DIR}/../../Utilities/cmtar) ENDIF(COMPLEX_TEST_CMAKELIB) +# Create an imported target for if(TARGET) test below. +ADD_LIBRARY(ExeImportedTarget UNKNOWN IMPORTED) + +# Test if(TARGET) command. +IF(NOT TARGET CMakeTestLibrary) + MESSAGE(FATAL_ERROR "IF(NOT TARGET CMakeTestLibrary) returned true!") +ENDIF(NOT TARGET CMakeTestLibrary) +IF(NOT TARGET ExeImportedTarget) + MESSAGE(FATAL_ERROR "IF(NOT TARGET ExeImportedTarget) returned true!") +ENDIF(NOT TARGET ExeImportedTarget) +IF(TARGET LibImportedTarget) + MESSAGE(FATAL_ERROR "IF(TARGET LibImportedTarget) returned true!") +ENDIF(TARGET LibImportedTarget) +IF(TARGET NotATarget) + MESSAGE(FATAL_ERROR "IF(TARGET NotATarget) returned true!") +ENDIF(TARGET NotATarget) + # Use LINK_LIBRARIES instead of TARGET_LINK_LIBRARIES to SET(COMPLEX_LIBS CMakeTestLibrary;CMakeTestLibraryShared;CMakeTestCLibraryShared) LINK_LIBRARIES(${COMPLEX_LIBS}) diff --git a/Tests/Complex/Library/CMakeLists.txt b/Tests/Complex/Library/CMakeLists.txt index 0fae1ec1e0..4f2e5a4aec 100644 --- a/Tests/Complex/Library/CMakeLists.txt +++ b/Tests/Complex/Library/CMakeLists.txt @@ -114,6 +114,10 @@ INSTALL_FILES(/tmp .cxx ${Complex_BINARY_DIR}/cmTestConfigure.h) # Test creating a library that is not built by default. ADD_LIBRARY(notInAllLib EXCLUDE_FROM_ALL notInAllLib.cxx) +# Create an imported target for if(TARGET) test in Executable dir. +# That test should not see this target. +ADD_LIBRARY(LibImportedTarget UNKNOWN IMPORTED) + # Test generation of preprocessed sources. IF("${CMAKE_GENERATOR}" MATCHES "Makefile" AND CMAKE_MAKE_PROGRAM) IF(CMAKE_CXX_CREATE_PREPROCESSED_SOURCE) diff --git a/Tests/ComplexOneConfig/Executable/CMakeLists.txt b/Tests/ComplexOneConfig/Executable/CMakeLists.txt index 1c406b90e1..9f64cbb152 100644 --- a/Tests/ComplexOneConfig/Executable/CMakeLists.txt +++ b/Tests/ComplexOneConfig/Executable/CMakeLists.txt @@ -16,6 +16,23 @@ IF(COMPLEX_TEST_CMAKELIB) LINK_DIRECTORIES(${Complex_BINARY_DIR}/../../Utilities/cmtar) ENDIF(COMPLEX_TEST_CMAKELIB) +# Create an imported target for if(TARGET) test below. +ADD_LIBRARY(ExeImportedTarget UNKNOWN IMPORTED) + +# Test if(TARGET) command. +IF(NOT TARGET CMakeTestLibrary) + MESSAGE(FATAL_ERROR "IF(NOT TARGET CMakeTestLibrary) returned true!") +ENDIF(NOT TARGET CMakeTestLibrary) +IF(NOT TARGET ExeImportedTarget) + MESSAGE(FATAL_ERROR "IF(NOT TARGET ExeImportedTarget) returned true!") +ENDIF(NOT TARGET ExeImportedTarget) +IF(TARGET LibImportedTarget) + MESSAGE(FATAL_ERROR "IF(TARGET LibImportedTarget) returned true!") +ENDIF(TARGET LibImportedTarget) +IF(TARGET NotATarget) + MESSAGE(FATAL_ERROR "IF(TARGET NotATarget) returned true!") +ENDIF(TARGET NotATarget) + # Use LINK_LIBRARIES instead of TARGET_LINK_LIBRARIES to SET(COMPLEX_LIBS CMakeTestLibrary;CMakeTestLibraryShared;CMakeTestCLibraryShared) LINK_LIBRARIES(${COMPLEX_LIBS}) diff --git a/Tests/ComplexOneConfig/Library/CMakeLists.txt b/Tests/ComplexOneConfig/Library/CMakeLists.txt index 0fae1ec1e0..4f2e5a4aec 100644 --- a/Tests/ComplexOneConfig/Library/CMakeLists.txt +++ b/Tests/ComplexOneConfig/Library/CMakeLists.txt @@ -114,6 +114,10 @@ INSTALL_FILES(/tmp .cxx ${Complex_BINARY_DIR}/cmTestConfigure.h) # Test creating a library that is not built by default. ADD_LIBRARY(notInAllLib EXCLUDE_FROM_ALL notInAllLib.cxx) +# Create an imported target for if(TARGET) test in Executable dir. +# That test should not see this target. +ADD_LIBRARY(LibImportedTarget UNKNOWN IMPORTED) + # Test generation of preprocessed sources. IF("${CMAKE_GENERATOR}" MATCHES "Makefile" AND CMAKE_MAKE_PROGRAM) IF(CMAKE_CXX_CREATE_PREPROCESSED_SOURCE) diff --git a/Tests/ComplexRelativePaths/Executable/CMakeLists.txt b/Tests/ComplexRelativePaths/Executable/CMakeLists.txt index 1c406b90e1..9f64cbb152 100644 --- a/Tests/ComplexRelativePaths/Executable/CMakeLists.txt +++ b/Tests/ComplexRelativePaths/Executable/CMakeLists.txt @@ -16,6 +16,23 @@ IF(COMPLEX_TEST_CMAKELIB) LINK_DIRECTORIES(${Complex_BINARY_DIR}/../../Utilities/cmtar) ENDIF(COMPLEX_TEST_CMAKELIB) +# Create an imported target for if(TARGET) test below. +ADD_LIBRARY(ExeImportedTarget UNKNOWN IMPORTED) + +# Test if(TARGET) command. +IF(NOT TARGET CMakeTestLibrary) + MESSAGE(FATAL_ERROR "IF(NOT TARGET CMakeTestLibrary) returned true!") +ENDIF(NOT TARGET CMakeTestLibrary) +IF(NOT TARGET ExeImportedTarget) + MESSAGE(FATAL_ERROR "IF(NOT TARGET ExeImportedTarget) returned true!") +ENDIF(NOT TARGET ExeImportedTarget) +IF(TARGET LibImportedTarget) + MESSAGE(FATAL_ERROR "IF(TARGET LibImportedTarget) returned true!") +ENDIF(TARGET LibImportedTarget) +IF(TARGET NotATarget) + MESSAGE(FATAL_ERROR "IF(TARGET NotATarget) returned true!") +ENDIF(TARGET NotATarget) + # Use LINK_LIBRARIES instead of TARGET_LINK_LIBRARIES to SET(COMPLEX_LIBS CMakeTestLibrary;CMakeTestLibraryShared;CMakeTestCLibraryShared) LINK_LIBRARIES(${COMPLEX_LIBS}) diff --git a/Tests/ComplexRelativePaths/Library/CMakeLists.txt b/Tests/ComplexRelativePaths/Library/CMakeLists.txt index 0fae1ec1e0..4f2e5a4aec 100644 --- a/Tests/ComplexRelativePaths/Library/CMakeLists.txt +++ b/Tests/ComplexRelativePaths/Library/CMakeLists.txt @@ -114,6 +114,10 @@ INSTALL_FILES(/tmp .cxx ${Complex_BINARY_DIR}/cmTestConfigure.h) # Test creating a library that is not built by default. ADD_LIBRARY(notInAllLib EXCLUDE_FROM_ALL notInAllLib.cxx) +# Create an imported target for if(TARGET) test in Executable dir. +# That test should not see this target. +ADD_LIBRARY(LibImportedTarget UNKNOWN IMPORTED) + # Test generation of preprocessed sources. IF("${CMAKE_GENERATOR}" MATCHES "Makefile" AND CMAKE_MAKE_PROGRAM) IF(CMAKE_CXX_CREATE_PREPROCESSED_SOURCE)