mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-26 04:32:40 +00:00
Add absolute file names option to BFBS (#8055)
* Add absolute file names option (#1) * Use ternary style for if --------- Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
@@ -676,6 +676,7 @@ struct IDLOptions {
|
|||||||
bool binary_schema_comments;
|
bool binary_schema_comments;
|
||||||
bool binary_schema_builtins;
|
bool binary_schema_builtins;
|
||||||
bool binary_schema_gen_embed;
|
bool binary_schema_gen_embed;
|
||||||
|
bool binary_schema_absolute_paths;
|
||||||
std::string go_import;
|
std::string go_import;
|
||||||
std::string go_namespace;
|
std::string go_namespace;
|
||||||
std::string go_module_name;
|
std::string go_module_name;
|
||||||
@@ -796,6 +797,7 @@ struct IDLOptions {
|
|||||||
binary_schema_comments(false),
|
binary_schema_comments(false),
|
||||||
binary_schema_builtins(false),
|
binary_schema_builtins(false),
|
||||||
binary_schema_gen_embed(false),
|
binary_schema_gen_embed(false),
|
||||||
|
binary_schema_absolute_paths(false),
|
||||||
protobuf_ascii_alike(false),
|
protobuf_ascii_alike(false),
|
||||||
size_prefixed(false),
|
size_prefixed(false),
|
||||||
force_defaults(false),
|
force_defaults(false),
|
||||||
|
|||||||
@@ -479,6 +479,11 @@ std::string PosixPath(const std::string &path);
|
|||||||
// creating dirs for any parts of the path that don't exist yet.
|
// creating dirs for any parts of the path that don't exist yet.
|
||||||
void EnsureDirExists(const std::string &filepath);
|
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.
|
// Obtains the absolute path from any other path.
|
||||||
// Returns the input path if the absolute path couldn't be resolved.
|
// Returns the input path if the absolute path couldn't be resolved.
|
||||||
std::string AbsolutePath(const std::string &filepath);
|
std::string AbsolutePath(const std::string &filepath);
|
||||||
|
|||||||
@@ -187,6 +187,7 @@ const static FlatCOption flatc_options[] = {
|
|||||||
"relative to. The 'root' is denoted with `//`. E.g. if PATH=/a/b/c "
|
"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 "
|
"then /a/d/e.fbs will be serialized as //../d/e.fbs. (PATH defaults to the "
|
||||||
"directory of the first provided schema file." },
|
"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-comments", "", "Add doc comments to the binary schema files." },
|
||||||
{ "", "bfbs-builtins", "",
|
{ "", "bfbs-builtins", "",
|
||||||
"Add builtin attributes to the binary schema files." },
|
"Add builtin attributes to the binary schema files." },
|
||||||
@@ -596,6 +597,8 @@ FlatCOptions FlatCompiler::ParseFromCommandLineArguments(int argc,
|
|||||||
opts.binary_schema_builtins = true;
|
opts.binary_schema_builtins = true;
|
||||||
} else if (arg == "--bfbs-gen-embed") {
|
} else if (arg == "--bfbs-gen-embed") {
|
||||||
opts.binary_schema_gen_embed = true;
|
opts.binary_schema_gen_embed = true;
|
||||||
|
} else if (arg == "--bfbs-absolute-paths") {
|
||||||
|
opts.binary_schema_absolute_paths = true;
|
||||||
} else if (arg == "--reflect-types") {
|
} else if (arg == "--reflect-types") {
|
||||||
opts.mini_reflect = IDLOptions::kTypes;
|
opts.mini_reflect = IDLOptions::kTypes;
|
||||||
} else if (arg == "--reflect-names") {
|
} else if (arg == "--reflect-names") {
|
||||||
|
|||||||
@@ -2483,7 +2483,7 @@ CheckedError Parser::ParseEnum(const bool is_union, EnumDef **dest,
|
|||||||
ECHECK(StartEnum(enum_name, is_union, &enum_def));
|
ECHECK(StartEnum(enum_name, is_union, &enum_def));
|
||||||
if (filename != nullptr && !opts.project_root.empty()) {
|
if (filename != nullptr && !opts.project_root.empty()) {
|
||||||
enum_def->declaration_file =
|
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;
|
enum_def->doc_comment = enum_comment;
|
||||||
if (!opts.proto_mode) {
|
if (!opts.proto_mode) {
|
||||||
@@ -2762,7 +2762,7 @@ CheckedError Parser::ParseDecl(const char *filename) {
|
|||||||
struct_def->fixed = fixed;
|
struct_def->fixed = fixed;
|
||||||
if (filename && !opts.project_root.empty()) {
|
if (filename && !opts.project_root.empty()) {
|
||||||
struct_def->declaration_file =
|
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));
|
ECHECK(ParseMetaData(&struct_def->attributes));
|
||||||
struct_def->sortbysize =
|
struct_def->sortbysize =
|
||||||
@@ -2856,7 +2856,7 @@ CheckedError Parser::ParseService(const char *filename) {
|
|||||||
service_def.defined_namespace = current_namespace_;
|
service_def.defined_namespace = current_namespace_;
|
||||||
if (filename != nullptr && !opts.project_root.empty()) {
|
if (filename != nullptr && !opts.project_root.empty()) {
|
||||||
service_def.declaration_file =
|
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),
|
if (services_.Add(current_namespace_->GetFullyQualifiedName(service_name),
|
||||||
&service_def))
|
&service_def))
|
||||||
@@ -3937,11 +3937,12 @@ void Parser::Serialize() {
|
|||||||
std::vector<Offset<flatbuffers::String>> included_files;
|
std::vector<Offset<flatbuffers::String>> included_files;
|
||||||
for (auto f = files_included_per_file_.begin();
|
for (auto f = files_included_per_file_.begin();
|
||||||
f != files_included_per_file_.end(); f++) {
|
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++) {
|
for (auto i = f->second.begin(); i != f->second.end(); i++) {
|
||||||
included_files.push_back(builder_.CreateSharedString(
|
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);
|
const auto included_files__ = builder_.CreateVector(included_files);
|
||||||
included_files.clear();
|
included_files.clear();
|
||||||
|
|||||||
@@ -336,6 +336,10 @@ void EnsureDirExists(const std::string &filepath) {
|
|||||||
// clang-format on
|
// 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) {
|
std::string AbsolutePath(const std::string &filepath) {
|
||||||
// clang-format off
|
// clang-format off
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user