Skip to content

Commit

Permalink
Add absolute file names option to BFBS (google#8055)
Browse files Browse the repository at this point in the history
* Add absolute file names option (google#1)

* Use ternary style for if

---------

Co-authored-by: Derek Bailey <derekbailey@google.com>
  • Loading branch information
2 people authored and candysonya committed Jan 8, 2024
1 parent 1149feb commit 7ae4060
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ struct IDLOptions {
bool binary_schema_comments;
bool binary_schema_builtins;
bool binary_schema_gen_embed;
bool binary_schema_absolute_paths;
std::string go_import;
std::string go_namespace;
std::string go_module_name;
Expand Down Expand Up @@ -796,6 +797,7 @@ struct IDLOptions {
binary_schema_comments(false),
binary_schema_builtins(false),
binary_schema_gen_embed(false),
binary_schema_absolute_paths(false),
protobuf_ascii_alike(false),
size_prefixed(false),
force_defaults(false),
Expand Down
5 changes: 5 additions & 0 deletions include/flatbuffers/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,11 @@ std::string PosixPath(const std::string &path);
// creating dirs for any parts of the path that don't exist yet.
void EnsureDirExists(const std::string &filepath);

// Obtains the relative or absolute path.
std::string FilePath(const std::string &project,
const std::string &filePath,
bool absolute);

// Obtains the absolute path from any other path.
// Returns the input path if the absolute path couldn't be resolved.
std::string AbsolutePath(const std::string &filepath);
Expand Down
3 changes: 3 additions & 0 deletions src/flatc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ const static FlatCOption flatc_options[] = {
"relative to. The 'root' is denoted with `//`. E.g. if PATH=/a/b/c "
"then /a/d/e.fbs will be serialized as //../d/e.fbs. (PATH defaults to the "
"directory of the first provided schema file." },
{ "", "bfbs-absolute-paths", "", "Uses absolute paths instead of relative paths in the BFBS output." },
{ "", "bfbs-comments", "", "Add doc comments to the binary schema files." },
{ "", "bfbs-builtins", "",
"Add builtin attributes to the binary schema files." },
Expand Down Expand Up @@ -596,6 +597,8 @@ FlatCOptions FlatCompiler::ParseFromCommandLineArguments(int argc,
opts.binary_schema_builtins = true;
} else if (arg == "--bfbs-gen-embed") {
opts.binary_schema_gen_embed = true;
} else if (arg == "--bfbs-absolute-paths") {
opts.binary_schema_absolute_paths = true;
} else if (arg == "--reflect-types") {
opts.mini_reflect = IDLOptions::kTypes;
} else if (arg == "--reflect-names") {
Expand Down
13 changes: 7 additions & 6 deletions src/idl_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2483,7 +2483,7 @@ CheckedError Parser::ParseEnum(const bool is_union, EnumDef **dest,
ECHECK(StartEnum(enum_name, is_union, &enum_def));
if (filename != nullptr && !opts.project_root.empty()) {
enum_def->declaration_file =
&GetPooledString(RelativeToRootPath(opts.project_root, filename));
&GetPooledString(FilePath(opts.project_root, filename, opts.binary_schema_absolute_paths));
}
enum_def->doc_comment = enum_comment;
if (!opts.proto_mode) {
Expand Down Expand Up @@ -2762,7 +2762,7 @@ CheckedError Parser::ParseDecl(const char *filename) {
struct_def->fixed = fixed;
if (filename && !opts.project_root.empty()) {
struct_def->declaration_file =
&GetPooledString(RelativeToRootPath(opts.project_root, filename));
&GetPooledString(FilePath(opts.project_root, filename, opts.binary_schema_absolute_paths));
}
ECHECK(ParseMetaData(&struct_def->attributes));
struct_def->sortbysize =
Expand Down Expand Up @@ -2856,7 +2856,7 @@ CheckedError Parser::ParseService(const char *filename) {
service_def.defined_namespace = current_namespace_;
if (filename != nullptr && !opts.project_root.empty()) {
service_def.declaration_file =
&GetPooledString(RelativeToRootPath(opts.project_root, filename));
&GetPooledString(FilePath(opts.project_root, filename, opts.binary_schema_absolute_paths));
}
if (services_.Add(current_namespace_->GetFullyQualifiedName(service_name),
&service_def))
Expand Down Expand Up @@ -3937,11 +3937,12 @@ void Parser::Serialize() {
std::vector<Offset<flatbuffers::String>> included_files;
for (auto f = files_included_per_file_.begin();
f != files_included_per_file_.end(); f++) {
const auto filename__ = builder_.CreateSharedString(
RelativeToRootPath(opts.project_root, f->first));

const auto filename__ = builder_.CreateSharedString(FilePath(
opts.project_root, f->first, opts.binary_schema_absolute_paths));
for (auto i = f->second.begin(); i != f->second.end(); i++) {
included_files.push_back(builder_.CreateSharedString(
RelativeToRootPath(opts.project_root, i->filename)));
FilePath(opts.project_root, i->filename, opts.binary_schema_absolute_paths)));
}
const auto included_files__ = builder_.CreateVector(included_files);
included_files.clear();
Expand Down
4 changes: 4 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ void EnsureDirExists(const std::string &filepath) {
// clang-format on
}

std::string FilePath(const std::string& project, const std::string& filePath, bool absolute) {
return (absolute) ? AbsolutePath(filePath) : RelativeToRootPath(project, filePath);
}

std::string AbsolutePath(const std::string &filepath) {
// clang-format off

Expand Down

0 comments on commit 7ae4060

Please sign in to comment.