From d22763d29d846cad1881af6b8661a3d9fd00976d Mon Sep 17 00:00:00 2001 From: Christiano Haesbaert Date: Tue, 4 Jun 2024 11:08:11 +0200 Subject: [PATCH] Make this work with cmake, unfinished It works but I'm not happy. It's missing the whole bmake pruning, and figuring out if we should bother tracking dependencies, since cmake does its own thing. --- CMakeLists.txt | 1 + Makefile | 4 --- cmake/modules/libelf.cmake | 10 +++---- .../{README.quark => README.elastic} | 10 +++---- contrib/elftoolchain/libelf/GNUmakefile | 28 ++++++++++--------- contrib/elftoolchain/libelf/elf.h | 4 +-- contrib/elftoolchain/libelf/elf_open.c | 4 +-- contrib/elftoolchain/libelf/gelf.h | 6 ++-- contrib/elftoolchain/libelf/gelf_shdr.c | 4 +-- contrib/elftoolchain/libelf/libelf.h | 4 +-- 10 files changed, 37 insertions(+), 38 deletions(-) rename contrib/elftoolchain/{README.quark => README.elastic} (78%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c82f519..a4412b1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ # Copyright 2021 Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one # or more contributor license agreements. Licensed under the Elastic License 2.0; # you may not use this file except in compliance with the Elastic License 2.0. +set(CMAKE_VERBOSE_MAKEFILE ON) cmake_minimum_required(VERSION 3.20) diff --git a/Makefile b/Makefile index 063d0659..8da4cbee 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,6 @@ USER ?= $(shell whoami) CURRENT_DATE_TAG ?= $(shell date +%Y%m%d-%H%M) PKG_VERSION ?= $(shell cat VERSION) -# bmake Settings -MAKE_SYS_PATH ?= /usr/share/mk -export MAKESYSPATH = ${MAKE_SYS_PATH} - # Container Settings NOCONTAINER ?= BUILD_CONTAINER_IMAGE ?= diff --git a/cmake/modules/libelf.cmake b/cmake/modules/libelf.cmake index a4e50ee5..1db9c41e 100644 --- a/cmake/modules/libelf.cmake +++ b/cmake/modules/libelf.cmake @@ -5,18 +5,18 @@ # you may not use this file except in compliance with the Elastic License 2.0. -set(LIBELF_SRC "${PROJECT_SOURCE_DIR}/contrib/elftoolchain") -set(LIBELF_BUILD_DIR "${PROJECT_BINARY_DIR}/contrib/libelf") -set(LIBELF_LIB "${EBPF_INSTALL_DIR}/lib/libelf.a") +set(LIBELF_SRC "${PROJECT_SOURCE_DIR}/contrib/elftoolchain/libelf") +set(LIBELF_BUILD_DIR "${PROJECT_BINARY_DIR}/contrib/elftoolchain") +set(LIBELF_LIB "${EBPF_INSTALL_DIR}/lib/libelf_pic.a") ExternalProject_Add( libelf-external DOWNLOAD_COMMAND "" CONFIGURE_COMMAND "" BINARY_DIR ${LIBELF_BUILD_DIR} - BUILD_COMMAND CC=${CMAKE_C_COMPILER} ${EBPF_EXT_ENV_FLAGS} BINOWN=${CURRENT_USER} BINGRP=${CURRENT_GROUP} MFLAGS= MAKEFLAGS= WITH_TESTS=no WITH_BUILD_TOOLS=no BUILD_STATIC_ONLY=1 WITH_ADDITIONAL_DOCUMENTATION=no WITH_PE=no WITH_ISA=no MAKEOBJDIR=${LIBELF_BUILD_DIR} INCSDIR=/include /bin/sh -c "bmake -j${NPROC} -C ${LIBELF_SRC} -e" + BUILD_COMMAND make -C ${LIBELF_SRC} V=1 MAKEOBJDIR=${LIBELF_BUILD_DIR} + INSTALL_COMMAND cp ${LIBELF_BUILD_DIR}/libelf/libelf_pic.a ${EBPF_INSTALL_DIR}/lib/libelf_pic.a BUILD_IN_SOURCE 0 - INSTALL_COMMAND CC=${CMAKE_C_COMPILER} ${EBPF_EXT_ENV_FLAGS} BINOWN=${CURRENT_USER} BINGRP=${CURRENT_GROUP} MFLAGS= MAKEFLAGS= WITH_TESTS=no WITH_BUILD_TOOLS=no BUILD_STATIC_ONLY=1 WITH_ADDITIONAL_DOCUMENTATION=no WITH_PE=no WITH_ISA=no MAKEOBJDIR=${LIBELF_BUILD_DIR} /bin/sh -c "bmake -j${NPROC} -C ${LIBELF_SRC} -e install DESTDIR=${EBPF_INSTALL_DIR} prefix='' INCSDIR=/include" BUILD_BYPRODUCTS ${LIBELF_LIB} ) diff --git a/contrib/elftoolchain/README.quark b/contrib/elftoolchain/README.elastic similarity index 78% rename from contrib/elftoolchain/README.quark rename to contrib/elftoolchain/README.elastic index 68ba50b4..c2c5602a 100644 --- a/contrib/elftoolchain/README.quark +++ b/contrib/elftoolchain/README.elastic @@ -1,7 +1,7 @@ -Elftoolchain@Quark +Elftoolchain@Elastic ~~~~~~~~~~~~~~~~~~ - Elftoolchain has been imported in quark to get rid of the GPLed libelf present - on most linux systems. + Elftoolchain has been imported in elastic/ebpf to get rid of the GPLed libelf + present on most linux systems. In order to make Elftoolchain fit the project and also work with a recent libbpf we had to make a few changes. This file documents those changes. @@ -19,9 +19,9 @@ Changes to the build system Changes to please libbpf ~~~~~~~~~~~~~~~~~~~~~~~~ - o All the following changes are guarded under ifdef QUARK. + o All the following changes are guarded under ifdef ELASTIC. o Define empty stubs for a bunch of gelf_* functions. They return NULL/zero, - and since it's not used by quark@libbpf, this is fine. + and since it's not used by elastic/ebpf, this is fine. o Declared missing types in gelf.h. o Renamed elf_{openmemory, open} to elftoolchain_{openmemory, open}, these names clash with an exported symbol from libbpf. diff --git a/contrib/elftoolchain/libelf/GNUmakefile b/contrib/elftoolchain/libelf/GNUmakefile index a42087a8..ea3b6720 100644 --- a/contrib/elftoolchain/libelf/GNUmakefile +++ b/contrib/elftoolchain/libelf/GNUmakefile @@ -8,7 +8,7 @@ endif CFLAGS?= -g -O2 -fPIC -CPPFLAGS?= -D_GNU_SOURCE -DQUARK -I. -I../common +CPPFLAGS?= -D_GNU_SOURCE -DELASTIC -I. -I../common -I$(MAKEOBJDIR)/common # Keep original flags CDIAGFLAGS+= -Wa,--fatal-warnings @@ -44,9 +44,10 @@ CDIAGFLAGS+= -Wwrite-strings CC?= cc M4?= m4 +OBJDIR= $(MAKEOBJDIR)/libelf -GENSRCS:= libelf_fsize.c libelf_msize.c libelf_convert.c -GENDEPS:= elf_types.m4 $(patsubst %.c,%.m4,$(GENSRCS)) +GENSRCS:= $(OBJDIR)/libelf_fsize.c $(OBJDIR)/libelf_msize.c $(OBJDIR)/libelf_convert.c +GENDEPS:= $(wildcard *.m4) SRCS= elf.c \ elf_begin.c \ @@ -105,35 +106,36 @@ SRCS= elf.c \ libelf_xlate.c \ $(GENSRCS) -ELFDEFS:= ../common/sys/elfdefinitions.h +ELFDEFS:= $(MAKEOBJDIR)/common/sys/elfdefinitions.h ELFDEPS:= ../common/sys/elfdefinitions.m4 DEPS:= $(wildcard *.h) $(ELFDEPS) OBJS:= $(patsubst %.c,%.o,$(SRCS)) + LIBELF_STATIC:= libelf_pic.a -all: $(LIBELF_STATIC) +all: mkdir-objdir $(LIBELF_STATIC) $(LIBELF_STATIC): $(OBJS) $(call msg,AR,$@) - $(Q)ar rcs $@ $^ + $(Q)cd $(OBJDIR) && ar rcs $(OBJDIR)/$@ $^ -%.o: %.c GNUmakefile $(ELFDEFS) +$(OBJS): %.o: %.c GNUmakefile $(ELFDEFS) $(GENSRCS) $(call msg,CC,$@) - $(Q)$(CC) -c $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) $< + $(Q)$(CC) -c $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) $< -o $(OBJDIR)/$(notdir $@) $(GENSRCS): $(GENDEPS) $(call msg,M4,$@) - $(Q)$(M4) -D SRCDIR=/d/e/quark/elftoolchain/libelf $(@:.c=.m4) > $@ + $(Q)$(M4) -D SRCDIR=. $(notdir $(@:.c=.m4)) > $@ $(ELFDEFS): $(ELFDEPS) $(call msg,M4,$@) $(Q)$(M4) -I../common/sys -D SRCDIR=../common/sys elfdefinitions.m4 > $@ -clean: - $(call msg,CLEAN) - $(Q)rm -f $(OBJS) $(LIBELF_STATIC) $(ELFDEFS) +mkdir-objdir: + mkdir -p $(OBJDIR) + mkdir -p $(MAKEOBJDIR)/common/sys -.PHONY: all clean +.PHONY: all mkdir-objdir .SUFFIXES: diff --git a/contrib/elftoolchain/libelf/elf.h b/contrib/elftoolchain/libelf/elf.h index a938b5a7..392c80ba 100644 --- a/contrib/elftoolchain/libelf/elf.h +++ b/contrib/elftoolchain/libelf/elf.h @@ -29,8 +29,8 @@ * $Id$ */ -#ifdef QUARK +#ifdef ELASTIC #include -#endif /* QUARK */ +#endif /* ELASTIC */ #endif /* _ELF_H */ diff --git a/contrib/elftoolchain/libelf/elf_open.c b/contrib/elftoolchain/libelf/elf_open.c index b1d5e011..e892cf86 100644 --- a/contrib/elftoolchain/libelf/elf_open.c +++ b/contrib/elftoolchain/libelf/elf_open.c @@ -34,7 +34,7 @@ ELFTC_VCSID("$Id$"); /*@ELFTC-DOWNSTREAM-VCSID@*/ -#ifdef QUARK +#ifdef ELASTIC /* * Extension API: open a file for reading, ignoring parse errors. */ @@ -71,4 +71,4 @@ elftoolchain_elf_openmemory(char *image, size_t sz) return (_libelf_memory((unsigned char *) image, sz, 0)); } -#endif /* QUARK */ +#endif /* ELASTIC */ diff --git a/contrib/elftoolchain/libelf/gelf.h b/contrib/elftoolchain/libelf/gelf.h index 67fe2ad6..7e4308ba 100644 --- a/contrib/elftoolchain/libelf/gelf.h +++ b/contrib/elftoolchain/libelf/gelf.h @@ -51,12 +51,12 @@ typedef Elf64_Cap GElf_Cap; /* SW/HW capabilities */ typedef Elf64_Move GElf_Move; /* Move entries */ typedef Elf64_Syminfo GElf_Syminfo; /* Symbol information */ -#ifdef QUARK +#ifdef ELASTIC typedef Elf64_Nhdr GElf_Nhdr; /* Note section */ typedef Elf64_Verdef GElf_Verdef; /* Version definition sections. */ typedef Elf64_Verdaux GElf_Verdaux; /* Auxiliary version information */ typedef Elf64_Versym GElf_Versym; /* Version symbol information */ -#endif /* QUARK */ +#endif /* ELASTIC */ #define GELF_M_INFO ELF64_M_INFO #define GELF_M_SIZE ELF64_M_SIZE @@ -111,7 +111,7 @@ int gelf_update_cap(Elf_Data *_dst, int _index, GElf_Cap *_src); int gelf_update_move(Elf_Data *_dst, int _index, GElf_Move *_src); int gelf_update_syminfo(Elf_Data *_dst, int _index, GElf_Syminfo *_src); -#ifdef QUARK +#ifdef ELASTIC size_t gelf_getnote(Elf_Data *_data, size_t _offset, GElf_Nhdr *_result, size_t *_name_offset, size_t *_desc_offset); GElf_Verdef *gelf_getverdef(Elf_Data *_data, int _offset, GElf_Verdef *_dst); diff --git a/contrib/elftoolchain/libelf/gelf_shdr.c b/contrib/elftoolchain/libelf/gelf_shdr.c index b053b94d..443eccba 100644 --- a/contrib/elftoolchain/libelf/gelf_shdr.c +++ b/contrib/elftoolchain/libelf/gelf_shdr.c @@ -133,7 +133,7 @@ gelf_update_shdr(Elf_Scn *scn, GElf_Shdr *s) return (1); } -#ifdef QUARK +#ifdef ELASTIC #define UD __attribute__((unused)) size_t @@ -162,4 +162,4 @@ gelf_getversym(UD Elf_Data *_data, UD int _ndx, UD GElf_Versym *_dst) } #undef UD -#endif /* QUARK */ +#endif /* ELASTIC */ diff --git a/contrib/elftoolchain/libelf/libelf.h b/contrib/elftoolchain/libelf/libelf.h index 7f99dbc5..2785811b 100644 --- a/contrib/elftoolchain/libelf/libelf.h +++ b/contrib/elftoolchain/libelf/libelf.h @@ -214,10 +214,10 @@ Elf_Data *elf_newdata(Elf_Scn *_scn); Elf_Scn *elf_newscn(Elf *_elf); Elf_Scn *elf_nextscn(Elf *_elf, Elf_Scn *_scn); Elf_Cmd elf_next(Elf *_elf); -#ifdef QUARK +#ifdef ELASTIC Elf *elftoolchain_elf_open(int _fd); Elf *elftoolchain_elf_openmemory(char *_image, size_t _size); -#endif /* QUARK */ +#endif /* ELASTIC */ off_t elf_rand(Elf *_elf, off_t _off); Elf_Data *elf_rawdata(Elf_Scn *_scn, Elf_Data *_data); char *elf_rawfile(Elf *_elf, size_t *_size);