Skip to content

Commit 964909e

Browse files
committed
[CMake] Fix the value of config.target_cflags for non-macOS Apple platforms. Attempt #2.
Summary: The main problem here is that `-*-version_min=` was not being passed to the compiler when building test cases. This can cause problems when testing on devices running older OSs because Clang would previously assume the minimum deployment target is the the latest OS in the SDK which could be much newer than what the device is running. Previously the generated value looked like this: `-arch arm64 -isysroot <path_to_xcode>/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.1.sdk` With this change it now looks like: `-arch arm64 -stdlib=libc++ -miphoneos-version-min=8.0 -isysroot <path_to_xcode>/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.1.sdk` This mirrors the setting of `config.target_cflags` on macOS. This change is made for ASan, LibFuzzer, TSan, and UBSan. To implement this a new `get_test_cflags_for_apple_platform()` function has been added that when given an Apple platform name and architecture returns a string containing the C compiler flags to use when building tests. This also calls a new helper function `is_valid_apple_platform()` that validates Apple platform names. This is the second attempt at landing the patch. The first attempt (r359305) had to be reverted (r359327) due to a buildbot failure. The problem was that calling `get_test_cflags_for_apple_platform()` can trigger a CMake error if the provided architecture is not supported by the current CMake configuration. Previously, this could be triggered by passing `-DCOMPILER_RT_ENABLE_IOS=OFF` to CMake. The root cause is that we were generating test configurations for a list of architectures without checking if the relevant Sanitizer actually supported that architecture. We now intersect the list of architectures for an Apple platform with `<SANITIZER>_SUPPORTED_ARCH` (where `<SANITIZER>` is a Sanitizer name) to iterate through the correct list of architectures. rdar://problem/50124489 Reviewers: kubamracek, yln, vsk, juliehockett, phosek Subscribers: mgorny, javed.absar, kristof.beyls, #sanitizers, llvm-commits Tags: #llvm, #sanitizers Differential Revision: https://reviews.llvm.org/D61242 llvm-svn: 363633
1 parent 1468822 commit 964909e

File tree

5 files changed

+97
-35
lines changed

5 files changed

+97
-35
lines changed

compiler-rt/cmake/config-ix.cmake

+26
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,32 @@ macro(get_test_cc_for_arch arch cc_out cflags_out)
207207
endif()
208208
endmacro()
209209

210+
# Returns CFLAGS that should be used to run tests for the
211+
# specific apple platform and architecture.
212+
function(get_test_cflags_for_apple_platform platform arch cflags_out)
213+
is_valid_apple_platform("${platform}" is_valid_platform)
214+
if (NOT is_valid_platform)
215+
message(FATAL_ERROR "\"${platform}\" is not a valid apple platform")
216+
endif()
217+
set(test_cflags "")
218+
get_target_flags_for_arch(${arch} test_cflags)
219+
list(APPEND test_cflags ${DARWIN_${platform}_CFLAGS})
220+
string(REPLACE ";" " " test_cflags_str "${test_cflags}")
221+
string(APPEND test_cflags_str "${COMPILER_RT_TEST_COMPILER_CFLAGS}")
222+
set(${cflags_out} "${test_cflags_str}" PARENT_SCOPE)
223+
endfunction()
224+
225+
function(is_valid_apple_platform platform is_valid_out)
226+
set(is_valid FALSE)
227+
if ("${platform}" STREQUAL "")
228+
message(FATAL_ERROR "platform cannot be empty")
229+
endif()
230+
if ("${platform}" MATCHES "^(osx|((ios|watchos|tvos)(sim)?))$")
231+
set(is_valid TRUE)
232+
endif()
233+
set(${is_valid_out} ${is_valid} PARENT_SCOPE)
234+
endfunction()
235+
210236
set(ARM64 aarch64)
211237
set(ARM32 arm armhf)
212238
set(HEXAGON hexagon)

compiler-rt/test/asan/CMakeLists.txt

+15-4
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,22 @@ endforeach()
8181
# variable to select which iOS device or simulator to use, e.g.:
8282
# SANITIZER_IOSSIM_TEST_DEVICE_IDENTIFIER="iPhone 6"
8383
if(APPLE)
84+
# FIXME(dliew): This logic should be refactored to the way UBSan Darwin
85+
# testing is done.
8486
set(EXCLUDE_FROM_ALL ON)
8587

8688
set(ASAN_TEST_TARGET_CC ${COMPILER_RT_TEST_COMPILER})
8789
set(ASAN_TEST_DYNAMIC True)
8890

