1# Copied & modified from https://android.googlesource.com/platform/external/eigen/+/master/cmake/FindStandardMathLibrary.cmake
2# Copyright (c) 2010 Benoit Jacob <jacob.benoit.1@gmail.com>
3# Redistribution and use is allowed according to the terms of the 2-clause BSD license.
4# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
5
6#Detect whether this platform requires libm for pow().
7
8include(CheckCXXSourceCompiles)
9macro (gnc_check_standard_math_library)
10  set(find_standard_math_library_test_program
11"
12#include <math.h>
13int main(int argc, char** argv)
14{
15    double foo = pow(2.0, 2.0);
16    return foo == 4.0;
17}"
18  )
19
20  set(CMAKE_REQUIRED_FLAGS "")
21  set(CMAKE_REQUIRED_LIBRARIES "")
22  check_c_source_compiles(
23    "${find_standard_math_library_test_program}"
24    standard_math_library_linked_to_automatically
25    )
26  if(standard_math_library_linked_to_automatically)
27    # the test program linked successfully without any linker flag.
28    set(STANDARD_MATH_LIBRARY "")
29    set(STANDARD_MATH_LIBRARY_FOUND TRUE)
30  else()
31    # the test program did not link successfully without any linker flag.
32    # Try again with standard name 'm' for the standard math library.
33    set(CMAKE_REQUIRED_LIBRARIES "m")
34    check_c_source_compiles(
35      "${find_standard_math_library_test_program}"
36      standard_math_library_linked_to_as_m)
37    if(standard_math_library_linked_to_as_m)
38      # the test program linked successfully when linking to the 'm' library
39      set(STANDARD_MATH_LIBRARY "m")
40      set(STANDARD_MATH_LIBRARY_FOUND TRUE)
41    else()
42      # the test program still doesn't link successfully
43      set(STANDARD_MATH_LIBRARY_FOUND FALSE)
44    endif()
45  endif()
46endmacro()
47