~nicohman/signal-rs

ref: 6edf1d75be03d89940921ef0f84c0ee550d3c19e signal-rs/src/build.rs -rw-r--r-- 6.2 KiB
6edf1d75 — nicohman Fix syntax error in RegistrationComplete 1 year, 2 months ago
                                                                                
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* Copyright (C) 2018 Olivier Goffart <ogoffart@woboq.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
extern crate cpp_build;
use std::env;
use std::fs;
use std::fs::DirEntry;
use std::path::PathBuf;
use std::process::Command;

fn qmake_query(qmake: &str, args: &str, var: &str) -> String {
    let mut qmake_cmd_list: Vec<&str> = qmake.split(' ').collect();
    qmake_cmd_list.push("-query");
    qmake_cmd_list.push(var);
    if !args.is_empty() {
        let mut qmake_args_list: Vec<&str> = args.split(' ').collect();
        qmake_cmd_list.append(&mut qmake_args_list);
    }
    String::from_utf8(
        Command::new(qmake_cmd_list[0])
            .args(&qmake_cmd_list[1..])
            .output()
            .expect("Failed to execute qmake. Make sure 'qmake' is in your path")
            .stdout,
    )
    .expect("UTF-8 conversion failed")
}

/// Gets environment variables by name
fn env_var(key: &str) -> Result<String, env::VarError> {
    Ok(String::from(env::var(key)?))
}

/// Generates the shell command to call for qmake
fn qmake_call() -> String {
    env_var("QMAKE").unwrap_or(String::from("qmake"))
}

/// Generates the arguments for the call to qmake
fn qmake_args() -> String {
    env_var("QMAKE_ARGS").unwrap_or(String::new())
}

/// Generate gettext translation files
fn gettext() {
    let pot_file = "po/signal-rs.nicohman.pot";
    let source_files = source_files();

    let mut child = Command::new("xgettext")
        .args(&[
            &format!("--output={}", pot_file),
            "--language=javascript",
            "--qt",
            "--keyword=tr",
            "--keyword=tr:1,2",
            "--add-comments=i18n",
        ])
        .args(&source_files)
        .spawn()
        .unwrap();

    let exit_status = child.wait().unwrap();
    assert!(exit_status.code() == Some(0));

    for po_file in po_files() {
        let mut child = Command::new("msgmerge")
            .args(&["--update", &po_file.to_str().unwrap(), pot_file])
            .spawn()
            .unwrap();

        let exit_status = child.wait().unwrap();
        assert!(exit_status.code() == Some(0));

        let mo_dir = format!(
            "share/locale/{}/LC_MESSAGES",
            po_file.file_stem().unwrap().to_str().unwrap()
        );
        fs::create_dir_all(&mo_dir).unwrap();

        let mo_file = format!("{}/signal-rs.nicohman.mo", mo_dir);

        let mut child = Command::new("msgfmt")
            .args(&[&po_file.to_str().unwrap(), "-o", &mo_file])
            .spawn()
            .unwrap();

        let exit_status = child.wait().unwrap();
        assert!(exit_status.code() == Some(0));
    }
}

/// Obtains a list of all QML files
fn source_files() -> Vec<PathBuf> {
    // Directory in which to search for QML files and file extension
    walk_dir(PathBuf::from("qml"), "qml")
}

/// Recursively searches for files in a directory and
/// returns a list of paths to the files
fn walk_dir<T>(dir: PathBuf, ext: T) -> Vec<PathBuf>
where
    T: AsRef<str>,
{
    let extension = ext.as_ref().to_string();
    let mut files: Vec<PathBuf> = Vec::new();
    let entries: Vec<DirEntry> = fs::read_dir(dir).unwrap().filter_map(|e| e.ok()).collect();
    for entry in entries.iter() {
        if entry.file_type().unwrap().is_dir() {
            files.append(&mut walk_dir(entry.path(), &extension));
        } else {
            match entry.path().extension() {
                Some(file_ext) => {
                    if file_ext.to_str().unwrap().to_string() == extension {
                        files.push(entry.path())
                    }
                }
                None => {}
            }
        }
    }
    files
}

/// Obtains a list of all translation files
fn po_files() -> Vec<PathBuf> {
    // Directory in which to search for translation files
    fs::read_dir("po")
        .unwrap()
        .filter_map(|e| e.ok())
        .map(|e| e.path())
        .filter(|p| p.extension().map(|ext| ext == "po").unwrap_or(false))
        .collect()
}

fn main() {
    gettext();

    let qmake_cmd = qmake_call();
    let args = qmake_args();

    let qt_include_path = qmake_query(&qmake_cmd, &args, "QT_INSTALL_HEADERS");
    let qt_library_path = qmake_query(&qmake_cmd, &args, "QT_INSTALL_LIBS");

    cpp_build::Config::new()
        .include(qt_include_path.clone().trim())
        .build("src/main.rs");

    let macos_lib_search = if cfg!(target_os = "macos") {
        "=framework"
    } else {
        ""
    };
    let macos_lib_framework = if cfg!(target_os = "macos") { "" } else { "5" };

    println!(
        "cargo:rustc-link-search{}={}",
        macos_lib_search,
        qt_library_path.trim()
    );
    println!(
        "cargo:rustc-link-lib{}=Qt{}Widgets",
        macos_lib_search, macos_lib_framework
    );
    println!(
        "cargo:rustc-link-lib{}=Qt{}Gui",
        macos_lib_search, macos_lib_framework
    );
    println!(
        "cargo:rustc-link-lib{}=Qt{}Core",
        macos_lib_search, macos_lib_framework
    );
    println!(
        "cargo:rustc-link-lib{}=Qt{}Quick",
        macos_lib_search, macos_lib_framework
    );
    println!(
        "cargo:rustc-link-lib{}=Qt{}Qml",
        macos_lib_search, macos_lib_framework
    );
    println!(
        "cargo:rustc-link-lib{}=Qt{}QuickControls2",
        macos_lib_search, macos_lib_framework
    );
}