|
| 1 | +# Move bundled RubyInstaller DLLs to a subdirectory. |
| 2 | +# This avoids interferences with other apps when ruby.exe is in the PATH. |
| 3 | + |
| 4 | +if package.rubyver2 >= "3.4" |
| 5 | + libruby_regex = /(msvcrt|ucrt)-ruby\d+\.dll$/i |
| 6 | + bin_dir = File.join(sandboxdir, "bin") |
| 7 | + dlls_dir = File.join(sandboxdir, "bin/ruby_builtin_dlls") |
| 8 | + directory bin_dir |
| 9 | + directory dlls_dir |
| 10 | + |
| 11 | + # Select the DLLs from "bin/" which shall be moved into "bin/ruby_builtin_dlls/" |
| 12 | + dlls = self.sandboxfiles.select do |destpath| |
| 13 | + destpath.start_with?(bin_dir+"/") && destpath =~ /\.dll$/i && destpath !~ libruby_regex |
| 14 | + end |
| 15 | + |
| 16 | + ext_dll_defs = { |
| 17 | + "lib/ruby/#{package.rubylibver}/#{package.ruby_arch}/fiddle.so" => /^libffi-\d.dll$/, |
| 18 | + "lib/ruby/#{package.rubylibver}/#{package.ruby_arch}/openssl.so" => /^libssl-[\d_]+(-x64)?.dll$|^libcrypto-[\d_]+(-x64)?.dll$/, |
| 19 | + "lib/ruby/#{package.rubylibver}/#{package.ruby_arch}/psych.so" => /^libyaml-[-\d]+.dll$/, |
| 20 | + "lib/ruby/#{package.rubylibver}/#{package.ruby_arch}/zlib.so" => /^zlib\d.dll$/, |
| 21 | + } |
| 22 | + |
| 23 | + core_dll_defs = [ |
| 24 | + /^libgmp-\d+.dll$/, |
| 25 | + /^libwinpthread-\d+.dll$/, |
| 26 | + /^libgcc_s_.*.dll$/, |
| 27 | + ] |
| 28 | + |
| 29 | + # create rake tasks to trigger additional processing of so files |
| 30 | + ext_dll_defs.keys.each do |so_file| |
| 31 | + self.sandboxfiles << File.join(sandboxdir, so_file) |
| 32 | + end |
| 33 | + |
| 34 | + core_dlls, dlls = dlls.partition do |destpath| |
| 35 | + core_dll_defs.any? { |re| re =~ File.basename(destpath) } |
| 36 | + end |
| 37 | + ext_dlls, dlls = dlls.partition do |destpath| |
| 38 | + ext_dll_defs.values.any? { |re| re =~ File.basename(destpath) } |
| 39 | + end |
| 40 | + raise "DLL belonging neither to core nor to exts: #{dlls}" unless dlls.empty? |
| 41 | + |
| 42 | + |
| 43 | + ########################################################################### |
| 44 | + # Add manifest to extension.so files pointing to linked MINGW library DLLs |
| 45 | + # next to it |
| 46 | + ########################################################################### |
| 47 | + |
| 48 | + # Add tasks to move the DLLs into the extension directory |
| 49 | + ext_dlls.each do |destpath| |
| 50 | + so_fname, _ = ext_dll_defs.find { |_, re| re =~ File.basename(destpath) } |
| 51 | + |
| 52 | + new_destpath = File.join(sandboxdir, File.dirname(so_fname), File.basename(destpath)) |
| 53 | + file new_destpath => [destpath.sub(sandboxdir, unpackdirmgw), File.dirname(new_destpath)] do |t| |
| 54 | + cp(t.prerequisites.first, t.name) |
| 55 | + end |
| 56 | + |
| 57 | + # Move the DLLs in the dependent files list to the subdirectory |
| 58 | + self.sandboxfiles.delete(destpath) |
| 59 | + self.sandboxfiles << new_destpath |
| 60 | + end |
| 61 | + |
| 62 | + # Add a custom manifest to each extension.so, so that they find the DLLs to be moved |
| 63 | + ext_dlls.each do |destpath| |
| 64 | + so_fname, _ = ext_dll_defs.find { |_, re| re =~ File.basename(destpath) } |
| 65 | + sandbox_so_fname = File.join(sandboxdir, so_fname) |
| 66 | + |
| 67 | + file sandbox_so_fname => [sandbox_so_fname.sub(sandboxdir, unpackdirmgw), File.dirname(sandbox_so_fname)] do |t| |
| 68 | + puts "patching manifest of #{t.name}" |
| 69 | + |
| 70 | + # The XML elements we want to add to the default MINGW manifest: |
| 71 | + new = <<~EOT |
| 72 | + <dependency> |
| 73 | + <dependentAssembly> |
| 74 | + <assemblyIdentity version="1.0.0.0" type="win32" name="#{File.basename(so_fname)}-assembly" /> |
| 75 | + </dependentAssembly> |
| 76 | + </dependency> |
| 77 | + EOT |
| 78 | + |
| 79 | + ManifestUpdater.update_file(t.prerequisites.first, new, t.name) |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + # Add a detached manifest file within the ext.so directory that lists all linked DLLs |
| 84 | + ext_dll_defs.each do |so_fname, re| |
| 85 | + mani_path = File.join(sandboxdir, so_fname + "-assembly.manifest") |
| 86 | + e_dlls = ext_dlls.select { |dll| re =~ File.basename(dll) } |
| 87 | + |
| 88 | + file mani_path => [File.dirname(mani_path)] do |t| |
| 89 | + puts "generate #{t.name}" |
| 90 | + |
| 91 | + File.binwrite t.name, <<~EOT |
| 92 | + <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 93 | + <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> |
| 94 | + <assemblyIdentity type="win32" name="#{File.basename(so_fname)}-assembly" version="1.0.0.0"></assemblyIdentity> |
| 95 | +
|
| 96 | + #{ e_dlls.map{|dll| %Q{<file name="#{File.basename(dll)}"/>} }.join } |
| 97 | + </assembly> |
| 98 | + EOT |
| 99 | + end |
| 100 | + self.sandboxfiles << mani_path |
| 101 | + end |
| 102 | + |
| 103 | + |
| 104 | + ################################################################################# |
| 105 | + # Add manifest to ruby.exe, rubyw.exe files pointing to DLLs in ruby_builtin_dlls |
| 106 | + ################################################################################# |
| 107 | + |
| 108 | + core_dlls.each do |destpath| |
| 109 | + |
| 110 | + # Add tasks to write the DLLs into the sub directory |
| 111 | + new_destpath = File.join(File.dirname(destpath), "ruby_builtin_dlls", File.basename(destpath)) |
| 112 | + file new_destpath => [destpath.sub(sandboxdir, unpackdirmgw), dlls_dir] do |t| |
| 113 | + cp(t.prerequisites.first, t.name) |
| 114 | + end |
| 115 | + |
| 116 | + # Move the DLLs in the dependent files list to the subdirectory |
| 117 | + self.sandboxfiles.delete(destpath) |
| 118 | + self.sandboxfiles << new_destpath |
| 119 | + end |
| 120 | + |
| 121 | + # Add a custom manifest to ruby.exe, rubyw.exe and libruby, so that they find the DLLs to be moved |
| 122 | + self.sandboxfiles.select do |destpath| |
| 123 | + destpath =~ libruby_regex |
| 124 | + end.each do |destpath| |
| 125 | + |
| 126 | + file destpath => [destpath.sub(sandboxdir, unpackdirmgw), bin_dir] do |t| |
| 127 | + puts "patching manifest of #{t.name}" |
| 128 | + |
| 129 | + # The XML elements we want to add to the default MINGW manifest: |
| 130 | + new = <<~EOT |
| 131 | + <dependency> |
| 132 | + <dependentAssembly> |
| 133 | + <assemblyIdentity version="1.0.0.0" type="win32" name="ruby_builtin_dlls" /> |
| 134 | + </dependentAssembly> |
| 135 | + </dependency> |
| 136 | + EOT |
| 137 | + |
| 138 | + ManifestUpdater.update_file(t.prerequisites.first, new, t.name) |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + # Add a detached manifest file within the sub directory that lists all DLLs in question |
| 143 | + manifest2 = File.join(sandboxdir, "bin/ruby_builtin_dlls/ruby_builtin_dlls.manifest") |
| 144 | + file manifest2 => [dlls_dir] do |t| |
| 145 | + puts "generate #{t.name}" |
| 146 | + File.binwrite t.name, <<~EOT |
| 147 | + <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 148 | + <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> |
| 149 | + <assemblyIdentity type="win32" name="ruby_builtin_dlls" version="1.0.0.0"></assemblyIdentity> |
| 150 | +
|
| 151 | + #{ core_dlls.map{|dll| %Q{<file name="#{File.basename(dll)}"/>} }.join } |
| 152 | + </assembly> |
| 153 | + EOT |
| 154 | + end |
| 155 | + self.sandboxfiles << manifest2 |
| 156 | +end |
0 commit comments