xref: /tonic/codegen/src/main.rs (revision 44aa46db)
1 use std::path::PathBuf;
2 
3 fn main() {
4     // tonic-health
5     codegen(
6         &PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
7             .parent()
8             .unwrap()
9             .join("tonic-health"),
10         &["proto/health.proto"],
11         &["proto"],
12         &PathBuf::from("src/generated"),
13         &PathBuf::from("src/generated/grpc_health_v1.bin"),
14         true,
15         true,
16     );
17 
18     // tonic-reflection
19     codegen(
20         &PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
21             .parent()
22             .unwrap()
23             .join("tonic-reflection"),
24         &["proto/reflection.proto"],
25         &["proto"],
26         &PathBuf::from("src/generated"),
27         &PathBuf::from("src/generated/reflection_v1alpha1.bin"),
28         true,
29         true,
30     );
31 
32     // tonic-types
33     codegen(
34         &PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
35             .parent()
36             .unwrap()
37             .join("tonic-types"),
38         &["proto/status.proto", "proto/error_details.proto"],
39         &["proto"],
40         &PathBuf::from("src/generated"),
41         &PathBuf::from("src/generated/types.bin"),
42         false,
43         false,
44     );
45 }
46 
47 fn codegen(
48     root_dir: &PathBuf,
49     iface_files: &[&str],
50     include_dirs: &[&str],
51     out_dir: &PathBuf,
52     file_descriptor_set_path: &PathBuf,
53     build_client: bool,
54     build_server: bool,
55 ) {
56     let iface_files: Vec<PathBuf> = iface_files
57         .into_iter()
58         .map(|&path| root_dir.join(path))
59         .collect();
60 
61     let include_dirs: Vec<PathBuf> = include_dirs
62         .into_iter()
63         .map(|&path| root_dir.join(path))
64         .collect();
65     let out_dir = root_dir.join(out_dir);
66     let file_descriptor_set_path = root_dir.join(file_descriptor_set_path);
67 
68     tonic_build::configure()
69         .build_client(build_client)
70         .build_server(build_server)
71         .out_dir(&out_dir)
72         .file_descriptor_set_path(file_descriptor_set_path)
73         .compile(&iface_files, &include_dirs)
74         .unwrap();
75 }
76