From 2c6be797a342a5f463fdd85c7d7ef2e747171057 Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Mon, 1 Nov 2021 15:01:02 +0530 Subject: [PATCH] Initial Commit. --- .gitignore | 2 ++ Cargo.toml | 17 ++++++++++++ build.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 27 ++++++++++++++++++ tests/tests.rs | 8 ++++++ 5 files changed, 128 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 build.rs create mode 100644 src/lib.rs create mode 100644 tests/tests.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..fe37130 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "ki18n-rs" +version = "0.1.0" +edition = "2018" +links = "KF5I18n" + +[dependencies] +cpp = "0.5" +qmetaobject = { git = "https://github.com/woboq/qmetaobject-rs" } +qttypes = { git = "https://github.com/woboq/qmetaobject-rs", features = [ + "qtquick", +] } + +[build-dependencies] +cpp_build = "0.5" +semver = "1.0" +pkg-config = "0.3" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..c8a2d64 --- /dev/null +++ b/build.rs @@ -0,0 +1,74 @@ +/* Copyright (C) 2018 Olivier Goffart + +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. +*/ + +use semver::Version; + +fn main() { + eprintln!("cargo:warning={:?}", std::env::vars().collect::>()); + + let mut config = cpp_build::Config::new(); + + let qt_version = qt_setup(&mut config); + ki18n_setup(&mut config); + + config.build("src/lib.rs"); + + for minor in 7..=15 { + if qt_version >= Version::new(5, minor, 0) { + println!("cargo:rustc-cfg=qt_{}_{}", 5, minor); + } + } + let mut minor = 0; + while qt_version >= Version::new(6, minor, 0) { + println!("cargo:rustc-cfg=qt_{}_{}", 6, minor); + minor += 1; + } +} + +fn qt_setup(config: &mut cpp_build::Config) -> Version { + let qt_include_path = std::env::var("DEP_QT_INCLUDE_PATH").unwrap(); + let qt_library_path = std::env::var("DEP_QT_LIBRARY_PATH").unwrap(); + let qt_version = std::env::var("DEP_QT_VERSION") + .unwrap() + .parse::() + .expect("Parsing Qt version failed"); + + if cfg!(target_os = "macos") { + config.flag("-F"); + config.flag(&qt_library_path); + } + + if qt_version >= Version::new(6, 0, 0) { + config.flag_if_supported("-std=c++17"); + config.flag_if_supported("/std:c++17"); + config.flag_if_supported("/Zc:__cplusplus"); + } + + config.include(&qt_include_path); + + qt_version +} + +fn ki18n_setup(config: &mut cpp_build::Config) { + let kf5_i18n_path = "/usr/include/KF5/KI18n"; + let qt_core = "/usr/include/qt/QtCore"; + + config.include(kf5_i18n_path).include(qt_core); + + println!("cargo:rustc-link-lib=KF5I18n"); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..441db71 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,27 @@ +use cpp::{cpp, cpp_class}; +use qmetaobject::prelude::*; +use qmetaobject::QObjectPinned; + +cpp! {{ + #include + #include + #include + #include + + struct KLocalizedContextHolder { + std::unique_ptr klocalized; + + KLocalizedContextHolder(QObject *parent) : klocalized(new KLocalizedContext(parent)) {} + }; +}} + +cpp_class!(pub unsafe struct KLocalizedContext as "KLocalizedContextHolder"); + +impl KLocalizedContext { + pub fn init_from_engine(engine: &QmlEngine) { + let engine_ptr = engine.cpp_ptr(); + cpp!(unsafe [engine_ptr as "QQmlEngine*"] { + engine_ptr->rootContext()->setContextObject(new KLocalizedContext(engine_ptr)); + }); + } +} diff --git a/tests/tests.rs b/tests/tests.rs new file mode 100644 index 0000000..09db8a9 --- /dev/null +++ b/tests/tests.rs @@ -0,0 +1,8 @@ +use ki18n_rs::*; +use qmetaobject::prelude::*; + +#[test] +fn test_klocalized_init() { + let mut engine = QmlEngine::new(); + KLocalizedContext::init_from_engine(&engine); +}