xref: /aosp_15_r20/external/bcc/tests/cc/CMakeLists.txt (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1# Copyright (c) PLUMgrid, Inc.
2# Licensed under the Apache License, Version 2.0 (the "License")
3
4include_directories(${PROJECT_SOURCE_DIR}/src/cc)
5include_directories(${PROJECT_SOURCE_DIR}/src/cc/api)
6if (CMAKE_USE_LIBBPF_PACKAGE AND LIBBPF_FOUND)
7include_directories(${PROJECT_SOURCE_DIR}/src/cc/compat)
8else()
9include_directories(${PROJECT_SOURCE_DIR}/src/cc/libbpf/include/uapi)
10endif()
11include_directories(${PROJECT_SOURCE_DIR}/tests/python/include)
12include_directories(${LLVM_INCLUDE_DIRS})
13
14add_executable(test_static test_static.c)
15if(NOT CMAKE_USE_LIBBPF_PACKAGE)
16  target_link_libraries(test_static bcc-static)
17else()
18  target_link_libraries(test_static bcc-shared)
19endif()
20
21add_test(NAME c_test_static COMMAND ${TEST_WRAPPER} c_test_static sudo ${CMAKE_CURRENT_BINARY_DIR}/test_static)
22add_compile_options(-DCMAKE_CURRENT_BINARY_DIR="${CMAKE_CURRENT_BINARY_DIR}")
23
24set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-result -fPIC")
25set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-result -fPIC")
26
27if(${LLVM_PACKAGE_VERSION} VERSION_EQUAL 16 OR ${LLVM_PACKAGE_VERSION} VERSION_GREATER 16)
28set(CMAKE_CXX_STANDARD 14)
29endif()
30
31if(ENABLE_USDT)
32set(TEST_LIBBCC_SOURCES
33	test_libbcc.cc
34	test_c_api.cc
35	test_array_table.cc
36	test_bpf_table.cc
37	test_cg_storage.cc
38	test_hash_table.cc
39	test_map_in_map.cc
40	test_perf_event.cc
41	test_pinned_table.cc
42	test_prog_table.cc
43	test_queuestack_table.cc
44	test_shared_table.cc
45	test_sk_storage.cc
46	test_sock_table.cc
47	test_usdt_args.cc
48	test_usdt_probes.cc
49	utils.cc
50	test_parse_tracepoint.cc
51	test_zip.cc)
52
53file(COPY dummy_proc_map.txt DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
54add_library(usdt_test_lib SHARED usdt_test_lib.cc)
55add_library(debuginfo_test_lib SHARED debuginfo_test_lib.cc)
56
57add_custom_command(OUTPUT debuginfo.so
58                   COMMAND ${CMAKE_OBJCOPY}
59                   ARGS --add-symbol debuginfo_only_symbol=.text:0
60                   ${CMAKE_CURRENT_BINARY_DIR}/libdebuginfo_test_lib.so
61                   ${CMAKE_CURRENT_BINARY_DIR}/debuginfo.so
62                   DEPENDS debuginfo_test_lib)
63add_custom_target(debuginfo DEPENDS debuginfo.so)
64
65add_custom_command(OUTPUT with_gnu_debuglink.so
66                   COMMAND ${CMAKE_OBJCOPY}
67                   ARGS --add-gnu-debuglink=debuginfo.so
68                   ${CMAKE_CURRENT_BINARY_DIR}/libdebuginfo_test_lib.so
69                   ${CMAKE_CURRENT_BINARY_DIR}/with_gnu_debuglink.so
70                   DEPENDS debuginfo debuginfo_test_lib)
71add_custom_target(with_gnu_debuglink DEPENDS with_gnu_debuglink.so)
72SET(DEBUGINFO_TARGETS debuginfo_test_lib debuginfo with_gnu_debuglink)
73
74if(LIBLZMA_FOUND)
75  add_custom_command(OUTPUT debuginfo.so.xz
76                     COMMAND xz
77                     ARGS -k ${CMAKE_CURRENT_BINARY_DIR}/debuginfo.so
78                     DEPENDS debuginfo)
79  add_custom_target(debuginfo_xz DEPENDS debuginfo.so.xz)
80
81  add_custom_command(OUTPUT with_gnu_debugdata.so
82                     COMMAND ${CMAKE_OBJCOPY}
83                     ARGS --add-section .gnu_debugdata=${CMAKE_CURRENT_BINARY_DIR}/debuginfo.so.xz
84                     ${CMAKE_CURRENT_BINARY_DIR}/libdebuginfo_test_lib.so
85                     ${CMAKE_CURRENT_BINARY_DIR}/with_gnu_debugdata.so
86                     DEPENDS debuginfo_xz debuginfo_test_lib)
87  add_custom_target(with_gnu_debugdata DEPENDS with_gnu_debugdata.so)
88  list(APPEND DEBUGINFO_TARGETS with_gnu_debugdata)
89endif(LIBLZMA_FOUND)
90
91add_custom_command(OUTPUT archive.zip
92                   COMMAND mkdir -p zip_subdir
93                   COMMAND echo "This is a text file" > zip_subdir/file.txt
94                   COMMAND zip -0 archive.zip libdebuginfo_test_lib.so zip_subdir/file.txt
95                   DEPENDS debuginfo_test_lib
96                   BYPRODUCTS zip_subdir/file.txt)
97add_custom_target(zip_archive DEPENDS archive.zip)
98
99if(NOT CMAKE_USE_LIBBPF_PACKAGE)
100  add_executable(test_libbcc ${TEST_LIBBCC_SOURCES})
101  add_dependencies(test_libbcc bcc-shared zip_archive ${DEBUGINFO_TARGETS})
102
103  target_link_libraries(test_libbcc ${PROJECT_BINARY_DIR}/src/cc/libbcc.so dl usdt_test_lib)
104  set_target_properties(test_libbcc PROPERTIES INSTALL_RPATH ${PROJECT_BINARY_DIR}/src/cc)
105  target_compile_definitions(test_libbcc PRIVATE -DLIBBCC_NAME=\"libbcc.so\")
106
107  add_test(NAME test_libbcc COMMAND ${TEST_WRAPPER} c_test_all sudo ${CMAKE_CURRENT_BINARY_DIR}/test_libbcc)
108endif()
109
110if(LIBBPF_FOUND)
111  add_executable(test_libbcc_no_libbpf ${TEST_LIBBCC_SOURCES})
112  add_dependencies(test_libbcc_no_libbpf bcc-shared zip_archive ${DEBUGINFO_TARGETS})
113
114  target_link_libraries(test_libbcc_no_libbpf ${PROJECT_BINARY_DIR}/src/cc/libbcc.so dl usdt_test_lib ${LIBBPF_LIBRARIES})
115  set_target_properties(test_libbcc_no_libbpf PROPERTIES INSTALL_RPATH ${PROJECT_BINARY_DIR}/src/cc)
116  target_compile_definitions(test_libbcc_no_libbpf PRIVATE -DLIBBCC_NAME=\"libbcc.so\")
117
118  add_test(NAME test_libbcc_no_libbpf COMMAND ${TEST_WRAPPER} c_test_all_no_libbpf sudo ${CMAKE_CURRENT_BINARY_DIR}/test_libbcc_no_libbpf)
119endif()
120
121endif(ENABLE_USDT)
122