From 2b001c12e5bcb1ceda54f3174e8796186156d799 Mon Sep 17 00:00:00 2001
From: Gregor Jasny <gjasny@googlemail.com>
Date: Mon, 8 Dec 2014 13:32:13 +0100
Subject: [PATCH 2/3] POC for adding XCTest targets
To: cmake-developers@cmake.org

---
 Source/cmGlobalXCodeGenerator.cxx | 31 +++++++++++++++++++++++++++++--
 Source/cmTarget.cxx               |  9 +++++++++
 Source/cmTarget.h                 |  3 +++
 3 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx
index d222288..ff8fc96 100644
--- a/Source/cmGlobalXCodeGenerator.cxx
+++ b/Source/cmGlobalXCodeGenerator.cxx
@@ -761,6 +761,10 @@ GetSourcecodeValueFromFileExtension(const std::string& _ext,
     {
     sourcecode = "compiled.mach-o.objfile";
     }
+  else if(ext == "xctest")
+    {
+    sourcecode = "wrapper.cfbundle";
+    }
   else if(ext == "xib")
     {
     keepLastKnownFileType = true;
@@ -2360,6 +2364,25 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
     buildSettings->AddAttribute("DYLIB_COMPATIBILITY_VERSION",
                                 this->CreateString(vso.str().c_str()));
     }
+
+  if(target.IsXCTestOnApple())
+    {
+    buildSettings->AddAttribute("BUNDLE_LOADER", this->CreateString("$(TEST_HOST)"));
+
+    const char *testHost= target.GetProperty("TEST_HOST");
+    if(testHost)
+      {
+      	cmTarget *target = this->CurrentMakefile->FindTarget(testHost);
+      	if(target)
+      	  {
+      	  //std::cout << "****** " << target->GetAppBundleDirectory(configName, false) << std::endl;
+      	  //std::cout << "****** " << target->GetLocationForBuild() << std::endl;
+      	  buildSettings->AddAttribute("TEST_HOST", this->CreateString(target->GetLocationForBuild()));
+      	  }
+      	///asdf;
+      }
+    //buildSettings->AddAttribute("TEST_HOST", this->CreateString("$(BUILT_PRODUCTS_DIR)/CocoaExample.app/Contents/MacOS/CocoaExample"));
+    }
   // put this last so it can override existing settings
   // Convert "XCODE_ATTRIBUTE_*" properties directly.
   {
@@ -2558,7 +2581,9 @@ const char* cmGlobalXCodeGenerator::GetTargetFileType(cmTarget& cmtarget)
     case cmTarget::STATIC_LIBRARY:
       return "archive.ar";
     case cmTarget::MODULE_LIBRARY:
-      if (cmtarget.IsCFBundleOnApple())
+      if (cmtarget.IsXCTestOnApple())
+      	return "wrapper.cfbundle";
+      else if (cmtarget.IsCFBundleOnApple())
         return "wrapper.plug-in";
       else
         return ((this->XcodeVersion >= 22)?
@@ -2582,7 +2607,9 @@ const char* cmGlobalXCodeGenerator::GetTargetProductType(cmTarget& cmtarget)
     case cmTarget::STATIC_LIBRARY:
       return "com.apple.product-type.library.static";
     case cmTarget::MODULE_LIBRARY:
-      if (cmtarget.IsCFBundleOnApple())
+      if (cmtarget.IsXCTestOnApple())
+      	return "com.apple.product-type.bundle.unit-test";
+      else if (cmtarget.IsCFBundleOnApple())
         return "com.apple.product-type.bundle";
       else
         return ((this->XcodeVersion >= 22)?
diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx
index f0957d0..2b6c343 100644
--- a/Source/cmTarget.cxx
+++ b/Source/cmTarget.cxx
@@ -628,6 +628,15 @@ bool cmTarget::IsCFBundleOnApple() const
 }
 
 //----------------------------------------------------------------------------
+bool cmTarget::IsXCTestOnApple() const
+{
+  return (this->GetType() == cmTarget::MODULE_LIBRARY &&
+          this->Makefile->IsOn("APPLE") &&
+          this->GetPropertyAsBool("BUNDLE") &&
+          this->GetPropertyAsBool("XCTEST"));
+}
+
+//----------------------------------------------------------------------------
 bool cmTarget::IsBundleOnApple() const
 {
   return this->IsFrameworkOnApple() || this->IsAppBundleOnApple() ||
diff --git a/Source/cmTarget.h b/Source/cmTarget.h
index a3ecca0..5786d78 100644
--- a/Source/cmTarget.h
+++ b/Source/cmTarget.h
@@ -526,6 +526,9 @@ public:
   /** Return whether this target is a CFBundle (plugin) on Apple.  */
   bool IsCFBundleOnApple() const;
 
+  /** Return whether this target is a XCTest on Apple.  */
+  bool IsXCTestOnApple() const;
+
   /** Return whether this target is an executable Bundle on Apple.  */
   bool IsAppBundleOnApple() const;
 
-- 
1.9.3 (Apple Git-50)

