Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement cross-compilation based on sysroot using Clang #6

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ FROM debian:testing

WORKDIR /agent

# cross_debian_arch: amd64 or arm64
# cross_pkg_arch: x86-64 or aarch64
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 && \
RUN apt-get update -y && \
apt-get dist-upgrade -y && \
apt-get install -y wget make git clang-16 unzip libc6-dev g++ gcc pkgconf \
gcc-${cross_pkg_arch}-linux-gnu libc6-${cross_debian_arch}-cross && \
apt-get install -y clang-16 git lld-16 make pkgconf unzip wget && \
apt-get clean autoclean && \
apt-get autoremove --yes

Expand Down
30 changes: 17 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@ endif
# Valid values are: amd64, arm64.
TARGET_ARCH ?= $(NATIVE_ARCH)

ifeq ($(NATIVE_ARCH),$(TARGET_ARCH))
ARCH_PREFIX :=
else ifeq ($(TARGET_ARCH),arm64)
ARCH_PREFIX := aarch64-linux-gnu-
ifeq ($(TARGET_ARCH),arm64)
COMPILER_TARGET_ARCH := aarch64
else ifeq ($(TARGET_ARCH),amd64)
ARCH_PREFIX := x86_64-linux-gnu-
COMPILER_TARGET_ARCH := x86_64
else
$(error Unsupported architecture: $(TARGET_ARCH))
endif

export TARGET_ARCH
COMPILER_TARGET ?= $(COMPILER_TARGET_ARCH)-redhat-linux
SYSROOT_PATH ?= /

export CC = clang-16
export COMPILER_TARGET
export CGO_CFLAGS = --sysroot=/usr/sysroot --target=$(COMPILER_TARGET)
export CGO_ENABLED = 1
export CGO_LDFLAGS = -fuse-ld=lld --sysroot=/usr/sysroot
export GOARCH = $(TARGET_ARCH)
export CC = $(ARCH_PREFIX)clang-16
export OBJCOPY = $(ARCH_PREFIX)objcopy
export TARGET_ARCH

BRANCH = $(shell git rev-parse --abbrev-ref HEAD | tr -d '-' | tr '[:upper:]' '[:lower:]')
COMMIT_SHORT_SHA = $(shell git rev-parse --short=8 HEAD)
Expand All @@ -41,7 +44,8 @@ REVISION ?= $(BRANCH)-$(COMMIT_SHORT_SHA)

LDFLAGS := -X go.opentelemetry.io/ebpf-profiler/vc.version=$(VERSION) \
-X go.opentelemetry.io/ebpf-profiler/vc.revision=$(REVISION) \
-X go.opentelemetry.io/ebpf-profiler/vc.buildTimestamp=$(BUILD_TIMESTAMP)
-X go.opentelemetry.io/ebpf-profiler/vc.buildTimestamp=$(BUILD_TIMESTAMP) \
\"-extldflags=$(CGO_CFLAGS)\"

GO_TAGS := osusergo,netgo
EBPF_FLAGS :=
Expand Down Expand Up @@ -118,12 +122,12 @@ docker-image:
docker build -t profiling-agent -f Dockerfile .

agent:
docker run -v "$$PWD":/agent -it --rm --user $(shell id -u):$(shell id -g) profiling-agent \
"make TARGET_ARCH=$(TARGET_ARCH) VERSION=$(VERSION) REVISION=$(REVISION) BUILD_TIMESTAMP=$(BUILD_TIMESTAMP)"
docker run -v "$$PWD":/agent -v $(SYSROOT_PATH):/usr/sysroot -it --rm --user $(shell id -u):$(shell id -g) profiling-agent \
"make COMPILER_TARGET=$(COMPILER_TARGET) TARGET_ARCH=$(TARGET_ARCH) VERSION=$(VERSION) REVISION=$(REVISION) BUILD_TIMESTAMP=$(BUILD_TIMESTAMP)"

debug-agent:
docker run -v "$$PWD":/agent -it --rm --user $(shell id -u):$(shell id -g) profiling-agent \
"make TARGET_ARCH=$(TARGET_ARCH) VERSION=$(VERSION) REVISION=$(REVISION) BUILD_TIMESTAMP=$(BUILD_TIMESTAMP) debug"
docker run -v "$$PWD":/agent -v $(SYSROOT_PATH):/usr/sysroot -it --rm --user $(shell id -u):$(shell id -g) profiling-agent \
"make COMPILER_TARGET=$(COMPILER_TARGET) TARGET_ARCH=$(TARGET_ARCH) VERSION=$(VERSION) REVISION=$(REVISION) BUILD_TIMESTAMP=$(BUILD_TIMESTAMP) debug"

legal:
@go install github.com/google/go-licenses@latest
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ The agent can be built with the provided make targets. Docker is required for co
```sh
make docker-image
```
2. Build the agent for your current machine's architecture:
2. Build the agent for your current machine's architecture with your current environment serving as sysroot:
```sh
make agent
```
Or `make debug-agent` for debug build.
3. To cross-complie for a different architecture (e.g. arm64):
3. To cross-compile for a different architecture (e.g. arm64):
```sh
make agent TARGET_ARCH=arm64
SYSROOT_PATH=/path/to/your/desired/sysroot TARGET_ARCH=arm64 make agent
```
Where `SYSROOT_PATH` is directed at a valid sysroot for the specified `TARGET_ARCH` as defined [here](https://clang.llvm.org/docs/CrossCompilation.html).

The resulting binary will be named <ebpf-profiler> in the current directory.

## Other OSes
Expand Down
11 changes: 3 additions & 8 deletions support/ebpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,12 @@ BUILD_TYPE ?= release

TRACER_NAME ?= tracer.ebpf.$(BUILD_TYPE).$(TARGET_ARCH)

ifeq ($(TARGET_ARCH),arm64)
TARGET_FLAGS = -target aarch64-linux-gnu
else
TARGET_FLAGS = -target x86_64-linux-gnu
endif

CFLAGS = $(CGO_CFLAGS)
ifeq ($(BUILD_TYPE),debug)
TARGET_FLAGS+=$(DEBUG_FLAGS)
CFLAGS+=$(DEBUG_FLAGS)
endif

FLAGS=$(TARGET_FLAGS) \
FLAGS=$(CFLAGS) \
-fno-jump-tables \
-nostdlib \
-nostdinc \
Expand Down
Loading