dwarves.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luca Boccassi <bluca@debian.org>
To: dwarves@vger.kernel.org
Subject: [PATCH dwarves v3] libbpf: allow to use packaged version
Date: Mon,  4 Jan 2021 22:16:22 +0000	[thread overview]
Message-ID: <20210104221622.256663-1-bluca@debian.org> (raw)
In-Reply-To: <20210103213201.829827-1-bluca@debian.org>

Add a new CMake option, LIBBPF_EMBEDDED, to switch between the
embedded version and the system version (searched via pkg-config)
of libbpf. Set the embedded version as the default.

Signed-off-by: Luca Boccassi <bluca@debian.org>
---
v2: switched default to use embedded version
v3: add lib/include/bpf -> lib/bpf/src symlink to allow using 'system'
    style #include everywhere, rather than #ifdefs.
    Thanks Andrii for the suggestion!

 CMakeLists.txt   | 43 ++++++++++++++++++++++++++++++-------------
 btf_encoder.c    |  4 ++--
 btf_loader.c     |  2 +-
 lib/include/bpf  |  1 +
 libbtf.c         |  7 +++----
 libbtf.h         |  2 +-
 pahole.c         |  2 +-
 pahole_strings.h |  2 +-
 strings.c        |  2 +-
 9 files changed, 41 insertions(+), 24 deletions(-)
 create mode 120000 lib/include/bpf

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 857487a..eefdebe 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,9 +2,24 @@ project(pahole C)
 cmake_minimum_required(VERSION 2.8.8)
 cmake_policy(SET CMP0005 NEW)
 
+option(LIBBPF_EMBEDDED "Use the embedded version of libbpf instead of searching it via pkg-config" ON)
+if (NOT LIBBPF_EMBEDDED)
+	find_package(PkgConfig)
+	if(PKGCONFIG_FOUND)
+		pkg_check_modules(LIBBPF libbpf>=0.3.0)
+	endif()
+endif()
+
 INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}
-		    ${CMAKE_CURRENT_SOURCE_DIR}
-		    ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include/uapi)
+		    ${CMAKE_CURRENT_SOURCE_DIR})
+if(NOT LIBBPF_FOUND)
+	# Allows to use 'system' style #include with both embedded and system libbpf
+	INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/lib/include)
+	INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include/uapi)
+else()
+	INCLUDE_DIRECTORIES(${LIBBPF_INCLUDE_DIRS})
+	LINK_DIRECTORIES(${LIBBPF_LIBRARY_DIRS})
+endif()
 
 # Try to parse this later, Helio just showed me a KDE4 example to support
 # x86-64 builds.
@@ -56,7 +71,7 @@ if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
 		endif()
 	endif()
 endif()
-if(NOT EXISTS "${PROJECT_SOURCE_DIR}/lib/bpf/src/btf.h")
+if(NOT LIBBPF_FOUND AND NOT EXISTS "${PROJECT_SOURCE_DIR}/lib/bpf/src/btf.h")
 	message(FATAL_ERROR "The submodules were not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
 endif()
 
@@ -81,22 +96,24 @@ endif()
 
 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64")
 
-file(GLOB libbpf_sources "lib/bpf/src/*.c")
-add_library(bpf OBJECT ${libbpf_sources})
-set_property(TARGET bpf PROPERTY POSITION_INDEPENDENT_CODE 1)
-target_include_directories(bpf PRIVATE
-			   ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include
-			   ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include/uapi)
+if (NOT LIBBPF_FOUND)
+	file(GLOB libbpf_sources "lib/bpf/src/*.c")
+	add_library(bpf OBJECT ${libbpf_sources})
+	set_property(TARGET bpf PROPERTY POSITION_INDEPENDENT_CODE 1)
+	target_include_directories(bpf PRIVATE
+				   ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include)
+endif()
 
 set(dwarves_LIB_SRCS dwarves.c dwarves_fprintf.c gobuffer strings
 		     ctf_encoder.c ctf_loader.c libctf.c btf_encoder.c btf_loader.c libbtf.c
 		     dwarf_loader.c dutil.c elf_symtab.c rbtree.c)
-add_library(dwarves SHARED ${dwarves_LIB_SRCS} $<TARGET_OBJECTS:bpf>)
+if (NOT LIBBPF_FOUND)
+	list(APPEND dwarves_LIB_SRCS $<TARGET_OBJECTS:bpf>)
+endif()
+add_library(dwarves SHARED ${dwarves_LIB_SRCS})
 set_target_properties(dwarves PROPERTIES VERSION 1.0.0 SOVERSION 1)
 set_target_properties(dwarves PROPERTIES INTERFACE_LINK_LIBRARIES "")
-target_include_directories(dwarves PRIVATE
-			   ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include/uapi)
-target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES})
+target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES})
 
 set(dwarves_emit_LIB_SRCS dwarves_emit.c)
 add_library(dwarves_emit SHARED ${dwarves_emit_LIB_SRCS})
diff --git a/btf_encoder.c b/btf_encoder.c
index 3339730..76e7d43 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -11,12 +11,12 @@
 
 #include "dwarves.h"
 #include "libbtf.h"
-#include "lib/bpf/include/uapi/linux/btf.h"
-#include "lib/bpf/src/libbpf.h"
 #include "hash.h"
 #include "elf_symtab.h"
 #include "btf_encoder.h"
 
