-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Documentation for Bazel rules #8388
Comments
FlatBuffers Bazel Integration GuideThis guide will walk you through the process of integrating FlatBuffers Bazel rules into your project. We'll create a simple example project and provide step-by-step instructions for novice Bazel users. Prerequisites
Example Project Structure
Step-by-Step Instructions1. Set up your WORKSPACE fileIn your project's root directory, create or modify the load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "com_github_google_flatbuffers",
strip_prefix = "flatbuffers-23.5.26",
urls = ["https://github.com/google/flatbuffers/archive/v23.5.26.tar.gz"],
) Replace the version number with the latest FlatBuffers release. 2. Create your FlatBuffers schemaIn
3. Set up your BUILD filesIn the root package(default_visibility = ["//visibility:public"]) In load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_library_public")
flatbuffer_library_public(
name = "monster_fbs",
srcs = ["monster.fbs"],
language_flag = "--cpp",
)
cc_binary(
name = "monster_example",
srcs = ["monster_example.cc"],
deps = [":monster_fbs"],
) 4. Use the generated codeCreate #include "src/monster_generated.h"
#include <iostream>
int main() {
flatbuffers::FlatBufferBuilder builder(1024);
auto name = builder.CreateString("Orc");
auto monster = MyGame::CreateMonster(builder, name, 80, 100);
builder.Finish(monster);
auto monster_buffer = builder.GetBufferPointer();
auto monster_size = builder.GetSize();
auto verified_monster = flatbuffers::GetRoot<MyGame::Monster>(monster_buffer);
std::cout << "Monster name: " << verified_monster->name()->str() << std::endl;
std::cout << "Monster HP: " << verified_monster->hp() << std::endl;
std::cout << "Monster Mana: " << verified_monster->mana() << std::endl;
return 0;
} 5. Build and runFrom your project root, run: bazel build //src:monster_example
bazel run //src:monster_example Common Use Cases
Potential Pitfalls
Advanced UsageFor more advanced usage, including custom build flags and multi-language support, refer to the Remember to regenerate your FlatBuffers code whenever you modify your |
Following on from the work done in Request for Bazel rules, could you add documentation on how to use these new rules from our own projects? Examples are very scarce and as a novice Bazel user I can't figure out how to make use of these rules without good examples.
Hopefully this is an easy request.
The text was updated successfully, but these errors were encountered: