Skip to content

Commit 5ef1971

Browse files
committed
convert all programs to rust programs calling mains from cpp lib
1 parent 9b8c069 commit 5ef1971

25 files changed

+285
-223
lines changed

build.rs

+28-17
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,51 @@
1+
use std::collections::HashMap;
2+
use std::env;
13
use std::error::Error;
24
use std::fs;
35
use std::io::{BufRead, BufReader};
6+
use std::path::Path;
47

58
fn main() -> Result<(), Box<dyn Error>> {
6-
let (app_version, config_dir, lib_dir) = {
9+
let conf = {
10+
let mut conf = HashMap::new();
11+
712
let f = fs::File::open("conf.pri")?;
813
let reader = BufReader::new(f);
914

10-
let mut av = String::new();
11-
let mut cd = String::new();
12-
let mut ld = String::new();
15+
const CONF_VARS: &[&str] = &["APP_VERSION", "CONFIGDIR", "LIBDIR", "QT_INSTALL_LIBS"];
1316

1417
for line in reader.lines() {
1518
let line = line?;
1619

17-
if line.starts_with("APP_VERSION =") {
18-
let pos = line.find('=').unwrap();
19-
20-
av = line[(pos + 1)..].trim().into();
21-
} else if line.starts_with("CONFIGDIR =") {
22-
let pos = line.find('=').unwrap();
23-
24-
cd = line[(pos + 1)..].trim().into();
25-
} else if line.starts_with("LIBDIR =") {
26-
let pos = line.find('=').unwrap();
27-
28-
ld = line[(pos + 1)..].trim().into();
20+
for name in CONF_VARS {
21+
if line.starts_with(name) {
22+
let pos = line
23+
.find('=')
24+
.unwrap_or_else(|| panic!("no '=' character following var {}", name));
25+
conf.insert(name.to_string(), line[(pos + 1)..].trim().to_string());
26+
break;
27+
}
2928
}
3029
}
3130

32-
(av, cd, ld)
31+
conf
3332
};
3433

34+
let app_version = conf.get("APP_VERSION").unwrap();
35+
let config_dir = conf.get("CONFIGDIR").unwrap();
36+
let lib_dir = conf.get("LIBDIR").unwrap();
37+
let qt_install_libs = conf.get("QT_INSTALL_LIBS").unwrap();
38+
39+
let out_dir = env::var("OUT_DIR").unwrap();
40+
let cpp_lib_dir = fs::canonicalize(Path::new("src/cpp")).unwrap();
41+
3542
println!("cargo:rustc-env=APP_VERSION={}", app_version);
3643
println!("cargo:rustc-env=CONFIG_DIR={}", config_dir);
3744
println!("cargo:rustc-env=LIB_DIR={}", lib_dir);
3845

46+
println!("cargo:rustc-link-search=framework={}", qt_install_libs);
47+
println!("cargo:rustc-link-search={}", out_dir);
48+
println!("cargo:rustc-link-search={}", cpp_lib_dir.display());
49+
3950
Ok(())
4051
}

configure

+9
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,15 @@ public:
496496
497497
conf->addIncludePath("\$\$PWD/src");
498498
499+
QByteArray out;
500+
if(conf->doCommand(conf->qmake_path, QStringList() << "-query" << "QT_INSTALL_LIBS", &out) != 0)
501+
{
502+
return false;
503+
}
504+
505+
QString qtlibdir = QString::fromUtf8(out).trimmed();
506+
conf->addExtra(QString("QT_INSTALL_LIBS = %1").arg(qtlibdir));
507+
499508
QFile file("src/config.h");
500509
if(file.open(QIODevice::WriteOnly | QIODevice::Text))
501510
{

qcm/conf.qcm

+9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ public:
7676

7777
conf->addIncludePath("$$PWD/src");
7878

79+
QByteArray out;
80+
if(conf->doCommand(conf->qmake_path, QStringList() << "-query" << "QT_INSTALL_LIBS", &out) != 0)
81+
{
82+
return false;
83+
}
84+
85+
QString qtlibdir = QString::fromUtf8(out).trimmed();
86+
conf->addExtra(QString("QT_INSTALL_LIBS = %1").arg(qtlibdir));
87+
7988
QFile file("src/config.h");
8089
if(file.open(QIODevice::WriteOnly | QIODevice::Text))
8190
{

src/bin/m2adapter.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) 2023 Fastly, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
use pushpin::call_c_main;
18+
use std::process::ExitCode;
19+
20+
#[cfg(target_os = "macos")]
21+
#[link(name = "pushpin-cpp")]
22+
#[link(name = "QtCore", kind = "framework")]
23+
#[link(name = "QtNetwork", kind = "framework")]
24+
extern "C" {
25+
fn m2adapter_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
26+
}
27+
28+
#[cfg(not(target_os = "macos"))]
29+
#[link(name = "pushpin-cpp")]
30+
#[link(name = "Qt5Core")]
31+
#[link(name = "Qt5Network")]
32+
extern "C" {
33+
fn m2adapter_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
34+
}
35+
36+
fn main() -> ExitCode {
37+
unsafe { ExitCode::from(call_c_main(m2adapter_main)) }
38+
}

src/bin/pushpin-handler.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) 2023 Fastly, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
use pushpin::call_c_main;
18+
use std::process::ExitCode;
19+
20+
#[cfg(target_os = "macos")]
21+
#[link(name = "pushpin-cpp")]
22+
#[link(name = "QtCore", kind = "framework")]
23+
#[link(name = "QtNetwork", kind = "framework")]
24+
extern "C" {
25+
fn handler_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
26+
}
27+
28+
#[cfg(not(target_os = "macos"))]
29+
#[link(name = "pushpin-cpp")]
30+
#[link(name = "Qt5Core")]
31+
#[link(name = "Qt5Network")]
32+
extern "C" {
33+
fn handler_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
34+
}
35+
36+
fn main() -> ExitCode {
37+
unsafe { ExitCode::from(call_c_main(handler_main)) }
38+
}

src/bin/pushpin-legacy.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) 2023 Fastly, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
use pushpin::call_c_main;
18+
use std::process::ExitCode;
19+
20+
#[cfg(target_os = "macos")]
21+
#[link(name = "pushpin-cpp")]
22+
#[link(name = "QtCore", kind = "framework")]
23+
#[link(name = "QtNetwork", kind = "framework")]
24+
extern "C" {
25+
fn runner_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
26+
}
27+
28+
#[cfg(not(target_os = "macos"))]
29+
#[link(name = "pushpin-cpp")]
30+
#[link(name = "Qt5Core")]
31+
#[link(name = "Qt5Network")]
32+
extern "C" {
33+
fn runner_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
34+
}
35+
36+
fn main() -> ExitCode {
37+
unsafe { ExitCode::from(call_c_main(runner_main)) }
38+
}

src/bin/pushpin-proxy.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) 2023 Fastly, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
use pushpin::call_c_main;
18+
use std::process::ExitCode;
19+
20+
#[cfg(target_os = "macos")]
21+
#[link(name = "pushpin-cpp")]
22+
#[link(name = "QtCore", kind = "framework")]
23+
#[link(name = "QtNetwork", kind = "framework")]
24+
extern "C" {
25+
fn proxy_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
26+
}
27+
28+
#[cfg(not(target_os = "macos"))]
29+
#[link(name = "pushpin-cpp")]
30+
#[link(name = "Qt5Core")]
31+
#[link(name = "Qt5Network")]
32+
extern "C" {
33+
fn proxy_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
34+
}
35+
36+
fn main() -> ExitCode {
37+
unsafe { ExitCode::from(call_c_main(proxy_main)) }
38+
}

