1#
2#//===----------------------------------------------------------------------===//
3#//
4#// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5#// See https://llvm.org/LICENSE.txt for license information.
6#// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7#//
8#//===----------------------------------------------------------------------===//
9#
10
11include(GNUInstallDirs)
12
13# Checking a linker flag to build a shared library
14# There is no real trivial way to do this in CMake, so we implement it here
15# this will have ${boolean} = TRUE if the flag succeeds, otherwise FALSE.
16function(libomp_check_linker_flag flag boolean)
17  if(NOT DEFINED "${boolean}")
18  set(retval TRUE)
19  set(library_source
20    "int foo(int a) { return a*a; }")
21  set(cmake_source
22    "cmake_minimum_required(VERSION 3.13.4)
23     project(foo C)
24     set(CMAKE_SHARED_LINKER_FLAGS \"${flag}\")
25     add_library(foo SHARED src_to_link.c)")
26  # Compiling as a part of runtimes introduces ARCH-unknown-linux-gnu as a part
27  # of a working directory.  So adding a guard for unknown.
28  set(failed_regexes "[Ee]rror;[Uu]nknown[^-];[Ss]kipping;LINK : warning;Unsupported command line")
29  set(base_dir ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/link_flag_check_${boolean})
30  file(MAKE_DIRECTORY ${base_dir})
31  file(MAKE_DIRECTORY ${base_dir}/build)
32  file(WRITE ${base_dir}/src_to_link.c "${library_source}")
33  file(WRITE ${base_dir}/CMakeLists.txt "${cmake_source}")
34
35  message(STATUS "Performing Test ${boolean}")
36  try_compile(
37    try_compile_result
38    ${base_dir}/build
39    ${base_dir}
40    foo
41    OUTPUT_VARIABLE OUTPUT)
42
43  if(try_compile_result)
44    foreach(regex IN LISTS failed_regexes)
45      # Ignore the warning about the newer or unknown CUDA version.
46      if(("${OUTPUT}" MATCHES ${regex}) AND NOT ("${OUTPUT}" MATCHES "Unknown CUDA version"))
47        set(retval FALSE)
48      endif()
49    endforeach()
50  else()
51    set(retval FALSE)
52  endif()
53
54  if(${retval})
55    set(${boolean} 1 CACHE INTERNAL "Test ${boolean}")
56    message(STATUS "Performing Test ${boolean} - Success")
57    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
58      "Performing C Linker Flag test ${boolean} succeeded with the following output:\n"
59      "${OUTPUT}\n"
60      "Source file was:\n${library_source}\n")
61  else()
62    set(${boolean} "" CACHE INTERNAL "Test ${boolean}")
63    message(STATUS "Performing Test ${boolean} - Failed")
64    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
65      "Performing C Linker Flag test ${boolean} failed with the following output:\n"
66      "${OUTPUT}\n"
67      "Source file was:\n${library_source}\n")
68  endif()
69
70  set(${boolean} ${retval} PARENT_SCOPE)
71  endif()
72endfunction()
73