Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed bfbs gen to pass extra options #7949

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions goldens/generate_goldens.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
GenerateJava()
GenerateKotlin()
GenerateLobster()
# TODO these doesn't respect the output prefix, fix and reenable
# GenerateLua()
# GenerateNim()
GenerateLua()
GenerateNim()
GeneratePhp()
GeneratePython()
GenerateRust()
Expand Down
48 changes: 48 additions & 0 deletions goldens/lua/Galaxy.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--[[ Galaxy

Automatically generated by the FlatBuffers compiler, do not modify.
Or modify. I'm a message, not a cop.

flatc version: 23.5.9

Declared by : //basic.fbs
Rooting type : Universe (//basic.fbs)

--]]

local flatbuffers = require('flatbuffers')

local Galaxy = {}
local mt = {}

function Galaxy.New()
local o = {}
setmetatable(o, {__index = mt})
return o
end

function mt:Init(buf, pos)
self.view = flatbuffers.view.New(buf, pos)
end

function mt:NumStars()
local o = self.view:Offset(4)
if o ~= 0 then
return self.view:Get(flatbuffers.N.Int64, self.view.pos + o)
end
return 0
end

function Galaxy.Start(builder)
builder:StartObject(1)
end

function Galaxy.AddNumStars(builder, numStars)
builder:PrependInt64Slot(0, numStars, 0)
end

function Galaxy.End(builder)
return builder:EndObject()
end

return Galaxy
88 changes: 88 additions & 0 deletions goldens/lua/Universe.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
--[[ Universe

Automatically generated by the FlatBuffers compiler, do not modify.
Or modify. I'm a message, not a cop.

flatc version: 23.5.9

Declared by : //basic.fbs
Rooting type : Universe (//basic.fbs)

--]]

local __Galaxy = require('Galaxy')
local flatbuffers = require('flatbuffers')

local Universe = {}
local mt = {}

function Universe.New()
local o = {}
setmetatable(o, {__index = mt})
return o
end

function Universe.GetRootAsUniverse(buf, offset)
if type(buf) == "string" then
buf = flatbuffers.binaryArray.New(buf)
end

local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
local o = Universe.New()
o:Init(buf, n + offset)
return o
end

function mt:Init(buf, pos)
self.view = flatbuffers.view.New(buf, pos)
end

function mt:Age()
local o = self.view:Offset(4)
if o ~= 0 then
return self.view:Get(flatbuffers.N.Float64, self.view.pos + o)
end
return 0.0
end

function mt:Galaxies(j)
local o = self.view:Offset(6)
if o ~= 0 then
local x = self.view:Vector(o)
x = x + ((j-1) * 4)
x = self.view:Indirect(x)
local obj = __Galaxy.New()
obj:Init(self.view.bytes, x)
return obj
end
end

function mt:GalaxiesLength()
local o = self.view:Offset(6)
if o ~= 0 then
return self.view:VectorLen(o)
end
return 0
end

function Universe.Start(builder)
builder:StartObject(2)
end

function Universe.AddAge(builder, age)
builder:PrependFloat64Slot(0, age, 0.0)
end

function Universe.AddGalaxies(builder, galaxies)
builder:PrependUOffsetTRelativeSlot(1, galaxies, 0)
end

function Universe.StartGalaxiesVector(builder, numElems)
return builder:StartVector(4, numElems, 4)
end

function Universe.End(builder)
return builder:EndObject()
end

return Universe
26 changes: 26 additions & 0 deletions goldens/nim/Galaxy.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[ Galaxy
Automatically generated by the FlatBuffers compiler, do not modify.
Or modify. I'm a message, not a cop.

flatc version: 23.5.9

Declared by : //basic.fbs
Rooting type : Universe (//basic.fbs)
]#

import flatbuffers

type Galaxy* = object of FlatObj
func numStars*(self: Galaxy): int64 =
let o = self.tab.Offset(4)
if o != 0:
return Get[int64](self.tab, self.tab.Pos + o)
return 0
func `numStars=`*(self: var Galaxy, n: int64): bool =
return self.tab.MutateSlot(4, n)
proc GalaxyStart*(builder: var Builder) =
builder.StartObject(1)
proc GalaxyAddnumStars*(builder: var Builder, numStars: int64) =
builder.PrependSlot(0, numStars, default(int64))
proc GalaxyEnd*(builder: var Builder): uoffset =
return builder.EndObject()
46 changes: 46 additions & 0 deletions goldens/nim/Universe.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#[ Universe
Automatically generated by the FlatBuffers compiler, do not modify.
Or modify. I'm a message, not a cop.

flatc version: 23.5.9

Declared by : //basic.fbs
Rooting type : Universe (//basic.fbs)
]#

import Galaxy as Galaxy
import flatbuffers
import std/options

type Universe* = object of FlatObj
func age*(self: Universe): float64 =
let o = self.tab.Offset(4)
if o != 0:
return Get[float64](self.tab, self.tab.Pos + o)
return 0.0
func `age=`*(self: var Universe, n: float64): bool =
return self.tab.MutateSlot(4, n)
func galaxiesLength*(self: Universe): int =
let o = self.tab.Offset(6)
if o != 0:
return self.tab.VectorLen(o)
func galaxies*(self: Universe, j: int): Galaxy.Galaxy =
let o = self.tab.Offset(6)
if o != 0:
var x = self.tab.Vector(o)
x += j.uoffset * 4.uoffset
return Galaxy.Galaxy(tab: Vtable(Bytes: self.tab.Bytes, Pos: x))
func galaxies*(self: Universe): seq[Galaxy.Galaxy] =
let len = self.galaxiesLength
for i in countup(0, len - 1):
result.add(self.galaxies(i))
proc UniverseStart*(builder: var Builder) =
builder.StartObject(2)
proc UniverseAddage*(builder: var Builder, age: float64) =
builder.PrependSlot(0, age, default(float64))
proc UniverseAddgalaxies*(builder: var Builder, galaxies: uoffset) =
builder.PrependSlot(1, galaxies, default(uoffset))
proc UniverseStartgalaxiesVector*(builder: var Builder, numElems: uoffset) =
builder.StartVector(4, numElems, 4)
proc UniverseEnd*(builder: var Builder): uoffset =
return builder.EndObject()
7 changes: 6 additions & 1 deletion include/flatbuffers/code_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

namespace flatbuffers {

struct CodeGenOptions {
std::string output_path;
};

// A code generator interface for producing converting flatbuffer schema into
// code.
class CodeGenerator {
Expand Down Expand Up @@ -56,7 +60,8 @@ class CodeGenerator {

// Generate code from the provided `buffer` of given `length`. The buffer is a
// serialized reflection.fbs.
virtual Status GenerateCode(const uint8_t *buffer, int64_t length) = 0;
virtual Status GenerateCode(const uint8_t *buffer, int64_t length,
const CodeGenOptions &options) = 0;

virtual Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
Expand Down
10 changes: 5 additions & 5 deletions src/bfbs_gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ class BaseBfbsGenerator : public CodeGenerator {
virtual ~BaseBfbsGenerator() {}
BaseBfbsGenerator() : schema_(nullptr) {}

virtual Status GenerateFromSchema(
const reflection::Schema *schema) = 0;
virtual Status GenerateFromSchema(const reflection::Schema *schema,
const CodeGenOptions &options) = 0;

virtual uint64_t SupportedAdvancedFeatures() const = 0;

// Override of the Generator::GenerateCode method that does the initial
// deserialization and verification steps.
Status GenerateCode(const uint8_t *buffer,
int64_t length) FLATBUFFERS_OVERRIDE {
Status GenerateCode(const uint8_t *buffer, int64_t length,
const CodeGenOptions &options) FLATBUFFERS_OVERRIDE {
flatbuffers::Verifier verifier(buffer, static_cast<size_t>(length));
if (!reflection::VerifySchemaBuffer(verifier)) {
return FAILED_VERIFICATION;
Expand All @@ -124,7 +124,7 @@ class BaseBfbsGenerator : public CodeGenerator {
return FAILED_VERIFICATION;
}

Status status = GenerateFromSchema(schema_);
Status status = GenerateFromSchema(schema_, options);
schema_ = nullptr;
return status;
}
Expand Down
12 changes: 9 additions & 3 deletions src/bfbs_gen_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "bfbs_namer.h"

// The intermediate representation schema.
#include "flatbuffers/code_generator.h"
#include "flatbuffers/reflection.h"
#include "flatbuffers/reflection_generated.h"

Expand Down Expand Up @@ -78,7 +79,10 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
flatc_version_(flatc_version),
namer_(LuaDefaultConfig(), LuaKeywords()) {}

Status GenerateFromSchema(const r::Schema *schema) FLATBUFFERS_OVERRIDE {
Status GenerateFromSchema(const r::Schema *schema,
const CodeGenOptions &options)
FLATBUFFERS_OVERRIDE {
options_ = options;
if (!GenerateEnums(schema->enums())) { return ERROR; }
if (!GenerateObjects(schema->objects(), schema->root_table())) {
return ERROR;
Expand All @@ -89,7 +93,7 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
using BaseBfbsGenerator::GenerateCode;

Status GenerateCode(const Parser &, const std::string &,
const std::string &) FLATBUFFERS_OVERRIDE {
const std::string &) override {
return Status::NOT_IMPLEMENTED;
}

Expand Down Expand Up @@ -652,12 +656,14 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {

// TODO(derekbailey): figure out a save file without depending on util.h
EnsureDirExists(path);
const std::string file_name = path + "/" + namer_.File(name);
const std::string file_name = options_.output_path + path + "/" + namer_.File(name);
SaveFile(file_name.c_str(), code, false);
}

std::unordered_set<std::string> keywords_;
std::map<std::string, std::string> requires_;
CodeGenOptions options_;

const r::Object *current_obj_;
const r::Enum *current_enum_;
const std::string flatc_version_;
Expand Down
11 changes: 9 additions & 2 deletions src/bfbs_gen_nim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "bfbs_namer.h"

// The intermediate representation schema.
#include "flatbuffers/code_generator.h"
#include "flatbuffers/reflection.h"
#include "flatbuffers/reflection_generated.h"

Expand Down Expand Up @@ -95,7 +96,10 @@ class NimBfbsGenerator : public BaseBfbsGenerator {
flatc_version_(flatc_version),
namer_(NimDefaultConfig(), NimKeywords()) {}

Status GenerateFromSchema(const r::Schema *schema) FLATBUFFERS_OVERRIDE {
Status GenerateFromSchema(const r::Schema *schema,
const CodeGenOptions &options)
FLATBUFFERS_OVERRIDE {
options_ = options;
ForAllEnums(schema->enums(), [&](const r::Enum *enum_def) {
StartCodeBlock(enum_def);
GenerateEnum(enum_def);
Expand Down Expand Up @@ -671,12 +675,15 @@ class NimBfbsGenerator : public BaseBfbsGenerator {

// TODO(derekbailey): figure out a save file without depending on util.h
EnsureDirExists(path);
const std::string file_name = path + "/" + namer_.File(name);
const std::string file_name =
options_.output_path + path + "/" + namer_.File(name);
SaveFile(file_name.c_str(), code, false);
}

std::unordered_set<std::string> keywords_;
std::map<std::string, std::string> imports_;
CodeGenOptions options_;

const r::Object *current_obj_;
const r::Enum *current_enum_;
const std::string flatc_version_;
Expand Down
7 changes: 5 additions & 2 deletions src/flatc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,11 @@ std::unique_ptr<Parser> FlatCompiler::GenerateCode(const FlatCOptions &options,

// Prefer bfbs generators if present.
if (code_generator->SupportsBfbsGeneration()) {
const CodeGenerator::Status status =
code_generator->GenerateCode(bfbs_buffer, bfbs_length);
CodeGenOptions code_gen_options;
code_gen_options.output_path = options.output_path;

const CodeGenerator::Status status = code_generator->GenerateCode(
bfbs_buffer, bfbs_length, code_gen_options);
if (status != CodeGenerator::Status::OK) {
Error("Unable to generate " + code_generator->LanguageName() +
" for " + filebase + code_generator->status_detail +
Expand Down
Loading