From caf295d201f5c6a5133b3f86b15c69a998abfe3c Mon Sep 17 00:00:00 2001
From: Sam Spilsbury <smspillaz@gmail.com>
Date: Fri, 6 Jun 2014 21:51:38 +0800
Subject: [PATCH 1/2] cmNinjaTargetGenerator: Create files to satisfy
 pending-creation imported libs

Ninja evaluates all dependencies before building any targets unlike other
generators, so if a file forming part of a dependency rule does not exist
then this is a fatal error for Ninja. These files might be created as
part of the build process (and not custom commands) so if the user
imports a library target with add_library (... IMPORTED) and the path
to that target is within the build directory, then just touch the file
and assume it will be created later.

Added ImportedExternalProjectLibrary, which builds a library in a sister
subdirectory to an executable as an external project, the alternate
sister subdirectory adding the library as an IMPORTED library
depending on the external project and linking to it.

Fixes #13574.
---
 Source/cmNinjaTargetGenerator.cxx                  | 37 ++++++++++++++++++++++
 Tests/CMakeLists.txt                               |  1 +
 .../ImportedExternalProjectLibrary/CMakeLists.txt  |  5 +++
 .../CMakeLists.txt                                 |  3 ++
 .../ImportedExternalProjectLibrary.c               |  6 ++++
 .../LibrarySubdir/CMakeLists.txt                   | 17 ++++++++++
 .../ProjectWithImportedLibrary/CMakeLists.txt      |  4 +++
 .../ProjectWithImportedLibrary/library.c           |  4 +++
 8 files changed, 77 insertions(+)
 create mode 100644 Tests/ImportedExternalProjectLibrary/CMakeLists.txt
 create mode 100644 Tests/ImportedExternalProjectLibrary/ImportedExternalProjectLibraryDir/CMakeLists.txt
 create mode 100644 Tests/ImportedExternalProjectLibrary/ImportedExternalProjectLibraryDir/ImportedExternalProjectLibrary.c
 create mode 100644 Tests/ImportedExternalProjectLibrary/LibrarySubdir/CMakeLists.txt
 create mode 100644 Tests/ImportedExternalProjectLibrary/LibrarySubdir/ProjectWithImportedLibrary/CMakeLists.txt
 create mode 100644 Tests/ImportedExternalProjectLibrary/LibrarySubdir/ProjectWithImportedLibrary/library.c

diff --git a/Source/cmNinjaTargetGenerator.cxx b/Source/cmNinjaTargetGenerator.cxx
index c3b4c75..e5b9646 100644
--- a/Source/cmNinjaTargetGenerator.cxx
+++ b/Source/cmNinjaTargetGenerator.cxx
@@ -248,6 +248,15 @@ ComputeDefines(cmSourceFile const* source, const std::string& language)
   return definesString;
 }
 
