Author: Danny Milosavljevic <dannym@friendly-machines.com>
Date: 2025-10-20
Subject: Fix module.modulemap to use build directory instead of src directory

Swift corelibs-libdispatch 5.7.3 tries to copy module.modulemap files into the
source directory, which fails when the source is in the read-only /gnu/store.
This patch changes the output paths to use PROJECT_BINARY_DIR instead.

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -262,25 +262,29 @@

 if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
   add_custom_command(OUTPUT
-                       "${PROJECT_SOURCE_DIR}/dispatch/module.modulemap"
-                       "${PROJECT_SOURCE_DIR}/private/module.modulemap"
+                       "${PROJECT_BINARY_DIR}/dispatch/module.modulemap"
+                       "${PROJECT_BINARY_DIR}/private/module.modulemap"
                      COMMAND
-                       ${CMAKE_COMMAND} -E copy_if_different "${PROJECT_SOURCE_DIR}/dispatch/darwin/module.modulemap" "${PROJECT_SOURCE_DIR}/dispatch/module.modulemap"
+                       ${CMAKE_COMMAND} -E make_directory "${PROJECT_BINARY_DIR}/dispatch" "${PROJECT_BINARY_DIR}/private"
                      COMMAND
-                       ${CMAKE_COMMAND} -E copy_if_different "${PROJECT_SOURCE_DIR}/private/darwin/module.modulemap" "${PROJECT_SOURCE_DIR}/private/module.modulemap")
+                       ${CMAKE_COMMAND} -E copy_if_different "${PROJECT_SOURCE_DIR}/dispatch/darwin/module.modulemap" "${PROJECT_BINARY_DIR}/dispatch/module.modulemap"
+                     COMMAND
+                       ${CMAKE_COMMAND} -E copy_if_different "${PROJECT_SOURCE_DIR}/private/darwin/module.modulemap" "${PROJECT_BINARY_DIR}/private/module.modulemap")
 else()
   add_custom_command(OUTPUT
-                       "${PROJECT_SOURCE_DIR}/dispatch/module.modulemap"
-                       "${PROJECT_SOURCE_DIR}/private/module.modulemap"
+                       "${PROJECT_BINARY_DIR}/dispatch/module.modulemap"
+                       "${PROJECT_BINARY_DIR}/private/module.modulemap"
+                     COMMAND
+                       ${CMAKE_COMMAND} -E make_directory "${PROJECT_BINARY_DIR}/dispatch" "${PROJECT_BINARY_DIR}/private"
                      COMMAND
-                       ${CMAKE_COMMAND} -E copy_if_different "${PROJECT_SOURCE_DIR}/dispatch/generic/module.modulemap" "${PROJECT_SOURCE_DIR}/dispatch/module.modulemap"
+                       ${CMAKE_COMMAND} -E copy_if_different "${PROJECT_SOURCE_DIR}/dispatch/generic/module.modulemap" "${PROJECT_BINARY_DIR}/dispatch/module.modulemap"
                      COMMAND
-                       ${CMAKE_COMMAND} -E copy_if_different "${PROJECT_SOURCE_DIR}/private/generic/module.modulemap" "${PROJECT_SOURCE_DIR}/private/module.modulemap")
+                       ${CMAKE_COMMAND} -E copy_if_different "${PROJECT_SOURCE_DIR}/private/generic/module.modulemap" "${PROJECT_BINARY_DIR}/private/module.modulemap")
 endif()
 add_custom_target(module-maps ALL
                   DEPENDS
-                     "${PROJECT_SOURCE_DIR}/dispatch/module.modulemap"
-                     "${PROJECT_SOURCE_DIR}/private/module.modulemap")
+                     "${PROJECT_BINARY_DIR}/dispatch/module.modulemap"
+                     "${PROJECT_BINARY_DIR}/private/module.modulemap")

 configure_file("${PROJECT_SOURCE_DIR}/cmake/config.h.in"
                "${PROJECT_BINARY_DIR}/config/config_ac.h")
