Skip to contentSkip to conversation

tools/bpf: generate pkg-config file for libbpf

PATCH Superseded Luca Boccassi 6 participants 31 messages Mar 19, 2019 – Mar 28, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/bpf: generate pkg-config file for libbpf

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 18 +++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +15 -3
index a05c43468bd0..542c64e2a6d9 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)
@@ -137,7 +138,7 @@ GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
 			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
 
-CMD_TARGETS = $(LIB_FILE)
+CMD_TARGETS = $(LIB_FILE) $(PC_FILE)
 
 CXX_TEST_TARGET = $(OUTPUT)test_libbpf
 
@@ -179,6 +180,12 @@ $(OUTPUT)libbpf.a: $(BPF_IN)
 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
 	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
 
+$(OUTPUT)libbpf.pc:
+	$(QUIET_LINK)sed -e "s|@PREFIX@|$(prefix)|" \
+		-e "s|@LIBDIR@|$(libdir_SQ)|" \
+		-e "s|@VERSION@|$(shell make --no-print-directory -sC ../../.. kernelversion)|" \
+		< [email protected] > $@
+
 check: check_abi
 
 check_abi: $(OUTPUT)libbpf.so
@@ -208,7 +215,12 @@ install_headers:
 		$(call do_install,libbpf.h,$(prefix)/include/bpf,644);
 		$(call do_install,btf.h,$(prefix)/include/bpf,644);
 
-install: install_lib
+install_pkgconfig: $(PC_FILE)
+	$(call QUIET_INSTALL, $(PC_FILE)) \
+		$(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
+
+
+install: install_lib install_pkgconfig
 
 ### Cleaning rules
 
@@ -218,7 +230,7 @@ config-clean:
 
 clean:
 	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
-		*.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS
+		*.o *~ *.a *.so .*.d .*.cmd *.pc LIBBPF-CFLAGS
 	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
 
 
tools/lib/bpf/libbpf.pc.template new file +11 -0
new file mode 100644
index 000000000000..0cac2f5f54a6
@@ -0,0 +1,11 @@
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include/bpf
+
+Name: libbpf
+URL: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
+Description: Linux kernel BPF library
+Version: @VERSION@
+Libs: -L${libdir} -lbpf
+Requires.private: libelf
+Cflags: -I${includedir}
Signature · 1 line
2.20.1
#1 lore raw
Quoted 42 lines · Luca Boccassi

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 18 +++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +2 -1
index a05c43468bd0..542c64e2a6d9 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)
@@ -137,7 +138,7 @@ GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
 			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
 
-CMD_TARGETS = $(LIB_FILE)
+CMD_TARGETS = $(LIB_FILE) $(PC_FILE)
 
 CXX_TEST_TARGET = $(OUTPUT)test_libbpf
 

tools/lib/bpf/Makefile $(OUTPUT)libbpf.a: $(BPF_IN) · line 184

Quoted 5 lines
tools/lib/bpf/Makefile +2 -0
@@ -179,6 +180,12 @@ $(OUTPUT)libbpf.a: $(BPF_IN)
 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
 	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
 
+$(OUTPUT)libbpf.pc:
+	$(QUIET_LINK)sed -e "s|@PREFIX@|$(prefix)|" \

Maybe QUIET_GEN instead? Or QUIET_INSTALL (tools/lib/traceevent/Makefile)?

tools/lib/bpf/Makefile $(OUTPUT)libbpf.a: $(BPF_IN) · line 186

Quoted 2 lines
tools/lib/bpf/Makefile +2 -0
+		-e "s|@LIBDIR@|$(libdir_SQ)|" \
+		-e "s|@VERSION@|$(shell make --no-print-directory -sC ../../.. kernelversion)|" \

Make it its own variable, like we do in bpftool?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/bpf/bpftool/Makefile#n26

Also, I wonder what would happen in the case of out-of-tree libbpf:
https://github.com/libbpf/libbpf

Should there be some fallback?

Quoted 48 lines
tools/lib/bpf/Makefile +9 -2
+		< [email protected] > $@
+
 check: check_abi
 
 check_abi: $(OUTPUT)libbpf.so
@@ -208,7 +215,12 @@ install_headers:
 		$(call do_install,libbpf.h,$(prefix)/include/bpf,644);
 		$(call do_install,btf.h,$(prefix)/include/bpf,644);
 
-install: install_lib
+install_pkgconfig: $(PC_FILE)
+	$(call QUIET_INSTALL, $(PC_FILE)) \
+		$(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
+
+
+install: install_lib install_pkgconfig
 
 ### Cleaning rules
 
@@ -218,7 +230,7 @@ config-clean:
 
 clean:
 	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
-		*.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS
+		*.o *~ *.a *.so .*.d .*.cmd *.pc LIBBPF-CFLAGS
 	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
 
 
tools/lib/bpf/libbpf.pc.template new file +11 -0
new file mode 100644
index 000000000000..0cac2f5f54a6
@@ -0,0 +1,11 @@
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include/bpf
+
+Name: libbpf
+URL: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
+Description: Linux kernel BPF library
+Version: @VERSION@
+Libs: -L${libdir} -lbpf
+Requires.private: libelf
+Cflags: -I${includedir}
Signature · 1 line
2.20.1
#2 lore raw
Quoted 46 lines · Stanislav Fomichev
Quoted 45 lines · Luca Boccassi

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
 tools/lib/bpf/.gitignore         |  1 + tools/lib/bpf/Makefile           | 18 +++++++++++++++--- tools/lib/bpf/libbpf.pc.template | 11 +++++++++++

 3 files changed, 27 insertions(+), 3 deletions(-)

 create mode 100644 tools/lib/bpf/libbpf.pc.template

tools/lib/bpf/.gitignore +0 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@

 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf

tools/lib/bpf/Makefile +0 -0
index a05c43468bd0..542c64e2a6d9 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
  libdir_relative_SQ = $(subst ','\'',$(libdir_relative))


 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc

 VERSION = $(BPF_VERSION)
 PATCHLEVEL = $(BPF_PATCHLEVEL)

tools/lib/bpf/Makefile +0 -0
@@ -137,7 +138,7 @@ GLOBAL_SYM_COUNT = $(shell readelf -s --wide
 $(BPF_IN) | \

 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
        grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)

tools/lib/bpf/Makefile +1 -1
-CMD_TARGETS = $(LIB_FILE)
+CMD_TARGETS = $(LIB_FILE) $(PC_FILE)


 CXX_TEST_TARGET = $(OUTPUT)test_libbpf

tools/lib/bpf/Makefile +0 -0
@@ -179,6 +180,12 @@ $(OUTPUT)libbpf.a: $(BPF_IN)
Quoted 8 lines

tools/lib/bpf/Makefile $(OUTPUT)libbpf.a: $(BPF_IN) · line 184

Quoted 5 lines

 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a $(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
+$(OUTPUT)libbpf.pc:

tools/lib/bpf/Makefile +1 -0
+	$(QUIET_LINK)sed -e "s|@PREFIX@|$(prefix)|" \

Maybe QUIET_GEN instead? Or QUIET_INSTALL
(tools/lib/traceevent/Makefile)?

Changed to QUIET_GEN in v2.

Quoted 7 lines

tools/lib/bpf/Makefile

Quoted 3 lines
tools/lib/bpf/Makefile +2 -1
+		-e "s|@LIBDIR@|$(libdir_SQ)|" \
+		-e "s|@VERSION@|$(shell make --no-print-directory
-sC ../../.. kernelversion)|" \

Make it its own variable, like we do in bpftool?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tr
ee/tools/bpf/bpftool/Makefile#n26

Done in v2.

Quoted 4 lines

Also, I wonder what would happen in the case of out-of-tree libbpf:
https://github.com/libbpf/libbpf

Should there be some fallback?

That tree looks like it's autosynced from the kernel tree, isn't it? Then it will just pick up the change and start generating the pc file as well, won't it?

Signature · 2 lines
Kind regards,
Luca Boccassi
#3 lore raw
Quoted 68 lines · Luca Boccassi
Quoted 46 lines · Stanislav Fomichev
Quoted 45 lines · Luca Boccassi

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
 tools/lib/bpf/.gitignore         |  1 + tools/lib/bpf/Makefile           | 18 +++++++++++++++--- tools/lib/bpf/libbpf.pc.template | 11 +++++++++++

 3 files changed, 27 insertions(+), 3 deletions(-)

 create mode 100644 tools/lib/bpf/libbpf.pc.template

tools/lib/bpf/.gitignore +0 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@

 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf

tools/lib/bpf/Makefile +0 -0
index a05c43468bd0..542c64e2a6d9 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
  libdir_relative_SQ = $(subst ','\'',$(libdir_relative))


 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc

 VERSION = $(BPF_VERSION)
 PATCHLEVEL = $(BPF_PATCHLEVEL)

tools/lib/bpf/Makefile +0 -0
@@ -137,7 +138,7 @@ GLOBAL_SYM_COUNT = $(shell readelf -s --wide
 $(BPF_IN) | \

 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
        grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)

tools/lib/bpf/Makefile +1 -1
-CMD_TARGETS = $(LIB_FILE)
+CMD_TARGETS = $(LIB_FILE) $(PC_FILE)


 CXX_TEST_TARGET = $(OUTPUT)test_libbpf

tools/lib/bpf/Makefile +0 -0
@@ -179,6 +180,12 @@ $(OUTPUT)libbpf.a: $(BPF_IN)
Quoted 8 lines

tools/lib/bpf/Makefile $(OUTPUT)libbpf.a: $(BPF_IN) · line 184

Quoted 5 lines

 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a $(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
+$(OUTPUT)libbpf.pc:

tools/lib/bpf/Makefile +1 -0
+	$(QUIET_LINK)sed -e "s|@PREFIX@|$(prefix)|" \

Maybe QUIET_GEN instead? Or QUIET_INSTALL
(tools/lib/traceevent/Makefile)?

Changed to QUIET_GEN in v2.

Quoted 7 lines

tools/lib/bpf/Makefile

Quoted 3 lines
tools/lib/bpf/Makefile +2 -1
+		-e "s|@LIBDIR@|$(libdir_SQ)|" \
+		-e "s|@VERSION@|$(shell make --no-print-directory
-sC ../../.. kernelversion)|" \

Make it its own variable, like we do in bpftool?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tr
ee/tools/bpf/bpftool/Makefile#n26

Done in v2.

Quoted 8 lines
Quoted 4 lines

Also, I wonder what would happen in the case of out-of-tree libbpf:
https://github.com/libbpf/libbpf

Should there be some fallback?

That tree looks like it's autosynced from the kernel tree, isn't it? Then it will just pick up the change and start generating the pc file as well, won't it?

Yes, it's autosynched, but it autosyncs only tools/lib/bpf directory and it lacks top-level makefile with kernelversion target. I guess in this case make will spill out an error to stderr and version will be empty.

Quoted 4 lines
Signature · 3 lines
-- 
Kind regards,
Luca Boccassi
#4 lore raw

PATCH v2 1/1 tools/bpf: generate pkg-config file for libbpf

In reply to Luca Boccassi

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 19 ++++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +16 -3
index a05c43468bd0..1df3ebfb3118 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)
@@ -89,6 +90,7 @@ OBJ		= $@
 N		=
 
 LIBBPF_VERSION = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