+namespace
+{
+  // Assumes that absPath and dir are canonical and absolute
+  bool PathIsInDirectory(const std::string &absPath, const std::string &dir)
+  {
+      return dir.compare(0, dir.size(), absPath);
+  }
+}
+
 cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps() const
 {
   // Static libraries never depend on other targets for linking.
@@ -264,6 +273,34 @@ cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps() const
   cmNinjaDeps result(deps.size());
   std::transform(deps.begin(), deps.end(), result.begin(), MapToNinjaPath());
 
+  const std::string &buildDir (this->GetMakefile()->GetHomeOutputDirectory());
+  for (cmNinjaDeps::const_iterator it = result.begin();
+       it != result.end(); ++it)
+    {
+    const std::string &dep (*it);
+    // For link dependencies, the file needs to exist. If it doesn't
+    // then create it, but only if it would be inside the binary
+    // directory
+    if (!cmSystemTools::FileExists(dep.c_str()))
+      {
+      if (!PathIsInDirectory(dep, buildDir))
+        {
+        std::stringstream ss;
+        ss << "Imported library "
+           << dep
+           << " is outside CMAKE_CURRENT_BINARY_DIR and does not "
+           << "exist."
+           << std::endl;
+        std::string errorMessage (ss.str());
+        cmSystemTools::Error(errorMessage.c_str());
+        }
+
+      std::string dependencyPath (cmSystemTools::GetFilenamePath(dep));
+      cmSystemTools::MakeDirectory(dependencyPath.c_str());
+      cmSystemTools::Touch(dep.c_str(), true);
+      }
+    }
+
   // Add a dependency on the link definitions file, if any.
   if(!this->ModuleDefinitionFile.empty())
     {
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt
index 8d2b7fc..8c3c49d 100644
--- a/Tests/CMakeLists.txt
+++ b/Tests/CMakeLists.txt
@@ -281,6 +281,7 @@ if(BUILD_TESTING)
   ADD_TEST_MACRO(AliasTarget AliasTarget)
   ADD_TEST_MACRO(StagingPrefix StagingPrefix)
   ADD_TEST_MACRO(InterfaceLibrary InterfaceLibrary)
+  ADD_TEST_MACRO(ImportedExternalProjectLibrary ImportedExternalProjectLibrary)
   if (CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]")
     set(ConfigSources_BUILD_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
     ADD_TEST_MACRO(ConfigSources ConfigSources)
diff --git a/Tests/ImportedExternalProjectLibrary/CMakeLists.txt b/Tests/ImportedExternalProjectLibrary/CMakeLists.txt
new file mode 100644
index 0000000..e1976b3
--- /dev/null
+++ b/Tests/ImportedExternalProjectLibrary/CMakeLists.txt
@@ -0,0 +1,5 @@
+cmake_minimum_required(VERSION 2.8)
+project(ImportExternalProjectLibraryTest)
+
+add_subdirectory(LibrarySubdir)
+add_subdirectory(ImportedExternalProjectLibraryDir)
\ No newline at end of file
diff --git a/Tests/ImportedExternalProjectLibrary/ImportedExternalProjectLibraryDir/CMakeLists.txt b/Tests/ImportedExternalProjectLibrary/ImportedExternalProjectLibraryDir/CMakeLists.txt
new file mode 100644
index 0000000..68ff241
--- /dev/null
+++ b/Tests/ImportedExternalProjectLibrary/ImportedExternalProjectLibraryDir/CMakeLists.txt
@@ -0,0 +1,3 @@
+add_executable(ImportedExternalProjectLibrary
+               ${CMAKE_CURRENT_SOURCE_DIR}/ImportedExternalProjectLibrary.c)
+target_link_libraries(ImportedExternalProjectLibrary imported_library)
diff --git a/Tests/ImportedExternalProjectLibrary/ImportedExternalProjectLibraryDir/ImportedExternalProjectLibrary.c b/Tests/ImportedExternalProjectLibrary/ImportedExternalProjectLibraryDir/ImportedExternalProjectLibrary.c
new file mode 100644
index 0000000..6ee5800
--- /dev/null
+++ b/Tests/ImportedExternalProjectLibrary/ImportedExternalProjectLibraryDir/ImportedExternalProjectLibrary.c
@@ -0,0 +1,6 @@
+extern int library_func(void);
+
+int main(void)
+{
+    return library_func();
+}
diff --git a/Tests/ImportedExternalProjectLibrary/LibrarySubdir/CMakeLists.txt b/Tests/ImportedExternalProjectLibrary/LibrarySubdir/CMakeLists.txt
new file mode 100644
index 0000000..47bedfe
--- /dev/null
+++ b/Tests/ImportedExternalProjectLibrary/LibrarySubdir/CMakeLists.txt
@@ -0,0 +1,17 @@
+include(ExternalProject)
+
+set (PROJECT_WITH_IMPORTED_LIBRARY_SOURCE_DIR
+     ${CMAKE_CURRENT_SOURCE_DIR}/ProjectWithImportedLibrary)
+set (PROJECT_WITH_IMPORTED_LIBRARY_BINARY_DIR
+     ${CMAKE_CURRENT_BINARY_DIR}/ProjectWithImportedLibrary)
+
+ExternalProject_Add(ProjectWithImportedLibrary
+                    URL ${PROJECT_WITH_IMPORTED_LIBRARY_SOURCE_DIR}
+                    BINARY_DIR ${PROJECT_WITH_IMPORTED_LIBRARY_BINARY_DIR}
+                    INSTALL_COMMAND "")
+
+add_library(imported_library STATIC IMPORTED GLOBAL)
+set_target_properties(imported_library
+                      PROPERTIES IMPORTED_LOCATION
+                      ${PROJECT_WITH_IMPORTED_LIBRARY_BINARY_DIR}/liblibrary.a)
+add_dependencies(imported_library ProjectWithImportedLibrary)
diff --git a/Tests/ImportedExternalProjectLibrary/LibrarySubdir/ProjectWithImportedLibrary/CMakeLists.txt b/Tests/ImportedExternalProjectLibrary/LibrarySubdir/ProjectWithImportedLibrary/CMakeLists.txt
new file mode 100644
index 0000000..8c9802a
--- /dev/null
+++ b/Tests/ImportedExternalProjectLibrary/LibrarySubdir/ProjectWithImportedLibrary/CMakeLists.txt
@@ -0,0 +1,4 @@
+cmake_minimum_required(VERSION 2.8)
+project(ImportedLibrary)
+
+add_library(library STATIC ${CMAKE_CURRENT_SOURCE_DIR}/library.c)
diff --git a/Tests/ImportedExternalProjectLibrary/LibrarySubdir/ProjectWithImportedLibrary/library.c b/Tests/ImportedExternalProjectLibrary/LibrarySubdir/ProjectWithImportedLibrary/library.c
new file mode 100644
index 0000000..db375b4
--- /dev/null
+++ b/Tests/ImportedExternalProjectLibrary/LibrarySubdir/ProjectWithImportedLibrary/library.c
@@ -0,0 +1,4 @@
+int library_func(void)
+{
+    return 0;
+}
-- 
1.8.5.2 (Apple Git-48)

