]> git.tdb.fi Git - builder.git/commitdiff
Make VirtualFileSystem able to find binaries
authorMikko Rasa <tdb@tdb.fi>
Sun, 8 Jul 2012 20:17:56 +0000 (23:17 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 8 Jul 2012 21:08:55 +0000 (00:08 +0300)
source/virtualfilesystem.cpp
source/virtualfilesystem.h

index 0dc8de604ffd4253e4ae51e4920ef4c95dd09d0a..cccc16945f6c74db42750a0c80e4eaac32000bf7 100644 (file)
@@ -1,9 +1,11 @@
+#include <cstdlib>
 #include <msp/fs/stat.h>
 #include <msp/fs/utils.h>
 #include <msp/io/print.h>
 #include <msp/strings/utils.h>
 #include "builder.h"
 #include "csourcefile.h"
+#include "executable.h"
 #include "misc.h"
 #include "sharedlibrary.h"
 #include "staticlibrary.h"
@@ -129,6 +131,41 @@ FileTarget *VirtualFileSystem::find_library(const string &lib, const SearchPath
        return 0;
 }
 
+FileTarget *VirtualFileSystem::find_binary(const string &name)
+{
+       SearchPath path;
+       if(const char *env_path = getenv("PATH"))
+       {
+               vector<string> parts = split(env_path, ':');
+               for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
+                       path.push_back(*i);
+       }
+       else
+       {
+               path.push_back("/bin");
+               path.push_back("/usr/bin");
+       }
+
+       for(SearchPath::const_iterator i=path.begin(); i!=path.end(); ++i)
+       {
+               FS::Path filename = *i/name;
+               if(FileTarget *tgt = get_target(filename))
+               {
+                       builder.get_logger().log("vfs", format("Binary %s found in %s as existing %s", name, *i, tgt->get_type()));
+                       return tgt;
+               }
+               else if(file_exists(filename))
+               {
+                       builder.get_logger().log("vfs", format("Binary %s found in %s", name, *i));
+                       return new Executable(builder, filename);
+               }
+
+               builder.get_logger().log("vfs", format("Binary %s not found in %s", name, *i));
+       }
+
+       return 0;
+}
+
 bool VirtualFileSystem::file_exists(const FS::Path &filename)
 {
        if(nonexistent.count(filename))
index aef6ab09392a6cafc83a1f68d58abb295a6f709b..a2ce05c4dad880c7f1bfea7c790a4d72c67a3256 100644 (file)
@@ -47,6 +47,11 @@ public:
        created. */
        FileTarget *find_library(const std::string &, const SearchPath &, LibMode);
 
+       /** Locates a binary.  The normal search path for binaries is used (usually
+       this means the PATH environment variable).  If a file is found but no target
+       is associated with it, a new package-less Executable target is created. */
+       FileTarget *find_binary(const std::string &);
+
 private:
        bool file_exists(const Msp::FS::Path &);
 };