Skip to content

Commit

Permalink
Bundle remaining annotation files
Browse files Browse the repository at this point in the history
Summary:
Generalizes the mechanism in place for scope.thrift to all files without hardcoding a list.
Uses the same mechanism to pass in the directory name that is used in build_templates.cc instead of using source_manager both for simplicity and for eventual OSS compatibility.

Reviewed By: yoney

Differential Revision: D68128288

fbshipit-source-id: 4191f46e0496e6e3844c7fa7aaa7c40d02d5688b
  • Loading branch information
iahs authored and facebook-github-bot committed Jan 15, 2025
1 parent de1f90b commit 60f4d5c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
51 changes: 40 additions & 11 deletions thrift/annotation/build_annotations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@
* limitations under the License.
*/

#include <filesystem>
#include <fstream>
#include <map>
#include <fmt/core.h>
#include "thrift/compiler/source_location.h"

int main() {
namespace fs = std::filesystem;

int main(int argc, char** argv) {
if (argc != 2) {
fmt::print(stderr, "usage: {} templates-dir\n", argv[0]);
return 1;
}

const auto path = fs::path(argv[1]);

// Print the header.
fmt::print(
"// {}generated\n"
Expand All @@ -26,20 +37,38 @@ int main() {
"\n",
'@');

// Read the file.
apache::thrift::compiler::source_manager sm;
std::string_view content = sm.get_file("scope.thrift")->text;
content.remove_suffix(1); // Remove trailing NUL.
// Read the files.
std::map<std::string, std::string> files;
for (const auto& entry : fs::directory_iterator(path)) {
const auto& filepath = entry.path();
if (filepath.extension() == ".thrift" && fs::is_regular_file(filepath)) {
std::ifstream file(filepath);
auto buf = std::string(
std::istreambuf_iterator<char>(file),
std::istreambuf_iterator<char>());
files[fmt::format(
"thrift/annotation/{}", filepath.filename().generic_string())] =
std::move(buf);
}
}

// Print the content.
fmt::print(
"namespace apache::thrift::detail {{\n"
"\n"
"const std::string_view bundled_annotations::scope_file_content() {{\n"
" return R\"{0}({1}){0}\";\n"
"const std::map<std::string, std::string>& bundled_annotations::files() {{\n"
" static const std::map<std::string, std::string> files = {{\n");
for (const auto& [path, content] : files) {
fmt::print(
" {{\"{1}\", R\"{0}({2}){0}\"}},\n",
"__FBTHRIFT_TAG__",
path,
content);
}
fmt::print(
" }};\n"
" return files;\n"
"}}\n"
"\n"
"}} // namespace apache::thrift::detail\n",
"__FBTHRIFT_TAG__",
content);
"}} // namespace apache::thrift::detail\n");
}
5 changes: 3 additions & 2 deletions thrift/annotation/bundled_annotations.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* limitations under the License.
*/

#include <string_view>
#include <map>
#include <string>

namespace apache::thrift::detail {

struct bundled_annotations {
static const std::string_view scope_file_content();
static const std::map<std::string, std::string>& files();
};

} // namespace apache::thrift::detail

0 comments on commit 60f4d5c

Please sign in to comment.