]> git.tdb.fi Git - builder.git/blobdiff - source/lib/csourcefile.cpp
Rearrange sources into subdirectories
[builder.git] / source / lib / csourcefile.cpp
diff --git a/source/lib/csourcefile.cpp b/source/lib/csourcefile.cpp
new file mode 100644 (file)
index 0000000..bc76605
--- /dev/null
@@ -0,0 +1,73 @@
+#include <msp/core/maputils.h>
+#include <msp/fs/utils.h>
+#include <msp/io/print.h>
+#include <msp/strings/regex.h>
+#include "builder.h"
+#include "component.h"
+#include "csourcefile.h"
+#include "sourcepackage.h"
+#include "tool.h"
+
+using namespace std;
+using namespace Msp;
+
+CSourceFile::CSourceFile(Builder &b, const Component &c, const FS::Path &p):
+       SourceFile(b, c, p)
+{
+       string ext = FS::extpart(FS::basename(path));
+       if(ext==".h" || ext==".H" || ext==".hpp")
+               install_location = FS::Path("include")/package->get_name();
+}
+
+void CSourceFile::parse_includes(IO::Base &in)
+{
+       static Regex r_include("^[ \t]*#include[ \t]+([\"<].*)[\">]");
+
+       string line;
+       while(in.getline(line))
+               if(RegMatch match = r_include.match(line))
+                       includes.push_back(match[1].str);
+}
+
+void CSourceFile::find_dependencies()
+{
+       if(!component || !mtime)
+               return;
+
+       const SourcePackage &spkg = component->get_package();
+
+       Cache &cache = spkg.get_cache();
+       if(mtime<cache.get_mtime() && cache.has_key(this, "includes"))
+               includes = cache.get_values(this, "includes");
+       else
+       {
+               IO::BufferedFile in(path.str());
+
+               builder.get_logger().log("files", "Reading includes from %s", path.str());
+
+               parse_includes(in);
+               cache.set_values(this, "includes", includes);
+       }
+
+       const BuildInfo &build_info = component->get_build_info_for_path(path);
+       const auto &incpath = build_info.incpath;
+       VirtualFileSystem::SearchPath local_incpath;
+       local_incpath.reserve(1+build_info.local_incpath.size()+incpath.size());
+       local_incpath.push_back(FS::dirname(path).str());
+       local_incpath.insert(local_incpath.end(), build_info.local_incpath.begin(), build_info.local_incpath.end());
+       local_incpath.insert(local_incpath.end(), incpath.begin(), incpath.end());
+
+       Tool *compiler = builder.get_toolchain().get_tool_for_suffix(FS::extpart(FS::basename(path)), true);
+       if(compiler)
+               compiler->prepare();
+       for(const string &i: includes)
+               if(Target *hdr = builder.get_vfs().find_header(i.substr(1), compiler, (i[0]=='"' ? local_incpath : incpath)))
+                       add_transitive_dependency(*hdr);
+}
+
+void CSourceFile::modified()
+{
+       includes.clear();
+       trans_depends.clear();
+       find_dependencies();
+}