[CMake] Fix the value of `config.target_cflags` for non-macOS Apple

platforms.

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.

rdar://problem/50124489

Differential Revision: https://reviews.llvm.org/D58578

llvm-svn: 359305
This commit is contained in:
Dan Liew 2019-04-26 13:22:39 +00:00
parent 2ff59e554b
commit 9f59704a5d
5 changed files with 55 additions and 9 deletions

View File

@ -205,6 +205,32 @@ macro(get_test_cc_for_arch arch cc_out cflags_out)
endif() endif()
endmacro() endmacro()
# Returns CFLAGS that should be used to run tests for the
# specific apple platform and architecture.
function(get_test_cflags_for_apple_platform platform arch cflags_out)
is_valid_apple_platform("${platform}" is_valid_platform)
if (NOT is_valid_platform)
message(FATAL_ERROR "\"${platform}\" is not a valid apple platform")
endif()
set(test_cflags "")
get_target_flags_for_arch(${arch} test_cflags)
list(APPEND test_cflags ${DARWIN_${platform}_CFLAGS})
string(REPLACE ";" " " test_cflags_str "${test_cflags}")
string(APPEND test_cflags_str "${COMPILER_RT_TEST_COMPILER_CFLAGS}")
set(${cflags_out} "${test_cflags_str}" PARENT_SCOPE)
endfunction()
function(is_valid_apple_platform platform is_valid_out)
set(is_valid FALSE)
if ("${platform}" STREQUAL "")
message(FATAL_ERROR "platform cannot be empty")
endif()
if ("${platform}" MATCHES "^(osx|((ios|watchos|tvos)(sim)?))$")
set(is_valid TRUE)
endif()
set(${is_valid_out} ${is_valid} PARENT_SCOPE)
endfunction()
set(ARM64 aarch64) set(ARM64 aarch64)
set(ARM32 arm armhf) set(ARM32 arm armhf)
set(HEXAGON hexagon) set(HEXAGON hexagon)

View File

@ -89,7 +89,11 @@ if(APPLE)
foreach(arch ${DARWIN_iossim_ARCHS}) foreach(arch ${DARWIN_iossim_ARCHS})
set(ASAN_TEST_APPLE_PLATFORM "iossim") set(ASAN_TEST_APPLE_PLATFORM "iossim")
set(ASAN_TEST_TARGET_ARCH ${arch}) set(ASAN_TEST_TARGET_ARCH ${arch})
set(ASAN_TEST_TARGET_CFLAGS "-arch ${arch} -isysroot ${DARWIN_iossim_SYSROOT} ${COMPILER_RT_TEST_COMPILER_CFLAGS}") get_test_cflags_for_apple_platform(
"${ASAN_TEST_APPLE_PLATFORM}"
"${ASAN_TEST_TARGET_ARCH}"
ASAN_TEST_TARGET_CFLAGS
)
set(ASAN_TEST_CONFIG_SUFFIX "-${arch}-${ASAN_TEST_APPLE_PLATFORM}") set(ASAN_TEST_CONFIG_SUFFIX "-${arch}-${ASAN_TEST_APPLE_PLATFORM}")
get_bits_for_arch(${arch} ASAN_TEST_BITS) get_bits_for_arch(${arch} ASAN_TEST_BITS)
string(TOUPPER ${arch} ARCH_UPPER_CASE) string(TOUPPER ${arch} ARCH_UPPER_CASE)
@ -106,7 +110,10 @@ if(APPLE)
foreach (arch ${DARWIN_ios_ARCHS}) foreach (arch ${DARWIN_ios_ARCHS})
set(ASAN_TEST_APPLE_PLATFORM "ios") set(ASAN_TEST_APPLE_PLATFORM "ios")
set(ASAN_TEST_TARGET_ARCH ${arch}) set(ASAN_TEST_TARGET_ARCH ${arch})
set(ASAN_TEST_TARGET_CFLAGS "-arch ${arch} -isysroot ${DARWIN_ios_SYSROOT} ${COMPILER_RT_TEST_COMPILER_CFLAGS}") get_test_cflags_for_apple_platform(
"${ASAN_TEST_APPLE_PLATFORM}"
"${arch}"
ASAN_TEST_TARGET_CFLAGS)
set(ASAN_TEST_CONFIG_SUFFIX "-${arch}-${ASAN_TEST_APPLE_PLATFORM}") set(ASAN_TEST_CONFIG_SUFFIX "-${arch}-${ASAN_TEST_APPLE_PLATFORM}")
get_bits_for_arch(${arch} ASAN_TEST_BITS) get_bits_for_arch(${arch} ASAN_TEST_BITS)
string(TOUPPER ${arch} ARCH_UPPER_CASE) string(TOUPPER ${arch} ARCH_UPPER_CASE)

View File

