Create a new crate to expose the C++

This commit is contained in:
Olivier Goffart 2021-03-16 16:33:09 +01:00
parent 705b76d2bd
commit 0e351de1a6
10 changed files with 88 additions and 54 deletions

View file

@ -11,6 +11,7 @@ members = [
'api/sixtyfps-rs',
'api/sixtyfps-rs/sixtyfps-macros',
'api/sixtyfps-rs/sixtyfps-build',
'api/sixtyfps-cpp',
'api/sixtyfps-node/native',
'api/sixtyfps-wasm-interpreter',
'tools/compiler',
@ -45,6 +46,7 @@ default-members = [
'sixtyfps_compiler',
'api/sixtyfps-rs',
'api/sixtyfps-rs/sixtyfps-build',
'api/sixtyfps-cpp',
'api/sixtyfps-node/native',
'tools/compiler',
'tools/viewer',

View file

@ -18,11 +18,11 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(Corrosion)
corrosion_import_crate(MANIFEST_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../Cargo.toml"
CRATES sixtyfps-compiler sixtyfps-rendering-backend-default xtask)
CRATES sixtyfps-compiler sixtyfps-cpp xtask)
add_library(SixtyFPS INTERFACE)
add_library(SixtyFPS::SixtyFPS ALIAS SixtyFPS)
target_link_libraries(SixtyFPS INTERFACE sixtyfps-rendering-backend-default)
target_link_libraries(SixtyFPS INTERFACE sixtyfps-cpp)
target_compile_features(SixtyFPS INTERFACE cxx_std_17)
file(GLOB api_headers RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/include/"
@ -69,18 +69,18 @@ target_include_directories(SixtyFPS INTERFACE
add_executable(SixtyFPS::sixtyfps_compiler ALIAS sixtyfps_compiler)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/SixtyFPSMacro.cmake)
export(TARGETS SixtyFPS sixtyfps-rendering-backend-default
export(TARGETS SixtyFPS sixtyfps-cpp
NAMESPACE SixtyFPS:: FILE "${CMAKE_BINARY_DIR}/lib/cmake/SixtyFPS/SixtyFPSTargets.cmake")
install(EXPORT SixtyFPSTargets NAMESPACE SixtyFPS:: DESTINATION lib/cmake/SixtyFPS)
install(TARGETS SixtyFPS sixtyfps-rendering-backend-default
install(TARGETS SixtyFPS sixtyfps-cpp
EXPORT SixtyFPSTargets LIBRARY DESTINATION lib PUBLIC_HEADER DESTINATION include/sixtyfps)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
install(FILES $<TARGET_FILE:sixtyfps-rendering-backend-default-shared> TYPE LIB)
install(FILES $<TARGET_FILE:sixtyfps-cpp-shared> TYPE LIB)
if(WIN32)
install(FILES $<TARGET_LINKER_FILE:sixtyfps-rendering-backend-default-shared> TYPE LIB)
install(FILES $<TARGET_LINKER_FILE:sixtyfps-cpp-shared> TYPE LIB)
endif()
install(PROGRAMS $<TARGET_FILE:sixtyfps_compiler> TYPE BIN)
@ -89,7 +89,7 @@ set(SIXTYFPS_LIB_PROPERTIES "")
foreach(prop
IMPORTED_LOCATION IMPORTED_LOCATION_DEBUG IMPORTED_LOCATION_RELEASE
IMPORTED_IMPLIB IMPORTED_IMPLIB_DEBUG IMPORTED_IMPLIB_RELEASE)
get_target_property(value sixtyfps-rendering-backend-default-shared ${prop})
get_target_property(value sixtyfps-cpp-shared ${prop})
if(value)
get_filename_component(value ${value} NAME)
list(APPEND SIXTYFPS_LIB_PROPERTIES ${prop} "\${_IMPORT_PREFIX}/${CMAKE_INSTALL_LIBDIR}/${value}")

View file

@ -0,0 +1,19 @@
[package]
name = "sixtyfps-cpp"
version = "0.0.5"
authors = ["SixtyFPS <info@sixtyfps.io>"]
edition = "2018"
license = "GPL-3.0-only"
description = "SixtyFPS C++ integration"
repository = "https://github.com/sixtyfpsui/sixtyfps"
homepage = "https://sixtyfps.io"
publish = false
[lib]
path = "lib.rs"
crate-type = ["cdylib"]
[dependencies]
sixtyfps-rendering-backend-default = { version = "=0.0.5", path="../../sixtyfps_runtime/rendering_backends/default" }
sixtyfps-corelib = { version = "=0.0.5", path="../../sixtyfps_runtime/corelib" }

View file

@ -17,8 +17,8 @@ if(_IMPORT_PREFIX STREQUAL "/")
set(_IMPORT_PREFIX "")
endif()
add_library(sixtyfps-rendering-backend-default-shared SHARED IMPORTED)
set_target_properties(sixtyfps-rendering-backend-default-shared PROPERTIES @SIXTYFPS_LIB_PROPERTIES@)
add_library(sixtyfps-cpp-shared SHARED IMPORTED)
set_target_properties(sixtyfps-cpp-shared PROPERTIES @SIXTYFPS_LIB_PROPERTIES@)
add_executable(SixtyFPS::sixtyfps_compiler IMPORTED GLOBAL)
set_target_properties(SixtyFPS::sixtyfps_compiler PROPERTIES IMPORTED_LOCATION "${_IMPORT_PREFIX}/@CMAKE_INSTALL_BINDIR@/sixtyfps_compiler${CMAKE_EXECUTABLE_SUFFIX}")

View file

@ -27,7 +27,7 @@ struct ComponentVTable;
struct ItemVTable;
}
#include "sixtyfps_internal.h"
#include "sixtyfps_default_backend_internal.h"
#include "sixtyfps_backend_internal.h"
#include "sixtyfps_qt_internal.h"
namespace sixtyfps {

50
api/sixtyfps-cpp/lib.rs Normal file
View file

@ -0,0 +1,50 @@
/* LICENSE BEGIN
This file is part of the SixtyFPS Project -- https://sixtyfps.io
Copyright (c) 2020 Olivier Goffart <olivier.goffart@sixtyfps.io>
Copyright (c) 2020 Simon Hausmann <simon.hausmann@sixtyfps.io>
SPDX-License-Identifier: GPL-3.0-only
This file is also available under commercial licensing terms.
Please contact info@sixtyfps.io for more information.
LICENSE END */
/*! This scrates just expose the function used by the C++ integration */
#[doc(hidden)]
#[cold]
pub fn use_modules() {
sixtyfps_rendering_backend_default::use_modules();
}
use sixtyfps_rendering_backend_default::backend;
#[cfg(not(target_arch = "wasm32"))]
use sixtyfps_corelib::window::ffi::ComponentWindowOpaque;
use sixtyfps_corelib::window::ComponentWindow;
#[no_mangle]
pub unsafe extern "C" fn sixtyfps_component_window_init(out: *mut ComponentWindowOpaque) {
assert_eq!(
core::mem::size_of::<ComponentWindow>(),
core::mem::size_of::<ComponentWindowOpaque>()
);
core::ptr::write(out as *mut ComponentWindow, crate::backend().create_window());
}
#[no_mangle]
pub unsafe extern "C" fn sixtyfps_run_event_loop() {
crate::backend().run_event_loop();
}
#[no_mangle]
pub unsafe extern "C" fn sixtyfps_register_font_from_path(
path: &sixtyfps_corelib::SharedString,
error_str: *mut sixtyfps_corelib::SharedString,
) {
core::ptr::write(
error_str,
match crate::backend().register_font_from_path(std::path::Path::new(path.as_str())) {
Ok(()) => Default::default(),
Err(err) => err.to_string().into(),
},
)
}

View file

@ -10,7 +10,6 @@ homepage = "https://sixtyfps.io"
[lib]
path = "lib.rs"
crate-type = [ "lib", "cdylib" ]
[features]
x11 = ["sixtyfps-rendering-backend-gl/x11"]

View file

@ -77,37 +77,3 @@ pub fn use_modules() {
#[cfg(feature = "sixtyfps-rendering-backend-gl")]
sixtyfps_rendering_backend_gl::use_modules();
}
#[cfg(not(target_arch = "wasm32"))]
pub mod ffi {
use sixtyfps_corelib::window::ffi::ComponentWindowOpaque;
use sixtyfps_corelib::window::ComponentWindow;
#[no_mangle]
pub unsafe extern "C" fn sixtyfps_component_window_init(out: *mut ComponentWindowOpaque) {
assert_eq!(
core::mem::size_of::<ComponentWindow>(),
core::mem::size_of::<ComponentWindowOpaque>()
);
core::ptr::write(out as *mut ComponentWindow, crate::backend().create_window());
}
#[no_mangle]
pub unsafe extern "C" fn sixtyfps_run_event_loop() {
crate::backend().run_event_loop();
}
#[no_mangle]
pub unsafe extern "C" fn sixtyfps_register_font_from_path(
path: &sixtyfps_corelib::SharedString,
error_str: *mut sixtyfps_corelib::SharedString,
) {
core::ptr::write(
error_str,
match crate::backend().register_font_from_path(std::path::Path::new(path.as_str())) {
Ok(()) => Default::default(),
Err(err) => err.to_string().into(),
},
)
}
}

View file

@ -97,13 +97,11 @@ pub fn test(testcase: &test_driver_lib::TestCase) -> Result<(), Box<dyn Error>>
compiler_command.arg("-g");
compiler_command.arg("-Werror").arg("-Wall").arg("-Wextra");
compiler_command.arg(concat!("-L", env!("CPP_LIB_PATH")));
compiler_command.arg("-lsixtyfps_rendering_backend_default");
compiler_command.arg("-lsixtyfps_cpp");
compiler_command.arg("-o").arg(&*binary_path);
} else if compiler.is_like_msvc() {
compiler_command.arg("/std:c++17");
compiler_command
.arg("/link")
.arg(concat!(env!("CPP_LIB_PATH"), "\\sixtyfps_rendering_backend_default.dll.lib"));
compiler_command.arg("/link").arg(concat!(env!("CPP_LIB_PATH"), "\\sixtyfps_cpp.dll.lib"));
let mut out_arg = std::ffi::OsString::from("/OUT:");
out_arg.push(&*binary_path);
compiler_command.arg(out_arg);

View file

@ -304,17 +304,17 @@ fn gen_backend_qt(root_dir: &Path, include_dir: &Path) -> anyhow::Result<()> {
Ok(())
}
fn gen_backend_default(root_dir: &Path, include_dir: &Path) -> anyhow::Result<()> {
fn gen_backend(root_dir: &Path, include_dir: &Path) -> anyhow::Result<()> {
let config = default_config();
let mut crate_dir = root_dir.to_owned();
crate_dir.extend(["sixtyfps_runtime", "rendering_backends", "default"].iter());
crate_dir.extend(["api", "sixtyfps-cpp"].iter());
cbindgen::Builder::new()
.with_config(config)
.with_crate(crate_dir)
.with_header("#include <sixtyfps_internal.h>")
.generate()
.context("Unable to generate bindings for sixtyfps_default_backend_internal.h")?
.write_to_file(include_dir.join("sixtyfps_default_backend_internal.h"));
.context("Unable to generate bindings for sixtyfps_backend_internal.h")?
.write_to_file(include_dir.join("sixtyfps_backend_internal.h"));
Ok(())
}
@ -326,6 +326,6 @@ pub fn gen_all(root_dir: &Path, include_dir: &Path) -> anyhow::Result<()> {
std::fs::create_dir_all(include_dir).context("Could not create the include directory")?;
gen_corelib(root_dir, include_dir)?;
gen_backend_qt(root_dir, include_dir)?;
gen_backend_default(root_dir, include_dir)?;
gen_backend(root_dir, include_dir)?;
Ok(())
}