From 000f4c7b171c62f851e50ae99a4bd793cd04660c Mon Sep 17 00:00:00 2001 From: Jordan08 Date: Sat, 19 Sep 2026 13:37:35 +0200 Subject: [PATCH 1/6] Make Approx relative for large values, handle NaN and inf --- src/core/tools/codac2_Approx.h | 20 ++++++++++++++++++- .../tube/codac2_tests_SlicedTube_integral.cpp | 4 ++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/core/tools/codac2_Approx.h b/src/core/tools/codac2_Approx.h index 721da8249..ef0583af5 100644 --- a/src/core/tools/codac2_Approx.h +++ b/src/core/tools/codac2_Approx.h @@ -40,7 +40,25 @@ namespace codac2 friend bool operator==(const T& x1, const Approx& x2) { if constexpr(std::is_same_v) - return std::fabs(x1-x2._x) < x2._eps; + { + if(std::isnan(x1) && std::isnan(x2._x)) + return true; + + else if(x2._x == std::numeric_limits::max()) + return x1 == std::numeric_limits::max(); + + else if(x2._x == -std::numeric_limits::max()) + return x1 == -std::numeric_limits::max(); + + else if(std::isinf(x2._x)) + return x1 == x2._x; + + else if(x2._x < 1.0 && x2._x > -1.0) + return std::fabs(x1-x2._x) < x2._eps; // absolute error + + else + return std::fabs(x1-x2._x) < x2._eps*std::max(std::fabs(x1),std::fabs(x2._x)); // relative error + } else if(x1.size() != x2._x.size()) return false; diff --git a/tests/core/domains/tube/codac2_tests_SlicedTube_integral.cpp b/tests/core/domains/tube/codac2_tests_SlicedTube_integral.cpp index 4cba879ab..c5bd5294b 100644 --- a/tests/core/domains/tube/codac2_tests_SlicedTube_integral.cpp +++ b/tests/core/domains/tube/codac2_tests_SlicedTube_integral.cpp @@ -151,8 +151,8 @@ TEST_CASE("Computing integration from 0, interval argument") CHECK(Approx(x.integral(Interval(12.5))) == Interval(6.5,20.5)); CHECK(Approx(x.integral(Interval(14.5))) == Interval(7,23.5)); auto p_intv = x.partial_integral(Interval(12.5,14.5)); - CHECK(p_intv.first == Interval(6.,7.)); - CHECK(p_intv.second == Interval(20.5,23.5)); + CHECK(Approx(p_intv.first) == Interval(6.,7.)); + CHECK(Approx(p_intv.second) == Interval(20.5,23.5)); CHECK(Approx(x.integral(Interval(12.5,14.5))) == Interval(6.0,23.5)); CHECK(Approx(x.integral(Interval(0))) == Interval(0)); CHECK(Approx(x.integral(Interval(10.2))) == Interval(9.3,19.7)); From fad24346670f8995b77cc363bacf36f73296cdae Mon Sep 17 00:00:00 2001 From: Jordan08 Date: Sat, 19 Sep 2026 13:37:35 +0200 Subject: [PATCH 2/6] Build Codac on GAOL v5 by default, keeping IBEX and its guards behind WITH_IBEX --- CMakeLists.txt | 80 +- .../tuto/cp_robotics/src/CMakeLists.txt | 12 +- examples/00_graphics/CMakeLists.txt | 4 - examples/01_batman/CMakeLists.txt | 4 - examples/02_centered_form/CMakeLists.txt | 4 - examples/03_sivia/CMakeLists.txt | 4 - examples/04_explored_area/CMakeLists.txt | 4 - examples/05_capd_solver/CMakeLists.txt | 11 - examples/06_graphics_3D/CMakeLists.txt | 4 - examples/07_centered_2D/CMakeLists.txt | 4 - examples/08_centered_3D/CMakeLists.txt | 4 - examples/09_robot_simu/CMakeLists.txt | 4 - examples/10_lie_groups/CMakeLists.txt | 4 - examples/11_peibos/CMakeLists.txt | 4 - examples/12_peibos_capd/CMakeLists.txt | 5 - examples/13_qinter/CMakeLists.txt | 4 - examples/14_lohner/CMakeLists.txt | 4 - examples/15_sympy/CMakeLists.txt | 5 - examples/16_visibility/CMakeLists.txt | 4 - examples/ellipsoid_example/CMakeLists.txt | 12 +- python/src/core/CMakeLists.txt | 2 +- python/src/graphics/CMakeLists.txt | 2 +- python/src/unsupported/CMakeLists.txt | 2 +- scripts/CMakeModules/FindGAOL.cmake | 75 ++ scripts/CMakeModules/codac_gaol.cmake | 553 +++++++++ src/CMakeLists.txt | 55 +- src/core/CMakeLists.txt | 2 +- src/core/domains/interval/codac2_Interval.h | 163 +++ .../domains/interval/codac2_Interval_impl.h | 1055 ++++++++++++----- .../codac2_Interval_operations_impl.h | 174 ++- src/core/operators/codac2_atan2.h | 19 + src/core/operators/codac2_ceil.h | 14 + src/core/operators/codac2_floor.h | 13 + src/core/operators/codac2_max.h | 16 + src/core/operators/codac2_min.h | 13 + src/core/operators/codac2_pow.h | 56 + src/core/operators/codac2_sign.h | 16 + src/core/operators/codac2_sinh.h | 8 + src/extensions/capd/CMakeLists.txt | 2 +- src/extensions/sympy/CMakeLists.txt | 2 +- src/graphics/CMakeLists.txt | 2 +- tests/CMakeLists.txt | 22 +- .../codac2_tests_AnalyticFunction.cpp | 10 + .../analytic/codac2_tests_AnalyticFunction.py | 16 +- .../core/operators/codac2_tests_operators.cpp | 80 ++ tests/test_codac/CMakeLists.txt | 13 +- 46 files changed, 2114 insertions(+), 452 deletions(-) create mode 100644 scripts/CMakeModules/FindGAOL.cmake create mode 100644 scripts/CMakeModules/codac_gaol.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 89aa66f1d..865a34137 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,15 @@ #set(CMAKE_C_COMPILER "gcc-7") #set(CMAKE_CXX_COMPILER "/usr/bin/g++-7") - project(codac VERSION ${VERSION} LANGUAGES CXX) + # C as well as C++: the codac sources are all C++, but GAOL v5 compiles the + # elementary functions of CORE-MATH, which are C, into libgaol, and Codac + # builds GAOL as a part of itself when no GAOL v5 is installed (FetchContent, + # see scripts/CMakeModules/codac_gaol.cmake). CMake wants a language enabled + # in the highest directory common to the targets that use it, directly or + # through what they link (see enable_language()): the Codac libraries link + # libgaol, so C is enabled here rather than by the project() of GAOL, in its + # subdirectory. + project(codac VERSION ${VERSION} LANGUAGES C CXX) if(NOT VERSION_ID) set(PROJECT_VERSION_FULL ${PROJECT_VERSION}) @@ -119,12 +127,74 @@ ################################################################################ -# Looking for IBEX +# Looking for GAOL ################################################################################ - find_package(IBEX REQUIRED) - ibex_init_common() # IBEX should have installed this function - message(STATUS "Found IBEX version ${IBEX_VERSION}") + # GAOL is the interval arithmetic library codac2::Interval is built upon. + # Codac used to reach it through IBEX, which bundles a GAOL 4 (and mathlib, + # with which GAOL 4 computes its elementary functions), and that was all Codac + # needed IBEX for. It is now built on GAOL v5, Jordan Ninin's continuation of + # GAOL (https://github.com/Jordan08/GAOL), which Codac finds or builds itself + # (scripts/CMakeModules/codac_gaol.cmake, which also says what comes from IBEX + # in doing so and who wrote it, and FindGAOL.cmake next to it), and which + # Codac calls without the guards GAOL 4 needs (see + # src/core/domains/interval/codac2_Interval.h). + include(codac_gaol) + + # WITH_IBEX, a developer option, gives back the build Codac had before GAOL + # v5: IBEX, found by find_package(IBEX) and set up by ibex_init_common(), the + # GAOL 4 it bundles, and Codac's guards around GAOL, which codac2_Interval.h + # selects from the version of the GAOL headers it includes. It is there to + # tell whether a wrong result comes from GAOL v5: a program built both ways + # should compute the same intervals. Nothing of IBEX is looked for, built or + # installed without it. + option(WITH_IBEX "Build Codac on IBEX and its GAOL 4, with Codac's guards around GAOL, as before GAOL v5 (developer option)" OFF) + + if(WITH_IBEX) + + # As before GAOL v5. CMAKE_CXX_FLAGS is kept as it is before + # ibex_init_common(), for what it appends to them to be handed over to the + # users of the installed Codac (codac_gaol_from_ibex()). + set(_codac_cxx_flags_before_ibex "${CMAKE_CXX_FLAGS}") + find_package(IBEX REQUIRED) + ibex_init_common() # IBEX should have installed this function + message(STATUS "Found IBEX version ${IBEX_VERSION}") + codac_gaol_from_ibex("${_codac_cxx_flags_before_ibex}") + set(CODAC_PORTABILITY_CXX_FLAGS "") + + else() + + # GAOL v5, found or downloaded and built, as the imported target Codac::gaol: + # its include directories, its library, and the flags floating-point + # rounding depends on, which ibex_init_common() used to put on the command + # line -- without -frounding-math and its companions, the compiler is free + # to constant-fold in the wrong rounding mode, and an interval is no longer + # a bound -- with those of the instructions libgaol is compiled with + # (-mfma). They are GAOL's to say: codac_gaol_find() takes them from the + # CMake package of GAOL, from its gaol.pc, or from the GAOL it builds. The + # Codac libraries link Codac::gaol PUBLIC, so that every target linking them + # is compiled with those flags -- the tests, the examples and the Python + # bindings included. ENABLE_FIND_PACKAGE_GAOL OFF downloads GAOL even when + # one is installed. + option(ENABLE_FIND_PACKAGE_GAOL "Look for an installed GAOL v5 before downloading it" ON) + codac_gaol_find() + + # The flags of ibex_init_common() that are not those of interval + # arithmetic, where ibex_init_common() put them, in CMAKE_CXX_FLAGS, so that + # every target is compiled with them; handed over to consumers with the + # others. After codac_gaol_find(): a GAOL built with Codac is not compiled + # with them. + codac_gaol_portability_flags(CODAC_PORTABILITY_CXX_FLAGS) + string(REPLACE ";" " " _codac_portability_cxx_flags "${CODAC_PORTABILITY_CXX_FLAGS}") + string(APPEND CMAKE_CXX_FLAGS " ${_codac_portability_cxx_flags}") + + endif() + + # Consumers need those flags as much as this build does: they are handed over + # through CODAC_CXX_FLAGS in codac-config.cmake, and through codac.pc (see + # src/CMakeLists.txt). + string(REPLACE ";" " " _codac_interval_cxx_flags "${CODAC_INTERVAL_CXX_FLAGS}") + message(STATUS "Interval arithmetic flags of Codac::gaol, for the users of Codac: ${_codac_interval_cxx_flags}") ################################################################################ diff --git a/doc/manual/tuto/cp_robotics/src/CMakeLists.txt b/doc/manual/tuto/cp_robotics/src/CMakeLists.txt index 89e271fe7..d749449f4 100644 --- a/doc/manual/tuto/cp_robotics/src/CMakeLists.txt +++ b/doc/manual/tuto/cp_robotics/src/CMakeLists.txt @@ -8,16 +8,6 @@ set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) -# Adding IBEX - - # In case you installed IBEX in a local directory, you need - # to specify its path with the CMAKE_PREFIX_PATH option. - # set(CMAKE_PREFIX_PATH "~/ibex-lib/build_install") - - find_package(IBEX REQUIRED) - ibex_init_common() # IBEX should have installed this function - message(STATUS "Found IBEX version ${IBEX_VERSION}") - # Adding Codac # In case you installed Codac in a local directory, you need @@ -38,4 +28,4 @@ ) target_compile_options(${PROJECT_NAME} PUBLIC ${CODAC_CXX_FLAGS}) target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC ${CODAC_INCLUDE_DIRS}) - target_link_libraries(${PROJECT_NAME} PUBLIC ${CODAC_LIBRARIES} Ibex::ibex) \ No newline at end of file + target_link_libraries(${PROJECT_NAME} PUBLIC ${CODAC_LIBRARIES}) \ No newline at end of file diff --git a/examples/00_graphics/CMakeLists.txt b/examples/00_graphics/CMakeLists.txt index 61711b793..f67374ceb 100644 --- a/examples/00_graphics/CMakeLists.txt +++ b/examples/00_graphics/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) diff --git a/examples/01_batman/CMakeLists.txt b/examples/01_batman/CMakeLists.txt index 4efd11f57..cc24aecb4 100644 --- a/examples/01_batman/CMakeLists.txt +++ b/examples/01_batman/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) diff --git a/examples/02_centered_form/CMakeLists.txt b/examples/02_centered_form/CMakeLists.txt index 4efd11f57..cc24aecb4 100644 --- a/examples/02_centered_form/CMakeLists.txt +++ b/examples/02_centered_form/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) diff --git a/examples/03_sivia/CMakeLists.txt b/examples/03_sivia/CMakeLists.txt index 4efd11f57..cc24aecb4 100644 --- a/examples/03_sivia/CMakeLists.txt +++ b/examples/03_sivia/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) diff --git a/examples/04_explored_area/CMakeLists.txt b/examples/04_explored_area/CMakeLists.txt index be5ba69d7..081978ee8 100644 --- a/examples/04_explored_area/CMakeLists.txt +++ b/examples/04_explored_area/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) diff --git a/examples/05_capd_solver/CMakeLists.txt b/examples/05_capd_solver/CMakeLists.txt index f938d5b82..a96e2d9ff 100644 --- a/examples/05_capd_solver/CMakeLists.txt +++ b/examples/05_capd_solver/CMakeLists.txt @@ -8,16 +8,6 @@ project(codac_example LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) -# Adding IBEX - -# In case you installed IBEX in a local directory, you need -# to specify its path with the CMAKE_PREFIX_PATH option. -# set(CMAKE_PREFIX_PATH "~/ibex-lib/build_install") - -find_package(IBEX REQUIRED) -ibex_init_common() # IBEX should have installed this function -message(STATUS "Found IBEX version ${IBEX_VERSION}") - # Adding Codac # In case you installed Codac in a local directory, you need @@ -41,5 +31,4 @@ target_link_libraries(${PROJECT_NAME} PRIVATE ${CODAC_LIBRARIES} ${CODAC_CAPD_LIBRARY} capd::capd - Ibex::ibex ) \ No newline at end of file diff --git a/examples/06_graphics_3D/CMakeLists.txt b/examples/06_graphics_3D/CMakeLists.txt index 4efd11f57..cc24aecb4 100644 --- a/examples/06_graphics_3D/CMakeLists.txt +++ b/examples/06_graphics_3D/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) diff --git a/examples/07_centered_2D/CMakeLists.txt b/examples/07_centered_2D/CMakeLists.txt index 4efd11f57..cc24aecb4 100644 --- a/examples/07_centered_2D/CMakeLists.txt +++ b/examples/07_centered_2D/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) diff --git a/examples/08_centered_3D/CMakeLists.txt b/examples/08_centered_3D/CMakeLists.txt index 4efd11f57..cc24aecb4 100644 --- a/examples/08_centered_3D/CMakeLists.txt +++ b/examples/08_centered_3D/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) diff --git a/examples/09_robot_simu/CMakeLists.txt b/examples/09_robot_simu/CMakeLists.txt index 4efd11f57..cc24aecb4 100644 --- a/examples/09_robot_simu/CMakeLists.txt +++ b/examples/09_robot_simu/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) diff --git a/examples/10_lie_groups/CMakeLists.txt b/examples/10_lie_groups/CMakeLists.txt index 04a9a0f15..027a69fb1 100644 --- a/examples/10_lie_groups/CMakeLists.txt +++ b/examples/10_lie_groups/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) diff --git a/examples/11_peibos/CMakeLists.txt b/examples/11_peibos/CMakeLists.txt index 4efd11f57..cc24aecb4 100644 --- a/examples/11_peibos/CMakeLists.txt +++ b/examples/11_peibos/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) diff --git a/examples/12_peibos_capd/CMakeLists.txt b/examples/12_peibos_capd/CMakeLists.txt index 7f84d43f5..b6a33d97d 100644 --- a/examples/12_peibos_capd/CMakeLists.txt +++ b/examples/12_peibos_capd/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) @@ -35,5 +31,4 @@ ${CODAC_LIBRARIES} ${CODAC_CAPD_LIBRARY} capd::capd - Ibex::ibex ) \ No newline at end of file diff --git a/examples/13_qinter/CMakeLists.txt b/examples/13_qinter/CMakeLists.txt index 4efd11f57..cc24aecb4 100644 --- a/examples/13_qinter/CMakeLists.txt +++ b/examples/13_qinter/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) diff --git a/examples/14_lohner/CMakeLists.txt b/examples/14_lohner/CMakeLists.txt index 4efd11f57..cc24aecb4 100644 --- a/examples/14_lohner/CMakeLists.txt +++ b/examples/14_lohner/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) diff --git a/examples/15_sympy/CMakeLists.txt b/examples/15_sympy/CMakeLists.txt index b3b7b9000..956d0857e 100644 --- a/examples/15_sympy/CMakeLists.txt +++ b/examples/15_sympy/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) @@ -37,5 +33,4 @@ ${CODAC_LIBRARIES} ${CODAC_SYMPY_LIBRARY} # linking to the codac-sympy extension pybind11::embed # linking to pybind11 - Ibex::ibex ) \ No newline at end of file diff --git a/examples/16_visibility/CMakeLists.txt b/examples/16_visibility/CMakeLists.txt index 4efd11f57..cc24aecb4 100644 --- a/examples/16_visibility/CMakeLists.txt +++ b/examples/16_visibility/CMakeLists.txt @@ -17,10 +17,6 @@ find_package(CODAC REQUIRED) message(STATUS "Found Codac version ${CODAC_VERSION}") -# Initializating Ibex - - ibex_init_common() - # Compilation if(FAST_RELEASE) diff --git a/examples/ellipsoid_example/CMakeLists.txt b/examples/ellipsoid_example/CMakeLists.txt index 51fbc750e..2adc29fa7 100644 --- a/examples/ellipsoid_example/CMakeLists.txt +++ b/examples/ellipsoid_example/CMakeLists.txt @@ -8,16 +8,6 @@ set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) -# Adding IBEX - - # In case you installed IBEX in a local directory, you need - # to specify its path with the CMAKE_PREFIX_PATH option. - # set(CMAKE_PREFIX_PATH "~/ibex-lib/build_install") - - find_package(IBEX REQUIRED) - ibex_init_common() # IBEX should have installed this function - message(STATUS "Found IBEX version ${IBEX_VERSION}") - # Adding Eigen3 # In case you installed Eigen3 in a local directory, you need @@ -41,4 +31,4 @@ add_executable(${PROJECT_NAME} main.cpp) target_compile_options(${PROJECT_NAME} PUBLIC ${CODAC_CXX_FLAGS}) target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC ${CODAC_INCLUDE_DIRS}) - target_link_libraries(${PROJECT_NAME} PUBLIC ${CODAC_LIBRARIES} Ibex::ibex Eigen3::Eigen) + target_link_libraries(${PROJECT_NAME} PUBLIC ${CODAC_LIBRARIES} Eigen3::Eigen) diff --git a/python/src/core/CMakeLists.txt b/python/src/core/CMakeLists.txt index 6adb86409..ecbba91cc 100644 --- a/python/src/core/CMakeLists.txt +++ b/python/src/core/CMakeLists.txt @@ -160,7 +160,7 @@ ) target_link_libraries(_core - PRIVATE ${PROJECT_NAME}-core ${PROJECT_NAME}-sympy ${LIBS} Ibex::ibex + PRIVATE ${PROJECT_NAME}-core ${PROJECT_NAME}-sympy ${LIBS} Codac::gaol ) # Copy the generated library in the package folder diff --git a/python/src/graphics/CMakeLists.txt b/python/src/graphics/CMakeLists.txt index 7b180a814..0b4386d75 100644 --- a/python/src/graphics/CMakeLists.txt +++ b/python/src/graphics/CMakeLists.txt @@ -28,7 +28,7 @@ ) target_link_libraries(_graphics - PRIVATE ${PROJECT_NAME}-graphics ${LIBS} Ibex::ibex + PRIVATE ${PROJECT_NAME}-graphics ${LIBS} Codac::gaol ) # Copy the generated library in the package folder diff --git a/python/src/unsupported/CMakeLists.txt b/python/src/unsupported/CMakeLists.txt index a413804dd..861175754 100644 --- a/python/src/unsupported/CMakeLists.txt +++ b/python/src/unsupported/CMakeLists.txt @@ -16,7 +16,7 @@ ) target_link_libraries(_unsupported - PRIVATE ${PROJECT_NAME}-unsupported ${LIBS} Ibex::ibex + PRIVATE ${PROJECT_NAME}-unsupported ${LIBS} Codac::gaol ) # Copy the generated library in the package folder diff --git a/scripts/CMakeModules/FindGAOL.cmake b/scripts/CMakeModules/FindGAOL.cmake new file mode 100644 index 000000000..68c90e1d9 --- /dev/null +++ b/scripts/CMakeModules/FindGAOL.cmake @@ -0,0 +1,75 @@ +# ================================================================== +# Codac - cmake module looking for an installed GAOL v5 with pkg-config +# ================================================================== +# +# Looks for GAOL v5 (https://github.com/Jordan08/GAOL), the interval arithmetic +# library Codac is built upon, by its gaol.pc: the second way of +# codac_gaol_find() (scripts/CMakeModules/codac_gaol.cmake), for a GAOL v5 +# installed without its CMake package, which codac_gaol_find() looks for first. +# The three builds of GAOL v5 (autotools, meson and CMake) install a gaol.pc +# whose Cflags carry the flags of interval arithmetic and of the instructions +# libgaol was compiled with, and whose Libs carry libgaol, into which the +# elementary functions of CORE-MATH are compiled. That GAOL is used with those +# flags, as its gaol.pc gives them. +# +# Where to look, when gaol.pc is not in a directory pkg-config searches by +# default: PKG_CONFIG_PATH, or CMAKE_PREFIX_PATH, which FindPkgConfig adds to +# it. +# +# A gaol.pc older than CODAC_GAOL_MIN_VERSION (codac_gaol.cmake: GAOL 4, which +# Codac would have to call with the guards it no longer has, GAOL 5.0.0, whose +# power Codac no longer reverses rightly, or GAOL 5.0.1, whose headers GCC 9.3 +# and 10.2 for 64-bit ARM cannot compile) is not used, and neither is a +# gaol.pc whose Cflags lack -frounding-math with a compiler that takes it, as +# the one the meson build of Frederic Goualard's GAOL 4 installs: it does not +# describe the flags GAOL needs. A message says why. Not with Visual C++, whose +# libraries pkg-config does not name. +# +# Defines: +# +# GAOL_FOUND +# GAOL_VERSION the version gaol.pc states +# GAOL_PC_PREFIX the prefix of gaol.pc +# PkgConfig::CODAC_GAOL_PC the imported target pkg_check_modules() makes +# of gaol.pc: include directories, flags (its +# INTERFACE_COMPILE_OPTIONS), library files and +# link options +# +# codac_gaol_find() makes Codac::gaol link PkgConfig::CODAC_GAOL_PC. + +set(GAOL_VERSION "") +set(GAOL_PC_PREFIX "") + +if(NOT MSVC) + find_package(PkgConfig QUIET) +endif() + +if(PKG_CONFIG_FOUND) + + pkg_check_modules(CODAC_GAOL_PC QUIET IMPORTED_TARGET "gaol>=${CODAC_GAOL_MIN_VERSION}") + + if(CODAC_GAOL_PC_FOUND) + include(CheckCXXCompilerFlag) + check_cxx_compiler_flag(-frounding-math COMPILER_SUPPORTS_FROUNDING_MATH) + if(COMPILER_SUPPORTS_FROUNDING_MATH AND NOT "-frounding-math" IN_LIST CODAC_GAOL_PC_CFLAGS_OTHER) + message(STATUS "Found gaol.pc ${CODAC_GAOL_PC_VERSION} in ${CODAC_GAOL_PC_PREFIX}, whose Cflags lack " + "-frounding-math: not used") + else() + set(GAOL_VERSION "${CODAC_GAOL_PC_VERSION}") + set(GAOL_PC_PREFIX "${CODAC_GAOL_PC_PREFIX}") + endif() + + else() + # Any gaol.pc, to say why it is not used + pkg_check_modules(CODAC_GAOL_PC_ANY QUIET gaol) + if(CODAC_GAOL_PC_ANY_FOUND) + message(STATUS "Found gaol.pc ${CODAC_GAOL_PC_ANY_VERSION} in ${CODAC_GAOL_PC_ANY_PREFIX}, " + "older than ${CODAC_GAOL_MIN_VERSION}: not used") + endif() + endif() +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(GAOL + REQUIRED_VARS GAOL_PC_PREFIX GAOL_VERSION + VERSION_VAR GAOL_VERSION) diff --git a/scripts/CMakeModules/codac_gaol.cmake b/scripts/CMakeModules/codac_gaol.cmake new file mode 100644 index 000000000..2bdab7517 --- /dev/null +++ b/scripts/CMakeModules/codac_gaol.cmake @@ -0,0 +1,553 @@ +# ================================================================== +# Codac - cmake module for GAOL, the interval arithmetic library +# ================================================================== +# +# codac2::Interval derives from gaol::interval (see +# src/core/domains/interval/codac2_Interval.h). GAOL is written by Frederic +# Goualard (https://frederic.goualard.net, https://github.com/goualard-f/GAOL) +# and distributed under the GNU LGPL v2. +# +# Codac is built on GAOL v5, the continuation of GAOL by Jordan Ninin, +# https://github.com/Jordan08/GAOL: a CMake build, the fixes Codac used to work +# around (Codac now calls GAOL without guards, see codac2_Interval.h), the +# elementary functions of CORE-MATH, correctly rounded, compiled into libgaol in +# the place of mathlib (libultim), and tests of its bounds. Fixes of GAOL go into +# that repository rather than into Codac. +# +# GAOL's interval operations are inline: the code including its headers, Codac's +# and that of Codac's users, has to be compiled with the flags of interval +# arithmetic (-frounding-math and its companions: without them, the compiler is +# free to evaluate a floating-point expression at compile time in the rounding +# mode to nearest, or to contract it, and a bound computed that way no longer +# encloses anything), and with the flags of the instructions libgaol was compiled +# with (-mfma). Which flags is GAOL's to say, and codac_gaol_find() takes them +# from the GAOL it finds or builds, in this order: +# +# 1. the CMake package of GAOL v5, find_package(gaol ${CODAC_GAOL_MIN_VERSION} +# CONFIG): gaol::gaol carries the include directory and the flags (PUBLIC); +# 2. pkg-config, for a GAOL v5 installed without its CMake package (by its +# autotools or meson build): the Cflags of its gaol.pc carry the flags, its +# Libs libgaol (FindGAOL.cmake); +# 3. when neither is found, or with ENABLE_FIND_PACKAGE_GAOL OFF, GAOL is +# downloaded from the master branch of https://github.com/Jordan08/GAOL and +# built as a part of Codac (FetchContent), its version checked to be +# CODAC_GAOL_MIN_VERSION or later, and installed with Codac +# (codac_gaol_fetch()). +# +# A GAOL older than CODAC_GAOL_MIN_VERSION found in 1 or 2 is passed over, with +# a message saying so, and GAOL v5 is downloaded in 3: GAOL 4, which still needs +# the guards, GAOL 5.0.0, whose power Codac no longer reverses rightly, and GAOL +# 5.0.1, whose headers GCC 9.3 and 10.2 for 64-bit ARM cannot compile (see +# CODAC_GAOL_MIN_VERSION below). +# +# All three end in Codac::gaol, the imported target the Codac libraries link +# PUBLIC, and which codac-config.cmake defines again for their users +# (codac_gaol_config_snippet()). +# +# With the developer option WITH_IBEX (top-level CMakeLists.txt), none of the +# above: Codac is built as it was before GAOL v5, on IBEX and the GAOL 4 that +# IBEX bundles, with the guards. Codac::gaol then names Ibex::ibex +# (codac_gaol_from_ibex()). +# +# Origin +# ------ +# What follows comes from IBEX (https://github.com/ibex-team/ibex-lib, GNU +# LGPL v3): +# +# - codac_gaol_portability_flags() is the part of ibex_init_common() +# (cmake.utils/ibex-config-utils.cmake) that sets the floating-point and +# portability flags which are not those of interval arithmetic, in the version +# of the IBEX fork maintained by Fabrice Le Bars, +# https://github.com/lebarsfa/ibex-lib, tag ibex-2.8.9.20260819, which Codac +# was built against until now. ibex_init_common() was written by Cyril +# Bouvier for the CMake build of IBEX; the IEEE 754 flags it sets were added +# to it by Fabrice Le Bars. +# +# - codac_gaol_config_snippet() does, for Codac, what +# create_target_import_and_export() of cmake.utils/IbexUtils.cmake (Cyril +# Bouvier) does for IBEX: the lines that define the imported target of GAOL +# again for a consumer of the installed library. +# +# What differs from IBEX +# ---------------------- +# IBEX extracts GAOL and mathlib from archives kept in its own repository, and +# builds them as a part of itself (add_subdirectory()), installing them in +# include/ibex/3rd and lib/ibex/3rd. Here GAOL is found on the machine, or +# downloaded from its repository and built as a part of Codac by FetchContent, +# with the CMake build of GAOL v5, which supports it, and installed in the +# standard directories (include/gaol, lib, lib/cmake/gaol), where a later build +# of Codac, or of another project, finds it with find_package(gaol). The other +# differences are explained where they occur. + + +################################################################################ +# CODAC_GAOL_MIN_VERSION +################################################################################ +# +# The oldest GAOL Codac is built on without guards, asked of the GAOL found +# (find_package(), gaol.pc), of the GAOL downloaded, and again by +# codac-config.cmake and codac.pc for the users of the installed Codac. +# +# 5.0.2, for two reasons: +# - not 5.0.0: PowOp::bwd() (src/core/operators/codac2_pow.h) reverses x1^x2 +# with pow_rel() and pow_exponent_rel() of GAOL alone, which GAOL 5.0.1 made +# the reverses of gaol::pow, the power Codac computes, negative bases with an +# integer exponent included (https://github.com/Jordan08/GAOL/issues/400). In +# GAOL 5.0.0 they reversed the pow of IEEE 1788, which has no negative base, +# and gaol::pow left out the negative bases of any exponent but a degenerate +# integer: with it, PowOp::bwd() would empty x1 for y = x1^3 and x1 < 0, +# losing solutions, and pow([-2,2],[0.5,3]) would be [0,8], without +# (-1)^3 = -1. +# - not 5.0.1: GCC 9.3 and 10.2 for 64-bit ARM (10.2 is the compiler of Debian +# 11 Bullseye, which the CI of Codac builds on for arm64) stop with an +# internal compiler error in output_constant_pool_2 on the NEON intervals of +# GAOL 5.0.0 and 5.0.1, whose constructor interval(l, r) set the two bounds +# with vcombine_f64() (GCC bugs 99037 and 100441, fixed in GCC 9.4 and 10.3). +# GAOL 5.0.2 sets them with vsetq_lane_f64() +# (https://github.com/Jordan08/GAOL/pull/404). That constructor is in the +# headers of GAOL, which the sources of Codac and those of its users include: +# a GAOL 5.0.1 with its NEON intervals would stop such a compiler on Codac +# too, even with libgaol compiled by another compiler. +# The package of GAOL is versioned SameMajorVersion: find_package(gaol 5.0.2) +# takes any GAOL 5 from 5.0.2 on. +set(CODAC_GAOL_MIN_VERSION 5.0.2) + + +################################################################################ +# codac_gaol_portability_flags() +################################################################################ +# +# Returns in the flags of ibex_init_common() (see the top of this file) +# that are not those of interval arithmetic, and that Codac keeps whatever GAOL +# says: the top-level CMakeLists.txt puts them in CMAKE_CXX_FLAGS, where +# ibex_init_common() put them, after GAOL is brought in, so that GAOL is not +# compiled with them. They are returned as a list, one flag per element, +# because Codac also hands them over to its consumers, through CODAC_CXX_FLAGS +# and codac.pc; for the same reason, the Visual Studio "/D NAME" pairs of IBEX +# are spelt "/DNAME", which target_compile_options() cannot mistake for two +# duplicated "/D" and merge. The rest of ibex_init_common() is left out: the +# installation directories, the build type, the C++ standard and the uninstall +# target are Codac's own decisions, and the Debug-only -Wall -DDEBUG (/D DEBUG) +# have no use here, Codac choosing its own warnings and none of its code reading +# DEBUG. +# +# The two flags IBEX added for filib, which is not GAOL, are kept all the same, +# so that Codac goes on being compiled with the flags it had; so are those of +# Visual C++, until its jobs of the continuous integration tell whether Codac's +# own code still needs them with GAOL v5. With WITH_IBEX, ibex_init_common() is +# called itself and this function is not used. +function(codac_gaol_portability_flags outvar) + + include(CheckCXXCompilerFlag) + set(flags "") + + if(MSVC) + list(APPEND flags /D_CRT_SECURE_NO_WARNINGS /D_CRT_NONSTDC_NO_WARNINGS /Zc:__cplusplus /Zc:strictStrings-) + elseif(APPLE) + # Due to warnings on macOS with filib + check_cxx_compiler_flag("-Wno-undefined-var-template" COMPILER_SUPPORTS_WNO_UNDEFINED_VAR_TEMPLATE) + if(COMPILER_SUPPORTS_WNO_UNDEFINED_VAR_TEMPLATE) + list(APPEND flags -Wno-undefined-var-template) + endif() + endif() + # Claim IEEE 754 double compatibility, for filib + list(APPEND flags -D__STDC_IEC_559__=1) + + set(${outvar} ${flags} PARENT_SCOPE) +endfunction() + + +################################################################################ +# codac_gaol_fetch() +################################################################################ +# +# Downloads GAOL from the master branch of https://github.com/Jordan08/GAOL and +# adds it to this build (FetchContent), as Eigen and Catch2 are: the target gaol, +# its alias gaol::gaol, and the installation of GAOL with Codac, in the standard +# directories (include/gaol, lib/libgaol.a or gaol.lib, lib/cmake/gaol and +# lib/pkgconfig/gaol.pc), since the Codac libraries are of no use without it. +# Installed there, it is found by find_package(gaol) at a later configuration, +# of Codac or of another project. Being the standard directories, they are +# also those of a GAOL installed in the same prefix by other means, a GAOL 4 +# among them, which the installation of Codac overwrites. +# +# Built as a part of Codac rather than as a project of its own (Codac used to +# configure, build and install GAOL apart while it was configured): the CMake +# build of GAOL v5 is made for it (the alias gaol::gaol, -fPIC by default, an +# optimization when no build type is given, its tests and other targets only +# when GAOL is the project built, its installation with the project that brings +# it in). GAOL is then compiled with the compilers, the toolchain, the build +# type and the runtime library of Visual C++ of Codac, so that they always +# match: Visual C++ refuses to link objects compiled for its Debug runtime +# (/MDd) with others compiled for the Release one (/MD) -- error LNK2038, +# "mismatch detected for 'RuntimeLibrary'" -- which is why the top-level +# CMakeLists.txt had to force the Release runtime on Codac while GAOL was built +# apart, in Release. +# +# Not compiled with Codac's warnings and definitions, though (/W4, -Wall -Wextra +# -Wpedantic, /bigobj, FAST_RELEASE...): the top-level CMakeLists.txt adds them +# with add_compile_options() and add_compile_definitions(), whose directory +# properties a subdirectory takes from its parent when it is added. They are +# emptied while GAOL is added, and given back right after: GAOL is compiled with +# the flags it chooses, and with CMAKE_CXX_FLAGS as it is given to this build +# (codac_gaol_portability_flags() is added to it afterwards). +# +# From the head of the master branch, so that the fixes pushed to GAOL reach +# Codac without a change here. Cloned with Git, the sources are brought up to +# date with the branch at each configuration of Codac (the update step of +# FetchContent), which needs network access: without it, configure with +# -DFETCHCONTENT_UPDATES_DISCONNECTED_GAOL=ON to build the sources downloaded +# before. Without Git, the archive GitHub makes of the branch is downloaded +# instead, only once per build directory: a new commit reaches such a build once +# _deps/gaol-* is deleted from it. Whichever it is, the version of the GAOL +# obtained is checked: below CODAC_GAOL_MIN_VERSION, the configuration stops. +# +# Sets CODAC_GAOL_BINARY_DIR in the caller's scope: the build directory of GAOL, +# where its gaol.pc is (codac_gaol_pkg_config()). +function(codac_gaol_fetch) + + include(FetchContent) + find_package(Git QUIET) + if(GIT_FOUND) + # The files as they are in the repository, whatever core.autocrlf says (true + # on the Windows runners of GitHub Actions) + FetchContent_Declare(gaol + GIT_REPOSITORY https://github.com/Jordan08/GAOL.git + GIT_TAG master + GIT_SHALLOW TRUE + GIT_CONFIG core.autocrlf=false) + else() + FetchContent_Declare(gaol URL https://github.com/Jordan08/GAOL/archive/refs/heads/master.zip) + endif() + + message(STATUS "GAOL: downloading the master branch of https://github.com/Jordan08/GAOL, built with Codac") + + get_property(_codac_compile_options DIRECTORY PROPERTY COMPILE_OPTIONS) + get_property(_codac_compile_definitions DIRECTORY PROPERTY COMPILE_DEFINITIONS) + set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "") + set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS "") + FetchContent_MakeAvailable(gaol) + set_property(DIRECTORY PROPERTY COMPILE_OPTIONS "${_codac_compile_options}") + set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS "${_codac_compile_definitions}") + + # The version of the GAOL downloaded, which its CMake build gives the library + # (VERSION, from its VERSION.txt). master is GAOL v5; a branch that went back + # below CODAC_GAOL_MIN_VERSION, or sources of an older GAOL left in _deps by an + # older build of Codac and not updated (offline), would give Codac an interval + # arithmetic that needs the guards it no longer has (GAOL 4), a power whose + # reverse is not the one PowOp::bwd() needs (GAOL 5.0.0), or headers GCC 9.3 + # and 10.2 for 64-bit ARM cannot compile (GAOL 5.0.1). + set(_version "") + if(TARGET gaol::gaol) + get_target_property(_target gaol::gaol ALIASED_TARGET) + if(_target) + get_target_property(_version ${_target} VERSION) + endif() + endif() + if(NOT _version OR _version VERSION_LESS CODAC_GAOL_MIN_VERSION) + message(FATAL_ERROR "The GAOL downloaded from the master branch of https://github.com/Jordan08/GAOL is " + "version ${_version}, older than ${CODAC_GAOL_MIN_VERSION}, the oldest one Codac is " + "built on without guards (or it defines no target gaol::gaol). Configure Codac again " + "with network access, after " + "deleting ${FETCHCONTENT_BASE_DIR}/gaol-*, or with -DWITH_IBEX=ON to build on IBEX.") + endif() + message(STATUS "GAOL ${_version} downloaded, built with Codac and installed with it") + + set(GAOL_VERSION "${_version}" PARENT_SCOPE) + set(CODAC_GAOL_BINARY_DIR "${gaol_BINARY_DIR}" PARENT_SCOPE) +endfunction() + + +################################################################################ +# codac_gaol_find() +################################################################################ +# +# Finds GAOL v5 in the order given at the top of this file, or downloads it, +# and defines Codac::gaol, the imported target through which the Codac +# libraries, and the targets linking them, get GAOL's include directories, +# flags and library. Sets in the caller's scope: +# +# CODAC_GAOL_FROM "package", "pkg-config" or "fetched" +# CODAC_GAOL_BINARY_DIR the build directory of the GAOL fetched +# CODAC_INTERVAL_CXX_FLAGS the flags Codac::gaol gives (codac_gaol_flags()) +# GAOL_VERSION +# +# An INTERFACE library rather than GAOL's own target, whatever provided GAOL: +# CODAC_LIBRARIES names it, and codac-config.cmake defines it again +# (codac_gaol_config_snippet()), which it could not do under the name gaol::gaol +# without clashing with a find_package(gaol) of the consumer. +function(codac_gaol_find) + + set(_from "") + set(_version "") + set(_binary_dir "") + + if(ENABLE_FIND_PACKAGE_GAOL) + + # 1. The CMake package of GAOL (gaol_DIR, or CMAKE_PREFIX_PATH), version + # CODAC_GAOL_MIN_VERSION asked. The version is checked by the package's + # gaolConfigVersion.cmake (SameMajorVersion) before the package is loaded: a + # GAOL 4, 5.0.0 or 5.0.1 is refused without defining its targets. Loaded, + # the gaol::gaol of such a GAOL would clash with the alias gaol::gaol of the + # GAOL v5 downloaded next, and CMake would stop ("add_library cannot create + # ALIAS target "gaol::gaol" because another target with the same name + # already exists"). + find_package(gaol ${CODAC_GAOL_MIN_VERSION} CONFIG QUIET) + if(gaol_FOUND) + set(_from package) + set(_version "${gaol_VERSION}") + message(STATUS "Found GAOL ${gaol_VERSION}, CMake package in ${gaol_DIR}") + elseif(gaol_CONSIDERED_VERSIONS) + message(STATUS "Found GAOL ${gaol_CONSIDERED_VERSIONS} (CMake package ${gaol_CONSIDERED_CONFIGS}), " + "older than ${CODAC_GAOL_MIN_VERSION}: not used") + endif() + + # 2. pkg-config (PKG_CONFIG_PATH, or CMAKE_PREFIX_PATH), see FindGAOL.cmake + if(NOT _from) + find_package(GAOL MODULE QUIET) + if(GAOL_FOUND) + set(_from pkg-config) + set(_version "${GAOL_VERSION}") + message(STATUS "Found GAOL ${GAOL_VERSION}, gaol.pc in ${GAOL_PC_PREFIX}, used with the flags of its Cflags") + endif() + endif() + endif() + + # 3. Downloaded and built with Codac + if(NOT _from) + codac_gaol_fetch() + set(_from fetched) + set(_version "${GAOL_VERSION}") + set(_binary_dir "${CODAC_GAOL_BINARY_DIR}") + endif() + + add_library(Codac::gaol INTERFACE IMPORTED) + if(_from STREQUAL "pkg-config") + set_target_properties(Codac::gaol PROPERTIES INTERFACE_LINK_LIBRARIES PkgConfig::CODAC_GAOL_PC) + else() + set_target_properties(Codac::gaol PROPERTIES INTERFACE_LINK_LIBRARIES gaol::gaol) + endif() + + codac_gaol_flags(Codac::gaol _flags) + + set(CODAC_GAOL_FROM "${_from}" PARENT_SCOPE) + set(CODAC_GAOL_BINARY_DIR "${_binary_dir}" PARENT_SCOPE) + set(CODAC_INTERVAL_CXX_FLAGS ${_flags} PARENT_SCOPE) + set(GAOL_VERSION "${_version}" PARENT_SCOPE) +endfunction() + + +################################################################################ +# codac_gaol_from_ibex() +################################################################################ +# +# With WITH_IBEX: Codac::gaol for the GAOL 4 of IBEX, after the top-level +# CMakeLists.txt has run find_package(IBEX) and ibex_init_common() as Codac did +# before GAOL v5 (ibex_init_common() sets variables in the scope it is called +# from, and cannot be called from a function here). Codac::gaol names Ibex::ibex, +# which brings GAOL, mathlib and their flags (Ibex::gaol carries -frounding-math). +# +# Sets CODAC_GAOL_FROM to "ibex" in the caller's scope, and +# CODAC_INTERVAL_CXX_FLAGS to what ibex_init_common() appended to +# CMAKE_CXX_FLAGS: the IEEE 754 flags of the IBEX of Fabrice Le Bars, for +# instance, which this build compiles every target with, and which the users of +# the installed Codac get through CODAC_CXX_FLAGS. They used to call +# ibex_init_common() themselves after find_package(CODAC), as the examples did; +# the examples no longer do, whatever IBEX or GAOL Codac is built on. +function(codac_gaol_from_ibex cxx_flags_before) + + set(_added "") + string(LENGTH "${cxx_flags_before}" _length) + string(LENGTH "${CMAKE_CXX_FLAGS}" _length_after) + if(_length_after GREATER _length) + string(SUBSTRING "${CMAKE_CXX_FLAGS}" 0 ${_length} _start) + if(_start STREQUAL cxx_flags_before) + string(SUBSTRING "${CMAKE_CXX_FLAGS}" ${_length} -1 _added) + separate_arguments(_added UNIX_COMMAND "${_added}") + endif() + endif() + + add_library(Codac::gaol INTERFACE IMPORTED) + set_target_properties(Codac::gaol PROPERTIES INTERFACE_LINK_LIBRARIES Ibex::ibex) + + set(CODAC_GAOL_FROM ibex PARENT_SCOPE) + set(CODAC_INTERVAL_CXX_FLAGS ${_added} PARENT_SCOPE) +endfunction() + + +################################################################################ +# codac_gaol_flags( ) +################################################################################ +# +# Returns in the compilation flags gives the targets linking +# it, walked out of it and of the targets it links: its options and its +# definitions (as -D, or /D with Visual C++). This is for what cannot link +# Codac::gaol: CODAC_CXX_FLAGS in codac-config.cmake. Of the generator +# expressions, $<$:...> is unwrapped (GAOL gives +# -faligned-new=8 to C++ only, on 32-bit Windows), the others left out, and so +# is what $ names, which gives no flag. +function(codac_gaol_flags target outvar) + + set(flags "") + set(queue ${target}) + set(seen "") + + while(queue) + list(GET queue 0 item) + list(REMOVE_AT queue 0) + if(item IN_LIST seen OR NOT TARGET "${item}") + continue() + endif() + list(APPEND seen "${item}") + + get_target_property(_value "${item}" INTERFACE_COMPILE_OPTIONS) + if(_value) + list(APPEND flags ${_value}) + endif() + get_target_property(_value "${item}" INTERFACE_COMPILE_DEFINITIONS) + if(_value) + foreach(_definition ${_value}) + if(MSVC) + list(APPEND flags "/D${_definition}") + else() + list(APPEND flags "-D${_definition}") + endif() + endforeach() + endif() + get_target_property(_value "${item}" INTERFACE_LINK_LIBRARIES) + if(_value) + list(APPEND queue ${_value}) + endif() + endwhile() + + set(_flags "") + foreach(flag ${flags}) + if(flag MATCHES "^\\$<\\$:(.*)>$") + list(APPEND _flags "${CMAKE_MATCH_1}") + elseif(NOT flag MATCHES "\\$<") + list(APPEND _flags "${flag}") + endif() + endforeach() + if(_flags) + list(REMOVE_DUPLICATES _flags) + endif() + + set(${outvar} ${_flags} PARENT_SCOPE) +endfunction() + + +################################################################################ +# codac_gaol_pkg_config( ) +################################################################################ +# +# Returns what codac.pc says of GAOL (src/CMakeLists.txt), which cannot link a +# target: +# +# - for the GAOL of IBEX (WITH_IBEX): "Requires: ibex" and +# -I${includedir}/ibex, as codac.pc said before GAOL v5; +# - for a GAOL v5 found by its CMake package or its gaol.pc: "Requires: gaol +# >= CODAC_GAOL_MIN_VERSION", whose gaol.pc gives the flags of interval +# arithmetic and libgaol; +# - for the GAOL v5 downloaded and installed with Codac: the Cflags and Libs of +# the gaol.pc its build wrote, copied into those of codac.pc. GAOL installs +# its gaol.pc in lib/pkgconfig, and codac.pc goes to share/pkgconfig: a +# "Requires: gaol" would fail for a user whose PKG_CONFIG_PATH names only the +# latter, with a prefix pkg-config does not search by default. The Cflags +# and Libs of gaol.pc name ${includedir} and ${libdir}, which codac.pc +# defines too, as the same directories: GAOL is installed under the prefix of +# Codac, with the same CMAKE_INSTALL_INCLUDEDIR and CMAKE_INSTALL_LIBDIR. +# With Visual C++, GAOL writes no gaol.pc, and nothing is copied. +function(codac_gaol_pkg_config cflags_var requires_var libs_var) + + set(_cflags "") + set(_requires "") + set(_libs "") + + if(CODAC_GAOL_FROM STREQUAL "ibex") + set(_cflags "-I\${includedir}/ibex") + set(_requires "ibex") + elseif(CODAC_GAOL_FROM STREQUAL "fetched") + if(EXISTS "${CODAC_GAOL_BINARY_DIR}/gaol.pc") + file(STRINGS "${CODAC_GAOL_BINARY_DIR}/gaol.pc" _lines REGEX "^(Cflags|Libs):") + foreach(_line ${_lines}) + if(_line MATCHES "^Cflags:(.*)$") + string(STRIP "${CMAKE_MATCH_1}" _cflags) + elseif(_line MATCHES "^Libs:(.*)$") + string(STRIP "${CMAKE_MATCH_1}" _libs) + endif() + endforeach() + # Without the -L${libdir} that the Libs of codac.pc start with already + string(REGEX REPLACE "^-L\\$\\{libdir\\}[ ]*" "" _libs "${_libs}") + endif() + else() + set(_requires "gaol >= ${CODAC_GAOL_MIN_VERSION}") + endif() + + set(${cflags_var} "${_cflags}" PARENT_SCOPE) + set(${requires_var} "${_requires}" PARENT_SCOPE) + set(${libs_var} "${_libs}" PARENT_SCOPE) +endfunction() + + +################################################################################ +# codac_gaol_config_snippet() +################################################################################ +# +# Returns the lines of codac-config.cmake that define Codac::gaol for a consumer +# of the installed Codac, which CODAC_LIBRARIES names -- the counterpart of the +# ibex-config-gaol.cmake and ibex-config-ultim.cmake files that +# create_target_import_and_export() writes for IBEX. +# +# - The GAOL v5 downloaded and installed with Codac is found again by +# find_package(gaol CODAC_GAOL_MIN_VERSION CONFIG) under the prefix Codac is +# installed under, by a path relative to the configuration file, since the +# prefix may be moved, and only there: Codac's inline interval operations are +# compiled against the GAOL of the program that includes them, which has to +# be the one libgaol was compiled from, and a package search path of the +# consumer could lead to another. +# - A GAOL v5 found by its CMake package, where it was found, before the usual +# search, version CODAC_GAOL_MIN_VERSION asked as when Codac was built. +# - A GAOL v5 found by pkg-config is named by what its gaol.pc gave: the +# include directories, the flags, the library files and the link options. +# - The GAOL of IBEX (WITH_IBEX), by find_package(IBEX), as codac-config.cmake +# did before GAOL v5, where IBEX was found. +function(codac_gaol_config_snippet outvar) + + if(CODAC_GAOL_FROM STREQUAL "ibex") + set(_find "find_package(IBEX REQUIRED HINTS \"${IBEX_DIR}\")") + set(_properties "INTERFACE_LINK_LIBRARIES Ibex::ibex") + elseif(CODAC_GAOL_FROM STREQUAL "fetched") + file(RELATIVE_PATH _to_prefix "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_CMAKE}" "${CMAKE_INSTALL_PREFIX}") + set(_find "get_filename_component(_codac_prefix \"\${CMAKE_CURRENT_LIST_DIR}/${_to_prefix}\" ABSOLUTE) + find_package(gaol ${CODAC_GAOL_MIN_VERSION} CONFIG REQUIRED NO_DEFAULT_PATH + PATHS \"\${_codac_prefix}/${CMAKE_INSTALL_LIBDIR}/cmake/gaol\")") + set(_properties "INTERFACE_LINK_LIBRARIES gaol::gaol") + elseif(CODAC_GAOL_FROM STREQUAL "package") + set(_find "find_package(gaol ${CODAC_GAOL_MIN_VERSION} CONFIG REQUIRED HINTS \"${gaol_DIR}\")") + set(_properties "INTERFACE_LINK_LIBRARIES gaol::gaol") + else() + set(_find "") + set(_properties "") + foreach(_property INTERFACE_INCLUDE_DIRECTORIES INTERFACE_COMPILE_OPTIONS + INTERFACE_LINK_LIBRARIES INTERFACE_LINK_OPTIONS) + get_target_property(_value PkgConfig::CODAC_GAOL_PC ${_property}) + if(_value) + if(_properties) + string(APPEND _properties "\n ") + endif() + string(APPEND _properties "${_property} \"${_value}\"") + endif() + endforeach() + endif() + + set(${outvar} " + # GAOL, the interval arithmetic library Codac is built upon, with the flags + # of interval arithmetic, as Codac::gaol, which CODAC_LIBRARIES names + ${_find} + if(NOT TARGET Codac::gaol) + add_library(Codac::gaol INTERFACE IMPORTED) + set_target_properties(Codac::gaol PROPERTIES + ${_properties}) + endif() +" PARENT_SCOPE) +endfunction() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c284c553e..05fc1d03a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -24,8 +24,26 @@ set(CODAC_PKG_CONFIG_FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc) - set(CODAC_PKG_CONFIG_CFLAGS "-I\${includedir}/ibex") + # GAOL as codac.pc names it (codac_gaol_pkg_config(), in + # scripts/CMakeModules/codac_gaol.cmake): "Requires: gaol >= 5.0.2" + # (CODAC_GAOL_MIN_VERSION) for a GAOL v5 found on this machine, whose gaol.pc + # carries the flags of interval arithmetic; the Cflags and Libs of its gaol.pc + # for the GAOL v5 Codac downloads and installs with itself; "Requires: ibex" + # and -I\${includedir}/ibex, as before GAOL v5, with WITH_IBEX. + codac_gaol_pkg_config(CODAC_PKG_CONFIG_CFLAGS CODAC_PKG_CONFIG_REQUIRES _codac_gaol_pkg_config_libs) set(CODAC_PKG_CONFIG_LIBS "-L\${libdir}") + if(_codac_gaol_pkg_config_libs) + string(APPEND CODAC_PKG_CONFIG_LIBS " ${_codac_gaol_pkg_config_libs}") + endif() + + # Codac's own flags, which this build puts in CMAKE_CXX_FLAGS + # (codac_gaol_portability_flags() in scripts/CMakeModules/codac_gaol.cmake). + # A CMake consumer gets them through CODAC_CXX_FLAGS; a pkg-config consumer + # through these Cflags. + if(CODAC_PORTABILITY_CXX_FLAGS) + string(REPLACE ";" " " _codac_pkg_config_flags "${CODAC_PORTABILITY_CXX_FLAGS}") + string(APPEND CODAC_PKG_CONFIG_CFLAGS " ${_codac_pkg_config_flags}") + endif() file(GENERATE OUTPUT ${CODAC_PKG_CONFIG_FILE} CONTENT " prefix=${CMAKE_INSTALL_PREFIX} @@ -36,7 +54,7 @@ Description: ${PROJECT_DESCRIPTION} Url: ${PROJECT_HOMEPAGE_URL} Version: ${PROJECT_VERSION} - Requires: ibex + Requires: ${CODAC_PKG_CONFIG_REQUIRES} Cflags: ${CODAC_PKG_CONFIG_CFLAGS} Libs: ${CODAC_PKG_CONFIG_LIBS} ") @@ -49,6 +67,19 @@ set(CODAC_CMAKE_CONFIG_FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake) + # The definition of Codac::gaol, written by + # scripts/CMakeModules/codac_gaol.cmake, which knows where GAOL came from. + codac_gaol_config_snippet(CODAC_GAOL_CONFIG_SNIPPET) + + # Codac's own flags (codac_gaol_portability_flags()) and the interval + # arithmetic flags GAOL gives (-frounding-math, -mfma, etc., or with WITH_IBEX + # those ibex_init_common() added), for external projects that do not link + # CODAC_LIBRARIES, whose Codac::gaol gives them too. In that order, which is + # the order this build hands them to the compiler in -- the first through + # CMAKE_CXX_FLAGS, the second through Codac::gaol, whose options come after a + # target's own -- and decides which of two conflicting flags wins. + set(CODAC_CXX_FLAGS ${CODAC_PORTABILITY_CXX_FLAGS} ${CODAC_INTERVAL_CXX_FLAGS}) + file(WRITE ${CODAC_CMAKE_CONFIG_FILE} "# Try to find Codac # This file has been generated by CMake @@ -69,14 +100,26 @@ find_library(CODAC_UNSUPPORTED_LIBRARY NAMES ${PROJECT_NAME}-unsupported PATH_SUFFIXES lib) - find_package(IBEX REQUIRED) +${CODAC_GAOL_CONFIG_SNIPPET} + # Projects written for a Codac that depended on IBEX call ibex_init_common() + # after find_package(CODAC), as the manual used to tell them to, for the + # interval arithmetic flags. Those flags now come with CODAC_CXX_FLAGS, and + # this ibex_init_common() does nothing: it is only there so that such + # projects still configure. A project that finds IBEX itself, before or + # after Codac, gets IBEX's own function instead, and so does every project + # when Codac is built with WITH_IBEX, whose find_package(IBEX) above defines + # it. + if(NOT COMMAND ibex_init_common) + function(ibex_init_common) + endfunction() + endif() set(CODAC_VERSION ${PROJECT_VERSION}) - set(CODAC_LIBRARIES \${CODAC_CORE_LIBRARY} \${CODAC_GRAPHICS_LIBRARY} \${CODAC_UNSUPPORTED_LIBRARY} Ibex::ibex) + set(CODAC_LIBRARIES \${CODAC_CORE_LIBRARY} \${CODAC_GRAPHICS_LIBRARY} \${CODAC_UNSUPPORTED_LIBRARY} Codac::gaol) set(CODAC_INCLUDE_DIRS \${CODAC_CORE_INCLUDE_DIR}/../ \${CODAC_EIGEN_INCLUDE_DIR} \${CODAC_CORE_INCLUDE_DIR} \${CODAC_GRAPHICS_INCLUDE_DIR} \${CODAC_UNSUPPORTED_INCLUDE_DIR}) set(CODAC_C_FLAGS \"\") - set(CODAC_CXX_FLAGS \"\") + set(CODAC_CXX_FLAGS \"${CODAC_CXX_FLAGS}\") ") if(WITH_PYTHON) @@ -113,7 +156,7 @@ endif() file(APPEND ${CODAC_CMAKE_CONFIG_FILE} " - set(CODAC_LIBRARIES \${CODAC_LIBRARIES} \${CODAC_GRAPHICS_LIBRARY} \${CODAC_CORE_LIBRARY}) + set(CODAC_LIBRARIES \${CODAC_LIBRARIES} \${CODAC_GRAPHICS_LIBRARY} \${CODAC_CORE_LIBRARY} Codac::gaol) ") diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 7c3752756..f99e4c846 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -331,7 +331,7 @@ ${CMAKE_CURRENT_SOURCE_DIR}/tools ${CMAKE_CURRENT_SOURCE_DIR}/trajectory ) - target_link_libraries(${PROJECT_NAME}-core PUBLIC ${PROJECT_NAME}-eigen Ibex::ibex Threads::Threads) #Eigen3::Eigen + target_link_libraries(${PROJECT_NAME}-core PUBLIC ${PROJECT_NAME}-eigen Codac::gaol Threads::Threads) #Eigen3::Eigen ################################################################################ diff --git a/src/core/domains/interval/codac2_Interval.h b/src/core/domains/interval/codac2_Interval.h index 09dca1ac5..04df34b30 100644 --- a/src/core/domains/interval/codac2_Interval.h +++ b/src/core/domains/interval/codac2_Interval.h @@ -16,7 +16,47 @@ #include #include +#include +#include #include + +// CODAC_GAOL_GUARDS: whether Codac calls GAOL through the guards it kept around +// GAOL 4, the one place where Codac chooses between the two interval +// arithmetics it can be built on. The choice follows the GAOL whose headers are +// included, so that Codac's libraries and every program including Codac's +// headers make the same one, with nothing for the build to pass on: +// +// - 0 for GAOL v5 (https://github.com/Jordan08/GAOL), which Codac is built on +// by default: GAOL v5 leaves the rounding upward after each operation, gives +// the empty set where GAOL 4 gave intervals Codac had to correct (an +// infinite double, log of an interval without positive numbers, the +// intersection of disjoint intervals), computes unbounded cosh, asinh of +// negative numbers and the roots of odd order of negative numbers itself, +// and provides rad(), bisect(), inflate(), atan2, sign, the relations +// between intervals (subset, interior, disjoint, equal) of IEEE 1788 and the +// reverse functions (*_rel) of floor, sign, min, max, sinh, atan2 and of +// the powers. Codac calls it directly, and takes from it every function it +// provides. +// - 1 for GAOL 4, the GAOL that IBEX bundles, which Codac is built on with the +// developer option WITH_IBEX (see the top-level CMakeLists.txt): Codac +// keeps the guards it had around it before GAOL v5, unchanged, to tell +// whether a wrong result comes from GAOL v5. +// +// GAOL_MAJOR_VERSION comes from gaol/gaol_configuration.h, which +// gaol/gaol_interval.h includes: GAOL v5 defines it with every compiler, the +// GAOL 4 of IBEX defines it as 4, and Frederic Goualard's GAOL 4 does not +// define it with Visual C++ or MinGW, which gives 1 as well. The functions +// concerned are in the blocks "#if CODAC_GAOL_GUARDS" at the end of +// codac2_Interval_impl.h, among the elementary functions of +// codac2_Interval_operations_impl.h and in the bwd() functions of the +// operators of src/core/operators whose reverse GAOL v5 provides (atan2, ceil, +// floor, max, min, pow, sign and sinh), the only places that read it. +#if defined(GAOL_MAJOR_VERSION) && GAOL_MAJOR_VERSION >= 5 + #define CODAC_GAOL_GUARDS 0 +#else + #define CODAC_GAOL_GUARDS 1 +#endif + #include "codac2_Index.h" #include "codac2_Domain.h" #include "codac2_assert.h" @@ -27,6 +67,15 @@ namespace codac2 { class Interval; + /** + * \brief The integer types that the arithmetic operators of Interval take as + * they are (see operator+(const Interval&, T) and the others at the end of + * this file): every integer type but bool, as GAOL v5 has them + * (gaol_detail::is_integer, in gaol/core/gaol_interval_detail.h). + */ + template + concept IsIntervalIntegerType = std::integral && !std::is_same_v,bool>; + template<> struct is_interval_based : std::true_type {}; @@ -674,6 +723,16 @@ namespace codac2 _dec_friend_interval2_arithm_op(operator*) _dec_friend_interval2_arithm_op(operator/) + // The arithmetic operators with an integer (see the end of this file) + #define _dec_friend_interval2_integer_arithm_op(f) \ + template friend Interval f(T, const Interval&); \ + template friend Interval f(const Interval&, T); \ + + _dec_friend_interval2_integer_arithm_op(operator+) + _dec_friend_interval2_integer_arithm_op(operator-) + _dec_friend_interval2_integer_arithm_op(operator*) + _dec_friend_interval2_integer_arithm_op(operator/) + #define _dec_friend_interval2_unary_op(f) \ friend Interval f(const Interval&); \ @@ -730,6 +789,15 @@ namespace codac2 friend struct ExpOp; friend struct LogOp; friend struct PowOp; + // The bwd() of these operators call the reverse functions of GAOL v5 + // (floor_rel(), max_rel(), min_rel() and sign_rel()) on the gaol::interval + // of their arguments, as the operators above do (CeilOp through the + // floor_rel() of the opposites). + friend struct CeilOp; + friend struct FloorOp; + friend struct MaxOp; + friend struct MinOp; + friend struct SignOp; friend struct SinOp; friend struct SinhOp; friend struct SqrOp; @@ -931,6 +999,101 @@ namespace codac2 * \return the division result */ Interval operator/(const Interval& x, const Interval& y); + + // The arithmetic operators with an integer n, in either order. GAOL v5 has + // them for gaol::interval (gaol/core/gaol_interval_core.h): it computes with + // the tightest interval containing n, where the conversion of n to a double + // could give a double that is not n (an integer beyond 2^53). Found for an + // Interval too, by argument-dependent lookup since Interval derives from + // gaol::interval, they made each expression of an Interval and an integer + // ambiguous with the operators of Interval with a double declared above + // ("ambiguous overload for 'operator*' (operand types are 'int' and + // 'codac2::Interval')", as in 2*x or Interval::pi()/2): the integer is an + // exact match for GAOL's, the Interval for Codac's. These templates are an + // exact match for both operands, and are chosen. With GAOL v5, they call + // GAOL's operators with an integer; with the GAOL 4 of IBEX (WITH_IBEX), + // which has none, they convert n to a double, as the implicit conversion did + // before (see the block "#if CODAC_GAOL_GUARDS" of codac2_Interval_impl.h). + + /** + * \brief Returns \f$[x]+n\f$ with \f$n\in\mathbb{Z}\f$ + * + * \param x interval value + * \param n integer value + * \return the addition result + */ + template + Interval operator+(const Interval& x, T n); + + /** + * \brief Returns \f$n+[y]\f$ with \f$n\in\mathbb{Z}\f$ + * + * \param n integer value + * \param y interval value + * \return the addition result + */ + template + Interval operator+(T n, const Interval& y); + + /** + * \brief Returns \f$[x]-n\f$ with \f$n\in\mathbb{Z}\f$ + * + * \param x interval value + * \param n integer value + * \return the subtraction result + */ + template + Interval operator-(const Interval& x, T n); + + /** + * \brief Returns \f$n-[y]\f$ with \f$n\in\mathbb{Z}\f$ + * + * \param n integer value + * \param y interval value + * \return the subtraction result + */ + template + Interval operator-(T n, const Interval& y); + + /** + * \brief Returns \f$[x]\cdot n\f$ with \f$n\in\mathbb{Z}\f$ + * + * \param x interval value + * \param n integer value + * \return the multiplication result + */ + template + Interval operator*(const Interval& x, T n); + + /** + * \brief Returns \f$n\cdot[y]\f$ with \f$n\in\mathbb{Z}\f$ + * + * \param n integer value + * \param y interval value + * \return the multiplication result + */ + template + Interval operator*(T n, const Interval& y); + + /** + * \brief Returns \f$[x]/n\f$ with \f$n\in\mathbb{Z}\f$ + * + * \param x interval value + * \param n integer value + * \return the division result + */ + template + Interval operator/(const Interval& x, T n); + + /** + * \brief Returns \f$n/[y]\f$ with \f$n\in\mathbb{Z}\f$ + * + * \param n integer value + * \param y interval value + * \return the division result + */ + template + Interval operator/(T n, const Interval& y); } #include "codac2_Interval_impl.h" diff --git a/src/core/domains/interval/codac2_Interval_impl.h b/src/core/domains/interval/codac2_Interval_impl.h index bb7d31826..ba019f16e 100644 --- a/src/core/domains/interval/codac2_Interval_impl.h +++ b/src/core/domains/interval/codac2_Interval_impl.h @@ -20,17 +20,6 @@ namespace codac2 { - inline Interval::Interval() - : gaol::interval(-oo,oo) - { } - - inline Interval::Interval(double a) - : gaol::interval(a) - { - if(a == -oo || a == oo) - set_empty(); - } - inline Interval::Interval(double a, double b) : gaol::interval(a,b) { } @@ -53,12 +42,6 @@ namespace codac2 init_from_list(l); } - inline Interval& Interval::init() - { - *this = Interval(-oo,oo); - return *this; - } - inline Interval& Interval::init(const Interval& x) { *this = x; @@ -81,43 +64,12 @@ namespace codac2 return *this; } - inline Interval& Interval::operator=(double x) - { - if(x == -oo || x == oo) - set_empty(); - else - gaol::interval::operator=(x); - - return *this; - } - inline Interval& Interval::operator=(const Interval& x) { gaol::interval::operator=(x); return *this; } - inline bool Interval::operator==(const Interval& x) const - { - return (is_empty() && x.is_empty()) || (lb() == x.lb() && ub() == x.ub()); - } - - inline bool Interval::operator!=(const Interval& x) const - { - return !(*this == x); - } - - inline BoolInterval Interval::operator<(const Interval& x) const - { - if(is_empty() || x.is_empty()) - return BoolInterval::EMPTY; - if(this->ub() < x.lb()) - return BoolInterval::TRUE; - if(x.ub() <= this->lb()) - return BoolInterval::FALSE; - return BoolInterval::UNKNOWN; - } - inline BoolInterval Interval::operator>(const Interval& x) const { return x < *this; @@ -133,13 +85,6 @@ namespace codac2 return gaol::interval::right(); } - inline double Interval::mid() const - { - double m = gaol::interval::midpoint(); - gaol::round_upward(); - return m; - } - inline double Interval::mag() const { return gaol::interval::mag(); @@ -173,36 +118,6 @@ namespace codac2 return std::max(lb(),std::min(r,ub())); } - inline double Interval::rad() const - { - if(is_empty()) - return std::numeric_limits::quiet_NaN(); - - else if(is_unbounded()) - return oo; - - else - { - double t = mid(); - double t1 = (t-*this).ub(); - double t2 = (*this-t).ub(); - return (t1>t2) ? t1 : t2; - } - } - - inline double Interval::diam() const - { - if(is_empty()) - return std::numeric_limits::quiet_NaN(); - - else - { - double d = gaol::interval::width(); - gaol::round_upward(); - return d; - } - } - inline double Interval::volume() const { return diam(); @@ -228,11 +143,6 @@ namespace codac2 return gaol::interval::set_contains(x); } - inline bool Interval::interior_contains(const double& x) const - { - return !is_empty() && x > lb() && x < ub(); - } - inline bool Interval::is_unbounded() const { return !gaol::interval::is_finite(); @@ -253,104 +163,11 @@ namespace codac2 return trunc(lb()) == lb() && trunc(ub()) == ub(); } - inline bool Interval::intersects(const Interval &x) const - { - return !is_empty() && !x.is_empty() && lb() <= x.ub() && ub() >= x.lb(); - } - - inline bool Interval::is_disjoint(const Interval& x) const - { - return is_empty() || x.is_empty() || lb() > x.ub() || ub() < x.lb(); - } - inline bool Interval::overlaps(const Interval& x) const { return !is_empty() && !x.is_empty() && ub() > x.lb() && x.ub() > lb(); } - inline bool Interval::is_subset(const Interval& x) const - { - return is_empty() || (!x.is_empty() && x.lb() <= lb() && x.ub() >= ub()); - } - - inline bool Interval::is_strict_subset(const Interval& x) const - { - return !x.is_empty() && (is_empty() || (x.lb() < lb() && x.ub() >= ub()) || (x.ub() > ub() && x.lb() <= lb())); - } - - inline bool Interval::is_interior_subset(const Interval& x) const - { - return is_empty() || (!x.is_empty() && (x.lb() == -oo || x.lb() < lb()) && (x.ub() == oo || x.ub() > ub())); - } - - inline bool Interval::is_strict_interior_subset(const Interval& x) const - { - return !x.is_empty() && (is_empty() || ( - (x.lb() < lb() && (x.ub() == oo || x.ub() > ub())) - || (x.ub() > ub() && (x.lb() == -oo || x.lb() < lb())) - )); - } - - inline bool Interval::is_superset(const Interval& x) const - { - return x.is_subset(*this); - } - - inline bool Interval::is_strict_superset(const Interval& x) const - { - return x.is_strict_subset(*this); - } - - inline Interval& Interval::inflate(const double& rad) - { - (*this) += Interval(-rad,rad); - return *this; - } - - inline bool Interval::is_bisectable() const - { - if(is_empty()) - return false; - double m = mid(); - return lb() < m && m < ub(); - } - - inline std::pair Interval::bisect(float ratio) const - { - assert_release(is_bisectable()); - assert_release(Interval(0,1).interior_contains(ratio)); - - if(lb() == -oo) - { - if(ub() == oo) - return { Interval(-oo,0), Interval(0,oo) }; - else - return { Interval(-oo,-std::numeric_limits::max()), Interval(-std::numeric_limits::max(),ub()) }; - } - - else if(ub() == oo) - return { Interval(lb(),std::numeric_limits::max()), Interval(std::numeric_limits::max(),oo) }; - - else - { - double m; - - if(ratio == 0.5) - m = mid(); - - else - { - m = lb() + ratio*diam(); - if(m >= ub()) - m = next_float(lb()); - - assert(m < ub()); - } - - return { Interval(lb(),m), Interval(m,ub()) }; - } - } - inline std::vector Interval::complementary(bool compactness) const { if(is_empty() || (compactness && is_degenerated())) @@ -388,20 +205,6 @@ namespace codac2 return l; } - inline Interval operator&(const Interval& x, const Interval& y) - { - if(x.is_empty() || y.is_empty() || x.ub() < y.lb()) - return Interval::empty(); - - else - return gaol::operator&(x,y); - } - - inline Interval operator|(const Interval& x, double y) - { - return gaol::operator|(x,Interval(y)); - } - inline Interval operator|(const Interval& x, const Interval& y) { return gaol::operator|(x,y); @@ -412,228 +215,854 @@ namespace codac2 return x; } - inline Interval operator+(const Interval& x, double y) + inline Interval operator+(const Interval& x, const Interval& y) { - if(y == -oo || y == oo) - return Interval::empty(); - - else - return gaol::operator+(x,y); + return gaol::operator+(x,y); } - inline Interval operator+(double x, const Interval& y) + inline Interval operator-(const Interval& x, const Interval& y) { - if(x == -oo || x == oo) - return Interval::empty(); - - else - return gaol::operator+(x,y); + return gaol::operator-(x, y); } - inline Interval operator+(const Interval& x, const Interval& y) + inline Interval operator*(const Interval& x, const Interval& y) { - return gaol::operator+(x,y); + return gaol::operator*(x,y); } - inline Interval operator-(const Interval& x, double y) + inline Interval operator/(const Interval& x, const Interval& y) { - if(y == -oo || y == oo) - return Interval::empty(); - - else - return gaol::operator-(x, y); + return gaol::operator/(x,y); } - inline Interval operator-(double x, const Interval& y) + inline Interval& Interval::operator|=(const Interval& x) { - if(x == -oo || x == oo) - return Interval::empty(); - - else - return gaol::operator-(x, y); + gaol::interval::operator|=(x); + return *this; } - inline Interval operator-(const Interval& x, const Interval& y) + inline Interval& Interval::operator&=(const Interval& x) { - return gaol::operator-(x, y); + gaol::interval::operator&=(x); + return *this; } - inline Interval operator*(const Interval& x, double y) + inline Interval& Interval::operator+=(const Interval& x) { - if(y == -oo || y == oo) - return Interval::empty(); - - else - return gaol::operator*(x,y); + gaol::interval::operator+=(x); + return *this; } - inline Interval operator*(double x, const Interval& y) + inline Interval& Interval::operator-=(const Interval& x) { - if(x == -oo || x == oo) - return Interval::empty(); + gaol::interval::operator-=(x); + return *this; + } - else + inline Interval& Interval::operator*=(const Interval& x) + { + gaol::interval::operator*=(x); + return *this; + } + + inline Interval& Interval::operator/=(const Interval& x) + { + gaol::interval::operator/=(x); + return *this; + } + + inline Interval Interval::zero() + { + return gaol::interval::zero(); + } + + inline Interval Interval::one() + { + return gaol::interval::one(); + } + + inline Interval Interval::half_pi() + { + return gaol::interval::half_pi(); + } + + inline Interval Interval::pi() + { + return gaol::interval::pi(); + } + + inline Interval Interval::two_pi() + { + return gaol::interval::two_pi(); + } + + inline std::ostream& operator<<(std::ostream& os, const Interval& x) + { + gaol::interval::precision(os.precision()); + gaol::operator<<(os,x); + return os; + } + + inline Interval::Interval(const gaol::interval& x) + : gaol::interval(x) + { } + + inline Interval operator""_i(long double x) + { + return Interval(static_cast(x)); + } + + inline double prev_float(double x) + { + return gaol::previous_float(x); + } + + inline double next_float(double x) + { + return gaol::next_float(x); + } + + // --------------------------------------------------------------------------- + // The functions whose code depends on the GAOL Codac is built on + // --------------------------------------------------------------------------- + // + // CODAC_GAOL_GUARDS (see codac2_Interval.h) chooses between two versions of + // them: the code Codac had before GAOL v5, with its guards around GAOL 4, and + // the code that calls GAOL v5 directly, taking from it every function it + // provides. This block, the one of codac2_Interval_operations_impl.h and the + // bwd() of the operators listed in codac2_Interval.h are the only places + // where the two versions differ. The functions of this block used to be defined among the + // others above; their version with the guards is moved here unchanged, so + // that going back to it (WITH_IBEX) gives exactly the Codac of before. +#if CODAC_GAOL_GUARDS + + // With the guards, for GAOL 4, the GAOL that IBEX bundles (WITH_IBEX): + // + // - Interval(double), operator=(double), and the arithmetic operators and + // compound assignments with a double give the empty set for an infinite + // double, of which GAOL 4 makes the interval [+oo,+oo] (or [-oo,-oo]), + // which contains no real number; + // - mid() and diam() set the rounding upward again after midpoint() and + // width() of GAOL 4, which may leave it in another direction, whereas + // Codac's own computations on doubles assume it upward (see the + // initialization of oo in codac2_math.h); diam() of the empty set is NaN; + // - rad() and bisect() compute the radius and the cuts themselves; + // - intersects(), is_disjoint(), the subset and superset relations, strict + // and interior, compare the bounds themselves: set_le() of GAOL 4, its + // interior relation, is false for the empty set in the empty set and for + // two intervals with the same infinite bound ([-oo,+oo] in [-oo,+oo]), + // where is_interior_subset() is true; + // - operator& returns Interval::empty() for an empty operand or when + // x.ub() < y.lb(): GAOL 4 gives reversed bounds for disjoint intervals, + // [3,2] for [1,2] & [3,4], which is_empty() takes for empty but the + // operations computing on the bounds do not ([3,2] + [0,1] gives [3,3]); + // - the others (the default constructor, init(), ==, !=, <, + // interior_contains(), inflate(), is_bisectable(), operator-() of an + // interval, the union with a double and empty()) are Codac's code of + // before GAOL v5, which the version without the guards replaces by the + // functions of GAOL v5. + + inline Interval::Interval() + : gaol::interval(-oo,oo) + { } + + inline Interval::Interval(double a) + : gaol::interval(a) + { + if(a == -oo || a == oo) + set_empty(); + } + + inline Interval& Interval::init() + { + *this = Interval(-oo,oo); + return *this; + } + + inline Interval& Interval::operator=(double x) + { + if(x == -oo || x == oo) + set_empty(); + else + gaol::interval::operator=(x); + + return *this; + } + + inline bool Interval::operator==(const Interval& x) const + { + return (is_empty() && x.is_empty()) || (lb() == x.lb() && ub() == x.ub()); + } + + inline bool Interval::operator!=(const Interval& x) const + { + return !(*this == x); + } + + inline BoolInterval Interval::operator<(const Interval& x) const + { + if(is_empty() || x.is_empty()) + return BoolInterval::EMPTY; + if(this->ub() < x.lb()) + return BoolInterval::TRUE; + if(x.ub() <= this->lb()) + return BoolInterval::FALSE; + return BoolInterval::UNKNOWN; + } + + inline double Interval::mid() const + { + double m = gaol::interval::midpoint(); + gaol::round_upward(); + return m; + } + + inline double Interval::rad() const + { + if(is_empty()) + return std::numeric_limits::quiet_NaN(); + + else if(is_unbounded()) + return oo; + + else + { + double t = mid(); + double t1 = (t-*this).ub(); + double t2 = (*this-t).ub(); + return (t1>t2) ? t1 : t2; + } + } + + inline double Interval::diam() const + { + if(is_empty()) + return std::numeric_limits::quiet_NaN(); + + else + { + double d = gaol::interval::width(); + gaol::round_upward(); + return d; + } + } + + inline bool Interval::interior_contains(const double& x) const + { + return !is_empty() && x > lb() && x < ub(); + } + + inline bool Interval::intersects(const Interval &x) const + { + return !is_empty() && !x.is_empty() && lb() <= x.ub() && ub() >= x.lb(); + } + + inline bool Interval::is_disjoint(const Interval& x) const + { + return is_empty() || x.is_empty() || lb() > x.ub() || ub() < x.lb(); + } + + inline bool Interval::is_subset(const Interval& x) const + { + return is_empty() || (!x.is_empty() && x.lb() <= lb() && x.ub() >= ub()); + } + + inline bool Interval::is_strict_subset(const Interval& x) const + { + return !x.is_empty() && (is_empty() || (x.lb() < lb() && x.ub() >= ub()) || (x.ub() > ub() && x.lb() <= lb())); + } + + inline bool Interval::is_interior_subset(const Interval& x) const + { + return is_empty() || (!x.is_empty() && (x.lb() == -oo || x.lb() < lb()) && (x.ub() == oo || x.ub() > ub())); + } + + inline bool Interval::is_strict_interior_subset(const Interval& x) const + { + return !x.is_empty() && (is_empty() || ( + (x.lb() < lb() && (x.ub() == oo || x.ub() > ub())) + || (x.ub() > ub() && (x.lb() == -oo || x.lb() < lb())) + )); + } + + inline bool Interval::is_superset(const Interval& x) const + { + return x.is_subset(*this); + } + + inline bool Interval::is_strict_superset(const Interval& x) const + { + return x.is_strict_subset(*this); + } + + inline Interval& Interval::inflate(const double& rad) + { + (*this) += Interval(-rad,rad); + return *this; + } + + inline bool Interval::is_bisectable() const + { + if(is_empty()) + return false; + double m = mid(); + return lb() < m && m < ub(); + } + + inline std::pair Interval::bisect(float ratio) const + { + assert_release(is_bisectable()); + assert_release(Interval(0,1).interior_contains(ratio)); + + if(lb() == -oo) + { + if(ub() == oo) + return { Interval(-oo,0), Interval(0,oo) }; + else + return { Interval(-oo,-std::numeric_limits::max()), Interval(-std::numeric_limits::max(),ub()) }; + } + + else if(ub() == oo) + return { Interval(lb(),std::numeric_limits::max()), Interval(std::numeric_limits::max(),oo) }; + + else + { + double m; + + if(ratio == 0.5) + m = mid(); + + else + { + m = lb() + ratio*diam(); + if(m >= ub()) + m = next_float(lb()); + + assert(m < ub()); + } + + return { Interval(lb(),m), Interval(m,ub()) }; + } + } + + inline Interval operator&(const Interval& x, const Interval& y) + { + if(x.is_empty() || y.is_empty() || x.ub() < y.lb()) + return Interval::empty(); + + else + return gaol::operator&(x,y); + } + + inline Interval Interval::operator-() const + { + return 0.-*this; + } + + inline Interval operator|(const Interval& x, double y) + { + return gaol::operator|(x,Interval(y)); + } + + inline Interval operator+(const Interval& x, double y) + { + if(y == -oo || y == oo) + return Interval::empty(); + + else + return gaol::operator+(x,y); + } + + inline Interval operator+(double x, const Interval& y) + { + if(x == -oo || x == oo) + return Interval::empty(); + + else + return gaol::operator+(x,y); + } + + inline Interval operator-(const Interval& x, double y) + { + if(y == -oo || y == oo) + return Interval::empty(); + + else + return gaol::operator-(x, y); + } + + inline Interval operator-(double x, const Interval& y) + { + if(x == -oo || x == oo) + return Interval::empty(); + + else + return gaol::operator-(x, y); + } + + inline Interval operator*(const Interval& x, double y) + { + if(y == -oo || y == oo) + return Interval::empty(); + + else + return gaol::operator*(x,y); + } + + inline Interval operator*(double x, const Interval& y) + { + if(x == -oo || x == oo) + return Interval::empty(); + + else return gaol::operator*(x,y); } - inline Interval operator*(const Interval& x, const Interval& y) + inline Interval operator/(const Interval& x, double y) + { + if(y == -oo || y == oo) + return Interval::empty(); + + else + return gaol::operator/(x,y); + } + + inline Interval operator/(double x, const Interval& y) + { + if(x == -oo || x == oo) + return Interval::empty(); + + else + return gaol::operator/(x,y); + } + + inline Interval& Interval::operator+=(double x) + { + if(x == -oo || x == oo) + set_empty(); + else + gaol::interval::operator+=(x); + return *this; + } + + inline Interval& Interval::operator-=(double x) + { + if(x == -oo || x == oo) + set_empty(); + else + gaol::interval::operator-=(x); + return *this; + } + + inline Interval& Interval::operator*=(double x) { - return gaol::operator*(x,y); + if(x == -oo || x == oo) + set_empty(); + else + gaol::interval::operator*=(x); + return *this; } - inline Interval operator/(const Interval& x, double y) + inline Interval& Interval::operator/=(double x) { - if(y == -oo || y == oo) - return Interval::empty(); - + if(x == -oo || x == oo) + set_empty(); else - return gaol::operator/(x,y); + gaol::interval::operator/=(x); + return *this; } - inline Interval operator/(double x, const Interval& y) + + inline Interval Interval::empty() { - if(x == -oo || x == oo) - return Interval::empty(); + return std::numeric_limits::quiet_NaN(); + } - else - return gaol::operator/(x,y); + // The operators with an integer (declared at the end of codac2_Interval.h), + // which GAOL 4 does not have: n converted to a double, as the implicit + // conversion did before they were declared, and the operator with a double + // above. + + template + inline Interval operator+(const Interval& x, T n) + { + return x + static_cast(n); } - inline Interval operator/(const Interval& x, const Interval& y) + template + inline Interval operator+(T n, const Interval& y) { - return gaol::operator/(x,y); + return static_cast(n) + y; } - inline Interval& Interval::operator|=(const Interval& x) + template + inline Interval operator-(const Interval& x, T n) { - gaol::interval::operator|=(x); - return *this; + return x - static_cast(n); } - inline Interval& Interval::operator&=(const Interval& x) + template + inline Interval operator-(T n, const Interval& y) { - gaol::interval::operator&=(x); + return static_cast(n) - y; + } + + template + inline Interval operator*(const Interval& x, T n) + { + return x * static_cast(n); + } + + template + inline Interval operator*(T n, const Interval& y) + { + return static_cast(n) * y; + } + + template + inline Interval operator/(const Interval& x, T n) + { + return x / static_cast(n); + } + + template + inline Interval operator/(T n, const Interval& y) + { + return static_cast(n) / y; + } + +#else + + // Without the guards, for GAOL v5 (https://github.com/Jordan08/GAOL), Codac's + // default: GAOL is called directly, GAOL v5 handling each of the cases above + // itself. An infinite double gives the empty set, every operation leaves the + // rounding upward, width() of the empty set is NaN, the empty intersection is + // the canonical empty set [NaN,NaN] (in operator& and operator&= alike, for + // disjoint intervals in either order), and Codac takes rad() and split() from + // GAOL rather than computing them again. + // + // The relations between two intervals are GAOL's too, those of IEEE + // 1788-2015 (Table 10.3): set_disjoint() (disjoint), set_leq() (subset), + // set_le() (interior), set_geq() (its converse) and set_eq() (equal), the + // strict relations adding !set_eq(). They give the results of the comparisons + // of bounds of the version with the guards, which were compared to them on + // every pair of 36 intervals (the empty set, points, infinite bounds, -0 and + // +0). + // + // So are, giving the same results as Codac's code of before (compared on 78 + // intervals and 15 doubles, NaN among them): the default constructor and + // init() (gaol::interval(), [-oo,+oo], and universe()), == and != (set_eq() + // and set_neq()), < (certainly_le() and certainly_leq()), + // interior_contains() (set_strictly_contains()), inflate() (GAOL's + // inflate(), the sum with [-rad,rad]), is_bisectable(), the union with a + // double (|= of GAOL) and empty() (emptyset()). bisect() is GAOL's as well, + // whose cut points may differ from Codac's in their last bits (see it), and + // so is operator-() of an interval, GAOL's -x, whose zero bounds are -0 where + // Codac's 0.-x gave +0 (see it). + // + // Codac keeps its own functions where GAOL v5 has none: smag(), rand(), + // has_integer_bounds(), overlaps() (no relation of GAOL tells that the + // intersection of two intervals is wider than a point; the state "overlaps" + // of overlap(), al < bl < au < bu, is another relation), complementary() + // and diff(). + + inline Interval::Interval() + : gaol::interval() + { } + + inline Interval::Interval(double a) + : gaol::interval(a) + { } + + inline Interval& Interval::init() + { + gaol::interval::operator=(gaol::interval::universe()); return *this; } - inline Interval& Interval::operator+=(double x) + inline Interval& Interval::operator=(double x) { - if(x == -oo || x == oo) - set_empty(); - else - gaol::interval::operator+=(x); + gaol::interval::operator=(x); return *this; } - inline Interval& Interval::operator+=(const Interval& x) + inline bool Interval::operator==(const Interval& x) const { - gaol::interval::operator+=(x); + return gaol::interval::set_eq(x); + } + + inline bool Interval::operator!=(const Interval& x) const + { + return gaol::interval::set_neq(x); + } + + inline BoolInterval Interval::operator<(const Interval& x) const + { + // certainly_le() and certainly_leq() of GAOL, strictPrecedes and precedes + // of IEEE 1788, are true when an operand is empty: the empty set is told + // first + if(is_empty() || x.is_empty()) + return BoolInterval::EMPTY; + if(gaol::interval::certainly_le(x)) // ub() < x.lb() + return BoolInterval::TRUE; + if(static_cast(x).certainly_leq(*this)) // x.ub() <= lb() + return BoolInterval::FALSE; + return BoolInterval::UNKNOWN; + } + + inline double Interval::mid() const + { + return gaol::interval::midpoint(); + } + + inline double Interval::rad() const + { + // GAOL's rad(), rad of IEEE 1788-2015 (12.12.8), in the fork of GAOL since its + // version 4.3.1: the smallest double r such that this interval is included in + // [m-r,m+r], m being mid(), computed as the greater of m-lb() and ub()-m rounded + // upward; NaN for the empty set, +oo for an unbounded interval. Codac used to + // compute the same bound itself, with two interval subtractions, + // (mid()-*this).ub() and (*this-mid()).ub(), and the same special cases. + return gaol::interval::rad(); + } + + inline double Interval::diam() const + { + return gaol::interval::width(); + } + + inline bool Interval::interior_contains(const double& x) const + { + return gaol::interval::set_strictly_contains(x); + } + + inline bool Interval::intersects(const Interval &x) const + { + return !gaol::interval::set_disjoint(x); + } + + inline bool Interval::is_disjoint(const Interval& x) const + { + return gaol::interval::set_disjoint(x); + } + + inline bool Interval::is_subset(const Interval& x) const + { + return gaol::interval::set_leq(x); + } + + inline bool Interval::is_strict_subset(const Interval& x) const + { + return gaol::interval::set_leq(x) && !gaol::interval::set_eq(x); + } + + inline bool Interval::is_interior_subset(const Interval& x) const + { + return gaol::interval::set_le(x); + } + + inline bool Interval::is_strict_interior_subset(const Interval& x) const + { + return gaol::interval::set_le(x) && !gaol::interval::set_eq(x); + } + + inline bool Interval::is_superset(const Interval& x) const + { + return gaol::interval::set_geq(x); + } + + inline bool Interval::is_strict_superset(const Interval& x) const + { + return gaol::interval::set_geq(x) && !gaol::interval::set_eq(x); + } + + inline Interval& Interval::inflate(const double& rad) + { + gaol::interval::operator=(gaol::interval::inflate(rad)); return *this; } + inline bool Interval::is_bisectable() const + { + return gaol::interval::is_bisectable(); + } + + inline std::pair Interval::bisect(float ratio) const + { + assert_release(is_bisectable()); + assert_release(Interval(0,1).interior_contains(ratio)); + + // GAOL's bisect() (GAOL v5): the two parts of [l,r] on either side of the + // cut point c, which they share, so that they cover this interval exactly. + // In halves, and an unbounded interval whatever the ratio, c is midpoint() + // (mid()), where Codac used to cut them itself: 0 for [-oo,+oo], -MAX for + // [-oo,b] and MAX for [a,+oo], MAX being the largest double. For another ratio, c is l(1-ratio) + r ratio, in GAOL's + // rounding, where Codac computed lb()+ratio*diam(): it does not overflow, + // as diam() of [-MAX,MAX] did (+oo), which made Codac cut that interval at + // -MAX, and when c falls on a bound of a bisectable interval, it is moved + // to the double next to that bound, on the side of the other one, where + // Codac always took the double next to lb(). Elsewhere, the cut points + // differ in their last bits only, in about half of the cases (the parts + // still covering the interval exactly). Codac's assertions are kept: for an + // interval that is not bisectable, GAOL would return two empty sets or the + // point twice, and it throws std::domain_error for a ratio outside (0,1). + const std::pair parts = gaol::interval::bisect(ratio); + return { parts.first, parts.second }; + } + + inline Interval operator&(const Interval& x, const Interval& y) + { + // GAOL is called without a guard. Codac used to return Interval::empty() itself + // when an operand was empty or when x.ub() < y.lb(), because GAOL did not give + // the canonical empty set for disjoint intervals: it kept the largest lower bound + // and the smallest upper bound, [3, 2] for [1, 2] & [3, 4]. is_empty() took such + // an interval for empty, but the operations computing on its bounds did not: + // [3, 2] + [0, 1] gave [3, 3]. That guard also missed disjoint intervals in the + // other order (y.ub() < x.lb()), and did not protect operator&= at all. Since its + // version 4.3.2, the fork of GAOL returns the empty set [NaN, NaN] for every empty + // intersection, empty operands included, in operator& and operator&= alike. + return gaol::operator&(x,y); + } + inline Interval Interval::operator-() const { - return 0.-*this; + // GAOL's -x (GAOL v5): the opposites of the bounds, exactly, where Codac + // computed 0.-x. Both give the same set, but a zero bound changes its sign + // with GAOL: -[0,1] is [-1,-0], written [-1, -0], where 0.-[0,1] gives + // [-1,0] (0-0 is +0). -0 and +0 are the same number for the operations and + // the relations of GAOL and Codac (-0 == 0, Interval(-1,-0) == + // Interval(-1,0)); only the writing and std::signbit() tell them apart. + return gaol::interval::operator-(); } - inline Interval& Interval::operator-=(double x) + inline Interval operator|(const Interval& x, double y) { - if(x == -oo || x == oo) - set_empty(); - else - gaol::interval::operator-=(x); - return *this; + gaol::interval z(x); + z |= y; + return z; } - inline Interval& Interval::operator-=(const Interval& x) + inline Interval operator+(const Interval& x, double y) { - gaol::interval::operator-=(x); - return *this; + return gaol::operator+(x,y); } - inline Interval& Interval::operator*=(double x) + inline Interval operator+(double x, const Interval& y) { - if(x == -oo || x == oo) - set_empty(); - else - gaol::interval::operator*=(x); + return gaol::operator+(x,y); + } + + inline Interval operator-(const Interval& x, double y) + { + return gaol::operator-(x, y); + } + + inline Interval operator-(double x, const Interval& y) + { + return gaol::operator-(x, y); + } + + inline Interval operator*(const Interval& x, double y) + { + return gaol::operator*(x,y); + } + + inline Interval operator*(double x, const Interval& y) + { + return gaol::operator*(x,y); + } + + inline Interval operator/(const Interval& x, double y) + { + return gaol::operator/(x,y); + } + + inline Interval operator/(double x, const Interval& y) + { + return gaol::operator/(x,y); + } + + inline Interval& Interval::operator+=(double x) + { + gaol::interval::operator+=(x); return *this; } - inline Interval& Interval::operator*=(const Interval& x) + inline Interval& Interval::operator-=(double x) { - gaol::interval::operator*=(x); + gaol::interval::operator-=(x); return *this; } - inline Interval& Interval::operator/=(double x) + inline Interval& Interval::operator*=(double x) { - if(x == -oo || x == oo) - set_empty(); - else - gaol::interval::operator/=(x); + gaol::interval::operator*=(x); return *this; } - inline Interval& Interval::operator/=(const Interval& x) + inline Interval& Interval::operator/=(double x) { gaol::interval::operator/=(x); return *this; } + inline Interval Interval::empty() { - return std::numeric_limits::quiet_NaN(); - } - - inline Interval Interval::zero() - { - return gaol::interval::zero(); + return gaol::interval::emptyset(); } - - inline Interval Interval::one() + + // The operators with an integer (declared at the end of codac2_Interval.h): + // GAOL v5's, which compute with the double n where n is one for certain, and + // with the tightest interval containing n otherwise. + + template + inline Interval operator+(const Interval& x, T n) { - return gaol::interval::one(); + return gaol::operator+(static_cast(x), n); } - inline Interval Interval::half_pi() + template + inline Interval operator+(T n, const Interval& y) { - return gaol::interval::half_pi(); + return gaol::operator+(n, static_cast(y)); } - inline Interval Interval::pi() + template + inline Interval operator-(const Interval& x, T n) { - return gaol::interval::pi(); + return gaol::operator-(static_cast(x), n); } - inline Interval Interval::two_pi() + template + inline Interval operator-(T n, const Interval& y) { - return gaol::interval::two_pi(); + return gaol::operator-(n, static_cast(y)); } - inline std::ostream& operator<<(std::ostream& os, const Interval& x) + template + inline Interval operator*(const Interval& x, T n) { - gaol::interval::precision(os.precision()); - gaol::operator<<(os,x); - return os; + return gaol::operator*(static_cast(x), n); } - inline Interval::Interval(const gaol::interval& x) - : gaol::interval(x) - { } - - inline Interval operator""_i(long double x) + template + inline Interval operator*(T n, const Interval& y) { - return Interval(static_cast(x)); + return gaol::operator*(n, static_cast(y)); } - inline double prev_float(double x) + template + inline Interval operator/(const Interval& x, T n) { - return gaol::previous_float(x); + return gaol::operator/(static_cast(x), n); } - inline double next_float(double x) + template + inline Interval operator/(T n, const Interval& y) { - return gaol::next_float(x); + return gaol::operator/(n, static_cast(y)); } + +#endif } diff --git a/src/core/domains/interval/codac2_Interval_operations_impl.h b/src/core/domains/interval/codac2_Interval_operations_impl.h index 53e33a4ac..5d58c9628 100644 --- a/src/core/domains/interval/codac2_Interval_operations_impl.h +++ b/src/core/domains/interval/codac2_Interval_operations_impl.h @@ -23,6 +23,27 @@ namespace codac2 return gaol::sqr(x); } + // --------------------------------------------------------------------------- + // The elementary functions, whose code depends on the GAOL Codac is built on + // --------------------------------------------------------------------------- + // + // CODAC_GAOL_GUARDS (see codac2_Interval.h) chooses between two versions of + // them, as in the block at the end of codac2_Interval_impl.h: the code Codac + // had before GAOL v5, with its guards around GAOL 4, unchanged, and the code + // that calls GAOL v5 directly. +#if CODAC_GAOL_GUARDS + + // With the guards, for GAOL 4, the GAOL that IBEX bundles (WITH_IBEX): + // gaol::round_upward() after each function, which GAOL 4 may leave rounding + // in another direction (see codac2_Interval_impl.h); pow(x,p) empty for an + // infinite p; log of an interval without positive numbers empty, where GAOL 4 + // gives [-oo,-DBL_MAX] for x.ub() == 0; cosh of an unbounded interval made to + // reach +oo; asinh computed from nonnegative arguments, by symmetry; the + // roots of odd order of negative numbers computed by symmetry too, the + // nth_root of GAOL 4 leaving negative numbers out of its domain; atan2 + // computed by Codac; and sign(), which GAOL 4 does not have, computed by + // Codac, a sign of 0 being any of [-1,1]. + inline Interval sqrt(const Interval& x) { Interval y = gaol::sqrt(x); @@ -261,6 +282,154 @@ namespace codac2 return y; } + inline Interval sign(const Interval& x) + { + return x.ub() < 0 ? -1. : x.lb() > 0 ? 1. : Interval(-1.,1.); + } + +#else + + // Without the guards, for GAOL v5 (https://github.com/Jordan08/GAOL), Codac's + // default: GAOL is called directly. GAOL v5 leaves the rounding upward after + // each function, handles each of the cases above itself (its nth_root is the + // rootn of IEEE 1788, which takes the roots of odd order of negative + // numbers), and Codac takes atan2 and sign from it (those of IEEE 1788). + // + // chi(), after this block, stays Codac's in both versions: the chi() of GAOL + // is another function, the degree of symmetry of an interval of Ratschek and + // Rokne (a double, a/b or b/a for [a,b]), where Codac's chi(x,y,z) chooses + // between y and z after the sign of x. + + inline Interval sqrt(const Interval& x) + { + return gaol::sqrt(x); + } + + inline Interval pow(const Interval& x, int p) + { + return gaol::pow(x,p); + } + + inline Interval pow(const Interval& x, double p) + { + return gaol::pow(x,p); + } + + // gaol::pow(x,p) of GAOL v5 is the hull of x^v over x and p, x^v being the + // integer power x^n (pown of IEEE 1788, defined for x < 0 too, 0^0 being 1) + // for an integer v = n of p, and the pow of IEEE 1788 for any other v: + // pow([-2,2],[0.5,3]) is [-8,8], (-2)^3 being -8, and pow([0],[0,1]) is + // [0,1]. GAOL 5.0.0 left the negative bases out for any p but a degenerate + // integer, and gave [0,8] and [0] for these, which GAOL 5.0.1 corrected + // (https://github.com/Jordan08/GAOL/issues/400): Codac asks for GAOL 5.0.2 or + // later (CODAC_GAOL_MIN_VERSION, in scripts/CMakeModules/codac_gaol.cmake). + // PowOp::bwd() takes the reverses of this power, pow_rel() and + // pow_exponent_rel() of GAOL v5 (see codac2_pow.h). + inline Interval pow(const Interval& x, const Interval& p) + { + return gaol::pow(x,p); + } + + inline Interval root(const Interval& x, int p) + { + Interval y = gaol::nth_root(x, p >= 0 ? p : -p); + + if(p < 0) + y = 1.0/y; + + return y; + } + + inline Interval exp(const Interval& x) + { + return gaol::exp(x); + } + + inline Interval log(const Interval& x) + { + return gaol::log(x); + } + + inline Interval cos(const Interval& x) + { + return gaol::cos(x); + } + + inline Interval sin(const Interval& x) + { + return gaol::sin(x); + } + + inline Interval tan(const Interval& x) + { + return gaol::tan(x); + } + + inline Interval acos(const Interval& x) + { + return gaol::acos(x); + } + + inline Interval asin(const Interval& x) + { + return gaol::asin(x); + } + + inline Interval atan(const Interval& x) + { + return gaol::atan(x); + } + + inline Interval atan2(const Interval& y, const Interval& x) + { + return gaol::atan2(y,x); + } + + inline Interval cosh(const Interval& x) + { + return gaol::cosh(x); + } + + inline Interval sinh(const Interval& x) + { + return gaol::sinh(x); + } + + inline Interval tanh(const Interval& x) + { + return gaol::tanh(x); + } + + inline Interval acosh(const Interval& x) + { + return gaol::acosh(x); + } + + inline Interval asinh(const Interval& x) + { + return gaol::asinh(x); + } + + inline Interval atanh(const Interval& x) + { + return gaol::atanh(x); + } + + inline Interval sign(const Interval& x) + { + // The sign of IEEE 1788 (GAOL v5): the hull of the signs of the numbers of + // x, -1 below 0, 0 at 0 (and at -0), 1 above, and the empty set for the + // empty set. Codac's sign, which WITH_IBEX keeps, gave [-1,1] for every x + // containing 0, a sign of 0 being any of [-1,1]: the two differ when 0 is + // a bound of x or x itself, sign([0,5]) being [0,1] instead of [-1,1] and + // sign([0]) [0] instead of [-1,1], and for the empty set. SignOp::bwd() + // (codac2_sign.h) takes the reverse of this sign, sign_rel() of GAOL v5, + // which keeps 0 for the sign 0 only. + return gaol::sign(x); + } + +#endif + inline Interval abs(const Interval& x) { return gaol::abs(x); @@ -276,11 +445,6 @@ namespace codac2 return gaol::max(x,y); } - inline Interval sign(const Interval& x) - { - return x.ub() < 0 ? -1. : x.lb() > 0 ? 1. : Interval(-1.,1.); - } - inline Interval integer(const Interval& x) { return gaol::integer(x); diff --git a/src/core/operators/codac2_atan2.h b/src/core/operators/codac2_atan2.h index 7bae87f9e..e1eb06b1f 100644 --- a/src/core/operators/codac2_atan2.h +++ b/src/core/operators/codac2_atan2.h @@ -84,6 +84,7 @@ namespace codac2 inline void Atan2Op::bwd(const Interval& y, Interval& x1, Interval& x2) { +#if CODAC_GAOL_GUARDS // The content of this function comes from the IBEX library. // See ibex::Interval (IBEX lib, main author: Gilles Chabert) // https://ibex-lib.readthedocs.io @@ -152,5 +153,23 @@ namespace codac2 x2 = xres; x1 = yres; +#else + // atan2_rel_y() and atan2_rel_x() of GAOL v5, atan2Rev1 and atan2Rev2 of + // IEEE 1788-2015: the hull of the ordinates x1, then of the abscissas x2 + // (with the x1 just contracted), of the points of the box x1 x x2 whose + // angle atan2(x1,x2) is in y, the point (0,0), which has no angle, left + // out. The angles are those of atan2, in (-pi,pi]: the half-line x1 = 0, + // x2 < 0 has the angle pi. The bounds are within 2 doubles of the tightest + // ones. The code of IBEX above, compared on 41 x 41 boxes and 27 intervals + // of angles, gave the same intervals or wider ones, and lost solutions on + // the line x2 = 0: for y = [1.5,1.6], which holds pi/2, x1 = [1] and x2 = + // [0], where atan2(1,0) = pi/2, it gave the empty set, x1/x2 being empty + // for x2 = [0] and pi/2 being only taken into account when it was the + // lower bound of y. + x1 = gaol::atan2_rel_y(y, x2, x1); + x2 = gaol::atan2_rel_x(y, x1, x2); + if(x2.is_empty()) + x1.set_empty(); +#endif } } diff --git a/src/core/operators/codac2_ceil.h b/src/core/operators/codac2_ceil.h index df5866ab3..f08247ebc 100644 --- a/src/core/operators/codac2_ceil.h +++ b/src/core/operators/codac2_ceil.h @@ -71,6 +71,7 @@ namespace codac2 inline void CeilOp::bwd(const Interval& y, Interval& x1) { +#if CODAC_GAOL_GUARDS // The content of this function comes from the IBEX library. // See ibex::Interval (IBEX lib, main author: Gilles Chabert) // https://ibex-lib.readthedocs.io @@ -89,5 +90,18 @@ namespace codac2 else x1 &= Interval(l,r) + Interval(-1,0); } +#else + // GAOL v5 has no reverse of ceil, but ceil(x) = -floor(-x) for every real + // x: the x of x1 whose ceil is in y are the opposites of the u of -x1 whose + // floor is in -y, which floor_rel() of GAOL v5 gives (see FloorOp::bwd()), + // the opposite of an interval being exact. The result is the hull of the + // part of x1 in (ceil(y.lb())-1,floor(y.ub())], and the empty set when y + // holds no integer or one of y and x1 is empty. The code of IBEX above + // computed the same interval, closed at ceil(y.lb())-1, whose ceil is not + // in y: GAOL leaves that end out where x1 ends there (the reverse of ceil + // for y = [1] and x1 = [0] is empty, where IBEX gave [0]). A zero bound of + // the result may be -0, the opposite of +0, which is the same number. + x1 = -gaol::floor_rel(-y, -x1); +#endif } } diff --git a/src/core/operators/codac2_floor.h b/src/core/operators/codac2_floor.h index 12d99679e..a428c285e 100644 --- a/src/core/operators/codac2_floor.h +++ b/src/core/operators/codac2_floor.h @@ -74,6 +74,7 @@ namespace codac2 inline void FloorOp::bwd(const Interval& y, Interval& x1) { +#if CODAC_GAOL_GUARDS // The content of this function comes from the IBEX library. // See ibex::Interval (IBEX lib, main author: Gilles Chabert) // https://ibex-lib.readthedocs.io @@ -92,5 +93,17 @@ namespace codac2 else x1 &= Interval(l,r) + Interval(0,1); } +#else + // floor_rel() of GAOL v5, the reverse of floor: the hull of the x of x1 + // whose floor is in y, the part of x1 in [ceil(y.lb()),floor(y.ub())+1), + // and the empty set when y holds no integer or one of y and x1 is empty. + // The code of IBEX above computed the same interval, closed at + // floor(y.ub())+1, whose floor is not in y: GAOL leaves that end out where + // x1 starts there (floor_rel([0],[1]) is empty, where IBEX gave [1]) and + // keeps it elsewhere, in the tightest closed enclosure of the set, the + // reals just below it being in the set. floor(y.ub())+1 is rounded upward + // beyond 2^53, where it is no double. + x1 = gaol::floor_rel(y, x1); +#endif } } diff --git a/src/core/operators/codac2_max.h b/src/core/operators/codac2_max.h index 514974f9f..368d21eea 100644 --- a/src/core/operators/codac2_max.h +++ b/src/core/operators/codac2_max.h @@ -82,6 +82,7 @@ namespace codac2 inline void MaxOp::bwd(const Interval& y, Interval& x1, Interval& x2) { +#if CODAC_GAOL_GUARDS // The content of this function comes from the IBEX library. // See ibex::Interval (IBEX lib, main author: Gilles Chabert) // https://ibex-lib.readthedocs.io @@ -122,5 +123,20 @@ namespace codac2 if(x2.ub() > y.ub()) x2 = Interval(x2.lb(),y.ub()); } +#else + // max_rel() of GAOL v5, the reverse of max: max_rel(y,x2,x1) is the hull of + // the x of x1 with max(x,v) in y for some v of x2 (the result, the other + // operand, then the operand it contracts), and x2 is contracted in turn + // with the x1 just contracted. Each operand is empty when y lies below it, + // contracted to y when the other operand lies below y, and to [-oo,y.ub()] + // when the other operand meets y. The bounds are those of x1, x2 and y, the + // tightest. The code of IBEX above gave the same intervals, but left an + // operand as it was when the other one was empty, where GAOL gives the + // empty set for both (compared on every triple of 41 intervals). + x1 = gaol::max_rel(y, x2, x1); + x2 = gaol::max_rel(y, x1, x2); + if(x2.is_empty()) + x1.set_empty(); +#endif } } diff --git a/src/core/operators/codac2_min.h b/src/core/operators/codac2_min.h index a8fee3df0..0791e565e 100644 --- a/src/core/operators/codac2_min.h +++ b/src/core/operators/codac2_min.h @@ -82,6 +82,7 @@ namespace codac2 inline void MinOp::bwd(const Interval& y, Interval& x1, Interval& x2) { +#if CODAC_GAOL_GUARDS // The content of this function comes from the IBEX library. // See ibex::Interval (IBEX lib, main author: Gilles Chabert) // https://ibex-lib.readthedocs.io @@ -102,5 +103,17 @@ namespace codac2 x1 = -mx1; x2 = -mx2; } +#else + // min_rel() of GAOL v5, the reverse of min, max_rel() turned upside down + // (see MaxOp::bwd()): min_rel(y,x2,x1) is the hull of the x of x1 with + // min(x,v) in y for some v of x2, and x2 is contracted in turn with the x1 + // just contracted. The code of IBEX above, the reverse of max on the + // opposites, gave the same intervals (compared on every triple of 41 + // intervals). + x1 = gaol::min_rel(y, x2, x1); + x2 = gaol::min_rel(y, x1, x2); + if(x2.is_empty()) + x1.set_empty(); +#endif } } diff --git a/src/core/operators/codac2_pow.h b/src/core/operators/codac2_pow.h index 01c44aa0e..ab77b44c6 100644 --- a/src/core/operators/codac2_pow.h +++ b/src/core/operators/codac2_pow.h @@ -119,6 +119,7 @@ namespace codac2 inline void PowOp::bwd(const Interval& y, Interval& x1, int x2) { +#if CODAC_GAOL_GUARDS // The content of this function comes from the IBEX library. // See ibex::Interval (IBEX lib, main author: Gilles Chabert) // https://ibex-lib.readthedocs.io @@ -133,12 +134,67 @@ namespace codac2 else x1 &= root(y, x2); +#else + // nth_root_rel() of GAOL v5, pownRev of IEEE 1788-2015, the reverse of the + // integer power x^x2 that pow(x,int) computes (pown of IEEE 1788): the hull + // of the x of x1 with x^x2 defined and in y, for every integer x2. + // - x2 >= 1: the same intervals as the code of IBEX above (compared on + // every pair of 41 intervals, for x2 from 1 to 5). + // - x2 = 0: x^0 is 1 for every x, 0 included, so that x1 is kept where y + // holds 1 and emptied elsewhere. The code of IBEX emptied x1 in every + // case, the root of order 0 being the empty set: it lost every solution + // (x1 = [-oo,+oo] and y = [-oo,+oo] gave the empty set). + // - x2 < 0: x^x2 = 1/x^|x2| has no value at 0, which is left out, and y is + // cut at 0 before the roots are taken, where the code of IBEX took the + // inverse of the root of the whole of y, whose hull may span the gap + // between its two parts: for y = [-1,2], x2 = -3 and x1 = [-0.5,0.5], + // GAOL gives the empty set (|x^-3| >= 8 on x1) and IBEX [-0.5,0.5]. + x1 = gaol::nth_root_rel(y, x2, x1); +#endif } inline void PowOp::bwd(const Interval& y, Interval& x1, Interval& x2) { +#if CODAC_GAOL_GUARDS assert_release(x2.is_degenerated() && "PowOp::bwd(y,x1,x2) (with x1 and x2 intervals) not implemented yet with Gaol"); assert_release(x2 == (int)(x2.mid()) && "PowOp::bwd(y,x1,x2) (x2 not integer) not implemented yet with Gaol"); PowOp::bwd(y, x1, (int)(x2.mid())); +#else + // pow(x1,x2) is the power gaol::pow(x1,x2) of GAOL v5 (see PowOp::fwd()), + // the hull over the box of x^v, where x^v is + // - for an integer v = n: the integer power x^n, pown of IEEE 1788, which + // is defined for a negative x too, is 1 at n = 0 for every x, 0 + // included, and has no value at x = 0 for n < 0; + // - for any other v: the pow of IEEE 1788, x^v = e^(v log x) for x > 0, + // 0 for x = 0 and v > 0, and no value elsewhere. + // pow_rel() and pow_exponent_rel() of GAOL v5 are its reverses, for the + // same x^v: the hull of the bases x of x1 with x^v in y for some v of x2, + // then of the exponents v of x2 with x^v in y for some x of the x1 just + // contracted. They cover every exponent: a degenerate integer x2 = [n] + // gives the reverse of x^n, as bwd(y,x1,int) above, beyond the ints too + // (n >= 2^31 or n < -2^31), and the negative bases are kept for the + // integers of x2: (-1,3) is a solution of y = [-1] in the box x1 = [-2,2], + // x2 = [0.5,3], which x1 = [-1] and x2 = [1,3] hold. + // + // This needs GAOL 5.0.1 (https://github.com/Jordan08/GAOL/issues/400) or + // later: Codac asks for 5.0.2 (CODAC_GAOL_MIN_VERSION, in + // scripts/CMakeModules/codac_gaol.cmake). In GAOL 5.0.0, gaol::pow() took + // the pow of IEEE 1788 for any exponent but a degenerate integer, which left + // out the negative bases of the integers of x2 (gaol::pow([-2,2],[0.5,3]) + // was [0,8], although (-1)^3 = -1), and pow_rel() and pow_exponent_rel() + // reversed the pow of IEEE 1788 only: x1 = [-3,-1] was emptied for y = + // [-27,-1] and x2 = [3]. With GAOL 4, Codac only contracted x1 for an x2 [n] + // within the ints (the assertions above). + x1 = gaol::pow_rel(y, x2, x1); + x2 = gaol::pow_exponent_rel(y, x1, x2); + + // No base of x1 with an exponent of x2, or the converse: the box holds no + // solution + if(x1.is_empty() || x2.is_empty()) + { + x1.set_empty(); + x2.set_empty(); + } +#endif } } diff --git a/src/core/operators/codac2_sign.h b/src/core/operators/codac2_sign.h index 2d048bbe8..60499289b 100644 --- a/src/core/operators/codac2_sign.h +++ b/src/core/operators/codac2_sign.h @@ -74,6 +74,7 @@ namespace codac2 inline void SignOp::bwd(const Interval& y, Interval& x1) { +#if CODAC_GAOL_GUARDS if(y.is_empty()) x1.set_empty(); @@ -82,5 +83,20 @@ namespace codac2 else if(y.ub() < 0) x1 &= Interval(-oo,0); +#else + // sign_rel() of GAOL v5, the reverse of the sign of GAOL v5 that + // sign(const Interval&) is (see codac2_Interval_operations_impl.h): the hull + // of the negative x of x1 where y holds -1, of 0 where y and x1 hold it, + // and of the positive x of x1 where y holds 1, and the empty set where y + // holds none of -1, 0 and 1: sign_rel([0.5],x1) is empty, and + // sign_rel([0],[-2,3]) is [0]. The negative numbers go up to 0 and the + // positive ones down from 0 in their closed hull: sign_rel([-1],[-2,3]) is + // [-2,0]. The code above, written for Codac's former sign, a sign of 0 + // being any of [-1,1], only contracted x1 to [0,+oo] when y was positive and + // to [-oo,0] when y was negative: it kept the whole of x1 for y = [0] and + // for y = [0,+oo] ([-2,3] for x1 = [-2,3]), and the part of x1 in [0,+oo] + // for y = [0.5]. + x1 = gaol::sign_rel(y, x1); +#endif } } diff --git a/src/core/operators/codac2_sinh.h b/src/core/operators/codac2_sinh.h index 789f28f42..40d9590ad 100644 --- a/src/core/operators/codac2_sinh.h +++ b/src/core/operators/codac2_sinh.h @@ -78,6 +78,14 @@ namespace codac2 inline void SinhOp::bwd(const Interval& y, Interval& x1) { +#if CODAC_GAOL_GUARDS x1 &= asinh(y); +#else + // asinh_rel() of GAOL v5, the reverse of sinh: the hull of the x of x1 + // whose sinh is in y. sinh being a bijection of R, it is x1 & asinh(y), the + // code above, which gives the same intervals (compared on every pair of 41 + // intervals); GAOL computes it in one function. + x1 = gaol::asinh_rel(y, x1); +#endif } } diff --git a/src/extensions/capd/CMakeLists.txt b/src/extensions/capd/CMakeLists.txt index 72c364450..b0e2ccb54 100644 --- a/src/extensions/capd/CMakeLists.txt +++ b/src/extensions/capd/CMakeLists.txt @@ -22,7 +22,7 @@ list(APPEND CODAC_CAPD_SRC #endif() add_library(${PROJECT_NAME}-capd ${CODAC_CAPD_SRC}) - target_link_libraries(${PROJECT_NAME}-capd PUBLIC ${PROJECT_NAME}-core ${PROJECT_NAME}-eigen Ibex::ibex capd::capd) # Eigen3::Eigen + target_link_libraries(${PROJECT_NAME}-capd PUBLIC ${PROJECT_NAME}-core ${PROJECT_NAME}-eigen Codac::gaol capd::capd) # Eigen3::Eigen ################################################################################ diff --git a/src/extensions/sympy/CMakeLists.txt b/src/extensions/sympy/CMakeLists.txt index 00e893260..139c9cb8e 100644 --- a/src/extensions/sympy/CMakeLists.txt +++ b/src/extensions/sympy/CMakeLists.txt @@ -31,7 +31,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) add_library(${PROJECT_NAME}-sympy ${CODAC_SYMPY_SRC}) - target_link_libraries(${PROJECT_NAME}-sympy PUBLIC ${PROJECT_NAME}-core ${PROJECT_NAME}-eigen Ibex::ibex) # Eigen3::Eigen + target_link_libraries(${PROJECT_NAME}-sympy PUBLIC ${PROJECT_NAME}-core ${PROJECT_NAME}-eigen Codac::gaol) # Eigen3::Eigen target_link_libraries(${PROJECT_NAME}-sympy PRIVATE pybind11::pybind11) ################################################################################ diff --git a/src/graphics/CMakeLists.txt b/src/graphics/CMakeLists.txt index 3a08363f5..a31dd76f2 100644 --- a/src/graphics/CMakeLists.txt +++ b/src/graphics/CMakeLists.txt @@ -53,7 +53,7 @@ ${CMAKE_CURRENT_SOURCE_DIR}/paver # deprecated, to be removed ${CMAKE_CURRENT_SOURCE_DIR}/styles ) - target_link_libraries(${PROJECT_NAME}-graphics PUBLIC ${PROJECT_NAME}-core ${PROJECT_NAME}-eigen Ibex::ibex ${PROJECT_NAME}-core) # Eigen3::Eigen + target_link_libraries(${PROJECT_NAME}-graphics PUBLIC ${PROJECT_NAME}-core ${PROJECT_NAME}-eigen Codac::gaol ${PROJECT_NAME}-core) # Eigen3::Eigen ################################################################################ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d933ab001..f8a95c009 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -109,7 +109,7 @@ list(APPEND SRC_TESTS # listing files without extension core/tools/codac2_tests_serialization core/tools/codac2_tests_transformations core/tools/codac2_tests_trunc - core/tools/ibex/codac2_tests_ibex + # core/tools/ibex/codac2_tests_ibex is added below, with WITH_IBEX only ../doc/manual/manual/tools/src core/trajectory/codac2_tests_AnalyticTraj @@ -153,6 +153,17 @@ if (WITH_CAPD) set(CODAC_LIBRARIES ${CODAC_LIBRARIES} ${PROJECT_NAME}-capd capd::capd) endif() +# Test of the conversions between Codac and IBEX, with WITH_IBEX only (see the +# top-level CMakeLists.txt): it includes the headers of IBEX, which Codac is +# not built with otherwise (the conversions it tests are only compiled when a +# program includes IBEX before them). IBEX comes with the Codac libraries, +# through Codac::gaol. +if (WITH_IBEX) + list(APPEND SRC_TESTS + core/tools/ibex/codac2_tests_ibex + ) +endif() + # IBEX test #if (WITH_IBEX) #find_package(IBEX REQUIRED) @@ -169,7 +180,7 @@ foreach(SRC_TEST ${SRC_TESTS}) add_executable(${TEST_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${SRC_TEST}.cpp) set(CODAC_HEADERS_DIR ${CMAKE_CURRENT_BINARY_DIR}/../include) target_include_directories(${TEST_NAME} SYSTEM PUBLIC ${CODAC_HEADERS_DIR}) - target_link_libraries(${TEST_NAME} PUBLIC Ibex::ibex ${CODAC_LIBRARIES} PRIVATE Catch2::Catch2WithMain) + target_link_libraries(${TEST_NAME} PUBLIC ${CODAC_LIBRARIES} PRIVATE Catch2::Catch2WithMain) if(WITH_PYTHON AND SRC_TEST MATCHES "extensions/sympy/") target_link_libraries(${TEST_NAME} PRIVATE ${PROJECT_NAME}-sympy pybind11::embed) @@ -180,6 +191,13 @@ foreach(SRC_TEST ${SRC_TESTS}) # Python test if(WITH_PYTHON) add_test(NAME ${TEST_NAME}_py COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/${SRC_TEST}.py) + if(WITH_IBEX) + # The C++ tests tell the GAOL Codac is built on from CODAC_GAOL_GUARDS + # (codac2_Interval.h); the Python ones, which only see the codac module, + # from this variable, for the functions whose results differ between the + # two (the sign of 0, see core/functions/analytic/codac2_tests_AnalyticFunction.py) + set_tests_properties(${TEST_NAME}_py PROPERTIES ENVIRONMENT "CODAC_GAOL_GUARDS=1") + endif() endif() endforeach() diff --git a/tests/core/functions/analytic/codac2_tests_AnalyticFunction.cpp b/tests/core/functions/analytic/codac2_tests_AnalyticFunction.cpp index 96a179aed..81748a9ad 100644 --- a/tests/core/functions/analytic/codac2_tests_AnalyticFunction.cpp +++ b/tests/core/functions/analytic/codac2_tests_AnalyticFunction.cpp @@ -377,9 +377,19 @@ TEST_CASE("AnalyticFunction") { AnalyticFunction f({x1}, 2*sign(x1+1)); CHECK(f.eval(0.) == 2.); +#if CODAC_GAOL_GUARDS + // Codac's sign, kept with GAOL 4 (WITH_IBEX): a sign of 0 is any of [-1,1] CHECK(sign(Interval::zero()) == Interval(-1,1)); CHECK(sign(Interval(-0,0)) == Interval(-1,1)); CHECK(f.eval(-1.) == Interval(-2,2)); +#else + // The sign of IEEE 1788, which Codac takes from GAOL v5: sign(0) = 0 + CHECK(sign(Interval::zero()) == 0.); + CHECK(sign(Interval(-0,0)) == 0.); + CHECK(sign(Interval(0,5)) == Interval(0,1)); + CHECK(sign(Interval(-5,5)) == Interval(-1,1)); + CHECK(f.eval(-1.) == 0.); +#endif CHECK(f.eval(-2.) == -2.); } diff --git a/tests/core/functions/analytic/codac2_tests_AnalyticFunction.py b/tests/core/functions/analytic/codac2_tests_AnalyticFunction.py index 7704aa720..4f0a47290 100644 --- a/tests/core/functions/analytic/codac2_tests_AnalyticFunction.py +++ b/tests/core/functions/analytic/codac2_tests_AnalyticFunction.py @@ -326,9 +326,19 @@ def test_eval(i,f,*args): f = AnalyticFunction([x1], 2*sign(x1+1)) self.assertTrue(f.eval(0.) == 2.) - self.assertTrue(sign(Interval.zero()) == Interval(-1,1)) - self.assertTrue(sign(Interval(-0,0)) == Interval(-1,1)) - self.assertTrue(f.eval(-1.) == Interval(-2,2)) + if os.environ.get("CODAC_GAOL_GUARDS") == "1": + # Codac's sign, kept with GAOL 4 (WITH_IBEX, whose tests are run with + # CODAC_GAOL_GUARDS=1, see tests/CMakeLists.txt): a sign of 0 is any of [-1,1] + self.assertTrue(sign(Interval.zero()) == Interval(-1,1)) + self.assertTrue(sign(Interval(-0,0)) == Interval(-1,1)) + self.assertTrue(f.eval(-1.) == Interval(-2,2)) + else: + # The sign of IEEE 1788, which Codac takes from GAOL v5: sign(0) = 0 + self.assertTrue(sign(Interval.zero()) == 0.) + self.assertTrue(sign(Interval(-0,0)) == 0.) + self.assertTrue(sign(Interval(0,5)) == Interval(0,1)) + self.assertTrue(sign(Interval(-5,5)) == Interval(-1,1)) + self.assertTrue(f.eval(-1.) == 0.) self.assertTrue(f.eval(-2.) == -2.) f = AnalyticFunction([x1], 2*floor(x1)) diff --git a/tests/core/operators/codac2_tests_operators.cpp b/tests/core/operators/codac2_tests_operators.cpp index 9b3277d15..605d0cf60 100644 --- a/tests/core/operators/codac2_tests_operators.cpp +++ b/tests/core/operators/codac2_tests_operators.cpp @@ -331,6 +331,86 @@ TEST_CASE("Interval bwd operations") x = Interval(-1.57079632679489678, 1.1780972450961728626); CHECK(!(tan(x).is_empty())); + +#if !CODAC_GAOL_GUARDS + // The reverse functions of GAOL v5 that the bwd() of the operators call + // (see codac2_Interval.h), in the cases where the code of IBEX that Codac + // keeps with GAOL 4 (WITH_IBEX) gave wider intervals or lost solutions + + // floor(1) = 1 and ceil(0) = 0: the closed end of the code of IBEX is left out + x = Interval(1); FloorOp::bwd(Interval(0),x); CHECK(x == Interval::empty()); + x = Interval(0); CeilOp::bwd(Interval(1),x); CHECK(x == Interval::empty()); + + // x^0 = 1 for every x: the code of IBEX emptied x1 for any y + x = Interval(-oo,oo); PowOp::bwd(Interval(-oo,oo),x,0); CHECK(x == Interval(-oo,oo)); + x = Interval(-oo,oo); PowOp::bwd(Interval(2,3),x,0); CHECK(x == Interval::empty()); + // |x^-3| >= 8 on [-0.5,0.5], whereas the inverse of the root of [-1,2] spans R + x = Interval(-0.5,0.5); PowOp::bwd(Interval(-1,2),x,-3); CHECK(x == Interval::empty()); + // 0^-1 has no value + x = Interval(0); PowOp::bwd(Interval(-oo,oo),x,-1); CHECK(x == Interval::empty()); + + // An exponent that is an interval: pow_rel() and pow_exponent_rel(), where + // Codac asserted that the exponent was a degenerate integer + { + Interval x1(0,10), x2(0.5,3); + PowOp::bwd(Interval(4,9),x1,x2); + CHECK(Approx(x1) == Interval(std::cbrt(4.),10)); // x1^3 >= 4 and 10^0.954 = 9 + CHECK(Approx(x2) == Interval(std::log(4.)/std::log(10.),3)); // 10^x2 >= 4 + } + // x^n, n an integer, is the integer power, which has negative bases too: + // pow(x1,x2) holds (-2)^3 = -8 and (-2)^2 = 4 on this box, and (-1,1) and + // (-1,3) are the solutions of x1^x2 = -1 in it, (-2,3) the only one of + // x1^x2 = -8 + CHECK(PowOp::fwd(Interval(-2,2),Interval(0.5,3)) == Interval(-8,8)); + CHECK(PowOp::fwd(Interval(-2,-1),Interval(0.5,3)) == Interval(-8,4)); + { + Interval x1(-2,2), x2(0.5,3); + PowOp::bwd(Interval(-1),x1,x2); + CHECK(x1 == -1.); + CHECK(x2 == Interval(1,3)); + } + { + Interval x1(-3,3), x2(0.5,3); + PowOp::bwd(Interval(-8),x1,x2); + CHECK(x1 == -2.); + CHECK(x2 == 3.); + } + // 0^0 is 1, 0^v is 0 for v > 0 + CHECK(PowOp::fwd(Interval(0),Interval(0,1)) == Interval(0,1)); + { + Interval x1(0), x2(0,1); + PowOp::bwd(Interval(1),x1,x2); + CHECK(x1 == 0.); + CHECK(x2 == 0.); + } + // A degenerate integer exponent keeps the negative bases + { + Interval x1(-3,-1), x2(2); + PowOp::bwd(Interval(4,9),x1,x2); + CHECK(x1 == Interval(-3,-2)); + CHECK(x2 == 2.); + } + + // The sign of IEEE 1788: sign(x) is -1, 0 or 1 + x = Interval(-2,3); SignOp::bwd(Interval(0),x); CHECK(x == 0.); + x = Interval(-2,3); SignOp::bwd(Interval(0.5),x); CHECK(x == Interval::empty()); + x = Interval(-2,3); SignOp::bwd(Interval(-1),x); CHECK(x == Interval(-2,0)); + x = Interval(-2.5,-1.5); SignOp::bwd(Interval(0,oo),x); CHECK(x == Interval::empty()); + + // An empty operand empties the other one + { + Interval x1 = Interval::empty(), x2(-2.5); + MaxOp::bwd(Interval(-oo,oo),x1,x2); + CHECK(x1.is_empty()); CHECK(x2.is_empty()); + } + + // atan2(1,0) = pi/2, in a = [1.5,1.6]: the code of IBEX gave the empty set + a = Interval(1.5,1.6); y = Interval(1); x = Interval(0); + Atan2Op::bwd(a,y,x); CHECK(y == 1.); CHECK(x == 0.); + // (0,0) has no angle + a = Interval(); y = Interval(0); x = Interval(0); + Atan2Op::bwd(a,y,x); CHECK(y.is_empty()); CHECK(x.is_empty()); +#endif } #include diff --git a/tests/test_codac/CMakeLists.txt b/tests/test_codac/CMakeLists.txt index b9c77d4fa..fd82398c7 100644 --- a/tests/test_codac/CMakeLists.txt +++ b/tests/test_codac/CMakeLists.txt @@ -8,17 +8,6 @@ set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) -# Adding IBEX - - # In case you installed IBEX in a local directory, you need - # to specify its path with the CMAKE_PREFIX_PATH option. - # set(CMAKE_PREFIX_PATH "~/ibex-lib/build_install") - set(CMAKE_PREFIX_PATH "../ibex") - - find_package(IBEX REQUIRED) - ibex_init_common() # IBEX should have installed this function - message(STATUS "Found IBEX version ${IBEX_VERSION}") - # Adding Codac # In case you installed Codac in a local directory, you need @@ -39,4 +28,4 @@ add_executable(${PROJECT_NAME} main.cpp) target_compile_options(${PROJECT_NAME} PUBLIC ${CODAC_CXX_FLAGS}) target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC ${CODAC_INCLUDE_DIRS}) - target_link_libraries(${PROJECT_NAME} PUBLIC ${CODAC_LIBRARIES} Ibex::ibex) + target_link_libraries(${PROJECT_NAME} PUBLIC ${CODAC_LIBRARIES}) From fd64bcd39e213c9feb934056a9c37b967afa2765 Mon Sep 17 00:00:00 2001 From: Jordan08 Date: Sat, 19 Sep 2026 13:37:35 +0200 Subject: [PATCH 3/6] Hand Threads to the users of codac-config.cmake --- CMakeLists.txt | 5 +++++ python/src/core/CMakeLists.txt | 2 +- python/src/graphics/CMakeLists.txt | 2 +- python/src/unsupported/CMakeLists.txt | 2 +- src/CMakeLists.txt | 13 +++++++++++-- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 865a34137..605aa8681 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -246,6 +246,11 @@ # Looking for Threads ################################################################################ + # The same preference the generated codac-config.cmake asks of consumers, so + # that both sides of a link end up with the same answer: without it this + # build could settle on -lpthread while a consumer, which does set it, got + # -pthread. + set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) ################################################################################ diff --git a/python/src/core/CMakeLists.txt b/python/src/core/CMakeLists.txt index ecbba91cc..6175f7291 100644 --- a/python/src/core/CMakeLists.txt +++ b/python/src/core/CMakeLists.txt @@ -160,7 +160,7 @@ ) target_link_libraries(_core - PRIVATE ${PROJECT_NAME}-core ${PROJECT_NAME}-sympy ${LIBS} Codac::gaol + PRIVATE ${PROJECT_NAME}-core ${PROJECT_NAME}-sympy Threads::Threads Codac::gaol ) # Copy the generated library in the package folder diff --git a/python/src/graphics/CMakeLists.txt b/python/src/graphics/CMakeLists.txt index 0b4386d75..79f7639f8 100644 --- a/python/src/graphics/CMakeLists.txt +++ b/python/src/graphics/CMakeLists.txt @@ -28,7 +28,7 @@ ) target_link_libraries(_graphics - PRIVATE ${PROJECT_NAME}-graphics ${LIBS} Codac::gaol + PRIVATE ${PROJECT_NAME}-graphics Threads::Threads Codac::gaol ) # Copy the generated library in the package folder diff --git a/python/src/unsupported/CMakeLists.txt b/python/src/unsupported/CMakeLists.txt index 861175754..15bccec3a 100644 --- a/python/src/unsupported/CMakeLists.txt +++ b/python/src/unsupported/CMakeLists.txt @@ -16,7 +16,7 @@ ) target_link_libraries(_unsupported - PRIVATE ${PROJECT_NAME}-unsupported ${LIBS} Codac::gaol + PRIVATE ${PROJECT_NAME}-unsupported Threads::Threads Codac::gaol ) # Copy the generated library in the package folder diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 05fc1d03a..e60995a64 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -114,8 +114,17 @@ ${CODAC_GAOL_CONFIG_SNIPPET} endfunction() endif() + # codac-core links PUBLIC against Threads::Threads (see src/core/CMakeLists.txt), + # e.g. for peibos/threading code. That requirement doesn't come back through + # find_library() above (a plain path has no transitive usage requirements), + # so it has to be re-declared here -- otherwise any downstream target that + # actually pulls in that code (such as the 11_peibos example) fails to link + # with an undefined reference to pthread_create. + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) + set(CODAC_VERSION ${PROJECT_VERSION}) - set(CODAC_LIBRARIES \${CODAC_CORE_LIBRARY} \${CODAC_GRAPHICS_LIBRARY} \${CODAC_UNSUPPORTED_LIBRARY} Codac::gaol) + set(CODAC_LIBRARIES \${CODAC_CORE_LIBRARY} \${CODAC_GRAPHICS_LIBRARY} \${CODAC_UNSUPPORTED_LIBRARY} Codac::gaol Threads::Threads) set(CODAC_INCLUDE_DIRS \${CODAC_CORE_INCLUDE_DIR}/../ \${CODAC_EIGEN_INCLUDE_DIR} \${CODAC_CORE_INCLUDE_DIR} \${CODAC_GRAPHICS_INCLUDE_DIR} \${CODAC_UNSUPPORTED_INCLUDE_DIR}) set(CODAC_C_FLAGS \"\") @@ -156,7 +165,7 @@ ${CODAC_GAOL_CONFIG_SNIPPET} endif() file(APPEND ${CODAC_CMAKE_CONFIG_FILE} " - set(CODAC_LIBRARIES \${CODAC_LIBRARIES} \${CODAC_GRAPHICS_LIBRARY} \${CODAC_CORE_LIBRARY} Codac::gaol) + set(CODAC_LIBRARIES \${CODAC_LIBRARIES} \${CODAC_GRAPHICS_LIBRARY} \${CODAC_CORE_LIBRARY} Codac::gaol Threads::Threads) ") From 5bfc8a141372ad469735efc5b747622023ded8fd Mon Sep 17 00:00:00 2001 From: Jordan08 Date: Sat, 19 Sep 2026 13:37:35 +0200 Subject: [PATCH 4/6] Enable ASan/UBSan in Debug builds when the toolchain ships the runtime --- CMakeLists.txt | 200 ++++++++++++++++++++ python/CMakeLists.txt | 4 +- python/src/core/CMakeLists.txt | 56 ++++++ scripts/CMakeModules/codac_gaol.cmake | 15 +- tests/CMakeLists.txt | 260 +++++++++++++++++++++++++- 5 files changed, 525 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 605aa8681..04f4a5dcb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -93,6 +93,206 @@ add_compile_options(-Wall -Wextra -Wpedantic) endif() + if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_CONFIGURATION_TYPES) + # CMAKE_BUILD_TYPE only has meaning for single-configuration generators + # (Makefiles, Ninja): multi-configuration generators (Visual Studio, + # Xcode, Ninja Multi-Config -- notably what this project's own CI + # drives MSVC through, via `-G "Visual Studio 17/18"` in + # .github/workflows/*matrix.yml) instead select the actual + # configuration later, at build time (cmake --build . --config + # Debug), not at configure time; CMAKE_BUILD_TYPE stays whatever it + # was defaulted to above ("Release") regardless of what configuration + # is later built. So this outer if() only decides whether it is worth + # *probing* for Debug-only capabilities at all -- true either when a + # single-config build was explicitly configured as Debug, or when a + # multi-config generator defers that choice (CMAKE_CONFIGURATION_TYPES + # is then non-empty) and so might still end up building a Debug + # configuration later. The actual compile/link options below are + # separately wrapped in a $ generator expression, which + # -- unlike CMAKE_BUILD_TYPE -- CMake evaluates correctly against + # whichever configuration is ultimately built, for both single- and + # multi-config generators alike. + if(MSVC) + # cl.exe silently ignores GCC/Clang-style "-f..."/"-g"/"-O0" flags + # with a D9002 warning rather than failing the build, so without this + # branch a Debug build under MSVC would quietly end up *unsanitized* + # instead of erroring -- a behavior gap invisible until someone + # actually diffs test coverage between platforms. + # + # MSVC's AddressSanitizer (/fsanitize=address) has shipped since + # Visual Studio 16.9; there is no UndefinedBehaviorSanitizer on this + # toolchain, so Debug builds here get ASan coverage only, not the + # ASan+UBSan combination GCC/Clang get below. Its runtime is a DLL + # shipped next to cl.exe, rather than a LD_PRELOAD-style shared + # object that the dynamic loader pulls in transparently: *every* + # executable and DLL built with /fsanitize=address needs that + # runtime DLL to be discoverable at load time (next to the binary, + # or on PATH) -- per Microsoft's own documentation, this holds even + # for /MT static-CRT builds, unlike every other CRT component. + # See tests/CMakeLists.txt and python/src/core/CMakeLists.txt for + # how CODAC_MSVC_ASAN_RUNTIME_DLL, set below, is used to make that + # DLL discoverable for the test suite and for the Python bindings. + # + # Source: https://learn.microsoft.com/en-us/cpp/sanitizers/asan-runtime + # + # Rather than enable /fsanitize=address unconditionally, this whole + # feature is opt-in on whether the matching runtime DLL can actually + # be located next to this cl.exe: if it can't (toolset too old, + # target architecture unsupported by MSVC ASan, or a packaging + # layout this check doesn't recognize), Debug builds simply stay + # unsanitized rather than either failing configuration or compiling + # binaries that are guaranteed to fail to load at run time with a + # missing-DLL error. + # + # MSVC ASan runtime binaries are named using Clang's architecture + # conventions (i386/x86_64/aarch64), not MSVC's own (X86/x64/ARM64) -- + # this is documented by Microsoft itself, not a codac convention. + # CMAKE_CXX_COMPILER_ARCHITECTURE_ID (a builtin CMake variable, MSVC + # only) reports the *target* architecture regardless of host, which + # is what a flag like /fsanitize=address needs to match; MSVC + # AddressSanitizer only supports x86, x64 and ARM64 (ARM64 as of + # VS 2022 17.9, as a preview) -- any other value (32-bit ARM, IA64, + # ARM64EC...) is simply left unmapped below, so the search for a + # runtime DLL naturally comes up empty and the feature is skipped, + # exactly as intended. + set(_msvc_asan_dll_arch "") + if(CMAKE_CXX_COMPILER_ARCHITECTURE_ID STREQUAL "x64") + set(_msvc_asan_dll_arch "x86_64") + elseif(CMAKE_CXX_COMPILER_ARCHITECTURE_ID STREQUAL "X86") + set(_msvc_asan_dll_arch "i386") + elseif(CMAKE_CXX_COMPILER_ARCHITECTURE_ID STREQUAL "ARM64") + set(_msvc_asan_dll_arch "aarch64") + endif() + + set(CODAC_MSVC_ASAN_RUNTIME_DLL "") + if(_msvc_asan_dll_arch) + get_filename_component(_msvc_bin_dir "${CMAKE_CXX_COMPILER}" DIRECTORY) + # Visual Studio 2022 17.7 Preview 3 unified every CRT configuration + # (/MT, /MTd, /MD, /MDd) onto a single clang_rt.asan_dynamic-{arch}.dll. + # Older toolsets instead shipped a separate debug-CRT DLL + # (clang_rt.asan_dbg_dynamic-{arch}.dll) for /MDd specifically. + # A Debug build uses the Debug CRT (/MDd), CMake's default, and so + # does GAOL, built with the runtime library of Codac when it is + # built as a part of it (scripts/CMakeModules/codac_gaol.cmake): with + # an older toolset, the DLL it needs is that /MDd one, looked for + # first; a newer toolset only ships the unified one. Rather than pin + # an exact MSVC_VERSION cutoff for the 17.7-era switch -- a + # compiler-rt packaging detail, not something reported by any + # compiler flag -- just look for whichever of the two names actually + # exists next to this cl.exe. + foreach(_dll_name + "clang_rt.asan_dbg_dynamic-${_msvc_asan_dll_arch}.dll" + "clang_rt.asan_dynamic-${_msvc_asan_dll_arch}.dll" + ) + if(EXISTS "${_msvc_bin_dir}/${_dll_name}") + set(CODAC_MSVC_ASAN_RUNTIME_DLL "${_msvc_bin_dir}/${_dll_name}") + break() + endif() + endforeach() + endif() + + if(CODAC_MSVC_ASAN_RUNTIME_DLL) + add_compile_options($<$:/fsanitize=address>) + # Incremental linking cannot be combined with ASan: the linker drops + # /INCREMENTAL as soon as an input module carries ASan metadata, and + # says so once per link (warning LNK4300, 870 times across the five + # MSVC Debug jobs of .github/workflows/windebugmatrix.yml, VS 2022 and + # VS 2026 alike). Asking for it off removes the cause rather than the + # message, and leaves LNK4300 free to report any other option ASan + # would silently disable. One item per generator expression, for the + # DIRECTORY-scope reason explained below. + add_link_options($<$:/INCREMENTAL:NO>) + # Enabling ASan on MSVC also switches on the MSVC STL's own + # "container annotation" instrumentation for std::string, + # std::vector and (recent toolsets) std::optional: it poisons + # their reserved-but-unused capacity so ASan can catch + # out-of-bounds access within it, and every translation unit + # linked together records, via #pragma detect_mismatch, whether it + # was compiled with that instrumentation on or off. gaol.lib is + # compiled without ASan: a GAOL found on the system comes prebuilt, + # and the GAOL built as a part of Codac is compiled without the + # options and definitions added here, which codac_gaol_fetch() + # (scripts/CMakeModules/codac_gaol.cmake) keeps from it. Its object + # files thus carry the instrumentation-off value; without this, + # codac's own ASan-instrumented Debug object files would carry the + # opposite value, and MSVC's linker would reject the mix with error + # LNK2038 ("mismatch detected for 'annotate_string' / + # 'annotate_vector' / 'annotate_optional'"). + # _DISABLE_STRING_ANNOTATION and + # _DISABLE_VECTOR_ANNOTATION are Microsoft's own documented way to + # turn this instrumentation back off so codac's object files match + # gaol.lib's; _DISABLE_OPTIONAL_ANNOTATION is added defensively for + # the same std::optional check (undocumented as of this writing, + # but following Microsoft's existing naming convention for the + # other two) -- worst case it is simply an unused macro if that + # name turns out to be wrong. Kept as three separate + # add_compile_definitions() calls, one flag per $ + # generator expression, for the same reason as the single-flag + # add_compile_options() calls further below: this is a + # DIRECTORY-scope property, which does not evaluate correctly when + # one such expression packs more than one ';'-separated item. + add_compile_definitions($<$:_DISABLE_STRING_ANNOTATION>) + add_compile_definitions($<$:_DISABLE_VECTOR_ANNOTATION>) + add_compile_definitions($<$:_DISABLE_OPTIONAL_ANNOTATION>) + message(STATUS "${COLOR_BLUE}MSVC AddressSanitizer will be enabled for Debug-configuration builds (runtime: ${CODAC_MSVC_ASAN_RUNTIME_DLL}).${COLOR_RESET}") + else() + message(STATUS "${COLOR_RED}No MSVC AddressSanitizer runtime found next to ${CMAKE_CXX_COMPILER} for architecture '${CMAKE_CXX_COMPILER_ARCHITECTURE_ID}': Debug-configuration builds will not be sanitized.${COLOR_RESET}") + endif() + + # Cached so python/src/core/CMakeLists.txt and tests/CMakeLists.txt + # can reuse this result (empty string if not found) without + # repeating the detection above. + set(CODAC_MSVC_ASAN_RUNTIME_DLL ${CODAC_MSVC_ASAN_RUNTIME_DLL} CACHE INTERNAL "Path to the MSVC ASan runtime DLL, empty if unavailable") + else() + # add_compile_options() (a DIRECTORY-scope property, unlike + # target_compile_options()) does not evaluate correctly when a single + # $ generator expression's content packs more than one + # flag separated by ';' -- reproduced locally with both Ninja and + # Unix Makefiles (CMake 3.16 and 4.4.3 alike): the embedded ';' gets + # split before the genexpr is fully evaluated, corrupting it into + # literal, unrecognized compiler arguments (e.g. the literal text + # "$<1:-Wreturn-type" as one broken token). target_compile_options() + # is not affected, but this is DIRECTORY-scope by design (it must + # apply project-wide, not to one target), so instead each flag gets + # its own single-flag generator expression below. + add_compile_options($<$:-Wreturn-type>) + add_compile_options($<$:-fno-omit-frame-pointer>) + add_compile_options($<$:-g>) + add_compile_options($<$:-O0>) + + # -fsanitize=address,undefined is only added once we have actually + # confirmed that this compiler can *link* a sanitized binary, rather + # than unconditionally, the way this project used to. On Linux/macOS + # GCC/Clang, libasan/libubsan are essentially always installed + # alongside the compiler and this check is a formality; the case it + # actually guards against is MinGW-w64: the choco `mingw` package used + # by .github/workflows/windebugmatrix.yml does not ship libasan.a/ + # libubsan.a at all for some GCC versions/architectures, and neither + # x86 (i686-w64-mingw32) nor x86_64 (x86_64-w64-mingw32) is reliably + # covered across the versions that package offers -- there is no + # version/architecture combination worth hardcoding an exception list + # for. Probing capability directly, exactly like the MSVC ASan + # runtime-DLL search above, means + # a Debug configuration simply comes out unsanitized on a toolchain + # that lacks the runtime, instead of configuring successfully and then + # failing later at link time with "cannot find -lasan"/"-lubsan" on + # every single target. + include(CheckCXXSourceCompiles) + set(CMAKE_REQUIRED_FLAGS "-fsanitize=address,undefined") + check_cxx_source_compiles("int main() { return 0; }" CODAC_COMPILER_SUPPORTS_ASAN_UBSAN) + unset(CMAKE_REQUIRED_FLAGS) + + if(CODAC_COMPILER_SUPPORTS_ASAN_UBSAN) + add_compile_options($<$:-fsanitize=address>) + add_compile_options($<$:-fsanitize=undefined>) + add_link_options($<$:-fsanitize=address,undefined>) + message(STATUS "${COLOR_BLUE}ASan/UBSan will be enabled for Debug-configuration builds.${COLOR_RESET}") + else() + message(STATUS "${COLOR_RED}No ASan/UBSan runtime found for ${CMAKE_CXX_COMPILER}: Debug-configuration builds will not be sanitized.${COLOR_RESET}") + endif() + endif() + endif() + # Temporary attempts to fix errors similar to: # _ number of sections exceeded object file format limit. # _ out of memory allocating XXX bytes. diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 609e5805f..8b1df6bcd 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -17,7 +17,9 @@ # Adds pybind11::headers, pybind11::module, pybind11::embed set(PYTHON_PACKAGE_NAME ${PROJECT_NAME}) - set(PYTHON_PACKAGE_DIR "${CMAKE_CURRENT_BINARY_DIR}/python_package") + set(PYTHON_PACKAGE_DIR "${CMAKE_CURRENT_BINARY_DIR}/python_package" + CACHE INTERNAL "Codac Python package build directory" + ) file(MAKE_DIRECTORY ${PYTHON_PACKAGE_DIR}) execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/${PYTHON_PACKAGE_NAME}/" "${PYTHON_PACKAGE_DIR}/${PYTHON_PACKAGE_NAME}") diff --git a/python/src/core/CMakeLists.txt b/python/src/core/CMakeLists.txt index 6175f7291..affc59c7a 100644 --- a/python/src/core/CMakeLists.txt +++ b/python/src/core/CMakeLists.txt @@ -163,6 +163,62 @@ PRIVATE ${PROJECT_NAME}-core ${PROJECT_NAME}-sympy Threads::Threads Codac::gaol ) + # -------------------------------------------------------------- + # Sanitizer runtime as a shared library (Debug builds only) + # -------------------------------------------------------------- + # + # _core.so is loaded via dlopen() by an unsanitized python + # interpreter. By default Clang/GCC embed the ASan/UBSan runtime + # statically into shared libraries, which leaves symbols like + # __ubsan_vptr_type_cache or __asan_* unresolved at dlopen() time + # (python has no sanitizer runtime of its own to provide them). + # -shared-libsan makes _core.so depend on the sanitizer runtime as + # a proper shared library instead; that runtime is then LD_PRELOAD-ed + # for the python tests (see tests/CMakeLists.txt). + # Not gated behind CMAKE_BUILD_TYPE (see the matching note in the root + # CMakeLists.txt): that variable is meaningless for multi-configuration + # generators, so -shared-libsan is applied via a $ + # generator expression instead, which CMake evaluates correctly for + # both single- and multi-config generators. + if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") + target_compile_options(_core PRIVATE $<$:-shared-libsan>) + target_link_options(_core PRIVATE $<$:-shared-libsan>) + endif() + + # -------------------------------------------------------------- + # MSVC AddressSanitizer runtime DLL (Debug builds only) + # -------------------------------------------------------------- + # + # /fsanitize=address (enabled in Debug mode only if a matching runtime + # was found -- see the MSVC branch in the root CMakeLists.txt, which + # sets CODAC_MSVC_ASAN_RUNTIME_DLL) makes _core.pyd import that runtime + # DLL. Unlike Clang/GCC on Linux, there is no LD_PRELOAD-style env var + # and no "must be the first library loaded" requirement to work around + # on Windows -- the loader simply needs to be able to find that DLL when + # Python calls LoadLibrary() on _core.pyd, which it does by searching + # the directory of the loading module before PATH. So it is enough to + # copy the runtime DLL next to the built _core.pyd, the same way + # _core.pyd itself is already copied into the package folder below. + # CODAC_MSVC_ASAN_RUNTIME_DLL is already empty (and this is then a + # no-op) whenever the root CMakeLists.txt couldn't locate that runtime, + # so /fsanitize=address was in that case never even added. + if(MSVC AND CODAC_MSVC_ASAN_RUNTIME_DLL) + # CODAC_MSVC_ASAN_RUNTIME_DLL is now found regardless of which + # configuration will be built (see the root CMakeLists.txt), since a + # multi-config generator does not know that yet at configure time. So + # the copy itself has to stay conditional on the configuration that is + # actually being built, which a plain if() cannot express: + # $,copy_if_different,echo> selects between the real + # "-E copy_if_different" and "-E echo", which merely prints the two + # paths, so the DLL is only copied for a Debug-configuration build, in + # any generator. "-E true" would read better, but it only exists since + # CMake 3.16 while this project accepts 3.14. + add_custom_command(TARGET _core POST_BUILD + COMMAND ${CMAKE_COMMAND} -E $,copy_if_different,echo> "${CODAC_MSVC_ASAN_RUNTIME_DLL}" "${PYTHON_PACKAGE_DIR}/${PYTHON_PACKAGE_NAME}" + ) + endif() + + # Copy the generated library in the package folder add_custom_command(TARGET _core POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "$" "${PYTHON_PACKAGE_DIR}/${PYTHON_PACKAGE_NAME}" diff --git a/scripts/CMakeModules/codac_gaol.cmake b/scripts/CMakeModules/codac_gaol.cmake index 2bdab7517..1b822ba32 100644 --- a/scripts/CMakeModules/codac_gaol.cmake +++ b/scripts/CMakeModules/codac_gaol.cmake @@ -184,13 +184,14 @@ endfunction() # CMakeLists.txt had to force the Release runtime on Codac while GAOL was built # apart, in Release. # -# Not compiled with Codac's warnings and definitions, though (/W4, -Wall -Wextra -# -Wpedantic, /bigobj, FAST_RELEASE...): the top-level CMakeLists.txt adds them -# with add_compile_options() and add_compile_definitions(), whose directory -# properties a subdirectory takes from its parent when it is added. They are -# emptied while GAOL is added, and given back right after: GAOL is compiled with -# the flags it chooses, and with CMAKE_CXX_FLAGS as it is given to this build -# (codac_gaol_portability_flags() is added to it afterwards). +# Not compiled with Codac's warnings, sanitizers and definitions, though (/W4, +# -Wall -Wextra -Wpedantic, /bigobj, -fsanitize=address,undefined and +# /fsanitize=address in Debug, FAST_RELEASE...): the top-level CMakeLists.txt +# adds them with add_compile_options() and add_compile_definitions(), whose +# directory properties a subdirectory takes from its parent when it is added. +# They are emptied while GAOL is added, and given back right after: GAOL is +# compiled with the flags it chooses, and with CMAKE_CXX_FLAGS as it is given to +# this build (codac_gaol_portability_flags() is added to it afterwards). # # From the head of the master branch, so that the fixes pushed to GAOL reach # Codac without a change here. Cloned with Git, the sources are brought up to diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f8a95c009..37ef5ac3c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -171,6 +171,249 @@ endif() #message(STATUS "Found IBEX version ${IBEX_VERSION}") #endif() +# ------------------------------------------------------------------ +# Sanitizer runtime preload for Python tests (Debug builds only) +# ------------------------------------------------------------------ +# +# In Debug mode, _core.so (the pybind11 module) is compiled with +# -fsanitize=address,undefined and depends on a *shared* sanitizer +# runtime rather than one statically embedded in the .so: +# - Clang needs an explicit -shared-libsan flag to get that (see +# python/src/core/CMakeLists.txt) -- its default is to embed the +# runtime statically into shared libraries. +# - GCC needs no flag at all: unless -static-libasan/-static-libubsan +# is passed (which codac never does), GCC already links libasan.so / +# libubsan.so dynamically by default. Passing Clang's -shared-libsan +# to GCC is not an option either way -- GCC's driver rejects it +# outright as an unrecognized flag. +# Either way, since python3 itself has no sanitizer runtime, that shared +# runtime must be preloaded before Python imports codac, otherwise symbols +# like __ubsan_vptr_type_cache/__asan_* stay unresolved -- or, on GCC, the +# module load aborts immediately with "ASan runtime does not come first in +# initial library list" (reproduced locally: a plain dlopen(), from an +# unsanitized host, of a GCC ASan-built .so fails exactly this way without +# LD_PRELOAD, and succeeds once libasan.so is preloaded). + +set(PYTHON_TEST_ENV_ARGS "PYTHONPATH=${PYTHON_PACKAGE_DIR}") + +# Not gated behind a plain CMAKE_BUILD_TYPE STREQUAL "Debug" check (see the +# matching note in the root CMakeLists.txt): that variable is meaningless +# for multi-configuration generators, which instead pick the actual +# configuration later, at build time. CMAKE_CONFIGURATION_TYPES is set +# precisely on those generators, so probing here also runs whenever it is +# non-empty -- the individual LD_PRELOAD/ASAN_OPTIONS/UBSAN_OPTIONS entries +# appended below are themselves wrapped in a $ generator +# expression (see how PYTHON_TEST_ENV_ARGS is consumed via the ENVIRONMENT +# test property further down), so they still only take effect for an +# actual Debug-configuration test run. +if(WITH_PYTHON AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_CONFIGURATION_TYPES) + AND CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU" AND CMAKE_SYSTEM_NAME STREQUAL "Linux") + + # This whole block is Linux-specific by construction, not just by + # "is the compiler Clang or GCC": LD_PRELOAD is a Linux/glibc loader + # feature with no equivalent environment variable on Windows, and the + # shared runtime file names it looks for below only exist in that form + # on Linux -- macOS uses DYLD_INSERT_LIBRARIES and ships its runtimes + # under different names (e.g. Clang's combined, no-arch-suffix + # libclang_rt.asan_osx_dynamic.dylib), and Windows uses neither an + # env-var preload mechanism nor this naming (clang-cl names them + # clang_rt.asan_dynamic-.dll; MinGW-w64 GCC ships asan/ubsan as + # DLLs with their own layout). Real MSVC (cl.exe) is already excluded + # by the CMAKE_CXX_COMPILER_ID check above, but clang-cl (Windows) and + # AppleClang (macOS) both also match "Clang" (and MinGW GCC still + # matches "GNU"), so without this explicit Linux check they would + # silently fall through to the "no shared runtime found" warning below + # instead of making clear that the feature just does not apply there. + + if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") + + # Note: `clang --print-runtime-dir` assumes a per-target-runtime-dir + # layout (lib//) which some distro packages (e.g. Ubuntu/ + # Debian's llvm.org apt packages) do not use, instead keeping the legacy + # lib/linux/ layout: the reported directory would not exist there, even + # though the runtime libraries are actually installed. `-print-file-name` + # asks the driver to resolve each file the way it would at link time, + # which works regardless of which layout the distro packaging uses. + # + # The runtime file names themselves are not portable either: Clang embeds + # its own architecture name in them (e.g. libclang_rt.asan-aarch64.so on + # an AArch64 Raspberry Pi OS, libclang_rt.asan-armhf.so on a 32-bit + # ARMv7/ARMv8 hard-float userland). Rather than maintaining our own + # CMAKE_SYSTEM_PROCESSOR -> Clang-runtime-name table (which would drift + # out of sync as LLVM adds targets), ask the compiler itself: + # `-dumpmachine` prints its default target triple (understood by both + # Clang and GCC, and already normalized -- e.g. a 32-bit x86 compiler + # reports "i386", never "i686"), and its first field is, for essentially + # every architecture, precisely the suffix Clang's runtime uses. + # + # The one documented exception is 32-bit ARM, where the suffix instead + # follows the float ABI recorded in the triple's environment field + # (.../gnueabihf -> "armhf" hard-float, vs. plain "arm" otherwise) rather + # than the architecture field (which is typically "arm"/"armv7"/...) -- + # this is an LLVM naming convention, not something any compiler flag + # reports directly, so it is the one case still special-cased below. + execute_process( + COMMAND ${CMAKE_CXX_COMPILER} -dumpmachine + OUTPUT_VARIABLE _clang_target_triple + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + set(_sanitizer_rt_arch "") + if(_clang_target_triple) + string(REPLACE "-" ";" _clang_triple_fields "${_clang_target_triple}") + list(GET _clang_triple_fields 0 _sanitizer_rt_arch) + if(_sanitizer_rt_arch MATCHES "^armv?[0-9]*l?$") + if(_clang_target_triple MATCHES "eabihf$") + set(_sanitizer_rt_arch "armhf") + else() + set(_sanitizer_rt_arch "arm") + endif() + endif() + endif() + + # The ASan runtime only, deliberately: built with -fsanitize=address, + # undefined, Clang puts the UndefinedBehaviorSanitizer inside the ASan + # runtime, and ships libclang_rt.ubsan_standalone-* for the builds that + # use UBSan on its own. Preloading both therefore loads two copies of the + # same initialisation and the same interceptors into one process. Clang 18 + # tolerated it; Clang 21 wedges the interpreter so thoroughly that even + # SIGKILL does not remove it, which is what made every Python test of the + # Clang jobs of .github/workflows/unixdebug.yml run into its timeout while + # the GCC jobs of the same matrix passed them in about two seconds -- + # GCC's libasan and libubsan being genuinely separate libraries, designed + # to be loaded together. Preloading ASan alone still gives both sets of + # checks, since one runtime implements them both. + set(_sanitizer_rt_candidates "") + if(_sanitizer_rt_arch) + set(_sanitizer_rt_candidates + libclang_rt.asan-${_sanitizer_rt_arch}.so + ) + endif() + + else() # GNU (GCC) + + # Unlike Clang, GCC's driver is built for one target only, so its + # runtime file names carry no architecture suffix -- `-print-file-name` + # already resolves to the right library for whatever target this GCC + # was built for, on every architecture. GCC also splits ASan and UBSan + # into two separately-named libraries (libasan.so / libubsan.so), + # rather than Clang's libclang_rt.asan-*/libclang_rt.ubsan_standalone-* + # naming. + set(_sanitizer_rt_candidates libasan.so libubsan.so) + + endif() + + set(SANITIZER_RT_LIBS "") + foreach(_rt_lib_name ${_sanitizer_rt_candidates}) + execute_process( + COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=${_rt_lib_name} + OUTPUT_VARIABLE _rt_lib_path + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if(EXISTS "${_rt_lib_path}") + list(APPEND SANITIZER_RT_LIBS "${_rt_lib_path}") + endif() + endforeach() + + if(SANITIZER_RT_LIBS) + list(JOIN SANITIZER_RT_LIBS ":" SANITIZER_RT_LIBS_JOINED) + list(APPEND PYTHON_TEST_ENV_ARGS "$<$:LD_PRELOAD=${SANITIZER_RT_LIBS_JOINED}>") + + # LeakSanitizer (part of ASan) flags many long-lived, process-lifetime + # allocations made by the Python interpreter and by pybind11 module + # init (interned strings, type objects, strdup'd docstrings...) as + # "leaks", since it can't trace Python's own object graph. This is a + # well-known false-positive source for embedded Python interpreters, + # not something codac's code controls. UBSan and ASan's other checks + # (use-after-free, buffer overflow, UB...) remain fully active. + list(APPEND PYTHON_TEST_ENV_ARGS "$<$:ASAN_OPTIONS=detect_leaks=0>") + list(APPEND PYTHON_TEST_ENV_ARGS "$<$:UBSAN_OPTIONS=print_stacktrace=1>") + message(STATUS "Python tests: LD_PRELOAD=${SANITIZER_RT_LIBS_JOINED}") + else() + message(WARNING + "Debug build with WITH_PYTHON: no shared ASan/UBSan runtime found " + "for ${CMAKE_CXX_COMPILER}. With Clang, make sure the _core target " + "is linked with -shared-libsan; with GCC, make sure it is not " + "linked with -static-libasan/-static-libubsan. Otherwise Python " + "tests will fail with 'undefined symbol' errors, or abort with " + "'ASan runtime does not come first in initial library list'.") + endif() + +endif() + +# ------------------------------------------------------------------ +# Sanitizer runtime discoverability for MSVC (Debug builds only) +# ------------------------------------------------------------------ +# +# CODAC_MSVC_ASAN_RUNTIME_DLL is set by the root CMakeLists.txt (empty if +# no matching runtime could be found, in which case /fsanitize=address was +# never even added -- nothing to do here either). Windows has no +# LD_PRELOAD-style env var and no "must load first" requirement: the +# loader just needs to be able to find the runtime DLL via PATH or next +# to the binary that imports it, when that binary is loaded. Python's +# _core.pyd gets its own copy placed next to it (see +# python/src/core/CMakeLists.txt); the plain C++ test executables and the +# python.exe launched below have no such fixed "next to" location, so +# their directory search is extended via PATH instead, the direct +# Windows analogue of prepending to LD_PRELOAD above. +set(CODAC_MSVC_ASAN_PATH_ENV "") +if(MSVC AND CODAC_MSVC_ASAN_RUNTIME_DLL) + get_filename_component(_msvc_asan_dll_dir "${CODAC_MSVC_ASAN_RUNTIME_DLL}" DIRECTORY) + # Both ctest's ENVIRONMENT test property and the COMMAND argument list + # passed to `cmake -E env` below are themselves ';'-separated CMake + # lists, so the ';' that Windows uses to separate PATH entries has to be + # escaped as '\;' here -- otherwise CMake would silently split this one + # "PATH=......" value into several unrelated list entries. + # + # NOTE: $ENV{PATH} is captured at CMake *configure* time, i.e. whatever + # PATH was active when `cmake` was run to configure the project. If + # ctest is later invoked from a shell with a materially different PATH, + # this won't reflect that -- a limitation inherent to baking an + # environment value into generated build files, not specific to this + # check. + string(REPLACE ";" "\\;" _msvc_asan_path_escaped "${_msvc_asan_dll_dir};$ENV{PATH}") + set(CODAC_MSVC_ASAN_PATH_ENV "PATH=${_msvc_asan_path_escaped}") + # Appended for every configuration rather than wrapped in a $ + # generator expression. `cmake -E env` reads its arguments as VAR=value + # assignments up to the first one that is not, which it then takes for the + # command to run: an element that a generator expression reduced to nothing + # leaves an empty argument in the middle of the list, and the command becomes + # the empty string. CODAC_MSVC_ASAN_RUNTIME_DLL is computed whatever the + # configuration (see the root CMakeLists.txt), so on MSVC the element was + # always there and always empty outside Debug -- every Python test of + # .github/workflows/vcmatrix.yml died on "no such file or directory" while + # the C++ ones passed. Prepending the sanitizer directory to PATH costs a + # Release run nothing, which is what the C++ tests below already rely on. + list(APPEND PYTHON_TEST_ENV_ARGS "${CODAC_MSVC_ASAN_PATH_ENV}") +endif() + + +# Environment of the C++ tests and examples +# ================================================================== +# +# The same environment is needed by every C++ binary this project runs under +# ctest, the examples of examples/CMakeLists.txt included, so it is built once +# here and exported to the parent scope rather than recomputed per test. +# +# UBSAN_OPTIONS only means something to the GCC/Clang single-configuration +# builds, where CMAKE_BUILD_TYPE holds the answer. The MSVC AddressSanitizer +# runtime is a DLL that has to be discoverable at load time by every binary +# compiled with /fsanitize=address: without it on PATH a Debug test cannot even +# start, Windows failing the process creation with 0xC0000135 +# (STATUS_DLL_NOT_FOUND) before a single assertion runs. It is added whatever +# the configuration, because the multi-configuration generators MSVC is driven +# through only choose theirs at build time, and prepending a toolchain +# directory to PATH costs a Release run nothing. + +set(CODAC_CPP_TEST_ENV "") + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + list(APPEND CODAC_CPP_TEST_ENV "UBSAN_OPTIONS=print_stacktrace=1") +endif() + +if(CODAC_MSVC_ASAN_PATH_ENV) + list(APPEND CODAC_CPP_TEST_ENV "${CODAC_MSVC_ASAN_PATH_ENV}") +endif() + foreach(SRC_TEST ${SRC_TESTS}) string(REPLACE "/" "_" TEST_NAME ${SRC_TEST}) string(REPLACE "codac2_tests_" "" TEST_NAME ${TEST_NAME}) @@ -188,9 +431,22 @@ foreach(SRC_TEST ${SRC_TESTS}) add_dependencies(check ${TEST_NAME}) add_test(NAME ${TEST_NAME}_cpp COMMAND ${TEST_NAME}) + if(CODAC_CPP_TEST_ENV) + set_tests_properties(${TEST_NAME}_cpp PROPERTIES + ENVIRONMENT "${CODAC_CPP_TEST_ENV}" + ) + endif() + # Python test - if(WITH_PYTHON) - add_test(NAME ${TEST_NAME}_py COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/${SRC_TEST}.py) + if(WITH_PYTHON AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_TEST}.py") + add_test( + NAME ${TEST_NAME}_py + COMMAND + ${CMAKE_COMMAND} -E env + ${PYTHON_TEST_ENV_ARGS} + ${PYTHON_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/${SRC_TEST}.py + ) if(WITH_IBEX) # The C++ tests tell the GAOL Codac is built on from CODAC_GAOL_GUARDS # (codac2_Interval.h); the Python ones, which only see the codac module, From 3b089d08f7038cf3fdf06241b10653e3288b11e9 Mon Sep 17 00:00:00 2001 From: Jordan08 Date: Sat, 19 Sep 2026 13:37:35 +0200 Subject: [PATCH 5/6] Compile against the headers in src/ instead of build/include copies --- CMakeLists.txt | 36 +++++++++ python/src/core/CMakeLists.txt | 7 +- python/src/graphics/CMakeLists.txt | 7 +- python/src/unsupported/CMakeLists.txt | 7 +- src/CMakeLists.txt | 36 ++++++++- src/core/CMakeLists.txt | 55 ++++++++++--- src/extensions/capd/CMakeLists.txt | 35 ++++++-- src/extensions/sympy/CMakeLists.txt | 39 +++++++-- src/graphics/CMakeLists.txt | 36 +++++++-- src/unsupported/CMakeLists.txt | 43 +++++++--- tests/CMakeLists.txt | 112 ++++++++++++++++++++++++-- 11 files changed, 357 insertions(+), 56 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 04f4a5dcb..c0af6ba65 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -397,6 +397,42 @@ message(STATUS "Interval arithmetic flags of Codac::gaol, for the users of Codac: ${_codac_interval_cxx_flags}") + # Records the directories in which a module keeps its public headers, in + # their canonical location under src/. Every consumer (the tests, the + # examples and the Python bindings) lists CODAC_SOURCE_INCLUDE_DIRS first, + # so that a header is opened at src/... rather than through a copy in the + # build tree. Both compile identically, but the path used is the path gcov + # records: reached both ways, a header ends up as two unrelated entries in + # the coverage report, each showing only what its own callers exercised, + # and is reported well below its real coverage. + # + # The variable is a cache entry because it is filled in by src/*/CMakeLists.txt + # and read from tests/, examples/ and python/, which are separate directory + # scopes; src/CMakeLists.txt empties it before the modules append to it, so + # that a reconfigure does not append to what the previous one left behind. + function(codac_publish_include_dirs) + set(CODAC_SOURCE_INCLUDE_DIRS ${CODAC_SOURCE_INCLUDE_DIRS} ${ARGN} + CACHE INTERNAL "Directories holding the public headers of the codac modules") + endfunction() + + + # The companion of the above, for the umbrella headers CMake generates rather + # than the ones kept in the source tree: codac-core.h, codac-graphics.h and + # the rest. Each module writes its own into its own binary directory, so + # ${CMAKE_BINARY_DIR}/src -- where the tests used to look, and the only place + # they looked -- holds none of them. Only the CAPD snippet of the manual + # includes them (every other test reaches for a codac2_*.h directly), which + # is why the omission surfaced as nothing but the CAPD jobs failing on + # "codac-core.h: No such file or directory". + # + # It is a cache entry, and emptied by src/CMakeLists.txt before the modules + # fill it, for the same reasons as CODAC_SOURCE_INCLUDE_DIRS above. + function(codac_publish_generated_include_dirs) + set(CODAC_GENERATED_INCLUDE_DIRS ${CODAC_GENERATED_INCLUDE_DIRS} ${ARGN} + CACHE INTERNAL "Directories holding the umbrella headers generated for the codac modules") + endfunction() + + ################################################################################ # Looking for Eigen3 ################################################################################ diff --git a/python/src/core/CMakeLists.txt b/python/src/core/CMakeLists.txt index affc59c7a..58ba7a128 100644 --- a/python/src/core/CMakeLists.txt +++ b/python/src/core/CMakeLists.txt @@ -136,8 +136,13 @@ ) target_include_directories(_core + # First, so that a header is opened at src/... rather than through + # build/include: the path a translation unit used is the path gcov records, + # and the two spellings would split every header into two entries in the + # coverage report. See codac_publish_include_dirs() in the top-level CMakeLists.txt. + PRIVATE ${CODAC_SOURCE_INCLUDE_DIRS} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../../docstring - PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../../../include + PRIVATE ${CMAKE_BINARY_DIR}/src PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../ PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/actions/ diff --git a/python/src/graphics/CMakeLists.txt b/python/src/graphics/CMakeLists.txt index 79f7639f8..b35a6683c 100644 --- a/python/src/graphics/CMakeLists.txt +++ b/python/src/graphics/CMakeLists.txt @@ -18,8 +18,13 @@ ) target_include_directories(_graphics + # First, so that a header is opened at src/... rather than through + # build/include: the path a translation unit used is the path gcov records, + # and the two spellings would split every header into two entries in the + # coverage report. See codac_publish_include_dirs() in the top-level CMakeLists.txt. + PRIVATE ${CODAC_SOURCE_INCLUDE_DIRS} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../../docstring - PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../../../include + PRIVATE ${CMAKE_BINARY_DIR}/src PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../core/ PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../ PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ diff --git a/python/src/unsupported/CMakeLists.txt b/python/src/unsupported/CMakeLists.txt index 15bccec3a..6b3e93ec6 100644 --- a/python/src/unsupported/CMakeLists.txt +++ b/python/src/unsupported/CMakeLists.txt @@ -9,8 +9,13 @@ ) target_include_directories(_unsupported + # First, so that a header is opened at src/... rather than through + # build/include: the path a translation unit used is the path gcov records, + # and the two spellings would split every header into two entries in the + # coverage report. See codac_publish_include_dirs() in the top-level CMakeLists.txt. + PRIVATE ${CODAC_SOURCE_INCLUDE_DIRS} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../../docstring - PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../../../include + PRIVATE ${CMAKE_BINARY_DIR}/src PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../ PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ ) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e60995a64..285bad255 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,6 +6,18 @@ # Compiling sources # ================= + # Emptied here, then appended to by each module below through + # codac_publish_include_dirs(). It is a cache entry, so it survives a + # reconfigure: without this reset it would grow a duplicate of every + # directory on each run, and would keep the directories of a module that has + # since been switched off (capd, sympy). + set(CODAC_SOURCE_INCLUDE_DIRS "" CACHE INTERNAL + "Directories holding the public headers of the codac modules") + + # Same treatment for the directories of the generated umbrella headers. + set(CODAC_GENERATED_INCLUDE_DIRS "" CACHE INTERNAL + "Directories holding the umbrella headers generated for the codac modules") + add_subdirectory(core) add_subdirectory(graphics) add_subdirectory(unsupported) @@ -176,10 +188,28 @@ ${CODAC_GAOL_CONFIG_SNIPPET} # ==================================== set(CODAC_MAIN_HEADER ${CMAKE_CURRENT_BINARY_DIR}/codac) - file(WRITE ${CODAC_MAIN_HEADER} "/* This file is generated by CMake */\n\n") - file(APPEND ${CODAC_MAIN_HEADER} "#pragma once\n\n") + + set(CODAC_MAIN_HEADER_CONTENT "/* This file is generated by CMake */\n\n") + string(APPEND CODAC_MAIN_HEADER_CONTENT "#pragma once\n\n") foreach(header_path ${CODAC_MAIN_SUBHEADERS}) get_filename_component(header_name ${header_path} NAME) - file(APPEND ${CODAC_MAIN_HEADER} "#include <${header_name}>\n") + string(APPEND CODAC_MAIN_HEADER_CONTENT "#include <${header_name}>\n") endforeach() + + # Compare with the existing file, if any + if(EXISTS "${CODAC_MAIN_HEADER}") + file(READ "${CODAC_MAIN_HEADER}" EXISTING_CONTENT) + string(MD5 EXISTING_HASH "${EXISTING_CONTENT}") + string(MD5 NEW_HASH "${CODAC_MAIN_HEADER_CONTENT}") + else() + set(EXISTING_HASH "") + set(NEW_HASH "new") + endif() + + # Only rewrite it when it changed: 24 examples #include , and an + # unconditional rewrite moves its timestamp on every cmake run, rebuilding + # all of them although nothing changed. + if(NOT EXISTING_HASH STREQUAL NEW_HASH) + file(WRITE "${CODAC_MAIN_HEADER}" "${CODAC_MAIN_HEADER_CONTENT}") + endif() install(FILES ${CODAC_MAIN_HEADER} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/) \ No newline at end of file diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index f99e4c846..ae0521ea4 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -303,7 +303,20 @@ #endif() add_library(${PROJECT_NAME}-core ${CODAC_CORE_SRC}) - target_include_directories(${PROJECT_NAME}-core PUBLIC + + # The directories holding the public core headers, in their canonical + # location under src/core. Published as a cache variable so that the tests, + # the examples and the Python bindings all reach the headers by the same + # path as codac-core itself, instead of each keeping its own copy of this + # list (which had already drifted). + # + # The path a translation unit used is the path gcov records, so a header + # reached both through src/ and through a copy in the build tree ends up as + # two unrelated entries in the coverage report, each showing only the part + # its own callers exercised, and is reported well below its real coverage. + # Consumers therefore list these directories first; the build tree stays on + # the path for the generated umbrella headers (codac-core.h and the rest). + set(CODAC_CORE_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/actions ${CMAKE_CURRENT_SOURCE_DIR}/contractors ${CMAKE_CURRENT_SOURCE_DIR}/domains @@ -329,8 +342,12 @@ ${CMAKE_CURRENT_SOURCE_DIR}/proj ${CMAKE_CURRENT_SOURCE_DIR}/separators ${CMAKE_CURRENT_SOURCE_DIR}/tools + ${CMAKE_CURRENT_SOURCE_DIR}/tools/ibex ${CMAKE_CURRENT_SOURCE_DIR}/trajectory - ) + CACHE INTERNAL "Directories holding the public headers of codac-core") + + target_include_directories(${PROJECT_NAME}-core PUBLIC ${CODAC_CORE_INCLUDE_DIRS}) + codac_publish_include_dirs(${CODAC_CORE_INCLUDE_DIRS}) target_link_libraries(${PROJECT_NAME}-core PUBLIC ${PROJECT_NAME}-eigen Codac::gaol Threads::Threads) #Eigen3::Eigen @@ -341,36 +358,52 @@ set(CODAC_PKG_CONFIG_CFLAGS "${CODAC_PKG_CONFIG_CFLAGS} -I\${includedir}/${PROJECT_NAME}-core" PARENT_SCOPE) set(CODAC_PKG_CONFIG_LIBS "${CODAC_PKG_CONFIG_LIBS} -l${PROJECT_NAME}-core" PARENT_SCOPE) - + ################################################################################ -# Installation of libcodac-core files +# Installation / build include tree ################################################################################ - -# Getting header files from sources foreach(srcfile ${CODAC_CORE_SRC}) if(srcfile MATCHES "\\.h$" OR srcfile MATCHES "\\.hpp$") list(APPEND CODAC_CORE_HDR ${srcfile}) - file(COPY ${srcfile} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/../../include) endif() endforeach() + # Generating the file codac-core.h +# ================================ set(CODAC_CORE_MAIN_HEADER ${CMAKE_CURRENT_BINARY_DIR}/codac-core.h) + codac_publish_generated_include_dirs(${CMAKE_CURRENT_BINARY_DIR}) set(CODAC_MAIN_SUBHEADERS ${CODAC_MAIN_SUBHEADERS} "codac-core.h" PARENT_SCOPE) - file(WRITE ${CODAC_CORE_MAIN_HEADER} "/* This file is generated by CMake */\n\n") - file(APPEND ${CODAC_CORE_MAIN_HEADER} "#pragma once\n\n") + + # Generate the content of the umbrella header + set(CODAC_CORE_HEADER_CONTENT "/* This file is generated by CMake */\n\n#pragma once\n\n") foreach(header_path ${CODAC_CORE_HDR}) get_filename_component(header_name ${header_path} NAME) if((NOT header_name MATCHES "^.*_addons.*$") AND (NOT header_name MATCHES "^.*_impl.*$")) - file(APPEND ${CODAC_CORE_MAIN_HEADER} "#include <${header_name}>\n") + string(APPEND CODAC_CORE_HEADER_CONTENT "#include <${header_name}>\n") endif() endforeach() - file(COPY ${CODAC_CORE_MAIN_HEADER} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/../../include) + + # Compare with the existing file, if any + if(EXISTS "${CODAC_CORE_MAIN_HEADER}") + file(READ "${CODAC_CORE_MAIN_HEADER}" EXISTING_CONTENT) + string(MD5 EXISTING_HASH "${EXISTING_CONTENT}") + string(MD5 NEW_HASH "${CODAC_CORE_HEADER_CONTENT}") + else() + set(EXISTING_HASH "") + set(NEW_HASH "new") + endif() + + # Only rewrite it when it changed + if(NOT EXISTING_HASH STREQUAL NEW_HASH) + file(WRITE "${CODAC_CORE_MAIN_HEADER}" "${CODAC_CORE_HEADER_CONTENT}") + endif() # Install files in system directories +# ==================================== install(TARGETS ${PROJECT_NAME}-core DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES ${CODAC_CORE_HDR} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}-core) diff --git a/src/extensions/capd/CMakeLists.txt b/src/extensions/capd/CMakeLists.txt index b0e2ccb54..6bae28409 100644 --- a/src/extensions/capd/CMakeLists.txt +++ b/src/extensions/capd/CMakeLists.txt @@ -24,6 +24,16 @@ list(APPEND CODAC_CAPD_SRC add_library(${PROJECT_NAME}-capd ${CODAC_CAPD_SRC}) target_link_libraries(${PROJECT_NAME}-capd PUBLIC ${PROJECT_NAME}-core ${PROJECT_NAME}-eigen Codac::gaol capd::capd) # Eigen3::Eigen + # The directory holding this module's public headers, in its canonical + # location under src/. It had none declared at all: its headers were only + # reachable through their copy in build/include. Publishing it lets every + # consumer open them at their source path instead -- see + # codac_publish_include_dirs() in the top-level CMakeLists.txt for why that + # decides what the coverage report is worth. + set(CODAC_CAPD_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}) + target_include_directories(${PROJECT_NAME}-capd PUBLIC ${CODAC_CAPD_INCLUDE_DIRS}) + codac_publish_include_dirs(${CODAC_CAPD_INCLUDE_DIRS}) + ################################################################################ # For the generation of the PKG file @@ -42,20 +52,35 @@ list(APPEND CODAC_CAPD_SRC foreach(srcfile ${CODAC_CAPD_SRC}) if(srcfile MATCHES "\\.h$" OR srcfile MATCHES "\\.hpp$") list(APPEND CODAC_CAPD_HDR ${srcfile}) - file(COPY ${srcfile} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/../../../include) endif() endforeach() # Generating the file codac-capd.h set(CODAC_CAPD_MAIN_HEADER ${CMAKE_CURRENT_BINARY_DIR}/codac-capd.h) - file(WRITE ${CODAC_CAPD_MAIN_HEADER} "/* This file is generated by CMake */\n\n") - file(APPEND ${CODAC_CAPD_MAIN_HEADER} "#pragma once\n\n") + codac_publish_generated_include_dirs(${CMAKE_CURRENT_BINARY_DIR}) + + # Generate the content of the umbrella header + set(CODAC_CAPD_HEADER_CONTENT "/* This file is generated by CMake */\n\n#pragma once\n\n") foreach(header_path ${CODAC_CAPD_HDR}) get_filename_component(header_name ${header_path} NAME) - file(APPEND ${CODAC_CAPD_MAIN_HEADER} "#include <${header_name}>\n") + string(APPEND CODAC_CAPD_HEADER_CONTENT "#include <${header_name}>\n") endforeach() - file(COPY ${CODAC_CAPD_MAIN_HEADER} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/../../../include) + + # Compare with the existing file, if any + if(EXISTS "${CODAC_CAPD_MAIN_HEADER}") + file(READ "${CODAC_CAPD_MAIN_HEADER}" EXISTING_CONTENT) + string(MD5 EXISTING_HASH "${EXISTING_CONTENT}") + string(MD5 NEW_HASH "${CODAC_CAPD_HEADER_CONTENT}") + else() + set(EXISTING_HASH "") + set(NEW_HASH "new") + endif() + + # Only rewrite it when it changed + if(NOT EXISTING_HASH STREQUAL NEW_HASH) + file(WRITE "${CODAC_CAPD_MAIN_HEADER}" "${CODAC_CAPD_HEADER_CONTENT}") + endif() # Install files in system directories diff --git a/src/extensions/sympy/CMakeLists.txt b/src/extensions/sympy/CMakeLists.txt index 139c9cb8e..0a301a69b 100644 --- a/src/extensions/sympy/CMakeLists.txt +++ b/src/extensions/sympy/CMakeLists.txt @@ -34,6 +34,16 @@ target_link_libraries(${PROJECT_NAME}-sympy PUBLIC ${PROJECT_NAME}-core ${PROJECT_NAME}-eigen Codac::gaol) # Eigen3::Eigen target_link_libraries(${PROJECT_NAME}-sympy PRIVATE pybind11::pybind11) + # The directory holding this module's public headers, in its canonical + # location under src/. It had none declared at all: its headers were only + # reachable through their copy in build/include. Publishing it lets every + # consumer open them at their source path instead -- see + # codac_publish_include_dirs() in the top-level CMakeLists.txt for why that + # decides what the coverage report is worth. + set(CODAC_SYMPY_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}) + target_include_directories(${PROJECT_NAME}-sympy PUBLIC ${CODAC_SYMPY_INCLUDE_DIRS}) + codac_publish_include_dirs(${CODAC_SYMPY_INCLUDE_DIRS}) + ################################################################################ # For the generation of the PKG file ################################################################################ @@ -45,18 +55,31 @@ # Installation of libcodac-sympy files ################################################################################ - foreach(srcfile ${CODAC_SYMPY_PUBLIC_HDR}) - file(COPY ${srcfile} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/../../../include) - endforeach() - + # Generating the file codac-sympy.h set(CODAC_SYMPY_MAIN_HEADER ${CMAKE_CURRENT_BINARY_DIR}/codac-sympy.h) - file(WRITE ${CODAC_SYMPY_MAIN_HEADER} "/* This file is generated by CMake */\n\n") - file(APPEND ${CODAC_SYMPY_MAIN_HEADER} "#pragma once\n\n") + codac_publish_generated_include_dirs(${CMAKE_CURRENT_BINARY_DIR}) + + # Generate the content of the umbrella header + set(CODAC_SYMPY_HEADER_CONTENT "/* This file is generated by CMake */\n\n#pragma once\n\n") foreach(header_path ${CODAC_SYMPY_PUBLIC_HDR}) get_filename_component(header_name ${header_path} NAME) - file(APPEND ${CODAC_SYMPY_MAIN_HEADER} "#include <${header_name}>\n") + string(APPEND CODAC_SYMPY_HEADER_CONTENT "#include <${header_name}>\n") endforeach() - file(COPY ${CODAC_SYMPY_MAIN_HEADER} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/../../../include) + + # Compare with the existing file, if any + if(EXISTS "${CODAC_SYMPY_MAIN_HEADER}") + file(READ "${CODAC_SYMPY_MAIN_HEADER}" EXISTING_CONTENT) + string(MD5 EXISTING_HASH "${EXISTING_CONTENT}") + string(MD5 NEW_HASH "${CODAC_SYMPY_HEADER_CONTENT}") + else() + set(EXISTING_HASH "") + set(NEW_HASH "new") + endif() + + # Only rewrite it when it changed + if(NOT EXISTING_HASH STREQUAL NEW_HASH) + file(WRITE "${CODAC_SYMPY_MAIN_HEADER}" "${CODAC_SYMPY_HEADER_CONTENT}") + endif() install(TARGETS ${PROJECT_NAME}-sympy DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES ${CODAC_SYMPY_PUBLIC_HDR} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}-sympy) diff --git a/src/graphics/CMakeLists.txt b/src/graphics/CMakeLists.txt index a31dd76f2..2f7c2a3d7 100644 --- a/src/graphics/CMakeLists.txt +++ b/src/graphics/CMakeLists.txt @@ -46,13 +46,22 @@ #endif() add_library(${PROJECT_NAME}-graphics ${CODAC_GRAPHICS_SRC}) - target_include_directories(${PROJECT_NAME}-graphics PUBLIC + + # The directories holding the public graphics headers, in their canonical + # location under src/graphics. Published so that the tests, the examples and + # the Python bindings reach them by this path rather than through their link + # in build/include -- see codac_publish_include_dirs() in the top-level + # CMakeLists.txt for why that decides what the coverage report is worth. + set(CODAC_GRAPHICS_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/3rd/ipe ${CMAKE_CURRENT_SOURCE_DIR}/3rd/vibes ${CMAKE_CURRENT_SOURCE_DIR}/figures ${CMAKE_CURRENT_SOURCE_DIR}/paver # deprecated, to be removed ${CMAKE_CURRENT_SOURCE_DIR}/styles ) + + target_include_directories(${PROJECT_NAME}-graphics PUBLIC ${CODAC_GRAPHICS_INCLUDE_DIRS}) + codac_publish_include_dirs(${CODAC_GRAPHICS_INCLUDE_DIRS}) target_link_libraries(${PROJECT_NAME}-graphics PUBLIC ${PROJECT_NAME}-core ${PROJECT_NAME}-eigen Codac::gaol ${PROJECT_NAME}-core) # Eigen3::Eigen @@ -73,21 +82,36 @@ foreach(srcfile ${CODAC_GRAPHICS_SRC}) if(srcfile MATCHES "\\.h$" OR srcfile MATCHES "\\.hpp$") list(APPEND CODAC_GRAPHICS_HDR ${srcfile}) - file(COPY ${srcfile} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/../../include) endif() endforeach() # Generating the file codac-graphics.h set(CODAC_GRAPHICS_MAIN_HEADER ${CMAKE_CURRENT_BINARY_DIR}/codac-graphics.h) + codac_publish_generated_include_dirs(${CMAKE_CURRENT_BINARY_DIR}) set(CODAC_MAIN_SUBHEADERS ${CODAC_MAIN_SUBHEADERS} "codac-graphics.h" PARENT_SCOPE) - file(WRITE ${CODAC_GRAPHICS_MAIN_HEADER} "/* This file is generated by CMake */\n\n") - file(APPEND ${CODAC_GRAPHICS_MAIN_HEADER} "#pragma once\n\n") + + # Generate the content of the umbrella header + set(CODAC_GRAPHICS_HEADER_CONTENT "/* This file is generated by CMake */\n\n#pragma once\n\n") foreach(header_path ${CODAC_GRAPHICS_HDR}) get_filename_component(header_name ${header_path} NAME) - file(APPEND ${CODAC_GRAPHICS_MAIN_HEADER} "#include <${header_name}>\n") + string(APPEND CODAC_GRAPHICS_HEADER_CONTENT "#include <${header_name}>\n") endforeach() - file(COPY ${CODAC_GRAPHICS_MAIN_HEADER} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/../../include) + + # Compare with the existing file, if any + if(EXISTS "${CODAC_GRAPHICS_MAIN_HEADER}") + file(READ "${CODAC_GRAPHICS_MAIN_HEADER}" EXISTING_CONTENT) + string(MD5 EXISTING_HASH "${EXISTING_CONTENT}") + string(MD5 NEW_HASH "${CODAC_GRAPHICS_HEADER_CONTENT}") + else() + set(EXISTING_HASH "") + set(NEW_HASH "new") + endif() + + # Only rewrite it when it changed + if(NOT EXISTING_HASH STREQUAL NEW_HASH) + file(WRITE "${CODAC_GRAPHICS_MAIN_HEADER}" "${CODAC_GRAPHICS_HEADER_CONTENT}") + endif() # Install files in system directories diff --git a/src/unsupported/CMakeLists.txt b/src/unsupported/CMakeLists.txt index f229909a1..451deeccb 100644 --- a/src/unsupported/CMakeLists.txt +++ b/src/unsupported/CMakeLists.txt @@ -18,13 +18,17 @@ #endif() add_library(${PROJECT_NAME}-unsupported ${CODAC_UNSUPPORTED_SRC}) - target_include_directories(${PROJECT_NAME}-unsupported PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/3rd/ipe - ${CMAKE_CURRENT_SOURCE_DIR}/3rd/vibes - ${CMAKE_CURRENT_SOURCE_DIR}/figures - ${CMAKE_CURRENT_SOURCE_DIR}/paver - ${CMAKE_CURRENT_SOURCE_DIR}/styles - ) + + # The directory holding the public unsupported headers. The list used to be + # a copy of the graphics one (3rd/ipe, figures, paver, styles), none of which + # exists here: this module keeps its single header at its own root, so that + # is what has to be on the include path -- and what consumers must reach it + # through rather than through its link in build/include, see + # codac_publish_include_dirs() in the top-level CMakeLists.txt. + set(CODAC_UNSUPPORTED_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}) + + target_include_directories(${PROJECT_NAME}-unsupported PUBLIC ${CODAC_UNSUPPORTED_INCLUDE_DIRS}) + codac_publish_include_dirs(${CODAC_UNSUPPORTED_INCLUDE_DIRS}) target_link_libraries(${PROJECT_NAME}-unsupported PUBLIC ${PROJECT_NAME}-core ${PROJECT_NAME}-eigen) # Eigen3::Eigen @@ -45,20 +49,35 @@ foreach(srcfile ${CODAC_UNSUPPORTED_SRC}) if(srcfile MATCHES "\\.h$" OR srcfile MATCHES "\\.hpp$") list(APPEND CODAC_UNSUPPORTED_HDR ${srcfile}) - file(COPY ${srcfile} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/../../include) endif() endforeach() # Generating the file codac-unsupported.h set(CODAC_UNSUPPORTED_MAIN_HEADER ${CMAKE_CURRENT_BINARY_DIR}/codac-unsupported.h) - file(WRITE ${CODAC_UNSUPPORTED_MAIN_HEADER} "/* This file is generated by CMake */\n\n") - file(APPEND ${CODAC_UNSUPPORTED_MAIN_HEADER} "#pragma once\n\n") + codac_publish_generated_include_dirs(${CMAKE_CURRENT_BINARY_DIR}) + + # Generate the content of the umbrella header + set(CODAC_UNSUPPORTED_HEADER_CONTENT "/* This file is generated by CMake */\n\n#pragma once\n\n") foreach(header_path ${CODAC_UNSUPPORTED_HDR}) get_filename_component(header_name ${header_path} NAME) - file(APPEND ${CODAC_UNSUPPORTED_MAIN_HEADER} "#include <${header_name}>\n") + string(APPEND CODAC_UNSUPPORTED_HEADER_CONTENT "#include <${header_name}>\n") endforeach() - file(COPY ${CODAC_UNSUPPORTED_MAIN_HEADER} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/../../include) + + # Compare with the existing file, if any + if(EXISTS "${CODAC_UNSUPPORTED_MAIN_HEADER}") + file(READ "${CODAC_UNSUPPORTED_MAIN_HEADER}" EXISTING_CONTENT) + string(MD5 EXISTING_HASH "${EXISTING_CONTENT}") + string(MD5 NEW_HASH "${CODAC_UNSUPPORTED_HEADER_CONTENT}") + else() + set(EXISTING_HASH "") + set(NEW_HASH "new") + endif() + + # Only rewrite it when it changed + if(NOT EXISTING_HASH STREQUAL NEW_HASH) + file(WRITE "${CODAC_UNSUPPORTED_MAIN_HEADER}" "${CODAC_UNSUPPORTED_HEADER_CONTENT}") + endif() # Install files in system directories diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 37ef5ac3c..e117c095f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -171,6 +171,37 @@ endif() #message(STATUS "Found IBEX version ${IBEX_VERSION}") #endif() + + +# Common Codac include directories +# ================================================================== +# +# IMPORTANT: +# Use the source tree as the canonical header tree. +# +# The headers are now used directly from the source tree. + +# CODAC_SOURCE_INCLUDE_DIRS is filled in by the src/*/CMakeLists.txt of every +# module (core, graphics, unsupported, and capd/sympy when enabled), which are +# all configured before this directory. Using it rather than a second copy of +# the same list keeps the tests compiling against exactly the headers the +# libraries were built from; the copy that used to live here covered core only +# and had already drifted from it. +set(CODAC_CORE_SOURCE_INCLUDE_DIRS ${CODAC_SOURCE_INCLUDE_DIRS}) + + +# ${CMAKE_BINARY_DIR}/src holds the generated "codac" umbrella header; +# CODAC_GENERATED_INCLUDE_DIRS, filled in by the src/*/CMakeLists.txt of every +# module, holds the per-module ones (codac-core.h and the rest), each of which +# is written into its own module's binary directory rather than into this one. +set( + CODAC_HEADERS_DIR + ${CMAKE_BINARY_DIR}/src + ${CODAC_GENERATED_INCLUDE_DIRS} +) + + + # ------------------------------------------------------------------ # Sanitizer runtime preload for Python tests (Debug builds only) # ------------------------------------------------------------------ @@ -414,22 +445,84 @@ if(CODAC_MSVC_ASAN_PATH_ENV) list(APPEND CODAC_CPP_TEST_ENV "${CODAC_MSVC_ASAN_PATH_ENV}") endif() + +# Build every test +# ================================================================== + foreach(SRC_TEST ${SRC_TESTS}) string(REPLACE "/" "_" TEST_NAME ${SRC_TEST}) string(REPLACE "codac2_tests_" "" TEST_NAME ${TEST_NAME}) set(TEST_NAME codac2_tests_${TEST_NAME}) + + # --------------------------------------------------------------- # C++ test - add_executable(${TEST_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${SRC_TEST}.cpp) - set(CODAC_HEADERS_DIR ${CMAKE_CURRENT_BINARY_DIR}/../include) - target_include_directories(${TEST_NAME} SYSTEM PUBLIC ${CODAC_HEADERS_DIR}) - target_link_libraries(${TEST_NAME} PUBLIC ${CODAC_LIBRARIES} PRIVATE Catch2::Catch2WithMain) + # --------------------------------------------------------------- + + add_executable( + ${TEST_NAME} + ${CMAKE_CURRENT_SOURCE_DIR}/${SRC_TEST}.cpp + ) + + + # Use the same canonical source headers as the libraries, and reach them by + # the same path: the source directories come first so that a header is + # opened at src/... Both compile identically, but the path used is the path gcov + # records, and different paths would split every header into two unrelated + # entries in the coverage report (see codac_publish_include_dirs() in the + # top-level CMakeLists.txt). + # + # CODAC_HEADERS_DIR is kept, after them, because it holds the generated + # umbrella headers: codac, codac-core.h, codac-graphics.h and the rest. + + target_include_directories( + ${TEST_NAME} + SYSTEM PUBLIC + + ${CODAC_CORE_SOURCE_INCLUDE_DIRS} + + ${CODAC_HEADERS_DIR} + ) + + + target_link_libraries( + ${TEST_NAME} + PUBLIC + + ${CODAC_LIBRARIES} + + PRIVATE + + Catch2::Catch2WithMain + ) + + + # Sympy embedded tests + if( + WITH_PYTHON + AND SRC_TEST MATCHES "extensions/sympy/" + ) + + target_link_libraries( + ${TEST_NAME} + PRIVATE + + ${PROJECT_NAME}-sympy + pybind11::embed + ) - if(WITH_PYTHON AND SRC_TEST MATCHES "extensions/sympy/") - target_link_libraries(${TEST_NAME} PRIVATE ${PROJECT_NAME}-sympy pybind11::embed) endif() - add_dependencies(check ${TEST_NAME}) - add_test(NAME ${TEST_NAME}_cpp COMMAND ${TEST_NAME}) + + + add_dependencies( + check + ${TEST_NAME} + ) + + add_test( + NAME ${TEST_NAME}_cpp + COMMAND ${TEST_NAME} + ) if(CODAC_CPP_TEST_ENV) set_tests_properties(${TEST_NAME}_cpp PROPERTIES @@ -437,7 +530,10 @@ foreach(SRC_TEST ${SRC_TESTS}) ) endif() + # --------------------------------------------------------------- # Python test + # --------------------------------------------------------------- + if(WITH_PYTHON AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_TEST}.py") add_test( NAME ${TEST_NAME}_py From 250ad16116113bb7beb1fa296d088c2be879f57a Mon Sep 17 00:00:00 2001 From: Jordan08 Date: Sat, 19 Sep 2026 13:37:35 +0200 Subject: [PATCH 6/6] Add the WITH_COVERAGE option and the coverage targets --- CMakeLists.txt | 151 ++++++++++++++++++++++++++ scripts/CMakeModules/codac_gaol.cmake | 18 +-- 2 files changed, 161 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c0af6ba65..4d5b16fc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -499,6 +499,157 @@ find_package(CAPD REQUIRED) endif() +################################################################################ +# Code coverage +################################################################################ + + # WITH_COVERAGE instruments the build and adds two targets: `coverage`, which + # runs the whole ctest suite and turns the counters gcov leaves behind into a + # report, and `coverage-report`, which only does the second half, so that a + # developer measuring coverage by hand and a continuous integration job + # reporting it use the same flags, the same gcov and the same filters, and + # cannot drift apart. Deciding all of that here + # rather than leaving it to the caller is also what spares everyone the traps + # below. + # + # Coverage answers "what do the tests reach", so it is worth turning + # BUILD_TESTS and TEST_EXAMPLES on with it -- the examples are integration + # tests and reach code the unit tests do not. + # + # This block has to come before the add_subdirectory() calls below: + # add_compile_options() is a directory property, and only reaches the targets + # created after it. Declared any later, it configures cleanly, builds + # cleanly, and leaves no .gcno file behind at all -- an empty report rather + # than an error. + option(WITH_COVERAGE "Instrument the build for code coverage (GCC/Clang)" OFF) + + if(WITH_COVERAGE) + + if(MSVC) + message(FATAL_ERROR "WITH_COVERAGE relies on gcov, which MSVC does not provide.") + endif() + + add_compile_options(--coverage) + add_link_options(--coverage) + + # pybind11 adds -flto to the modules it builds, and GCC cannot combine that + # with gcov instrumentation: the link then fails on an undefined vtable, + # the vtable being emitted nowhere once both are on. pybind11 leaves LTO + # alone as soon as CMAKE_INTERPROCEDURAL_OPTIMIZATION is set, which is what + # this does -- silently, because a coverage build that does not link is of + # no use to anyone. + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION OFF) + + # gcov itself is toolchain-specific: the notes and counters Clang writes are + # read by `llvm-cov gcov`, not by GCC's gcov, and pointing gcovr at the + # wrong one yields an empty report rather than an error. The one shipped + # next to the compiler in use is therefore named explicitly. + set(CODAC_GCOV_ARG "") + if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") + # Debian and its derivatives ship the tools of each LLVM release under a + # versioned name in /usr/bin and unversioned in the release's own prefix, + # so both spellings and both places are tried, the version being taken + # from the compiler that will produce the notes. + get_filename_component(_codac_cxx_dir "${CMAKE_CXX_COMPILER}" DIRECTORY) + string(REGEX MATCH "^[0-9]+" _codac_llvm_major "${CMAKE_CXX_COMPILER_VERSION}") + find_program(LLVM_COV_EXECUTABLE + NAMES llvm-cov llvm-cov-${_codac_llvm_major} + HINTS ${_codac_cxx_dir} + /usr/lib/llvm-${_codac_llvm_major}/bin) + if(LLVM_COV_EXECUTABLE) + set(CODAC_GCOV_ARG --gcov-executable "${LLVM_COV_EXECUTABLE} gcov") + else() + message(STATUS "${COLOR_RED}llvm-cov was not found next to ${CMAKE_CXX_COMPILER}: the coverage report would come out empty.${COLOR_RESET}") + endif() + endif() + + find_program(GCOVR_EXECUTABLE gcovr) + + if(NOT GCOVR_EXECUTABLE) + + message(STATUS "${COLOR_RED}gcovr was not found: the build is instrumented, but the `coverage` target is unavailable (pip install gcovr).${COLOR_RESET}") + + else() + + # A note on paths, since it decides what the report is worth: gcov + # records the path each translation unit actually opened a header + # through, so a header reached both directly and through a copy in + # the build tree comes out as two unrelated entries, each showing + # only what its own callers exercised. Every consumer (tests, + # examples, Python bindings) therefore lists CODAC_SOURCE_INCLUDE_DIRS + # first -- see codac_publish_include_dirs() above. Without that, a + # header was reported well below its real coverage, and every header + # of graphics/, unsupported/ and extensions/ was measured on a + # build-tree copy that is not the file anyone edits. + # + # --gcov-exclude-directories keeps gcovr from even descending into the + # dependencies FetchContent puts in the build tree, which --exclude alone + # would only drop from the finished report. It matters beyond noise: + # Catch2 compiles with -ffile-prefix-map, so the paths recorded in its + # notes do not resolve from anywhere and gcov fails outright on them; and + # gcov reports negative hit counts on heavily inlined code, Eigen being + # where that first shows, which gcovr treats as fatal unless told + # otherwise. The pattern has no leading ".*/" on purpose: gcovr matches it + # against a path relative to the working directory, which here starts with + # "_deps/" and so has nothing for that slash to match. + set(CODAC_GCOVR_COMMAND + ${GCOVR_EXECUTABLE} + --root ${CMAKE_SOURCE_DIR} + ${CODAC_GCOV_ARG} + --gcov-exclude-directories ".*_deps.*" + --gcov-ignore-parse-errors=negative_hits.warn_once_per_file + --exclude ".*/tests/.*" + --exclude ".*/examples/.*" + --exclude ".*/python/.*" + --print-summary + --html-details ${CMAKE_BINARY_DIR}/coverage.html + --xml ${CMAKE_BINARY_DIR}/coverage.xml + # Where to look for the counters. Without it gcovr falls back on + # --root, i.e. the source tree, and walks every build directory + # sitting under it: a second configuration kept next to this one + # (a Debug tree, an older coverage tree) then has its own .gcda + # read into this report, mixing counters produced by a different + # build of different sources. "." rather than an absolute path + # because the working directory below is already the build tree, + # and because --gcov-exclude-directories above matches against + # paths relative to it. + .) + + # Two targets rather than one, because the counters gcov leaves behind + # accumulate across runs and are read independently of what produced + # them: `coverage` is the whole thing, `coverage-report` re-reads whatever + # the last ctest invocation happened to touch. The second is what to use + # after running part of the suite by hand -- ctest -R something -- which + # is the usual way of asking what one test reaches. + # + # VERBATIM on both, because the arguments above are regular expressions: + # without it CMake hands them to the shell unquoted, which expands + # ".*/tests/.*" as a file pattern -- gcovr then receives "./tests/.." and + # refuses it. + add_custom_target(coverage + COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure + COMMAND ${CODAC_GCOVR_COMMAND} + COMMAND ${CMAKE_COMMAND} -E echo + "Coverage report: ${CMAKE_BINARY_DIR}/coverage.html (and coverage.xml, Cobertura)" + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + COMMENT "Running the tests and measuring what they reach" + VERBATIM) + + add_custom_target(coverage-report + COMMAND ${CODAC_GCOVR_COMMAND} + COMMAND ${CMAKE_COMMAND} -E echo + "Coverage report: ${CMAKE_BINARY_DIR}/coverage.html (and coverage.xml, Cobertura)" + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + COMMENT "Turning the counters left by the last test run into a report" + VERBATIM) + + message(STATUS "${COLOR_BLUE}Coverage instrumentation enabled; run `make coverage` to produce the report.${COLOR_RESET}") + + endif() + + endif() + + ################################################################################ # Compile sources ################################################################################ diff --git a/scripts/CMakeModules/codac_gaol.cmake b/scripts/CMakeModules/codac_gaol.cmake index 1b822ba32..276d89eea 100644 --- a/scripts/CMakeModules/codac_gaol.cmake +++ b/scripts/CMakeModules/codac_gaol.cmake @@ -184,14 +184,16 @@ endfunction() # CMakeLists.txt had to force the Release runtime on Codac while GAOL was built # apart, in Release. # -# Not compiled with Codac's warnings, sanitizers and definitions, though (/W4, -# -Wall -Wextra -Wpedantic, /bigobj, -fsanitize=address,undefined and -# /fsanitize=address in Debug, FAST_RELEASE...): the top-level CMakeLists.txt -# adds them with add_compile_options() and add_compile_definitions(), whose -# directory properties a subdirectory takes from its parent when it is added. -# They are emptied while GAOL is added, and given back right after: GAOL is -# compiled with the flags it chooses, and with CMAKE_CXX_FLAGS as it is given to -# this build (codac_gaol_portability_flags() is added to it afterwards). +# Not compiled with Codac's warnings, sanitizers, coverage instrumentation and +# definitions, though (/W4, -Wall -Wextra -Wpedantic, /bigobj, +# -fsanitize=address,undefined and /fsanitize=address in Debug, --coverage with +# WITH_COVERAGE, FAST_RELEASE...): the top-level CMakeLists.txt adds them with +# add_compile_options() and add_compile_definitions(), whose directory +# properties a subdirectory takes from its parent when it is added. They are +# emptied while GAOL is added, and given back right after, and --coverage is +# only added afterwards: GAOL is compiled with the flags it chooses, and with +# CMAKE_CXX_FLAGS as it is given to this build (codac_gaol_portability_flags() +# is added to it afterwards). # # From the head of the master branch, so that the fixes pushed to GAOL reach # Codac without a change here. Cloned with Git, the sources are brought up to