+KERNEL_VERSION = $(shell make --no-print-directory -sC ../../.. kernelversion)
 
 # Set compile option CFLAGS
 ifdef EXTRA_CFLAGS
@@ -137,7 +139,7 @@ GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
 			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
 
-CMD_TARGETS = $(LIB_FILE)
+CMD_TARGETS = $(LIB_FILE) $(PC_FILE)
 
 CXX_TEST_TARGET = $(OUTPUT)test_libbpf
 
@@ -179,6 +181,12 @@ $(OUTPUT)libbpf.a: $(BPF_IN)
 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
 	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
 
+$(OUTPUT)libbpf.pc:
+	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
+		-e "s|@LIBDIR@|$(libdir_SQ)|" \
+		-e "s|@VERSION@|$(KERNEL_VERSION)|" \
+		< [email protected] > $@
+
 check: check_abi
 
 check_abi: $(OUTPUT)libbpf.so
@@ -208,7 +216,12 @@ install_headers:
 		$(call do_install,libbpf.h,$(prefix)/include/bpf,644);
 		$(call do_install,btf.h,$(prefix)/include/bpf,644);
 
-install: install_lib
+install_pkgconfig: $(PC_FILE)
+	$(call QUIET_INSTALL, $(PC_FILE)) \
+		$(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
+
+
+install: install_lib install_pkgconfig
 
 ### Cleaning rules
 
@@ -218,7 +231,7 @@ config-clean:
 
 clean:
 	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
-		*.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS
+		*.o *~ *.a *.so .*.d .*.cmd *.pc LIBBPF-CFLAGS
 	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
 
 
tools/lib/bpf/libbpf.pc.template new file +11 -0
new file mode 100644
index 000000000000..0cac2f5f54a6
@@ -0,0 +1,11 @@
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include/bpf
+
+Name: libbpf
+URL: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
+Description: Linux kernel BPF library
+Version: @VERSION@
+Libs: -L${libdir} -lbpf
+Requires.private: libelf
+Cflags: -I${includedir}
Signature · 1 line
2.20.1
#5 lore raw
Quoted 37 lines · Luca Boccassi

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 19 ++++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +1 -0
index a05c43468bd0..1df3ebfb3118 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)

tools/lib/bpf/Makefile OBJ = $@ · line 93

Quoted 4 lines
tools/lib/bpf/Makefile +1 -0
@@ -89,6 +90,7 @@ OBJ		= $@
 N		=
 
 LIBBPF_VERSION = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
+KERNEL_VERSION = $(shell make --no-print-directory -sC ../../.. kernelversion)

Oh, we do already have LIBBPF_VERSION, why not use that? This way you don't need to do anything for out-of-tree libbpf from github.

I don't remember what was the strategy regarding libbpf versioning, but that 0.0.1 should be changed to something sensible (be set to the kernel version upon release?)

Quoted 68 lines
tools/lib/bpf/Makefile +14 -3
 # Set compile option CFLAGS
 ifdef EXTRA_CFLAGS
@@ -137,7 +139,7 @@ GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
 			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
 
-CMD_TARGETS = $(LIB_FILE)
+CMD_TARGETS = $(LIB_FILE) $(PC_FILE)
 
 CXX_TEST_TARGET = $(OUTPUT)test_libbpf
 
@@ -179,6 +181,12 @@ $(OUTPUT)libbpf.a: $(BPF_IN)
 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
 	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
 
+$(OUTPUT)libbpf.pc:
+	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
+		-e "s|@LIBDIR@|$(libdir_SQ)|" \
+		-e "s|@VERSION@|$(KERNEL_VERSION)|" \
+		< [email protected] > $@
+
 check: check_abi
 
 check_abi: $(OUTPUT)libbpf.so
@@ -208,7 +216,12 @@ install_headers:
 		$(call do_install,libbpf.h,$(prefix)/include/bpf,644);
 		$(call do_install,btf.h,$(prefix)/include/bpf,644);
 
-install: install_lib
+install_pkgconfig: $(PC_FILE)
+	$(call QUIET_INSTALL, $(PC_FILE)) \
+		$(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
+
+
+install: install_lib install_pkgconfig
 
 ### Cleaning rules
 
@@ -218,7 +231,7 @@ config-clean:
 
 clean:
 	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
-		*.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS
+		*.o *~ *.a *.so .*.d .*.cmd *.pc LIBBPF-CFLAGS
 	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
 
 
tools/lib/bpf/libbpf.pc.template new file +11 -0
new file mode 100644
index 000000000000..0cac2f5f54a6
@@ -0,0 +1,11 @@
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include/bpf
+
+Name: libbpf
+URL: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
+Description: Linux kernel BPF library
+Version: @VERSION@
+Libs: -L${libdir} -lbpf
+Requires.private: libelf
+Cflags: -I${includedir}
Signature · 1 line
2.20.1
#6 lore raw
Quoted 47 lines · Stanislav Fomichev
Quoted 37 lines · Luca Boccassi

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 19 ++++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +1 -0
index a05c43468bd0..1df3ebfb3118 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)

tools/lib/bpf/Makefile OBJ = $@ · line 95

Quoted 6 lines
tools/lib/bpf/Makefile +1 -0
@@ -89,6 +90,7 @@ OBJ		= $@
 N		=
 
 LIBBPF_VERSION =
 $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
+KERNEL_VERSION = $(shell make --no-print-directory -sC ../../..
 kernelversion)

Oh, we do already have LIBBPF_VERSION, why not use that? This way you don't need to do anything for out-of-tree libbpf from github.

Quoted 5 lines

I don't remember what was the strategy regarding libbpf versioning, but
that 0.0.1 should be changed to something sensible (be set to the kernel
version upon release?)

[re-sending as reply via phone added html and the list daemon said no- no]

That looks like the ABI version though, rather than the source version? The source version is more appropriate for the PC file

Signature · 2 lines
Kind regards,
Luca Boccassi
#7 lore raw
Quoted 54 lines · Luca Boccassi
Quoted 47 lines · Stanislav Fomichev
Quoted 37 lines · Luca Boccassi

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 19 ++++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +1 -0
index a05c43468bd0..1df3ebfb3118 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)

tools/lib/bpf/Makefile OBJ = $@ · line 95

Quoted 6 lines
tools/lib/bpf/Makefile +1 -0
@@ -89,6 +90,7 @@ OBJ		= $@
 N		=
 
 LIBBPF_VERSION =
 $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
+KERNEL_VERSION = $(shell make --no-print-directory -sC ../../..
 kernelversion)

Oh, we do already have LIBBPF_VERSION, why not use that? This way you don't need to do anything for out-of-tree libbpf from github.

Quoted 5 lines

I don't remember what was the strategy regarding libbpf versioning, but
that 0.0.1 should be changed to something sensible (be set to the kernel
version upon release?)

Quoted 7 lines

[re-sending as reply via phone added html and the list daemon said no-
no]

