Browse Source

ENH: Improved behavior when run with arguments from the command line.

Brad King 22 years ago
parent
commit
6849cbdfcb
2 changed files with 73 additions and 36 deletions
  1. 70 34
      Source/MFCDialog/CMakeSetupDialog.cpp
  2. 3 2
      Source/MFCDialog/CMakeSetupDialog.h

+ 70 - 34
Source/MFCDialog/CMakeSetupDialog.cpp

@@ -1343,51 +1343,87 @@ void CMakeSetupDialog::OnDoubleclickedAdvancedValues()
 }
 
 // Handle param or single dropped file.
-// If the dropped file is a build directory or any file in a 
-// build directory, set the source dir from the cache file,
-// otherwise set the source and build dirs to this file (or dir).
-
-void CMakeSetupDialog::ChangeDirectoriesFromFile(const char* buffer)
+void CMakeSetupDialog::ChangeDirectoriesFromFile(const char* arg)
 {
-  // Get the path to this file
-
-  std::string path = buffer;
-  if (!cmSystemTools::FileIsDirectory(path.c_str()))
+  // Check if the argument refers to a CMakeCache.txt or
+  // CMakeLists.txt file.
+  std::string listPath;
+  std::string cachePath;
+  bool argIsFile = false;
+  if(cmSystemTools::FileIsDirectory(arg))
     {
-    path = cmSystemTools::GetFilenamePath(path);
+    std::string path = cmSystemTools::CollapseFullPath(arg);
+    cmSystemTools::ConvertToUnixSlashes(path);
+    std::string cacheFile = path;
+    cacheFile += "/CMakeCache.txt";
+    std::string listFile = path;
+    listFile += "/CMakeLists.txt";
+    if(cmSystemTools::FileExists(cacheFile.c_str()))
+      {
+      cachePath = path;
+      }
+    if(cmSystemTools::FileExists(listFile.c_str()))
+      {
+      listPath = path;
+      }
     }
-  else
+  else if(cmSystemTools::FileExists(arg))
     {
-    cmSystemTools::ConvertToUnixSlashes(path);
+    argIsFile = true;
+    std::string fullPath = cmSystemTools::CollapseFullPath(arg);
+    std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
+    name = cmSystemTools::LowerCase(name);
+    if(name == "cmakecache.txt")
+      {
+      cachePath = cmSystemTools::GetFilenamePath(fullPath.c_str());
+      }
+    else if(name == "cmakelists.txt")
+      {
+      listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
+      }
     }
-
-  // Check if it's a build dir and grab the cache
-
-  std::string cache_file = path;
-  cache_file += "/CMakeCache.txt";
-
-  cmCacheManager *cachem = this->m_CMakeInstance->GetCacheManager();
-
-  cmCacheManager::CacheIterator it = 
-    cachem->NewIterator();
-
-  if (cmSystemTools::FileExists(cache_file.c_str()) &&
-      cachem->LoadCache(path.c_str()) &&
-      it.Find("CMAKE_HOME_DIRECTORY"))
+  
+  // If there is a CMakeCache.txt file, use its settings.
+  if(cachePath.length() > 0)
     {
-    path = cmSystemTools::ConvertToOutputPath(path.c_str());
-    this->m_WhereBuild = path.c_str();
-
-    path = cmSystemTools::ConvertToOutputPath(it.GetValue());
-    this->m_WhereSource = path.c_str();
+    cmCacheManager* cachem = m_CMakeInstance->GetCacheManager();
+    cmCacheManager::CacheIterator it = cachem->NewIterator();
+    if(cachem->LoadCache(cachePath.c_str()) && it.Find("CMAKE_HOME_DIRECTORY"))
+      {
+      std::string path = cmSystemTools::ConvertToOutputPath(cachePath.c_str());
+      m_WhereBuild = path.c_str();
+      
+      path = cmSystemTools::ConvertToOutputPath(it.GetValue());
+      m_WhereSource = path.c_str();
+      return;
+      }
     }
-  else
+  
+  // If there is a CMakeLists.txt file, use it as the source tree.
+  if(listPath.length() > 0)
     {
-    path = cmSystemTools::ConvertToOutputPath(path.c_str());
-    this->m_WhereSource = this->m_WhereBuild = path.c_str();
+    std::string path = cmSystemTools::ConvertToOutputPath(listPath.c_str());
+    m_WhereSource = path.c_str();
+    
+    if(argIsFile)
+      {
+      // Source CMakeLists.txt file given.  It was probably dropped
+      // onto the window or executable.  Default to an in-source
+      // build.
+      m_WhereBuild = path.c_str();
+      }
+    else
+      {
+      // Source directory given on command line.  Use current working
+      // directory as build tree.
+      std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
+      path = cmSystemTools::ConvertToOutputPath(cwd.c_str());
+      m_WhereBuild = path.c_str();
+      }
     }
 }
 
+
 // The framework calls this member function when the user releases the
 // left mouse button over a window that has registered itself as the 
 // recipient of dropped files. 

+ 3 - 2
Source/MFCDialog/CMakeSetupDialog.h

@@ -97,8 +97,9 @@ protected:
   void FillCacheManagerFromCacheGUI();
   // Create a shortcut on the desktop with the current Source/Build dir.
   int CreateShortcut();
-  // Handle param or single dropped file.
-  void ChangeDirectoriesFromFile(const char *file);
+  
+  // Set initial directories from a file path.
+  void ChangeDirectoriesFromFile(const char* arg);
   
   HICON m_hIcon;
   CString m_RegistryKey;