This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit b6eda1b89d9b6511561d5c693e86850827f99595
Author: pangzhen1 <[email protected]>
AuthorDate: Fri Nov 20 12:25:14 2026 +0800

    arch: share NUTTX_EXTRA_FLAGS collection across arches
    
    The logic that collects the directory-level COMPILE_OPTIONS (e.g. -m32,
    -march, -mcpu) into NUTTX_EXTRA_FLAGS was duplicated in the
    platform.cmake of arm, risc-v, tricore, x86_64, xtensa and rh850, and sim
    did not publish the flags at all.
    
    Factor the shared logic into cmake/nuttx_collect_extra_flags.cmake and
    call it from those arches and from sim, so that every arch publishes the
    same flags through the GLOBAL property and steps that invoke the
    compiler directly through add_custom_command get the flags the image is
    built with (for example -m32 on sim).  tricore and rh850 keep their
    -nostdlib exclusion via the new EXCLUDE argument.  rh850 now also reads
    the options from NUTTX_DIR and removes duplicates, like the other
    arches.
    
    arm64 is left untouched: it collects the options into CMAKE_C_FLAG_ARGS
    instead, so unifying it needs a separate change.
    
    Signed-off-by: pangzhen1 <[email protected]>
---
 arch/arm/src/cmake/platform.cmake     | 24 +---------
 arch/rh850/src/cmake/platform.cmake   | 22 +--------
 arch/risc-v/src/cmake/platform.cmake  | 24 +---------
 arch/sim/src/cmake/platform.cmake     |  2 +
 arch/tricore/src/cmake/platform.cmake | 26 +----------
 arch/x86_64/src/cmake/platform.cmake  | 24 +---------
 arch/xtensa/src/cmake/platform.cmake  | 24 +---------
 cmake/nuttx_collect_extra_flags.cmake | 84 +++++++++++++++++++++++++++++++++++
 cmake/nuttx_toolchain.cmake           |  2 +
 9 files changed, 94 insertions(+), 138 deletions(-)

diff --git a/arch/arm/src/cmake/platform.cmake 
b/arch/arm/src/cmake/platform.cmake
index 8656850791f..6ce83cf86df 100644
--- a/arch/arm/src/cmake/platform.cmake
+++ b/arch/arm/src/cmake/platform.cmake
@@ -62,29 +62,7 @@ else()
   message(FATAL_ERROR "CMAKE_SYSTEM_PROCESSOR not set")
 endif()
 
-get_directory_property(TOOLCHAIN_DIR_FLAGS DIRECTORY ${NUTTX_DIR}
-                                                     COMPILE_OPTIONS)
-
-set(NUTTX_EXTRA_FLAGS "")
-foreach(FLAG ${TOOLCHAIN_DIR_FLAGS})
-  if(FLAG IN_LIST COMPILE_ONLY_FLAGS)
-    continue()
-  endif()
-  if(NOT FLAG MATCHES "^\\$<.*>$")
-    list(APPEND NUTTX_EXTRA_FLAGS ${FLAG})
-  else()
-    string(REGEX MATCH "\\$<\\$<COMPILE_LANGUAGE:C>:(.*)>" matched ${FLAG})
-    if(matched)
-      list(APPEND NUTTX_EXTRA_FLAGS ${CMAKE_MATCH_1})
-    endif()
-  endif()
-endforeach()
-
-if(NUTTX_EXTRA_FLAGS)
-  list(REMOVE_DUPLICATES NUTTX_EXTRA_FLAGS)
-endif()
-
-set_property(GLOBAL PROPERTY NUTTX_EXTRA_FLAGS ${NUTTX_EXTRA_FLAGS})
+nuttx_collect_extra_flags()
 
 separate_arguments(CMAKE_C_FLAG_ARGS NATIVE_COMMAND ${CMAKE_C_FLAGS})
 
diff --git a/arch/rh850/src/cmake/platform.cmake 
b/arch/rh850/src/cmake/platform.cmake
index 22b091b4607..908d1d78ca0 100644
--- a/arch/rh850/src/cmake/platform.cmake
+++ b/arch/rh850/src/cmake/platform.cmake
@@ -20,27 +20,7 @@
 #
 # 
##############################################################################
 
