Skip to content

Commit

Permalink
Implement cross-compilation based on sysroot using Clang
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrk2 committed Dec 6, 2024
1 parent e7acabc commit 104dac3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
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
25 changes: 14 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@ 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 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 +43,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,11 +121,11 @@ 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 \
docker run -v "$$PWD":/agent -v $(SYSROOT_PATH):/usr/sysroot -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-agent:
docker run -v "$$PWD":/agent -it --rm --user $(shell id -u):$(shell id -g) profiling-agent \
docker run -v "$$PWD":/agent -v $(SYSROOT_PATH):/usr/sysroot -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"

legal:
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

0 comments on commit 104dac3

Please sign in to comment.