From e5170c60d0443d8f50b5bf6a85bea64bea29fbc0 Mon Sep 17 00:00:00 2001
From: "Luis Gustavo S. Barreto" <gustavo@ossystems.com.br>
Date: Wed, 6 Apr 2016 19:42:07 +0000
Subject: [PATCH 3/3] Avoid try_run
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When cross compiling, the executable compiled in the first step usually
cannot be run on the build host. For this reason, we use try_compile()
to determine what version of libjpeg is available.

Upstream-Status: Pending

Signed-off-by: Luis Gustavo S. Barreto <gustavo@ossystems.com.br>

Adjusted to 19.04
Signed-off-by: Andreas Müller <schnitzeltony@gmail.com>

---
 lib/CMakeLists.txt | 75 ++++++++++++++++++++++++++--------------------
 1 file changed, 42 insertions(+), 33 deletions(-)

diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 2a6a08d..c8696ba 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -12,42 +12,51 @@ message(STATUS "Looking for libjpeg version in ${JPEG_INCLUDE_DIR}/jpeglib.h")
 # found in (between libjpeg, libjpeg-turbo and various multilib header
 # forwarding schemes seen in distros), have a simple program print out the
 # right version.
-set(JPEGLIB_VERSION_CHECK_PATH "${CMAKE_CURRENT_BINARY_DIR}/jpeglib-version-check.c")
-file(WRITE ${JPEGLIB_VERSION_CHECK_PATH} "
-#include <stdio.h>
-#include <stdlib.h>
-#include <jpeglib.h>
-
-int main(void) { printf(\"%d\\\n\", JPEG_LIB_VERSION); return 0; }
-")
-
-try_run(JPEGLIB_RUN_RESULT JPEGLIB_COMPILE_RESULT
-    ${CMAKE_CURRENT_BINARY_DIR} ${JPEGLIB_VERSION_CHECK_PATH}
-    CMAKE_FLAGS -DINCLUDE_DIRECTORIES:PATH=${JPEG_INCLUDE_DIR}
-    RUN_OUTPUT_VARIABLE jpeglib_version)
-
-if ((${JPEGLIB_COMPILE_RESULT} EQUAL FALSE) OR ("${JPEGLIB_RUN_RESULT}" EQUAL FAILED_TO_RUN) OR "${jpeglib_version}" STREQUAL "")
+function(JPEGVersionCheck Ret Op Version)
+    set(JPEGLIB_VERSION_CHECK_PATH "${CMAKE_CURRENT_BINARY_DIR}/jpeglib-version-check.c")
+    file(WRITE ${JPEGLIB_VERSION_CHECK_PATH} "
+    #include <stddef.h>
+    #include <stdio.h>
+    #include <jpeglib.h>
+    int main()
+    {
+    #if (JPEG_LIB_VERSION ${Op} ${Version})
+    #error JPEG_LIB_VERSION ${Op} ${Version}
+    #endif
+    return 0;
+    }
+    ")
+    try_compile(COMPILE_RESULT_VAR
+     "${CMAKE_BINARY_DIR}" "${JPEGLIB_VERSION_CHECK_PATH}"
+      COMPILE_DEFINITIONS "${COMPILE_DEFINITIONS}"
+      OUTPUT_VARIABLE COMPILE_OUT)
+    set(${Ret} ${COMPILE_OUT} PARENT_SCOPE)
+endfunction()
+
+find_package(JPEG REQUIRED)
+if(JPEG_FOUND)
+    JPEGVersionCheck(JPEG_LIB_VERSION < 80)
+    if (${JPEG_LIB_VERSION} MATCHES "#error")
+      set(GV_JPEG_DIR libjpeg-62)
+    endif()
+    JPEGVersionCheck(JPEG_LIB_VERSION == 80)
+    if (${JPEG_LIB_VERSION} MATCHES "#error")
+      set(GV_JPEG_DIR libjpeg-80)
+    endif()
+    JPEGVersionCheck(JPEG_LIB_VERSION == 90)
+    if (${JPEG_LIB_VERSION} MATCHES "#error")
+      set(GV_JPEG_DIR libjpeg-90)
+    endif()
+
+    if ("${GV_JPEG_DIR}" STREQUAL "")
+        message(FATAL_ERROR "Unknown libjpeg version")
+    else()
+        message(STATUS "using ${GV_JPEG_DIR}")
+    endif()
+else(JPEG_FOUND)
     message(FATAL_ERROR "Could not find jpeglib.h. This file comes with libjpeg.")
 endif()
 
-if ("${jpeglib_version}" LESS 80)
-    set(GV_JPEG_DIR libjpeg-62)
-endif()
-
-if ("${jpeglib_version}" EQUAL 80)
-    set(GV_JPEG_DIR libjpeg-80)
-endif()
-
-if ("${jpeglib_version}" EQUAL 90)
-    set(GV_JPEG_DIR libjpeg-90)
-endif()
-
-if ("${GV_JPEG_DIR}" STREQUAL "")
-    message(FATAL_ERROR "Unknown libjpeg version: ${jpeglib_version}")
-endif()
-
-message(STATUS "libjpeg version: ${jpeglib_version}")
-
 add_definitions(-Dlibjpeg_EXPORTS)
 include_directories(
     ${CMAKE_CURRENT_SOURCE_DIR}/${GV_JPEG_DIR}
-- 
2.20.1