src/cpp/cpp.pro

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
TEMPLATE = subdirs
22

33
cpp.subdir = cpp
4-
tests.subdir = tests
5-
tests.depends = cpp
64

7-
tests.CONFIG += no_default_install
8-
9-
SUBDIRS += \
10-
cpp \
11-
tests
5+
SUBDIRS += cpp

src/cpp/cpp/cpp.pro

-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ DESTDIR = ..
99
MOC_DIR = $$OUT_PWD/_moc
1010
OBJECTS_DIR = $$OUT_PWD/_obj
1111

12-
include($$OUT_PWD/../../rust/lib.pri)
1312
include($$OUT_PWD/../../../conf.pri)
1413
include(cpp.pri)

src/handler/handler.pri

-16
This file was deleted.

src/handler/handler.pro

-21
This file was deleted.

src/handler/main.c

-6
This file was deleted.

src/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub mod zhttppacket;
5757
pub mod zhttpsocket;
5858
pub mod zmq;
5959

60+
use std::env;
6061
use std::ffi::CString;
6162
use std::future::Future;
6263
use std::io;
@@ -266,3 +267,17 @@ pub struct ListenConfig {
266267
pub spec: ListenSpec,
267268
pub stream: bool,
268269
}
270+
271+
/// # Safety
272+
///
273+
/// * `main_fn` must be safe to call.
274+
pub unsafe fn call_c_main(
275+
main_fn: unsafe extern "C" fn(libc::c_int, *const *const libc::c_char) -> libc::c_int,
276+
) -> u8 {
277+
let args: Vec<CString> = env::args_os()
278+
.map(|s| CString::new(s.into_string().unwrap()).unwrap())
279+
.collect();
280+
let args: Vec<*const libc::c_char> = args.iter().map(|s| s.as_c_str().as_ptr()).collect();
281+
282+
main_fn(args.len() as libc::c_int, args.as_ptr()) as u8
283+
}

src/m2adapter/m2adapter.pri

-10
This file was deleted.

src/m2adapter/m2adapter.pro

-21
This file was deleted.

src/m2adapter/main.c

-6
This file was deleted.

src/proxy/main.c

-6
This file was deleted.

0 commit comments

Comments
 (0)