Browse Source

cmake: allow lower language versions when building for linux target

Ivan Grokhotkov 3 năm trước cách đây
mục cha
commit
7f971429bc
2 tập tin đã thay đổi với 48 bổ sung4 xóa
  1. 46 4
      tools/cmake/build.cmake
  2. 2 0
      tools/cmake/idf.cmake

+ 46 - 4
tools/cmake/build.cmake

@@ -117,16 +117,57 @@ function(__build_set_default_build_specifications)
                                     # go into the final binary so have no impact on size
                                     "-ggdb")
 
-    list(APPEND c_compile_options   "-std=gnu17")
-
-    list(APPEND cxx_compile_options "-std=gnu++20")
-
     idf_build_set_property(COMPILE_DEFINITIONS "${compile_definitions}" APPEND)
     idf_build_set_property(COMPILE_OPTIONS "${compile_options}" APPEND)
     idf_build_set_property(C_COMPILE_OPTIONS "${c_compile_options}" APPEND)
     idf_build_set_property(CXX_COMPILE_OPTIONS "${cxx_compile_options}" APPEND)
 endfunction()
 
+function(__build_set_lang_version)
+    if(NOT IDF_TARGET STREQUAL "linux")
+        # Building for chip targets: we use a known version of the toolchain.
+        # Use latest supported versions.
+        set(c_std gnu17)
+        set(cxx_std gnu++20)
+    else()
+        enable_language(C CXX)
+        # Building for Linux target, fall back to an older version of the standard
+        # if the preferred one is not supported by the compiler.
+        set(preferred_c_versions gnu17 gnu11 gnu99)
+        set(ver_found FALSE)
+        foreach(c_version ${preferred_c_versions})
+            check_c_compiler_flag("-std=${c_version}" ver_${c_version}_supported)
+            if(ver_${c_version}_supported)
+                set(c_std ${c_version})
+                set(ver_found TRUE)
+                break()
+            endif()
+        endforeach()
+        if(NOT ver_found)
+            message(FATAL_ERROR "Failed to set C language standard to one of the supported versions: "
+                                "${preferred_c_versions}. Please upgrade the host compiler.")
+        endif()
+
+        set(preferred_cxx_versions gnu++20 gnu++2a gnu++17 gnu++14)
+        set(ver_found FALSE)
+        foreach(cxx_version ${preferred_cxx_versions})
+            check_cxx_compiler_flag("-std=${cxx_version}" ver_${cxx_version}_supported)
+            if(ver_${cxx_version}_supported)
+                set(cxx_std ${cxx_version})
+                set(ver_found TRUE)
+                break()
+            endif()
+        endforeach()
+        if(NOT ver_found)
+            message(FATAL_ERROR "Failed to set C++ language standard to one of the supported versions: "
+                                "${preferred_cxx_versions}. Please upgrade the host compiler.")
+        endif()
+    endif()
+
+    idf_build_set_property(C_COMPILE_OPTIONS "-std=${c_std}" APPEND)
+    idf_build_set_property(CXX_COMPILE_OPTIONS "-std=${cxx_std}" APPEND)
+endfunction()
+
 #
 # Initialize the build. This gets called upon inclusion of idf.cmake to set internal
 # properties used for the processing phase of the build.
@@ -151,6 +192,7 @@ function(__build_init idf_path)
     idf_build_set_property(IDF_COMPONENT_MANAGER 0)
 
     __build_set_default_build_specifications()
+    __build_set_lang_version()
 
     # Add internal components to the build
     idf_build_get_property(idf_path IDF_PATH)

+ 2 - 0
tools/cmake/idf.cmake

@@ -36,6 +36,8 @@ if(NOT __idf_env_set)
     set(IDF_PATH ${idf_path})
 
     include(GetGitRevisionDescription)
+    include(CheckCCompilerFlag)
+    include(CheckCXXCompilerFlag)
     include(git_submodules)
     include(crosstool_version_check)
     include(kconfig)