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 1/3] 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 \ From 093c2a60e746d8cda84f4a8631c3ebe061da8785 Mon Sep 17 00:00:00 2001 From: ltrk2 <107155950+ltrk2@users.noreply.github.com> Date: Fri, 6 Dec 2024 13:23:31 -0500 Subject: [PATCH 2/3] Propagate computed environment variable into Dockerized invocation --- Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index f8bbdeb4..402949bd 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,7 @@ 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 @@ -122,11 +123,11 @@ docker-image: 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)" + "make COMPILER_TARGET=$(COMPILER_TARGET) TARGET_ARCH=$(TARGET_ARCH) VERSION=$(VERSION) REVISION=$(REVISION) BUILD_TIMESTAMP=$(BUILD_TIMESTAMP)" debug-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" + "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 From c53740851ca50bb1fadec48f7df88abe3f9a350b Mon Sep 17 00:00:00 2001 From: ltrk2 <107155950+ltrk2@users.noreply.github.com> Date: Fri, 6 Dec 2024 19:21:36 -0500 Subject: [PATCH 3/3] Add documentation --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3922aa8a..8d6d932b 100644 --- a/README.md +++ b/README.md @@ -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 in the current directory. ## Other OSes