From d4e551a90b6bf6ea4e18fa408f40f39df6ad6fb4 Mon Sep 17 00:00:00 2001
From: Steffen Dettmer <steffen.dettmer@gmail.com>
Date: Mon, 7 Aug 2017 15:30:33 +0200
Subject: FindThreads: Drop try_run to improve cross-compiling support

Use `try_compile` instead of `try_run`.  It is not clear why `try_run`
was ever needed, and it does not work during cross-compiling.

Update the check's source file to remove code associated with actually
running things.  Also remove the ancient `__CLASSIC_C__` code path and
use a simple `int main(void)` as in `Modules/CheckIncludeFile.c.in`.

Fixes: #16920
---
 Modules/CheckForPthreads.c | 40 +++++++---------------------------------
 Modules/FindThreads.cmake  | 15 ++++-----------
 2 files changed, 11 insertions(+), 44 deletions(-)

diff --git a/Modules/CheckForPthreads.c b/Modules/CheckForPthreads.c
index 6fc6f26..e70ceb1 100644
--- a/Modules/CheckForPthreads.c
+++ b/Modules/CheckForPthreads.c
@@ -1,41 +1,15 @@
 #include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
 
-void* runner(void*);
-
-int res = 0;
-#ifdef __CLASSIC_C__
-int main()
-{
-  int ac;
-  char* av[];
-#else
-int main(int ac, char* av[])
+void* start_routine(void* args)
 {
-#endif
-  pthread_t tid[2];
-  pthread_create(&tid[0], 0, runner, (void*)1);
-  pthread_create(&tid[1], 0, runner, (void*)2);
-
-#if defined(__BEOS__) && !defined(__ZETA__) /* (no usleep on BeOS 5.) */
-  usleep(1); /* for strange behavior on single-processor sun */
-#endif
-
-  pthread_join(tid[0], 0);
-  pthread_join(tid[1], 0);
-  if (ac > 1000) {
-    return *av[0];
-  }
-  return res;
+  return args;
 }
 
-void* runner(void* args)
+int main(void)
 {
-  int cc;
-  for (cc = 0; cc < 10; cc++) {
-    printf("%p CC: %d\n", args, cc);
-  }
-  res++;
+  /* This is a compile and link test, no code to actually run things. */
+  pthread_t thread;
+  pthread_create(&thread, 0, start_routine, 0);
+  pthread_join(thread, 0);
   return 0;
 }
diff --git a/Modules/FindThreads.cmake b/Modules/FindThreads.cmake
index 2c6a58b..a0148dd 100644
--- a/Modules/FindThreads.cmake
+++ b/Modules/FindThreads.cmake
@@ -86,23 +86,16 @@ macro(_check_pthreads_flag)
         set(_threads_src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/FindThreads/CheckForPthreads.cxx)
         configure_file(${CMAKE_CURRENT_LIST_DIR}/CheckForPthreads.c "${_threads_src}" COPYONLY)
       endif()
-      try_run(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG
+      try_compile(THREADS_HAVE_PTHREAD_ARG
         ${CMAKE_BINARY_DIR}
         ${_threads_src}
         CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
-        COMPILE_OUTPUT_VARIABLE OUTPUT)
+        OUTPUT_VARIABLE OUTPUT)
       unset(_threads_src)
 
       if(THREADS_HAVE_PTHREAD_ARG)
-        if(THREADS_PTHREAD_ARG STREQUAL "2")
-          set(Threads_FOUND TRUE)
-          message(STATUS "Check if compiler accepts -pthread - yes")
-        else()
-          message(STATUS "Check if compiler accepts -pthread - no")
-          file(APPEND
-            ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
-            "Determining if compiler accepts -pthread returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n")
-        endif()
+        set(Threads_FOUND TRUE)
+        message(STATUS "Check if compiler accepts -pthread - yes")
       else()
         message(STATUS "Check if compiler accepts -pthread - no")
         file(APPEND
-- 
cgit v0.12