From d02709d7f76624d9ec5e178d79cf3658d551f197 Mon Sep 17 00:00:00 2001 From: Gregor Jasny Date: Mon, 27 Mar 2017 20:54:22 +0200 Subject: Genex: Add `TARGET_BUNDLE_[CONTENT_]_DIR` generator expressions Closes #16733 --- Help/manual/cmake-generator-expressions.7.rst | 9 ++++ Help/release/dev/bundle-genex.rst | 12 +++++ Source/cmGeneratorExpressionNode.cxx | 61 ++++++++++++++++++++++ Source/cmGeneratorTarget.cxx | 10 +++- Source/cmGeneratorTarget.h | 1 + ...rtedTarget-TARGET_BUNDLE_CONTENT_DIR-result.txt | 1 + ...rtedTarget-TARGET_BUNDLE_CONTENT_DIR-stderr.txt | 8 +++ .../ImportedTarget-TARGET_BUNDLE_CONTENT_DIR.cmake | 2 + .../ImportedTarget-TARGET_BUNDLE_DIR-result.txt | 1 + .../ImportedTarget-TARGET_BUNDLE_DIR-stderr.txt | 8 +++ .../ImportedTarget-TARGET_BUNDLE_DIR.cmake | 2 + ...alidTarget-TARGET_BUNDLE_CONTENT_DIR-result.txt | 1 + ...alidTarget-TARGET_BUNDLE_CONTENT_DIR-stderr.txt | 8 +++ .../NonValidTarget-TARGET_BUNDLE_CONTENT_DIR.cmake | 9 ++++ .../NonValidTarget-TARGET_BUNDLE_DIR-result.txt | 1 + .../NonValidTarget-TARGET_BUNDLE_DIR-stderr.txt | 8 +++ .../NonValidTarget-TARGET_BUNDLE_DIR.cmake | 9 ++++ .../GeneratorExpression/RunCMakeTest.cmake | 4 ++ Tests/RunCMake/XcodeProject/XcodeBundles.cmake | 24 +++++++++ 19 files changed, 177 insertions(+), 2 deletions(-) create mode 100644 Help/release/dev/bundle-genex.rst create mode 100644 Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_CONTENT_DIR-result.txt create mode 100644 Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_CONTENT_DIR-stderr.txt create mode 100644 Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_CONTENT_DIR.cmake create mode 100644 Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_DIR-result.txt create mode 100644 Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_DIR-stderr.txt create mode 100644 Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_DIR.cmake create mode 100644 Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_CONTENT_DIR-result.txt create mode 100644 Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_CONTENT_DIR-stderr.txt create mode 100644 Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_CONTENT_DIR.cmake create mode 100644 Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_DIR-result.txt create mode 100644 Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_DIR-stderr.txt create mode 100644 Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_DIR.cmake diff --git a/Help/manual/cmake-generator-expressions.7.rst b/Help/manual/cmake-generator-expressions.7.rst index 3a225ad..645be1d 100644 --- a/Help/manual/cmake-generator-expressions.7.rst +++ b/Help/manual/cmake-generator-expressions.7.rst @@ -205,6 +205,15 @@ Available informational expressions are: Name of the linker generated program database file (.pdb). ``$`` Directory of the linker generated program database file (.pdb). +``$`` + Full path to the bundle directory (``my.app``, ``my.framework``, or + ``my.bundle``) where ``tgt`` is the name of a target. +``$`` + Full path to the bundle content directory where ``tgt`` is the name of a + target. For the macOS SDK it leads to ``my.app/Contents``, ``my.framework``, + or ``my.bundle/Contents``. For all other SDKs (e.g. iOS) it leads to + ``my.app``, ``my.framework``, or ``my.bundle`` due to the flat bundle + structure. ``$`` Value of the property ``prop`` on the target ``tgt``. diff --git a/Help/release/dev/bundle-genex.rst b/Help/release/dev/bundle-genex.rst new file mode 100644 index 0000000..e79b84c --- /dev/null +++ b/Help/release/dev/bundle-genex.rst @@ -0,0 +1,12 @@ +bundle-genex +------------ + +* Two new informational generator expressions to retrieve Apple Bundle + directories have been added. The first one ``$`` + outputs the full path to the Bundle directory, the other one + ``$`` outputs the full path to the + ``Contents`` directory of macOS Bundles and App Bundles. For all other + bundle types and SDKs it is identical with ``$``. + + Those new expressions are helpful to query Bundle locations independent of + the different Bundle types and layouts on macOS and iOS. diff --git a/Source/cmGeneratorExpressionNode.cxx b/Source/cmGeneratorExpressionNode.cxx index 66202df..4443499 100644 --- a/Source/cmGeneratorExpressionNode.cxx +++ b/Source/cmGeneratorExpressionNode.cxx @@ -1504,6 +1504,8 @@ class ArtifactNameTag; class ArtifactPathTag; class ArtifactPdbTag; class ArtifactSonameTag; +class ArtifactBundleDirTag; +class ArtifactBundleContentDirTag; template struct TargetFilesystemArtifactResultCreator @@ -1600,6 +1602,56 @@ struct TargetFilesystemArtifactResultCreator }; template <> +struct TargetFilesystemArtifactResultCreator +{ + static std::string Create(cmGeneratorTarget* target, + cmGeneratorExpressionContext* context, + const GeneratorExpressionContent* content) + { + if (target->IsImported()) { + ::reportError(context, content->GetOriginalExpression(), + "TARGET_BUNDLE_DIR not allowed for IMPORTED targets."); + return std::string(); + } + if (!target->IsBundleOnApple()) { + ::reportError(context, content->GetOriginalExpression(), + "TARGET_BUNDLE_DIR is allowed only for Bundle targets."); + return std::string(); + } + + std::string outpath = target->GetDirectory(context->Config) + '/'; + return target->BuildBundleDirectory(outpath, context->Config, + cmGeneratorTarget::BundleDirLevel); + } +}; + +template <> +struct TargetFilesystemArtifactResultCreator +{ + static std::string Create(cmGeneratorTarget* target, + cmGeneratorExpressionContext* context, + const GeneratorExpressionContent* content) + { + if (target->IsImported()) { + ::reportError( + context, content->GetOriginalExpression(), + "TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets."); + return std::string(); + } + if (!target->IsBundleOnApple()) { + ::reportError( + context, content->GetOriginalExpression(), + "TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets."); + return std::string(); + } + + std::string outpath = target->GetDirectory(context->Config) + '/'; + return target->BuildBundleDirectory(outpath, context->Config, + cmGeneratorTarget::ContentLevel); + } +}; + +template <> struct TargetFilesystemArtifactResultCreator { static std::string Create(cmGeneratorTarget* target, @@ -1716,6 +1768,13 @@ static const TargetFilesystemArtifactNodeGroup static const TargetFilesystemArtifactNodeGroup targetPdbNodeGroup; +static const TargetFilesystemArtifact + targetBundleDirNode; + +static const TargetFilesystemArtifact + targetBundleContentDirNode; + static const struct ShellPathNode : public cmGeneratorExpressionNode { ShellPathNode() {} @@ -1772,6 +1831,8 @@ const cmGeneratorExpressionNode* cmGeneratorExpressionNode::GetNode( nodeMap["TARGET_LINKER_FILE_DIR"] = &targetLinkerNodeGroup.FileDir; nodeMap["TARGET_SONAME_FILE_DIR"] = &targetSoNameNodeGroup.FileDir; nodeMap["TARGET_PDB_FILE_DIR"] = &targetPdbNodeGroup.FileDir; + nodeMap["TARGET_BUNDLE_DIR"] = &targetBundleDirNode; + nodeMap["TARGET_BUNDLE_CONTENT_DIR"] = &targetBundleContentDirNode; nodeMap["STREQUAL"] = &strEqualNode; nodeMap["EQUAL"] = &equalNode; nodeMap["LOWER_CASE"] = &lowerCaseNode; diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 2938fde..7797508 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -1493,6 +1493,12 @@ static bool shouldAddFullLevel(cmGeneratorTarget::BundleDirectoryLevel level) return level == cmGeneratorTarget::FullLevel; } +static bool shouldAddContentLevel( + cmGeneratorTarget::BundleDirectoryLevel level) +{ + return level == cmGeneratorTarget::ContentLevel || shouldAddFullLevel(level); +} + std::string cmGeneratorTarget::GetAppBundleDirectory( const std::string& config, BundleDirectoryLevel level) const { @@ -1503,7 +1509,7 @@ std::string cmGeneratorTarget::GetAppBundleDirectory( ext = "app"; } fpath += ext; - if (!this->Makefile->PlatformIsAppleIos()) { + if (shouldAddContentLevel(level) && !this->Makefile->PlatformIsAppleIos()) { fpath += "/Contents"; if (shouldAddFullLevel(level)) { fpath += "/MacOS"; @@ -1533,7 +1539,7 @@ std::string cmGeneratorTarget::GetCFBundleDirectory( } } fpath += ext; - if (!this->Makefile->PlatformIsAppleIos()) { + if (shouldAddContentLevel(level) && !this->Makefile->PlatformIsAppleIos()) { fpath += "/Contents"; if (shouldAddFullLevel(level)) { fpath += "/MacOS"; diff --git a/Source/cmGeneratorTarget.h b/Source/cmGeneratorTarget.h index 07a59ae..7ceaab6 100644 --- a/Source/cmGeneratorTarget.h +++ b/Source/cmGeneratorTarget.h @@ -163,6 +163,7 @@ public: /** What hierarchy level should the reported directory contain */ enum BundleDirectoryLevel { + BundleDirLevel, ContentLevel, FullLevel }; diff --git a/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_CONTENT_DIR-result.txt b/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_CONTENT_DIR-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_CONTENT_DIR-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_CONTENT_DIR-stderr.txt b/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_CONTENT_DIR-stderr.txt new file mode 100644 index 0000000..854447f --- /dev/null +++ b/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_CONTENT_DIR-stderr.txt @@ -0,0 +1,8 @@ +CMake Error at ImportedTarget-TARGET_BUNDLE_CONTENT_DIR.cmake:[0-9]* \(add_custom_target\): + Error evaluating generator expression: + + \$ + + TARGET_BUNDLE_CONTENT_DIR not allowed for IMPORTED targets. +Call Stack \(most recent call first\): + CMakeLists.txt:[0-9]* \(include\) diff --git a/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_CONTENT_DIR.cmake b/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_CONTENT_DIR.cmake new file mode 100644 index 0000000..ac2d3ce --- /dev/null +++ b/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_CONTENT_DIR.cmake @@ -0,0 +1,2 @@ +add_library(empty UNKNOWN IMPORTED) +add_custom_target(custom COMMAND echo $) diff --git a/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_DIR-result.txt b/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_DIR-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_DIR-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_DIR-stderr.txt b/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_DIR-stderr.txt new file mode 100644 index 0000000..9b97df1 --- /dev/null +++ b/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_DIR-stderr.txt @@ -0,0 +1,8 @@ +CMake Error at ImportedTarget-TARGET_BUNDLE_DIR.cmake:[0-9]* \(add_custom_target\): + Error evaluating generator expression: + + \$ + + TARGET_BUNDLE_DIR not allowed for IMPORTED targets. +Call Stack \(most recent call first\): + CMakeLists.txt:[0-9]* \(include\) diff --git a/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_DIR.cmake b/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_DIR.cmake new file mode 100644 index 0000000..17c8128 --- /dev/null +++ b/Tests/RunCMake/GeneratorExpression/ImportedTarget-TARGET_BUNDLE_DIR.cmake @@ -0,0 +1,2 @@ +add_library(empty UNKNOWN IMPORTED) +add_custom_target(custom COMMAND echo $) diff --git a/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_CONTENT_DIR-result.txt b/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_CONTENT_DIR-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_CONTENT_DIR-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_CONTENT_DIR-stderr.txt b/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_CONTENT_DIR-stderr.txt new file mode 100644 index 0000000..03c02d9 --- /dev/null +++ b/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_CONTENT_DIR-stderr.txt @@ -0,0 +1,8 @@ +CMake Error at NonValidTarget-TARGET_BUNDLE_CONTENT_DIR.cmake:[0-9]* \(file\): + Error evaluating generator expression: + + \$ + + TARGET_BUNDLE_CONTENT_DIR is allowed only for Bundle targets. +Call Stack \(most recent call first\): + CMakeLists.txt:[0-9]* \(include\) diff --git a/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_CONTENT_DIR.cmake b/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_CONTENT_DIR.cmake new file mode 100644 index 0000000..63b3b1b --- /dev/null +++ b/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_CONTENT_DIR.cmake @@ -0,0 +1,9 @@ + +enable_language(C) + +add_library(empty STATIC empty.c) + +file(GENERATE + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/test.txt" + CONTENT "[$]" +) diff --git a/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_DIR-result.txt b/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_DIR-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_DIR-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_DIR-stderr.txt b/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_DIR-stderr.txt new file mode 100644 index 0000000..f895c88 --- /dev/null +++ b/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_DIR-stderr.txt @@ -0,0 +1,8 @@ +CMake Error at NonValidTarget-TARGET_BUNDLE_DIR.cmake:[0-9]* \(file\): + Error evaluating generator expression: + + \$ + + TARGET_BUNDLE_DIR is allowed only for Bundle targets. +Call Stack \(most recent call first\): + CMakeLists.txt:[0-9]* \(include\) diff --git a/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_DIR.cmake b/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_DIR.cmake new file mode 100644 index 0000000..19f333a --- /dev/null +++ b/Tests/RunCMake/GeneratorExpression/NonValidTarget-TARGET_BUNDLE_DIR.cmake @@ -0,0 +1,9 @@ + +enable_language(C) + +add_library(empty STATIC empty.c) + +file(GENERATE + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/test.txt" + CONTENT "[$]" +) diff --git a/Tests/RunCMake/GeneratorExpression/RunCMakeTest.cmake b/Tests/RunCMake/GeneratorExpression/RunCMakeTest.cmake index 084b5c3..63cd2da 100644 --- a/Tests/RunCMake/GeneratorExpression/RunCMakeTest.cmake +++ b/Tests/RunCMake/GeneratorExpression/RunCMakeTest.cmake @@ -17,6 +17,8 @@ run_cmake(NonValidTarget-C_COMPILER_ID) run_cmake(NonValidTarget-CXX_COMPILER_ID) run_cmake(NonValidTarget-C_COMPILER_VERSION) run_cmake(NonValidTarget-CXX_COMPILER_VERSION) +run_cmake(NonValidTarget-TARGET_BUNDLE_DIR) +run_cmake(NonValidTarget-TARGET_BUNDLE_CONTENT_DIR) run_cmake(NonValidTarget-TARGET_PROPERTY) run_cmake(NonValidTarget-TARGET_POLICY) run_cmake(COMPILE_LANGUAGE-add_custom_target) @@ -32,6 +34,8 @@ run_cmake(OUTPUT_NAME-recursion) run_cmake(TARGET_PROPERTY-LOCATION) run_cmake(LINK_ONLY-not-linking) +run_cmake(ImportedTarget-TARGET_BUNDLE_DIR) +run_cmake(ImportedTarget-TARGET_BUNDLE_CONTENT_DIR) run_cmake(ImportedTarget-TARGET_PDB_FILE) if(LINKER_SUPPORTS_PDB) run_cmake(NonValidTarget-TARGET_PDB_FILE) diff --git a/Tests/RunCMake/XcodeProject/XcodeBundles.cmake b/Tests/RunCMake/XcodeProject/XcodeBundles.cmake index 833eb85..0b854d8 100644 --- a/Tests/RunCMake/XcodeProject/XcodeBundles.cmake +++ b/Tests/RunCMake/XcodeProject/XcodeBundles.cmake @@ -58,6 +58,10 @@ if(NOT TEST_IOS OR NOT XCODE_VERSION VERSION_LESS 6) add_custom_target(SharedFrameworkTest ALL COMMAND ${CMAKE_COMMAND} -E copy + "$" "$.old" + COMMAND ${CMAKE_COMMAND} -E copy + "$" "$.old" + COMMAND ${CMAKE_COMMAND} -E copy "$" "$.old") add_dependencies(SharedFrameworkTest SharedFramework) @@ -71,6 +75,10 @@ if(NOT TEST_IOS OR NOT XCODE_VERSION VERSION_LESS 6) add_custom_target(SharedFrameworkExtTest ALL COMMAND ${CMAKE_COMMAND} -E copy + "$" "$.old" + COMMAND ${CMAKE_COMMAND} -E copy + "$" "$.old" + COMMAND ${CMAKE_COMMAND} -E copy "$" "$.old") add_dependencies(SharedFrameworkExtTest SharedFrameworkExt) @@ -84,6 +92,10 @@ if(NOT XCODE_VERSION VERSION_LESS 6) add_custom_target(StaticFrameworkTest ALL COMMAND ${CMAKE_COMMAND} -E copy + "$" "$.old" + COMMAND ${CMAKE_COMMAND} -E copy + "$" "$.old" + COMMAND ${CMAKE_COMMAND} -E copy "$" "$.old") add_dependencies(StaticFrameworkTest StaticFramework) @@ -97,6 +109,10 @@ if(NOT XCODE_VERSION VERSION_LESS 6) add_custom_target(StaticFrameworkExtTest ALL COMMAND ${CMAKE_COMMAND} -E copy + "$" "$.old" + COMMAND ${CMAKE_COMMAND} -E copy + "$" "$.old" + COMMAND ${CMAKE_COMMAND} -E copy "$" "$.old") add_dependencies(StaticFrameworkExtTest StaticFrameworkExt) @@ -110,6 +126,10 @@ if(NOT CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE) add_custom_target(BundleTest ALL COMMAND ${CMAKE_COMMAND} -E copy + "$" "$.old" + COMMAND ${CMAKE_COMMAND} -E copy + "$" "$.old" + COMMAND ${CMAKE_COMMAND} -E copy "$" "$.old") add_dependencies(BundleTest Bundle) @@ -123,6 +143,10 @@ if(NOT CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE) add_custom_target(BundleExtTest ALL COMMAND ${CMAKE_COMMAND} -E copy + "$" "$.old" + COMMAND ${CMAKE_COMMAND} -E copy + "$" "$.old" + COMMAND ${CMAKE_COMMAND} -E copy "$" "$.old") add_dependencies(BundleExtTest BundleExt) -- cgit v0.12