Skip to content

Commit

Permalink
Add scripts to generate ruby packages.
Browse files Browse the repository at this point in the history
This is required to automatically create ruby packages for flutter
testing.

Bug: flutter/flutter#122057
Bug: flutter/flutter#125812
Bug: flutter/flutter#133096
  • Loading branch information
godofredoc committed Oct 6, 2023
1 parent 560a439 commit 70811b3
Show file tree
Hide file tree
Showing 5 changed files with 411 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

Google Inc.
Chromium authors.
Stephan Nordnes Eriksen <stephan-nordnes-eriksen>.
24 changes: 24 additions & 0 deletions cipd_packages/ruby/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Ruby at Flutter

The scripts in this folder are used to build and package
ruby as a CIPD package. This package is intended for
flutter/flutter repository testing.

The scripts inside ruby_ship are based on the scripts
from https://github.com/stephan-nordnes-eriksen/ruby_ship
with changes to adapt them to the flutter use cases.


# Updating to new ruby versions.

Update `ruby_version.txt` with the major version
and the file name. E.g. `3.1,ruby-3.1.4.tar.gz` to
change the version of ruby to build.

# Installing cocoapods.

The CIPD package will bundle cocoapods it corresponding
wrapper scripts.

Update `cococapods_version.txt` with the version of cocoapods
to use.
52 changes: 52 additions & 0 deletions cipd_packages/ruby/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash
# Copyright 2023 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Fetches corresponding dart sdk from CIPD for different platforms, builds
# an executable binary of codesign to `build` folder.
#
# This build script will be triggered on Mac code signing machines.

set -e
set -x

# Get versions from config files.
RUBY_MAJOR_VERSION=$(cat ruby_version.txt | cut -d ',' -f 1)
RUBY_FILE_NAME=$(cat ruby_version.txt | cut -d ',' -f 2)
COCOAPODS_VERSION=$(cat cocoapods_version.txt)

