diff --git a/third-party/thrift/src/thrift/annotation/build_annotations.cc b/third-party/thrift/src/thrift/annotation/build_annotations.cc new file mode 100644 index 0000000000000..5f9a13224bfa1 --- /dev/null +++ b/third-party/thrift/src/thrift/annotation/build_annotations.cc @@ -0,0 +1,45 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "thrift/compiler/source_location.h" + +int main() { + // Print the header. + fmt::print( + "// {}generated\n" + "\n" + "#include \n" + "\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. + + // 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" + "}}\n" + "\n" + "}} // namespace apache::thrift::detail\n", + "__FBTHRIFT_TAG__", + content); +} diff --git a/third-party/thrift/src/thrift/annotation/bundled_annotations.h b/third-party/thrift/src/thrift/annotation/bundled_annotations.h new file mode 100644 index 0000000000000..447cc14630b03 --- /dev/null +++ b/third-party/thrift/src/thrift/annotation/bundled_annotations.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +namespace apache::thrift::detail { + +struct bundled_annotations { + static const std::string_view scope_file_content(); +}; + +} // namespace apache::thrift::detail diff --git a/third-party/thrift/src/thrift/compiler/CMakeLists.txt b/third-party/thrift/src/thrift/compiler/CMakeLists.txt index 9c1c4723b94c3..d669113fce3ec 100644 --- a/third-party/thrift/src/thrift/compiler/CMakeLists.txt +++ b/third-party/thrift/src/thrift/compiler/CMakeLists.txt @@ -200,6 +200,7 @@ add_executable( main.cc compiler.cc ) +target_compile_definitions(thrift1 PRIVATE THRIFT_OSS) target_link_libraries( thrift1 compiler_ast diff --git a/third-party/thrift/src/thrift/compiler/compiler.cc b/third-party/thrift/src/thrift/compiler/compiler.cc index 68f4d0cd8a309..c6328e9bb1e7e 100644 --- a/third-party/thrift/src/thrift/compiler/compiler.cc +++ b/third-party/thrift/src/thrift/compiler/compiler.cc @@ -38,6 +38,7 @@ #include +#include #include #include #include @@ -50,7 +51,6 @@ namespace apache::thrift::compiler { namespace { - /** * Flags to control code generation */ @@ -733,7 +733,7 @@ std::unique_ptr parse_and_mutate( } // Load standard library if available. - static const std::string schema_path = "thrift/lib/thrift/schema.thrift"; + const std::string schema_path = "thrift/lib/thrift/schema.thrift"; auto found_or_error = source_mgr.find_include_file(schema_path, "", pparams.incl_searchpath); if (found_or_error.index() == 0) { @@ -767,6 +767,19 @@ std::unique_ptr parse_and_mutate( std::get<1>(found_or_error)); } +#ifndef THRIFT_OSS + const std::string scope_path = "thrift/annotation/scope.thrift"; + found_or_error = + source_mgr.find_include_file(scope_path, "", pparams.incl_searchpath); + if (found_or_error.index() != 0) { + // Fall back to the bundled annotation file. + std::string_view content = + apache::thrift::detail::bundled_annotations::scope_file_content(); + source_mgr.add_virtual_file( + scope_path, std::string(content.data(), content.size())); + } +#endif + // C++ codegen inserts an empty const if this is false. Other languages may // dynamically determine whether the schema const exists. if (gparams.inject_schema_const) {