Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1566250ybl; Mon, 2 Dec 2019 05:20:51 -0800 (PST) X-Google-Smtp-Source: APXvYqxmDBoJOERHPSXlmYF9Ke0L9ZdS0c67ov/jtshJuiIFOGbRz2kLgsTeO0dK6KO163m3DAv4 X-Received: by 2002:a17:906:390d:: with SMTP id f13mr10323449eje.151.1575292851314; Mon, 02 Dec 2019 05:20:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575292851; cv=none; d=google.com; s=arc-20160816; b=RA/t0dodDcy1b17745/mmn7b5691/W3YqkCLSI476W0xYPpzG5xycGpkxJ1BchJwEM Sery9KAWVd7Ck5A6NHmtM6R2F086pLw7/8ha3Vr0ZF/kcgbbxCkjn4BiAKWaHCL7XTTK UqkkPHL9jeysheuKm8WZ55xFWV326mRAxPzD49iSq/7Nu+EEgh/+Ok6/uU5utb3cPga+ VdBLT5rTAmj/NZTv4hSloP265N+2zMnHv/gSENVc4HxSHQ7UlnuSochajH4OQHeGyqW7 fICQj98hbGQPKxfIHezZrjLpBIBwuX2S0bwvpbgTPlRE/vHqZRiB3OrACGkFqBosDpL9 vbgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=VjA5/IFt+Zi5fvUEcHrQ0XErgojKDkeAPL3uvrzE6tY=; b=ztFlV42FqiIRTjeAYJtjdKonP/M0wwQtOm9MhiEo2PNSzhSCn78JC0Abhs8uHhG9WG amnH39n7CyStxjmGTyIFiMv1VtpnKkboOeqwbHgT6HOZxdj9S4IUmZ0q8w23MFP2fLoQ /mMDfKkfpy6micVHES0Gtl9znnivyhNO3JGlkyNukHnvYonbCanbIumnTX8bBATeKWd6 9kQOzhXUXKUNDVDc/MmOY8VR4aBBro/sTJayiGO6+fePM1Dr6bWGPpzqz8uqaAWSjPxf 3fGdaZL0RwL6dZ6ch4jqUDwiDLEG2pPQ2vlynY37KcqUhRes0EJN7cycXto+4GsOs3tm VZGA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h13si14477723edv.249.2019.12.02.05.20.26; Mon, 02 Dec 2019 05:20:51 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727562AbfLBNTK convert rfc822-to-8bit (ORCPT + 99 others); Mon, 2 Dec 2019 08:19:10 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:35360 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727533AbfLBNTJ (ORCPT ); Mon, 2 Dec 2019 08:19:09 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-352-jWYtbyG7PlG6nn6dqdpm2A-1; Mon, 02 Dec 2019 08:19:05 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5F73110054E3; Mon, 2 Dec 2019 13:19:03 +0000 (UTC) Received: from krava.redhat.com (unknown [10.43.17.48]) by smtp.corp.redhat.com (Postfix) with ESMTP id DFFDF600C8; Mon, 2 Dec 2019 13:18:59 +0000 (UTC) From: Jiri Olsa To: Arnaldo Carvalho de Melo Cc: lkml , netdev@vger.kernel.org, bpf@vger.kernel.org, Ingo Molnar , Namhyung Kim , Alexander Shishkin , Peter Zijlstra , Michael Petlan , =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= , Jesper Dangaard Brouer , Daniel Borkmann , Alexei Starovoitov , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , Quentin Monnet Subject: [PATCH 2/6] bpftool: Allow to link libbpf dynamically Date: Mon, 2 Dec 2019 14:18:42 +0100 Message-Id: <20191202131847.30837-3-jolsa@kernel.org> In-Reply-To: <20191202131847.30837-1-jolsa@kernel.org> References: <20191202131847.30837-1-jolsa@kernel.org> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-MC-Unique: jWYtbyG7PlG6nn6dqdpm2A-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently we support only static linking with kernel's libbpf (tools/lib/bpf). This patch adds LIBBPF_DYNAMIC compile variable that triggers libbpf detection and bpf dynamic linking: $ make -C tools/bpf/bpftool make LIBBPF_DYNAMIC=1 If libbpf is not installed, build (with LIBBPF_DYNAMIC=1) stops with: $ make -C tools/bpf/bpftool LIBBPF_DYNAMIC=1 Auto-detecting system features: ... libbfd: [ on ] ... disassembler-four-args: [ on ] ... zlib: [ on ] ... libbpf: [ OFF ] Makefile:102: *** Error: No libbpf devel library found, please install libbpf-devel or libbpf-dev. Adding LIBBPF_DIR compile variable to allow linking with libbpf installed into specific directory: $ make -C tools/lib/bpf/ prefix=/tmp/libbpf/ install_lib install_headers $ make -C tools/bpf/bpftool/ LIBBPF_DYNAMIC=1 LIBBPF_DIR=/tmp/libbpf/ It might be needed to clean build tree first because features framework does not detect the change properly: $ make -C tools/build/feature clean $ make -C tools/bpf/bpftool/ clean Since bpftool uses bits of libbpf that are not exported as public API in the .so version, we also pass in libbpf.a to the linker, which allows it to pick up the private functions from the static library without having to expose them as ABI. Signed-off-by: Toke Høiland-Jørgensen Signed-off-by: Jiri Olsa --- tools/bpf/bpftool/Makefile | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile index 39bc6f0f4f0b..be6dea7eb9fd 100644 --- a/tools/bpf/bpftool/Makefile +++ b/tools/bpf/bpftool/Makefile @@ -1,6 +1,21 @@ # SPDX-License-Identifier: GPL-2.0-only + +# To link dynamically with libbpf run: +# make LIBBPF_DYNAMIC=1 +# build will try to detect installed devel package of libbpf. +# +# For linking dynamically with libbpf installed at specific PATH run: +# make LIBBPF_DYNAMIC=1 LIBBPF_DIR= + include ../../scripts/Makefile.include include ../../scripts/utilities.mak +include ../../scripts/Makefile.arch + +ifeq ($(LP64), 1) + libdir_relative = lib64 +else + libdir_relative = lib +endif ifeq ($(srctree),) srctree := $(patsubst %/,%,$(dir $(CURDIR))) @@ -63,6 +78,17 @@ RM ?= rm -f FEATURE_USER = .bpftool FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib FEATURE_DISPLAY = libbfd disassembler-four-args zlib +ifdef LIBBPF_DYNAMIC + FEATURE_TESTS += libbpf + FEATURE_DISPLAY += libbpf + + ifdef LIBBPF_DIR + LIBBPF_CFLAGS := -I$(LIBBPF_DIR)/include + LIBBPF_LDFLAGS := -L$(LIBBPF_DIR)/$(libdir_relative) + FEATURE_CHECK_CFLAGS-libbpf := $(LIBBPF_CFLAGS) + FEATURE_CHECK_LDFLAGS-libbpf := $(LIBBPF_LDFLAGS) + endif +endif check_feat := 1 NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall @@ -88,6 +114,18 @@ ifeq ($(feature-reallocarray), 0) CFLAGS += -DCOMPAT_NEED_REALLOCARRAY endif +ifdef LIBBPF_DYNAMIC + ifeq ($(feature-libbpf), 1) + # bpftool uses non-exported functions from libbpf, so just add the dynamic + # version of libbpf and let the linker figure it out + LIBS := -lbpf $(LIBS) + CFLAGS += $(LIBBPF_CFLAGS) + LDFLAGS += $(LIBBPF_LDFLAGS) + else + dummy := $(error Error: No libbpf devel library found, please install libbpf-devel or libbpf-dev.) + endif +endif + include $(wildcard $(OUTPUT)*.d) all: $(OUTPUT)bpftool -- 2.21.0