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