Skip to content

Commit

Permalink
Make this work with cmake, unfinished
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
haesbaert committed Jun 4, 2024
1 parent 4126cfb commit d22763d
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 38 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?=
Expand Down
10 changes: 5 additions & 5 deletions cmake/modules/libelf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand Down
28 changes: 15 additions & 13 deletions contrib/elftoolchain/libelf/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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:
4 changes: 2 additions & 2 deletions contrib/elftoolchain/libelf/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
* $Id$
*/

#ifdef QUARK
#ifdef ELASTIC
#include <sys/elfdefinitions.h>
#endif /* QUARK */
#endif /* ELASTIC */

#endif /* _ELF_H */
4 changes: 2 additions & 2 deletions contrib/elftoolchain/libelf/elf_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ELFTC_VCSID("$Id$");

/*@ELFTC-DOWNSTREAM-VCSID@*/

#ifdef QUARK
#ifdef ELASTIC
/*
* Extension API: open a file for reading, ignoring parse errors.
*/
Expand Down Expand Up @@ -71,4 +71,4 @@ elftoolchain_elf_openmemory(char *image, size_t sz)
return (_libelf_memory((unsigned char *) image, sz, 0));
}

#endif /* QUARK */
#endif /* ELASTIC */
6 changes: 3 additions & 3 deletions contrib/elftoolchain/libelf/gelf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions contrib/elftoolchain/libelf/gelf_shdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -162,4 +162,4 @@ gelf_getversym(UD Elf_Data *_data, UD int _ndx, UD GElf_Versym *_dst)
}

#undef UD
#endif /* QUARK */
#endif /* ELASTIC */
4 changes: 2 additions & 2 deletions contrib/elftoolchain/libelf/libelf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d22763d

Please sign in to comment.