• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2021 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14project(arkbase)
15
16include(CheckFunctionExists)
17include(cmake/mm_coverage.cmake)
18
19set(SOURCES
20    ${PANDA_ROOT}/libpandabase/mem/alloc_tracker.cpp
21    ${PANDA_ROOT}/libpandabase/mem/arena.cpp
22    ${PANDA_ROOT}/libpandabase/mem/arena_allocator.cpp
23    ${PANDA_ROOT}/libpandabase/mem/code_allocator.cpp
24    ${PANDA_ROOT}/libpandabase/mem/pool_manager.cpp
25    ${PANDA_ROOT}/libpandabase/mem/pool_map.cpp
26    ${PANDA_ROOT}/libpandabase/mem/mem_config.cpp
27    ${PANDA_ROOT}/libpandabase/mem/base_mem_stats.cpp
28    ${PANDA_ROOT}/libpandabase/utils/debug.cpp
29    ${PANDA_ROOT}/libpandabase/utils/logger.cpp
30    ${PANDA_ROOT}/libpandabase/utils/dfx.cpp
31    ${PANDA_ROOT}/libpandabase/utils/utf.cpp
32    ${PANDA_ROOT}/libpandabase/utils/time.cpp
33    ${PANDA_ROOT}/libpandabase/utils/terminate.cpp
34    ${PANDA_ROOT}/libpandabase/utils/type_converter.cpp
35    ${PANDA_ROOT}/libpandabase/utils/json_parser.cpp
36    ${PANDA_ROOT}/libpandabase/trace/trace.cpp
37    ${PANDA_ROOT}/libpandabase/os/filesystem.cpp
38    ${PANDA_ROOT}/libpandabase/os/native_stack.cpp
39    ${PANDA_ROOT}/libpandabase/os/property.cpp
40    ${PANDA_ROOT}/libpandabase/os/dfx_option.cpp
41    ${PANDA_ROOT}/libpandabase/os/time.cpp
42)
43
44set(UNIX_SOURCES_COMMON
45    ${PANDA_ROOT}/libpandabase/os/unix/error.cpp
46    ${PANDA_ROOT}/libpandabase/os/unix/thread.cpp
47    ${PANDA_ROOT}/libpandabase/os/unix/native_stack.cpp
48    ${PANDA_ROOT}/libpandabase/os/unix/property.cpp
49    ${PANDA_ROOT}/libpandabase/os/unix/time_unix.cpp
50    ${PANDA_ROOT}/libpandabase/os/unix/file.cpp
51    ${PANDA_ROOT}/libpandabase/os/unix/filesystem.cpp
52    ${PANDA_ROOT}/libpandabase/os/unix/library_loader.cpp
53    ${PANDA_ROOT}/libpandabase/os/unix/mem.cpp
54    ${PANDA_ROOT}/libpandabase/trace/unix/trace.cpp
55    ${PANDA_ROOT}/libpandabase/os/unix/sighooklib/sighook.cpp
56)
57# Handle pthread and futex mutexes
58if(PANDA_USE_FUTEX)
59    set(UNIX_SOURCES_COMMON ${UNIX_SOURCES_COMMON}
60        ${PANDA_ROOT}/libpandabase/os/unix/futex/mutex.cpp
61    )
62else()
63    set(UNIX_SOURCES_COMMON ${UNIX_SOURCES_COMMON}
64        ${PANDA_ROOT}/libpandabase/os/unix/mutex.cpp
65    )
66endif()
67
68set(WINDOWS_SOURCES
69    ${PANDA_ROOT}/libpandabase/utils/logger.cpp
70    ${PANDA_ROOT}/libpandabase/utils/debug.cpp
71    ${PANDA_ROOT}/libpandabase/utils/dfx.cpp
72    ${PANDA_ROOT}/libpandabase/utils/utf.cpp
73    ${PANDA_ROOT}/libpandabase/utils/json_parser.cpp
74    ${PANDA_ROOT}/libpandabase/os/windows/error.cpp
75    ${PANDA_ROOT}/libpandabase/os/windows/file.cpp
76    ${PANDA_ROOT}/libpandabase/os/windows/mem.cpp
77    ${PANDA_ROOT}/libpandabase/os/windows/thread.cpp
78    ${PANDA_ROOT}/libpandabase/trace/windows/trace.cpp
79    ${PANDA_ROOT}/libpandabase/os/filesystem.cpp
80    ${PANDA_ROOT}/libpandabase/os/unix/mutex.cpp
81    ${PANDA_ROOT}/libpandabase/mem/arena.cpp
82    ${PANDA_ROOT}/libpandabase/mem/arena_allocator.cpp
83    ${PANDA_ROOT}/libpandabase/mem/pool_manager.cpp
84    ${PANDA_ROOT}/libpandabase/mem/pool_map.cpp
85    ${PANDA_ROOT}/libpandabase/mem/base_mem_stats.cpp
86    ${PANDA_ROOT}/libpandabase/mem/mem_config.cpp
87)
88
89set(INCLUDE_DIRECTORIES)
90set(LINK_LIBRARIES)
91
92# Native stacktraces
93if (PANDA_TARGET_WINDOWS)
94    # We are not interesting in native stacktraces on windows
95    list(APPEND WINDOWS_SOURCES
96        ${PANDA_ROOT}/libpandabase/os/stacktrace_stub.cpp
97    )
98elseif (PANDA_TARGET_OHOS)
99    # Not yet supported
100    list(APPEND SOURCES
101        ${PANDA_ROOT}/libpandabase/os/stacktrace_stub.cpp
102    )
103elseif (PANDA_PRODUCT_BUILD)
104    # Don't add libdwarf dependency for product build
105    list(APPEND SOURCES
106        ${PANDA_ROOT}/libpandabase/os/stacktrace_stub.cpp
107    )
108else()
109    # Add common sources for native stacktraces
110    list(APPEND UNIX_SOURCES_COMMON
111        ${PANDA_ROOT}/libpandabase/os/stacktrace.cpp
112        ${PANDA_ROOT}/libpandabase/os/debug_info.cpp
113    )
114    # Handle backtrace function
115    if (PANDA_TARGET_MOBILE)
116        # Not yet supported
117        list(APPEND SOURCES
118            ${PANDA_ROOT}/libpandabase/os/stacktrace_stub.cpp
119        )
120    else()
121        list(APPEND LINK_LIBRARIES dwarf z)
122    endif()
123endif()
124
125if(PANDA_TARGET_UNIX)
126    set(SOURCES ${SOURCES} ${UNIX_SOURCES_COMMON})
127    if(NOT PANDA_TARGET_MACOS)
128        set(SOURCES ${SOURCES}
129            ${PANDA_ROOT}/libpandabase/os/unix/exec.cpp
130            ${PANDA_ROOT}/libpandabase/os/unix/pipe.cpp)
131    endif()
132elseif (PANDA_TARGET_WINDOWS)
133    set(SOURCES ${WINDOWS_SOURCES})
134else()
135    message(FATAL_ERROR "Platform ${CMAKE_SYSTEM_NAME} is not supported")
136endif ()
137
138add_library(arkbase ${PANDA_DEFAULT_LIB_TYPE} ${SOURCES})
139
140set(GENERATED_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated)
141file(MAKE_DIRECTORY ${GENERATED_DIR})
142
143set(EVENTS_GEN_H ${GENERATED_DIR}/events_gen.h)
144panda_gen_file(
145        DATAFILE ${PANDA_ROOT}/libpandabase/events/events.yaml
146        TEMPLATE ${PANDA_ROOT}/libpandabase/events/events_gen.h.erb
147        OUTPUTFILE ${EVENTS_GEN_H}
148        REQUIRES ${PANDA_ROOT}/libpandabase/events/events.rb
149)
150add_custom_target(events_gen DEPENDS ${EVENTS_GEN_H})
151add_dependencies(arkbase events_gen)
152
153panda_add_to_clang_tidy(TARGET arkbase)
154
155if (NOT (PANDA_TARGET_WINDOWS OR PANDA_TARGET_MOBILE OR PANDA_TARGET_MACOS OR PANDA_TARGET_OHOS))
156    target_link_libraries(arkbase stdc++fs)
157endif()
158
159if(NOT PANDA_TARGET_WINDOWS)
160    list(APPEND LINK_LIBRARIES dl)
161endif()
162
163if(PANDA_NEED_LIBATOMIC)
164    list(APPEND LINK_LIBRARIES atomic)
165endif()
166
167if(NOT (PANDA_TARGET_MOBILE OR PANDA_TARGET_OHOS))
168    list(APPEND LINK_LIBRARIES pthread)
169endif()
170
171list(APPEND LINK_LIBRARIES c_secshared)
172
173target_link_libraries(arkbase ${LINK_LIBRARIES})
174
175check_function_exists(gettid HAVE_GETTID)
176
177if (HAVE_GETTID)
178    add_definitions(-DHAVE_GETTID)
179endif (HAVE_GETTID)
180
181target_include_directories(arkbase
182    INTERFACE .
183    PUBLIC ${PANDA_ROOT}
184    PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
185    PUBLIC ${GENERATED_DIR}
186    PUBLIC ${INCLUDE_DIRECTORIES}
187)
188
189panda_add_gtest(
190    NO_CORES
191    NAME arkbase_tests
192    SOURCES
193        tests/list_test.cpp
194        tests/bit_helpers_test.cpp
195        tests/bit_memory_region_test.cpp
196        tests/bit_table_test.cpp
197        tests/bit_utils_test.cpp
198        tests/bit_vector_test.cpp
199        tests/string_helpers_test.cpp
200        tests/type_converter_tests.cpp
201        tests/logger_test.cpp
202        tests/dfx_test.cpp
203        tests/leb128_test.cpp
204        tests/utf_test.cpp
205        tests/arena_test.cpp
206        tests/arena_allocator_test.cpp
207        tests/expected_test.cpp
208        tests/code_allocator_test.cpp
209        tests/small_vector_test.cpp
210        tests/span_test.cpp
211        tests/pandargs_test.cpp
212        tests/pool_map_test.cpp
213        tests/hash_test.cpp
214        tests/math_helpers_test.cpp
215        tests/serializer_test.cpp
216        tests/base_mem_stats_test.cpp
217        tests/unique_fd_test.cpp
218        tests/mem_range_test.cpp
219        tests/mmap_fixed_test.cpp
220        tests/mmap_mem_pool_test.cpp
221        tests/native_bytes_from_mallinfo_test.cpp
222        tests/json_parser_test.cpp
223        tests/alloc_tracker_test.cpp
224    LIBRARIES
225        arkbase
226    SANITIZERS
227        ${PANDA_SANITIZERS_LIST}
228)
229if (TARGET arkbase_tests)
230    target_compile_options(arkbase_tests PUBLIC "-Wno-unused")
231endif()
232
233if(PANDA_TARGET_32)
234    # This test is considered passed as soon as it's built,
235    # so it's enough to add building it to the umbrella tests target.
236    add_executable(memory_literals_test tests/memory_literals_test.cpp)
237    target_link_libraries(memory_literals_test arkbase)
238    set_target_properties(memory_literals_test
239        PROPERTIES
240            EXCLUDE_FROM_ALL TRUE
241            EXCLUDE_FROM_DEFAULT_BUILD TRUE
242            RUNTIME_OUTPUT_DIRECTORY "${PANDA_BINARY_ROOT}/bin-gtests"
243    )
244    add_dependencies(tests memory_literals_test)
245    add_test(
246        NAME memory_literals_test
247        COMMAND ${PANDA_RUN_PREFIX} ${PANDA_BINARY_ROOT}/bin-gtests/memory_literals_test
248    )
249endif()
250
251panda_gen_options(TARGET arkbase YAML_FILE options.yaml GENERATED_HEADER base_options.h)
252
253panda_add_sanitizers(TARGET arkbase SANITIZERS ${PANDA_SANITIZERS_LIST})
254
255if (PANDA_ENABLE_AFL)
256    include("${PANDA_ROOT}/fuzzing/Fuzzing.cmake")
257    panda_substitute_libs(TARGET arkbase LIBS c_secshared)
258endif()
259
260add_check_style("mem")
261add_check_style("utils")
262add_check_style("os")
263add_check_style("tests")
264add_check_style("trace")
265add_check_style("serializer")
266