|
3 | 3 | // |
4 | 4 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
5 | 5 |
|
6 | | -use std::{fs::File, io::Write}; |
| 6 | +use std::path::PathBuf; |
| 7 | + |
| 8 | +use cxx_qt_build::{CxxQtBuilder, Interface}; |
| 9 | + |
| 10 | +fn header_dir() -> PathBuf { |
| 11 | + PathBuf::from(std::env::var("OUT_DIR").unwrap()) |
| 12 | + .join("include") |
| 13 | + .join("cxx-qt") |
| 14 | +} |
| 15 | + |
| 16 | +fn write_headers() { |
| 17 | + println!("cargo::rerun-if-changed=include/"); |
| 18 | + std::fs::create_dir_all(header_dir()).expect("Failed to create include directory"); |
| 19 | + |
| 20 | + for file_path in [ |
| 21 | + "connection.h", |
| 22 | + "signalhandler.h", |
| 23 | + "thread.h", |
| 24 | + "threading.h", |
| 25 | + "type.h", |
| 26 | + ] { |
| 27 | + println!("cargo::rerun-if-changed=include/{file_path}"); |
| 28 | + std::fs::copy(format!("include/{file_path}"), header_dir().join(file_path)) |
| 29 | + .expect("Failed to copy header file!"); |
| 30 | + } |
| 31 | +} |
7 | 32 |
|
8 | 33 | fn main() { |
9 | | - let qtbuild = qt_build_utils::QtBuild::new(vec!["Core".to_owned()]) |
10 | | - .expect("Could not find Qt installation"); |
| 34 | + write_headers(); |
| 35 | + |
| 36 | + let interface = Interface::default() |
| 37 | + .export_include_prefixes([]) |
| 38 | + .export_include_directory(header_dir(), "cxx-qt"); |
11 | 39 |
|
12 | | - // Required for tests |
13 | | - qt_build_utils::setup_linker(); |
| 40 | + let mut builder = CxxQtBuilder::library(interface); |
14 | 41 |
|
15 | 42 | let cpp_files = ["src/connection.cpp"]; |
16 | 43 | let rust_bridges = ["src/connection.rs"]; |
17 | 44 |
|
18 | 45 | for bridge in &rust_bridges { |
19 | | - println!("cargo:rerun-if-changed={bridge}"); |
| 46 | + builder = builder.file(bridge); |
20 | 47 | } |
21 | 48 |
|
22 | | - let mut builder = cxx_build::bridges(rust_bridges); |
| 49 | + builder = builder.cc_builder(move |cc| { |
| 50 | + for cpp_file in &cpp_files { |
| 51 | + cc.file(cpp_file); |
| 52 | + println!("cargo::rerun-if-changed={cpp_file}"); |
| 53 | + } |
| 54 | + }); |
23 | 55 |
|
24 | | - qtbuild.cargo_link_libraries(&mut builder); |
25 | | - |
26 | | - for cpp_file in &cpp_files { |
27 | | - builder.file(cpp_file); |
28 | | - println!("cargo:rerun-if-changed={cpp_file}"); |
29 | | - } |
30 | | - |
31 | | - // Write this library's manually written C++ headers to files and add them to include paths |
32 | | - let out_dir = std::env::var("OUT_DIR").unwrap(); |
33 | | - let directory = format!("{out_dir}/cxx-qt"); |
34 | | - std::fs::create_dir_all(&directory).expect("Could not create cxx-qt header directory"); |
35 | | - // Note we only need connection.h for now, but lets move all headers to be consistent |
36 | | - // ensure src/lib write_headers is consistent |
37 | | - for (file_contents, file_name) in [ |
38 | | - (include_str!("include/connection.h"), "connection.h"), |
39 | | - (include_str!("include/signalhandler.h"), "signalhandler.h"), |
40 | | - (include_str!("include/thread.h"), "thread.h"), |
41 | | - (include_str!("include/threading.h"), "threading.h"), |
42 | | - (include_str!("include/type.h"), "type.h"), |
43 | | - ] { |
44 | | - let h_path = format!("{directory}/{file_name}"); |
45 | | - let mut header = File::create(h_path).expect("Could not create cxx-qt header"); |
46 | | - write!(header, "{file_contents}").expect("Could not write cxx-qt header"); |
47 | | - } |
48 | | - builder.include(out_dir); |
49 | | - builder.includes(qtbuild.include_paths()); |
50 | | - |
51 | | - // Note, ensure our settings stay in sync across cxx-qt, cxx-qt-build, and cxx-qt-lib |
52 | | - builder.cpp(true); |
53 | | - builder.std("c++17"); |
54 | | - // MSVC |
55 | | - builder.flag_if_supported("/Zc:__cplusplus"); |
56 | | - builder.flag_if_supported("/permissive-"); |
57 | | - builder.flag_if_supported("/bigobj"); |
58 | | - // MinGW requires big-obj otherwise debug builds fail |
59 | | - builder.flag_if_supported("-Wa,-mbig-obj"); |
60 | | - |
61 | | - builder.compile("cxx-qt"); |
| 56 | + builder.build(); |
62 | 57 | } |
0 commit comments