Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert all programs to rust programs calling mains from cpp lib #47771

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
use std::collections::HashMap;
use std::error::Error;
use std::fs;
use std::io::{BufRead, BufReader};
use std::path::Path;

fn main() -> Result<(), Box<dyn Error>> {
let (app_version, config_dir, lib_dir) = {
let conf = {
let mut conf = HashMap::new();

let f = fs::File::open("conf.pri")?;
let reader = BufReader::new(f);

let mut av = String::new();
let mut cd = String::new();
let mut ld = String::new();
const CONF_VARS: &[&str] = &["APP_VERSION", "CONFIGDIR", "LIBDIR", "QT_INSTALL_LIBS"];

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

if line.starts_with("APP_VERSION =") {
let pos = line.find('=').unwrap();

av = line[(pos + 1)..].trim().into();
} else if line.starts_with("CONFIGDIR =") {
let pos = line.find('=').unwrap();

cd = line[(pos + 1)..].trim().into();
} else if line.starts_with("LIBDIR =") {
let pos = line.find('=').unwrap();

ld = line[(pos + 1)..].trim().into();
for name in CONF_VARS {
if line.starts_with(name) {
let pos = line
.find('=')
.unwrap_or_else(|| panic!("no '=' character following var {}", name));
conf.insert(name.to_string(), line[(pos + 1)..].trim().to_string());
break;
}
}
}

(av, cd, ld)
conf
};

let app_version = conf.get("APP_VERSION").unwrap();
let config_dir = conf.get("CONFIGDIR").unwrap();
let lib_dir = conf.get("LIBDIR").unwrap();
let qt_install_libs = conf.get("QT_INSTALL_LIBS").unwrap();

let cpp_lib_dir = fs::canonicalize(Path::new("src/cpp")).unwrap();

println!("cargo:rustc-env=APP_VERSION={}", app_version);
println!("cargo:rustc-env=CONFIG_DIR={}", config_dir);
println!("cargo:rustc-env=LIB_DIR={}", lib_dir);

println!("cargo:rustc-link-search={}", cpp_lib_dir.display());

#[cfg(target_os = "macos")]
println!("cargo:rustc-link-search=framework={}", qt_install_libs);

#[cfg(not(target_os = "macos"))]
println!("cargo:rustc-link-search={}", qt_install_libs);

println!("cargo:rerun-if-changed=conf.pri");

Ok(())
}
9 changes: 9 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,15 @@ public:

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

QByteArray out;
if(conf->doCommand(conf->qmake_path, QStringList() << "-query" << "QT_INSTALL_LIBS", &out) != 0)
{
return false;
}

QString qtlibdir = QString::fromUtf8(out).trimmed();
conf->addExtra(QString("QT_INSTALL_LIBS = %1").arg(qtlibdir));

QFile file("src/config.h");
if(file.open(QIODevice::WriteOnly | QIODevice::Text))
{
Expand Down
9 changes: 9 additions & 0 deletions qcm/conf.qcm
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ public:

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

QByteArray out;
if(conf->doCommand(conf->qmake_path, QStringList() << "-query" << "QT_INSTALL_LIBS", &out) != 0)
{
return false;
}

QString qtlibdir = QString::fromUtf8(out).trimmed();
conf->addExtra(QString("QT_INSTALL_LIBS = %1").arg(qtlibdir));

QFile file("src/config.h");
if(file.open(QIODevice::WriteOnly | QIODevice::Text))
{
Expand Down
38 changes: 38 additions & 0 deletions src/bin/m2adapter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2023 Fastly, Inc.
*
* Licensed 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 pushpin::call_c_main;
use std::process::ExitCode;

#[cfg(target_os = "macos")]
#[link(name = "pushpin-cpp")]
#[link(name = "QtCore", kind = "framework")]
#[link(name = "QtNetwork", kind = "framework")]
extern "C" {
fn m2adapter_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
}

#[cfg(not(target_os = "macos"))]
#[link(name = "pushpin-cpp")]
#[link(name = "Qt5Core")]
#[link(name = "Qt5Network")]
extern "C" {
fn m2adapter_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
}

fn main() -> ExitCode {
unsafe { ExitCode::from(call_c_main(m2adapter_main)) }
}
38 changes: 38 additions & 0 deletions src/bin/pushpin-handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2023 Fastly, Inc.
*
* Licensed 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 pushpin::call_c_main;
use std::process::ExitCode;

#[cfg(target_os = "macos")]
#[link(name = "pushpin-cpp")]
#[link(name = "QtCore", kind = "framework")]
#[link(name = "QtNetwork", kind = "framework")]
extern "C" {
fn handler_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
}

#[cfg(not(target_os = "macos"))]
#[link(name = "pushpin-cpp")]
#[link(name = "Qt5Core")]
#[link(name = "Qt5Network")]
extern "C" {
fn handler_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
}

fn main() -> ExitCode {
unsafe { ExitCode::from(call_c_main(handler_main)) }
}
38 changes: 38 additions & 0 deletions src/bin/pushpin-legacy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2023 Fastly, Inc.
*
* Licensed 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 pushpin::call_c_main;
use std::process::ExitCode;

#[cfg(target_os = "macos")]
#[link(name = "pushpin-cpp")]
#[link(name = "QtCore", kind = "framework")]
#[link(name = "QtNetwork", kind = "framework")]
extern "C" {
fn runner_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
}

#[cfg(not(target_os = "macos"))]
#[link(name = "pushpin-cpp")]
#[link(name = "Qt5Core")]
#[link(name = "Qt5Network")]
extern "C" {
fn runner_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
}

fn main() -> ExitCode {
unsafe { ExitCode::from(call_c_main(runner_main)) }
}
38 changes: 38 additions & 0 deletions src/bin/pushpin-proxy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2023 Fastly, Inc.
*
* Licensed 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 pushpin::call_c_main;
use std::process::ExitCode;

#[cfg(target_os = "macos")]
#[link(name = "pushpin-cpp")]
#[link(name = "QtCore", kind = "framework")]
#[link(name = "QtNetwork", kind = "framework")]
extern "C" {
fn proxy_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
}

#[cfg(not(target_os = "macos"))]
#[link(name = "pushpin-cpp")]
#[link(name = "Qt5Core")]
#[link(name = "Qt5Network")]
extern "C" {
fn proxy_main(argc: libc::c_int, argv: *const *const libc::c_char) -> libc::c_int;
}

fn main() -> ExitCode {
unsafe { ExitCode::from(call_c_main(proxy_main)) }
}
8 changes: 1 addition & 7 deletions src/cpp/cpp.pro
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
TEMPLATE = subdirs

cpp.subdir = cpp
tests.subdir = tests
tests.depends = cpp

tests.CONFIG += no_default_install

SUBDIRS += \
cpp \
tests
SUBDIRS += cpp
1 change: 0 additions & 1 deletion src/cpp/cpp/cpp.pro
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ DESTDIR = ..
MOC_DIR = $$OUT_PWD/_moc
OBJECTS_DIR = $$OUT_PWD/_obj

include($$OUT_PWD/../../rust/lib.pri)
include($$OUT_PWD/../../../conf.pri)
include(cpp.pri)
16 changes: 0 additions & 16 deletions src/handler/handler.pri

This file was deleted.

21 changes: 0 additions & 21 deletions src/handler/handler.pro

This file was deleted.

6 changes: 0 additions & 6 deletions src/handler/main.c

This file was deleted.

15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub mod zhttppacket;
pub mod zhttpsocket;
pub mod zmq;

use std::env;
use std::ffi::CString;
use std::future::Future;
use std::io;
Expand Down Expand Up @@ -266,3 +267,17 @@ pub struct ListenConfig {
pub spec: ListenSpec,
pub stream: bool,
}

/// # Safety
///
/// * `main_fn` must be safe to call.
pub unsafe fn call_c_main(
main_fn: unsafe extern "C" fn(libc::c_int, *const *const libc::c_char) -> libc::c_int,
) -> u8 {
let args: Vec<CString> = env::args_os()
.map(|s| CString::new(s.into_string().unwrap()).unwrap())
.collect();
let args: Vec<*const libc::c_char> = args.iter().map(|s| s.as_c_str().as_ptr()).collect();

main_fn(args.len() as libc::c_int, args.as_ptr()) as u8
}
10 changes: 0 additions & 10 deletions src/m2adapter/m2adapter.pri

This file was deleted.

21 changes: 0 additions & 21 deletions src/m2adapter/m2adapter.pro

This file was deleted.

6 changes: 0 additions & 6 deletions src/m2adapter/main.c

This file was deleted.

6 changes: 0 additions & 6 deletions src/proxy/main.c

This file was deleted.

Loading
Loading