89-
foreach(arch ${DARWIN_iossim_ARCHS})
91+
list_intersect(ASAN_TEST_IOSSIM_ARCHS ASAN_SUPPORTED_ARCH DARWIN_iossim_ARCHS)
92+
foreach(arch ${ASAN_TEST_IOSSIM_ARCHS})
9093
set(ASAN_TEST_APPLE_PLATFORM "iossim")
9194
set(ASAN_TEST_TARGET_ARCH ${arch})
92-
set(ASAN_TEST_TARGET_CFLAGS "-arch ${arch} -isysroot ${DARWIN_iossim_SYSROOT} ${COMPILER_RT_TEST_COMPILER_CFLAGS}")
95+
get_test_cflags_for_apple_platform(
96+
"${ASAN_TEST_APPLE_PLATFORM}"
97+
"${ASAN_TEST_TARGET_ARCH}"
98+
ASAN_TEST_TARGET_CFLAGS
99+
)
93100
set(ASAN_TEST_CONFIG_SUFFIX "-${arch}-${ASAN_TEST_APPLE_PLATFORM}")
94101
get_bits_for_arch(${arch} ASAN_TEST_BITS)
95102
string(TOUPPER ${arch} ARCH_UPPER_CASE)
@@ -103,10 +110,14 @@ if(APPLE)
103110
DEPENDS ${ASAN_TEST_DEPS})
104111
endforeach()
105112

106-
foreach (arch ${DARWIN_ios_ARCHS})
113+
list_intersect(ASAN_TEST_IOS_ARCHS ASAN_SUPPORTED_ARCH DARWIN_ios_ARCHS)
114+
foreach (arch ${ASAN_TEST_IOS_ARCHS})
107115
set(ASAN_TEST_APPLE_PLATFORM "ios")
108116
set(ASAN_TEST_TARGET_ARCH ${arch})
109-
set(ASAN_TEST_TARGET_CFLAGS "-arch ${arch} -isysroot ${DARWIN_ios_SYSROOT} ${COMPILER_RT_TEST_COMPILER_CFLAGS}")
117+
get_test_cflags_for_apple_platform(
118+
"${ASAN_TEST_APPLE_PLATFORM}"
119+
"${arch}"
120+
ASAN_TEST_TARGET_CFLAGS)
110121
set(ASAN_TEST_CONFIG_SUFFIX "-${arch}-${ASAN_TEST_APPLE_PLATFORM}")
111122
get_bits_for_arch(${arch} ASAN_TEST_BITS)
112123
string(TOUPPER ${arch} ARCH_UPPER_CASE)

compiler-rt/test/fuzzer/CMakeLists.txt

+9-2
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,19 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
8989
endif()
9090

9191
if (APPLE)
92+
# FIXME(dliew): This logic should be refactored to the way UBSan Darwin
93+
# testing is done.
9294
set(EXCLUDE_FROM_ALL ON)
9395

94-
foreach(arch ${DARWIN_ios_ARCHS})
96+
list_intersect(FUZZER_TEST_IOS_ARCHS FUZZER_SUPPORTED_ARCH DARWIN_ios_ARCHS)
97+
foreach(arch ${FUZZER_TEST_IOS_ARCHS})
9598
set(LIBFUZZER_TEST_APPLE_PLATFORM "ios")
9699
set(LIBFUZZER_TEST_TARGET_ARCH ${arch})
97-
set(LIBFUZZER_TEST_FLAGS "-arch ${arch} -isysroot ${DARWIN_ios_SYSROOT} ${COMPILER_RT_TEST_COMPILER_CFLAGS}")
100+
get_test_cflags_for_apple_platform(
101+
"${LIBFUZZER_TEST_APPLE_PLATFORM}"
102+
"${LIBFUZZER_TEST_TARGET_ARCH}"
103+
LIBFUZZER_TEST_FLAGS
104+
)
98105
set(LIBFUZZER_TEST_CONFIG_SUFFIX "-${arch}-${LIBFUZZER_TEST_APPLE_PLATFORM}")
99106
string(TOUPPER ${arch} ARCH_UPPER_CASE)
100107
set(CONFIG_NAME "IOS${ARCH_UPPER_CASE}Config")

compiler-rt/test/tsan/CMakeLists.txt

+42-28
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,53 @@ endforeach()
4949
# variable to select which iOS device or simulator to use, e.g.:
5050
# SANITIZER_IOSSIM_TEST_DEVICE_IDENTIFIER="iPhone 6"
5151
if(APPLE)
52+
# FIXME(dliew): This logic should be refactored to the way UBSan Darwin
53+
# testing is done.
5254
set(EXCLUDE_FROM_ALL ON)
5355

5456
set(TSAN_TEST_TARGET_CC ${COMPILER_RT_TEST_COMPILER})
5557