+#include <linux/btf.h>
+#include <bpf/libbpf.h>
 #include <ctype.h> /* for isalpha() and isalnum() */
 #include <stdlib.h> /* for qsort() and bsearch() */
 #include <inttypes.h>
diff --git a/btf_loader.c b/btf_loader.c
index ec286f4..fa85d06 100644
--- a/btf_loader.c
+++ b/btf_loader.c
@@ -20,12 +20,12 @@
 #include <string.h>
 #include <limits.h>
 #include <libgen.h>
+#include <linux/btf.h>
 #include <zlib.h>
 
 #include <gelf.h>
 
 #include "libbtf.h"
-#include "lib/bpf/include/uapi/linux/btf.h"
 #include "dutil.h"
 #include "dwarves.h"
 
diff --git a/lib/include/bpf b/lib/include/bpf
new file mode 120000
index 0000000..4c41b71
--- /dev/null
+++ b/lib/include/bpf
@@ -0,0 +1 @@
+../bpf/src
\ No newline at end of file
diff --git a/libbtf.c b/libbtf.c
index 16e1d45..3cc2ac5 100644
--- a/libbtf.c
+++ b/libbtf.c
@@ -16,12 +16,11 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <stdarg.h>
+#include <linux/btf.h>
+#include <bpf/btf.h>
+#include <bpf/libbpf.h>
 
 #include "libbtf.h"
-#include "lib/bpf/include/uapi/linux/btf.h"
-#include "lib/bpf/include/linux/err.h"
-#include "lib/bpf/src/btf.h"
-#include "lib/bpf/src/libbpf.h"
 #include "dutil.h"
 #include "gobuffer.h"
 #include "dwarves.h"
diff --git a/libbtf.h b/libbtf.h
index 191f586..0b99767 100644
--- a/libbtf.h
+++ b/libbtf.h
@@ -11,7 +11,7 @@
 
 #include <stdbool.h>
 #include <stdint.h>
-#include "lib/bpf/src/btf.h"
+#include <bpf/btf.h>
 
 struct btf_elf {
 	void		  *priv;
diff --git a/pahole.c b/pahole.c
index 4a34ba5..68dd166 100644
--- a/pahole.c
+++ b/pahole.c
@@ -16,6 +16,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <bpf/libbpf.h>
 
 #include "dwarves_reorganize.h"
 #include "dwarves.h"
@@ -23,7 +24,6 @@
 #include "ctf_encoder.h"
 #include "btf_encoder.h"
 #include "libbtf.h"
-#include "lib/bpf/src/libbpf.h"
 
 static bool btf_encode;
 static bool ctf_encode;
diff --git a/pahole_strings.h b/pahole_strings.h
index 522fbf2..657701b 100644
--- a/pahole_strings.h
+++ b/pahole_strings.h
@@ -6,7 +6,7 @@
   Copyright (C) 2008 Arnaldo Carvalho de Melo <acme@redhat.com>
 */
 
-#include "lib/bpf/src/btf.h"
+#include <bpf/btf.h>
 
 typedef unsigned int strings_t;
 
diff --git a/strings.c b/strings.c
index d37f49d..8244c49 100644
--- a/strings.c
+++ b/strings.c
@@ -13,9 +13,9 @@
 #include <stdio.h>
 #include <string.h>
 #include <zlib.h>
+#include <bpf/libbpf.h>
 
 #include "dutil.h"
-#include "lib/bpf/src/libbpf.h"
 
 struct strings *strings__new(void)
 {
-- 
2.29.2


  reply	other threads:[~2021-01-04 22:17 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-02 18:22 [PATCH dwarves] libbpf: allow to use packaged version Luca Boccassi
2021-01-03 19:10 ` Andrii Nakryiko
2021-01-03 21:30   ` Luca Boccassi
2021-01-04 20:23     ` Andrii Nakryiko
2021-01-04 22:17       ` Luca Boccassi
2021-01-21 13:29         ` Arnaldo Carvalho de Melo
2021-01-21 20:02           ` Andrii Nakryiko
2021-01-21 20:33             ` Arnaldo Carvalho de Melo
2021-01-21 20:34             ` Arnaldo Carvalho de Melo
2021-01-21 21:19               ` Luca Boccassi
2021-01-15 15:29       ` Arnaldo Carvalho de Melo
2021-01-15 15:40         ` Luca Boccassi
2021-06-09 16:07           ` Arnaldo Carvalho de Melo
2021-06-09 16:11             ` Luca Boccassi
2021-01-03 21:32 ` [PATCH dwarves v2] " Luca Boccassi
2021-01-04 22:16   ` Luca Boccassi [this message]
2021-01-13 11:18     ` [PATCH dwarves v3] " Arnaldo Carvalho de Melo
2021-03-30  4:47     ` Dominique Martinet
2021-03-30 10:50       ` Luca Boccassi
2021-03-30 11:06         ` Luca Boccassi
2021-03-30 11:45           ` Dominique Martinet
2021-03-30 15:12       ` Arnaldo Carvalho de Melo
2021-03-31  1:05         ` Dominique Martinet
2021-04-13 13:42           ` Luca Boccassi
2021-05-18 14:07             ` Luca Boccassi
2021-06-09  4:10               ` Dominique Martinet
2021-06-09 16:25     ` Arnaldo Carvalho de Melo
2021-06-09 16:38       ` Arnaldo Carvalho de Melo
2021-06-09 16:43         ` Luca Boccassi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210104221622.256663-1-bluca@debian.org \
    --to=bluca@debian.org \
    --cc=dwarves@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).