1include(CompilerRTUtils)
2
3set(SANITIZER_GEN_DYNAMIC_LIST
4  ${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common/scripts/gen_dynamic_list.py)
5
6set(SANITIZER_LINT_SCRIPT
7  ${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common/scripts/check_lint.sh)
8
9if(CMAKE_NM)
10  set(SANITIZER_NM "${CMAKE_NM}")
11else()
12  set(SANITIZER_NM nm)
13endif()
14
15# Create a target "<name>-<arch>-symbols" that would generate the list of
16# symbols that need to be exported from sanitizer runtime "<name>". Function
17# interceptors are exported automatically, user can also provide files with
18# symbol names that should be exported as well.
19#   add_sanitizer_rt_symbols(<name>
20#                            ARCHS <architectures>
21#                            PARENT_TARGET <convenience parent target>
22#                            EXTRA <files with extra symbols to export>)
23macro(add_sanitizer_rt_symbols name)
24  cmake_parse_arguments(ARG
25    ""
26    "PARENT_TARGET"
27    "ARCHS;EXTRA"
28    ${ARGN})
29  foreach(arch ${ARG_ARCHS})
30    set(target_name ${name}-${arch})
31    set(stamp ${CMAKE_CURRENT_BINARY_DIR}/${target_name}.syms-stamp)
32    set(extra_args)
33    foreach(arg ${ARG_EXTRA})
34      list(APPEND extra_args "--extra" ${arg})
35    endforeach()
36    add_custom_command(OUTPUT ${stamp}
37      COMMAND ${Python3_EXECUTABLE}
38        ${SANITIZER_GEN_DYNAMIC_LIST} ${extra_args} $<TARGET_FILE:${target_name}>
39        --nm-executable "${SANITIZER_NM}" -o $<TARGET_FILE:${target_name}>.syms
40      COMMAND ${CMAKE_COMMAND} -E touch ${stamp}
41      DEPENDS ${target_name} ${SANITIZER_GEN_DYNAMIC_LIST} ${ARG_EXTRA}
42      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
43      COMMENT "Generating exported symbols for ${target_name}"
44      VERBATIM)
45    add_custom_target(${target_name}-symbols ALL
46      DEPENDS ${stamp}
47      SOURCES ${SANITIZER_GEN_DYNAMIC_LIST} ${ARG_EXTRA})
48    get_compiler_rt_install_dir(${arch} install_dir)
49    install(FILES $<TARGET_FILE:${target_name}>.syms
50            DESTINATION ${install_dir})
51    if(ARG_PARENT_TARGET)
52      add_dependencies(${ARG_PARENT_TARGET} ${target_name}-symbols)
53    endif()
54  endforeach()
55endmacro()
56
57# This function is only used on Darwin, where undefined symbols must be specified
58# in the linker invocation.
59function(add_weak_symbols libname link_flags)
60  set(weak_symbols_file "${COMPILER_RT_SOURCE_DIR}/lib/${libname}/weak_symbols.txt")
61  file(STRINGS  "${weak_symbols_file}" WEAK_SYMBOLS)
62  # Add this file as a configure-time dependency so that changes to this
63  # file trigger a re-configure. This is necessary so that `${link_flags}`
64  # is changed when appropriate.
65  set_property(
66    DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
67    APPEND
68    PROPERTY CMAKE_CONFIGURE_DEPENDS "${weak_symbols_file}")
69  set(local_link_flags ${${link_flags}})
70  foreach(SYMBOL ${WEAK_SYMBOLS})
71    set(local_link_flags ${local_link_flags} -Wl,-U,${SYMBOL})
72  endforeach()
73  set(${link_flags} ${local_link_flags} PARENT_SCOPE)
74endfunction()
75
76macro(add_sanitizer_rt_version_list name)
77  set(vers ${CMAKE_CURRENT_BINARY_DIR}/${name}.vers)
78  cmake_parse_arguments(ARG "" "" "LIBS;EXTRA" ${ARGN})
79  set(args)
80  foreach(arg ${ARG_EXTRA})
81    list(APPEND args "--extra" ${arg})
82  endforeach()
83  foreach(arg ${ARG_LIBS})
84    list(APPEND args "$<TARGET_FILE:${arg}>")
85  endforeach()
86  add_custom_command(OUTPUT ${vers}
87    COMMAND ${Python3_EXECUTABLE}
88      ${SANITIZER_GEN_DYNAMIC_LIST} --version-list ${args}
89      --nm-executable "${SANITIZER_NM}" -o ${vers}
90    DEPENDS ${SANITIZER_GEN_DYNAMIC_LIST} ${ARG_EXTRA} ${ARG_LIBS}
91    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
92    COMMENT "Generating version list for ${name}"
93    VERBATIM)
94
95  add_custom_target(${name}-version-list ALL
96    DEPENDS ${vers})
97endmacro()
98
99# Add target to check code style for sanitizer runtimes.
100if(CMAKE_HOST_UNIX AND NOT OS_NAME MATCHES "OpenBSD")
101  add_custom_target(SanitizerLintCheck
102    COMMAND env LLVM_CHECKOUT=${LLVM_MAIN_SRC_DIR} SILENT=1 TMPDIR=
103      PYTHON_EXECUTABLE=${Python3_EXECUTABLE}
104      COMPILER_RT=${COMPILER_RT_SOURCE_DIR}
105      ${SANITIZER_LINT_SCRIPT}
106    DEPENDS ${SANITIZER_LINT_SCRIPT}
107    COMMENT "Running lint check for sanitizer sources..."
108    VERBATIM)
109else()
110  add_custom_target(SanitizerLintCheck
111    COMMAND echo "No lint check")
112endif()
113set_target_properties(SanitizerLintCheck
114  PROPERTIES FOLDER "Compiler-RT Misc")
115