Skip to content

Commit

Permalink
Handle commanddb filename defaults and --command-database-out option.
Browse files Browse the repository at this point in the history
  • Loading branch information
grafikrobot committed May 31, 2024
1 parent b04da48 commit ac66542
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Jamroot.jam
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import virtual-target ;

path-constant SELF : . ;

project b2
project /bfgroup/b2
: build-dir .build
: requirements
<cxxstd>11
Expand Down
17 changes: 16 additions & 1 deletion src/build/project.jam
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import print ;
import property-set ;
import regex ;
import sequence ;
import command-db ;


.debug-loading = [ MATCH ^(--debug-loading)$ : [ modules.peek : ARGV ] ] ;
Expand Down Expand Up @@ -712,11 +713,12 @@ rule initialize (

# load-parent can end up loading this module again. Make sure this is not
# duplicated.
local attributes ;
if ! $($(module-name).attributes)
{
$(module-name).attributes = [ new project-attributes $(location)
$(module-name) ] ;
local attributes = $($(module-name).attributes) ;
attributes = $($(module-name).attributes) ;

if $(location)
{
Expand Down Expand Up @@ -791,6 +793,12 @@ rule initialize (
}

.current-project = [ target $(module-name) ] ;

if $(jamroot) && [ $(attributes).get build-dir ]
{
command-db.set-output-dir
[ path.native [ $(attributes).get build-dir ] ] ;
}
}


Expand Down Expand Up @@ -1312,6 +1320,7 @@ module project-rules
{
import path ;
import project ;
import command-db ;

local caller = [ CALLER_MODULE ] ;
local attributes = [ project.attributes $(caller) ] ;
Expand Down Expand Up @@ -1382,6 +1391,12 @@ module project-rules
}
}
}

if $(explicit-build-dir)
{
command-db.set-output-dir
[ path.native [ $(attributes).get build-dir ] ] ;
}
}

# Declare and set a project global constant. Project global constants are
Expand Down
4 changes: 3 additions & 1 deletion src/engine/bindjam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Distributed under the Boost Software License, Version 1.0.
#include "value.h"
#include "variable.h"

#include "mod_command_db.h"
#include "mod_db.h"
#include "mod_jam_builtin.h"
#include "mod_jam_class.h"
Expand Down Expand Up @@ -823,7 +824,8 @@ void bind_jam(FRAME * f)
.bind(string_module())
.bind(sysinfo_module())
.bind(version_module())
.bind(db_module());
.bind(db_module())
.bind(command_db_module());
}

}} // namespace b2::jam
22 changes: 0 additions & 22 deletions src/engine/command_db.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/engine/jam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
#include "variable.h"
#include "execcmd.h"
#include "mod_sysinfo.h"
#include "command_db.h"
#include "mod_command_db.h"

#include <errno.h>
#include <string.h>
Expand Down
31 changes: 27 additions & 4 deletions src/engine/command_db.cpp → src/engine/mod_command_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ 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 "mod_command_db.h"

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

#include "ext_bfgroup_lyra.h"
Expand All @@ -25,6 +26,7 @@ struct database
bool output_flag = false;
std::string output_format = "json";
std::string output_filename = "compile_commands.json";
std::string output_directory;
std::string db_directory;
std::unordered_map<std::string, std::unique_ptr<regex::program>>
regex_cache;
Expand All @@ -36,6 +38,7 @@ struct 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();
output_directory = db_directory;
}

static database & get()
Expand Down Expand Up @@ -65,7 +68,7 @@ struct database
[](int s) { database::get().exit_main(s); }));
}

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

void pre_exec_cmd(TARGET * t)
{
Expand Down Expand Up @@ -121,7 +124,19 @@ struct database
void exit_main(int status)
{
if (status == EXIT_FAIL) return;
prop_db->write_file(output_filename, output_format);
std::string filename = output_filename;
if (!b2::paths::is_rooted(output_filename))
{
if (!b2::paths::is_rooted(output_directory))
filename = b2::cwd_str() + "/";
filename += output_directory + "/" + output_filename;
filename = b2::paths::normalize(filename);
}
if (prop_db->write_file(filename, output_format))
out_printf("...wrote command database '%s'...\n", filename.c_str());
else
err_printf("...writing to command database '%s' FAILED...\n",
filename.c_str());
}
};

Expand All @@ -136,7 +151,15 @@ void declare_args(lyra::cli & cli)
[](const std::string & f) { database::get().set_output_filename(f); },
"filename")
.name("--command-database-out")
.help("Filename to output the command database to.");
.help(
"Filename to output the command database to. "
"A relative path for the filename is rooted to the project "
"build-dir.");
}

void set_output_dir(value_ref dirname)
{
database::get().output_directory = dirname;
}

}} // namespace b2::command_db
44 changes: 44 additions & 0 deletions src/engine/mod_command_db.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
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"

#include "bind.h"
#include "value.h"

namespace lyra {
class cli;
} // namespace lyra

namespace b2 {

namespace command_db {

void declare_args(lyra::cli &);

void set_output_dir(value_ref dirname);

} // namespace command_db

struct command_db_module : b2::bind::module_<command_db_module>
{
const char * module_name = "command-db";

template <class Binder>
void def(Binder & binder)
{
binder.def(
&command_db::set_output_dir, "set-output-dir", ("dirname" * _1));
binder.loaded();
}
};

} // namespace b2

#endif
23 changes: 14 additions & 9 deletions src/engine/mod_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ void b2::property_db::emplace(list_cref k, value_ref v)
db[nk] = v;
}

void b2::property_db::write_file(value_ref filename, value_ref format)
bool b2::property_db::write_file(value_ref filename, value_ref format)
{
if (!format->has_value())
{
format = b2::value::make("json");
}
if (format == "json") write_file_json(filename);
if (format == "json") return write_file_json(filename);
return false;
}

std::string b2::property_db::dump(value_ref format)
Expand Down Expand Up @@ -101,19 +102,23 @@ void build_json_from_db(const T & db, nlohmann::json & out)
}
} // namespace

void b2::property_db::write_file_json(value_ref filename)
bool b2::property_db::write_file_json(value_ref filename)
{
nlohmann::json out;
build_json_from_db(db, out);
FILE * file = std::fopen(filename->str(), "w");
try
if (file)
{
auto data = out.dump(0);
std::fwrite(data.c_str(), data.size(), 1, file);
try
{
auto data = out.dump(0);
return std::fwrite(data.c_str(), data.size(), 1, file) == 1;
}
catch (const std::exception &)
{}
std::fclose(file);
}
catch (const std::exception &)
{}
std::fclose(file);
return false;
}

std::string b2::property_db::dump_json()
Expand Down
4 changes: 2 additions & 2 deletions src/engine/mod_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ end::reference[] */
struct property_db : public object
{
void emplace(list_cref k, value_ref v);
void write_file(value_ref filename, value_ref format);
bool write_file(value_ref filename, value_ref format);
std::string dump(value_ref format);

private:
Expand All @@ -58,7 +58,7 @@ struct property_db : public object
};
using db_type = std::map<list_ref, value_ref, key_less>;
db_type db;
void write_file_json(value_ref filename);
bool write_file_json(value_ref filename);
std::string dump_json();
};

Expand Down

0 comments on commit ac66542

Please sign in to comment.