From abe26af2bf31ce06fda0aa4dd40c61cd532dbaac Mon Sep 17 00:00:00 2001
From: Bartosz Kosiorek <gang65@poczta.onet.pl>
Date: Thu, 26 Nov 2015 13:22:59 +0100
Subject: [PATCH] Fix resource directory structure, update help and tests for
 iOS, bug 15848.

A typical iOS application bundle (also Framework Bundle
contains the application executable and any resources used by
the application (for instance, the application icon, other images,
and localized content) in the top-level bundle directory.
The same rule is applicable for Framework Bundles, bug: 15848
---
 Help/prop_tgt/MACOSX_BUNDLE.rst                    |  2 +-
 Help/prop_tgt/MACOSX_RPATH.rst                     |  6 +-
 Help/prop_tgt/RESOURCE.rst                         | 66 +++++++++++++++++++---
 Source/cmGeneratorTarget.cxx                       |  6 +-
 Tests/RunCMake/Framework/FrameworkLayout.cmake     |  4 +-
 .../Framework/OSXFrameworkLayout-build-check.cmake | 12 +++-
 .../Framework/iOSFrameworkLayout-build-check.cmake | 12 +++-
 Tests/RunCMake/Framework/res.txt                   |  0
 8 files changed, 92 insertions(+), 16 deletions(-)
 create mode 100644 Tests/RunCMake/Framework/res.txt

diff --git a/Help/prop_tgt/MACOSX_BUNDLE.rst b/Help/prop_tgt/MACOSX_BUNDLE.rst
index 8d7d914..7cd8046 100644
--- a/Help/prop_tgt/MACOSX_BUNDLE.rst
+++ b/Help/prop_tgt/MACOSX_BUNDLE.rst
@@ -3,7 +3,7 @@ MACOSX_BUNDLE
 
 Build an executable as an Application Bundle on OS X or iOS.
 
-When this property is set to true the executable when built on OS X
+When this property is set to ``TRUE`` the executable when built on OS X
 or iOS will be created as an application bundle.  This makes it
 a GUI executable that can be launched from the Finder.  See the
 :prop_tgt:`MACOSX_FRAMEWORK_INFO_PLIST` target property for information about
diff --git a/Help/prop_tgt/MACOSX_RPATH.rst b/Help/prop_tgt/MACOSX_RPATH.rst
index 41bb8cc..1f9a036 100644
--- a/Help/prop_tgt/MACOSX_RPATH.rst
+++ b/Help/prop_tgt/MACOSX_RPATH.rst
@@ -3,8 +3,8 @@ MACOSX_RPATH
 
 Whether this target on OS X or iOS is located at runtime using rpaths.
 
-When this property is set to true, the directory portion of
-the "install_name" field of this shared library will be ``@rpath``
+When this property is set to ``TRUE``, the directory portion of
+the ``install_name`` field of this shared library will be ``@rpath``
 unless overridden by :prop_tgt:`INSTALL_NAME_DIR`.  This indicates
 the shared library is to be found at runtime using runtime
 paths (rpaths).
@@ -18,6 +18,6 @@ can be controlled by the :prop_tgt:`INSTALL_RPATH` target property on
 the target linking to this target.
 
 Policy :policy:`CMP0042` was introduced to change the default value of
-``MACOSX_RPATH`` to ``TRUE.  This is because use of ``@rpath`` is a
+``MACOSX_RPATH`` to ``TRUE``.  This is because use of ``@rpath`` is a
 more flexible and powerful alternative to ``@executable_path`` and
 ``@loader_path``.
diff --git a/Help/prop_tgt/RESOURCE.rst b/Help/prop_tgt/RESOURCE.rst
index 5dad3ea..8b89c44 100644
--- a/Help/prop_tgt/RESOURCE.rst
+++ b/Help/prop_tgt/RESOURCE.rst
@@ -1,11 +1,61 @@
 RESOURCE
 --------
 
-Specify resource files in a :prop_tgt:`FRAMEWORK` shared library target.
-
-Shared library targets marked with the :prop_tgt:`FRAMEWORK` property generate
-frameworks on OS X, iOS and normal shared libraries on other platforms.
-This property may be set to a list of files to be placed in the
-``Resources`` directory inside the framework folder.  On non-Apple
-platforms these files may be installed using the ``RESOURCE`` option to
-the ``install(TARGETS)`` command.
+Specify resource files in a :prop_tgt:`FRAMEWORK` or :prop_tgt:`BUNDLE`.
+
+Target marked with the :prop_tgt:`FRAMEWORK` or :prop_tgt:`BUNDLE` property
+generate framework or application bundle (both OS X and iOS is supported)
+or normal shared libraries on other platforms.
+This property may be set to a list of files to be placed in the corresponding
+directory (eg. ``Resources`` directory for OS X) inside the bundle.
+On non-Apple platforms these files may be installed using the ``RESOURCE``
+option to the ``install(TARGETS)`` command.
+
+Following example of Application Bundle:
+
+.. code-block:: cmake
+
+  add_executable(ExecutableTarget
+    addDemo.c
+    resourcefile.txt
+    appresourcedir/appres.txt
+  )
+
+  target_link_libraries(ExecutableTarget heymath mul)
+
+  set(RESOURCE_FILES
+    resourcefile.txt
+    appresourcedir/appres.txt
+  )
+
+  set_target_properties(ExecutableTarget PROPERTIES
+    MACOSX_BUNDLE TRUE
+    MACOSX_FRAMEWORK_IDENTIFIER org.cmake.ExecutableTarget
+    RESOURCE "${RESOURCE_FILES}"
+  )
+
+will produce flat structure for iOS systems::
+
+  ExecutableTarget.app
+    appres.txt
+    ExecutableTarget
+    Info.plist
+    resourcefile.txt
+
+For OS X systems it will produce following directory structure::
+
+  ExecutableTarget.app/
+    Contents
+      Info.plist
+      MacOS
+        ExecutableTarget
+      Resources
+        appres.txt
+        resourcefile.txt
+
+For Linux, such cmake script produce following files::
+
+  ExecutableTarget
+  Resources
+    appres.txt
+    resourcefile.txt
\ No newline at end of file
diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx
index cc424b4..b05fb41 100644
--- a/Source/cmGeneratorTarget.cxx
+++ b/Source/cmGeneratorTarget.cxx
@@ -3752,7 +3752,11 @@ void cmGeneratorTarget::ConstructSourceFileFlags() const
       if(cmSourceFile* sf = this->Makefile->GetSource(*it))
         {
         SourceFileFlags& flags = this->SourceFlagsMap[sf];
-        flags.MacFolder = "Resources";
+        flags.MacFolder = "";
+        if(!this->Makefile->PlatformIsAppleIos())
+          {
+          flags.MacFolder = "Resources";
+          }
         flags.Type = cmGeneratorTarget::SourceFileTypeResource;
         }
       }
