From 763fe7da0c0f92afbf7df1969ea5857140c02425 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Johan=20Bj=C3=B6rk?= <phb@spotify.com>
Date: Sun, 20 Feb 2011 15:01:03 +0100
Subject: [PATCH 1/3] Patch from Nick

---
 Source/cmGlobalXCodeGenerator.cxx |  278 ++++++++++++++++++++++++++-----------
 Source/cmGlobalXCodeGenerator.h   |   11 ++-
 2 files changed, 206 insertions(+), 83 deletions(-)

diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx
index 2a1e7d3..2b92005 100644
--- a/Source/cmGlobalXCodeGenerator.cxx
+++ b/Source/cmGlobalXCodeGenerator.cxx
@@ -343,23 +343,6 @@ cmGlobalXCodeGenerator::AddExtraTargets(cmLocalGenerator* root,
   listfile += "CMakeLists.txt";
   allbuild->AddSource(listfile.c_str());
 
-  // Add XCODE depend helper
-  std::string dir = mf->GetCurrentOutputDirectory();
-  cmCustomCommandLine makecommand;
-  makecommand.push_back("make");
-  makecommand.push_back("-C");
-  makecommand.push_back(dir.c_str());
-  makecommand.push_back("-f");
-  makecommand.push_back(this->CurrentXCodeHackMakefile.c_str());
-  if(this->XcodeVersion > 20)
-    {
-    makecommand.push_back("all.$(CONFIGURATION)");
-    }
-  cmCustomCommandLines commandLines;
-  commandLines.push_back(makecommand);
-  // Add Re-Run CMake rules
-  this->CreateReRunCMakeFile(root, gens);
-
   // now make the allbuild depend on all the non-utility targets
   // in the project
   for(std::vector<cmLocalGenerator*>::iterator i = gens.begin();
@@ -374,23 +357,6 @@ cmGlobalXCodeGenerator::AddExtraTargets(cmLocalGenerator* root,
     for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
       {
       cmTarget& target = l->second;
-      // make all exe, shared libs and modules
-      // run the depend check makefile as a post build rule
-      // this will make sure that when the next target is built
-      // things are up-to-date
-      if((target.GetType() == cmTarget::EXECUTABLE ||
-          target.GetType() == cmTarget::STATIC_LIBRARY ||
-          target.GetType() == cmTarget::SHARED_LIBRARY ||
-          target.GetType() == cmTarget::MODULE_LIBRARY))
-        {
-        lg->GetMakefile()->AddCustomCommandToTarget(target.GetName(),
-                                                    no_depends,
-                                                    commandLines,
-                                                    cmTarget::POST_BUILD,
-                                                    "Depend check for xcode",
-                                                    dir.c_str());
-
-        }
       if(!target.GetPropertyAsBool("EXCLUDE_FROM_ALL"))
         {
         allbuild->AddUtility(target.GetName());
@@ -956,25 +922,21 @@ cmGlobalXCodeGenerator::CreateXCodeTargets(cmLocalGenerator* gen,
         }
       }
 
-    // create framework build phase
-    cmXCodeObject* frameworkBuildPhase = 0;
-    if (!externalObjFiles.empty())
-      {
-      frameworkBuildPhase =
+    // always create framework build phase, libraries may be added later
+    cmXCodeObject* frameworkBuildPhase = 
         this->CreateObject(cmXCodeObject::PBXFrameworksBuildPhase);
-      frameworkBuildPhase->SetComment("Frameworks");
-      frameworkBuildPhase->AddAttribute("buildActionMask",
-                                        this->CreateString("2147483647"));
-      buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
-      frameworkBuildPhase->AddAttribute("files", buildFiles);
-      for(std::vector<cmXCodeObject*>::iterator i =  externalObjFiles.begin();
-          i != externalObjFiles.end(); ++i)
-        {
-        buildFiles->AddObject(*i);
-        }
-      frameworkBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
-                                        this->CreateString("0"));
+    frameworkBuildPhase->SetComment("Frameworks");
+    frameworkBuildPhase->AddAttribute("buildActionMask",
+                                      this->CreateString("2147483647"));
+    buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
+    frameworkBuildPhase->AddAttribute("files", buildFiles);
+    for(std::vector<cmXCodeObject*>::iterator i =  externalObjFiles.begin();
+        i != externalObjFiles.end(); ++i)
+      {
+      buildFiles->AddObject(*i);
       }
+    frameworkBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
+                                      this->CreateString("0"));
 
     // create list of build phases and create the XCode target
     cmXCodeObject* buildPhases =
@@ -1056,6 +1018,25 @@ bool cmGlobalXCodeGenerator::IsHeaderFile(cmSourceFile* sf)
 
 //----------------------------------------------------------------------------
 cmXCodeObject*
+cmGlobalXCodeGenerator::CreateCopyFilePhase(const char* name, 
+                                            const char* dir)
+{
+  cmXCodeObject* buildPhase =
+    this->CreateObject(cmXCodeObject::PBXCopyFilesBuildPhase);
+  buildPhase->AddAttribute("buildActionMask",
+                           this->CreateString("2147483647"));
+  buildPhase->AddAttribute("dstPath",this->CreateString(dir));
+  buildPhase->AddAttribute("dstSubfolderSpec", this->CreateString("0"));
+  cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
+  buildPhase->AddAttribute("files", buildFiles);
+  buildPhase->AddAttribute("name", this->CreateString(name));
+  buildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
+                           this->CreateString("0"));
+  return buildPhase;
+}
+
+//----------------------------------------------------------------------------
+cmXCodeObject*
 cmGlobalXCodeGenerator::CreateBuildPhase(const char* name,
                                          const char* name2,
                                          cmTarget& cmtarget,
@@ -1101,8 +1082,6 @@ void cmGlobalXCodeGenerator::CreateCustomCommands(cmXCodeObject* buildPhases,
     = cmtarget.GetPreBuildCommands();
   std::vector<cmCustomCommand> const & prelink
     = cmtarget.GetPreLinkCommands();
-  std::vector<cmCustomCommand> const & postbuild
-    = cmtarget.GetPostBuildCommands();
   std::vector<cmSourceFile*>const &classes = cmtarget.GetSourceFiles();
   // add all the sources
   std::vector<cmCustomCommand> commands;
@@ -1114,16 +1093,13 @@ void cmGlobalXCodeGenerator::CreateCustomCommands(cmXCodeObject* buildPhases,
       commands.push_back(*(*i)->GetCustomCommand());
       }
     }
-  std::vector<cmCustomCommand> reruncom;
-  cmXCodeObject* cmakeReRunPhase =
-    this->CreateBuildPhase("CMake ReRun", "cmakeReRunPhase",
-                           cmtarget, reruncom);
-  buildPhases->AddObject(cmakeReRunPhase);
+
   // create prebuild phase
   cmXCodeObject* cmakeRulesBuildPhase =
     this->CreateBuildPhase("CMake Rules",
                            "cmakeRulesBuildPhase",
                            cmtarget, commands);
+
   // create prebuild phase
   cmXCodeObject* preBuildPhase =
     this->CreateBuildPhase("CMake PreBuild Rules", "preBuildCommands",
@@ -1132,11 +1108,30 @@ void cmGlobalXCodeGenerator::CreateCustomCommands(cmXCodeObject* buildPhases,
   cmXCodeObject* preLinkPhase =
     this->CreateBuildPhase("CMake PreLink Rules", "preLinkCommands",
                            cmtarget, prelink);
-  // create postbuild phase
-  cmXCodeObject* postBuildPhase =
-    this->CreateBuildPhase("CMake PostBuild Rules", "postBuildPhase",
-                           cmtarget, postbuild);
 
+  // create copy file phase for main executable to copy resulting
+  // binary from native Xcode location to where cmake wants it
+  cmXCodeObject* copyBuildPhase = NULL;
+  if(cmtarget.GetType() == cmTarget::EXECUTABLE)
+    {
+    const char* outdir = NULL;
+    std::string fulldir;
+    // follow rules on how final location can be specified
+    const char* rtdir = 
+        this->CurrentMakefile->GetDefinition("CMAKE_RUNTIME_OUTPUT_DIRECTORY");
+    if (rtdir && (strlen(rtdir)!=0))
+      {
+      fulldir = rtdir;
+      fulldir += "/$(CONFIGURATION)/";
+      outdir = fulldir.c_str();
+      }
+    if (!outdir || (strlen(outdir)==0))
+      outdir = this->CurrentMakefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
+    if (!outdir || (strlen(outdir)==0))
+      outdir = this->CurrentMakefile->GetDefinition("PROJECT_BINARY_DIR");
+    copyBuildPhase = this->CreateCopyFilePhase("copy to cmake build dir", outdir);
+    }
+    
   // The order here is the order they will be built in.
   // The order "headers, resources, sources" mimics a native project generated
   // from an xcode template...
@@ -1175,9 +1170,9 @@ void cmGlobalXCodeGenerator::CreateCustomCommands(cmXCodeObject* buildPhases,
     {
     buildPhases->AddObject(frameworkBuildPhase);
     }
-  if(postBuildPhase)
+  if(copyBuildPhase)
     {
-    buildPhases->AddObject(postBuildPhase);
+    buildPhases->AddObject(copyBuildPhase);
     }
 }
 
@@ -1287,6 +1282,25 @@ cmGlobalXCodeGenerator::AddCommandsToBuildPhase(cmXCodeObject* buildphase,
 }
 
 //----------------------------------------------------------------------------
+std::string  cmGlobalXCodeGenerator
+::GetPathForUseInMakeFile(const char* basename, const char* configName)
+{
+  // check to see if this is the name of a target
+  cmTarget* cmtarget = this->CurrentLocalGenerator->GetMakefile()
+                                              ->FindTargetToUse(basename);
+  if (cmtarget)
+    {
+    std::string fullName = cmtarget->GetFullName(NULL);
+    std::string path =  "${BUILT_PRODUCTS_DIR}/" + fullName;
+    return path;
+    }
+  std::string dep;
+  this->CurrentLocalGenerator->GetRealDependency(basename,
+                                                    configName, dep);
+  return this->ConvertToRelativeForMake(dep.c_str());
+}
+
+//----------------------------------------------------------------------------
 void  cmGlobalXCodeGenerator
 ::CreateCustomRulesMakefile(const char* makefileBasename,
                             cmTarget& target,
@@ -1369,13 +1383,8 @@ void  cmGlobalXCodeGenerator
           cc.GetDepends().begin();
           d != cc.GetDepends().end(); ++d)
         {
-        std::string dep;
-        if(this->CurrentLocalGenerator
-           ->GetRealDependency(d->c_str(), configName, dep))
-          {
-          makefileStream << "\\\n" <<
-            this->ConvertToRelativeForMake(dep.c_str());
-          }
+        std::string dep = this->GetPathForUseInMakeFile(d->c_str(), configName);
+        makefileStream << "\\\n" << dep.c_str();
         }
       makefileStream << "\n";
 
@@ -1598,7 +1607,7 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
     if(this->XcodeVersion >= 21)
       {
       std::string pncdir = target.GetDirectory(configName);
-      buildSettings->AddAttribute("CONFIGURATION_BUILD_DIR",
+      buildSettings->AddAttribute("INSTALL_DIR",
                                   this->CreateString(pncdir.c_str()));
       }
     else
@@ -1612,8 +1621,6 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
     buildSettings->AddAttribute("EXECUTABLE_SUFFIX",
                                 this->CreateString(pnsuffix.c_str()));
     }
-  buildSettings->AddAttribute("SYMROOT",
-                              this->CreateString(pndir.c_str()));
 
   // Handle settings for each target type.
   switch(target.GetType())
@@ -2178,6 +2185,8 @@ cmGlobalXCodeGenerator::CreateXCodeTarget(cmTarget& cmtarget,
   fileRef->SetComment(cmtarget.GetName());
   target->AddAttribute("productReference",
                        this->CreateObjectReference(fileRef));
+  // add mapping of target to fileref for later use when adding link libraries
+  LibFileRefs[&cmtarget] = fileRef;
   if(const char* productType = this->GetTargetProductType(cmtarget))
     {
     target->AddAttribute("productType", this->CreateString(productType));
@@ -2349,6 +2358,83 @@ void cmGlobalXCodeGenerator
     }
 }
 
+
+//----------------------------------------------------------------------------
+cmXCodeObject* cmGlobalXCodeGenerator
+::FindFrameworksFilesObjectForTarget(cmXCodeObject* target)
+{
+  if (target->GetIsA() != cmXCodeObject::PBXNativeTarget)
+    return NULL;
+  cmXCodeObject* buildPhases = target->GetObject("buildPhases");
+  if (!buildPhases)
+    return NULL;
+  cmXCodeObject* libs = buildPhases->GetObject(
+                                      cmXCodeObject::PBXFrameworksBuildPhase);
+  if (!libs)
+    return NULL;
+  return libs->GetObject("files");
+}
+
+//----------------------------------------------------------------------------
+cmXCodeObject* cmGlobalXCodeGenerator
+::CopyFileFilesObjectForTarget(cmXCodeObject* target)
+{
+  if (target->GetIsA() != cmXCodeObject::PBXNativeTarget)
+    return NULL;
+  cmXCodeObject* buildPhases = target->GetObject("buildPhases");
+  if (!buildPhases)
+    return NULL;
+  cmXCodeObject* files = buildPhases->GetObject(
+                                      cmXCodeObject::PBXCopyFilesBuildPhase);
+  if (!files)
+    return NULL;
+  return files->GetObject("files");
+}
+
+//----------------------------------------------------------------------------
+cmXCodeObject* cmGlobalXCodeGenerator
+::AddBuildFile(cmXCodeObject* fileRef, std::string& name)
+{
+  cmXCodeObject* file = this->CreateObject(cmXCodeObject::PBXBuildFile);
+  std::string comment = name;
+  comment += " in Frameworks";
+  file->SetComment(comment.c_str());
+  file->AddAttribute("fileRef", this->CreateObjectReference(fileRef));
+  return file;
+}
+
+//----------------------------------------------------------------------------
+void cmGlobalXCodeGenerator
+::AddCopyFileTarget(cmXCodeObject* target)
+{
+  cmTarget* cmtarget = target->GetTarget();
+  if(!cmtarget)
+    {
+    cmSystemTools::Error("Error no target on xobject\n");
+    return;
+    }
+  if(cmtarget->GetType() == cmTarget::EXECUTABLE)
+    {
+    cmXCodeObject* files = this->CopyFileFilesObjectForTarget(target);
+    if(files)
+      {
+      cmXCodeObject* product = target->GetObject("productReference");
+      if(product)
+        {
+        cmXCodeObject* productRef = product->GetObject();
+        if(productRef)
+          {
+          cmXCodeObject* targFile 
+                           = this->CreateObject(cmXCodeObject::PBXBuildFile);
+          targFile->AddAttribute("fileRef", 
+                                  this->CreateObjectReference(productRef));
+          files->AddObject(targFile);
+          }
+        }
+      }
+    }
+}
+
 //----------------------------------------------------------------------------
 void cmGlobalXCodeGenerator
 ::AddDependAndLinkInformation(cmXCodeObject* target)
@@ -2364,12 +2450,41 @@ void cmGlobalXCodeGenerator
   TargetDependSet const& deps = this->GetTargetDirectDepends(*cmtarget);
   for(TargetDependSet::const_iterator i = deps.begin(); i != deps.end(); ++i)
     {
-    if(cmXCodeObject* dptarget = this->FindXCodeTarget(*i))
+    cmTarget* dpcmtarget = *i;
+    if(cmXCodeObject* dptarget = this->FindXCodeTarget(dpcmtarget))
       {
       this->AddDependTarget(target, dptarget);
       }
     }
-
+    
+  // Add cmake build dependent libraries to frameworks phase in link order
+  const cmTarget::LinkLibraryVectorType& depLibs = cmtarget->GetLinkLibraries();
+  for(cmTarget::LinkLibraryVectorType::const_iterator libit = depLibs.begin();
+                                          libit != depLibs.end(); ++libit)  
+    {
+    const std::string& libName = libit->first;
+    // see if lib name matches a dependent target name
+    for(TargetDependSet::const_iterator i = deps.begin(); i != deps.end(); ++i)
+      {
+      cmTarget* dpcmtarget = *i;
+      if ( strcmp(dpcmtarget->GetName(), libName.c_str()) == 0 )
+        {
+        // If dependent cmake targets are libraries, add to frameworks phase
+        cmXCodeObject* libFileRef = LibFileRefs[dpcmtarget];
+        if (libFileRef)
+          {
+          cmXCodeObject* fls = this->FindFrameworksFilesObjectForTarget(target);
+          if (fls)
+            {
+            std::string libFullName = dpcmtarget->GetFullName(NULL);
+            cmXCodeObject* bldFile = this->AddBuildFile(libFileRef, libFullName);
+            fls->AddObject(bldFile);
+            }
+          }
+        }
+      }
+    }
+   
   // Skip link information for static libraries.
   if(cmtarget->GetType() == cmTarget::STATIC_LIBRARY)
     {
@@ -2460,19 +2575,17 @@ void cmGlobalXCodeGenerator
       {
       linkLibs += sep;
       sep = " ";
-      if(li->IsPath)
-        {
-        linkLibs += this->XCodeEscapePath(li->Value.c_str());
-        }
-      else
+      if(li->Target == NULL)
         {
+        // No target means this is a linker option or some library
+        // not produced by this project.
         linkLibs += li->Value;
         }
       }
     this->AppendBuildSettingAttribute(target, "OTHER_LDFLAGS",
                                       linkLibs.c_str(), configName);
     }
-    }
+  }
 }
 
 //----------------------------------------------------------------------------
@@ -2932,6 +3045,7 @@ void cmGlobalXCodeGenerator
     {
     cmXCodeObject* t = *i;
     this->AddDependAndLinkInformation(t);
+    this->AddCopyFileTarget(t);
     }
   // now create xcode depend hack makefile
   this->CreateXCodeDependHackTarget(targets);
diff --git a/Source/cmGlobalXCodeGenerator.h b/Source/cmGlobalXCodeGenerator.h
index 10aa828..c57a4a7 100644
--- a/Source/cmGlobalXCodeGenerator.h
+++ b/Source/cmGlobalXCodeGenerator.h
@@ -121,7 +121,9 @@ private:
                                  const std::map<cmStdString, cmStdString>& 
                                      multipleOutputPairs
                                 );
-  
+ std::string GetPathForUseInMakeFile(const char* basename, 
+                                    const char* configName);
+ 
   cmXCodeObject* FindXCodeTarget(cmTarget*);
   std::string GetOrCreateId(const char* name, const char* id);
 
@@ -143,6 +145,10 @@ private:
   void AppendBuildSettingAttribute(cmXCodeObject* target, const char* attr, 
                                    const char* value, const char* configName);
   cmXCodeObject* CreateUtilityTarget(cmTarget& target);
+  cmXCodeObject* AddBuildFile(cmXCodeObject* fileRef, std::string& name);
+  cmXCodeObject* FindFrameworksFilesObjectForTarget(cmXCodeObject* target);
+  cmXCodeObject* CopyFileFilesObjectForTarget(cmXCodeObject* target);
+  void AddCopyFileTarget(cmXCodeObject* target);
   void AddDependAndLinkInformation(cmXCodeObject* target);
   void CreateBuildSettings(cmTarget& target,
                            cmXCodeObject* buildSettings,
@@ -161,6 +167,7 @@ private:
   cmXCodeObject* CreateXCodeSourceFile(cmLocalGenerator* gen, 
                                        cmSourceFile* sf,
                                        cmTarget& cmtarget);
+  cmXCodeObject* CreateXCodeStaticLibraryFileReference(std::string const& lib);
   void CreateXCodeTargets(cmLocalGenerator* gen, 
                           std::vector<cmXCodeObject*>&);
   bool IsHeaderFile(cmSourceFile*);
@@ -175,6 +182,7 @@ private:
                                   const char* name2,
                                   cmTarget& cmtarget,
                                   const std::vector<cmCustomCommand>&);
+  cmXCodeObject* CreateCopyFilePhase(const char* name, const char* dir);
   void CreateReRunCMakeFile(cmLocalGenerator* root,
                             std::vector<cmLocalGenerator*> const& gens);
 
@@ -221,6 +229,7 @@ private:
   std::map<cmStdString, cmXCodeObject* > GroupNameMap;
   std::map<cmStdString, cmXCodeObject* > TargetGroup;
   std::map<cmStdString, cmXCodeObject* > FileRefs;
+  std::map<cmTarget*, cmXCodeObject* > LibFileRefs;
   std::vector<std::string> Architectures;
 };
 
-- 
1.7.2.1

