Skip to content

Commit b2b8b93

Browse files
committed
changed file structure and added run.sh to run CI
1 parent 078bbaa commit b2b8b93

18 files changed

Lines changed: 260 additions & 75 deletions

File tree

.gitignore

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
1-
# Generated by Cargo
2-
# will have compiled files and executables
3-
debug/
1+
# ==============================
2+
# Rust (global)
3+
# ==============================
4+
5+
# Compiled files
46
target/
7+
debug/
58

6-
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7-
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
9+
# Cargo lock (keep ignored for libraries)
810
Cargo.lock
911

10-
# These are backup files generated by rustfmt
12+
# Rustfmt backups
1113
**/*.rs.bk
1214

13-
# MSVC Windows builds of rustc generate these, which store debugging information
15+
# Windows debug symbols
1416
*.pdb
17+
18+
# ==============================
19+
# Example-specific (IMPORTANT)
20+
# ==============================
21+
22+
# C++ compiled binary
23+
example
24+
25+
# Any compiled outputs
26+
*.out
27+
*.exe
28+
*.o
29+
30+
# Rust build inside subfolder
31+
examples/incompatible_allocators/rust/target/
32+
33+
# macOS junk files
34+
.DS_Store
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// A C++ program that prints a message to the terminal, from C++ and Rust
2+
3+
#include <iostream>
4+
5+
// Declare the Rust function so C++ can call it
6+
extern "C" void print_rust_message(void);
7+
8+
// Prints a run message to the terminal, from C++ and Rust
9+
int main(void) {
10+
// Replace this with your C++ code
11+
std::cout << "C++ code ran while running the build-tool-template example" << std::endl;
12+
print_rust_message();
13+
return 0;
14+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
# ^ support bash that's not in the standard `/bin` path
3+
4+
# A script that builds, tests, and runs the build-tool-template example
5+
6+
# This file needs to be executable:
7+
# - on Linux or macOS, use `chmod +x run.sh` then `git add run.sh`
8+
# - on Windows, use `git add --chmod=+x run.sh` or `git update-index --chmod=+x run.sh`
9+
10+
# bash "strict mode": fail the script if any command fails, or any variable is unset when used
11+
set -euo pipefail
12+
13+
# Replace unset `$CXX` variable with the empty string
14+
if [ -z "${CXX:-}" ]; then
15+
echo "CXX environment variable is empty or unset, using the default 'c++' compiler"
16+
CXX=c++
17+
fi
18+
19+
# If the script is run outside the example directory, change to the expected location
20+
cd "$(dirname "$0")"
21+
22+
# Replace the lines below with your build tool command
23+
echo "Shell script ran while running the build-tool-template example"
24+
# Build the Rust library
25+
pushd rust-library
26+
cargo build
27+
popd
28+
# Build and run the C++ and Rust binary
29+
# This order is important, dependencies must be later than code that depends on them
30+
$CXX main.cpp rust-library/target/debug/librust_library.a -o main
31+
./main

examples/build-tool-template/rust-library/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "rust-library"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
# Build a static C/C++ library
7+
[lib]
8+
crate-type = ["staticlib"]
9+
10+
# Add Rust dependencies here
11+
[dependencies]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//! A Rust library that prints a message to the terminal
2+
3+
/// Prints a Rust run message to the terminal
4+
///
5+
/// # Safety
6+
///
7+
/// The entire binary must only have one function with this un-mangled name.
8+
/// Callers must use the C calling convention to call this function.
9+
#[unsafe(no_mangle)]
10+
pub unsafe extern "C" fn print_rust_message() {
11+
// Replace this with your Rust code
12+
println!("Rust code ran while running the build-tool-template example");
13+
}
14+
15+
#[cfg(test)]
16+
mod tests {
17+
use super::*;
18+
use std::io::Write;
19+
20+
// Optional: Replace this with your Rust tests, if you need any
21+
//
22+
/// Test printing a Rust message to the terminal
23+
#[test]
24+
fn test_rust_message() {
25+
// Rust tests hide test output, here's one way to print it anyway
26+
writeln!(
27+
&mut std::io::stderr(),
28+
"Rust code ran while testing the build-tool-template example",
29+
)
30+
.expect("failed to write to stderr");
31+
32+
unsafe { print_rust_message() };
33+
}
34+
}

examples/incompatible-allocators-simple/Readme.md

Lines changed: 0 additions & 28 deletions
This file was deleted.

examples/incompatible-allocators-simple/cpp.cpp

Lines changed: 0 additions & 10 deletions
This file was deleted.
-10.3 KB
Binary file not shown.
-456 KB
Binary file not shown.

0 commit comments

Comments
 (0)