That looks like the ABI version though, rather than the source version?
The source version is more appropriate for the PC file

That said, the versioning looks looks like it could use some love and it's not really a factor that should gate a pkg-config file and could be done separately if required, so in v3 I've removed KERNEL_VERSION and used LIBBPF_VERSION as you suggested.

Thanks for reviewing!

Signature · 2 lines
Kind regards,
Luca Boccassi
#8 lore raw
Quoted 59 lines · Luca Boccassi
Quoted 58 lines · Luca Boccassi
Quoted 47 lines · Stanislav Fomichev
Quoted 37 lines · Luca Boccassi

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 19 ++++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +1 -0
index a05c43468bd0..1df3ebfb3118 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)

tools/lib/bpf/Makefile OBJ = $@ · line 95

Quoted 6 lines
tools/lib/bpf/Makefile +1 -0
@@ -89,6 +90,7 @@ OBJ		= $@
 N		=
 
 LIBBPF_VERSION =
 $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
+KERNEL_VERSION = $(shell make --no-print-directory -sC ../../..
 kernelversion)

Oh, we do already have LIBBPF_VERSION, why not use that? This way you don't need to do anything for out-of-tree libbpf from github.

Quoted 5 lines

I don't remember what was the strategy regarding libbpf versioning, but
that 0.0.1 should be changed to something sensible (be set to the kernel
version upon release?)

[re-sending as reply via phone added html and the list daemon said no-
no]

Quoted 8 lines
Quoted 3 lines

That looks like the ABI version though, rather than the source version?
The source version is more appropriate for the PC file

That said, the versioning looks looks like it could use some love and it's not really a factor that should gate a pkg-config file and could be done separately if required, so in v3 I've removed KERNEL_VERSION and used LIBBPF_VERSION as you suggested.

Agree :-) There was some conversation recently about incrementing it per kernel release vs real api change. I don't remember what the outcome was exactly.

Quoted 5 lines

Thanks for reviewing!

Signature · 2 lines
Kind regards,
Luca Boccassi
#9 lore raw
Quoted 80 lines · Stanislav Fomichev
Quoted 72 lines · Luca Boccassi
Quoted 62 lines · Luca Boccassi
Quoted 53 lines · Stanislav Fomichev
Quoted 45 lines · Luca Boccassi

Generate a libbpf.pc file at build time so that users can rely
on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of
calling
make inline

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 19 ++++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +2 -0
index a05c43468bd0..1df3ebfb3118 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)
@@ -89,6 +90,7 @@ OBJ		= $@
 N		=
 
 LIBBPF_VERSION =
 $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
+KERNEL_VERSION = $(shell make --no-print-directory -sC
Quoted 2 lines

../../..
kernelversion)

Oh, we do already have LIBBPF_VERSION, why not use that? This way
you don't need to do anything for out-of-tree libbpf from github.

Quoted 7 lines

I don't remember what was the strategy regarding libbpf versioning,
but
that 0.0.1 should be changed to something sensible (be set to the
kernel
version upon release?)

Quoted 8 lines

[re-sending as reply via phone added html and the list daemon said
no-
no]

That looks like the ABI version though, rather than the source version?
The source version is more appropriate for the PC file

Quoted 7 lines

That said, the versioning looks looks like it could use some love and
it's not really a factor that should gate a pkg-config file and could
be done separately if required, so in v3 I've removed KERNEL_VERSION
and used LIBBPF_VERSION as you suggested.

Quoted 4 lines

Agree :-) There was some conversation recently about incrementing it per
kernel release vs real api change. I don't remember what the outcome was exactly.

I see, thanks. Anything else you'd like me to change?

Signature · 2 lines
Kind regards,
Luca Boccassi
#10 lore raw
Quoted 81 lines · Luca Boccassi
Quoted 80 lines · Stanislav Fomichev
Quoted 79 lines · Luca Boccassi
Quoted 62 lines · Luca Boccassi
Quoted 53 lines · Stanislav Fomichev
Quoted 45 lines · Luca Boccassi

Generate a libbpf.pc file at build time so that users can rely
on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of
calling
make inline

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 19 ++++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +2 -0
index a05c43468bd0..1df3ebfb3118 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)
@@ -89,6 +90,7 @@ OBJ		= $@
 N		=
 
 LIBBPF_VERSION =
 $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
+KERNEL_VERSION = $(shell make --no-print-directory -sC
Quoted 2 lines

../../..
kernelversion)

Oh, we do already have LIBBPF_VERSION, why not use that? This way
you don't need to do anything for out-of-tree libbpf from github.

Quoted 7 lines

I don't remember what was the strategy regarding libbpf versioning,
but
that 0.0.1 should be changed to something sensible (be set to the
kernel
version upon release?)

Quoted 8 lines

[re-sending as reply via phone added html and the list daemon said
no-
no]

That looks like the ABI version though, rather than the source version?
The source version is more appropriate for the PC file

That said, the versioning looks looks like it could use some love and
it's not really a factor that should gate a pkg-config file and could
be done separately if required, so in v3 I've removed KERNEL_VERSION
and used LIBBPF_VERSION as you suggested.

Quoted 6 lines
Quoted 4 lines

Agree :-) There was some conversation recently about incrementing it per
kernel release vs real api change. I don't remember what the outcome was exactly.

I see, thanks. Anything else you'd like me to change?

No, everything looks good to me, wait for the reply from the bpf maintainers.

Quoted 3 lines
Signature · 2 lines
Kind regards,
Luca Boccassi
maintainer
#11 lore raw
Quoted 82 lines · Stanislav Fomichev
Quoted 81 lines · Luca Boccassi
Quoted 80 lines · Stanislav Fomichev
Quoted 79 lines · Luca Boccassi
Quoted 62 lines · Luca Boccassi
Quoted 53 lines · Stanislav Fomichev
Quoted 45 lines · Luca Boccassi

Generate a libbpf.pc file at build time so that users can rely
on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of
calling
make inline

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 19 ++++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +2 -0
index a05c43468bd0..1df3ebfb3118 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)
@@ -89,6 +90,7 @@ OBJ		= $@
 N		=
 
 LIBBPF_VERSION =
 $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
+KERNEL_VERSION = $(shell make --no-print-directory -sC
Quoted 2 lines

../../..
kernelversion)

Oh, we do already have LIBBPF_VERSION, why not use that? This way
you don't need to do anything for out-of-tree libbpf from github.

Quoted 7 lines

I don't remember what was the strategy regarding libbpf versioning,
but
that 0.0.1 should be changed to something sensible (be set to the
kernel
version upon release?)

Quoted 8 lines

[re-sending as reply via phone added html and the list daemon said
no-
no]

That looks like the ABI version though, rather than the source version?
The source version is more appropriate for the PC file

That said, the versioning looks looks like it could use some love and
it's not really a factor that should gate a pkg-config file and could
be done separately if required, so in v3 I've removed KERNEL_VERSION
and used LIBBPF_VERSION as you suggested.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Agree :-) There was some conversation recently about incrementing it per
kernel release vs real api change. I don't remember what the outcome was exactly.

I see, thanks. Anything else you'd like me to change?

No, everything looks good to me, wait for the reply from the bpf maintainers.

Current workflow regarding libbpf versioning is that version number increase is synced with kernel release. Meaning, new development cycle we bump the version when we e.g. add entries into libbpf.map file for new api functions.

#12 lore raw
Quoted 88 lines · Daniel Borkmann
Quoted 87 lines · Stanislav Fomichev
Quoted 86 lines · Luca Boccassi
Quoted 84 lines · Stanislav Fomichev
Quoted 72 lines · Luca Boccassi
Quoted 62 lines · Luca Boccassi
Quoted 53 lines · Stanislav Fomichev
Quoted 45 lines · Luca Boccassi

Generate a libbpf.pc file at build time so that users can rely
on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of
calling
make inline

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 19 ++++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +2 -0
index a05c43468bd0..1df3ebfb3118 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)
@@ -89,6 +90,7 @@ OBJ		= $@
 N		=
 
 LIBBPF_VERSION =
 $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
+KERNEL_VERSION = $(shell make --no-print-directory -sC
Quoted 2 lines

../../..
kernelversion)

Oh, we do already have LIBBPF_VERSION, why not use that? This way
you don't need to do anything for out-of-tree libbpf from github.

Quoted 7 lines

I don't remember what was the strategy regarding libbpf versioning,
but
that 0.0.1 should be changed to something sensible (be set to the
kernel
version upon release?)

Quoted 8 lines

[re-sending as reply via phone added html and the list daemon said
no-
no]

That looks like the ABI version though, rather than the source version?
The source version is more appropriate for the PC file

Quoted 7 lines

That said, the versioning looks looks like it could use some love and
it's not really a factor that should gate a pkg-config file and could
be done separately if required, so in v3 I've removed KERNEL_VERSION
and used LIBBPF_VERSION as you suggested.

Agree :-) There was some conversation recently about incrementing it per
kernel release vs real api change. I don't remember what the outcome was exactly.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

I see, thanks. Anything else you'd like me to change?

No, everything looks good to me, wait for the reply from the bpf maintainers.

