]> git.tdb.fi Git - builder.git/commitdiff
Add support for generating compile_commands.json
authorMikko Rasa <tdb@tdb.fi>
Sun, 31 Oct 2021 15:44:48 +0000 (17:44 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 31 Oct 2021 15:44:48 +0000 (17:44 +0200)
This file is used by some clang tools to find compile options.

source/builtintools.cpp
source/compilecommandsgenerator.cpp [new file with mode: 0644]
source/compilecommandsgenerator.h [new file with mode: 0644]
source/compilecommandsjson.cpp [new file with mode: 0644]
source/compilecommandsjson.h [new file with mode: 0644]
source/sourcepackage.cpp

index 63a6889dc772401180fe9193e84096807a10e3ae..42ad33fdb9309b07ea7fd93474055dccac17b738 100644 (file)
@@ -1,5 +1,6 @@
 #include "builtintools.h"
 #include "copy.h"
+#include "compilecommandsgenerator.h"
 #include "pkgconfiggenerator.h"
 #include "tar.h"
 #include "vcxprojectgenerator.h"
@@ -10,4 +11,5 @@ BuiltinTools::BuiltinTools(Builder &builder)
        add_tool(new Tar(builder));
        add_tool(new PkgConfigGenerator(builder));
        add_tool(new VcxProjectGenerator(builder));
+       add_tool(new CompileCommandsGenerator(builder));
 }
diff --git a/source/compilecommandsgenerator.cpp b/source/compilecommandsgenerator.cpp
new file mode 100644 (file)
index 0000000..07ee15e
--- /dev/null
@@ -0,0 +1,65 @@
+#include <msp/io/file.h>
+#include <msp/io/print.h>
+#include <msp/strings/utils.h>
+#include "builder.h"
+#include "compilecommandsgenerator.h"
+#include "compilecommandsjson.h"
+#include "objectfile.h"
+#include "sourcefile.h"
+
+using namespace std;
+using namespace Msp;
+
+CompileCommandsGenerator::CompileCommandsGenerator(Builder &b):
+       Tool(b, "CCJG")
+{ }
+
+Target *CompileCommandsGenerator::create_target(const list<Target *> &, const string &)
+{
+       throw logic_error("Not implemented");
+}
+
+Task *CompileCommandsGenerator::run(const Target &target) const
+{
+       const CompileCommandsJson &cmds = dynamic_cast<const CompileCommandsJson &>(target);
+       Worker *worker = new Worker(cmds);
+       return new InternalTask(worker);
+}
+
+
+CompileCommandsGenerator::Worker::Worker(const CompileCommandsJson &t):
+       target(t)
+{ }
+
+void CompileCommandsGenerator::Worker::main()
+{
+       Builder &builder = target.get_package()->get_builder();
+       const SourcePackage &spkg = *target.get_package();
+       string work_dir = c_escape(spkg.get_source_directory().str());
+
+       IO::BufferedFile out(target.get_path().str(), IO::M_WRITE);
+       IO::print(out, "[");
+
+       bool first = true;
+       const BuildGraph::TargetMap &targets = builder.get_build_graph().get_targets();
+       for(BuildGraph::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
+               if(i->second->is_buildable() && i->second->get_package()==&spkg)
+                       if(ObjectFile *obj = dynamic_cast<ObjectFile *>(i->second))
+                       {
+                               FS::Path src_path = obj->get_source().get_path();
+                               Task *task = i->second->build();
+                               if(!first)
+                                       out.put(',');
+                               IO::print(out, "\n\t{\n");
+                               IO::print(out, "\t\t\"file\": \"%s\"\n", src_path);
+                               IO::print(out, "\t\t\"command\": \"%s\"\n", c_escape(task->get_command()));
+                               IO::print(out, "\t\t\"directory\": \"%s\"\n", work_dir);
+                               IO::print(out, "\t}");
+                               delete task;
+                               first = false;
+                       }
+
+       IO::print(out, "\n]\n");
+
+       status = Task::SUCCESS;
+}
diff --git a/source/compilecommandsgenerator.h b/source/compilecommandsgenerator.h
new file mode 100644 (file)
index 0000000..ba74f11
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef COMPILECOMMANDSGENERATOR_H_
+#define COMPILECOMMANDSGENERATOR_H_
+
+#include "internaltask.h"
+#include "tool.h"
+
+class CompileCommandsJson;
+
+class CompileCommandsGenerator: public Tool
+{
+private:
+       class Worker: public InternalTask::Worker
+       {
+       private:
+               const CompileCommandsJson &target;
+
+       public:
+               Worker(const CompileCommandsJson &);
+
+       private:
+               virtual void main();
+       };
+
+public:
+       CompileCommandsGenerator(Builder &);
+
+       virtual Target *create_target(const std::list<Target *> &, const std::string &);
+       virtual Task *run(const Target &) const;
+};
+
+#endif
diff --git a/source/compilecommandsjson.cpp b/source/compilecommandsjson.cpp
new file mode 100644 (file)
index 0000000..a8a1929
--- /dev/null
@@ -0,0 +1,18 @@
+#include "builder.h"
+#include "package.h"
+#include "compilecommandsjson.h"
+#include "objectfile.h"
+
+CompileCommandsJson::CompileCommandsJson(Builder &b, const SourcePackage &p):
+       FileTarget(b, p, p.get_source_directory()/("compile_commands.json"))
+{
+       tool = &builder.get_toolchain().get_tool("CCJG");
+}
+
+void CompileCommandsJson::find_dependencies()
+{
+       const BuildGraph::TargetMap &targets = builder.get_build_graph().get_targets();
+       for(BuildGraph::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
+               if(i->second->is_buildable() && i->second->get_package()==package && dynamic_cast<ObjectFile *>(i->second))
+                       i->second->prepare();
+}
diff --git a/source/compilecommandsjson.h b/source/compilecommandsjson.h
new file mode 100644 (file)
index 0000000..1d170c5
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef COMPILECOMMANDSJSON_H_
+#define COMPILECOMMANDSJSON_H_
+
+#include "sourcepackage.h"
+#include "filetarget.h"
+
+class CompileCommandsJson: public FileTarget
+{
+private:
+       
+public:
+       CompileCommandsJson(Builder &, const SourcePackage &);
+
+       virtual const char *get_type() const { return "CompileCommandsJson"; }
+
+protected:
+       virtual void find_dependencies();
+};
+
+#endif
index c4061a4afbeceae8a153cf45a1b05f0d82530cc3..1a23c4704d04c963677e8036a45b940a3c1467f5 100644 (file)
@@ -9,6 +9,7 @@
 #include "binarycomponent.h"
 #include "binarypackage.h"
 #include "builder.h"
+#include "compilecommandsjson.h"
 #include "datapackcomponent.h"
 #include "file.h"
 #include "installcomponent.h"
@@ -149,6 +150,8 @@ void SourcePackage::do_prepare()
 
        if(arch.get_system()=="windows")
                new VcxProjectFile(builder, *this);
+
+       new CompileCommandsJson(builder, *this);
 }
 
 void SourcePackage::save_caches()