opencv/cmake
Maksim Shabunin 4c81e174bf
Merge pull request #25901 from mshabunin:fix-riscv-aarch-baseline
RISC-V/AArch64: disable CPU features detection #25901

This PR is the first step in fixing current issues with NEON/RVV, FP16, BF16 and other CPU features on AArch64 and RISC-V platforms.

On AArch64 and RISC-V platforms we usually have the platform set by default in the toolchain when we compile it or in the cmake toolchain file or in CMAKE_CXX_FLAGS by user. Then, there are two ways to set platform options: a) "-mcpu=<some_cpu>" ; b) "-march=<arch description>" (e.g. "rv64gcv"). Furthermore, there are no similar "levels" of optimizations as for x86_64, instead we have features (RVV, FP16,...) which can be enabled or disabled. So, for example, if a user has "rv64gc" set by the toolchain and we want to enable RVV. Then we need to somehow parse their current feature set and append "v" (vector optimizations) to this string. This task is quite hard and the whole procedure is prone to errors.

I propose to use "CPU_BASELINE=DETECT" by default on AArch64 and RISC-V platforms. And somehow remove other features or make them read-only/detect-only, so that OpenCV wouldn't add any extra "-march" flags to the default configuration. We would rely only on the flags provided by the compiler and cmake toolchain file. We can have some predefined configurations in our cmake toolchain files.

Changes made by this PR:
- `CMakeLists.txt`: 
  - use `CMAKE_CROSSCOMPILING` instead of `CMAKE_TOOLCHAIN_FILE` to detect cross-compilation. This might be useful in cases of native compilation with a toolchain file
  - removed obsolete variables `ENABLE_NEON` and `ENABLE_VFPV3`, the first one have been turned ON by default on AArch64 platform which caused setting `CPU_BASELINE=NEON`
  - raise minimum cmake version allowed to 3.7 to allow using `CMAKE_CXX_FLAGS_INIT` in toolchain files
