Re-organize the Remakefile.
[tatoo.git] / remake.cpp
index 5d8dc3d..23a44f8 100644 (file)
@@ -676,9 +676,31 @@ static std::string escape_string(std::string const &s)
 /**
  * Initialize #working_dir.
  */
-void init_working_dir()
+void init_working_dir(const char* argv0)
 {
        char buf[1024];
+#ifdef WINDOWS
+       char const *delim = "/\\";
+#else
+       char delim = '/';
+#endif
+        if (!getenv("REMAKE_SOCKET"))
+        {
+          std::string path = argv0;
+          
+          size_t found = path.find_last_of(delim);
+          if (found != std::string::npos)
+          {
+            path = path.substr(0, found);
+            std::cout << "Entering directory `" << path << "'" << std::endl;
+            if (chdir(path.c_str()))
+            {
+              perror("Failed to change working directory");
+            exit(EXIT_FAILURE);
+            }
+          }
+        }
+
        char *res = getcwd(buf, sizeof(buf));
        if (!res)
        {
@@ -2230,7 +2252,7 @@ void usage(int exit_status)
  */
 int main(int argc, char *argv[])
 {
-       init_working_dir();
+       init_working_dir(argv[0]);
 
        string_list targets;
        bool indirect_targets = false;