Skip to contentSkip to conversation

libbpf: build as PIC and statically link into libdwarves

PATCH Applied Andrii Nakryiko 3 participants 4 messages Feb 19, 2019 BPF

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: build as PIC and statically link into libdwarves

As libbpf is not yet widely available, it's safer to statically link it into libdwarves for now. Easiest way to define that in cmake is through OBJECT library with PIC.

---
 CMakeLists.txt | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
CMakeLists.txt +6 -11
index d6929b4..e0fdaf7 100644
@@ -1,5 +1,5 @@
 project(pahole C)
-cmake_minimum_required(VERSION 2.4.8)
+cmake_minimum_required(VERSION 2.8.8)
 cmake_policy(SET CMP0005 NEW)
 
 INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} )
@@ -72,24 +72,19 @@ if (NOT HAVE_REALLOCARRAY_SUPPORT)
 endif()
 
 file(GLOB libbpf_sources "lib/bpf/src/*.c")
-add_library(bpf-static STATIC ${libbpf_sources})
-set_target_properties(bpf-static PROPERTIES OUTPUT_NAME bpf)
-target_include_directories(bpf-static PRIVATE
-			   ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include
-			   ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include/uapi)
-add_library(bpf-shared SHARED ${libbpf_sources})
-set_target_properties(bpf-shared PROPERTIES OUTPUT_NAME bpf)
-target_include_directories(bpf-shared PRIVATE
+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)
 
 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})
+add_library(dwarves SHARED ${dwarves_LIB_SRCS} $<TARGET_OBJECTS:bpf>)
 set_target_properties(dwarves PROPERTIES VERSION 1.0.0 SOVERSION 1)
 set_target_properties(dwarves PROPERTIES INTERFACE_LINK_LIBRARIES "")
-target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} bpf-shared)
+target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES})
 
 set(dwarves_emit_LIB_SRCS dwarves_emit.c)
 add_library(dwarves_emit SHARED ${dwarves_emit_LIB_SRCS})
Signature · 1 line
2.17.1
#1 lore raw

cc Yonghong (mistyped his email)

-- Andrii

Quoted 54 lines · Andrii Nakryiko

As libbpf is not yet widely available, it's safer to statically link it into libdwarves for now. Easiest way to define that in cmake is through OBJECT library with PIC.

---
 CMakeLists.txt | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
CMakeLists.txt +6 -12
index d6929b4..e0fdaf7 100644
@@ -1,5 +1,5 @@
 project(pahole C)
-cmake_minimum_required(VERSION 2.4.8)
+cmake_minimum_required(VERSION 2.8.8)
 cmake_policy(SET CMP0005 NEW)
 
 INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} )
@@ -72,24 +72,19 @@ if (NOT HAVE_REALLOCARRAY_SUPPORT)
 endif()
 
 file(GLOB libbpf_sources "lib/bpf/src/*.c")
-add_library(bpf-static STATIC ${libbpf_sources})
-set_target_properties(bpf-static PROPERTIES OUTPUT_NAME bpf)
-target_include_directories(bpf-static PRIVATE
-                          ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include
-                          ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include/uapi)
-add_library(bpf-shared SHARED ${libbpf_sources})
-set_target_properties(bpf-shared PROPERTIES OUTPUT_NAME bpf)
-target_include_directories(bpf-shared PRIVATE
+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)
 
 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})
+add_library(dwarves SHARED ${dwarves_LIB_SRCS} $<TARGET_OBJECTS:bpf>)
 set_target_properties(dwarves PROPERTIES VERSION 1.0.0 SOVERSION 1)
 set_target_properties(dwarves PROPERTIES INTERFACE_LINK_LIBRARIES "")
-target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} bpf-shared)
+target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES})
 
 set(dwarves_emit_LIB_SRCS dwarves_emit.c)
 add_library(dwarves_emit SHARED ${dwarves_emit_LIB_SRCS})
--

2.17.1

#2 lore raw

Em Mon, Feb 18, 2019 at 07:04:17PM -0800, Andrii Nakryiko escreveu:

Quoted 1 line

cc Yonghong (mistyped his email)

Applied, but before I split it into two patches, one bumping the minimum required cmake version, with an explanation about why that is needed, then the rest of the patch, with the explanation you provided.

Please try to make the patches in a granular fashion, with justification for each independent part,

Thanks,

- Arnaldo