# Cleanup ruby_ship/bin directory if it exists.
rm -rf ruby_ship/bin
pushd ruby_ship
# Download a ruby version
curl https://cache.ruby-lang.org/pub/ruby/$RUBY_MAJOR_VERSION/$RUBY_FILE_NAME -o ../$RUBY_FILE_NAME
# Install brew dependencies
brew install --build-from-source gdbm
brew install --build-from-source gmp
brew install --build-from-source libffi
brew install libyaml
brew install --build-from-source readline
brew install --build-from-source openssl@3
brew install --build-from-source m4
# Change directory to ruby_ship checkout
./tools/ruby_ship_build.sh ../$RUBY_FILE_NAME
bin/ruby tools/auto_relink_dylibs.rb
# Remove signatures
ls bin/shipyard/darwin_ruby/dylibs/* | xargs codesign --remove-signature
# Resign with adhoc
ls bin/shipyard/darwin_ruby/dylibs/* | xargs codesign --force -s -
codesign --remove-signature bin/shipyard/darwin_ruby/bin/ruby
codesign --force -s - bin/shipyard/darwin_ruby/bin/ruby
sed -i'' -e 's/bindir="\${0%\/\*}"/&\nLIBSPATH="\$( cd -- "\$(dirname "\$0")" >\/dev\/null 2>\&1 ; pwd -P )\/..\/dylibs"\nexport DYLD_FALLBACK_LIBRARY_PATH=\$LIBSPATH:\$DYLD_FALLBACK_LIBRARY_PATH/' bin/shipyard/darwin_ruby/bin/gem
sed -i'' -e 's/bindir="\${0%\/\*}"/&\nLIBSPATH="\$( cd -- "\$(dirname "\$0")" >\/dev\/null 2>\&1 ; pwd -P )\/..\/dylibs"\nexport DYLD_FALLBACK_LIBRARY_PATH=\$LIBSPATH:\$DYLD_FALLBACK_LIBRARY_PATH/' bin/shipyard/darwin_ruby/bin/bundler
sed -i'' -e 's/bindir="\${0%\/\*}"/&\nLIBSPATH="\$( cd -- "\$(dirname "\$0")" >\/dev\/null 2>\&1 ; pwd -P )\/..\/dylibs"\nexport DYLD_FALLBACK_LIBRARY_PATH=\$LIBSPATH:\$DYLD_FALLBACK_LIBRARY_PATH/' bin/shipyard/darwin_ruby/bin/bundle
bin/shipyard/darwin_ruby/bin/bundle --version
bin/shipyard/darwin_ruby/bin/bundler --version
bin/shipyard/darwin_ruby/bin/gem --version

# Post install cocoapod
bin/shipyard/darwin_gem.sh install cocoapods -v $COCOAPODS_VERSION
sed -i'' -e 's/bindir="\${0%\/\*}"/&\nLIBSPATH="\$( cd -- "\$(dirname "\$0")" >\/dev\/null 2>\&1 ; pwd -P )\/..\/dylibs"\nexport DYLD_FALLBACK_LIBRARY_PATH=\$LIBSPATH:\$DYLD_FALLBACK_LIBRARY_PATH/' bin/shipyard/darwin_ruby/bin/pod
bin/shipyard/darwin_ruby/bin/pod --version
popd
70 changes: 70 additions & 0 deletions cipd_packages/ruby/ruby_ship/tools/auto_relink_dylibs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2023 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

require "fileutils"

@new_dylib_path = File.join("bin", "shipyard", "darwin_ruby", "dylibs")

@current_dir = FileUtils.pwd

FileUtils.mkdir_p(@new_dylib_path)

def fix_dylib_for_file(file)
results = `otool -L "#{file}"` #Will get information about which dylibs to link

if results.is_a?(String) && results != "" && !results.include?("is not an object file") && !results.include?("Assertion failed:")
# puts "---------------------------------"
puts "--RELINKING--: #{file}"
# puts "---------------------------------"
expanded = File.expand_path(file)

#Setting new ID for this if needed
id_path = File.join("bin", "shipyard", "darwin_ruby", expanded.split(File.join(@current_dir, "bin", "shipyard","darwin_ruby"))[-1])
id_reset_results = `install_name_tool -id #{id_path} #{file} 2> /dev/null` #Will link the current file to itself

lines = results.split("\n")
lines = [] unless lines
itterate = lines[1..-1]
itterate = [] unless itterate


itterate.each do |libfile_line|
libfile = libfile_line.split(" (compatibility version")[0].strip
libfile = libfile.split("(")[0]

next if libfile.include?(@new_dylib_path) #We have already fixed this one
next if libfile.include?("/usr/lib/") #These are global and assumed to be present on all versions of osx
next if libfile.include?("/System/Library/")#Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation

new_libfile = File.join(@new_dylib_path, libfile.split(File.join("",""))[-1])

unless File.file?(new_libfile)
FileUtils.copy(libfile, new_libfile)
puts "--COPIED-- #{new_libfile}"
fix_dylib_for_file(new_libfile)
end

relink_command_results = `install_name_tool -change #{libfile} #{new_libfile} #{file} 2> /dev/null` #Will relink external library
puts "Linked: #{new_libfile} to #{file}"

if relink_command_results != "" && !relink_command_results.include?("error:")
puts "relinked in #{file} link: #{libfile} to #{new_libfile}"
end
end
end
end

puts File.dirname(File.expand_path($0))

folders = [File.expand_path(File.join(File.dirname(File.expand_path($0)), "..", "bin", "shipyard", "darwin_ruby"))]

full = folders.map{|f| Dir[File.join(f, '**', '*')]}
actual_files = full.flatten(1).uniq.select{|e| File.file? e}


actual_files.each do |file|
fix_dylib_for_file(file)
end

puts "Relinking of dylib done"
Loading

0 comments on commit 70811b3

Please sign in to comment.