pahole: Describe expected use of 'default' in the man page
[dwarves.git] / CMakeLists.txt
bloba25018255ccdd37abd592d1b85a8a8069e16552d
1 cmake_minimum_required(VERSION 2.8.12)
2 project(pahole C)
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)
8         if(PKGCONFIG_FOUND)
9                 pkg_check_modules(LIBBPF REQUIRED libbpf>=0.4.0)
10         endif()
11 endif()
13 INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}
14                     ${CMAKE_CURRENT_SOURCE_DIR})
15 if(NOT LIBBPF_FOUND)
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)
19 else()
20         INCLUDE_DIRECTORIES(${LIBBPF_INCLUDE_DIRS})
21         LINK_DIRECTORIES(${LIBBPF_LIBRARY_DIRS})
22 endif()
24 # Try to parse this later, Helio just showed me a KDE4 example to support
25 # x86-64 builds.
26 # the following are directories where stuff will be installed to
27 set(__LIB "" CACHE STRING "Define suffix of directory name (32/64)" )
29 macro(_set_fancy _var _value _comment)
30         if (NOT DEFINED ${_var})
31                 set(${_var} ${_value})
32         else (NOT DEFINED ${_var})
33                 set(${_var} "${${_var}}" CACHE PATH "${_comment}")
34         endif (NOT DEFINED ${_var})
35 endmacro(_set_fancy)
37 # where to look first for cmake modules,
38 # before ${CMAKE_ROOT}/Modules/ is checked
39 set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
41 if (NOT CMAKE_BUILD_TYPE)
42         set (CMAKE_BUILD_TYPE Debug CACHE STRING
43              "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
44              FORCE)
45 endif (NOT CMAKE_BUILD_TYPE)
47 set(CMAKE_C_FLAGS_DEBUG "-Wall -Werror -ggdb -O0")
48 set(CMAKE_C_FLAGS_RELEASE "-Wall -O2")
49 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
51 if (NOT DEFINED BUILD_SHARED_LIBS)
52         set (BUILD_SHARED_LIBS ON)
53         message(STATUS "Setting BUILD_SHARED_LIBS = ${BUILD_SHARED_LIBS}")
54 endif (NOT DEFINED BUILD_SHARED_LIBS)
56 # Just for grepping, DWARVES_VERSION isn't used anywhere anymore
57 # add_definitions(-D_GNU_SOURCE -DDWARVES_VERSION="v1.26")
58 add_definitions(-D_GNU_SOURCE -DDWARVES_MAJOR_VERSION=1)
59 add_definitions(-D_GNU_SOURCE -DDWARVES_MINOR_VERSION=26)
60 find_package(DWARF REQUIRED)
61 find_package(ZLIB REQUIRED)
62 find_package(argp REQUIRED)
63 find_package(obstack REQUIRED)
64 find_package(Python3 QUIET)
66 # make sure git submodule(s) are checked out
67 find_package(Git QUIET)
68 if(LIBBPF_EMBEDDED AND GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
69         # Update submodules as needed
70         option(GIT_SUBMODULE "Check submodules during build" ON)
71         if(GIT_SUBMODULE)
72                 message(STATUS "Submodule update")
73                 execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
74                                 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
75                                 RESULT_VARIABLE GIT_SUBMOD_RESULT)
76                 if(NOT GIT_SUBMOD_RESULT EQUAL "0")
77                         message(FATAL_ERROR "git submodule update --init failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
78                 else()
79                         message(STATUS "Submodule update - done")
80                 endif()
81         endif()
82 endif()
83 if(NOT LIBBPF_FOUND AND NOT EXISTS "${PROJECT_SOURCE_DIR}/lib/bpf/src/btf.h")
84         message(FATAL_ERROR "The submodules were not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
85 endif()
87 _set_fancy(LIB_INSTALL_DIR "${EXEC_INSTALL_PREFIX}${CMAKE_INSTALL_PREFIX}/${__LIB}" "libdir")
89 # libbpf uses reallocarray, which is not available in all versions of glibc
90 # libbpf's include/tools/libc_compat.h provides implementation, but needs
91 # COMPACT_NEED_REALLOCARRAY to be set
92 INCLUDE(CheckCSourceCompiles)
93 CHECK_C_SOURCE_COMPILES(
95 #define _GNU_SOURCE
96 #include <stdlib.h>
97 int main(void)
99         return !!reallocarray(NULL, 1, 1);
101 " HAVE_REALLOCARRAY_SUPPORT)
102 if (NOT HAVE_REALLOCARRAY_SUPPORT)
103   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOMPAT_NEED_REALLOCARRAY")
104 endif()
106 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64")
108 if (NOT LIBBPF_FOUND)
109         file(GLOB libbpf_sources "lib/bpf/src/*.c")
110         add_library(bpf OBJECT ${libbpf_sources})
111         set_property(TARGET bpf PROPERTY POSITION_INDEPENDENT_CODE 1)
112         target_include_directories(bpf PRIVATE
113                                    ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include
114                                    ${CMAKE_CURRENT_SOURCE_DIR}/lib/bpf/include/uapi)
115 endif()
117 set(dwarves_LIB_SRCS dwarves.c dwarves_fprintf.c gobuffer.c
118                      ctf_loader.c libctf.c btf_encoder.c btf_loader.c
119                      dwarf_loader.c dutil.c elf_symtab.c rbtree.c)
120 if (NOT LIBBPF_FOUND)
121         list(APPEND dwarves_LIB_SRCS $<TARGET_OBJECTS:bpf>)
122 endif()
123 add_library(dwarves ${dwarves_LIB_SRCS})
124 set_target_properties(dwarves PROPERTIES VERSION 1.0.0 SOVERSION 1)
125 set_target_properties(dwarves PROPERTIES INTERFACE_LINK_LIBRARIES "")
126 target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY})
128 set(dwarves_emit_LIB_SRCS dwarves_emit.c)
129 add_library(dwarves_emit ${dwarves_emit_LIB_SRCS})
130 set_target_properties(dwarves_emit PROPERTIES VERSION 1.0.0 SOVERSION 1)
131 target_link_libraries(dwarves_emit dwarves)
133 set(dwarves_reorganize_LIB_SRCS dwarves_reorganize.c)
134 add_library(dwarves_reorganize ${dwarves_reorganize_LIB_SRCS})
135 set_target_properties(dwarves_reorganize PROPERTIES VERSION 1.0.0 SOVERSION 1)
136 target_link_libraries(dwarves_reorganize dwarves)
138 set(codiff_SRCS codiff.c)
139 add_executable(codiff ${codiff_SRCS})
140 target_link_libraries(codiff dwarves)
142 set(ctracer_SRCS ctracer.c)
143 add_executable(ctracer ${ctracer_SRCS})
144 target_link_libraries(ctracer dwarves dwarves_emit dwarves_reorganize ${ELF_LIBRARY})
146 set(dtagnames_SRCS dtagnames.c)
147 add_executable(dtagnames ${dtagnames_SRCS})
148 target_link_libraries(dtagnames dwarves)
150 set(pahole_SRCS pahole.c)
151 add_executable(pahole ${pahole_SRCS})
152 target_link_libraries(pahole dwarves dwarves_emit dwarves_reorganize)
154 set(pdwtags_SRCS pdwtags.c)
155 add_executable(pdwtags ${pdwtags_SRCS})
156 target_link_libraries(pdwtags dwarves)
158 set(pglobal_SRCS pglobal.c)
159 add_executable(pglobal ${pglobal_SRCS})
160 target_link_libraries(pglobal dwarves)
162 set(pfunct_SRCS pfunct.c)
163 add_executable(pfunct ${pfunct_SRCS})
164 target_link_libraries(pfunct dwarves dwarves_emit ${ELF_LIBRARY})
166 set(prefcnt_SRCS prefcnt.c)
167 add_executable(prefcnt ${prefcnt_SRCS})
168 target_link_libraries(prefcnt dwarves)
170 set(scncopy_SRCS scncopy.c elfcreator.c)
171 add_executable(scncopy ${scncopy_SRCS})
172 target_link_libraries(scncopy dwarves ${ELF_LIBRARY})
174 set(syscse_SRCS syscse.c)
175 add_executable(syscse ${syscse_SRCS})
176 target_link_libraries(syscse dwarves)
178 install(TARGETS codiff ctracer dtagnames pahole pdwtags
179                 pfunct pglobal prefcnt scncopy syscse RUNTIME DESTINATION
180                 ${CMAKE_INSTALL_PREFIX}/bin)
181 install(TARGETS dwarves LIBRARY DESTINATION ${LIB_INSTALL_DIR} ARCHIVE DESTINATION ${LIB_INSTALL_DIR})
182 install(TARGETS dwarves dwarves_emit dwarves_reorganize LIBRARY DESTINATION ${LIB_INSTALL_DIR} ARCHIVE DESTINATION ${LIB_INSTALL_DIR})
183 install(FILES dwarves.h dwarves_emit.h dwarves_reorganize.h
184               dutil.h gobuffer.h list.h rbtree.h
185               btf_encoder.h config.h ctf.h
186               elfcreator.h elf_symtab.h hash.h libctf.h
187         DESTINATION ${CMAKE_INSTALL_PREFIX}/include/dwarves/)
188 install(FILES man-pages/pahole.1 DESTINATION ${CMAKE_INSTALL_PREFIX}/share/man/man1/)
189 if(Python3_FOUND)
190         install(PROGRAMS ostra/ostra-cg DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
191         install(FILES ostra/python/ostra.py DESTINATION ${CMAKE_INSTALL_PREFIX}/share/dwarves/runtime/python)
192 endif()
193 install(PROGRAMS btfdiff fullcircle DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
194 install(FILES lib/Makefile lib/ctracer_relay.c lib/ctracer_relay.h lib/linux.blacklist.cu
195         DESTINATION ${CMAKE_INSTALL_PREFIX}/share/dwarves/runtime)