Quoted 55 lines · Andrii Nakryiko
Quoted 54 lines

As libbpf is not yet widely available, it's safer to statically link it into libdwarves for now. Easiest way to define that in cmake is through OBJECT library with PIC.

---
 CMakeLists.txt | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
CMakeLists.txt +6 -12
index d6929b4..e0fdaf7 100644
@@ -1,5 +1,5 @@
 project(pahole C)
-cmake_minimum_required(VERSION 2.4.8)
+cmake_minimum_required(VERSION 2.8.8)
 cmake_policy(SET CMP0005 NEW)
 
 INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} )
@@ -72,24 +72,19 @@ if (NOT HAVE_REALLOCARRAY_SUPPORT)
 endif()
 
 file(GLOB libbpf_sources "lib/bpf/src/*.c")
-add_library(bpf-static STATIC ${libbpf_sources})
-set_target_properties(bpf-static PROPERTIES OUTPUT_NAME bpf)
-target_include_directories(bpf-static PRIVATE
-                          ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include
-                          ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include/uapi)
-add_library(bpf-shared SHARED ${libbpf_sources})
-set_target_properties(bpf-shared PROPERTIES OUTPUT_NAME bpf)
-target_include_directories(bpf-shared PRIVATE
+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)
 
 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})
+add_library(dwarves SHARED ${dwarves_LIB_SRCS} $<TARGET_OBJECTS:bpf>)
 set_target_properties(dwarves PROPERTIES VERSION 1.0.0 SOVERSION 1)
 set_target_properties(dwarves PROPERTIES INTERFACE_LINK_LIBRARIES "")
-target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} bpf-shared)
+target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES})
 
 set(dwarves_emit_LIB_SRCS dwarves_emit.c)
 add_library(dwarves_emit SHARED ${dwarves_emit_LIB_SRCS})
--

2.17.1

Signature · 1 line
- Arnaldo
#3 lore raw
Quoted 10 lines · Arnaldo Carvalho de Melo

Em Mon, Feb 18, 2019 at 07:04:17PM -0800, Andrii Nakryiko escreveu:

Quoted 1 line

cc Yonghong (mistyped his email)

Applied, but before I split it into two patches, one bumping the minimum required cmake version, with an explanation about why that is needed, then the rest of the patch, with the explanation you provided.

Please try to make the patches in a granular fashion, with justification for each independent part,

Yep, sorry about that, will do next time.

Quoted 64 lines

Thanks,

- Arnaldo

Quoted 50 lines · Andrii Nakryiko
Quoted 49 lines

As libbpf is not yet widely available, it's safer to statically link it into libdwarves for now. Easiest way to define that in cmake is through OBJECT library with PIC.

---
 CMakeLists.txt | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
CMakeLists.txt +6 -11
index d6929b4..e0fdaf7 100644
@@ -1,5 +1,5 @@
 project(pahole C)
-cmake_minimum_required(VERSION 2.4.8)
+cmake_minimum_required(VERSION 2.8.8)
 cmake_policy(SET CMP0005 NEW)
 
 INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} )
@@ -72,24 +72,19 @@ if (NOT HAVE_REALLOCARRAY_SUPPORT)
 endif()
 
 file(GLOB libbpf_sources "lib/bpf/src/*.c")
-add_library(bpf-static STATIC ${libbpf_sources})
-set_target_properties(bpf-static PROPERTIES OUTPUT_NAME bpf)
-target_include_directories(bpf-static PRIVATE
-                          ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include
-                          ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include/uapi)
-add_library(bpf-shared SHARED ${libbpf_sources})
-set_target_properties(bpf-shared PROPERTIES OUTPUT_NAME bpf)
-target_include_directories(bpf-shared PRIVATE
+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)
 
 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})
+add_library(dwarves SHARED ${dwarves_LIB_SRCS} $<TARGET_OBJECTS:bpf>)
 set_target_properties(dwarves PROPERTIES VERSION 1.0.0 SOVERSION 1)
 set_target_properties(dwarves PROPERTIES INTERFACE_LINK_LIBRARIES "")
-target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} bpf-shared)
+target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES})
 
Quoted 5 lines
Quoted 5 lines
CMakeLists.txt +0 -1
 set(dwarves_emit_LIB_SRCS dwarves_emit.c)
 add_library(dwarves_emit SHARED ${dwarves_emit_LIB_SRCS})
--

2.17.1

--

- Arnaldo