Skip to content

Commit

Permalink
WIP: Initial attempt at compile database generation.
Browse files Browse the repository at this point in the history
This implements enough to obtain and generate the compile commands from the gcc toolset and writes it out to a ${PWD}/compile_commands.json (or the specified filename). I.e. implements both the --command-database=json amd --command-database-out=<filename> CLI options.
  • Loading branch information
grafikrobot committed May 30, 2024
1 parent 63a7603 commit 5ceb30f
Show file tree
Hide file tree
Showing 11 changed files with 5,014 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/.build/gcc-13/debug/cxxstd-11-iso/threading-multi/b2",
"args": [],
"args": ["-d1", "--command-database=json", "b2"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
Expand Down
142 changes: 142 additions & 0 deletions src/engine/command_db.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
Copyright 2024 René Ferdinand Rivera Morell
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)
*/

#include "command_db.h"

#include "command.h"
#include "cwd.h"
#include "events.h"
#include "lists.h"
#include "mod_db.h"
#include "output.h"
#include "regexp.h"

#include "ext_bfgroup_lyra.h"

#include <unordered_map>

namespace b2 { namespace command_db {

struct database
{
bool output_flag = false;
std::string output_format = "json";
std::string output_filename = "compile_commands.json";
std::string db_directory;
std::unordered_map<std::string, std::unique_ptr<regex::program>>
regex_cache;
std::unique_ptr<property_db> prop_db;
uint64_t command_index = 0;

database()
{
// B2 doesn't change directories. And runs everything relative to CWD.
// So we can cache the value to fill into the database.
db_directory = b2::cwd_str();
}

static database & get()
{
static database db;
return db;
}

void set_output_format(const std::string & f)
{
// Set up for the format and create the output database.
output_flag = true;
output_format = f;
prop_db.reset(new property_db);

// Default to all targets and no regular action output.
++globs.noexec;
for (int i = 0; i < DEBUG_MAX; ++i) globs.debug[i] = 0;
++anyhow;

// Events to track the commands and exit to generate the output.
add_event_callback(event_tag::pre_exec_cmd,
std::function<void(TARGET *)>(
[](TARGET * t) { database::get().pre_exec_cmd(t); }));
add_event_callback(event_tag::exit_main,
std::function<void(int)>(
[](int s) { database::get().exit_main(s); }));
}

void set_output_filename(const std::string & f) {}

void pre_exec_cmd(TARGET * t)
{
CMD * cmd = (CMD *)t->cmds;
auto args_out = list_cref(lol_get((LOL *)&cmd->args, 0));
auto args_in = list_cref(lol_get((LOL *)&cmd->args, 1));
auto settings = t->settings;
for (; settings; settings = settings->next)
{
value_ref symbol(settings->symbol);
list_cref value(settings->value);
if (symbol == "COMMAND_DATABASE" && !value.empty())
{
auto db_file = args_in[0]->str();
auto db_output = args_out[0]->str();
auto db_command
= extract_command(value[0]->str(), cmd->buf->value);
list_ref db_node;
db_node.push_back("[]").push_back(double(command_index++));
prop_db->emplace(
list_ref(db_node).push_back("output").cref(), db_output);
prop_db->emplace(
list_ref(db_node).push_back("file").cref(), db_file);
prop_db->emplace(
list_ref(db_node).push_back("directory").cref(),
db_directory);
prop_db->emplace(
list_ref(db_node).push_back("command").cref(), db_command);
}
}
}

std::string extract_command(const char * cmd_regex, const char * cmd_text)
{
regex::program * regex_prog = nullptr;
auto regex_prog_i = regex_cache.find(cmd_regex);
if (regex_prog_i == regex_cache.end())
{
auto regex_prog_e = std::unique_ptr<regex::program>(
new regex::program(cmd_regex));
regex_prog = regex_prog_e.get();
regex_cache.emplace(cmd_regex, std::move(regex_prog_e));
}
else
{
regex_prog = regex_prog_i->second.get();
}
auto regex_sub = regex_prog->search(cmd_text)[0];
return std::string(
regex_sub.cbegin(), regex_sub.cend() - regex_sub.begin());
}

void exit_main(int status)
{
if (status == EXIT_FAIL) return;
prop_db->write_file(output_filename, output_format);
}
};

void declare_args(lyra::cli & cli)
{
cli |= lyra::opt(
[](const std::string & f) { database::get().set_output_format(f); },
"format")
.name("--command-database")
.help("Output a compile commands database as format.");
cli |= lyra::opt(
[](const std::string & f) { database::get().set_output_filename(f); },
"filename")
.name("--command-database-out")
.help("Filename to output the command database to.");
}

}} // namespace b2::command_db
22 changes: 22 additions & 0 deletions src/engine/command_db.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright 2024 René Ferdinand Rivera Morell
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)
*/

