cmake Problem: pthread and iOS

A cmake build issue I came across that occurs when building with cmake and the latest version of XCode. The macro find_package(Threads) fails with

-- Looking for pthread.h - not found

Could NOT find Threads (missing: Threads_FOUND)

Digging into the log file tells it is having problems with code signing. What happens is that try_compile() compiles a very small program using pthread, and even this tiny piece needs to be code signed:

Code Signing Error: Code signing is required for product type ‘Application’ in SDK ‘iOS 13.2’

After playing around a bit, I found this workaround:

set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES "CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED")
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO")

find_package(Threads REQUIRED)

unset(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES)
unset(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED)

Now it is able to compile the little test program and configures threads properly in cmake.