-get_directory_property(TOOLCHAIN_DIR_FLAGS DIRECTORY ${CMAKE_SOURCE_DIR}
-                                                     COMPILE_OPTIONS)
-
-set(NUTTX_EXTRA_FLAGS "")
-foreach(FLAG ${TOOLCHAIN_DIR_FLAGS})
-  if(FLAG IN_LIST COMPILE_ONLY_FLAGS)
-    continue()
-  endif()
-  if(NOT FLAG MATCHES "^\\$<.*>$")
-    if(NOT FLAG MATCHES "-nostdlib")
-      list(APPEND NUTTX_EXTRA_FLAGS ${FLAG})
-    endif()
-  else()
-    string(REGEX MATCH "\\$<\\$<COMPILE_LANGUAGE:C>:(.*)>" matched ${FLAG})
-    if(matched)
-      list(APPEND NUTTX_EXTRA_FLAGS ${CMAKE_MATCH_1})
-    endif()
-  endif()
-endforeach()
-
-set_property(GLOBAL PROPERTY NUTTX_EXTRA_FLAGS ${NUTTX_EXTRA_FLAGS})
+nuttx_collect_extra_flags(EXCLUDE "-nostdlib")
 
 separate_arguments(CMAKE_C_FLAG_ARGS NATIVE_COMMAND ${CMAKE_C_FLAGS})
 
diff --git a/arch/risc-v/src/cmake/platform.cmake 
b/arch/risc-v/src/cmake/platform.cmake
index 08fc17c544d..80321a15cba 100644
--- a/arch/risc-v/src/cmake/platform.cmake
+++ b/arch/risc-v/src/cmake/platform.cmake
@@ -20,29 +20,7 @@
 #
 # 
##############################################################################
 
-get_directory_property(TOOLCHAIN_DIR_FLAGS DIRECTORY ${NUTTX_DIR}
-                                                     COMPILE_OPTIONS)
-
-set(NUTTX_EXTRA_FLAGS "")
-foreach(FLAG ${TOOLCHAIN_DIR_FLAGS})
-  if(FLAG IN_LIST COMPILE_ONLY_FLAGS)
-    continue()
-  endif()
-  if(NOT FLAG MATCHES "^\\$<.*>$")
-    list(APPEND NUTTX_EXTRA_FLAGS ${FLAG})
-  else()
-    string(REGEX MATCH "\\$<\\$<COMPILE_LANGUAGE:C>:(.*)>" matched ${FLAG})
-    if(matched)
-      list(APPEND NUTTX_EXTRA_FLAGS ${CMAKE_MATCH_1})
-    endif()
-  endif()
-endforeach()
-
-if(NUTTX_EXTRA_FLAGS)
-  list(REMOVE_DUPLICATES NUTTX_EXTRA_FLAGS)
-endif()
-
-set_property(GLOBAL PROPERTY NUTTX_EXTRA_FLAGS ${NUTTX_EXTRA_FLAGS})
+nuttx_collect_extra_flags()
 
 separate_arguments(CMAKE_C_FLAG_ARGS NATIVE_COMMAND ${CMAKE_C_FLAGS})
 
diff --git a/arch/sim/src/cmake/platform.cmake 
b/arch/sim/src/cmake/platform.cmake
index 0f525e39e00..3fc510d33d0 100644
--- a/arch/sim/src/cmake/platform.cmake
+++ b/arch/sim/src/cmake/platform.cmake
@@ -21,3 +21,5 @@
 # 
##############################################################################
 
 set(PREPROCESS ${CMAKE_C_COMPILER} ${CMAKE_C_FLAG_ARGS} -E -P -x c)
+
+nuttx_collect_extra_flags()
diff --git a/arch/tricore/src/cmake/platform.cmake 
b/arch/tricore/src/cmake/platform.cmake
index bad8da7036f..65abd41a334 100644
--- a/arch/tricore/src/cmake/platform.cmake
+++ b/arch/tricore/src/cmake/platform.cmake
@@ -20,31 +20,7 @@
 #
 # 
##############################################################################
 
