diff --git a/common/cmake_modules/CMakeLists.txt b/common/cmake_modules/CMakeLists.txt index 115c90fd56..e25f806b28 100644 --- a/common/cmake_modules/CMakeLists.txt +++ b/common/cmake_modules/CMakeLists.txt @@ -1,7 +1,7 @@ set(cmake_FILES GncAddGSchemaTargets.cmake GncAddSchemeTargets.cmake - GncAddSwigCommand.cmake GncAddTest.cmake + GncAddSwigCommand.cmake GncAddTest.cmake GncFindLibm.cmake MacroAddSourceFileCompileFlags.cmake MacroAppendForeach.cmake MakeDist.cmake MakeDistFiles.cmake MakeDistCheck.cmake ) diff --git a/common/cmake_modules/GncFindLibm.cmake b/common/cmake_modules/GncFindLibm.cmake new file mode 100644 index 0000000000..ab29df95ce --- /dev/null +++ b/common/cmake_modules/GncFindLibm.cmake @@ -0,0 +1,46 @@ +# Copied & modified from https://android.googlesource.com/platform/external/eigen/+/master/cmake/FindStandardMathLibrary.cmake +# Copyright (c) 2010 Benoit Jacob +# Redistribution and use is allowed according to the terms of the 2-clause BSD license. +# For details see the accompanying COPYING-CMAKE-SCRIPTS file. + +#Detect whether this platform requires libm for pow(). + +include(CheckCXXSourceCompiles) +macro (gnc_check_standard_math_library) + set(find_standard_math_library_test_program +" +#include +int main(int argc, char** argv) +{ + double foo = pow(2.0, 2.0); + return foo == 4.0; +}" + ) + + set(CMAKE_REQUIRED_FLAGS "") + set(CMAKE_REQUIRED_LIBRARIES "") + check_c_source_compiles( + "${find_standard_math_library_test_program}" + standard_math_library_linked_to_automatically + ) + if(standard_math_library_linked_to_automatically) + # the test program linked successfully without any linker flag. + set(STANDARD_MATH_LIBRARY "") + set(STANDARD_MATH_LIBRARY_FOUND TRUE) + else() + # the test program did not link successfully without any linker flag. + # Try again with standard name 'm' for the standard math library. + set(CMAKE_REQUIRED_LIBRARIES "m") + check_c_source_compiles( + "${find_standard_math_library_test_program}" + standard_math_library_linked_to_as_m) + if(standard_math_library_linked_to_as_m) + # the test program linked successfully when linking to the 'm' library + set(STANDARD_MATH_LIBRARY "m") + set(STANDARD_MATH_LIBRARY_FOUND TRUE) + else() + # the test program still doesn't link successfully + set(STANDARD_MATH_LIBRARY_FOUND FALSE) + endif() + endif() +endmacro() diff --git a/libgnucash/app-utils/CMakeLists.txt b/libgnucash/app-utils/CMakeLists.txt index 4cfa65a35c..2cc9c82f1a 100644 --- a/libgnucash/app-utils/CMakeLists.txt +++ b/libgnucash/app-utils/CMakeLists.txt @@ -1,6 +1,9 @@ # NB: Unit tests which require GSchemas should be made conditional on COMPILE_GSCHEMAS. add_subdirectory(test) add_subdirectory(mocks) + +include (GncFindLibm) + # Build the library set (app_utils_noinst_HEADERS @@ -67,10 +70,15 @@ set (app_utils_SOURCES gnc-ui-util.c gnc-ui-balances.c option-util.c -) + ) set_source_files_properties (${app_utils_SOURCES} PROPERTIES OBJECT_DEPENDS ${CONFIG_H}) +gnc_check_standard_math_library() +if (NOT STANDARD_MATH_LIBRARY_FOUND) + message(FATAL_ERROR "An implementation of the standard C function pow() is required and is supported neither by the C runtime nor libm.so.") +endif() + set(app_utils_ALL_SOURCES ${app_utils_SOURCES} ${app_utils_HEADERS} ${app_utils_noinst_HEADERS}) set(app_utils_ALL_LIBRARIES gnc-engine @@ -78,7 +86,10 @@ set(app_utils_ALL_LIBRARIES gnucash-guile ${GIO_LDFLAGS} ${LIBXML2_LDFLAGS} - ${LIBXSLT_LDFLAGS}) + ${LIBXSLT_LDFLAGS} + ${STANDARD_MATH_LIBRARY} +) + set(app_utils_ALL_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/calculation ${GIO_INCLUDE_DIRS}