Skip to content

Commit

Permalink
support custom cpp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Riateche committed Jan 2, 2020
1 parent 4bd7fa3 commit fb7ce98
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 15 deletions.
4 changes: 4 additions & 0 deletions qt_ritual/crate_templates/moqt_core/c_lib/extra/extra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int extra_fn() {
QPoint p(42, 21);
return p.x();
}
7 changes: 7 additions & 0 deletions qt_ritual/crate_templates/moqt_core/tests/qpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,10 @@ fn operators() {
let _ = &b << vec.as_ref();
}
}

#[test]
fn extra() {
unsafe {
assert_eq!(moqt_core::extra_fn(), 42);
}
}
7 changes: 7 additions & 0 deletions qt_ritual/src/test_moqt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ fn build_moqt() -> Result<TempTestDir> {
let mut file = create_file(template_src_dir.join(item.file_name()))?;
write!(file, "{}", new_content)?;
}

if repo_template_dir.join(&real_lib).join("c_lib").exists() {
copy_recursively(
&repo_template_dir.join(&real_lib).join("c_lib"),
&template_dir.join(lib).join("c_lib"),
)?;
}
}
let lib_path = install_dir.join("lib");
add_env("LIBRARY_PATH", &lib_path)?;
Expand Down
4 changes: 4 additions & 0 deletions qt_ritual/test_assets/moqt/moqt_core/QObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class QMetaObject {
public:
class Connection {
public:
typedef void *Connection::*RestrictedBool;
operator RestrictedBool() const {
return nullptr;
}
};
};

Expand Down
24 changes: 21 additions & 3 deletions ritual/src/cpp_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use ritual_common::cpp_lib_builder::{
};
use ritual_common::errors::{bail, err_msg, Result};
use ritual_common::file_utils::{
create_dir_all, create_file, os_str_to_str, path_to_str, remove_dir_all,
copy_recursively, create_dir_all, create_file, os_str_to_str, path_to_str, read_dir,
remove_dir_all,
};
use ritual_common::target::{current_target, LibraryTarget};
use ritual_common::utils::{MapIfOk, ProgressBar};
Expand Down Expand Up @@ -268,6 +269,7 @@ pub struct LocalCppChecker {
crate_name: String,
cpp_build_config: CppBuildConfigData,
cpp_build_paths: CppBuildPaths,
crate_template_path: Option<PathBuf>,
tests: Vec<PreliminaryTest>,
}