@ -91,7 +91,11 @@ if (APPLE)
foreach(arch ${DARWIN_ios_ARCHS}) foreach(arch ${DARWIN_ios_ARCHS})
set(LIBFUZZER_TEST_APPLE_PLATFORM "ios") set(LIBFUZZER_TEST_APPLE_PLATFORM "ios")
set(LIBFUZZER_TEST_TARGET_ARCH ${arch}) set(LIBFUZZER_TEST_TARGET_ARCH ${arch})
set(LIBFUZZER_TEST_FLAGS "-arch ${arch} -isysroot ${DARWIN_ios_SYSROOT} ${COMPILER_RT_TEST_COMPILER_CFLAGS}") get_test_cflags_for_apple_platform(
"${LIBFUZZER_TEST_APPLE_PLATFORM}"
"${LIBFUZZER_TEST_TARGET_ARCH}"
LIBFUZZER_TEST_FLAGS
)
set(LIBFUZZER_TEST_CONFIG_SUFFIX "-${arch}-${LIBFUZZER_TEST_APPLE_PLATFORM}") set(LIBFUZZER_TEST_CONFIG_SUFFIX "-${arch}-${LIBFUZZER_TEST_APPLE_PLATFORM}")
string(TOUPPER ${arch} ARCH_UPPER_CASE) string(TOUPPER ${arch} ARCH_UPPER_CASE)
set(CONFIG_NAME "IOS${ARCH_UPPER_CASE}Config") set(CONFIG_NAME "IOS${ARCH_UPPER_CASE}Config")

View File

@ -56,7 +56,11 @@ if(APPLE)
set(TSAN_TEST_APPLE_PLATFORM "iossim") set(TSAN_TEST_APPLE_PLATFORM "iossim")
set(arch "x86_64") set(arch "x86_64")
set(TSAN_TEST_TARGET_ARCH ${arch}) set(TSAN_TEST_TARGET_ARCH ${arch})
set(TSAN_TEST_TARGET_CFLAGS "-arch ${arch} -isysroot ${DARWIN_iossim_SYSROOT} ${COMPILER_RT_TEST_COMPILER_CFLAGS}") get_test_cflags_for_apple_platform(
"${TSAN_TEST_APPLE_PLATFORM}"
"${TSAN_TEST_TARGET_ARCH}"
TSAN_TEST_TARGET_CFLAGS
)
set(TSAN_TEST_CONFIG_SUFFIX "-${arch}-${TSAN_TEST_APPLE_PLATFORM}") set(TSAN_TEST_CONFIG_SUFFIX "-${arch}-${TSAN_TEST_APPLE_PLATFORM}")
string(TOUPPER ${arch} ARCH_UPPER_CASE) string(TOUPPER ${arch} ARCH_UPPER_CASE)
set(CONFIG_NAME "IOSSim${ARCH_UPPER_CASE}Config") set(CONFIG_NAME "IOSSim${ARCH_UPPER_CASE}Config")
@ -71,7 +75,11 @@ if(APPLE)
set(TSAN_TEST_APPLE_PLATFORM "ios") set(TSAN_TEST_APPLE_PLATFORM "ios")
set(arch "arm64") set(arch "arm64")
set(TSAN_TEST_TARGET_ARCH ${arch}) set(TSAN_TEST_TARGET_ARCH ${arch})
set(TSAN_TEST_TARGET_CFLAGS "-arch ${arch} -isysroot ${DARWIN_ios_SYSROOT} ${COMPILER_RT_TEST_COMPILER_CFLAGS}") get_test_cflags_for_apple_platform(
"${TSAN_TEST_APPLE_PLATFORM}"
"${TSAN_TEST_TARGET_ARCH}"
TSAN_TEST_TARGET_CFLAGS
)
set(TSAN_TEST_CONFIG_SUFFIX "-${arch}-${TSAN_TEST_APPLE_PLATFORM}") set(TSAN_TEST_CONFIG_SUFFIX "-${arch}-${TSAN_TEST_APPLE_PLATFORM}")
string(TOUPPER ${arch} ARCH_UPPER_CASE) string(TOUPPER ${arch} ARCH_UPPER_CASE)
set(CONFIG_NAME "IOS${ARCH_UPPER_CASE}Config") set(CONFIG_NAME "IOS${ARCH_UPPER_CASE}Config")

View File

@ -113,10 +113,11 @@ if(APPLE)
endif() endif()
foreach(platform ${UBSAN_APPLE_PLATFORMS}) foreach(platform ${UBSAN_APPLE_PLATFORMS})
foreach(arch ${DARWIN_${platform}_ARCHS}) foreach(arch ${DARWIN_${platform}_ARCHS})
get_target_flags_for_arch(${arch} UBSAN_TEST_TARGET_ARCH_FLAGS_AS_LIST) get_test_cflags_for_apple_platform(
string(REPLACE ";" " " UBSAN_TEST_TARGET_ARCH_FLAGS "${UBSAN_TEST_TARGET_ARCH_FLAGS_AS_LIST}") "${platform}"
set(UBSAN_TEST_TARGET_CFLAGS "${arch}"
"${UBSAN_TEST_TARGET_ARCH_FLAGS} -isysroot ${DARWIN_${platform}_SYSROOT}") UBSAN_TEST_TARGET_CFLAGS
)
if (";${UBSAN_SUPPORTED_ARCH};" MATCHES ";${arch};") if (";${UBSAN_SUPPORTED_ARCH};" MATCHES ";${arch};")
add_ubsan_device_testsuite("Standalone" ubsan ${platform} ${arch}) add_ubsan_device_testsuite("Standalone" ubsan ${platform} ${arch})
endif() endif()