From 104dac38960ad04a3665be32a0777e567d4fcdee Mon Sep 17 00:00:00 2001 From: ltrk2 <107155950+ltrk2@users.noreply.github.com> Date: Fri, 6 Dec 2024 13:14:53 -0500 Subject: [PATCH] Implement cross-compilation based on sysroot using Clang --- Dockerfile | 9 ++------- Makefile | 25 ++++++++++++++----------- support/ebpf/Makefile | 11 +++-------- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/Dockerfile b/Dockerfile index a386ea0b..6455aa90 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/Makefile b/Makefile index 56735a21..f8bbdeb4 100644 --- a/Makefile +++ b/Makefile @@ -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) @@ -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 := @@ -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: diff --git a/support/ebpf/Makefile b/support/ebpf/Makefile index a757f5a9..a271e745 100644 --- a/support/ebpf/Makefile +++ b/support/ebpf/Makefile @@ -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 \