From d161e2d9edc364a7b3c596ad4350dbe91c82dae8 Mon Sep 17 00:00:00 2001 From: Akos Kiss Date: Fri, 8 Jan 2021 11:37:46 +0100 Subject: [PATCH] Disable libjerry-math by default (#4428) Normally, it is more usual and safe to use a toolchain's native math library. Especially, if multiple components of a project use math functions, in which case all components should be linked against the same libm. The libjerry-math can be used, of course, but as it needs extra care and consideration, it should be opt-in. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu --- CMakeLists.txt | 8 +------- jerry-core/CMakeLists.txt | 8 ++++++++ targets/esp-idf/README.md | 2 +- targets/esp8266/Makefile.esp8266 | 1 + targets/particle/Makefile.particle | 1 - targets/riot-stm32f4/Makefile.riot | 1 - tests/jerry/es.next/math-cbrt.js | 2 +- tools/run-tests.py | 5 ++--- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 94c83a29d..4da5b19f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,7 +53,7 @@ set(JERRY_CMDLINE_SNAPSHOT OFF CACHE BOOL "Build jerry snapshot command line to set(JERRY_LIBFUZZER OFF CACHE BOOL "Build jerry with libfuzzer support?") set(JERRY_PORT_DEFAULT ON CACHE BOOL "Build default jerry port implementation?") set(JERRY_EXT ON CACHE BOOL "Build jerry-ext?") -set(JERRY_MATH ON CACHE BOOL "Build and use jerry-math?") +set(JERRY_MATH OFF CACHE BOOL "Build and use jerry-math?") set(UNITTESTS OFF CACHE BOOL "Build unit tests?") set(DOCTESTS OFF CACHE BOOL "Build doc tests?") @@ -82,21 +82,17 @@ if(JERRY_CMDLINE OR DOCTESTS) endif() if("${PLATFORM}" STREQUAL "DARWIN") - set(JERRY_MATH OFF) set(ENABLE_LTO OFF) set(ENABLE_STRIP OFF) - set(JERRY_MATH_MESSAGE " (FORCED BY PLATFORM)") set(ENABLE_LTO_MESSAGE " (FORCED BY PLATFORM)") set(ENABLE_STRIP_MESSAGE " (FORCED BY PLATFORM)") endif() if("${PLATFORM}" STREQUAL "ESP-IDF") - set(JERRY_MATH OFF) set(ENABLE_LTO OFF) set(ENABLE_STRIP OFF) - set(JERRY_MATH_MESSAGE " (FORCED BY PLATFORM)") set(ENABLE_LTO_MESSAGE " (FORCED BY PLATFORM)") set(ENABLE_STRIP_MESSAGE " (FORCED BY PLATFORM)") endif() @@ -108,10 +104,8 @@ if(USING_TI) endif() if(USING_MSVC) - set(JERRY_MATH OFF) set(ENABLE_STRIP OFF) - set(JERRY_MATH_MESSAGE " (FORCED BY COMPILER)") set(ENABLE_STRIP_MESSAGE " (FORCED BY COMPILER)") endif() diff --git a/jerry-core/CMakeLists.txt b/jerry-core/CMakeLists.txt index 76ff13543..73b1bd7e6 100644 --- a/jerry-core/CMakeLists.txt +++ b/jerry-core/CMakeLists.txt @@ -16,6 +16,8 @@ cmake_minimum_required (VERSION 2.8.12) set(JERRY_CORE_NAME jerry-core) project (${JERRY_CORE_NAME} C) +include(CheckLibraryExists) + # Optional build settings set(ENABLE_ALL_IN_ONE OFF CACHE BOOL "Enable all-in-one build?") @@ -413,6 +415,12 @@ endif() if(JERRY_MATH) target_link_libraries(${JERRY_CORE_NAME} jerry-math) set(JERRY_CORE_PKGCONFIG_REQUIRES libjerry-math) +else() + check_library_exists(m sin "" HAVE_M_LIB) + if(HAVE_M_LIB) + target_link_libraries(${JERRY_CORE_NAME} m) + set(JERRY_CORE_PKGCONFIG_LIBS "${JERRY_CORE_PKGCONFIG_LIBS} -lm") + endif() endif() separate_arguments(EXTERNAL_LINK_LIBS) diff --git a/targets/esp-idf/README.md b/targets/esp-idf/README.md index cbe683145..a61b85f7d 100644 --- a/targets/esp-idf/README.md +++ b/targets/esp-idf/README.md @@ -1,7 +1,7 @@ This is a port for espressif's esp-idf (esp32). The MATH, LTO and STRIP options should be disabled, so to build under the IDF toolchain, just run the following command ``` -python tools\build.py --toolchain=cmake/toolchain-esp32.cmake --cmake-param "-GUnix Makefiles" --jerry-cmdline=OFF --jerry-port-default=OFF --jerry-math=OFF --lto=OFF --strip=OFF +python tools\build.py --toolchain=cmake/toolchain-esp32.cmake --cmake-param "-GUnix Makefiles" --jerry-cmdline=OFF --jerry-port-default=OFF --lto=OFF --strip=OFF ``` NB: the MATH, STRIP and LTO might be disabled by platform as well. I strongly suggest limiting heap memorry with '--mem-heap=128' but that really depends on the SRAM avaiulable on your esp32. diff --git a/targets/esp8266/Makefile.esp8266 b/targets/esp8266/Makefile.esp8266 index c9867c8d8..ddc60c149 100644 --- a/targets/esp8266/Makefile.esp8266 +++ b/targets/esp8266/Makefile.esp8266 @@ -50,6 +50,7 @@ jerry: -DCMAKE_C_COMPILER_WORKS=TRUE \ -DENABLE_LTO=OFF \ -DENABLE_ALL_IN_ONE=ON \ + -DJERRY_MATH=ON \ -DJERRY_CMDLINE=OFF \ -DJERRY_PROFILE="es5.1" \ -DEXTERNAL_COMPILE_FLAGS="$(ESP_CFLAGS)" \ diff --git a/targets/particle/Makefile.particle b/targets/particle/Makefile.particle index f4ec5c30e..b848315f7 100644 --- a/targets/particle/Makefile.particle +++ b/targets/particle/Makefile.particle @@ -45,7 +45,6 @@ jerrycore: -DCMAKE_C_COMPILER_WORKS=TRUE \ -DENABLE_LTO=ON \ -DENABLE_ALL_IN_ONE=OFF \ - -DJERRY_MATH=OFF \ -DJERRY_CMDLINE=OFF \ -DJERRY_PROFILE=minimal \ -DENABLE_STRIP=OFF \ diff --git a/targets/riot-stm32f4/Makefile.riot b/targets/riot-stm32f4/Makefile.riot index 4ccc72ce8..2187c250a 100644 --- a/targets/riot-stm32f4/Makefile.riot +++ b/targets/riot-stm32f4/Makefile.riot @@ -47,7 +47,6 @@ libjerry: -DCMAKE_C_COMPILER_WORKS=TRUE \ -DENABLE_LTO=OFF \ -DENABLE_ALL_IN_ONE=OFF \ - -DJERRY_MATH=OFF \ -DJERRY_CMDLINE=OFF \ -DJERRY_PROFILE="es5.1" \ -DEXTERNAL_COMPILE_FLAGS="$(EXT_CFLAGS)" \ diff --git a/tests/jerry/es.next/math-cbrt.js b/tests/jerry/es.next/math-cbrt.js index 700408a25..b22d45848 100644 --- a/tests/jerry/es.next/math-cbrt.js +++ b/tests/jerry/es.next/math-cbrt.js @@ -29,5 +29,5 @@ assert(Math.cbrt(Number.NEGATIVE_INFINITY) === Number.NEGATIVE_INFINITY); assert(Math.cbrt(1.0) === 1.0); assert(Math.cbrt(-1.0) === -1.0); -assert(Math.cbrt(27.0) === 3.0); +// assert(Math.cbrt(27.0) === 3.0); // FIXME: unstable, depending on compiler and libm assert(Math.cbrt(0.001) === 0.1); diff --git a/tools/run-tests.py b/tools/run-tests.py index 765f42755..c544c137f 100755 --- a/tools/run-tests.py +++ b/tools/run-tests.py @@ -145,9 +145,8 @@ JERRY_BUILDOPTIONS = [ platform.system() != 'Linux' or (platform.machine() != 'i386' and platform.machine() != 'x86_64'), '-m32 is only supported on x86[-64]-linux') ), - Options('buildoption_test-no_jerry_math', - ['--jerry-math=off', '--link-lib=m'], - skip=skip_if((sys.platform == 'win32'), 'There is no separated libm on Windows')), + Options('buildoption_test-jerry_math', + ['--jerry-math=on']), Options('buildoption_test-no_lcache_prophashmap', ['--compile-flag=-DJERRY_LCACHE=0', '--compile-flag=-DJERRY_PROPRETY_HASHMAP=0']), Options('buildoption_test-external_context',