Skip to content

Commit 659524d

Browse files
Reisenjayantk
andauthored
[pyth_oracle] Improve build.rs robustness and expose types. (#343)
* [pyth_oracle] robust build and library features * precommit --------- Co-authored-by: Jayant Krishnamurthy <jayantkrishnamurthy@gmail.com>
1 parent 590f9d0 commit 659524d

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

program/c/makefile

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
OUT_DIR := ./target
1+
# Use the OUT_DIR env variable as the output directory if available, otherwise
2+
# default to ./target
3+
OUT_DIR := $(if $(OUT_DIR),$(OUT_DIR),./target)
24
SOLANA := $(shell dirname $(shell which cargo-build-bpf))
35

46
ifneq ("$(wildcard $(SOLANA)/sdk/bpf/c/bpf.mk)","")
@@ -9,11 +11,18 @@ else
911
include $(SOLANA)/sdk/sbf/c/sbf.mk
1012
endif
1113

14+
15+
# Bundle C code compiled to bpf for use by rust
16+
.PHONY: cpyth-bpf
1217
cpyth-bpf:
13-
# Bundle C code compiled to bpf for use by rust
14-
bash -c "ar rcs target/libcpyth-bpf.a target/**/*.o"
18+
bash -c "ar rcs $(OUT_DIR)/libcpyth-bpf.a target/**/*.o"
19+
20+
21+
# 2-Stage Contract Build
22+
#
23+
# 1) Compile C code to system architecture for use by rust's cargo test
24+
# 2) Bundle C code compiled to system architecture for use by rust's cargo test
25+
.PHONY: cpyth-native
1526
cpyth-native:
16-
# Compile C code to system architecture for use by rust's cargo test
17-
gcc -c ./src/oracle/native/upd_aggregate.c -o ./target/cpyth-native.o -fPIC
18-
# Bundle C code compiled to system architecture for use by rust's cargo test
19-
ar rcs target/libcpyth-native.a ./target/cpyth-native.o
27+
gcc -c ./src/oracle/native/upd_aggregate.c -o $(OUT_DIR)/cpyth-native.o -fPIC
28+
ar rcs $(OUT_DIR)/libcpyth-native.a $(OUT_DIR)/cpyth-native.o

program/rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ test-generator = "0.3.1"
2929

3030
[features]
3131
debug = []
32+
library = []
3233

3334
[lib]
3435
crate-type = ["cdylib", "lib"]

program/rust/build.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,33 @@ use {
77
};
88

99
fn main() {
10-
println!("cargo:rustc-link-search=./program/c/target");
10+
// Cargo exposes ENV variables for feature flags that can be used to determine whether to do a
11+
// self-contained build that compiles the C components automatically.
12+
if std::env::var("CARGO_FEATURE_LIBRARY").is_ok() {
13+
// OUT_DIR is the path cargo provides to a build directory under `target/` specifically for
14+
// isolated build artifacts. We use this to build the C program and then link against the
15+
// resulting static library. This allows building the program when used as a dependency of
16+
// another crate.
17+
let out_dir = std::env::var("OUT_DIR").unwrap();
18+
let out_dir = PathBuf::from(out_dir);
19+
20+
// We must forward OUT_DIR as an env variable to the make script otherwise it will output
21+
// its artifacts to the wrong place.
22+
std::process::Command::new("make")
23+
.env("VERBOSE", "1")
24+
.env("OUT_DIR", out_dir.display().to_string())
25+
.current_dir("../c")
26+
.args(["cpyth-native"])
27+
.status()
28+
.expect("Failed to build C program");
29+
30+
// Emit instructions for cargo to link against the built static library.
31+
println!("cargo:rustc-link-lib=static=cpyth-native");
32+
println!("cargo:rustc-link-search={}", out_dir.display());
33+
} else {
34+
// Otherwise fall back to the old relative-path C style build we used to have.
35+
println!("cargo:rustc-link-search=./program/c/target");
36+
}
1137

1238
// Generate and write bindings
1339
let bindings = Builder::default()

program/rust/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ mod tests;
1717
#[cfg(feature = "debug")]
1818
mod log;
1919

20+
// When compiled in `library` mode the on-chain definitions provided by this library are
21+
// exported. This is useful when other libraries wish to directly utilise the exact Solana specific
22+
// on-chain definitions of the Pyth program.
23+
//
24+
// While we have `pyth-sdk-rs` which exposes a more friendly interface, this is still useful when a
25+
// downstream user wants to confirm for example that they can compile against the binary interface
26+
// of this program for their specific solana version.
27+
#[cfg(feature = "library")]
28+
pub use accounts::{
29+
AccountHeader,
30+
MappingAccount,
31+
PermissionAccount,
32+
PriceAccount,
33+
PriceComponent,
34+
PriceEma,
35+
PriceInfo,
36+
ProductAccount,
37+
PythAccount,
38+
};
2039
use {
2140
crate::error::OracleError,
2241
processor::process_instruction,

0 commit comments

Comments
 (0)