56-
set(TSAN_TEST_APPLE_PLATFORM "iossim")
57-
set(arch "x86_64")
58-
set(TSAN_TEST_TARGET_ARCH ${arch})
59-
set(TSAN_TEST_TARGET_CFLAGS "-arch ${arch} -isysroot ${DARWIN_iossim_SYSROOT} ${COMPILER_RT_TEST_COMPILER_CFLAGS}")
60-
set(TSAN_TEST_CONFIG_SUFFIX "-${arch}-${TSAN_TEST_APPLE_PLATFORM}")
61-
string(TOUPPER ${arch} ARCH_UPPER_CASE)
62-
set(CONFIG_NAME "IOSSim${ARCH_UPPER_CASE}Config")
63-
configure_lit_site_cfg(
64-
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
65-
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg
66-
)
67-
add_lit_testsuite(check-tsan-iossim-${arch} "ThreadSanitizer iOS Simulator ${arch} tests"
68-
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/
69-
DEPENDS ${TSAN_TEST_DEPS})
58+
list_intersect(TSAN_TEST_IOSSIM_ARCHS TSAN_SUPPORTED_ARCH DARWIN_iossim_ARCHS)
59+
foreach(arch ${TSAN_TEST_IOSSIM_ARCHS})
60+
set(TSAN_TEST_APPLE_PLATFORM "iossim")
61+
set(TSAN_TEST_TARGET_ARCH ${arch})
62+
get_test_cflags_for_apple_platform(
63+
"${TSAN_TEST_APPLE_PLATFORM}"
64+
"${TSAN_TEST_TARGET_ARCH}"
65+
TSAN_TEST_TARGET_CFLAGS
66+
)
67+
set(TSAN_TEST_CONFIG_SUFFIX "-${arch}-${TSAN_TEST_APPLE_PLATFORM}")
68+
string(TOUPPER ${arch} ARCH_UPPER_CASE)
69+
set(CONFIG_NAME "IOSSim${ARCH_UPPER_CASE}Config")
70+
configure_lit_site_cfg(
71+
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
72+
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg
73+
)
74+
add_lit_testsuite(check-tsan-iossim-${arch} "ThreadSanitizer iOS Simulator ${arch} tests"
75+
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/
76+
DEPENDS ${TSAN_TEST_DEPS})
77+
endforeach()
7078

71-
set(TSAN_TEST_APPLE_PLATFORM "ios")
72-
set(arch "arm64")
73-
set(TSAN_TEST_TARGET_ARCH ${arch})
74-
set(TSAN_TEST_TARGET_CFLAGS "-arch ${arch} -isysroot ${DARWIN_ios_SYSROOT} ${COMPILER_RT_TEST_COMPILER_CFLAGS}")
75-
set(TSAN_TEST_CONFIG_SUFFIX "-${arch}-${TSAN_TEST_APPLE_PLATFORM}")
76-
string(TOUPPER ${arch} ARCH_UPPER_CASE)
77-
set(CONFIG_NAME "IOS${ARCH_UPPER_CASE}Config")
78-
configure_lit_site_cfg(
79-
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
80-
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg
81-
)
82-
add_lit_testsuite(check-tsan-ios-${arch} "ThreadSanitizer iOS Simulator ${arch} tests"
83-
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/
84-
DEPENDS ${TSAN_TEST_DEPS})
79+
list_intersect(TSAN_TEST_IOS_ARCHS TSAN_SUPPORTED_ARCH DARWIN_ios_ARCHS)
80+
foreach(arch ${TSAN_TEST_IOS_ARCHS})
81+
set(TSAN_TEST_APPLE_PLATFORM "ios")
82+
set(TSAN_TEST_TARGET_ARCH ${arch})
83+
get_test_cflags_for_apple_platform(
84+
"${TSAN_TEST_APPLE_PLATFORM}"
85+
"${TSAN_TEST_TARGET_ARCH}"
86+
TSAN_TEST_TARGET_CFLAGS
87+
)
88+
set(TSAN_TEST_CONFIG_SUFFIX "-${arch}-${TSAN_TEST_APPLE_PLATFORM}")
89+
string(TOUPPER ${arch} ARCH_UPPER_CASE)
90+
set(CONFIG_NAME "IOS${ARCH_UPPER_CASE}Config")
91+
configure_lit_site_cfg(
92+
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
93+
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg
94+
)
95+
add_lit_testsuite(check-tsan-ios-${arch} "ThreadSanitizer iOS ${arch} tests"
96+
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/
97+
DEPENDS ${TSAN_TEST_DEPS})
98+
endforeach()
8599

86100
set(EXCLUDE_FROM_ALL OFF)
87101
endif()

compiler-rt/test/ubsan/CMakeLists.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ if(APPLE)
113113
endif()
114114
foreach(platform ${UBSAN_APPLE_PLATFORMS})
115115
foreach(arch ${DARWIN_${platform}_ARCHS})
116-
set(UBSAN_TEST_TARGET_CFLAGS "-arch ${arch} -isysroot ${DARWIN_${platform}_SYSROOT}")
116+
get_test_cflags_for_apple_platform(
117+
"${platform}"
118+
"${arch}"
119+
UBSAN_TEST_TARGET_CFLAGS
120+
)
117121
if (";${UBSAN_SUPPORTED_ARCH};" MATCHES ";${arch};")
118122
add_ubsan_device_testsuite("Standalone" ubsan ${platform} ${arch})
119123
endif()

0 commit comments

Comments
 (0)