Skip to main content

gen/
main.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Generates the Rust bindings for the Arrow Flight protobuf definitions.
19
20use std::{fs::OpenOptions, io::Write, path::Path};
21
22fn main() -> Result<(), Box<dyn std::error::Error>> {
23    let proto_dir = Path::new("../format");
24    let proto_path = Path::new("../format/Flight.proto");
25
26    tonic_prost_build::configure()
27        // protoc in Ubuntu builder needs this option
28        .protoc_arg("--experimental_allow_proto3_optional")
29        .out_dir("src")
30        .compile_with_config(prost_config(), &[proto_path], &[proto_dir])?;
31
32    let buffer = std::fs::read_to_string("src/arrow.flight.protocol.rs")?;
33    // append warning that file was auto-generated
34    let mut file = OpenOptions::new()
35        .write(true)
36        .truncate(true)
37        .open("src/arrow.flight.protocol.rs")?;
38    file.write_all("// This file was automatically generated through the build.rs script, and should not be edited.\n\n".as_bytes())?;
39    file.write_all(buffer.as_bytes())?;
40
41    let proto_dir = Path::new("../format");
42    let proto_path = Path::new("../format/FlightSql.proto");
43
44    tonic_prost_build::configure()
45        // protoc in Ubuntu builder needs this option
46        .protoc_arg("--experimental_allow_proto3_optional")
47        .out_dir("src/sql")
48        .compile_with_config(prost_config(), &[proto_path], &[proto_dir])?;
49
50    let buffer = std::fs::read_to_string("src/sql/arrow.flight.protocol.sql.rs")?;
51    // append warning that file was auto-generate
52    let mut file = OpenOptions::new()
53        .write(true)
54        .truncate(true)
55        .open("src/sql/arrow.flight.protocol.sql.rs")?;
56    file.write_all("// This file was automatically generated through the build.rs script, and should not be edited.\n\n".as_bytes())?;
57    file.write_all(buffer.as_bytes())?;
58
59    // Prost currently generates an empty file, this was fixed but then reverted
60    // https://github.com/tokio-rs/prost/pull/639
61    let google_protobuf_rs = Path::new("src/sql/google.protobuf.rs");
62    if google_protobuf_rs.exists() && google_protobuf_rs.metadata().unwrap().len() == 0 {
63        std::fs::remove_file(google_protobuf_rs).unwrap();
64    }
65
66    // As the proto file is checked in, the build should not fail if the file is not found
67    Ok(())
68}
69
70fn prost_config() -> prost_build::Config {
71    let mut config = prost_build::Config::new();
72    config.bytes([".arrow"]);
73    config
74}