1 cmake_minimum_required(VERSION 3.5)
3 cmake_policy(SET CMP0005 NEW)
5 option(LIBBPF_EMBEDDED "Use the embedded version of libbpf instead of searching it via pkg-config" ON)
6 if (NOT LIBBPF_EMBEDDED)
7 find_package(PkgConfig REQUIRED)
9 pkg_check_modules(LIBBPF REQUIRED libbpf>=0.4.0)
13 INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}
14 ${CMAKE_CURRENT_SOURCE_DIR})
16 # Allows to use 'system' style #include with both embedded and system libbpf
17 INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/lib/include)
18 INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include/uapi)
20 INCLUDE_DIRECTORIES(${LIBBPF_INCLUDE_DIRS})
21 LINK_DIRECTORIES(${LIBBPF_LIBRARY_DIRS})
24 # Use the standard library installation directory
25 include(GNUInstallDirs)
26 set(CMAKE_INSTALL_LIBDIR "lib" CACHE STRING "libdir name")
28 # where to look first for cmake modules,
29 # before ${CMAKE_ROOT}/Modules/ is checked
30 set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
32 if (NOT CMAKE_BUILD_TYPE)
33 set (CMAKE_BUILD_TYPE Debug CACHE STRING
34 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
36 endif (NOT CMAKE_BUILD_TYPE)
38 set(CMAKE_C_FLAGS_DEBUG "-Wall -Werror -ggdb -O0")
39 set(CMAKE_C_FLAGS_RELEASE "-Wall -O2")
40 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
42 if (NOT DEFINED BUILD_SHARED_LIBS)
43 set (BUILD_SHARED_LIBS ON)
44 message(STATUS "Setting BUILD_SHARED_LIBS = ${BUILD_SHARED_LIBS}")
45 endif (NOT DEFINED BUILD_SHARED_LIBS)
47 # Just for grepping, DWARVES_VERSION isn't used anywhere anymore
48 # add_definitions(-D_GNU_SOURCE -DDWARVES_VERSION="v1.27")
49 add_definitions(-D_GNU_SOURCE -DDWARVES_MAJOR_VERSION=1)
50 add_definitions(-D_GNU_SOURCE -DDWARVES_MINOR_VERSION=27)
51 find_package(DWARF REQUIRED)
52 find_package(ZLIB REQUIRED)
53 find_package(argp REQUIRED)
54 find_package(obstack REQUIRED)
55 find_package(Python3 QUIET)
57 # make sure git submodule(s) are checked out
58 find_package(Git QUIET)
59 if(LIBBPF_EMBEDDED AND GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
60 # Update submodules as needed
61 option(GIT_SUBMODULE "Check submodules during build" ON)
63 message(STATUS "Submodule update")
64 execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
65 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
66 RESULT_VARIABLE GIT_SUBMOD_RESULT)
67 if(NOT GIT_SUBMOD_RESULT EQUAL "0")
68 message(FATAL_ERROR "git submodule update --init failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
70 message(STATUS "Submodule update - done")
74 if(NOT LIBBPF_FOUND AND NOT EXISTS "${PROJECT_SOURCE_DIR}/lib/bpf/src/btf.h")
75 message(FATAL_ERROR "The submodules were not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
78 if (NOT DEFINED LIB_INSTALL_DIR)
79 set(LIB_INSTALL_DIR "${EXEC_INSTALL_PREFIX}${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
82 # libbpf uses reallocarray, which is not available in all versions of glibc
83 # libbpf's include/tools/libc_compat.h provides implementation, but needs
84 # COMPACT_NEED_REALLOCARRAY to be set
85 INCLUDE(CheckCSourceCompiles)
86 CHECK_C_SOURCE_COMPILES(
92 return !!reallocarray(NULL, 1, 1);
94 " HAVE_REALLOCARRAY_SUPPORT)
95 if (NOT HAVE_REALLOCARRAY_SUPPORT)
96 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOMPAT_NEED_REALLOCARRAY")
99 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64")
101 if (NOT LIBBPF_FOUND)
102 file(GLOB libbpf_sources "lib/bpf/src/*.c")
103 add_library(bpf OBJECT ${libbpf_sources})
104 set_property(TARGET bpf PROPERTY POSITION_INDEPENDENT_CODE 1)
105 target_include_directories(bpf PRIVATE
106 ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include
107 ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include/uapi)
110 set(dwarves_LIB_SRCS dwarves.c dwarves_fprintf.c gobuffer.c
111 ctf_loader.c libctf.c btf_encoder.c btf_loader.c
112 dwarf_loader.c dutil.c elf_symtab.c rbtree.c)
113 if (NOT LIBBPF_FOUND)
114 list(APPEND dwarves_LIB_SRCS $<TARGET_OBJECTS:bpf>)
116 add_library(dwarves ${dwarves_LIB_SRCS})
117 set_target_properties(dwarves PROPERTIES VERSION 1.0.0 SOVERSION 1)
118 set_target_properties(dwarves PROPERTIES INTERFACE_LINK_LIBRARIES "")
119 target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY})
121 set(dwarves_emit_LIB_SRCS dwarves_emit.c)
122 add_library(dwarves_emit ${dwarves_emit_LIB_SRCS})
123 set_target_properties(dwarves_emit PROPERTIES VERSION 1.0.0 SOVERSION 1)
124 target_link_libraries(dwarves_emit dwarves)
126 set(dwarves_reorganize_LIB_SRCS dwarves_reorganize.c)
127 add_library(dwarves_reorganize ${dwarves_reorganize_LIB_SRCS})
128 set_target_properties(dwarves_reorganize PROPERTIES VERSION 1.0.0 SOVERSION 1)
129 target_link_libraries(dwarves_reorganize dwarves)
131 set(codiff_SRCS codiff.c)
132 add_executable(codiff ${codiff_SRCS})
133 target_link_libraries(codiff dwarves)
135 set(ctracer_SRCS ctracer.c)
136 add_executable(ctracer ${ctracer_SRCS})
137 target_link_libraries(ctracer dwarves dwarves_emit dwarves_reorganize ${ELF_LIBRARY})
139 set(dtagnames_SRCS dtagnames.c)
140 add_executable(dtagnames ${dtagnames_SRCS})
141 target_link_libraries(dtagnames dwarves)
143 set(pahole_SRCS pahole.c)
144 add_executable(pahole ${pahole_SRCS})
145 target_link_libraries(pahole dwarves dwarves_emit dwarves_reorganize)
147 set(pdwtags_SRCS pdwtags.c)
148 add_executable(pdwtags ${pdwtags_SRCS})
149 target_link_libraries(pdwtags dwarves)
151 set(pglobal_SRCS pglobal.c)
152 add_executable(pglobal ${pglobal_SRCS})
153 target_link_libraries(pglobal dwarves)
155 set(pfunct_SRCS pfunct.c)
156 add_executable(pfunct ${pfunct_SRCS})
157 target_link_libraries(pfunct dwarves dwarves_emit ${ELF_LIBRARY})
159 set(prefcnt_SRCS prefcnt.c)
160 add_executable(prefcnt ${prefcnt_SRCS})
161 target_link_libraries(prefcnt dwarves)
163 set(scncopy_SRCS scncopy.c elfcreator.c)
164 add_executable(scncopy ${scncopy_SRCS})
165 target_link_libraries(scncopy dwarves ${ELF_LIBRARY})
167 set(syscse_SRCS syscse.c)
168 add_executable(syscse ${syscse_SRCS})
169 target_link_libraries(syscse dwarves)
171 install(TARGETS codiff ctracer dtagnames pahole pdwtags
172 pfunct pglobal prefcnt scncopy syscse RUNTIME DESTINATION
173 ${CMAKE_INSTALL_PREFIX}/bin)
174 install(TARGETS dwarves LIBRARY DESTINATION ${LIB_INSTALL_DIR} ARCHIVE DESTINATION ${LIB_INSTALL_DIR})
175 install(TARGETS dwarves dwarves_emit dwarves_reorganize LIBRARY DESTINATION ${LIB_INSTALL_DIR} ARCHIVE DESTINATION ${LIB_INSTALL_DIR})
176 install(FILES dwarves.h dwarves_emit.h dwarves_reorganize.h
177 dutil.h gobuffer.h list.h rbtree.h
178 btf_encoder.h config.h ctf.h
179 elfcreator.h elf_symtab.h hash.h libctf.h
180 DESTINATION ${CMAKE_INSTALL_PREFIX}/include/dwarves/)
181 install(FILES man-pages/pahole.1 DESTINATION ${CMAKE_INSTALL_PREFIX}/share/man/man1/)
183 install(PROGRAMS ostra/ostra-cg DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
184 install(FILES ostra/python/ostra.py DESTINATION ${CMAKE_INSTALL_PREFIX}/share/dwarves/runtime/python)
186 install(PROGRAMS btfdiff fullcircle DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
187 install(FILES lib/Makefile lib/ctracer_relay.c lib/ctracer_relay.h lib/linux.blacklist.cu
188 DESTINATION ${CMAKE_INSTALL_PREFIX}/share/dwarves/runtime)