- added separate files with arch flags for native compilation on AArch64 and RISC-V, these files will be used in our toolchain files and in regular cmake
- use `DETECT` as default value for `CPU_BASELINE` also allow `NATIVE`, warn user if other values were used (only for AArch64 and RISC-V)
- for each feature listed in `CPU_DISPATCH` check if corresponding `CPU_${opt}_FLAGS_ON` has been provided, warn user if it is empty (only for AArch64 and RISC-V)
- use `CPU_BASELINE_DISABLE` variable to actually turn off macros responsible for corresponding features even if they are enabled by compiler
- removed Aarch64 feature merge procedure (it didn't support `-mcpu` and built-in `-march`)
- reworked AArch64 and two RISC-V cmake toolchain files (does not affect Android/OSX/iOS/Win):
  - use `CMAKE_CXX_FLAGS_INIT` to set compiler flags
  - use variables `ENABLE_BF16`, `ENABLE_DOTPROD`, `ENABLE_RVV`, `ENABLE_FP16` to control `-march`
  - AArch64: removed other compiler and linker flags
    - `-fdata-sections`, `-fsigned-char`, `-Wl,--no-undefined`, `-Wl,--gc-sections`   - already set by OpenCV
    - `-Wa,--noexecstack`, `-Wl,-z,noexecstack`, `-Wl,-z,relro`, `-Wl,-z,now` - can be enabled by OpenCV via `ENABLE_HARDENING`
    - `-Wno-psabi` - this option used to disable some warnings on older ARM platforms, shouldn't harm
  - ARM: removed same common flags as for AArch64, but left `-mthumb` and `--fix-cortex-a8`, `-z nocopyreloc`
2024-09-12 18:07:24 +03:00
..
android Merge pull request #24930 from asmorkalov:as/android_enable_native_aar 2024-02-22 09:31:21 +03:00
checks Merge pull request #25930 from opencv-pushbot:gitee/alalek/cmake_try_detect_feature_without_flags 2024-08-01 20:09:49 +00:00
FindCUDA FIx misc. source and comment typos 2019-08-15 13:09:52 +03:00
mirrors Merge pull request #24122 from fengyuentau:remove_tengine 2023-08-09 09:26:02 +03:00
platforms Merge pull request #25901 from mshabunin:fix-riscv-aarch-baseline 2024-09-12 18:07:24 +03:00
templates Support VS 2022 17.1x.y 2024-05-23 16:05:19 +02:00
vars Merge pull request #20392 from fpetrogalli:aarch64-semihosting 2021-07-21 18:46:05 +03:00
cl2cpp.cmake cmake: don't add include <module>/src directory to avoid conflicts 2018-03-19 11:14:15 +03:00
copy_files.cmake build: w/a compiler warnings for GCC 11-12 and Clang 13, reduce build output 2023-07-10 11:27:59 +03:00
FindCUDA.cmake Merge pull request #22615 from cudawarped:nvcuvenc 2022-10-28 11:03:51 +03:00
FindCUDNN.cmake Do not store full CuDNN version in cache to exclude inconsistency during reconfiguration. 2022-09-15 16:15:52 +03:00
FindFlake8.cmake cmake: fix pylint/flake8 detection, update flake8 exclude 2021-01-29 17:06:54 +00:00
FindONNX.cmake Merge pull request #24601 from TolyaTalamanov:at/fix-onnxrt-include-path-in-cmake 2023-12-15 16:21:01 +03:00
FindOpenVX.cmake
FindPylint.cmake cmake: update Python linters handling 2021-03-05 12:54:51 +00:00
FindVulkan.cmake Merge pull request #12703 from wzw-intel:vkcom 2018-10-29 17:51:26 +03:00
OpenCVCompilerDefenses.cmake Do not force -O2 flag in hardening-enabled builds 2021-12-16 11:40:42 +03:00
OpenCVCompilerOptimizations.cmake Merge pull request #25901 from mshabunin:fix-riscv-aarch-baseline 2024-09-12 18:07:24 +03:00
OpenCVCompilerOptions.cmake Merge pull request #25901 from mshabunin:fix-riscv-aarch-baseline 2024-09-12 18:07:24 +03:00
OpenCVCRTLinkage.cmake build: first class cuda support 2023-12-26 09:39:18 +03:00
OpenCVDetectApacheAnt.cmake FIx misc. source and comment typos 2019-08-15 13:09:52 +03:00
OpenCVDetectCUDA.cmake Fix 24789 2023-12-29 16:58:05 +02:00
OpenCVDetectCUDALanguage.cmake build: first class cuda support 2023-12-26 09:39:18 +03:00
OpenCVDetectCUDAUtils.cmake build: first class cuda support 2023-12-26 09:39:18 +03:00
OpenCVDetectCXXCompiler.cmake build: minor changes for cmake 3.30 and some cleanup 2024-09-03 16:35:45 +03:00
OpenCVDetectDirectML.cmake Merge pull request #24060 from TolyaTalamanov:at/advanced-device-selection-onnxrt-directml 2023-11-16 08:49:53 +03:00
OpenCVDetectDirectX.cmake Merge pull request #22462 from Biswa96:fix-directx-check 2022-10-03 08:37:36 +03:00
OpenCVDetectFlatbuffers.cmake Update FlatBuffers source code to 23.5.9 2023-05-10 14:39:36 +03:00
OpenCVDetectHalide.cmake build: suppress the warning 2022-07-12 19:42:39 +09:00
OpenCVDetectInferenceEngine.cmake Fix for OpenVINO 2024.0 2024-03-18 15:05:50 +04:00
OpenCVDetectOpenCL.cmake Fixed and updated OpenCL-VA interoperability 2020-09-25 16:11:50 +03:00
OpenCVDetectPython.cmake Downgrade LIMITED_API_VERSION, if python3 is older than 3.6. 2024-01-17 13:09:08 +03:00
OpenCVDetectTBB.cmake core(parallel): plugins support 2021-02-15 17:07:36 +00:00
OpenCVDetectTrace.cmake
OpenCVDetectVTK.cmake Merge remote-tracking branch 'upstream/3.4' into merge-3.4 2021-10-04 19:50:01 +00:00
OpenCVDetectVulkan.cmake speed up vulkan dnn, and support ios and apple m1 chip. (#23349) 2023-05-18 20:02:27 +03:00
OpenCVDetectWebNN.cmake Merge pull request #20406 from MarkGHX:gsoc_2021_webnn 2021-11-23 21:15:31 +00:00
OpenCVDownload.cmake build: minor changes for cmake 3.30 and some cleanup 2024-09-03 16:35:45 +03:00
OpenCVExtraTargets.cmake cmake: update generation of 'uninstall' target 2020-04-07 22:03:12 +00:00
OpenCVFindAtlas.cmake
OpenCVFindAVIF.cmake Merge pull request #23596 from vrabaud:libavif 2023-06-09 15:39:10 +03:00
OpenCVFindCANN.cmake link libacl_dvpp_mpi library when building with CANN backend 2023-12-19 17:35:11 +08:00
OpenCVFindFrameworks.cmake Merge pull request #23095 from fengyuentau:fix_omp_macos 2023-01-16 12:44:13 +03:00
OpenCVFindIPP.cmake Enable build with both old and new layouts of IPP 2024-06-17 03:26:01 -07:00
OpenCVFindIPPIW.cmake Merge pull request #16769 from mshabunin:fix-ipp-install 2020-04-17 14:28:42 +00:00
OpenCVFindLAPACK.cmake Merge pull request #25625 from fengyuentau:core/deploy_fix_lapack_ilp64 2024-05-30 17:03:07 +03:00
OpenCVFindLATEX.cmake
OpenCVFindLibsGrfmt.cmake Merge pull request #25832 from chachoi-world:4.x 2024-08-06 20:25:39 +03:00
OpenCVFindLibsGUI.cmake Merge pull request #25822 from mqcmd196:gtk3-gl-support 2024-07-15 17:06:30 +03:00
OpenCVFindLibsPerf.cmake build: first class cuda support 2023-12-26 09:39:18 +03:00
OpenCVFindLibsVideo.cmake Fixed and updated OpenCL-VA interoperability 2020-09-25 16:11:50 +03:00
OpenCVFindMKL.cmake cmake: workaround oneAPI problem with -DMKL_WITH_TBB=ON 2021-02-03 19:41:54 +00:00
OpenCVFindOpenBLAS.cmake Update OpenCVFindOpenBLAS.cmake to accomodate alternative lib name 2023-09-14 09:04:41 +02:00
OpenCVFindOpenEXR.cmake Merge remote-tracking branch 'upstream/3.4' into merge-3.4 2021-12-24 16:48:07 +00:00
OpenCVFindProtobuf.cmake Merge pull request #24372 from Kumataro:fix24369 2023-10-19 08:45:08 +03:00
OpenCVFindTIMVX.cmake Disable default path while search libtim-vx.so with TIMVX_INSTALL_DIR 2022-09-13 11:11:49 +08:00
OpenCVFindVA.cmake core(libva): use dynamic loader 2021-02-19 10:32:59 +00:00
OpenCVFindWebP.cmake update CMakeList.txt 2018-02-05 16:23:52 +03:00
OpenCVFindXimea.cmake Merge pull request #13422 from mshabunin:split-videoio-cmake 2018-12-26 15:50:20 +03:00
OpenCVGenABI.cmake cmake: support OPENCV_ABI_SKIP_MODULES_LIST 2020-01-27 14:40:48 +03:00
OpenCVGenAndroidMK.cmake next(android): java3 -> java4 2018-04-10 18:09:54 +03:00
OpenCVGenConfig.cmake build: first class cuda support 2023-12-26 09:39:18 +03:00
OpenCVGenHeaders.cmake
OpenCVGenInfoPlist.cmake Merge pull request #24136 from komakai:visionos_support 2023-12-20 15:35:10 +03:00
OpenCVGenPkgconfig.cmake Merge pull request #24136 from komakai:visionos_support 2023-12-20 15:35:10 +03:00
OpenCVGenSetupVars.cmake Merge remote-tracking branch 'upstream/3.4' into merge-3.4 2018-12-18 19:07:43 +00:00
OpenCVInstallLayout.cmake Merge pull request #25832 from chachoi-world:4.x 2024-08-06 20:25:39 +03:00
OpenCVMinDepVersions.cmake Merge pull request #25901 from mshabunin:fix-riscv-aarch-baseline 2024-09-12 18:07:24 +03:00
OpenCVModule.cmake build: first class cuda support 2023-12-26 09:39:18 +03:00
OpenCVPackaging.cmake use CPACK_PACKAGE_VERSION instead of OPENCV_VCSVERSION for CPACK_PACKAGE_FILE_NAME so that OPENCV_CUSTOM_PACKAGE_INFO actually has full effect 2023-07-04 07:41:16 +00:00
OpenCVPCHSupport.cmake cmake: use upstream PCH support if possible 2022-10-12 17:01:28 +02:00
OpenCVPluginStandalone.cmake Option to enable/disable plugin linking with OpenCV 2022-02-01 14:53:15 +03:00
OpenCVPylint.cmake cmake: update Python linters handling 2021-03-05 12:54:51 +00:00
OpenCVUtils.cmake Merge pull request #25832 from chachoi-world:4.x 2024-08-06 20:25:39 +03:00
OpenCVVersion.cmake cmake: update SOVERSION 2021-12-10 16:14:56 +00:00