Make remake print "Entering/Leaving directory `foo'" if it changes the current workin...
authorKim Nguyễn <kn@lri.fr>
Sat, 30 Nov 2013 08:25:47 +0000 (09:25 +0100)
committerKim Nguyễn <kn@lri.fr>
Sat, 30 Nov 2013 08:25:47 +0000 (09:25 +0100)
remake.cpp

index b8387be..b6c3c6b 100644 (file)
@@ -724,6 +724,12 @@ static std::string prefix_dir;
  */
 static bool propagate_vars = false;
 
+
+/**
+ * Whether to print that we changed working directory (to be nice with emacs)
+ */
+static bool print_change_working_dir = false;
+
 #ifndef WINDOWS
 static volatile sig_atomic_t got_SIGCHLD = 0;
 
@@ -865,13 +871,28 @@ static void init_working_dir()
  */
 static void init_prefix_dir()
 {
+       std::string old_prefix_dir = prefix_dir;
        for (;;)
        {
                struct stat s;
                if (stat((prefix_dir + "/Remakefile").c_str(), &s) == 0)
                {
-                       chdir(prefix_dir.c_str());
-                       return;
+                       if (0 == chdir(prefix_dir.c_str()))
+                       {
+                               if (old_prefix_dir != prefix_dir)
+                               {
+                                       std::cout << "remake: Entering directory `"
+                                                 << prefix_dir << "'"
+                                                 << std::endl;
+                                       print_change_working_dir = true;
+                               }
+                               return;
+                       }
+                       else
+                       {
+                               std::cerr << "Cannot change working directory to '" << prefix_dir << "'";
+                               exit(EXIT_FAILURE);
+                       }
                }
                size_t pos = prefix_dir.find_last_of('/');
                if (pos == std::string::npos)
@@ -2792,6 +2813,9 @@ static void server_mode(std::string const &remakefile, string_list const &target
        free(socket_name);
 #endif
        save_dependencies();
+       if (print_change_working_dir)
+               std::cout << "remake: Leaving directory `" << prefix_dir 
+                         << "'" <<std::endl;
        exit(build_failure ? EXIT_FAILURE : EXIT_SUCCESS);
 }