Expand All @@ -280,6 +282,7 @@ impl LocalCppChecker {
parent_path: parent_path.into(),
include_directives: config.include_directives().to_vec(),
crate_name: config.crate_properties().name().to_string(),
crate_template_path: config.crate_template_path().cloned(),
cpp_build_paths: {
let mut data = config.cpp_build_paths().clone();
data.apply_env();
Expand All @@ -298,6 +301,22 @@ impl LocalCppChecker {
let src_path = root_path.join("src");
create_dir_all(&src_path)?;

let mut all_include_directives = self.include_directives.clone();
let extra_path = src_path.join("extra");
create_dir_all(&extra_path)?;
if let Some(crate_template_path) = &self.crate_template_path {
let extra_template = crate_template_path.join("c_lib/extra");
if extra_template.exists() {
copy_recursively(&extra_template, &extra_path)?;
for item in read_dir(&extra_template)? {
all_include_directives.push(PathBuf::from(format!(
"extra/{}",
os_str_to_str(&item?.file_name())?
)));
}
}
}

let mut cmake_file = create_file(src_path.join("CMakeLists.txt"))?;
write!(
cmake_file,
Expand All @@ -309,8 +328,7 @@ impl LocalCppChecker {
write!(
utils_file,
include_str!("../templates/cpp_checker/utils.h"),
include_directives_code = self
.include_directives
include_directives_code = all_include_directives
.iter()
.map_if_ok(|d| -> Result<_> { Ok(format!("#include \"{}\"", path_to_str(d)?)) })?
.join("\n")
Expand Down
43 changes: 33 additions & 10 deletions ritual/src/cpp_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use regex::Regex;
use ritual_common::env_var_names;
use ritual_common::errors::{bail, err_msg, format_err, print_trace, Result, ResultExt};
use ritual_common::file_utils::{
canonicalize, create_file, open_file, os_str_to_str, path_to_str, remove_file,
canonicalize, copy_recursively, create_file, open_file, os_str_to_str, path_to_str, read_dir,
remove_dir_all, remove_file,
};
use ritual_common::target::{current_target, LibraryTarget};
use std::io::Write;
Expand Down Expand Up @@ -236,15 +237,34 @@ fn run_clang<R, F: FnMut(Entity<'_>) -> Result<R>>(
let clang = init_clang()?;
let index = Index::new(&clang, false, false);
let tmp_cpp_path = tmp_path.join("1.cpp");
{
let mut tmp_file = create_file(&tmp_cpp_path)?;
for directive in config.include_directives() {
writeln!(tmp_file, "#include \"{}\"", path_to_str(directive)?)?;
}
if let Some(cpp_code) = cpp_code {
write!(tmp_file, "{}", cpp_code)?;
let mut tmp_file = create_file(&tmp_cpp_path)?;
for directive in config.include_directives() {
writeln!(tmp_file, "#include \"{}\"", path_to_str(directive)?)?;
}
if let Some(cpp_code) = cpp_code {
write!(tmp_file, "{}", cpp_code)?;
}

if let Some(template_path) = config.crate_template_path() {
let extra_files_dir = template_path.join("c_lib/extra");
if extra_files_dir.exists() {
let destination = tmp_path.join("extra");
if destination.exists() {
remove_dir_all(&destination)?;
}
copy_recursively(&extra_files_dir, &destination)?;
for item in read_dir(&extra_files_dir)? {
writeln!(
tmp_file,
"#include \"extra/{}\"",
os_str_to_str(&item?.file_name())?
)?;
}
}
}

drop(tmp_file);

let mut args = vec![
"-Xclang".to_string(),
"-detailed-preprocessing-record".to_string(),
Expand Down Expand Up @@ -284,7 +304,7 @@ fn run_clang<R, F: FnMut(Entity<'_>) -> Result<R>>(
.parse()
.with_context(|_| "clang parse failed")?;
let translation_unit = tu.get_entity();
assert!(translation_unit.get_kind() == EntityKind::TranslationUnit);
assert_eq!(translation_unit.get_kind(), EntityKind::TranslationUnit);
{
let diagnostics = tu.get_diagnostics();
if !diagnostics.is_empty() {
Expand Down Expand Up @@ -318,6 +338,9 @@ pub fn run(data: &mut ProcessorData<'_>) -> Result<()> {
data,
output: Default::default(),
};
parser
.current_target_paths
.push(parser.data.workspace.tmp_path().join("extra"));
run_clang(
&parser.data.config,
&parser.data.workspace.tmp_path(),
Expand Down Expand Up @@ -347,7 +370,7 @@ pub fn parse_generated_items(data: &mut ProcessorData<'_>) -> Result<()> {
}
let code = ffi_item.item.source_item_cpp_code(data.db)?;
let mut parser = CppParser {
current_target_paths: vec![data.workspace.tmp_path()],
current_target_paths: vec![data.workspace.tmp_path().join("1.cpp")],
source_id: Some(ffi_item_id),
data,
output: Default::default(),
Expand Down
17 changes: 15 additions & 2 deletions ritual/src/crate_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::rust_code_generator;
use ritual_common::errors::Result;
use ritual_common::file_utils::{
copy_file, copy_recursively, crate_version, create_dir, create_dir_all, create_file,
diff_paths, path_to_str, read_dir, remove_dir_all, repo_dir_path, save_json, save_toml_table,
diff_paths, os_str_to_str, path_to_str, read_dir, remove_dir_all, repo_dir_path, save_json,
save_toml_table,
};
use ritual_common::toml;
use ritual_common::utils::{run_command, MapIfOk};
Expand Down Expand Up @@ -247,11 +248,23 @@ pub fn run(data: &mut ProcessorData<'_>) -> Result<()> {
}
let c_lib_name = format!("{}_c", data.config.crate_properties().name());
let global_header_name = format!("{}_global.h", c_lib_name);
let mut all_include_directives = data.config.include_directives().to_vec();
if let Some(crate_template_path) = data.config.crate_template_path() {
let extra_template = crate_template_path.join("c_lib/extra");
if extra_template.exists() {
for item in read_dir(&extra_template)? {
all_include_directives.push(PathBuf::from(format!(
"extra/{}",
os_str_to_str(&item?.file_name())?
)));
}
}
}
generate_c_lib_template(
&c_lib_name,
&c_lib_path,
&global_header_name,
data.config.include_directives(),
&all_include_directives,
)?;

cpp_code_generator::generate_cpp_file(
Expand Down

0 comments on commit fb7ce98

Please sign in to comment.