• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2021-2022 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(ARKBASE_LTO_SOURCES
20    ${PANDA_ROOT}/libpandabase/mem/alloc_tracker.cpp
21    ${PANDA_ROOT}/libpandabase/mem/arena_allocator.cpp
22    ${PANDA_ROOT}/libpandabase/mem/base_mem_stats.cpp
23    ${PANDA_ROOT}/libpandabase/mem/pool_map.cpp
24    ${PANDA_ROOT}/libpandabase/os/time.cpp
25    ${PANDA_ROOT}/libpandabase/utils/json_builder.cpp
26    ${PANDA_ROOT}/libpandabase/utils/json_parser.cpp
27    ${PANDA_ROOT}/libpandabase/utils/time.cpp
28    ${PANDA_ROOT}/libpandabase/utils/type_converter.cpp
29    ${PANDA_ROOT}/libpandabase/utils/utf.cpp
30)
31
32set(SOURCES
33    ${PANDA_ROOT}/libpandabase/mem/code_allocator.cpp
34    ${PANDA_ROOT}/libpandabase/mem/pool_manager.cpp
35    ${PANDA_ROOT}/libpandabase/mem/mem_config.cpp
36    ${PANDA_ROOT}/libpandabase/utils/debug.cpp
37    ${PANDA_ROOT}/libpandabase/utils/logger.cpp
38    ${PANDA_ROOT}/libpandabase/trace/trace.cpp
39    ${PANDA_ROOT}/libpandabase/os/filesystem.cpp
40    ${PANDA_ROOT}/libpandabase/os/native_stack.cpp
41    ${PANDA_ROOT}/libpandabase/os/property.cpp
42    ${ARKBASE_LTO_SOURCES}
43)
44
45set(ARKBASE_LTO_UNIX_SOURCES
46    ${PANDA_ROOT}/platforms/unix/libpandabase/thread.cpp
47)
48
49set(UNIX_SOURCES_COMMON
50    ${PANDA_ROOT}/platforms/unix/libpandabase/error.cpp
51    ${PANDA_ROOT}/platforms/unix/libpandabase/native_stack.cpp
52    ${PANDA_ROOT}/platforms/unix/libpandabase/property.cpp
53    ${PANDA_ROOT}/platforms/unix/libpandabase/file.cpp
54    ${PANDA_ROOT}/platforms/unix/libpandabase/filesystem.cpp
55    ${PANDA_ROOT}/platforms/unix/libpandabase/library_loader_resolve_symbol.cpp
56    ${PANDA_ROOT}/platforms/unix/libpandabase/mem.cpp
57    ${PANDA_ROOT}/platforms/unix/libpandabase/trace.cpp
58    ${ARKBASE_LTO_UNIX_SOURCES}
59)
60
61
62# Handle pthread and futex mutexes
63if(PANDA_USE_FUTEX)
64    set(UNIX_SOURCES_COMMON ${UNIX_SOURCES_COMMON}
65        ${PANDA_ROOT}/platforms/unix/libpandabase/futex/fmutex.cpp
66        ${PANDA_ROOT}/platforms/unix/libpandabase/futex/mutex.cpp
67    )
68else()
69    set(UNIX_SOURCES_COMMON ${UNIX_SOURCES_COMMON}
70        ${PANDA_ROOT}/libpandabase/os/mutex.cpp
71    )
72endif()
73
74set(WINDOWS_SOURCES
75    ${PANDA_ROOT}/libpandabase/utils/logger.cpp
76    ${PANDA_ROOT}/libpandabase/utils/debug.cpp
77    ${PANDA_ROOT}/libpandabase/utils/time.cpp
78    ${PANDA_ROOT}/libpandabase/utils/utf.cpp
79    ${PANDA_ROOT}/libpandabase/utils/json_builder.cpp
80    ${PANDA_ROOT}/libpandabase/utils/json_parser.cpp
81    ${PANDA_ROOT}/libpandabase/utils/type_converter.cpp
82    ${PANDA_ROOT}/platforms/windows/libpandabase/error.cpp
83    ${PANDA_ROOT}/platforms/windows/libpandabase/library_loader.cpp
84    ${PANDA_ROOT}/platforms/windows/libpandabase/file.cpp
85    ${PANDA_ROOT}/platforms/windows/libpandabase/mem.cpp
86    ${PANDA_ROOT}/platforms/windows/libpandabase/thread.cpp
87    ${PANDA_ROOT}/platforms/windows/libpandabase/filesystem.cpp
88    ${PANDA_ROOT}/platforms/windows/libpandabase/trace.cpp
89    ${PANDA_ROOT}/libpandabase/os/filesystem.cpp
90    ${PANDA_ROOT}/libpandabase/os/mutex.cpp
91    ${PANDA_ROOT}/libpandabase/os/time.cpp
92    ${PANDA_ROOT}/libpandabase/mem/alloc_tracker.cpp
93    ${PANDA_ROOT}/libpandabase/mem/arena_allocator.cpp
94    ${PANDA_ROOT}/libpandabase/mem/pool_manager.cpp
95    ${PANDA_ROOT}/libpandabase/mem/pool_map.cpp
96    ${PANDA_ROOT}/libpandabase/mem/base_mem_stats.cpp
97    ${PANDA_ROOT}/libpandabase/mem/code_allocator.cpp
98    ${PANDA_ROOT}/libpandabase/mem/mem_config.cpp
99)
100
101set(INCLUDE_DIRECTORIES)
102set(LINK_LIBRARIES)
103
104# Handle library load method
105if(NOT PANDA_TARGET_MOBILE)
106    set(UNIX_SOURCES_COMMON ${UNIX_SOURCES_COMMON}
107        ${PANDA_ROOT}/platforms/unix/libpandabase/library_loader_load.cpp
108    )
109endif()
110
111# Native stacktraces
112if (PANDA_TARGET_WINDOWS)
113    # We are not interesting in native stacktraces on windows
114    list(APPEND WINDOWS_SOURCES
115        ${PANDA_ROOT}/libpandabase/os/stacktrace_stub.cpp
116    )
117elseif (PANDA_TARGET_OHOS)
118    # Not yet supported
119    list(APPEND SOURCES
120        ${PANDA_ROOT}/libpandabase/os/stacktrace_stub.cpp
121    )
122elseif (PANDA_PRODUCT_BUILD)
123    # Don't add libdwarf dependency for product build
124    list(APPEND SOURCES
125        ${PANDA_ROOT}/libpandabase/os/stacktrace_stub.cpp
126    )
127else()
128    # Add common sources for native stacktraces
129    list(APPEND UNIX_SOURCES_COMMON
130        ${PANDA_ROOT}/libpandabase/os/stacktrace.cpp
131        ${PANDA_ROOT}/libpandabase/os/debug_info.cpp
132    )
133    # Handle backtrace function
134    if (NOT PANDA_TARGET_MOBILE)
135        list(APPEND LINK_LIBRARIES dwarf z)
136    endif()
137endif()
138
139if(PANDA_TARGET_UNIX)
140    set(SOURCES ${SOURCES} ${UNIX_SOURCES_COMMON})
141    set(ARKBASE_LTO_SOURCES ${ARKBASE_LTO_SOURCES} ${ARKBASE_LTO_UNIX_SOURCES})
142    if(NOT PANDA_TARGET_MACOS)
143        set(SOURCES ${SOURCES}
144            ${PANDA_ROOT}/platforms/unix/libpandabase/exec.cpp)
145    endif()
146elseif (PANDA_TARGET_WINDOWS)
147    set(SOURCES ${WINDOWS_SOURCES})
148else()
149    message(FATAL_ERROR "Platform ${CMAKE_SYSTEM_NAME} is not supported")
150endif ()
151
152if (PANDA_TARGET_ARM64)
153  list(APPEND SOURCES ${PANDA_ROOT}/libpandabase/arch/aarch64/cpu_features.cpp)
154elseif (PANDA_TARGET_AMD64 OR PANDA_TARGET_ARM32)
155  list(APPEND SOURCES ${PANDA_ROOT}/libpandabase/arch/default/cpu_features.cpp)
156else()
157  message(FATAL_ERROR "Arch ${CMAKE_SYSTEM_PROCESSOR} is not supported")
158endif()
159
160add_library(arkbase ${PANDA_DEFAULT_LIB_TYPE} ${SOURCES})
161
162set(GENERATED_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated)
163file(MAKE_DIRECTORY ${GENERATED_DIR})
164
165# Version
166find_package(Git)
167if(Git_FOUND)
168    message(STATUS "Git found: ${GIT_EXECUTABLE}")
169    set(IS_GIT_REPOSITORY)
170    execute_process(COMMAND ${GIT_EXECUTABLE} -C ${PANDA_ROOT} rev-parse
171                    RESULT_VARIABLE IS_GIT_REPOSITORY)
172    if (IS_GIT_REPOSITORY EQUAL 0)
173        set(ARK_VERSION_GIT_HASH)
174        execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
175                        WORKING_DIRECTORY ${PANDA_ROOT}
176                        OUTPUT_VARIABLE ARK_VERSION_GIT_HASH
177                        OUTPUT_STRIP_TRAILING_WHITESPACE)
178    endif()
179else()
180    message("-- Git not found")
181endif()
182
183set(ARK_VERSION_H ${GENERATED_DIR}/ark_version.h)
184configure_file(${CMAKE_CURRENT_SOURCE_DIR}/templates/ark_version.h.in ${ARK_VERSION_H})
185
186add_custom_target(ark_version_gen DEPENDS ${ARK_VERSION_H})
187
188set(EVENTS_GEN_H ${GENERATED_DIR}/events_gen.h)
189panda_gen_file(
190        DATAFILE ${PANDA_ROOT}/libpandabase/events/events.yaml
191        TEMPLATE ${PANDA_ROOT}/libpandabase/events/events_gen.h.erb
192        OUTPUTFILE ${EVENTS_GEN_H}
193        REQUIRES ${PANDA_ROOT}/libpandabase/events/events.rb
194)
195add_custom_target(events_gen DEPENDS ${EVENTS_GEN_H})
196
197panda_add_to_clang_tidy(TARGET arkbase)
198
199if (NOT (PANDA_TARGET_WINDOWS OR PANDA_TARGET_MOBILE OR PANDA_TARGET_MACOS OR PANDA_TARGET_OHOS OR PANDA_ENABLE_FUZZBENCH))
200    list(APPEND LINK_LIBRARIES stdc++fs)
201endif()
202
203if(NOT PANDA_TARGET_WINDOWS)
204    list(APPEND LINK_LIBRARIES dl)
205endif()
206
207if(PANDA_NEED_LIBATOMIC)
208    list(APPEND LINK_LIBRARIES atomic)
209endif()
210
211if(NOT (PANDA_TARGET_MOBILE OR PANDA_TARGET_OHOS))
212    list(APPEND LINK_LIBRARIES pthread)
213endif()
214
215list(APPEND LINK_LIBRARIES c_secshared)
216
217check_function_exists(gettid HAVE_GETTID)
218
219panda_promote_to_definitions(HAVE_GETTID)
220
221option(PANDA_ARKBASE_LTO "Link part of arkbase library into every shared object if using LTO" ON)
222
223# If LTO is enabled, static libraries contain LLVM IR.
224# For an external symbol X defined in one of ARKBASE_LTO_SOURCES,
225# the expected layout with PANDA_ARKBASE_LTO=OFF is:
226# * libarkbase.so: X - defined, strong, default visibility
227# * every other library: X - undefined (if referenced at all)
228# With PANDA_ARKBASE_LTO=ON, the expected layout is as follows:
229# * libarkbase.so: X - defined, weak, default visibility
230# * every other library: X - defined, strong, hidden (if any)
231if(PANDA_ENABLE_LTO AND PANDA_ARKBASE_LTO)
232    add_library(arkbase_lto STATIC ${ARKBASE_LTO_SOURCES})
233    # arkbase_lto: do not export the same symbol from multiple non-arkbase libraries
234    target_compile_options(arkbase_lto PRIVATE "-fvisibility=hidden")
235    # arkbase: do not conflict with (strong hidden) symbols from
236    # other shared objects (due to linked arkbase_lto)
237    target_compile_definitions(arkbase PRIVATE WEAK_SYMBOLS_FOR_LTO)
238    # Force loading of the entire contents of libarkbase_lto.a to make sure
239    # LLD knows about strong symbols. Use INTERFACE to not replace public weak
240    # symbols with strong hidden ones in libarkbase.so itself.
241    set_property(TARGET arkbase APPEND PROPERTY INTERFACE_LINK_LIBRARIES
242                 -Wl,--push-state,--whole-archive arkbase_lto -Wl,--pop-state)
243    # Sanity check: arkbase_lto should not contain any *unexpected* writable
244    # sections (especially .data and .bss).
245    set(LTO_TEST_OBJECT "${CMAKE_CURRENT_BINARY_DIR}/test_arkbase_lto_writable_sections.o")
246    add_custom_target(test_arkbase_lto_writable_sections
247        # Codegen LLVM IR from arkbase_lto to a single object file.
248        # Technically, using ld.lld is enough here, but ask C compiler
249        # to find the correct LLD version for us, then prevent it from
250        # passing some conflicting options to LLD.
251        COMMAND ${CMAKE_C_COMPILER} --target=${CMAKE_C_COMPILER_TARGET}
252                -fuse-ld=lld -r -Wl,-r -nostdlib -o "${LTO_TEST_OBJECT}"
253                -Wl,--whole-archive "$<TARGET_FILE:arkbase_lto>"
254        # Dump section headers ...
255        COMMAND "${CMAKE_READELF}" --sections --wide "${LTO_TEST_OBJECT}"
256                > "${LTO_TEST_OBJECT}.sections"
257        # ... and filter those containing 'W' in the 4th field from the end of line
258        COMMAND grep -E "W[^ ]*( +[^ ]+){3}$" "${LTO_TEST_OBJECT}.sections"
259                > "${LTO_TEST_OBJECT}.sections-writable"
260        # Then check that only *.rel.ro.* and .init_array sections are found.
261        COMMAND sed "/\.rel\.ro\./ d; / \.init_array / d; q1" "${LTO_TEST_OBJECT}.sections-writable"
262        COMMENT "Testing for unexpected mutable state in arkbase_lto"
263        VERBATIM
264    )
265    add_dependencies(tests test_arkbase_lto_writable_sections)
266
267    set(ARKBASE_TARGETS arkbase arkbase_lto)
268else()
269    set(ARKBASE_TARGETS arkbase)
270endif()
271
272foreach(TARGET ${ARKBASE_TARGETS})
273    add_dependencies(${TARGET} ark_version_gen events_gen)
274    target_link_libraries(${TARGET} ${LINK_LIBRARIES})
275    target_include_directories(${TARGET}
276        INTERFACE .
277        PUBLIC ${PANDA_ROOT}
278        PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
279        PUBLIC ${GENERATED_DIR}
280        PUBLIC ${INCLUDE_DIRECTORIES}
281    )
282endforeach()
283
284set(ARKBASE_TESTS_SOURCES
285    tests/list_test.cpp
286    tests/bit_helpers_test.cpp
287    tests/bit_memory_region_test.cpp
288    tests/bit_table_test.cpp
289    tests/bit_utils_test.cpp
290    tests/bit_vector_test.cpp
291    tests/string_helpers_test.cpp
292    tests/type_converter_tests.cpp
293    tests/logger_test.cpp
294    tests/leb128_test.cpp
295    tests/utf_test.cpp
296    tests/arena_test.cpp
297    tests/arena_allocator_test.cpp
298    tests/expected_test.cpp
299    tests/code_allocator_test.cpp
300    tests/small_vector_test.cpp
301    tests/span_test.cpp
302    tests/pandargs_test.cpp
303    tests/pool_map_test.cpp
304    tests/hash_test.cpp
305    tests/math_helpers_test.cpp
306    tests/serializer_test.cpp
307    tests/base_mem_stats_test.cpp
308    tests/unique_fd_test.cpp
309    tests/mmap_fixed_test.cpp
310    tests/mmap_mem_pool_test.cpp
311    tests/native_bytes_from_mallinfo_test.cpp
312    tests/json_builder_test.cpp
313    tests/json_parser_test.cpp
314    tests/alloc_tracker_test.cpp
315    tests/regmask_test.cpp
316    tests/ring_buffer_test.cpp
317    tests/ringbuf/lock_free_ring_buffer_test.cpp
318    tests/base_thread_test.cpp
319)
320if (PANDA_TARGET_UNIX)
321    list(APPEND ARKBASE_TESTS_SOURCES
322            tests/unix_signal_test.cpp
323        )
324endif()
325
326panda_add_gtest(
327    NO_CORES
328    NAME arkbase_tests
329    SOURCES
330        ${ARKBASE_TESTS_SOURCES}
331    LIBRARIES
332        arkbase
333        gmock
334    SANITIZERS
335        ${PANDA_SANITIZERS_LIST}
336)
337if (TARGET arkbase_tests)
338    target_compile_options(arkbase_tests PUBLIC "-Wno-unused")
339endif()
340
341panda_add_gtest(
342    NO_CORES
343    NAME arkbase_mem_range_tests
344    SOURCES
345        tests/mem_range_test.cpp
346    LIBRARIES
347        arkbase
348    SANITIZERS
349        ${PANDA_SANITIZERS_LIST}
350)
351if (TARGET arkbase_mem_range_tests)
352    target_compile_options(arkbase_mem_range_tests PUBLIC "-Wno-unused")
353endif()
354
355if (DEFINED ENV{GENMC_PATH})
356    set(GENMC_TESTS
357        ${PANDA_ROOT}/libpandabase/tests/genmc/mutex_test_genmc.cpp
358        ${PANDA_ROOT}/libpandabase/tests/genmc/mutex_test_2.cpp
359        ${PANDA_ROOT}/libpandabase/tests/genmc/mutex_test_3.cpp
360        ${PANDA_ROOT}/libpandabase/tests/genmc/mutex_test_4.cpp
361    )
362
363    foreach(genmc_test ${GENMC_TESTS})
364        get_filename_component(test_name ${genmc_test} NAME_WE)
365        add_custom_target(genmc_${test_name}
366            COMMAND $ENV{GENMC_PATH} ${genmc_test}
367        )
368        list(APPEND dep_files genmc_${test_name})
369    endforeach()
370
371    add_custom_target(genmc_tests
372        DEPENDS ${dep_files}
373    )
374endif()
375
376if(PANDA_TARGET_32)
377    # This test is considered passed as soon as it's built,
378    # so it's enough to add building it to the umbrella tests target.
379    add_executable(memory_literals_test tests/memory_literals_test.cpp)
380    target_link_libraries(memory_literals_test arkbase)
381    set_target_properties(memory_literals_test
382        PROPERTIES
383            EXCLUDE_FROM_ALL TRUE
384            EXCLUDE_FROM_DEFAULT_BUILD TRUE
385            RUNTIME_OUTPUT_DIRECTORY "${PANDA_BINARY_ROOT}/bin-gtests"
386    )
387    add_dependencies(tests memory_literals_test)
388    add_test(
389        NAME memory_literals_test
390        COMMAND ${PANDA_RUN_PREFIX} ${PANDA_BINARY_ROOT}/bin-gtests/memory_literals_test
391    )
392endif()
393
394panda_gen_options(TARGET arkbase YAML_FILE options.yaml GENERATED_HEADER base_options.h)
395
396panda_add_sanitizers(TARGET arkbase SANITIZERS ${PANDA_SANITIZERS_LIST})
397
398add_check_style("mem")
399add_check_style("utils")
400add_check_style("os")
401add_check_style("trace")
402add_check_style("serializer")
403