Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 1 | # - Try to find how to link to the standard math library, if anything at all is needed to do. |
| 2 | # On most platforms this is automatic, but for example it's not automatic on QNX. |
| 3 | # |
| 4 | # Once done this will define |
| 5 | # |
| 6 | # STANDARD_MATH_LIBRARY_FOUND - we found how to successfully link to the standard math library |
| 7 | # STANDARD_MATH_LIBRARY - the name of the standard library that one has to link to. |
| 8 | # -- this will be left empty if it's automatic (most platforms). |
| 9 | # -- this will be set to "m" on platforms where one must explicitly |
| 10 | # pass the "-lm" linker flag. |
| 11 | # |
| 12 | # Copyright (c) 2010 Benoit Jacob <jacob.benoit.1@gmail.com> |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame] | 13 | # 2020 Susi Lehtola <susi.lehtola@gmail.com> |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 14 | # Redistribution and use is allowed according to the terms of the 2-clause BSD license. |
| 15 | |
| 16 | |
| 17 | include(CheckCXXSourceCompiles) |
| 18 | |
| 19 | # a little test program for c++ math functions. |
| 20 | # notice the std:: is required on some platforms such as QNX |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame] | 21 | # notice the (void) is required if -Wall (-Wunused-value) is added to CMAKE_CXX_FLAG |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 22 | |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame] | 23 | # We read in the arguments from standard input to avoid the compiler optimizing away the calls |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 24 | set(find_standard_math_library_test_program |
Austin Schuh | c55b017 | 2022-02-20 17:52:35 -0800 | [diff] [blame] | 25 | " |
| 26 | #include<cmath> |
| 27 | int main(int argc, char **){ |
| 28 | return int(std::sin(double(argc)) + std::log(double(argc))); |
| 29 | }") |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 30 | |
| 31 | # first try compiling/linking the test program without any linker flags |
| 32 | |
| 33 | set(CMAKE_REQUIRED_FLAGS "") |
| 34 | set(CMAKE_REQUIRED_LIBRARIES "") |
| 35 | CHECK_CXX_SOURCE_COMPILES( |
| 36 | "${find_standard_math_library_test_program}" |
| 37 | standard_math_library_linked_to_automatically |
| 38 | ) |
| 39 | |
| 40 | if(standard_math_library_linked_to_automatically) |
| 41 | |
| 42 | # the test program linked successfully without any linker flag. |
| 43 | set(STANDARD_MATH_LIBRARY "") |
| 44 | set(STANDARD_MATH_LIBRARY_FOUND TRUE) |
| 45 | |
| 46 | else() |
| 47 | |
| 48 | # the test program did not link successfully without any linker flag. |
| 49 | # This is a very uncommon case that so far we only saw on QNX. The next try is the |
| 50 | # standard name 'm' for the standard math library. |
| 51 | |
| 52 | set(CMAKE_REQUIRED_LIBRARIES "m") |
| 53 | CHECK_CXX_SOURCE_COMPILES( |
| 54 | "${find_standard_math_library_test_program}" |
| 55 | standard_math_library_linked_to_as_m) |
| 56 | |
| 57 | if(standard_math_library_linked_to_as_m) |
| 58 | |
| 59 | # the test program linked successfully when linking to the 'm' library |
| 60 | set(STANDARD_MATH_LIBRARY "m") |
| 61 | set(STANDARD_MATH_LIBRARY_FOUND TRUE) |
| 62 | |
| 63 | else() |
| 64 | |
| 65 | # the test program still doesn't link successfully |
| 66 | set(STANDARD_MATH_LIBRARY_FOUND FALSE) |
| 67 | |
| 68 | endif() |
| 69 | |
| 70 | endif() |