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