#ifndef B2_COMMAND_DB_H
#define B2_COMMAND_DB_H

#include "config.h"

namespace lyra {
class cli;
} // namespace lyra

namespace b2 { namespace command_db {

void declare_args(lyra::cli &);

}} // namespace b2::command_db

#endif
123 changes: 123 additions & 0 deletions src/engine/events.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
Copyright 2024 René Ferdinand Rivera Morell
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)
*/

#include "events.h"

#include <algorithm>
#include <memory>
#include <set>
#include <vector>

namespace b2 {

struct event_base
{
event_tag tag;
int32_t priority;
uint64_t id;

inline bool operator<(const event_base & o) const
{
if (tag < o.tag) return true;
if ((0 - priority) < (0 - o.priority)) return true;
return id < o.id;
}
};

template <typename F>
struct event : public event_base
{
F call;
};

struct events
{
struct ecmp
{
inline bool operator()(const event_base * a, const event_base * b) const
{
return (*a) < (*b);
}
};
std::set<const event_base *, ecmp> sorted_items;
std::vector<std::unique_ptr<event_base>> items;

static events & get()
{
static events e;
return e;
}

template <typename F>
uint64_t add(event_tag tag, F && call, int32_t priority)
{
uint64_t id = items.empty() ? 1 : items.back()->id + 1;
std::unique_ptr<event<F>> e(new event<F>);
e->tag = tag;
e->priority = priority;
e->id = id;
e->call = call;
sorted_items.insert(e.get());
items.push_back(std::move(e));
return id;
}

void remove(uint64_t e)
{
auto i = std::lower_bound(items.begin(), items.end(), e,
[](const std::unique_ptr<event_base> & a, uint64_t id) -> bool {
return a->id < id;
});
if (i != items.end() && (*i)->id == e)
{
items.erase(i);
}
}

template <typename... Args>
void trigger(event_tag tag, Args... args)
{
using E = event<std::function<void(Args...)>>;
static event_base x = { tag, std::numeric_limits<int32_t>::max(), 0 };
auto i = sorted_items.lower_bound(&x);
static event_base y = { tag, std::numeric_limits<int32_t>::min(), 0 };
auto j = sorted_items.lower_bound(&y);
if (j != sorted_items.end()) ++j;
for (; i != j; ++i)
{
if ((*i)->tag != tag) break;
static_cast<const E *>(*i)->call(args...);
}
}
};

void remove_event_callback(uint64_t e) { events::get().remove(e); }

template <>
uint64_t add_event_callback(
event_tag tag, std::function<void(TARGET *)> && call, int32_t priority)
{
return events::get().add(tag, std::move(call), priority);
}

void trigger_event_pre_exec_cmd(TARGET * t)
{
events::get().trigger<TARGET *>(event_tag::pre_exec_cmd, t);
}

template <>
uint64_t add_event_callback(
event_tag tag, std::function<void(int)> && call, int32_t priority)
{
return events::get().add(tag, std::move(call), priority);
}

void trigger_event_exit_main(int status)
{
events::get().trigger<int>(event_tag::exit_main, status);
}

} // namespace b2
36 changes: 36 additions & 0 deletions src/engine/events.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2024 René Ferdinand Rivera Morell
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)
*/

#ifndef B2_EVENTS_H
#define B2_EVENTS_H

#include "config.h"

#include "rules.h"

#include <cstdint>
#include <functional>

namespace b2 {

enum class event_tag : uint16_t
{
unknown = 0,
pre_exec_cmd,
exit_main
};

template <typename F>
uint64_t add_event_callback(
event_tag tag, std::function<F> && call, int32_t priority = 0);
void remove_event_callback(uint64_t e);

void trigger_event_pre_exec_cmd(TARGET * t);
void trigger_event_exit_main(int status);

} // namespace b2

#endif
Loading

0 comments on commit 5ceb30f

Please sign in to comment.