]> git.tdb.fi Git - builder.git/commitdiff
Handle directory creation and unlinking in Task
authorMikko Rasa <tdb@tdb.fi>
Sat, 21 Jul 2012 13:49:12 +0000 (16:49 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 21 Jul 2012 13:49:12 +0000 (16:49 +0300)
source/externaltask.cpp
source/filetarget.cpp
source/gnuarchiver.cpp
source/gnucompiler.cpp
source/gnulinker.cpp
source/internaltask.cpp
source/task.cpp [new file with mode: 0644]
source/task.h

index 5f5d394597b3a7d073d80bc2caa830172ba97d92..643831e993b9b7e8a8d4c2b65f11245c612bba18 100644 (file)
@@ -50,6 +50,8 @@ void ExternalTask::start()
        if(stdout_dest==CAPTURE || stderr_dest==CAPTURE)
                capture_pipe = new IO::Pipe;
 
+       prepare();
+
        if((pid = fork()))
        {
                if(pid==-1)
index 3d8014755de2b90dd2c81ff528f2e91029ca5278..fca8cea51f651d5409f9da90b8e893acd4738901 100644 (file)
@@ -6,6 +6,7 @@
 #include "builder.h"
 #include "filetarget.h"
 #include "sourcepackage.h"
+#include "task.h"
 #include "tool.h"
 
 using namespace std;
@@ -110,10 +111,10 @@ string FileTarget::create_build_signature() const
 
 Task *FileTarget::build()
 {
-       if(tool && !builder.get_dry_run() && mtime)
-               FS::unlink(path);
-
-       return Target::build();
+       Task *task = Target::build();
+       task->set_file(path);
+       task->set_unlink(true);
+       return task;
 }
 
 void FileTarget::build_finished(bool success)
index 168a163449e9814e234bf4f1ee1187cc7a0be61a..3499204954593db5587280e5d1679b514f2e2215 100644 (file)
@@ -63,12 +63,5 @@ Task *GnuArchiver::run(const Target &target) const
                if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
                        argv.push_back(relative(obj->get_path(), work_dir).str());
 
-       if(!builder.get_dry_run())
-       {
-               FS::mkpath(FS::dirname(lib.get_path()), 0755);
-               if(FS::exists(lib.get_path()))
-                       FS::unlink(lib.get_path());
-       }
-
        return new ExternalTask(argv, work_dir);
 }
index a73fad72765d65be8d88730e25b3570d5925cb8d..43e8e300ad08132bb8cf5ce72d4d254fdd463cd8 100644 (file)
@@ -136,8 +136,5 @@ Task *GnuCompiler::run(const Target &target) const
        argv.push_back(relative(obj_path, work_dir).str());
        argv.push_back(relative(src_path, work_dir).str());
 
-       if(!builder.get_dry_run())
-               FS::mkpath(FS::dirname(obj_path), 0755);
-
        return new ExternalTask(argv, work_dir);
 }
index 6ea88718fa4d8c3af4f3c2d91863eabefbe74531..7c3f872acdb84a668ad4b4c339dd6c826a829536 100644 (file)
@@ -176,8 +176,5 @@ Task *GnuLinker::Linker::run(const Target &target) const
        if(static_link_ok)
                argv.push_back("-static");
 
-       if(!builder.get_dry_run())
-               FS::mkpath(FS::dirname(bin.get_path()), 0755);
-
        return new ExternalTask(argv, work_dir);
 }
index bcea59156be3b6ae9b70a2dad96a72bf6bc31ab7..aa10649dc148c62efd07e798926e31425ab6c956 100644 (file)
@@ -12,6 +12,7 @@ InternalTask::~InternalTask()
 
 void InternalTask::start()
 {
+       prepare();
        worker->launch();
 }
 
diff --git a/source/task.cpp b/source/task.cpp
new file mode 100644 (file)
index 0000000..f6fa7b2
--- /dev/null
@@ -0,0 +1,38 @@
+#include <msp/fs/dir.h>
+#include <msp/fs/stat.h>
+#include <msp/fs/utils.h>
+#include "task.h"
+
+using namespace Msp;
+
+Task::Task():
+       unlink(false)
+{ }
+
+void Task::set_file(const FS::Path &f)
+{
+       file = f;
+}
+
+void Task::set_unlink(bool u)
+{
+       unlink = u;
+}
+
+void Task::prepare()
+{
+       if(!file.empty())
+       {
+               if(FS::exists(file))
+               {
+                       // If the file exists, the directory it's in must exist too
+                       FS::unlink(file);
+               }
+               else
+               {
+                       FS::Path dir = FS::dirname(file);
+                       if(!FS::exists(dir))
+                               FS::mkpath(dir, 0755);
+               }
+       }
+}
index e7e37e3f04423d525afdf44e00a2e9c854c5c4b1..0732453bfd460b0f0fe8ae881899c6d33581d89a 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <string>
 #include <sigc++/signal.h>
+#include <msp/fs/path.h>
 
 /**
 Tasks are used to manage other programs and worker threads involved in the
@@ -21,11 +22,21 @@ public:
 
        sigc::signal<void, bool> signal_finished;
 
+       Msp::FS::Path file;
+       bool unlink;
+
 protected:
-       Task() { }
+       Task();
 public:
        virtual ~Task() { }
 
+       /** Associate the task with a file. */
+       void set_file(const Msp::FS::Path &);
+
+       /** If set to true, the associated file is removed before the task is
+       started. */
+       void set_unlink(bool = true);
+
        /** Returns the command being executed for this task.  Only makes sense if
        an external command is involved. */
        virtual std::string get_command() const = 0;
@@ -33,6 +44,12 @@ public:
        /// Starts the task.
        virtual void start() = 0;
 
+protected:
+       /** Ensures that the output directory exists and removes the file if
+       necessary. */
+       void prepare();
+
+public:
        /// Checks the status of the task and immediately returns.
        virtual Status check() = 0;