Skip to content

Commit

Permalink
Add support for building programs completely from source. (#24)
Browse files Browse the repository at this point in the history
And add an example showing how to do this.
  • Loading branch information
sunfishcode authored Nov 15, 2023
1 parent 74bc0f1 commit bf27fa5
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exclude = ["/.github", "ci"]
keywords = ["linux"]

[dependencies]
c-gull = { version = "0.15.19", default-features = false, features = ["eyra"] }
c-gull = { version = "0.15.20", default-features = false, features = ["eyra"] }

[dev-dependencies]
assert_cmd = "2.0.12"
Expand All @@ -33,7 +33,7 @@ which = "5.0.0"
core_simd = { git = "https://github.com/rust-lang/portable-simd" }

[features]
default = ["be-std", "threadsafe-setenv"]
default = ["be-std", "threadsafe-setenv", "use-compiler-builtins"]

# This makes `setenv` and friends thread-safe by leaking memory.
threadsafe-setenv = ["c-gull/threadsafe-setenv"]
Expand All @@ -57,5 +57,8 @@ experimental-relocate = ["c-gull/experimental-relocate"]
# Have eyra do `use std::*;` so that it can be used as `std`.
be-std = []

# Should c-scape's `memcpy` etc. use compiler-builtins?
use-compiler-builtins = ["c-gull/use-compiler-builtins"]

# Enable `todo!()` stubs for unimplemented functions.
todo = ["c-gull/todo"]
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ Other examples include
- [enabling LTO],
- [using min-sized-rust technique to produce small binaries], and
- [adding Eyra as an optional dependency].
- [building a program completely from source].

[this hello world example]: https://github.com/sunfishcode/eyra/tree/main/example-crates/hello-world#readme
[enabling LTO]: https://github.com/sunfishcode/eyra/tree/main/example-crates/hello-world-lto#readme
[using min-sized-rust technique to produce small binaries]: https://github.com/sunfishcode/eyra/tree/main/example-crates/hello-world-small#readme
[adding Eyra as an optional dependency]: https://github.com/sunfishcode/eyra/tree/main/example-crates/eyra-optional-example#readme
[building a program completely from source]: https://github.com/sunfishcode/eyra/tree/main/example-crates/all-from-source#readme

## Why?

Expand All @@ -76,8 +78,8 @@ Why use Eyra?
For even more code-size reductions, see the techniques in
[the hello-world-small example].

- Support for compiling programs with alternate calling conventions, with
[Eyra and `-Zbuild-std`].
- Support for compiling programs with alternate calling conventions, using
[Eyra and `-Zbuild-std`] to [build a program completely from source].

- [Fully static linking] that supports the platform NSS/DNS config. "Is such
a thing even possible?", "Yes it is."
Expand Down Expand Up @@ -249,3 +251,4 @@ compatibility layer can use the underlying crates directly.
[c-ward]: https://github.com/sunfishcode/c-ward#readme
[rustix]: https://github.com/bytecodealliance/rustix#readme
[supported by Rust]: https://doc.rust-lang.org/nightly/rustc/platform-support.html
[build a program completely from source]: https://github.com/sunfishcode/eyra/tree/main/example-crates/all-from-source#readme
2 changes: 2 additions & 0 deletions example-crates/all-from-source/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
17 changes: 17 additions & 0 deletions example-crates/all-from-source/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "all-from-source"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies]
# Disable the "use-compiler-builtins" feature, to ensure that we build
# `memcpy` etc. from source instead of using the prebuilt library.
eyra = { path = "../..", default-features = false, features = ["be-std", "threadsafe-setenv"] }

[profile.release]
lto = true
codegen-units = 1

# This is just an example crate, and not part of the c-ward workspace.
[workspace]
12 changes: 12 additions & 0 deletions example-crates/all-from-source/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
This example demonstrates building a program completely from source, without
using any prebuilt libraries, if built with `-Zbuild-std`.

For example, use `cargo rustc` to build the LLVM IR file and test that it has
no external function declarations (other than LLVM intrinsics).

```
$ cargo +nightly rustc -Zbuild-std --release --target=x86_64-unknown-linux-gnu -- --emit=llvm-ir
[...]
$ grep ^declare ./target/x86_64-unknown-linux-gnu/release/deps/all_from_source-*.ll | grep -v '@llvm'
$
```
4 changes: 4 additions & 0 deletions example-crates/all-from-source/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
// Pass -nostartfiles to the linker.
println!("cargo:rustc-link-arg=-nostartfiles");
}
5 changes: 5 additions & 0 deletions example-crates/all-from-source/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern crate eyra;

fn main() {
println!("Hello, world!");
}

0 comments on commit bf27fa5

Please sign in to comment.