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