-get_directory_property(TOOLCHAIN_DIR_FLAGS DIRECTORY ${NUTTX_DIR}
-                                                     COMPILE_OPTIONS)
-
-set(NUTTX_EXTRA_FLAGS "")
-foreach(FLAG ${TOOLCHAIN_DIR_FLAGS})
-  if(FLAG IN_LIST COMPILE_ONLY_FLAGS)
-    continue()
-  endif()
-  if(NOT FLAG MATCHES "^\\$<.*>$")
-    if(NOT FLAG MATCHES "-nostdlib")
-      list(APPEND NUTTX_EXTRA_FLAGS ${FLAG})
-    endif()
-  else()
-    string(REGEX MATCH "\\$<\\$<COMPILE_LANGUAGE:C>:(.*)>" matched ${FLAG})
-    if(matched)
-      list(APPEND NUTTX_EXTRA_FLAGS ${CMAKE_MATCH_1})
-    endif()
-  endif()
-endforeach()
-
-if(NUTTX_EXTRA_FLAGS)
-  list(REMOVE_DUPLICATES NUTTX_EXTRA_FLAGS)
-endif()
-
-set_property(GLOBAL PROPERTY NUTTX_EXTRA_FLAGS ${NUTTX_EXTRA_FLAGS})
+nuttx_collect_extra_flags(EXCLUDE "-nostdlib")
 
 separate_arguments(CMAKE_C_FLAG_ARGS NATIVE_COMMAND ${CMAKE_C_FLAGS})
 
diff --git a/arch/x86_64/src/cmake/platform.cmake 
b/arch/x86_64/src/cmake/platform.cmake
index 90a7cdb7305..7c83e5dfddc 100644
--- a/arch/x86_64/src/cmake/platform.cmake
+++ b/arch/x86_64/src/cmake/platform.cmake
@@ -20,29 +20,7 @@
 #
 # 
##############################################################################
 
-get_directory_property(TOOLCHAIN_DIR_FLAGS DIRECTORY ${NUTTX_DIR}
-                                                     COMPILE_OPTIONS)
-
-set(NUTTX_EXTRA_FLAGS "")
-foreach(FLAG ${TOOLCHAIN_DIR_FLAGS})
-  if(FLAG IN_LIST COMPILE_ONLY_FLAGS)
-    continue()
-  endif()
-  if(NOT FLAG MATCHES "^\\$<.*>$")
-    list(APPEND NUTTX_EXTRA_FLAGS ${FLAG})
-  else()
-    string(REGEX MATCH "\\$<\\$<COMPILE_LANGUAGE:C>:(.*)>" matched ${FLAG})
-    if(matched)
-      list(APPEND NUTTX_EXTRA_FLAGS ${CMAKE_MATCH_1})
-    endif()
-  endif()
-endforeach()
-
-if(NUTTX_EXTRA_FLAGS)
-  list(REMOVE_DUPLICATES NUTTX_EXTRA_FLAGS)
-endif()
-
-set_property(GLOBAL PROPERTY NUTTX_EXTRA_FLAGS ${NUTTX_EXTRA_FLAGS})
+nuttx_collect_extra_flags()
 
 separate_arguments(CMAKE_C_FLAG_ARGS NATIVE_COMMAND ${CMAKE_C_FLAGS})
 
diff --git a/arch/xtensa/src/cmake/platform.cmake 
b/arch/xtensa/src/cmake/platform.cmake
index dcefedd54a9..545a19bdc72 100644
--- a/arch/xtensa/src/cmake/platform.cmake
+++ b/arch/xtensa/src/cmake/platform.cmake
@@ -19,29 +19,7 @@
 # the License.
 #
 # 
##############################################################################
-get_directory_property(TOOLCHAIN_DIR_FLAGS DIRECTORY ${NUTTX_DIR}
-                                                     COMPILE_OPTIONS)
-
-set(NUTTX_EXTRA_FLAGS "")
-foreach(FLAG ${TOOLCHAIN_DIR_FLAGS})
-  if(FLAG IN_LIST COMPILE_ONLY_FLAGS)
-    continue()
-  endif()
-  if(NOT FLAG MATCHES "^\\$<.*>$")
-    list(APPEND NUTTX_EXTRA_FLAGS ${FLAG})
-  else()
-    string(REGEX MATCH "\\$<\\$<COMPILE_LANGUAGE:C>:(.*)>" matched ${FLAG})
-    if(matched)
-      list(APPEND NUTTX_EXTRA_FLAGS ${CMAKE_MATCH_1})
-    endif()
-  endif()
-endforeach()
-
-if(NUTTX_EXTRA_FLAGS)
-  list(REMOVE_DUPLICATES NUTTX_EXTRA_FLAGS)
-endif()
-
-set_property(GLOBAL PROPERTY NUTTX_EXTRA_FLAGS ${NUTTX_EXTRA_FLAGS})
+nuttx_collect_extra_flags()
 
 separate_arguments(CMAKE_C_FLAG_ARGS NATIVE_COMMAND ${CMAKE_C_FLAGS})
 
