Skip to content

Commit

Permalink
helloworld: Add C implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque committed Sep 6, 2024
1 parent 0f5c424 commit b04f2a1
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 2 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,35 @@ jobs:
- name: Build and test program
run: cd ${{ matrix.program }} && cargo test-sbf

c-test:
name: Run tests against C implementations
strategy:
matrix:
program: [helloworld]
fail-fast: false
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
key: rust-${{ hashFiles('./Cargo.lock') }}

- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.78.0

- name: Install Rust build deps
run: ./install-rust-build-deps.sh

- name: Install Solana
run: |
./install-solana.sh
echo "$HOME/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH
- name: Build and test program
run: ./test-c.sh ${{ matrix.program }}
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,46 @@ cd helloworld/zig

```console
cd ..
SBF_OUT_DIR="./zig/zig-out/lib" cargo test"
SBF_OUT_DIR="./zig/zig-out/lib" cargo test
```

* OR use the helper from the root of this repo to build and test

```console
./test-zig helloworld
./test-zig.sh helloworld
```

### C

* Install Solana tools

```console
./install-solana.sh
```

* Go to a program directory

```console
cd helloworld/c
```

* Build a program

```console
make
```

* Test it

```console
cd ..
SBF_OUT_DIR="./c/out" cargo test
```

* OR use the helper from the root of this repo to build and test

```console
./test-c.sh helloworld
```

## Current Programs
Expand Down
49 changes: 49 additions & 0 deletions helloworld/c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
LOCAL_PATH := /home/$(USER)/.local/share/solana/install/active_release/bin/sdk/sbf/c/

.PHONY: all clean

SRC_DIR ?= ./src
OUT_DIR ?= ./out

all: $(OUT_DIR)/solana_program_rosetta_helloworld.so

clean:
rm -rf $(OUT_DIR)

LLVM_DIR = $(LOCAL_PATH)../dependencies/platform-tools/llvm
LLVM_SYSTEM_INC_DIRS := $(LLVM_DIR)/lib/clang/17/include
STD_INC_DIRS := $(LLVM_DIR)/include
STD_LIB_DIRS := $(LLVM_DIR)/lib

CC := $(LLVM_DIR)/bin/clang
LLD := $(LLVM_DIR)/bin/ld.lld

SYSTEM_INC_DIRS := \
$(LOCAL_PATH)inc \
$(LLVM_SYSTEM_INC_DIRS) \

C_FLAGS := \
-Werror \
-O2 \
-fno-builtin \
-std=c17 \
$(addprefix -isystem,$(SYSTEM_INC_DIRS)) \
$(addprefix -I,$(STD_INC_DIRS)) \
-target sbf \
-fPIC

SBF_LLD_FLAGS := \
-z notext \
-shared \
--Bdynamic \
$(LOCAL_PATH)sbf.ld \
--entry entrypoint \
-L $(STD_LIB_DIRS) \
-lc \

$(OUT_DIR)/main.o: $(SRC_DIR)/main.c
mkdir -p $(OUT_DIR)
$(CC) $(C_FLAGS) -o $(OUT_DIR)/main.o -c $(SRC_DIR)/main.c

$(OUT_DIR)/solana_program_rosetta_helloworld.so: $(OUT_DIR)/main.o
$(LLD) $(SBF_LLD_FLAGS) -o $(OUT_DIR)/solana_program_rosetta_helloworld.so $(OUT_DIR)/main.o
9 changes: 9 additions & 0 deletions helloworld/c/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @brief C-based Helloworld BPF program
*/
#include <solana_sdk.h>

extern uint64_t entrypoint(const uint8_t *input) {
sol_log("Hello world!");
return SUCCESS;
}
9 changes: 9 additions & 0 deletions test-c.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

PROGRAM_NAME="$1"
ROOT_DIR="$(cd "$(dirname "$0")"; pwd)"
set -e
PROGRAM_DIR=$ROOT_DIR/$PROGRAM_NAME
cd $PROGRAM_DIR/c
make
SBF_OUT_DIR="$PROGRAM_DIR/c/out" cargo test --manifest-path "$PROGRAM_DIR/Cargo.toml"

0 comments on commit b04f2a1

Please sign in to comment.