Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / cmake / Modules / SanitizerUtils.cmake
blobdea4d9a218d00c2f6f2cc6a134d9cacffaf24732
1 include(CompilerRTUtils)
3 set(SANITIZER_GEN_DYNAMIC_LIST
4   ${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common/scripts/gen_dynamic_list.py)
6 if(CMAKE_NM)
7   set(SANITIZER_NM "${CMAKE_NM}")
8 else()
9   set(SANITIZER_NM nm)
10 endif()
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
22     ""
23     "PARENT_TARGET"
24     "ARCHS;EXTRA"
25     ${ARGN})
26   foreach(arch ${ARG_ARCHS})
27     set(target_name ${name}-${arch})
28     set(stamp ${CMAKE_CURRENT_BINARY_DIR}/${target_name}.syms-stamp)
29     set(extra_args)
30     foreach(arg ${ARG_EXTRA})
31       list(APPEND extra_args "--extra" ${arg})
32     endforeach()
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}"
41       VERBATIM)
42     add_custom_target(${target_name}-symbols ALL
43       DEPENDS ${stamp}
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})
48     if(ARG_PARENT_TARGET)
49       add_dependencies(${ARG_PARENT_TARGET} ${target_name}-symbols)
50     endif()
51   endforeach()
52 endmacro()
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.
62   set_property(
63     DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
64     APPEND
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})
69   endforeach()
70   set(${link_flags} ${local_link_flags} PARENT_SCOPE)
71 endfunction()
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})
76   set(args)
77   foreach(arg ${ARG_EXTRA})
78     list(APPEND args "--extra" ${arg})
79   endforeach()
80   foreach(arg ${ARG_LIBS})
81     list(APPEND args "$<TARGET_FILE:${arg}>")
82   endforeach()
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}"
90     VERBATIM)
92   add_custom_target(${name}-version-list ALL
93     DEPENDS ${vers})
94 endmacro()