File tree Expand file tree Collapse file tree 4 files changed +63
-8
lines changed Expand file tree Collapse file tree 4 files changed +63
-8
lines changed Original file line number Diff line number Diff line change 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)
2
4
SOLANA := $(shell dirname $(shell which cargo-build-bpf) )
3
5
4
6
ifneq ("$(wildcard $(SOLANA ) /sdk/bpf/c/bpf.mk) ","")
9
11
include $(SOLANA ) /sdk/sbf/c/sbf.mk
10
12
endif
11
13
14
+
15
+ # Bundle C code compiled to bpf for use by rust
16
+ .PHONY : cpyth-bpf
12
17
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
15
26
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
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ test-generator = "0.3.1"
29
29
30
30
[features ]
31
31
debug = []
32
+ library = []
32
33
33
34
[lib ]
34
35
crate-type = [" cdylib" , " lib" ]
Original file line number Diff line number Diff line change 7
7
} ;
8
8
9
9
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
+ }
11
37
12
38
// Generate and write bindings
13
39
let bindings = Builder :: default ( )
Original file line number Diff line number Diff line change @@ -17,6 +17,25 @@ mod tests;
17
17
#[ cfg( feature = "debug" ) ]
18
18
mod log;
19
19
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
+ } ;
20
39
use {
21
40
crate :: error:: OracleError ,
22
41
processor:: process_instruction,
You can’t perform that action at this time.
0 commit comments