Jonathan Peyton | 2e01335 | 2015-07-15 16:05:30 +0000 | [diff] [blame] | 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. |
| 15 | function(libomp_check_linker_flag flag boolean) |
| 16 | if(NOT DEFINED "${boolean}") |
Jonathan Peyton | 5b4acbd | 2015-07-15 16:57:19 +0000 | [diff] [blame] | 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") |
Jonathan Peyton | 77c7898 | 2015-07-23 14:41:35 +0000 | [diff] [blame] | 26 | set(base_dir ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/link_flag_check_${boolean}) |
Jonathan Peyton | 5b4acbd | 2015-07-15 16:57:19 +0000 | [diff] [blame] | 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}") |
Jonathan Peyton | 2e01335 | 2015-07-15 16:05:30 +0000 | [diff] [blame] | 31 | |
Jonathan Peyton | 5b4acbd | 2015-07-15 16:57:19 +0000 | [diff] [blame] | 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) |
Jonathan Peyton | 2e01335 | 2015-07-15 16:05:30 +0000 | [diff] [blame] | 39 | |
Jonathan Peyton | 5b4acbd | 2015-07-15 16:57:19 +0000 | [diff] [blame] | 40 | if(try_compile_result) |
| 41 | foreach(regex IN LISTS failed_regexes) |
| 42 | if("${OUTPUT}" MATCHES ${regex}) |
Jonathan Peyton | 2e01335 | 2015-07-15 16:05:30 +0000 | [diff] [blame] | 43 | set(retval FALSE) |
Jonathan Peyton | 5b4acbd | 2015-07-15 16:57:19 +0000 | [diff] [blame] | 44 | endif() |
| 45 | endforeach() |
| 46 | else() |
| 47 | set(retval FALSE) |
| 48 | endif() |
Jonathan Peyton | 2e01335 | 2015-07-15 16:05:30 +0000 | [diff] [blame] | 49 | |
Jonathan Peyton | 5b4acbd | 2015-07-15 16:57:19 +0000 | [diff] [blame] | 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() |
Jonathan Peyton | 2e01335 | 2015-07-15 16:05:30 +0000 | [diff] [blame] | 65 | |
Jonathan Peyton | 5b4acbd | 2015-07-15 16:57:19 +0000 | [diff] [blame] | 66 | set(${boolean} ${retval} PARENT_SCOPE) |
Jonathan Peyton | 2e01335 | 2015-07-15 16:05:30 +0000 | [diff] [blame] | 67 | endif() |
| 68 | endfunction() |