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

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

commit 5349a5ab0c6aad60decaca331058e6bd17aefee3
Author: Shoji Tokunaga <[email protected]>
AuthorDate: Mon May 18 15:21:09 2026 +0900

    arch/sim: replace macOS C++ constructor runtime hack with post-link patch
    
    - Invoke `codesign` and after lief rewrites the binary, so the modified
       Mach-O remains loadable on macOS.
    - Run the patch script via a dedicated `sim_patch_macho_initsection`
      custom target attached to `nuttx_post`, instead of a POST_BUILD
      command on the `nuttx` target.
    
    Signed-off-by: Shoji Tokunaga <[email protected]>
---
 arch/sim/src/patch_macho_initsection.py | 11 +++++++++++
 arch/sim/src/sim/CMakeLists.txt         |  7 ++++---
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/arch/sim/src/patch_macho_initsection.py 
b/arch/sim/src/patch_macho_initsection.py
index 2369965af0e..269f0715fc6 100755
--- a/arch/sim/src/patch_macho_initsection.py
+++ b/arch/sim/src/patch_macho_initsection.py
@@ -32,6 +32,7 @@ Requires: pip install lief
 """
 
 import argparse
+import subprocess
 import sys
 
 try:
@@ -65,6 +66,16 @@ def main():
     if patched:
         fat.write(args.binary)
 
+        if sys.platform == "darwin":
+            subprocess.run(
+                ["codesign", "--force", "--sign", "-", args.binary],
+                check=True,
+            )
+            subprocess.run(
+                ["codesign", "--verify", "--verbose", args.binary],
+                check=True,
+            )
+
     return 0
 
 
diff --git a/arch/sim/src/sim/CMakeLists.txt b/arch/sim/src/sim/CMakeLists.txt
index 273acb2fcc7..42c569f145e 100644
--- a/arch/sim/src/sim/CMakeLists.txt
+++ b/arch/sim/src/sim/CMakeLists.txt
@@ -153,14 +153,15 @@ list(
 if(CONFIG_HOST_MACOS AND CONFIG_HAVE_CXXINITIALIZE)
   # Keep classic __mod_init_func format so post-link lief patching works
   target_link_options(nuttx PRIVATE -Wl,-ld_classic,-no_fixup_chains)
-  add_custom_command(
-    TARGET nuttx
-    POST_BUILD
+  add_custom_target(
+    sim_patch_macho_initsection ALL
     COMMAND
       ${Python3_EXECUTABLE}
       ${CMAKE_CURRENT_SOURCE_DIR}/../patch_macho_initsection.py
       $<TARGET_FILE:nuttx>
+    DEPENDS nuttx
     COMMENT "Patching Mach-O init section type flags")
+  add_dependencies(nuttx_post sim_patch_macho_initsection)
 endif()
 
 if(CONFIG_SIM_CAMERA_V4L2)

Reply via email to