Skip to main content

gen/
main.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Generates the Rust bindings for the Arrow Flight protobuf definitions.
6
7use std::{fs::OpenOptions, io::Write, path::Path};
8
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let proto_dir = Path::new("../format");
11    let proto_path = Path::new("../format/Flight.proto");
12
13    tonic_prost_build::configure()
14        // protoc in Ubuntu builder needs this option
15        .protoc_arg("--experimental_allow_proto3_optional")
16        .out_dir("src")
17        .compile_with_config(prost_config(), &[proto_path], &[proto_dir])?;
18
19    let buffer = std::fs::read_to_string("src/arrow.flight.protocol.rs")?;
20    // append warning that file was auto-generated
21    let mut file = OpenOptions::new()
22        .write(true)
23        .truncate(true)
24        .open("src/arrow.flight.protocol.rs")?;
25    file.write_all("// This file was automatically generated through the build.rs script, and should not be edited.\n\n".as_bytes())?;
26    file.write_all(buffer.as_bytes())?;
27
28    let proto_dir = Path::new("../format");
29    let proto_path = Path::new("../format/FlightSql.proto");
30
31    tonic_prost_build::configure()
32        // protoc in Ubuntu builder needs this option
33        .protoc_arg("--experimental_allow_proto3_optional")
34        .out_dir("src/sql")
35        .compile_with_config(prost_config(), &[proto_path], &[proto_dir])?;
36
37    let buffer = std::fs::read_to_string("src/sql/arrow.flight.protocol.sql.rs")?;
38    // append warning that file was auto-generate
39    let mut file = OpenOptions::new()
40        .write(true)
41        .truncate(true)
42        .open("src/sql/arrow.flight.protocol.sql.rs")?;
43    file.write_all("// This file was automatically generated through the build.rs script, and should not be edited.\n\n".as_bytes())?;
44    file.write_all(buffer.as_bytes())?;
45
46    // Prost currently generates an empty file, this was fixed but then reverted
47    // https://github.com/tokio-rs/prost/pull/639
48    let google_protobuf_rs = Path::new("src/sql/google.protobuf.rs");
49    if google_protobuf_rs.exists() && google_protobuf_rs.metadata().unwrap().len() == 0 {
50        std::fs::remove_file(google_protobuf_rs).unwrap();
51    }
52
53    // As the proto file is checked in, the build should not fail if the file is not found
54    Ok(())
55}
56
57fn prost_config() -> prost_build::Config {
58    let mut config = prost_build::Config::new();
59    config.bytes([".arrow"]);
60    config
61}