From 5f3f995fde85d0c8008abff9f2c89ad25b031b9c Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 27 Feb 2018 20:42:01 +0100 Subject: [PATCH] build system: add accidentally ignored compilation_database.rb --- .gitignore | 1 - rake.d/compilation_database.rb | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 rake.d/compilation_database.rb diff --git a/.gitignore b/.gitignore index 1ad18b91a..c872f3266 100644 --- a/.gitignore +++ b/.gitignore @@ -50,7 +50,6 @@ /po/*.mo /po/mkvtoolnix.pot /po/qt/*.qm -/rake.d/compilation_database.rb /rake.d/dependency.d /share/icons/*/*.h /src/*/qt_resources.cpp diff --git a/rake.d/compilation_database.rb b/rake.d/compilation_database.rb new file mode 100644 index 000000000..67f05b143 --- /dev/null +++ b/rake.d/compilation_database.rb @@ -0,0 +1,36 @@ +module Mtx + module CompilationDatabase + @file_name = "#{$build_dir}/compile_commands.json" + @compilation_commands = {} + + def self.database_file_name + @file_name + end + + def self.read + return {} unless FileTest.exists?(@file_name) + + Hash[ + *JSON.parse(IO.readlines(@file_name).join("")). + map { |entry| [ entry["file"], entry ] }. + flatten + ] + end + + def self.write + return if @compilation_commands.empty? + return if !FileTest.exists?(@file_name) && !c?(:BUILD_COMPILATION_DATABASE) + + entries = self.read.merge(@compilation_commands).values.sort_by { |e| e["file"] } + File.open(@file_name, "w") do |f| + f.write(JSON.generate(entries, :indent => " ", :object_nl => "\n", :array_nl => "\n")) + end + end + + def self.add entry + @compilation_commands[entry["file"]] = entry + end + end +end + +at_exit { Mtx::CompilationDatabase.write }