diff --git a/Tests/RunCMake/Framework/FrameworkLayout.cmake b/Tests/RunCMake/Framework/FrameworkLayout.cmake
index 107afdf..8a49d6e 100644
--- a/Tests/RunCMake/Framework/FrameworkLayout.cmake
+++ b/Tests/RunCMake/Framework/FrameworkLayout.cmake
@@ -2,4 +2,6 @@ cmake_minimum_required(VERSION 3.4)
 enable_language(C)
 
 add_library(Framework SHARED foo.c)
-set_target_properties(Framework PROPERTIES FRAMEWORK TRUE)
+set_target_properties(Framework PROPERTIES
+                      FRAMEWORK TRUE
+                      RESOURCE "res.txt")
diff --git a/Tests/RunCMake/Framework/OSXFrameworkLayout-build-check.cmake b/Tests/RunCMake/Framework/OSXFrameworkLayout-build-check.cmake
index 27d10d8..224c251 100644
--- a/Tests/RunCMake/Framework/OSXFrameworkLayout-build-check.cmake
+++ b/Tests/RunCMake/Framework/OSXFrameworkLayout-build-check.cmake
@@ -1,7 +1,9 @@
 set(framework-dir "${RunCMake_TEST_BINARY_DIR}/Framework.framework")
-set(plist-file "${framework-dir}/Resources/Info.plist")
+set(framework-resources "${framework-dir}/Resources")
+set(framework-resource-file "${framework-resources}/res.txt")
 set(framework-library "${framework-dir}/Framework")
 set(framework-versions "${framework-dir}/Versions")
+set(plist-file "${framework-resources}/Info.plist")
 
 if(NOT IS_DIRECTORY ${framework-dir})
   message(SEND_ERROR "Framework not found at ${framework-dir}")
@@ -15,6 +17,14 @@ if(NOT EXISTS ${framework-library})
   message(SEND_ERROR "Framework library not found at ${framework-library}")
 endif()
 
+if(NOT EXISTS ${framework-resource-file})
+  message(SEND_ERROR "Framework resource file not found at ${framework-resource-file}")
+endif()
+
 if(NOT EXISTS ${framework-versions})
   message(SEND_ERROR "Framework versions not found at ${framework-versions}")
 endif()
+
+if(NOT EXISTS ${framework-resources})
+  message(SEND_ERROR "Framework Resources not found at ${framework-resources}")
+endif()
diff --git a/Tests/RunCMake/Framework/iOSFrameworkLayout-build-check.cmake b/Tests/RunCMake/Framework/iOSFrameworkLayout-build-check.cmake
index 373baad..0792b23 100644
--- a/Tests/RunCMake/Framework/iOSFrameworkLayout-build-check.cmake
+++ b/Tests/RunCMake/Framework/iOSFrameworkLayout-build-check.cmake
@@ -1,7 +1,9 @@
 set(framework-dir "${RunCMake_TEST_BINARY_DIR}/Framework.framework")
-set(plist-file "${framework-dir}/Info.plist")
+set(framework-resources "${framework-dir}/Resources")
+set(framework-resource-file "${framework-dir}/res.txt")
 set(framework-library "${framework-dir}/Framework")
 set(framework-versions "${framework-dir}/Versions")
+set(plist-file "${framework-dir}/Info.plist")
 
 if(NOT IS_DIRECTORY ${framework-dir})
   message(SEND_ERROR "Framework not found at ${framework-dir}")
@@ -15,6 +17,14 @@ if(NOT EXISTS ${framework-library})
   message(SEND_ERROR "Framework library not found at ${framework-library}")
 endif()
 
+if(NOT EXISTS ${framework-resource-file})
+  message(SEND_ERROR "Framework resource file not found at ${framework-resource-file}")
+endif()
+
 if(EXISTS ${framework-versions})
   message(SEND_ERROR "Framework versions found at ${framework-versions}")
 endif()
+
+if(EXISTS ${framework-resources})
+  message(SEND_ERROR "Framework Resources not found at ${framework-resources}")
+endif()
diff --git a/Tests/RunCMake/Framework/res.txt b/Tests/RunCMake/Framework/res.txt
new file mode 100644
index 0000000..e69de29
-- 
2.4.9 (Apple Git-60)

