From d8a8cfe0746f5975177f1232a71c3080b5abe1ba Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 14 Jan 2025 10:09:58 -0800 Subject: [PATCH] Merge mustache between fbcode and xplat Summary: Demonstrate feasibility of merging fbcode and xplat copies of thrift on a single target, `thrift/compiler/detail/mustache:mustache`. To this end: * Switch fbcode_builder from fbcode to xplat. * Map dependencies to the correct variants for fbcode. * Exclude mustache directory from target remapping. Once the merge is complete remapping rules will no longer be necessary at all, further reducing maintenance burden and complexity. * Remove one copy of mustache (~2.6kLOC). This also demonstrates that autodeps are working as expected after the fix in D67919546. Reviewed By: yoney Differential Revision: D67676056 fbshipit-source-id: ba3854c0997c4dd10f47a5e623381c3dadef4ecc --- build/fbcode_builder/manifests/fbthrift | 14 +- thrift/compiler/detail/mustache/LICENSE | 25 ---- thrift/compiler/detail/mustache/mstch.cpp | 45 ------ thrift/compiler/detail/mustache/mstch.h | 174 ---------------------- 4 files changed, 7 insertions(+), 251 deletions(-) delete mode 100644 thrift/compiler/detail/mustache/LICENSE delete mode 100644 thrift/compiler/detail/mustache/mstch.cpp delete mode 100644 thrift/compiler/detail/mustache/mstch.h diff --git a/build/fbcode_builder/manifests/fbthrift b/build/fbcode_builder/manifests/fbthrift index 964b97cc028..803163ccb39 100644 --- a/build/fbcode_builder/manifests/fbthrift +++ b/build/fbcode_builder/manifests/fbthrift @@ -1,6 +1,6 @@ [manifest] name = fbthrift -fbsource_path = fbcode/thrift +fbsource_path = xplat/thrift shipit_project = fbthrift shipit_fbcode_builder = true @@ -37,11 +37,11 @@ xxhash python [shipit.pathmap] -fbcode/thrift/public_tld = . -fbcode/thrift = thrift +xplat/thrift/public_tld = . +xplat/thrift = thrift [shipit.strip] -^fbcode/thrift/thrift-config\.h$ -^fbcode/thrift/perf/canary.py$ -^fbcode/thrift/perf/loadtest.py$ -^fbcode/thrift/.castle/.* +^xplat/thrift/thrift-config\.h$ +^xplat/thrift/perf/canary.py$ +^xplat/thrift/perf/loadtest.py$ +^xplat/thrift/.castle/.* diff --git a/thrift/compiler/detail/mustache/LICENSE b/thrift/compiler/detail/mustache/LICENSE deleted file mode 100644 index 40654420d3c..00000000000 --- a/thrift/compiler/detail/mustache/LICENSE +++ /dev/null @@ -1,25 +0,0 @@ -The source code contained in this folder is based on the original code by -Daniel Sipka (https://github.com/no1msd/mstch). The original license by Daniel -Sipka can be read below: - -The MIT License (MIT) - -Copyright (c) 2015 Daniel Sipka - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/thrift/compiler/detail/mustache/mstch.cpp b/thrift/compiler/detail/mustache/mstch.cpp deleted file mode 100644 index 7d1aa5554c9..00000000000 --- a/thrift/compiler/detail/mustache/mstch.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/* - -The source code contained in this file is based on the original code by -Daniel Sipka (https://github.com/no1msd/mstch). The original license by Daniel -Sipka can be read below: - -The MIT License (MIT) - -Copyright (c) 2015 Daniel Sipka - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -#include - -#include -#include -#include - -namespace apache::thrift::mstch { - -node::node(std::size_t i) : base(static_cast(i)) { - if (i > static_cast(std::numeric_limits::max())) { - throw std::overflow_error( - fmt::format("size_t greater than int max: {}", i)); - } -} - -} // namespace apache::thrift::mstch diff --git a/thrift/compiler/detail/mustache/mstch.h b/thrift/compiler/detail/mustache/mstch.h deleted file mode 100644 index 50fb6fe41bd..00000000000 --- a/thrift/compiler/detail/mustache/mstch.h +++ /dev/null @@ -1,174 +0,0 @@ -/* - -The source code contained in this file is based on the original code by -Daniel Sipka (https://github.com/no1msd/mstch). The original license by Daniel -Sipka can be read below: - -The MIT License (MIT) - -Copyright (c) 2015 Daniel Sipka - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace apache::thrift::mstch { - -namespace internal { - -template -class object_t { - public: - const N& at(const std::string& name) const { return methods_.at(name)(); } - - bool has(const std::string& name) const { - return (methods_.find(name) != methods_.end()); - } - - std::vector property_names() const { - std::vector result; - for (auto& entry : methods_) { - result.push_back(entry.first); - } - return result; - } - - protected: - // Volatile (uncached) methods are re-invoked every time their value is needed - // during a template evaluation. - // - // This is potentially useful if mutating state during evaluation, but has a - // performance cost. There are usually better ways to express such logic. - template - std::enable_if_t, N>> - register_volatile_method(std::string name, F method) { - do_register_method( - std::move(name), - [method = std::move(method), - uncache = std::optional()]() mutable -> const N& { - uncache = method(); - return *uncache; - }); - } - - // Cached methods are invoked at most once on the same object. - template - std::enable_if_t, N>> - register_cached_method(std::string name, F method) { - do_register_method( - std::move(name), - [method = std::move(method), - cache = std::optional()]() mutable -> const N& { - if (!cache) { - cache = method(); - } - return *cache; - }); - } - - template - void register_volatile_methods( - S* s, const std::unordered_map& methods) { - for (const auto& method : methods) { - register_volatile_method(std::move(method.first), [s, m = method.second] { - return (s->*m)(); - }); - } - } - - template - void register_cached_methods( - S* s, const std::unordered_map& methods) { - for (const auto& method : methods) { - register_cached_method(std::move(method.first), [s, m = method.second] { - return (s->*m)(); - }); - } - } - - private: - void do_register_method(std::string name, std::function method) { - auto result = methods_.emplace(std::move(name), std::move(method)); - if (!result.second) { - throw std::runtime_error( - "Method already registered: " + result.first->first); - } - } - - std::unordered_map> methods_; -}; - -template -using node_base = std::variant< - std::nullptr_t, - std::string, - int, - double, - bool, - std::shared_ptr>, - std::map, - std::vector>; - -} // namespace internal - -struct node : internal::node_base { - using base = internal::node_base; - - using base::base; - /* implicit */ node(std::string_view sv) : base(std::string(sv)) {} - /* implicit */ node(std::size_t i); - - // Equivalent to the int constructor brought in by `using base::base`, but - // having this here forces integer callers which are neither int nor size_t to - // intentionally convert to one or the other. Without the following line, - // calls like node(int64_t) would silently disambiguate to the size_t - // constructor, which is potentially surprising. - /* implicit */ node(int i) : base(i) {} - - template - decltype(auto) visit(Visitor&&... visitor) { - return std::visit(visitor..., static_cast(*this)); - } - template - decltype(auto) visit(Visitor&&... visitor) const { - return std::visit(visitor..., static_cast(*this)); - } -}; - -template -node make_shared_node(A&&... a) { - return node(std::make_shared(static_cast(a)...)); -} - -using object = internal::object_t; -using map = std::map; -using array = std::vector; - -} // namespace apache::thrift::mstch