facebookexperimental/ExtendedAndroidTools

Adding libbpf

yanivagman opened this issue · 6 comments

Hi,
me again :-)

I'm working on a project which uses libbpf to load the bpf program (moving from bcc to libbpf).
Can I modify ExtendedAndroidTools to statically compile the library to libbpf.a for android?
If so, which files should I edit?

Thanks,
Yaniv

Hi, welcome back :)

I would add libbpf as a standalone project. To do that we would need to create projects/bpf/build.mk and projects/bpf/README with content heavily inspired by remaining projects (see projects/ffi for an example). It seems that libbpf is not packaged with autotools and it does not use cmake. It ships with a makefile and we need to make sure that the Makefile uses ndk and links against all the deps (libelf, zlib). I was able to build libbpf with build.mk that looks like this:

# Copyright (c) Facebook, Inc. and its affiliates.

bpf: $(ANDROID_BUILD_DIR)/bpf.done
fetch-sources: projects/bpf/sources
remove-sources: remove-bpf-sources

ifeq ($(BPF_SOURCES),)
BPF_SOURCES = $(abspath projects/bpf/sources)
$(ANDROID_BUILD_DIR)/bpf: projects/bpf/sources
endif

$(ANDROID_BUILD_DIR)/bpf.done: $(ANDROID_STANDALONE_TOOLCHAIN_DIR) $(ANDROID_BUILD_DIR)/bpf elfutils | $(ANDROID_OUT_DIR)
	cd $(BPF_SOURCES)/src && NO_PKG_CONFIG=1 \
		CC=clang \
		OBJDIR=$(abspath $(ANDROID_BUILD_DIR))/bpf \
		DESTDIR=$(abspath $(ANDROID_OUT_DIR)) \
		PREFIX=/ \
		CFLAGS="-g -O2 -I$(abspath $(ANDROID_OUT_DIR)/include) -I$(abspath $(ANDROID_STANDALONE_TOOLCHAIN_DIR)/sysroot/usr/include) -D__user=''" \
		LDFLAGS="-Wl,-L$(abspath $(ANDROID_OUT_DIR)/lib) -Wl,-L$(abspath $(ANDROID_OUT_DIR)/lib64) -Wl,-L$(abspath $(ANDROID_STANDALONE_TOOLCHAIN_DIR)/sysroot/usr/lib)" \
		make install
	touch $@

$(ANDROID_BUILD_DIR)/bpf: | $(ANDROID_BUILD_DIR)
	mkdir -p $@

BPF_COMMIT = ecbd504994f550162ec7d0a30d13862272afd50b
BPF_REPO = https://github.com/libbpf/libbpf
projects/bpf/sources:
	git clone $(BPF_REPO) $@
	cd $@ && git checkout $(BPF_COMMIT)

.PHONY: remove-bpf-sources
remove-bpf-sources:
	rm -rf projects/bpf/sources

It requires cleanup and libbpf requires testing before we can check this in. I won't have time to look into this within next few weeks, but if you're interested in doing some of that work then do not hesitate to take it over :)

Thanks, I'll try to use that.
I'm going to do some testing with libbpf on android, so I'll report back if this works.

Thanks again!
Yaniv

@yanivagman Hi, how about your trying libbpf work on android now? thanks

Hi @b-ripper,

Eventually I ended up statically compiling my code against libbpf which was easier for my use-case.
So sorry, but I didn't check this...

@b-ripper ,

If you are interested, the method I made it to work on Android is by using a docker container to emulate ARM64 environment:

  1. Prepare compilation environment with docker:
    docker run -it --rm --privileged multiarch/qemu-user-static --credential yes --persistent yes

  2. Use the following Dockerfile to build an image of the build environment (probably not all of the packages below are needed):

FROM arm64v8/golang:alpine
RUN apk update
RUN apk add clang llvm git vim make gcc wget libc6-compat coreutils elfutils-dev linux-headers musl-dev libelf-static zlib-static
WORKDIR /
  1. Run builder container with my sources and libbpf sources mounted and kernel headers as well
    docker run -it --rm -v /path/to/sources:/path/to/sources -v /path/to/kernel/headers:/headers my_builder

  2. Then make libbpf, and compile with my sources

47a27cd added standalone libbpf.so/libbpf.a