Current workflow regarding libbpf versioning is that version number increase is synced with kernel release. Meaning, new development cycle we bump the version when we e.g. add entries into libbpf.map file for new api functions.

So it will have its own version, not matching the kernel? I thought we would keep it 0.0.X until we figure things out and then just follow the kernel. (I'm fine either way, just wondering).

It's still 0.0.1 in Linus's tree, bpf and in bpf-next btw, even though we already have LIBBPF_0.0.2 in the libbpf.map file :-)

#13 lore raw

PATCH v3 1/1 tools/bpf: generate pkg-config file for libbpf

In reply to Luca Boccassi

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline
v3: use LIBBPF_VERSION instead of kernel_version

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 18 +++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +15 -3
index 61aaacf0cfa1..891fe3da1410 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)
@@ -137,7 +138,7 @@ GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
 			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
 
-CMD_TARGETS = $(LIB_FILE)
+CMD_TARGETS = $(LIB_FILE) $(PC_FILE)
 
 CXX_TEST_TARGET = $(OUTPUT)test_libbpf
 
@@ -180,6 +181,12 @@ $(OUTPUT)libbpf.a: $(BPF_IN)
 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
 	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
 
+$(OUTPUT)libbpf.pc:
+	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
+		-e "s|@LIBDIR@|$(libdir_SQ)|" \
+		-e "s|@VERSION@|$(LIBBPF_VERSION)|" \
+		< [email protected] > $@
+
 check: check_abi
 
 check_abi: $(OUTPUT)libbpf.so
@@ -209,7 +216,12 @@ install_headers:
 		$(call do_install,libbpf.h,$(prefix)/include/bpf,644);
 		$(call do_install,btf.h,$(prefix)/include/bpf,644);
 
-install: install_lib
+install_pkgconfig: $(PC_FILE)
+	$(call QUIET_INSTALL, $(PC_FILE)) \
+		$(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
+
+
+install: install_lib install_pkgconfig
 
 ### Cleaning rules
 
@@ -219,7 +231,7 @@ config-clean:
 
 clean:
 	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
-		*.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS
+		*.o *~ *.a *.so .*.d .*.cmd *.pc LIBBPF-CFLAGS
 	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
 
 
tools/lib/bpf/libbpf.pc.template new file +11 -0
new file mode 100644
index 000000000000..0cac2f5f54a6
@@ -0,0 +1,11 @@
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include/bpf
+
+Name: libbpf
+URL: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
+Description: Linux kernel BPF library
+Version: @VERSION@
+Libs: -L${libdir} -lbpf
+Requires.private: libelf
+Cflags: -I${includedir}
Signature · 1 line
2.20.1
#14 lore raw

[email protected] <[email protected]> [Wed, 2019-03-20 06:28 -0700]:

tools/lib/bpf/Makefile

Quoted 31 lines

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline
v3: use LIBBPF_VERSION instead of kernel_version

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 18 +++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +0 -0
index 61aaacf0cfa1..891fe3da1410 100644

Commenting here rather than on v1/v2 discussion.

Makefile on the github mirror is completely different one:
https://github.com/libbpf/libbpf/blob/master/src/Makefile

It was done like this to avoid the need to patch copied Makefile, mirror as few files as possible and avoid things like kernel & tools UAPI comparison, "feature" tests and other kernel-tree-only stuff that is not applicable to out-of-tree code.

I.e. if somebody uses libbpf mirror and wants to support pkg-config they would need to make similar changes there.

Just wondering if you plan to create a package using kernel tree?

Quoted 31 lines
tools/lib/bpf/Makefile +8 -1
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)
@@ -137,7 +138,7 @@ GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
 			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
 
-CMD_TARGETS = $(LIB_FILE)
+CMD_TARGETS = $(LIB_FILE) $(PC_FILE)
 
 CXX_TEST_TARGET = $(OUTPUT)test_libbpf
 
@@ -180,6 +181,12 @@ $(OUTPUT)libbpf.a: $(BPF_IN)
 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
 	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
 
+$(OUTPUT)libbpf.pc:
+	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
+		-e "s|@LIBDIR@|$(libdir_SQ)|" \
+		-e "s|@VERSION@|$(LIBBPF_VERSION)|" \
+		< [email protected] > $@
+
 check: check_abi
 
 check_abi: $(OUTPUT)libbpf.so

tools/lib/bpf/Makefile install_headers: · line 224

Quoted 10 lines
tools/lib/bpf/Makefile +6 -1
@@ -209,7 +216,12 @@ install_headers:
 		$(call do_install,libbpf.h,$(prefix)/include/bpf,644);
 		$(call do_install,btf.h,$(prefix)/include/bpf,644);
 
