From 6a023ee77ca48497382050998d9b33c56330f735 Mon Sep 17 00:00:00 2001
From: Randy Eckenrode <randy@largeandhighquality.com>
Date: Wed, 13 Nov 2024 13:53:14 -0500
Subject: [PATCH 09/18] Support LTO in nixpkgs

---
 src/ld/InputFiles.cpp       | 11 ++---------
 src/ld/meson.build          | 13 ++++++++++++-
 src/ld/parsers/lto_file.cpp |  2 +-
 src/ld/parsers/meson.build  | 13 ++++++++++++-
 4 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/src/ld/InputFiles.cpp b/src/ld/InputFiles.cpp
index e045e90..d5341d9 100644
--- a/src/ld/InputFiles.cpp
+++ b/src/ld/InputFiles.cpp
@@ -464,15 +464,8 @@ ld::File* InputFiles::makeFile(const Options::FileInfo& info, bool indirectDylib
 			if ( _options.overridePathlibLTO() != NULL ) {
 				libLTO = _options.overridePathlibLTO();
 			}
-			else if ( _NSGetExecutablePath(ldPath, &bufSize) != -1 ) {
-				if ( realpath(ldPath, tmpPath) != NULL ) {
-					char* lastSlash = strrchr(tmpPath, '/');
-					if ( lastSlash != NULL )
-						strcpy(lastSlash, "/../lib/libLTO.dylib");
-					libLTO = tmpPath;
-					if ( realpath(tmpPath, libLTOPath) != NULL ) 
-						libLTO = libLTOPath;
-				}
+			else {
+				libLTO = "@libllvm_path@/lib/libLTO.dylib";
 			}
 			throwf("could not process llvm bitcode object file, because %s could not be loaded", libLTO);
 		}
diff --git a/src/ld/meson.build b/src/ld/meson.build
index 8bc85b7..ad8359a 100644
--- a/src/ld/meson.build
+++ b/src/ld/meson.build
@@ -1,5 +1,16 @@
 subdir('parsers')
 
+InputFiles_cpp = configure_file(
+    configuration : {
+        'libllvm_path' : get_option('libllvm_path'),
+        # Silence warnings
+        'APPLE_LICENSE_HEADER_START' : '@APPLE_LICENSE_HEADER_START@',
+        'APPLE_LICENSE_HEADER_END' : '@APPLE_LICENSE_HEADER_END@',
+    },
+    input : 'InputFiles.cpp',
+    output : '@PLAINNAME@',
+)
+
 executable(
     'ld',
     cpp_args : [
@@ -31,8 +42,8 @@ executable(
     ],
     sources : [
         configure_h,
+        InputFiles_cpp,
         'FatFile.cpp',
-        'InputFiles.cpp',
         'Mangling.cpp',
         'Options.cpp',
         'OutputFile.cpp',
diff --git a/src/ld/parsers/lto_file.cpp b/src/ld/parsers/lto_file.cpp
index e32e2f8..1a3b846 100644
--- a/src/ld/parsers/lto_file.cpp
+++ b/src/ld/parsers/lto_file.cpp
@@ -1839,7 +1839,7 @@ bool optimize(  const std::vector<const ld::Atom*>&	allAtoms,
 
 }; // namespace lto
 
-static const char *sLTODylib = "@rpath/libLTO.dylib";
+static const char *sLTODylib = "@libllvm_path@/lib/libLTO.dylib";
 static std::atomic<bool> sLTOIsLoaded(false);
 
 static void *getHandle() {
diff --git a/src/ld/parsers/meson.build b/src/ld/parsers/meson.build
index a88f651..7bd3e89 100644
--- a/src/ld/parsers/meson.build
+++ b/src/ld/parsers/meson.build
@@ -1,3 +1,14 @@
+lto_file_cpp = configure_file(
+    configuration : {
+        'libllvm_path' : get_option('libllvm_path'),
+        # Silence warnings
+        'APPLE_LICENSE_HEADER_START' : '@APPLE_LICENSE_HEADER_START@',
+        'APPLE_LICENSE_HEADER_END' : '@APPLE_LICENSE_HEADER_END@',
+    },
+    input : 'lto_file.cpp',
+    output : '@PLAINNAME@',
+)
+
 parsers = static_library(
     'parsers',
     cpp_args : [ '-Wno-vla-cxx-extension' ],
@@ -10,9 +21,9 @@ parsers = static_library(
     ],
     sources : [
         configure_h,
+        lto_file_cpp,
         'archive_file.cpp',
         'generic_dylib_file.cpp',
-        'lto_file.cpp',
         'macho_dylib_file.cpp',
         'macho_relocatable_file.cpp',
         'opaque_section_file.cpp',
-- 
2.47.2

