blob: a4f23e5926d0765f853da58f1d25db5f1df7f7db [file] [log] [blame]
Marcel Hlopko42abfc82021-08-09 07:03:17 +00001// Part of the Crubit project, under the Apache License v2.0 with LLVM
2// Exceptions. See /LICENSE for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
Marcel Hlopko3962d182021-08-18 10:16:41 +00005#include "rs_bindings_from_cc/src_code_gen.h"
Marcel Hlopko42abfc82021-08-09 07:03:17 +00006
Marcel Hlopko42abfc82021-08-09 07:03:17 +00007#include <string>
8
Marcel Hlopko65978eb2022-04-08 07:40:18 -07009#include "common/ffi_types.h"
Lukasz Anforowicz6e8b3a72022-06-02 09:10:01 -070010#include "common/status_macros.h"
Marcel Hlopko42abfc82021-08-09 07:03:17 +000011#include "rs_bindings_from_cc/ir.h"
Lukasz Anforowiczcec7a8a2022-04-27 10:24:51 -070012#include "llvm/Support/FormatVariadic.h"
13#include "llvm/Support/JSON.h"
Marcel Hlopko42abfc82021-08-09 07:03:17 +000014
Marcel Hlopkof15e8ce2022-04-08 08:46:09 -070015namespace crubit {
Marcel Hlopko42abfc82021-08-09 07:03:17 +000016
Marcel Hlopko45fba972021-08-23 19:52:20 +000017// FFI equivalent of `Bindings`.
18struct FfiBindings {
19 FfiU8SliceBox rs_api;
20 FfiU8SliceBox rs_api_impl;
Michael VanBemmelf5cbdf42022-10-14 17:00:11 -070021 FfiU8SliceBox error_report;
Marcel Hlopko45fba972021-08-23 19:52:20 +000022};
Marcel Hlopko42abfc82021-08-09 07:03:17 +000023
Marcel Hlopko45fba972021-08-23 19:52:20 +000024// This function is implemented in Rust.
Lukasz Anforowicz54ff3182022-05-06 07:17:58 -070025extern "C" FfiBindings GenerateBindingsImpl(FfiU8Slice json,
Lukasz Anforowiczdd907702022-05-06 09:24:07 -070026 FfiU8Slice crubit_support_path,
Lukasz Anforowiczd7d68f02022-05-26 07:41:02 -070027 FfiU8Slice rustfmt_exe_path,
Michael VanBemmelf5cbdf42022-10-14 17:00:11 -070028 FfiU8Slice rustfmt_config_path,
29 bool generate_error_report);
Marcel Hlopko45fba972021-08-23 19:52:20 +000030
31// Creates `Bindings` instance from copied data from `ffi_bindings`.
Lukasz Anforowicz6e8b3a72022-06-02 09:10:01 -070032static absl::StatusOr<Bindings> MakeBindingsFromFfiBindings(
33 const FfiBindings& ffi_bindings) {
Marcel Hlopko45fba972021-08-23 19:52:20 +000034 Bindings bindings;
35
Marcel Hlopkoe936aac2021-08-24 20:52:27 +000036 const FfiU8SliceBox& rs_api = ffi_bindings.rs_api;
37 const FfiU8SliceBox& rs_api_impl = ffi_bindings.rs_api_impl;
Michael VanBemmelf5cbdf42022-10-14 17:00:11 -070038 const FfiU8SliceBox& error_report = ffi_bindings.error_report;
Marcel Hlopko45fba972021-08-23 19:52:20 +000039
40 bindings.rs_api = std::string(rs_api.ptr, rs_api.size);
Lukasz Anforowiczae9be082022-10-05 07:33:57 -070041 bindings.rs_api_impl = std::string(rs_api_impl.ptr, rs_api_impl.size);
Michael VanBemmelf5cbdf42022-10-14 17:00:11 -070042 bindings.error_report = std::string(error_report.ptr, error_report.size);
Marcel Hlopko45fba972021-08-23 19:52:20 +000043 return bindings;
44}
45
46// Deallocates given `ffi_bindings` instance that was created in Rust.
47static void FreeFfiBindings(FfiBindings ffi_bindings) {
48 FreeFfiU8SliceBox(ffi_bindings.rs_api);
49 FreeFfiU8SliceBox(ffi_bindings.rs_api_impl);
Michael VanBemmelf5cbdf42022-10-14 17:00:11 -070050 FreeFfiU8SliceBox(ffi_bindings.error_report);
Marcel Hlopko45fba972021-08-23 19:52:20 +000051}
52
Michael VanBemmelf5cbdf42022-10-14 17:00:11 -070053absl::StatusOr<Bindings> GenerateBindings(const IR& ir,
54 absl::string_view crubit_support_path,
55 absl::string_view rustfmt_exe_path,
56 absl::string_view rustfmt_config_path,
57 bool generate_error_report) {
Lukasz Anforowicz3b4be122022-03-16 00:09:35 +000058 std::string json = llvm::formatv("{0}", ir.ToJson());
Lukasz Anforowicz54ff3182022-05-06 07:17:58 -070059
60 FfiBindings ffi_bindings = GenerateBindingsImpl(
Lukasz Anforowiczdd907702022-05-06 09:24:07 -070061 MakeFfiU8Slice(json), MakeFfiU8Slice(crubit_support_path),
Michael VanBemmelf5cbdf42022-10-14 17:00:11 -070062 MakeFfiU8Slice(rustfmt_exe_path), MakeFfiU8Slice(rustfmt_config_path),
63 generate_error_report);
Lukasz Anforowicz6e8b3a72022-06-02 09:10:01 -070064 CRUBIT_ASSIGN_OR_RETURN(Bindings bindings,
65 MakeBindingsFromFfiBindings(ffi_bindings));
Marcel Hlopko45fba972021-08-23 19:52:20 +000066 FreeFfiBindings(ffi_bindings);
67 return bindings;
Marcel Hlopko42abfc82021-08-09 07:03:17 +000068}
69
Marcel Hlopkof15e8ce2022-04-08 08:46:09 -070070} // namespace crubit