|
1 | | -cmake_minimum_required(VERSION 4.0) |
| 1 | +cmake_minimum_required(VERSION 3.16) # 4.0 does not exist |
2 | 2 | project(pylibbpf) |
3 | 3 |
|
| 4 | +# pybind11 |
4 | 5 | add_subdirectory(pybind11) |
5 | 6 | pybind11_add_module(pylibbpf src/main.cpp) |
6 | 7 |
|
7 | | -include(ExternalProject) |
8 | | -ExternalProject_Add( |
9 | | - libbpf_build |
10 | | - PREFIX libbpf |
11 | | - SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libbpf/src |
12 | | - CONFIGURE_COMMAND "" |
13 | | - BUILD_COMMAND |
14 | | - make BUILD_STATIC_ONLY=1 OBJDIR=${CMAKE_CURRENT_BINARY_DIR}/libbpf/libbpf |
15 | | - DESTDIR=${CMAKE_CURRENT_BINARY_DIR}/libbpf INCLUDEDIR= LIBDIR= UAPIDIR= |
16 | | - install install_uapi_headers |
17 | | - BUILD_IN_SOURCE TRUE |
18 | | - INSTALL_COMMAND "" |
19 | | - STEP_TARGETS build) |
20 | | - |
21 | | -# Define a static library target pointing to the built libbpf.a |
22 | | -add_library(libbpf_static STATIC IMPORTED GLOBAL) |
| 8 | +# --- libbpf build rules --- |
| 9 | +set(LIBBPF_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libbpf/src) |
| 10 | +set(LIBBPF_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/libbpf) |
| 11 | +set(LIBBPF_A ${LIBBPF_BUILD_DIR}/libbpf.a) |
| 12 | + |
| 13 | +add_custom_command( |
| 14 | + OUTPUT ${LIBBPF_A} |
| 15 | + COMMAND make BUILD_STATIC_ONLY=1 OBJDIR=${LIBBPF_BUILD_DIR} -C |
| 16 | + ${LIBBPF_SRC_DIR} |
| 17 | + WORKING_DIRECTORY ${LIBBPF_SRC_DIR} |
| 18 | + COMMENT "Building libbpf.a" |
| 19 | + VERBATIM) |
23 | 20 |
|
24 | | -set(LIBBPF_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/libbpf/include) |
| 21 | +add_custom_target(libbpf_build ALL DEPENDS ${LIBBPF_A}) |
25 | 22 |
|
| 23 | +# Define static lib as imported |
| 24 | +add_library(libbpf_static STATIC IMPORTED GLOBAL) |
26 | 25 | set_target_properties( |
27 | 26 | libbpf_static |
28 | | - PROPERTIES IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/libbpf/libbpf.a" |
29 | | - INTERFACE_INCLUDE_DIRECTORIES "${LIBBPF_INCLUDE_DIRS}") |
30 | | -# Ensure build order: external project builds before linking |
| 27 | + PROPERTIES IMPORTED_LOCATION ${LIBBPF_A} |
| 28 | + INTERFACE_INCLUDE_DIRECTORIES |
| 29 | + ${CMAKE_CURRENT_SOURCE_DIR}/libbpf/include) |
| 30 | + |
31 | 31 | add_dependencies(libbpf_static libbpf_build) |
32 | | -add_dependencies(pylibbpf libbpf_static) |
33 | 32 |
|
34 | | -# Version info |
| 33 | +# Link pybind11 module against libbpf |
| 34 | +target_link_libraries(pylibbpf PRIVATE libbpf_static) |
| 35 | + |
| 36 | +# Version info for Python extension |
35 | 37 | target_compile_definitions(pylibbpf |
36 | 38 | PRIVATE VERSION_INFO=${PYLIBBPF_VERSION_INFO}) |
37 | | - |
38 | | -# Link the actual library target |
39 | | -target_link_libraries(pylibbpf PRIVATE libbpf_static) |
0 commit comments