diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index dd13918c15c..a08db9bb658 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -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; @@ -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), diff --git a/include/flatbuffers/util.h b/include/flatbuffers/util.h index 1ccf3517d30..82b37fb9867 100644 --- a/include/flatbuffers/util.h +++ b/include/flatbuffers/util.h @@ -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); diff --git a/src/flatc.cpp b/src/flatc.cpp index d493c5eb315..4bc88eb1449 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -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." }, @@ -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") { diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 5b42b35fc61..d01e18ef761 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -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) { @@ -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 = @@ -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)) @@ -3937,11 +3937,12 @@ void Parser::Serialize() { std::vector> 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(); diff --git a/src/util.cpp b/src/util.cpp index b201246fd7a..2d45ee7a0d5 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -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