1#
2#//===----------------------------------------------------------------------===//
3#//
4#//                     The LLVM Compiler Infrastructure
5#//
6#// This file is dual licensed under the MIT and the University of Illinois Open
7#// Source Licenses. See LICENSE.txt for details.
8#//
9#//===----------------------------------------------------------------------===//
10#
11
12# Checking a linker flag to build a shared library
13# There is no real trivial way to do this in CMake, so we implement it here
14# this will have ${boolean} = TRUE if the flag succeeds, otherwise FALSE.
15function(libomp_check_linker_flag flag boolean)
16  if(NOT DEFINED "${boolean}")
17  set(retval TRUE)
18  set(library_source
19    "int foo(int a) { return a*a; }")
20  set(cmake_source
21    "cmake_minimum_required(VERSION 2.8)
22     project(foo C)
23     set(CMAKE_SHARED_LINKER_FLAGS \"${flag}\")
24     add_library(foo SHARED src_to_link.c)")
25  set(failed_regexes "[Ee]rror;[Uu]nknown;[Ss]kipping;LINK : warning")
26  set(base_dir ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/link_flag_check_${boolean})
27  file(MAKE_DIRECTORY ${base_dir})
28  file(MAKE_DIRECTORY ${base_dir}/build)
29  file(WRITE ${base_dir}/src_to_link.c "${library_source}")
30  file(WRITE ${base_dir}/CMakeLists.txt "${cmake_source}")
31
32  message(STATUS "Performing Test ${boolean}")
33  try_compile(
34    try_compile_result
35    ${base_dir}/build
36    ${base_dir}
37    foo
38    OUTPUT_VARIABLE OUTPUT)
39
40  if(try_compile_result)
41    foreach(regex IN LISTS failed_regexes)
42      if("${OUTPUT}" MATCHES ${regex})
43        set(retval FALSE)
44      endif()
45    endforeach()
46  else()
47    set(retval FALSE)
48  endif()
49
50  if(${retval})
51    set(${boolean} 1 CACHE INTERNAL "Test ${boolean}")
52    message(STATUS "Performing Test ${boolean} - Success")
53    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
54      "Performing C Linker Flag test ${boolean} succeeded with the following output:\n"
55      "${OUTPUT}\n"
56      "Source file was:\n${library_source}\n")
57  else()
58    set(${boolean} "" CACHE INTERNAL "Test ${boolean}")
59    message(STATUS "Performing Test ${boolean} - Failed")
60    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
61      "Performing C Linker Flag test ${boolean} failed with the following output:\n"
62      "${OUTPUT}\n"
63      "Source file was:\n${library_source}\n")
64  endif()
65
66  set(${boolean} ${retval} PARENT_SCOPE)
67  endif()
68endfunction()
69