Skip to content

Commit

Permalink
rust: preparations to integrate Rust (#360)
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Lehner <florian.lehner@elastic.co>
  • Loading branch information
florianl authored Feb 25, 2025
1 parent c6cb9d4 commit 4e86769
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 33 deletions.
9 changes: 9 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[env]
CC_aarch64_unknown_linux_musl = "aarch64-linux-musl-gcc"
CC_x86_64_unknown_linux_musl = "x86_64-linux-musl-gcc"

# Zydis CMake config requires a C++ compiler for the test cases but the musl
# toolchain doesn't have one. We simply force the compiler to `g++` here to
# trick CMake into passing the check for a C++ compiler: it isn't actually
# used anyway.
TARGET_CXX = "g++"
21 changes: 20 additions & 1 deletion .github/workflows/env/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,29 @@ runs:
sudo apt-get update -y
sudo apt-get install -y curl unzip gcc-aarch64-linux-gnu \
libc6-arm64-cross qemu-user-binfmt libc6:arm64
libc6-arm64-cross qemu-user-binfmt libc6:arm64 \
musl-dev:amd64 musl-dev:arm64 musl-tools binutils-aarch64-linux-gnu
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache-dependency-path: go.sum
id: go
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
targets: x86_64-unknown-linux-musl,aarch64-unknown-linux-musl
toolchain: 1.77
- name: Install protoc
shell: bash
env:
PB_URL: "https://github.com/protocolbuffers/protobuf/releases/download/v24.4/"
PB_FILE: "protoc-24.4-linux-x86_64.zip"
INSTALL_DIR: "/usr/local"
run: |
wget -nv "$PB_URL/$PB_FILE"
sudo unzip "$PB_FILE" -d "$INSTALL_DIR" 'bin/*' 'include/*'
sudo chmod +xr "$INSTALL_DIR/bin/protoc"
sudo find "$INSTALL_DIR/include" -type d -exec chmod +x {} \;
sudo find "$INSTALL_DIR/include" -type f -exec chmod +r {} \;
rm "$PB_FILE"
2 changes: 0 additions & 2 deletions .github/workflows/push-docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up environment
uses: ./.github/workflows/env
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
Expand Down
26 changes: 2 additions & 24 deletions .github/workflows/unit-test-on-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,11 @@ jobs:
restore-keys: |
coredumps-${{ matrix.target_arch }}
coredumps-
- name: Direct Rust test
run: make rust-tests
- name: Tests
run: make test TARGET_ARCH=${{ matrix.target_arch }}

test-rust:
name: Test rust components
runs-on: ubuntu-24.04
steps:
- name: Clone code
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
- name: Install protoc
env:
PB_URL: "https://github.com/protocolbuffers/protobuf/releases/download/v24.4/"
PB_FILE: "protoc-24.4-linux-x86_64.zip"
INSTALL_DIR: "/usr/local"
run: |
wget -q "$PB_URL/$PB_FILE"
sudo unzip "$PB_FILE" -d "$INSTALL_DIR" 'bin/*' 'include/*'
sudo chmod +xr "$INSTALL_DIR/bin/protoc"
sudo find "$INSTALL_DIR/include" -type d -exec chmod +x {} \;
sudo find "$INSTALL_DIR/include" -type f -exec chmod +r {} \;
rm "$PB_FILE"
- name: Tests
run: |
cargo test
check-ebpf-blobs:
name: Check for differences in the eBPF binary blobs
runs-on: ubuntu-24.04
Expand Down
13 changes: 11 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RUN cross_debian_arch=$(uname -m | sed -e 's/aarch64/amd64/' -e 's/x86_64/arm64
cross_pkg_arch=$(uname -m | sed -e 's/aarch64/x86-64/' -e 's/x86_64/aarch64/'); \
apt-get update -y && \
apt-get dist-upgrade -y && \
apt-get install -y wget make git clang-17 unzip libc6-dev g++ gcc pkgconf \
apt-get install -y curl wget make git clang-17 unzip libc6-dev g++ gcc pkgconf \
gcc-${cross_pkg_arch}-linux-gnu libc6-${cross_debian_arch}-cross && \
apt-get clean autoclean && \
apt-get autoremove --yes
Expand All @@ -33,7 +33,7 @@ RUN
PB_FILE="protoc-24.4-linux-x86_64.zip"; \
INSTALL_DIR="/usr/local"; \
\
wget -q "$PB_URL/$PB_FILE" \
wget -nv "$PB_URL/$PB_FILE" \
&& unzip "$PB_FILE" -d "$INSTALL_DIR" 'bin/*' 'include/*' \
&& chmod +xr "$INSTALL_DIR/bin/protoc" \
&& find "$INSTALL_DIR/include" -type d -exec chmod +x {} \; \
Expand All @@ -43,4 +43,13 @@ RUN
# Append to /etc/profile for login shells
RUN echo 'export PATH="/usr/local/go/bin:$PATH"' >> /etc/profile

# Create .cargo directory
RUN mkdir -p /root/.cargo

# Install rustup and cargo
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain 1.77

# Add cargo to PATH
RUN echo 'export PATH="/root/.cargo/bin:$PATH"' >> /etc/profile

ENTRYPOINT ["/bin/bash", "-l", "-c"]
23 changes: 19 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: all all-common clean ebpf generate test test-deps protobuf docker-image agent legal \
integration-test-binaries codespell lint linter-version debug debug-agent ebpf-profiler \
format-ebpf
format-ebpf rust-components rust-targets rust-tests vanity-import-check vanity-import-fix

SHELL := /usr/bin/env bash

Expand Down Expand Up @@ -73,9 +73,26 @@ generate:
ebpf: generate
$(MAKE) $(EBPF_FLAGS) -C support/ebpf

ebpf-profiler: generate ebpf
ebpf-profiler: generate ebpf rust-components
go build $(GO_FLAGS) -tags $(GO_TAGS)

rust-targets:
ifeq ($(TARGET_ARCH),arm64)
rustup target add aarch64-unknown-linux-musl
else ifeq ($(TARGET_ARCH),amd64)
rustup target add x86_64-unknown-linux-musl
endif

rust-components: rust-targets
ifeq ($(TARGET_ARCH),arm64)
cargo build --lib --release --target aarch64-unknown-linux-musl
else ifeq ($(TARGET_ARCH),amd64)
cargo build --lib --release --target x86_64-unknown-linux-musl
endif

rust-tests: rust-targets
cargo test

GOLANGCI_LINT_VERSION = "v1.63.4"
lint: generate vanity-import-check
$(MAKE) lint -C support/ebpf
Expand All @@ -88,12 +105,10 @@ format-ebpf:
linter-version:
@echo $(GOLANGCI_LINT_VERSION)

.PHONY: vanity-import-check
vanity-import-check:
@go install github.com/jcchavezs/porto/cmd/porto@latest
@porto --include-internal -l . || ( echo "(run: make vanity-import-fix)"; exit 1 )

.PHONY: vanity-import-fix
vanity-import-fix: $(PORTO)
@go install github.com/jcchavezs/porto/cmd/porto@latest
@porto --include-internal -w .
Expand Down

0 comments on commit 4e86769

Please sign in to comment.