diff --git a/cmake/nuttx_collect_extra_flags.cmake 
b/cmake/nuttx_collect_extra_flags.cmake
new file mode 100644
index 00000000000..f73818ddd07
--- /dev/null
+++ b/cmake/nuttx_collect_extra_flags.cmake
@@ -0,0 +1,84 @@
+# 
##############################################################################
+# cmake/nuttx_collect_extra_flags.cmake
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more 
contributor
+# license agreements.  See the NOTICE file distributed with this work for
+# additional information regarding copyright ownership.  The ASF licenses this
+# file to you under the Apache License, Version 2.0 (the "License"); you may 
not
+# use this file except in compliance with the License.  You may obtain a copy 
of
+# the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations under
+# the License.
+#
+# 
##############################################################################
+
+# ~~~
+# nuttx_collect_extra_flags
+#
+# Description:
+#   Collect the directory-level COMPILE_OPTIONS of the NuttX source tree (e.g.
+#   -m32, -march, -mcpu) into NUTTX_EXTRA_FLAGS.  The final link and the steps
+#   that invoke the compiler directly (toolchain library lookup,
+#   add_custom_command) use these flags so that they match the compiled
+#   objects.
+#
+#   Flags listed in COMPILE_ONLY_FLAGS are dropped, generator expressions of
+#   the form $<$<COMPILE_LANGUAGE:C>:...> are unwrapped to their C payload and
+#   duplicates are removed.  The result is stored both in the GLOBAL property
+#   NUTTX_EXTRA_FLAGS and in the NUTTX_EXTRA_FLAGS variable of the caller.
+#
+# Usage:
+#   nuttx_collect_extra_flags([EXCLUDE <regex> ...])
+#
+# Parameters:
+#   EXCLUDE : optional regular expressions; matching plain flags are skipped
+#             (e.g. "-nostdlib").
+# ~~~
+
+function(nuttx_collect_extra_flags)
+  cmake_parse_arguments(ARG "" "" "EXCLUDE" ${ARGN})
+
+  get_directory_property(TOOLCHAIN_DIR_FLAGS DIRECTORY ${NUTTX_DIR}
+                                                       COMPILE_OPTIONS)
+
+  set(extra_flags "")
+  foreach(FLAG ${TOOLCHAIN_DIR_FLAGS})
+    if(FLAG IN_LIST COMPILE_ONLY_FLAGS)
+      continue()
+    endif()
+    if(NOT FLAG MATCHES "^\\$<.*>$")
+      set(skip FALSE)
+      foreach(regex ${ARG_EXCLUDE})
+        if(FLAG MATCHES "${regex}")
+          set(skip TRUE)
+          break()
+        endif()
+      endforeach()
+      if(NOT skip)
+        list(APPEND extra_flags ${FLAG})
+      endif()
+    else()
+      string(REGEX MATCH "\\$<\\$<COMPILE_LANGUAGE:C>:(.*)>" matched ${FLAG})
+      if(matched)
+        list(APPEND extra_flags ${CMAKE_MATCH_1})
+      endif()
+    endif()
+  endforeach()
+
+  if(extra_flags)
+    list(REMOVE_DUPLICATES extra_flags)
+  endif()
+
+  set_property(GLOBAL PROPERTY NUTTX_EXTRA_FLAGS ${extra_flags})
+  set(NUTTX_EXTRA_FLAGS
+      ${extra_flags}
+      PARENT_SCOPE)
+endfunction()
diff --git a/cmake/nuttx_toolchain.cmake b/cmake/nuttx_toolchain.cmake
index 2541ad4ef40..c375c98c1b0 100644
--- a/cmake/nuttx_toolchain.cmake
+++ b/cmake/nuttx_toolchain.cmake
@@ -23,6 +23,8 @@
 # search. If the manual of the newly supported toolchain is different, you can
 # override these methods in the toolchain
 
+include(nuttx_collect_extra_flags)
+
 if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
   if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.9)
     # force color for gcc > 4.9

Reply via email to