From 1a3d125de8b06a57cde26f12426601f422e5c755 Mon Sep 17 00:00:00 2001 From: Asit Dhal Date: Sat, 24 Oct 2020 18:34:50 +0200 Subject: target_sources: Support custom targets Fixes: #21034 --- Help/command/target_sources.rst | 6 ++-- .../dev/target-sources-supports-custom-target.rst | 4 +++ Source/cmTargetPropCommandBase.cxx | 34 ++++++++++++++++------ .../AddCustomTargetCheckProperty.cmake | 16 ++++++++++ .../TargetSources/AddCustomTargetGenx.cmake | 2 ++ .../AddCustomTargetInterfaceSources-result.txt | 1 + .../AddCustomTargetInterfaceSources-stderr.txt | 4 +++ .../AddCustomTargetInterfaceSources.cmake | 2 ++ .../AddCustomTargetPrivateSources.cmake | 2 ++ .../AddCustomTargetPublicSources-result.txt | 1 + .../AddCustomTargetPublicSources-stderr.txt | 4 +++ .../AddCustomTargetPublicSources.cmake | 2 ++ .../AddCustomTargetSources-result.txt | 1 + .../AddCustomTargetSources-stderr.txt | 4 +++ .../TargetSources/AddCustomTargetSources.cmake | 2 ++ Tests/RunCMake/TargetSources/RunCMakeTest.cmake | 6 ++++ 16 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 Help/release/dev/target-sources-supports-custom-target.rst create mode 100644 Tests/RunCMake/TargetSources/AddCustomTargetCheckProperty.cmake create mode 100644 Tests/RunCMake/TargetSources/AddCustomTargetGenx.cmake create mode 100644 Tests/RunCMake/TargetSources/AddCustomTargetInterfaceSources-result.txt create mode 100644 Tests/RunCMake/TargetSources/AddCustomTargetInterfaceSources-stderr.txt create mode 100644 Tests/RunCMake/TargetSources/AddCustomTargetInterfaceSources.cmake create mode 100644 Tests/RunCMake/TargetSources/AddCustomTargetPrivateSources.cmake create mode 100644 Tests/RunCMake/TargetSources/AddCustomTargetPublicSources-result.txt create mode 100644 Tests/RunCMake/TargetSources/AddCustomTargetPublicSources-stderr.txt create mode 100644 Tests/RunCMake/TargetSources/AddCustomTargetPublicSources.cmake create mode 100644 Tests/RunCMake/TargetSources/AddCustomTargetSources-result.txt create mode 100644 Tests/RunCMake/TargetSources/AddCustomTargetSources-stderr.txt create mode 100644 Tests/RunCMake/TargetSources/AddCustomTargetSources.cmake diff --git a/Help/command/target_sources.rst b/Help/command/target_sources.rst index 653b8d7..07fb937 100644 --- a/Help/command/target_sources.rst +++ b/Help/command/target_sources.rst @@ -15,7 +15,8 @@ Specifies sources to use when building a target and/or its dependents. Relative source file paths are interpreted as being relative to the current source directory (i.e. :variable:`CMAKE_CURRENT_SOURCE_DIR`). The named ```` must have been created by a command such as -:command:`add_executable` or :command:`add_library` and must not be an +:command:`add_executable` or :command:`add_library` or +:command:`add_custom_target` and must not be an :ref:`ALIAS target `. The ``INTERFACE``, ``PUBLIC`` and ``PRIVATE`` keywords are required to @@ -27,7 +28,8 @@ items will populate the :prop_tgt:`SOURCES` property of when building dependents. (:ref:`IMPORTED targets ` only support ``INTERFACE`` items because they are not build targets.) The following arguments specify sources. Repeated calls for the same -```` append items in the order called. +```` append items in the order called. The targets created by +:command:`add_custom_target` can only have ``PRIVATE`` scope. Arguments to ``target_sources`` may use "generator expressions" with the syntax ``$<...>``. See the :manual:`cmake-generator-expressions(7)` diff --git a/Help/release/dev/target-sources-supports-custom-target.rst b/Help/release/dev/target-sources-supports-custom-target.rst new file mode 100644 index 0000000..131fb3a --- /dev/null +++ b/Help/release/dev/target-sources-supports-custom-target.rst @@ -0,0 +1,4 @@ +target-sources-supports-custom-target +------------------------------------- + +* The :command:`target_sources` now supports custom targets. diff --git a/Source/cmTargetPropCommandBase.cxx b/Source/cmTargetPropCommandBase.cxx index 9e30136..b050a58 100644 --- a/Source/cmTargetPropCommandBase.cxx +++ b/Source/cmTargetPropCommandBase.cxx @@ -45,15 +45,26 @@ bool cmTargetPropCommandBase::HandleArguments( this->HandleMissingTarget(args[0]); return false; } - if ((this->Target->GetType() != cmStateEnums::EXECUTABLE) && - (this->Target->GetType() != cmStateEnums::STATIC_LIBRARY) && - (this->Target->GetType() != cmStateEnums::SHARED_LIBRARY) && - (this->Target->GetType() != cmStateEnums::MODULE_LIBRARY) && - (this->Target->GetType() != cmStateEnums::OBJECT_LIBRARY) && - (this->Target->GetType() != cmStateEnums::INTERFACE_LIBRARY) && - (this->Target->GetType() != cmStateEnums::UNKNOWN_LIBRARY)) { - this->SetError("called with non-compilable target type"); - return false; + const bool isRegularTarget = + (this->Target->GetType() == cmStateEnums::EXECUTABLE) || + (this->Target->GetType() == cmStateEnums::STATIC_LIBRARY) || + (this->Target->GetType() == cmStateEnums::SHARED_LIBRARY) || + (this->Target->GetType() == cmStateEnums::MODULE_LIBRARY) || + (this->Target->GetType() == cmStateEnums::OBJECT_LIBRARY) || + (this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY) || + (this->Target->GetType() == cmStateEnums::UNKNOWN_LIBRARY); + const bool isCustomTarget = this->Target->GetType() == cmStateEnums::UTILITY; + + if (prop == "SOURCES") { + if (!isRegularTarget && !isCustomTarget) { + this->SetError("called with non-compilable target type"); + return false; + } + } else { + if (!isRegularTarget) { + this->SetError("called with non-compilable target type"); + return false; + } } bool system = false; @@ -131,6 +142,11 @@ bool cmTargetPropCommandBase::ProcessContentArgs( this->SetError("may only set INTERFACE properties on IMPORTED targets"); return false; } + if (this->Target->GetType() == cmStateEnums::UTILITY && + scope != "PRIVATE") { + this->SetError("may only set PRIVATE properties on custom targets"); + return false; + } } return this->PopulateTargetProperies(scope, content, prepend, system); } diff --git a/Tests/RunCMake/TargetSources/AddCustomTargetCheckProperty.cmake b/Tests/RunCMake/TargetSources/AddCustomTargetCheckProperty.cmake new file mode 100644 index 0000000..1787e87 --- /dev/null +++ b/Tests/RunCMake/TargetSources/AddCustomTargetCheckProperty.cmake @@ -0,0 +1,16 @@ +add_custom_target(target1 ALL) +target_sources(target1 PRIVATE main.cpp) +get_property(actualProp1 TARGET target1 PROPERTY SOURCES) +set(desiredProp1 main.cpp) +if(NOT desiredProp1 STREQUAL actualProp1) + message("source property not set. desired: \"${desiredProp1}\" actual: \"${actualProp1}\"") +endif() + +add_custom_target(target2 ALL SOURCES main.cpp) +target_sources(target2 PRIVATE empty_1.cpp empty_2.cpp) +target_sources(target2 PRIVATE empty_3.cpp) +get_property(actualProp2 TARGET target2 PROPERTY SOURCES) +set(desiredProp2 main.cpp empty_1.cpp empty_2.cpp empty_3.cpp) +if (NOT desiredProp2 STREQUAL actualProp2) + message("source property not set. desired: \"${desiredProp2}\" actual: \"${actualProp2}\"") +endif() diff --git a/Tests/RunCMake/TargetSources/AddCustomTargetGenx.cmake b/Tests/RunCMake/TargetSources/AddCustomTargetGenx.cmake new file mode 100644 index 0000000..0078eab --- /dev/null +++ b/Tests/RunCMake/TargetSources/AddCustomTargetGenx.cmake @@ -0,0 +1,2 @@ +add_custom_target(target ALL) +target_sources(target PRIVATE $) diff --git a/Tests/RunCMake/TargetSources/AddCustomTargetInterfaceSources-result.txt b/Tests/RunCMake/TargetSources/AddCustomTargetInterfaceSources-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/TargetSources/AddCustomTargetInterfaceSources-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/TargetSources/AddCustomTargetInterfaceSources-stderr.txt b/Tests/RunCMake/TargetSources/AddCustomTargetInterfaceSources-stderr.txt new file mode 100644 index 0000000..9334bf6 --- /dev/null +++ b/Tests/RunCMake/TargetSources/AddCustomTargetInterfaceSources-stderr.txt @@ -0,0 +1,4 @@ +CMake Error at AddCustomTargetInterfaceSources.cmake:2 \(target_sources\): + target_sources may only set PRIVATE properties on custom targets +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/TargetSources/AddCustomTargetInterfaceSources.cmake b/Tests/RunCMake/TargetSources/AddCustomTargetInterfaceSources.cmake new file mode 100644 index 0000000..42a8ca2 --- /dev/null +++ b/Tests/RunCMake/TargetSources/AddCustomTargetInterfaceSources.cmake @@ -0,0 +1,2 @@ +add_custom_target(target ALL) +target_sources(target INTERFACE main.cpp) diff --git a/Tests/RunCMake/TargetSources/AddCustomTargetPrivateSources.cmake b/Tests/RunCMake/TargetSources/AddCustomTargetPrivateSources.cmake new file mode 100644 index 0000000..11f0258 --- /dev/null +++ b/Tests/RunCMake/TargetSources/AddCustomTargetPrivateSources.cmake @@ -0,0 +1,2 @@ +add_custom_target(target ALL) +target_sources(target PRIVATE main.cpp) diff --git a/Tests/RunCMake/TargetSources/AddCustomTargetPublicSources-result.txt b/Tests/RunCMake/TargetSources/AddCustomTargetPublicSources-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/TargetSources/AddCustomTargetPublicSources-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/TargetSources/AddCustomTargetPublicSources-stderr.txt b/Tests/RunCMake/TargetSources/AddCustomTargetPublicSources-stderr.txt new file mode 100644 index 0000000..afba4be --- /dev/null +++ b/Tests/RunCMake/TargetSources/AddCustomTargetPublicSources-stderr.txt @@ -0,0 +1,4 @@ +CMake Error at AddCustomTargetPublicSources.cmake:2 \(target_sources\): + target_sources may only set PRIVATE properties on custom targets +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/TargetSources/AddCustomTargetPublicSources.cmake b/Tests/RunCMake/TargetSources/AddCustomTargetPublicSources.cmake new file mode 100644 index 0000000..d9e82c0 --- /dev/null +++ b/Tests/RunCMake/TargetSources/AddCustomTargetPublicSources.cmake @@ -0,0 +1,2 @@ +add_custom_target(target ALL) +target_sources(target PUBLIC main.cpp) diff --git a/Tests/RunCMake/TargetSources/AddCustomTargetSources-result.txt b/Tests/RunCMake/TargetSources/AddCustomTargetSources-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/TargetSources/AddCustomTargetSources-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/TargetSources/AddCustomTargetSources-stderr.txt b/Tests/RunCMake/TargetSources/AddCustomTargetSources-stderr.txt new file mode 100644 index 0000000..4a153e9 --- /dev/null +++ b/Tests/RunCMake/TargetSources/AddCustomTargetSources-stderr.txt @@ -0,0 +1,4 @@ +CMake Error at AddCustomTargetSources.cmake:2 \(target_sources\): + target_sources called with invalid arguments +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/TargetSources/AddCustomTargetSources.cmake b/Tests/RunCMake/TargetSources/AddCustomTargetSources.cmake new file mode 100644 index 0000000..dd688d3 --- /dev/null +++ b/Tests/RunCMake/TargetSources/AddCustomTargetSources.cmake @@ -0,0 +1,2 @@ +add_custom_target(target ALL) +target_sources(target main.cpp) diff --git a/Tests/RunCMake/TargetSources/RunCMakeTest.cmake b/Tests/RunCMake/TargetSources/RunCMakeTest.cmake index 0d462ba..b56ee44 100644 --- a/Tests/RunCMake/TargetSources/RunCMakeTest.cmake +++ b/Tests/RunCMake/TargetSources/RunCMakeTest.cmake @@ -14,3 +14,9 @@ run_cmake(RelativePathInSubdirInterface) run_cmake(RelativePathInSubdirPrivate) run_cmake(RelativePathInSubdirInclude) run_cmake(ExportBuild) +run_cmake(AddCustomTargetPublicSources) +run_cmake(AddCustomTargetPrivateSources) +run_cmake(AddCustomTargetInterfaceSources) +run_cmake(AddCustomTargetSources) +run_cmake(AddCustomTargetCheckProperty) +run_cmake(AddCustomTargetGenx) -- cgit v0.12