From 9649f38d36facbc2ddcbd1f5268bd1c33637e562 Mon Sep 17 00:00:00 2001
From: 3405691582 <dsk@google.com>
Date: Sat, 29 Mar 2025 22:16:46 -0400
Subject: [PATCH] Add -z,origin linker flag on OpenBSD.

This is required for $ORIGIN rpath processing here (without having to
fiddle with workarounds like LD_LIBRARY_PATH).
---
 CMakeLists.txt                              | 6 ++++++
 Sources/Foundation/CMakeLists.txt           | 4 ----
 Sources/FoundationNetworking/CMakeLists.txt | 4 ----
 Sources/FoundationXML/CMakeLists.txt        | 4 ----
 Sources/plutil/CMakeLists.txt               | 3 +--
 5 files changed, 7 insertions(+), 14 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index da372ef7d5..08f699050f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -212,6 +212,12 @@ else()
         "/clang:-fcf-runtime-abi=swift")
 endif()
 
+set(CMAKE_INSTALL_REMOVE_ENVIRONMENT_RPATH ON)
+set(CMAKE_INSTALL_RPATH "$ORIGIN")
+if(CMAKE_SYSTEM_NAME MATCHES "OpenBSD|DragonFlyBSD")
+    add_link_options("LINKER:-z,origin")
+endif()
+
 if(CMAKE_BUILD_TYPE STREQUAL Debug)
     list(APPEND _Foundation_common_build_flags
         "-DDEBUG")
diff --git a/Sources/Foundation/CMakeLists.txt b/Sources/Foundation/CMakeLists.txt
index 108dfb1c19..f6299e5c90 100644
--- a/Sources/Foundation/CMakeLists.txt
+++ b/Sources/Foundation/CMakeLists.txt
@@ -164,10 +164,6 @@ if(NOT BUILD_SHARED_LIBS)
         "SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -public-autolink-library -Xfrontend swiftSynchronization>")
 endif()
 
-set_target_properties(Foundation PROPERTIES
-    INSTALL_RPATH "$ORIGIN"
-    INSTALL_REMOVE_ENVIRONMENT_RPATH ON)
-
 if(dispatch_FOUND)
     set_target_properties(Foundation PROPERTIES
         BUILD_RPATH "$<TARGET_FILE_DIR:swiftDispatch>")
diff --git a/Sources/FoundationNetworking/CMakeLists.txt b/Sources/FoundationNetworking/CMakeLists.txt
index 6ad40968dc..ec0818baeb 100644
--- a/Sources/FoundationNetworking/CMakeLists.txt
+++ b/Sources/FoundationNetworking/CMakeLists.txt
@@ -73,10 +73,6 @@ if(NOT BUILD_SHARED_LIBS)
 
 endif()
 
-set_target_properties(FoundationNetworking PROPERTIES
-    INSTALL_RPATH "$ORIGIN"
-    INSTALL_REMOVE_ENVIRONMENT_RPATH ON)
-
 if(LINKER_SUPPORTS_BUILD_ID)
   target_link_options(FoundationNetworking PRIVATE "LINKER:--build-id=sha1")
 endif()
diff --git a/Sources/FoundationXML/CMakeLists.txt b/Sources/FoundationXML/CMakeLists.txt
index 5c8a0fb301..bb3f78bf4d 100644
--- a/Sources/FoundationXML/CMakeLists.txt
+++ b/Sources/FoundationXML/CMakeLists.txt
@@ -46,10 +46,6 @@ if(NOT BUILD_SHARED_LIBS)
 
 endif()
 
-set_target_properties(FoundationXML PROPERTIES
-    INSTALL_RPATH "$ORIGIN"
-    INSTALL_REMOVE_ENVIRONMENT_RPATH ON)
-
 if(LINKER_SUPPORTS_BUILD_ID)
   target_link_options(FoundationXML PRIVATE "LINKER:--build-id=sha1")
 endif()
diff --git a/Sources/plutil/CMakeLists.txt b/Sources/plutil/CMakeLists.txt
index 19c18f59f0..1d8d5f42f5 100644
--- a/Sources/plutil/CMakeLists.txt
+++ b/Sources/plutil/CMakeLists.txt
@@ -19,8 +19,7 @@ target_link_libraries(plutil PRIVATE
 	Foundation)
 
 set_target_properties(plutil PROPERTIES
-	INSTALL_RPATH "$ORIGIN/../lib/swift/${SWIFT_SYSTEM_NAME}"
-    INSTALL_REMOVE_ENVIRONMENT_RPATH ON)
+	INSTALL_RPATH "$ORIGIN/../lib/swift/${SWIFT_SYSTEM_NAME}")
 
 set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS plutil)
 install(TARGETS plutil