1 include(CompilerRTUtils)
3 set(SANITIZER_GEN_DYNAMIC_LIST
4 ${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common/scripts/gen_dynamic_list.py)
7 set(SANITIZER_NM "${CMAKE_NM}")
12 # Create a target "<name>-<arch>-symbols" that would generate the list of
13 # symbols that need to be exported from sanitizer runtime "<name>". Function
14 # interceptors are exported automatically, user can also provide files with
15 # symbol names that should be exported as well.
16 # add_sanitizer_rt_symbols(<name>
17 # ARCHS <architectures>
18 # PARENT_TARGET <convenience parent target>
19 # EXTRA <files with extra symbols to export>)
20 macro(add_sanitizer_rt_symbols name)
21 cmake_parse_arguments(ARG
26 foreach(arch ${ARG_ARCHS})
27 set(target_name ${name}-${arch})
28 set(stamp ${CMAKE_CURRENT_BINARY_DIR}/${target_name}.syms-stamp)
30 foreach(arg ${ARG_EXTRA})
31 list(APPEND extra_args "--extra" ${arg})
33 add_custom_command(OUTPUT ${stamp}
34 COMMAND ${Python3_EXECUTABLE}
35 ${SANITIZER_GEN_DYNAMIC_LIST} ${extra_args} $<TARGET_FILE:${target_name}>
36 --nm-executable "${SANITIZER_NM}" -o $<TARGET_FILE:${target_name}>.syms
37 COMMAND ${CMAKE_COMMAND} -E touch ${stamp}
38 DEPENDS ${target_name} ${SANITIZER_GEN_DYNAMIC_LIST} ${ARG_EXTRA}
39 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
40 COMMENT "Generating exported symbols for ${target_name}"
42 add_custom_target(${target_name}-symbols ALL
44 SOURCES ${SANITIZER_GEN_DYNAMIC_LIST} ${ARG_EXTRA})
45 get_compiler_rt_install_dir(${arch} install_dir)
46 install(FILES $<TARGET_FILE:${target_name}>.syms
47 DESTINATION ${install_dir})
49 add_dependencies(${ARG_PARENT_TARGET} ${target_name}-symbols)
54 # This function is only used on Darwin, where undefined symbols must be specified
55 # in the linker invocation.
56 function(add_weak_symbols libname link_flags)
57 set(weak_symbols_file "${COMPILER_RT_SOURCE_DIR}/lib/${libname}/weak_symbols.txt")
58 file(STRINGS "${weak_symbols_file}" WEAK_SYMBOLS)
59 # Add this file as a configure-time dependency so that changes to this
60 # file trigger a re-configure. This is necessary so that `${link_flags}`
61 # is changed when appropriate.
63 DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
65 PROPERTY CMAKE_CONFIGURE_DEPENDS "${weak_symbols_file}")
66 set(local_link_flags ${${link_flags}})
67 foreach(SYMBOL ${WEAK_SYMBOLS})
68 set(local_link_flags ${local_link_flags} -Wl,-U,${SYMBOL})
70 set(${link_flags} ${local_link_flags} PARENT_SCOPE)
73 macro(add_sanitizer_rt_version_list name)
74 set(vers ${CMAKE_CURRENT_BINARY_DIR}/${name}.vers)
75 cmake_parse_arguments(ARG "" "" "LIBS;EXTRA" ${ARGN})
77 foreach(arg ${ARG_EXTRA})
78 list(APPEND args "--extra" ${arg})
80 foreach(arg ${ARG_LIBS})
81 list(APPEND args "$<TARGET_FILE:${arg}>")
83 add_custom_command(OUTPUT ${vers}
84 COMMAND ${Python3_EXECUTABLE}
85 ${SANITIZER_GEN_DYNAMIC_LIST} --version-list ${args}
86 --nm-executable "${SANITIZER_NM}" -o ${vers}
87 DEPENDS ${SANITIZER_GEN_DYNAMIC_LIST} ${ARG_EXTRA} ${ARG_LIBS}
88 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
89 COMMENT "Generating version list for ${name}"
92 add_custom_target(${name}-version-list ALL