|
1 | 1 | { |
2 | | - # To use: |
| 2 | + # To test with arbitrary firewood versions (alternative to firewood-go-ethhash): |
3 | 3 | # - Install nix: https://github.com/DeterminateSystems/nix-installer?tab=readme-ov-file#install-nix |
4 | | - # - Run from ffi/ dir: `nix build .#firewood-ffi` |
5 | | - # - Run from anywhere: `nix develop 'github:ava-labs/firewood?dir=ffi&ref=[SHA]'` |
| 4 | + # - Clone firewood locally at desired version/commit |
| 5 | + # - Build: `cd ffi && nix build` |
| 6 | + # - In your Go project: `go mod edit -replace github.com/ava-labs/firewood-go-ethhash/ffi=/path/to/firewood/ffi/result/ffi` |
6 | 7 |
|
7 | 8 | description = "Firewood FFI library and development environment"; |
8 | 9 |
|
|
34 | 35 | src = craneLib.path ./..; |
35 | 36 | filter = path: type: |
36 | 37 | (lib.hasSuffix "\.md" path) || |
| 38 | + (lib.hasSuffix "\.go" path) || |
| 39 | + (lib.hasSuffix "go.mod" path) || |
| 40 | + (lib.hasSuffix "go.sum" path) || |
| 41 | + (lib.hasSuffix "firewood.h" path) || |
37 | 42 | (craneLib.filterCargoSources path type); |
38 | 43 | }; |
39 | 44 |
|
|
64 | 69 | # Use cargo alias defined in .cargo/config.toml |
65 | 70 | cargoBuildCommand = "cargo build-static-ffi"; |
66 | 71 |
|
| 72 | + # Disable tests - we only need to build the static library |
| 73 | + doCheck = false; |
| 74 | + |
67 | 75 | # Install the static library and header |
68 | 76 | postInstall = '' |
69 | | - mkdir -p $out/lib $out/include |
70 | | - cp target/maxperf/libfirewood_ffi.a $out/lib/ || cp target/release/libfirewood_ffi.a $out/lib/ |
71 | | - cp ffi/firewood.h $out/include/ |
| 77 | + # Create a package structure compatible with FIREWOOD_LD_MODE=STATIC_LIBS |
| 78 | + mkdir -p $out/ffi |
| 79 | + cp -R ./ffi/* $out/ffi/ |
| 80 | + mkdir -p $out/ffi/libs/${pkgs.stdenv.hostPlatform.config} |
| 81 | + cp target/maxperf/libfirewood_ffi.a $out/ffi/libs/${pkgs.stdenv.hostPlatform.config}/ |
| 82 | +
|
| 83 | + # Run go generate to switch CGO directives to STATIC_LIBS mode |
| 84 | + cd $out/ffi |
| 85 | + HOME=$TMPDIR GOTOOLCHAIN=local FIREWOOD_LD_MODE=STATIC_LIBS ${pkgs.go}/bin/go generate |
72 | 86 | ''; |
73 | 87 |
|
74 | 88 | meta = with lib; { |
|
88 | 102 | default = firewood-ffi; |
89 | 103 | }; |
90 | 104 |
|
| 105 | + apps.go = { |
| 106 | + type = "app"; |
| 107 | + program = "${pkgs.go}/bin/go"; |
| 108 | + }; |
| 109 | + |
91 | 110 | devShells.default = craneLib.devShell { |
92 | 111 | inputsFrom = [ firewood-ffi ]; |
93 | 112 |
|
94 | 113 | packages = with pkgs; [ |
| 114 | + firewood-ffi |
95 | 115 | rustToolchain |
96 | 116 | go |
97 | 117 | ]; |
98 | 118 |
|
99 | 119 | shellHook = '' |
100 | | - echo "Firewood FFI development environment" |
101 | | - echo "Use 'nix build .#firewood-ffi' to build the FFI package" |
102 | | -
|
103 | | - # Set up CGO environment |
104 | | - export CGO_LDFLAGS="-L${firewood-ffi}/lib -lfirewood_ffi -lm" |
105 | | - export CGO_CFLAGS="-I${firewood-ffi}/include" |
106 | | -
|
107 | | - # TODO(marun) Maybe add a script? |
108 | | - echo "" |
109 | | - echo "To run Go tests:" |
110 | | - echo " export GOEXPERIMENT=cgocheck2" |
111 | | - echo " export TEST_FIREWOOD_HASH_MODE=ethhash" |
112 | | - echo " go test ./... -v" |
| 120 | + # Ensure golang bin is in the path |
| 121 | + GOBIN="$(go env GOPATH)/bin" |
| 122 | + if [[ ":$PATH:" != *":$GOBIN:"* ]]; then |
| 123 | + export PATH="$GOBIN:$PATH" |
| 124 | + fi |
113 | 125 | ''; |
114 | 126 | }; |
115 | 127 | }); |
|
0 commit comments