keep-prefix keeps relative pathing (#7394)

This commit is contained in:
Derek Bailey
2022-07-26 15:11:54 -07:00
committed by GitHub
parent 52fce5e532
commit 6e2791640e
7 changed files with 30 additions and 10 deletions

View File

@@ -577,7 +577,7 @@ struct IDLOptions {
bool allow_non_utf8; bool allow_non_utf8;
bool natural_utf8; bool natural_utf8;
std::string include_prefix; std::string include_prefix;
bool keep_include_path; bool keep_prefix;
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;
@@ -683,7 +683,7 @@ struct IDLOptions {
union_value_namespacing(true), union_value_namespacing(true),
allow_non_utf8(false), allow_non_utf8(false),
natural_utf8(false), natural_utf8(false),
keep_include_path(false), keep_prefix(false),
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),
@@ -1054,11 +1054,11 @@ class Parser : public ParserState {
uint64_t advanced_features_; uint64_t advanced_features_;
std::string file_being_parsed_;
private: private:
const char *source_; const char *source_;
std::string file_being_parsed_;
std::vector<std::pair<Value, FieldDef *>> field_stack_; std::vector<std::pair<Value, FieldDef *>> field_stack_;
// TODO(cneo): Refactor parser to use string_cache more often to save // TODO(cneo): Refactor parser to use string_cache more often to save

View File

@@ -448,6 +448,9 @@ std::string StripPath(const std::string &filepath);
// Strip the last component of the path + separator. // Strip the last component of the path + separator.
std::string StripFileName(const std::string &filepath); std::string StripFileName(const std::string &filepath);
std::string StripPrefix(const std::string &filepath,
const std::string &prefix_to_remove);
// Concatenates a path with a filename, regardless of whether the path // Concatenates a path with a filename, regardless of whether the path
// ends in a separator or not. // ends in a separator or not.
std::string ConCatPathFileName(const std::string &path, std::string ConCatPathFileName(const std::string &path,

View File

@@ -433,7 +433,7 @@ int FlatCompiler::Compile(int argc, const char **argv) {
opts.include_prefix = flatbuffers::ConCatPathFileName( opts.include_prefix = flatbuffers::ConCatPathFileName(
flatbuffers::PosixPath(argv[argi]), ""); flatbuffers::PosixPath(argv[argi]), "");
} else if (arg == "--keep-prefix") { } else if (arg == "--keep-prefix") {
opts.keep_include_path = true; opts.keep_prefix = true;
} else if (arg == "--strict-json") { } else if (arg == "--strict-json") {
opts.strict_json = true; opts.strict_json = true;
} else if (arg == "--allow-non-utf8") { } else if (arg == "--allow-non-utf8") {

View File

@@ -235,13 +235,20 @@ class CppGenerator : public BaseGenerator {
// interdependence between them. // interdependence between them.
std::stable_sort(included_files.begin(), included_files.end()); std::stable_sort(included_files.begin(), included_files.end());
// Get any prefix of the file being parsed, so that included filed can be
// properly stripped.
auto prefix = flatbuffers::StripFileName(parser_.file_being_parsed_) +
flatbuffers::kPathSeparator;
for (const std::string &included_file : included_files) { for (const std::string &included_file : included_files) {
auto noext = flatbuffers::StripExtension(included_file); auto file_without_extension = flatbuffers::StripExtension(included_file);
code_ += code_ +=
"#include \"" + "#include \"" +
GeneratedFileName( GeneratedFileName(
opts_.include_prefix, opts_.include_prefix,
opts_.keep_include_path ? noext : flatbuffers::StripPath(noext), opts_.keep_prefix
? flatbuffers::StripPrefix(file_without_extension, prefix)
: flatbuffers::StripPath(file_without_extension),
opts_) + opts_) +
"\""; "\"";
} }

View File

@@ -82,7 +82,7 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
if (it->second.empty()) if (it->second.empty())
continue; continue;
std::string basename; std::string basename;
if(parser.opts.keep_include_path) { if(parser.opts.keep_prefix) {
basename = flatbuffers::StripExtension(it->second); basename = flatbuffers::StripExtension(it->second);
} else { } else {
basename = flatbuffers::StripPath( basename = flatbuffers::StripPath(

View File

@@ -234,7 +234,7 @@ class TsGenerator : public BaseGenerator {
std::string basename = flatbuffers::StripPath(noext); std::string basename = flatbuffers::StripPath(noext);
std::string include_file = GeneratedFileName( std::string include_file = GeneratedFileName(
parser_.opts.include_prefix, parser_.opts.include_prefix,
parser_.opts.keep_include_path ? noext : basename, parser_.opts); parser_.opts.keep_prefix ? noext : basename, parser_.opts);
// TODO: what is the right behavior when different include flags are // TODO: what is the right behavior when different include flags are
// specified here? Should we always be adding the "./" for a relative // specified here? Should we always be adding the "./" for a relative
// path or turn it off if --include-prefix is specified, or something // path or turn it off if --include-prefix is specified, or something

View File

@@ -17,6 +17,7 @@
// clang-format off // clang-format off
// Dont't remove `format off`, it prevent reordering of win-includes. // Dont't remove `format off`, it prevent reordering of win-includes.
#include <cstring>
#if defined(__MINGW32__) || defined(__MINGW64__) || defined(__CYGWIN__) || \ #if defined(__MINGW32__) || defined(__MINGW64__) || defined(__CYGWIN__) || \
defined(__QNXNTO__) defined(__QNXNTO__)
# define _POSIX_C_SOURCE 200809L # define _POSIX_C_SOURCE 200809L
@@ -46,8 +47,8 @@
#include <clocale> #include <clocale>
#include <cstdlib> #include <cstdlib>
#include <functional>
#include <fstream> #include <fstream>
#include <functional>
#include "flatbuffers/base.h" #include "flatbuffers/base.h"
@@ -155,6 +156,15 @@ std::string StripFileName(const std::string &filepath) {
return i != std::string::npos ? filepath.substr(0, i) : ""; return i != std::string::npos ? filepath.substr(0, i) : "";
} }
std::string StripPrefix(const std::string &filepath,
const std::string &prefix_to_remove) {
if (!strncmp(filepath.c_str(), prefix_to_remove.c_str(),
prefix_to_remove.size())) {
return filepath.substr(prefix_to_remove.size());
}
return filepath;
}
std::string ConCatPathFileName(const std::string &path, std::string ConCatPathFileName(const std::string &path,
const std::string &filename) { const std::string &filename) {
std::string filepath = path; std::string filepath = path;