-install: install_lib
+install_pkgconfig: $(PC_FILE)
+	$(call QUIET_INSTALL, $(PC_FILE)) \
+		$(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
+
+
+install: install_lib install_pkgconfig

Should it be part of 'install'? Not everyone needs pc file. Also pc template below relies on headers to be installed along with the library itself, but headers are not installed as part of 'install' target, they're in a separate 'install_headers' one, so 'install_pkgconfig' and 'install_headers' should probably be consistent in this regard.

Quoted 17 lines
tools/lib/bpf/Makefile +1 -1
 ### Cleaning rules
 
@@ -219,7 +231,7 @@ config-clean:
 
 clean:
 	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
-		*.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS
+		*.o *~ *.a *.so .*.d .*.cmd *.pc LIBBPF-CFLAGS
 	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
 
 
tools/lib/bpf/libbpf.pc.template new file +0 -0
new file mode 100644
index 000000000000..0cac2f5f54a6

tools/lib/bpf/libbpf.pc.template line 3

Quoted 3 lines
tools/lib/bpf/libbpf.pc.template new file +3 -0
@@ -0,0 +1,11 @@
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include/bpf

I guess it should be includedir=${prefix}/include.

Paths in '#include' are usually provided relative to ${prefix}/include, e.g. all programs I have access to and that use libbpf do this:

  #include <bpf/bpf.h>
  #include <bpf/libbpf.h>

But includedir=${prefix}/include/bpf will force it to be:
#include <bpf.h>
#include <libbpf.h>

, what may create inconsistency with already written code.

I also checked .pc files on my devbox and this is what I see:

  % grep -h includedir /usr/share/pkgconfig/*.pc | sort | uniq -c
       28 Cflags: -I${includedir}
        1 Cflags: -I${includedir}/X11/dri
       29 includedir=/usr/include
Quoted 11 lines
tools/lib/bpf/libbpf.pc.template new file +8 -0
+
+Name: libbpf
+URL: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
+Description: Linux kernel BPF library
+Version: @VERSION@
+Libs: -L${libdir} -lbpf
+Requires.private: libelf
+Cflags: -I${includedir}
Signature · 1 line
2.20.1
Signature · 1 line
Andrey Ignatov
#15 lore raw
Quoted 46 lines · Andrey Ignatov

[email protected] <[email protected]> [Wed, 2019-03-20 06:28 -0700]:

tools/lib/bpf/Makefile

Quoted 31 lines

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline
v3: use LIBBPF_VERSION instead of kernel_version

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 18 +++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +0 -0
index 61aaacf0cfa1..891fe3da1410 100644

Commenting here rather than on v1/v2 discussion.

Makefile on the github mirror is completely different one:
https://github.com/libbpf/libbpf/blob/master/src/Makefile

It was done like this to avoid the need to patch copied Makefile, mirror
as few files as possible and avoid things like kernel & tools UAPI comparison, "feature" tests and other kernel-tree-only stuff that is not
applicable to out-of-tree code.

Quoted 3 lines

I.e. if somebody uses libbpf mirror and wants to support pkg-config they
would need to make similar changes there.

I'm happy to work on a patch for that tree as well, once this one is accepted.

Quoted 1 line

Just wondering if you plan to create a package using kernel tree?

Debian already is distributing a libbpf package built from the kernel source tree, it will be part of Debian 10 (note: I'm not a Debian kernel maintainer):

https://packages.debian.org/buster/libbpf4.19

Quoted 37 lines
Quoted 37 lines
tools/lib/bpf/Makefile +2 -1
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)
@@ -137,7 +138,7 @@ GLOBAL_SYM_COUNT = $(shell readelf -s --wide
 $(BPF_IN) | \
 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so
 | \
 			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 |
 sort -u | wc -l)
 
-CMD_TARGETS = $(LIB_FILE)
+CMD_TARGETS = $(LIB_FILE) $(PC_FILE)

CXX_TEST_TARGET = $(OUTPUT)test_libbpf

tools/lib/bpf/Makefile +6 -0
@@ -180,6 +181,12 @@ $(OUTPUT)libbpf.a: $(BPF_IN)
 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
 	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
 
+$(OUTPUT)libbpf.pc:
+	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
+		-e "s|@LIBDIR@|$(libdir_SQ)|" \
+		-e "s|@VERSION@|$(LIBBPF_VERSION)|" \
+		< [email protected] > $@
+
 check: check_abi
 
 check_abi: $(OUTPUT)libbpf.so
@@ -209,7 +216,12 @@ install_headers:
 		$(call do_install,libbpf.h,$(prefix)/include/bpf,644);
 		$(call do_install,btf.h,$(prefix)/include/bpf,644);
 
Quoted 16 lines

tools/lib/bpf/Makefile install_headers: · line 224

Quoted 8 lines
tools/lib/bpf/Makefile +6 -1
-install: install_lib
+install_pkgconfig: $(PC_FILE)
+	$(call QUIET_INSTALL, $(PC_FILE)) \
+		$(call
 do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
+
+
+install: install_lib install_pkgconfig

Should it be part of 'install'? Not everyone needs pc file. Also pc template below relies on headers to be installed along with the library
itself, but headers are not installed as part of 'install' target, they're in a separate 'install_headers' one, so 'install_pkgconfig' and
'install_headers' should probably be consistent in this regard.

I believe it should be part of the install rule - if somebody does not need the pc file, they can simply ignore it - it's just a small text file, it won't break anything. The vast majority of libraries that have a pc file just ship it, and users that don't want to use them, just don't.

The reason to always ship it is so that downstream projects can rely on it existing if libbpf is installed, without having to do awkward dances and fallbacks if packagers forget or fail to notice that there's an additional special install rule. In other words, a Meson project can rely on "dependency('libbpf')" and an autotools project can rely on "PKG_CHECK_MODULES([libbpf], [libbpf]" being accurate and authoritative. Which doesn't mean it's mandatory to use - just that there are strong guarantees for users and distributors, if they choose to use those macros and tools. It makes things simpler and nicer for them, and "just work" in the most common use case.

And it's not dependent on install_headers if we change the include flag
- -I/usr/include actually gets automatically pruned by pkg-config -- cflags. It is dependent on it if we keep -I/usr/include/bpf, but I'll change it as you suggested, more on that below.

Quoted 34 lines
Quoted 20 lines
tools/lib/bpf/Makefile +1 -1
 ### Cleaning rules
 
@@ -219,7 +231,7 @@ config-clean:
 
 clean:
 	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET)
 \
-		*.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS
+		*.o *~ *.a *.so .*.d .*.cmd *.pc LIBBPF-CFLAGS
 	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-

DUMP.libbpf

tools/lib/bpf/libbpf.pc.template new file +0 -0
new file mode 100644
index 000000000000..0cac2f5f54a6

tools/lib/bpf/libbpf.pc.template line 3

Quoted 3 lines
tools/lib/bpf/libbpf.pc.template new file +3 -0
@@ -0,0 +1,11 @@
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include/bpf

I guess it should be includedir=${prefix}/include.

Paths in '#include' are usually provided relative to ${prefix}/include,
e.g. all programs I have access to and that use libbpf do this:

  #include <bpf/bpf.h>
  #include <bpf/libbpf.h>
Quoted 5 lines

But includedir=${prefix}/include/bpf will force it to be:
#include <bpf.h>
#include <libbpf.h>

, what may create inconsistency with already written code.

Given /usr/include is always included by the compiler, one can actually use both ways, even in the same program, and it seems to work fine (just tested to be 100% sure) - so there's no breakage, but there is certainly inconsistency, especially as you say with existing code, as the pc file was not there from day 1. And I like consistency too :-)

Quoted 6 lines

I also checked .pc files on my devbox and this is what I see:

  % grep -h includedir /usr/share/pkgconfig/*.pc | sort | uniq -c
       28 Cflags: -I${includedir}
        1 Cflags: -I${includedir}/X11/dri
       29 includedir=/usr/include

pkg-config files from libraries are installed under /usr/lib/*/pkgconfig, rather than /usr/share, if you run the command there you'll see:

$ grep -h includedir /usr/lib/*/pkgconfig/*.pc | sort | uniq -c
      2 archincludedir=${includedir}/${arch}
     85 Cflags: -I${includedir}
      9 Cflags: -I${includedir} 
      1 Cflags: -I${includedir}  
      1 Cflags: -I${includedir}/atk-1.0
      1 Cflags: -I${includedir}/at-spi-2.0
      1 Cflags: -I${includedir}/at-spi2-atk/2.0
      1 Cflags: -I${includedir}/blkid
      1 Cflags: -I${includedir}/cairo
     13 Cflags: -I${includedir}/cairo 
      1 Cflags: -I${includedir}/dbus-1.0
      1 Cflags: -I${includedir}/dbus-1.0 -I${libdir}/dbus-1.0/include 
      3 Cflags: -I${includedir} -D_REENTRANT
      1 Cflags: -I${includedir}/et -I${includedir}
      1 Cflags: -I${includedir}/freetype2
      1 Cflags: -I${includedir}/fribidi
      1 Cflags: -I${includedir}/gck-1
      2 Cflags: -I${includedir}/gcr-3
      2 Cflags: -I${includedir}/gdk-pixbuf-2.0
      1 Cflags: -I${includedir}/gio-unix-2.0
      1 Cflags: -I${includedir}/glib-2.0 -I${libdir}/glib-2.0/include
      2 Cflags: -I${includedir}/gtk-2.0 
      2 Cflags: -I${includedir}/gtk-2.0 -I${libdir}/gtk-2.0/include 
      8 Cflags: -I${includedir}/gtk-3.0 
      1 Cflags: -I${includedir}/gtk-3.0/unix-print
      1 Cflags: -I${includedir}/gtk-unix-print-2.0
      3 Cflags: -I${includedir}/harfbuzz
      1 Cflags: -I${includedir} -I${includedir}/alsa
      4 Cflags: -I${includedir} -I${includedir}/libdrm
      1 Cflags: -I${includedir} -I${includedir}/libdrm -I${includedir}/libdrm/nouveau
      1 Cflags: -I${includedir}/libmount
      2 Cflags: -I${includedir}/libnl3
      1 Cflags: -I${includedir}/libnm
      1 Cflags: -I${includedir}/libsecret-1
      1 Cflags: -I${includedir}/libxml2 
      1 Cflags: -I${includedir}/p11-kit-1
      4 Cflags: -I${includedir}/pango-1.0
      1 Cflags: -I${includedir}/pgm-5.2
      1 Cflags: -I${includedir}/pixman-1
      3 Cflags: -I${includedir} -pthread
      3 Cflags: -I${includedir}/python2.7  -I${includedir}/x86_64-linux-gnu/python2.7
      3 Cflags: -I${includedir}/python3.7m -I${includedir}/x86_64-linux-gnu/python3.7m
      1 Cflags: -I${includedir}/SDL -D_GNU_SOURCE=1 -D_REENTRANT
      1 Cflags: -I${includedir}/uuid
      6 Cflags: -isystem ${includedir}
      1 Cflags: -isystem ${includedir}/bsd -DLIBBSD_OVERLAY
      1 # -I${includedir}/alsa below is just for backward compatibility
      3 includedir = ${prefix}/include
    171 includedir=${prefix}/include
      1 includedir=${prefix}/include/ayatana-messaging-menu
      2 includedir=${prefix}/include/libpng16
      6 includedir=${prefix}/include/mit-krb5
      1 includedir=${prefix}/include/PCSC
      4 includedir=${prefix}/include/x86_64-linux-gnu
     19 includedir=/usr/include
      2 rubyarchhdrdir=${archincludedir}/${RUBY_VERSION_NAME}
      2 rubyhdrdir=${includedir}/${RUBY_VERSION_NAME}
      2 sitearchhdrdir=${sitearchincludedir}/${RUBY_VERSION_NAME}/site_ruby
      2 sitearchincludedir=${includedir}/${sitearch}
      2 vendorarchhdrdir=${sitearchincludedir}/${RUBY_VERSION_NAME}/vendor_ruby
$ grep -h includedir /usr/lib/pkgconfig/*.pc | sort | uniq -c
      1 Cflags: -I${includedir}/libpurple
      1 Cflags: -I${includedir}/pidgin
      2 includedir=${prefix}/include

The vast majority does just include the canonical /usr/include path, but it's not uncommon to include the subdirectory as well. That's done so that users can include just the headers, which will work on any platform, and also when there are multiple source versions of the same library available on the system at the same time, and then a user picks the right pkg-config to parse at build time - the code doesn't have to change the include path. But it is by no mean a hard rule, and it's up to the individual developers.

Having said that, none of those apply at the moment and I don't see that they will in the future - certainly there's no multi-platform requirement, and hopefully there won't be backward-incompatible API breakages, hint hint :-)

And there is a good reason to just include /usr/include - so that pkg- config omits it by default, and there's no dependency on the headers actually being installed (if -I/usr/include/bpf is passed, then compiling with werror fails if the directory doesn't exist).

So I've changed the path in v4 to just -I${prefix}/include.

Thanks for the review!

Signature · 2 lines
Kind regards,
Luca Boccassi
#16 lore raw

PATCH v4 1/1 tools/bpf: generate pkg-config file for libbpf

In reply to Luca Boccassi

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline
v3: use LIBBPF_VERSION instead of kernel_version
v4: use -I${prefix}/include rather than -I${prefix}/include/bpf
in the Cflags field as requested, to keep consistency with how the headers are used now and to avoid a dependency from the pc file to the headers installation

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 18 +++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +15 -3
index 61aaacf0cfa1..891fe3da1410 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)
@@ -137,7 +138,7 @@ GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
 			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
 
-CMD_TARGETS = $(LIB_FILE)
+CMD_TARGETS = $(LIB_FILE) $(PC_FILE)
 
 CXX_TEST_TARGET = $(OUTPUT)test_libbpf
 
@@ -180,6 +181,12 @@ $(OUTPUT)libbpf.a: $(BPF_IN)
 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
 	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
 
+$(OUTPUT)libbpf.pc:
+	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
+		-e "s|@LIBDIR@|$(libdir_SQ)|" \
+		-e "s|@VERSION@|$(LIBBPF_VERSION)|" \
+		< [email protected] > $@
+
 check: check_abi
 
 check_abi: $(OUTPUT)libbpf.so
@@ -209,7 +216,12 @@ install_headers:
 		$(call do_install,libbpf.h,$(prefix)/include/bpf,644);
 		$(call do_install,btf.h,$(prefix)/include/bpf,644);
 
-install: install_lib
+install_pkgconfig: $(PC_FILE)
+	$(call QUIET_INSTALL, $(PC_FILE)) \
+		$(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
+
+
+install: install_lib install_pkgconfig
 
 ### Cleaning rules
 
@@ -219,7 +231,7 @@ config-clean:
 
 clean:
 	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
-		*.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS
+		*.o *~ *.a *.so .*.d .*.cmd *.pc LIBBPF-CFLAGS
 	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
 
 
tools/lib/bpf/libbpf.pc.template new file +11 -0
new file mode 100644
index 000000000000..0ecd334c109f
@@ -0,0 +1,11 @@
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include
+
+Name: libbpf
+URL: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
+Description: Linux kernel BPF library
+Version: @VERSION@
+Libs: -L${libdir} -lbpf
+Requires.private: libelf
+Cflags: -I${includedir}
Signature · 1 line
2.20.1
Acked-by on v4 1/1
#17 lore raw

[email protected] <[email protected]> [Thu, 2019-03-21 03:26 -0700]:

Quoted 6 lines

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

Quoted 9 lines

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline
v3: use LIBBPF_VERSION instead of kernel_version
v4: use -I${prefix}/include rather than -I${prefix}/include/bpf
in the Cflags field as requested, to keep consistency with how the headers are used now and to avoid a dependency from the pc file to the headers installation

Thanks for the change Luca!
The other part about install target is nit, so no strong preference.

Quoted 92 lines
 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 18 +++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 11 +++++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +15 -3
index 61aaacf0cfa1..891fe3da1410 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)
@@ -137,7 +138,7 @@ GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
 			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
 
-CMD_TARGETS = $(LIB_FILE)
+CMD_TARGETS = $(LIB_FILE) $(PC_FILE)
 
 CXX_TEST_TARGET = $(OUTPUT)test_libbpf
 
@@ -180,6 +181,12 @@ $(OUTPUT)libbpf.a: $(BPF_IN)
 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
 	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
 
+$(OUTPUT)libbpf.pc:
+	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
+		-e "s|@LIBDIR@|$(libdir_SQ)|" \
+		-e "s|@VERSION@|$(LIBBPF_VERSION)|" \
+		< [email protected] > $@
+
 check: check_abi
 
 check_abi: $(OUTPUT)libbpf.so
@@ -209,7 +216,12 @@ install_headers:
 		$(call do_install,libbpf.h,$(prefix)/include/bpf,644);
 		$(call do_install,btf.h,$(prefix)/include/bpf,644);
 
-install: install_lib
+install_pkgconfig: $(PC_FILE)
+	$(call QUIET_INSTALL, $(PC_FILE)) \
+		$(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
+
+
+install: install_lib install_pkgconfig
 
 ### Cleaning rules
 
@@ -219,7 +231,7 @@ config-clean:
 
 clean:
 	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
-		*.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS
+		*.o *~ *.a *.so .*.d .*.cmd *.pc LIBBPF-CFLAGS
 	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
 
 
tools/lib/bpf/libbpf.pc.template new file +11 -0
new file mode 100644
index 000000000000..0ecd334c109f
@@ -0,0 +1,11 @@
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include
+
+Name: libbpf
+URL: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
+Description: Linux kernel BPF library
+Version: @VERSION@
+Libs: -L${libdir} -lbpf
+Requires.private: libelf
+Cflags: -I${includedir}
Signature · 1 line
2.20.1
Signature · 1 line
Andrey Ignatov
#18 lore raw
Quoted 7 lines · Andrey Ignatov

[email protected] <[email protected]> [Thu, 2019-03-21 03:26 -0700]:

Quoted 6 lines

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

...

Quoted 5 lines
Quoted 5 lines
tools/lib/bpf/libbpf.pc.template new file +0 -0
new file mode 100644
index 000000000000..0ecd334c109f
Quoted 8 lines

tools/lib/bpf/libbpf.pc.template line 7

Quoted 8 lines
tools/lib/bpf/libbpf.pc.template new file +7 -0
@@ -0,0 +1,11 @@
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include
+
+Name: libbpf
+URL: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
+Description: Linux kernel BPF library

github/libbpf/libbpf is a true mirror of kernel's libbpf. I think if we start shipping libbpf.so from kernel and from github it will be very confusing to the users...
Which one is the true libbpf?
Also the package should mention the license.
And the license for libbpf is dual lgpl/bsd.
But if we point to the url above it will not make much sense. I think the packages URL should point to github/libbpf/libbpf and packaging scripts should be in github only.

Daniel,
what do you think?

maintainer
#19 lore raw
Quoted 33 lines · Alexei Starovoitov
Quoted 7 lines · Andrey Ignatov

[email protected] <[email protected]> [Thu, 2019-03-21 03:26 -0700]:

Quoted 6 lines

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

...

Quoted 5 lines
Quoted 5 lines
tools/lib/bpf/libbpf.pc.template new file +0 -0
new file mode 100644
index 000000000000..0ecd334c109f
Quoted 8 lines

tools/lib/bpf/libbpf.pc.template line 7

Quoted 8 lines
tools/lib/bpf/libbpf.pc.template new file +7 -0
@@ -0,0 +1,11 @@
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include
+
+Name: libbpf
+URL: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
+Description: Linux kernel BPF library

github/libbpf/libbpf is a true mirror of kernel's libbpf. I think if we start shipping libbpf.so from kernel and from github it will be very confusing to the users...
Which one is the true libbpf?
Also the package should mention the license.
And the license for libbpf is dual lgpl/bsd.
But if we point to the url above it will not make much sense. I think the packages URL should point to github/libbpf/libbpf and packaging scripts should be in github only.

Quoted 2 lines

Daniel,
what do you think?

Looking at [0], I don't see where license would be part of the keyword. Given this is just a pkg-config file where folks using it care mainly about the needed cflags/libs, it would make sense to me to ship it and have it under tools/lib/bpf/ in kernel tree (since this is distro independent). If the URL and Description causes confusion, I would probably just remove the URL field since it's not mandatory either. And description, I'd put something like 'official BPF library' or such, so it's generic enough.

[0] https://autotools.io/pkgconfig/file-format.html
https://dev.gentoo.org/~mgorny/pkg-config-spec.html#keywords

#20 lore raw
Quoted 54 lines · Daniel Borkmann
Quoted 36 lines · Alexei Starovoitov
Quoted 8 lines · Andrey Ignatov

[email protected] <[email protected]> [Thu, 2019-03- 21 03:26 -0700]:

Quoted 6 lines

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

...

Quoted 7 lines
Quoted 7 lines
tools/lib/bpf/libbpf.pc.template new file +0 -0
new file mode 100644
index 000000000000..0ecd334c109f
@@ -0,0 +1,11 @@
Quoted 8 lines
Quoted 8 lines
tools/lib/bpf/libbpf.pc.template new file +6 -0
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include
+
+Name: libbpf
+URL: 

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
+Description: Linux kernel BPF library

github/libbpf/libbpf is a true mirror of kernel's libbpf. I think if we start shipping libbpf.so from kernel and from github it will be very confusing to the users...
Which one is the true libbpf?
Also the package should mention the license.
And the license for libbpf is dual lgpl/bsd.
But if we point to the url above it will not make much sense. I think the packages URL should point to github/libbpf/libbpf and packaging scripts should be in github only.

Quoted 2 lines

Daniel,
what do you think?

Looking at [0], I don't see where license would be part of the keyword. Given
this is just a pkg-config file where folks using it care mainly about the
needed cflags/libs, it would make sense to me to ship it and have it under
tools/lib/bpf/ in kernel tree (since this is distro independent). If the URL
and Description causes confusion, I would probably just remove the URL field
since it's not mandatory either. And description, I'd put something like
'official BPF library' or such, so it's generic enough.

Quoted 2 lines

[0] https://autotools.io/pkgconfig/file-format.html
https://dev.gentoo.org/~mgorny/pkg-config-spec.html#keywords

I assumed the license referred to the file itself, so I added the SDPX comment. It's not too uncommon for pc files to have the license comment at the top.

Removed URL and changed Descriptin in v6, thanks for reviewing!

Signature · 2 lines
Kind regards,
Luca Boccassi
#21 lore raw
In reply to Alexei Starovoitov
Quoted 22 lines · Alexei Starovoitov
Quoted 8 lines · Andrey Ignatov

[email protected] <[email protected]> [Thu, 2019-03-21 03:26 -0700]:

Quoted 6 lines

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

...

Quoted 12 lines
Quoted 12 lines
tools/lib/bpf/libbpf.pc.template new file +5 -0
new file mode 100644
index 000000000000..0ecd334c109f
@@ -0,0 +1,11 @@
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include
+
+Name: libbpf
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
tools/lib/bpf/libbpf.pc.template new file +1 -0
+URL: 

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
+Description: Linux kernel BPF library

github/libbpf/libbpf is a true mirror of kernel's libbpf. I think if we start shipping libbpf.so from kernel and from github it will be very confusing to the users...
Which one is the true libbpf?

I'm afraid that ship already sailed :-) Users and distributions are already consuming libbpf from the kernel tree.

Quoted 2 lines

Also the package should mention the license.
And the license for libbpf is dual lgpl/bsd.

I followed the template from the other pkgconfig file (in traceevent) which does not have it, but I can add it in v5.

Quoted 3 lines

But if we point to the url above it will not make much sense. I think the packages URL should point to github/libbpf/libbpf and packaging scripts should be in github only.

If only one side ships the pc file, then you'll have half the users installing libbpf without a pc file, and the other half with it, so it would defeat the point of having one in the first place.

Quoted 3 lines

Daniel,
what do you think?

Signature · 2 lines
Kind regards,
Luca Boccassi
#22 lore raw

PATCH v5 1/1 tools/bpf: generate pkg-config file for libbpf

In reply to Luca Boccassi

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline
v3: use LIBBPF_VERSION instead of kernel_version
v4: use -I${prefix}/include rather than -I${prefix}/include/bpf
in the Cflags field as requested, to keep consistency with how the headers are used now and to avoid a dependency from the pc file to the headers installation
v5: add acked-by, add SPDX line to pc file

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 18 +++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 13 +++++++++++++
 3 files changed, 29 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +15 -3
index 61aaacf0cfa1..891fe3da1410 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)
@@ -137,7 +138,7 @@ GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
 			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
 
-CMD_TARGETS = $(LIB_FILE)
+CMD_TARGETS = $(LIB_FILE) $(PC_FILE)
 
 CXX_TEST_TARGET = $(OUTPUT)test_libbpf
 
@@ -180,6 +181,12 @@ $(OUTPUT)libbpf.a: $(BPF_IN)
 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
 	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
 
+$(OUTPUT)libbpf.pc:
+	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
+		-e "s|@LIBDIR@|$(libdir_SQ)|" \
+		-e "s|@VERSION@|$(LIBBPF_VERSION)|" \
+		< [email protected] > $@
+
 check: check_abi
 
 check_abi: $(OUTPUT)libbpf.so
@@ -209,7 +216,12 @@ install_headers:
 		$(call do_install,libbpf.h,$(prefix)/include/bpf,644);
 		$(call do_install,btf.h,$(prefix)/include/bpf,644);
 
-install: install_lib
+install_pkgconfig: $(PC_FILE)
+	$(call QUIET_INSTALL, $(PC_FILE)) \
+		$(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
+
+
+install: install_lib install_pkgconfig
 
 ### Cleaning rules
 
@@ -219,7 +231,7 @@ config-clean:
 
 clean:
 	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
-		*.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS
+		*.o *~ *.a *.so .*.d .*.cmd *.pc LIBBPF-CFLAGS
 	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
 
 
tools/lib/bpf/libbpf.pc.template new file +13 -0
new file mode 100644
index 000000000000..73e9f321f84f
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include
+
+Name: libbpf
+URL: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
+Description: Linux kernel BPF library
+Version: @VERSION@
+Libs: -L${libdir} -lbpf
+Requires.private: libelf
+Cflags: -I${includedir}
Signature · 1 line
2.20.1
#23 lore raw

PATCH v6 1/1 tools/bpf: generate pkg-config file for libbpf

In reply to Luca Boccassi

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline
v3: use LIBBPF_VERSION instead of kernel_version
v4: use -I${prefix}/include rather than -I${prefix}/include/bpf
in the Cflags field as requested, to keep consistency with how the headers are used now and to avoid a dependency from the pc file to the headers installation
v5: add acked-by, add SPDX line to pc file
v6: remove URL field, generalize Description field

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 18 +++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 12 ++++++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +15 -3
index 61aaacf0cfa1..891fe3da1410 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)
@@ -137,7 +138,7 @@ GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
 			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
 
-CMD_TARGETS = $(LIB_FILE)
+CMD_TARGETS = $(LIB_FILE) $(PC_FILE)
 
 CXX_TEST_TARGET = $(OUTPUT)test_libbpf
 
@@ -180,6 +181,12 @@ $(OUTPUT)libbpf.a: $(BPF_IN)
 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
 	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
 
+$(OUTPUT)libbpf.pc:
+	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
+		-e "s|@LIBDIR@|$(libdir_SQ)|" \
+		-e "s|@VERSION@|$(LIBBPF_VERSION)|" \
+		< [email protected] > $@
+
 check: check_abi
 
 check_abi: $(OUTPUT)libbpf.so
@@ -209,7 +216,12 @@ install_headers:
 		$(call do_install,libbpf.h,$(prefix)/include/bpf,644);
 		$(call do_install,btf.h,$(prefix)/include/bpf,644);
 
-install: install_lib
+install_pkgconfig: $(PC_FILE)
+	$(call QUIET_INSTALL, $(PC_FILE)) \
+		$(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
+
+
+install: install_lib install_pkgconfig
 
 ### Cleaning rules
 
@@ -219,7 +231,7 @@ config-clean:
 
 clean:
 	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
-		*.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS
+		*.o *~ *.a *.so .*.d .*.cmd *.pc LIBBPF-CFLAGS
 	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
 
 
tools/lib/bpf/libbpf.pc.template new file +12 -0
new file mode 100644
index 000000000000..ac17fcef2108
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include
+
+Name: libbpf
+Description: BPF library
+Version: @VERSION@
+Libs: -L${libdir} -lbpf
+Requires.private: libelf
+Cflags: -I${includedir}
Signature · 1 line
2.20.1
#24 lore raw
Quoted 7 lines · On Thu, Mar 21, 2019 at 10:33:17PM +0000, [email protected] wrote:

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

Quoted 11 lines

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline
v3: use LIBBPF_VERSION instead of kernel_version
v4: use -I${prefix}/include rather than -I${prefix}/include/bpf
in the Cflags field as requested, to keep consistency with how the headers are used now and to avoid a dependency from the pc file to the headers installation
v5: add acked-by, add SPDX line to pc file
v6: remove URL field, generalize Description field

It fails to build:
make -C ../../../lib/bpf OUTPUT=/data/users/ast/bpf-next/tools/testing/selftests/bpf/ make[1]: Entering directory /data/users/ast/bpf-next/tools/lib/bpf' make[2]: *** No rule to make target libbpf.pc', needed by `all_cmd'. Stop. make[1]: *** [all] Error 2
make[1]: Leaving directory `/data/users/ast/bpf-next/tools/lib/bpf'

#25 lore raw
Quoted 21 lines · Alexei Starovoitov
Quoted 7 lines · On Thu, Mar 21, 2019 at 10:33:17PM +0000, [email protected] wrote:

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

Quoted 11 lines

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline
v3: use LIBBPF_VERSION instead of kernel_version
v4: use -I${prefix}/include rather than -I${prefix}/include/bpf
in the Cflags field as requested, to keep consistency with how the headers are used now and to avoid a dependency from the pc file to the headers installation
v5: add acked-by, add SPDX line to pc file
v6: remove URL field, generalize Description field

Quoted 8 lines

It fails to build:
make -C ../../../lib/bpf OUTPUT=/data/users/ast/bpf- next/tools/testing/selftests/bpf/
make[1]: Entering directory /data/users/ast/bpf-next/tools/lib/bpf' make[2]: *** No rule to make target libbpf.pc', needed by `all_cmd'. Stop.
make[1]: *** [all] Error 2
make[1]: Leaving directory `/data/users/ast/bpf-next/tools/lib/bpf'

Thanks, the $(OUTPUT) prefix was missing so the out of tree build didn't work, fixed in v7.

Signature · 2 lines
Kind regards,
Luca Boccassi
#26 lore raw

PATCH v7 1/1 tools/bpf: generate pkg-config file for libbpf

In reply to Luca Boccassi

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline
v3: use LIBBPF_VERSION instead of kernel_version
v4: use -I${prefix}/include rather than -I${prefix}/include/bpf
in the Cflags field as requested, to keep consistency with how the headers are used now and to avoid a dependency from the pc file to the headers installation
v5: add acked-by, add SPDX line to pc file
v6: remove URL field, generalize Description field
v7: fix build with make OUTPUT=/foo/bar/ by prefixing $(OUTPUT)
to the pc file variable

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 19 ++++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 12 ++++++++++++
 3 files changed, 29 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +16 -3
index 61aaacf0cfa1..96e9571b0a2e 100644
@@ -80,6 +80,7 @@ libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
 LIB_FILE = libbpf.a libbpf.so
+PC_FILE = libbpf.pc
 
 VERSION		= $(BPF_VERSION)
 PATCHLEVEL	= $(BPF_PATCHLEVEL)
@@ -130,6 +131,7 @@ include $(srctree)/tools/build/Makefile.include
 
 BPF_IN    := $(OUTPUT)libbpf-in.o
 LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
+PC_FILE := $(addprefix $(OUTPUT),$(PC_FILE))
 VERSION_SCRIPT := libbpf.map
 
 GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
@@ -137,7 +139,7 @@ GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
 			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
 
-CMD_TARGETS = $(LIB_FILE)
+CMD_TARGETS = $(LIB_FILE) $(PC_FILE)
 
 CXX_TEST_TARGET = $(OUTPUT)test_libbpf
 
@@ -180,6 +182,12 @@ $(OUTPUT)libbpf.a: $(BPF_IN)
 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
 	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
 
+$(OUTPUT)libbpf.pc:
+	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
+		-e "s|@LIBDIR@|$(libdir_SQ)|" \
+		-e "s|@VERSION@|$(LIBBPF_VERSION)|" \
+		< libbpf.pc.template > $@
+
 check: check_abi
 
 check_abi: $(OUTPUT)libbpf.so
@@ -209,7 +217,12 @@ install_headers:
 		$(call do_install,libbpf.h,$(prefix)/include/bpf,644);
 		$(call do_install,btf.h,$(prefix)/include/bpf,644);
 
-install: install_lib
+install_pkgconfig: $(PC_FILE)
+	$(call QUIET_INSTALL, $(PC_FILE)) \
+		$(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
+
+
+install: install_lib install_pkgconfig
 
 ### Cleaning rules
 
@@ -219,7 +232,7 @@ config-clean:
 
 clean:
 	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
-		*.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS
+		*.o *~ *.a *.so .*.d .*.cmd *.pc LIBBPF-CFLAGS
 	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
 
 
tools/lib/bpf/libbpf.pc.template new file +12 -0
new file mode 100644
index 000000000000..ac17fcef2108
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include
+
+Name: libbpf
+Description: BPF library
+Version: @VERSION@
+Libs: -L${libdir} -lbpf
+Requires.private: libelf
+Cflags: -I${includedir}
Signature · 1 line
2.20.1
#27 lore raw
Quoted 8 lines · On Thu, Mar 21, 2019 at 4:09 PM <[email protected]> wrote:

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

Quoted 13 lines

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline
v3: use LIBBPF_VERSION instead of kernel_version
v4: use -I${prefix}/include rather than -I${prefix}/include/bpf
in the Cflags field as requested, to keep consistency with how the headers are used now and to avoid a dependency from the pc file to the headers installation
v5: add acked-by, add SPDX line to pc file
v6: remove URL field, generalize Description field
v7: fix build with make OUTPUT=/foo/bar/ by prefixing $(OUTPUT)
to the pc file variable

Applied. Thanks

#28 lore raw
Quoted 30 lines · Alexei Starovoitov

On Thu, Mar 21, 2019 at 4:09 PM <
[email protected]

Quoted 14 lines

wrote:
From: Luca Boccassi <
[email protected]

Quoted 1 line

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

Signed-off-by: Luca Boccassi <
[email protected]

Quoted 1 line

Acked-by: Andrey Ignatov <
[email protected]

Quoted 1 line
Quoted 13 lines

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline
v3: use LIBBPF_VERSION instead of kernel_version
v4: use -I${prefix}/include rather than -I${prefix}/include/bpf
in the Cflags field as requested, to keep consistency with how the headers are used now and to avoid a dependency from the pc file to the headers installation
v5: add acked-by, add SPDX line to pc file
v6: remove URL field, generalize Description field
v7: fix build with make OUTPUT=/foo/bar/ by prefixing $(OUTPUT)
to the pc file variable

Quoted 1 line

Applied. Thanks

Thank you! I'll send a patch to the Github mirror as soon as possible.

Signature · 2 lines
Kind regards,
Luca Boccassi
#29 lore raw

PATCH v8 1/1 tools/bpf: generate pkg-config file for libbpf

In reply to Luca Boccassi

From: Luca Boccassi <[email protected]>

Generate a libbpf.pc file at build time so that users can rely on pkg-config to find the library, its CFLAGS and LDFLAGS.

---
v2: use QUIET_GEN instead of QUIET_LINK to generate pc file,
save kernel version in its own variable instead of calling make inline
v3: use LIBBPF_VERSION instead of kernel_version
v4: use -I${prefix}/include rather than -I${prefix}/include/bpf
in the Cflags field as requested, to keep consistency with how the headers are used now and to avoid a dependency from the pc file to the headers installation
v5: add acked-by, add SPDX line to pc file
v6: remove URL field, generalize Description field
v7: fix build with make OUTPUT=/foo/bar/ by prefixing $(OUTPUT)
to the pc file variable
v8: rebased on latest bpf-next to solve merge conflict
(not that indentation looks wrong on 2 lines because the "+" added by the diff makes the first tab start at the 8th column rather than the 7th, it align once applied though)

 tools/lib/bpf/.gitignore         |  1 +
 tools/lib/bpf/Makefile           | 19 ++++++++++++++++---
 tools/lib/bpf/libbpf.pc.template | 12 ++++++++++++
 3 files changed, 29 insertions(+), 3 deletions(-)
 create mode 100644 tools/lib/bpf/libbpf.pc.template
tools/lib/bpf/.gitignore +1 -0
index 4db74758c674..7d9e182a1f51 100644
@@ -1,3 +1,4 @@
 libbpf_version.h
+libbpf.pc
 FEATURE-DUMP.libbpf
 test_libbpf
tools/lib/bpf/Makefile +16 -3
index 5bf8e52c41fc..0fe54fe4cc04 100644
@@ -90,6 +90,7 @@ LIBBPF_VERSION	= $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION)
 
 LIB_TARGET	= libbpf.a libbpf.so.$(LIBBPF_VERSION)
 LIB_FILE	= libbpf.a libbpf.so*
+PC_FILE		= libbpf.pc
 
 # Set compile option CFLAGS
 ifdef EXTRA_CFLAGS
@@ -134,13 +135,14 @@ VERSION_SCRIPT	:= libbpf.map
 
 LIB_TARGET	:= $(addprefix $(OUTPUT),$(LIB_TARGET))
 LIB_FILE	:= $(addprefix $(OUTPUT),$(LIB_FILE))
+PC_FILE		:= $(addprefix $(OUTPUT),$(PC_FILE))
 
 GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN) | \
 			   awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {s++} END{print s}')
 VERSIONED_SYM_COUNT = $(shell readelf -s --wide $(OUTPUT)libbpf.so | \
 			      grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
 
-CMD_TARGETS = $(LIB_TARGET)
+CMD_TARGETS = $(LIB_TARGET) $(PC_FILE)
 
 CXX_TEST_TARGET = $(OUTPUT)test_libbpf
 
@@ -187,6 +189,12 @@ $(OUTPUT)libbpf.a: $(BPF_IN)
 $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a
 	$(QUIET_LINK)$(CXX) $(INCLUDES) $^ -lelf -o $@
 
+$(OUTPUT)libbpf.pc:
+	$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
+		-e "s|@LIBDIR@|$(libdir_SQ)|" \
+		-e "s|@VERSION@|$(LIBBPF_VERSION)|" \
+		< libbpf.pc.template > $@
+
 check: check_abi
 
 check_abi: $(OUTPUT)libbpf.so
@@ -223,7 +231,12 @@ install_headers:
 		$(call do_install,libbpf.h,$(prefix)/include/bpf,644);
 		$(call do_install,btf.h,$(prefix)/include/bpf,644);
 
-install: install_lib
+install_pkgconfig: $(PC_FILE)
+	$(call QUIET_INSTALL, $(PC_FILE)) \
+		$(call do_install,$(PC_FILE),$(libdir_SQ)/pkgconfig,644)
+
+
+install: install_lib install_pkgconfig
 
 ### Cleaning rules
 
@@ -233,7 +246,7 @@ config-clean:
 
 clean:
 	$(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \
-		*.o *~ *.a *.so *.so.$(VERSION) .*.d .*.cmd LIBBPF-CFLAGS
+		*.o *~ *.a *.so *.so.$(VERSION) .*.d .*.cmd *.pc LIBBPF-CFLAGS
 	$(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf
 
 
tools/lib/bpf/libbpf.pc.template new file +12 -0
new file mode 100644
index 000000000000..ac17fcef2108
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+
+prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=${prefix}/include
+
+Name: libbpf
+Description: BPF library
+Version: @VERSION@
+Libs: -L${libdir} -lbpf
+Requires.private: libelf
+Cflags: -I${includedir}
Signature · 1 line
2.20.1