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
20#![deny(clippy::allow_attributes)]
21
22use std::{fs::OpenOptions, io::Write, path::Path};
23
24fn main() -> Result<(), Box<dyn std::error::Error>> {
25    let proto_dir = Path::new("../format");
26    let proto_path = Path::new("../format/Flight.proto");
27
28    tonic_prost_build::configure()
29        // protoc in Ubuntu builder needs this option
30        .protoc_arg("--experimental_allow_proto3_optional")
31        .out_dir("src")
32        .compile_with_config(prost_config(), &[proto_path], &[proto_dir])?;
33
34    let buffer = std::fs::read_to_string("src/arrow.flight.protocol.rs")?;
35    // append warning that file was auto-generated
36    let mut file = OpenOptions::new()
37        .write(true)
38        .truncate(true)
39        .open("src/arrow.flight.protocol.rs")?;
40    file.write_all("// This file was automatically generated through the build.rs script, and should not be edited.\n\n".as_bytes())?;
41    file.write_all(buffer.as_bytes())?;
42
43    let proto_dir = Path::new("../format");
44    let proto_path = Path::new("../format/FlightSql.proto");
45
46    tonic_prost_build::configure()
47        // protoc in Ubuntu builder needs this option
48        .protoc_arg("--experimental_allow_proto3_optional")
49        .out_dir("src/sql")
50        .compile_with_config(prost_config(), &[proto_path], &[proto_dir])?;
51
52    let buffer = std::fs::read_to_string("src/sql/arrow.flight.protocol.sql.rs")?;
53    // append warning that file was auto-generate
54    let mut file = OpenOptions::new()
55        .write(true)
56        .truncate(true)
57        .open("src/sql/arrow.flight.protocol.sql.rs")?;
58    file.write_all("// This file was automatically generated through the build.rs script, and should not be edited.\n\n".as_bytes())?;
59    file.write_all(buffer.as_bytes())?;
60
61    // Prost currently generates an empty file, this was fixed but then reverted
62    // https://github.com/tokio-rs/prost/pull/639
63    let google_protobuf_rs = Path::new("src/sql/google.protobuf.rs");
64    if google_protobuf_rs.exists() && google_protobuf_rs.metadata().unwrap().len() == 0 {
65        std::fs::remove_file(google_protobuf_rs).unwrap();
66    }
67
68    // As the proto file is checked in, the build should not fail if the file is not found
69    Ok(())
70}
71
72fn prost_config() -> prost_build::Config {
73    let mut config = prost_build::Config::new();
74    config.bytes([".arrow"]);
75    config
76}