Skip to content

Commit

Permalink
fix: stripping debug information from tebako package
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx committed Jul 21, 2024
1 parent 9bc0adf commit fe7fb8c
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 64 deletions.
19 changes: 7 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,6 @@ else (${SETUP_MODE})
)

add_custom_target(packaged_filesystem
COMMAND ruby ${EXE}/tebako-packager strip ${OSTYPE_TXT} ${DATA_SRC_DIR}
COMMAND ${GNU_BASH} -c "chmod +x ${DEPS_BIN_DIR}/mkdwarfs${EXE_SUFFIX}"
# No progress below is critical since in reporting mode mkdwarfs tries to work directly with terminal
# and it creates issues for stderr redirects (&2 > 1) used by Ninja and our test srcipts
Expand All @@ -487,22 +486,18 @@ else (${SETUP_MODE})
add_dependencies(tebako-fs packaged_filesystem)

if (${RUBY_VER} VERSION_LESS "3.0.0")
add_custom_target(patched_ruby
add_custom_target(tebako
COMMAND ${CMAKE_COMMAND} -E chdir ${RUBY_SOURCE_DIR} make ${RUBY_MAKEFILE} -j${NCORES}
)
COMMAND ruby ${EXE}/tebako-packager finalize ${OSTYPE_TXT} ${RUBY_SOURCE_DIR} ${APP_NAME}
)
else()
add_custom_target(patched_ruby
add_custom_target(tebako
COMMAND ${CMAKE_COMMAND} -E chdir ${RUBY_SOURCE_DIR} make ruby ${RUBY_MAKEFILE} -j${NCORES}
COMMAND ${CMAKE_COMMAND} -E chdir ${RUBY_SOURCE_DIR} make ${RUBY_MAKEFILE} -j${NCORES}
)
COMMAND ruby ${EXE}/tebako-packager finalize ${OSTYPE_TXT} ${RUBY_SOURCE_DIR} ${APP_NAME}
)
endif()

add_dependencies(patched_ruby setup tebako-fs)

add_custom_target(tebako ALL
COMMAND ${CMAKE_COMMAND} -E copy ${RUBY_SOURCE_DIR}/ruby${RUBY_SUFFIX}${EXE_SUFFIX} ${APP_NAME}${EXE_SUFFIX}
COMMAND ${CMAKE_COMMAND} -E echo "Tebako packaging has completed"
DEPENDS patched_ruby
)
add_dependencies(tebako setup tebako-fs)

endif(${SETUP_MODE})
30 changes: 10 additions & 20 deletions exe/tebako-packager
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,7 @@ begin
end

Tebako::Packager.pass1(ARGV[1], ARGV[2], ARGV[3], ARGV[4], ARGV[5])
when "stash"
# ARGV[0] -- command
# ARGV[1] -- DATA_SRC_DIR
# ARGV[2] -- RUBY_STASH_DIR
unless ARGV.length == 3
raise Tebako::Error,
"tebako-packager stash command expects 3 arguments, #{ARGV.length} has been provided."
end

Tebako::Packager.stash(ARGV[1], ARGV[2])
when "strip"
# ARGV[0] -- command
# ARGV[1] -- OSTYPE
# ARGV[2] -- DATA_SRC_DIR
unless ARGV.length == 3
raise Tebako::Error,
"tebako-packager strip command expects 3 arguments, #{ARGV.length} has been provided."
end

Tebako::Stripper.strip(ARGV[1], ARGV[2])
when "pass1a"
# ARGV[0] -- command
# ARGV[1] -- RUBY_SOURCE_DIR
Expand Down Expand Up @@ -114,12 +95,21 @@ begin
# That shall match CMakeLists.txt settings
Tebako::Packager.deploy(ARGV[1], ARGV[4], ARGV[5], ARGV[11], ARGV[7], ARGV[8], ARGV[9])

when "finalize"
# ARGV[0] -- command
# ARGV[1] -- OSTYPE
# ARGV[2] -- RUBY_SOURCE_DIR
# ARGV[3] -- APP_NAME
unless ARGV.length == 4
raise Tebako::Error,
"tebako-packager finalize command expects 4 arguments, #{ARGV.length} has been provided."
end
Tebako::Packager.finalize(ARGV[1], ARGV[2], ARGV[3])
else
raise Tebako::Error, "tebako-packager cannot process #{ARGV[0]} command"
end
rescue Tebako::Error => e
puts "tebako-packager failed: #{e.message} [#{e.error_code}]"
exit(e.error_code)
end

exit(0)
23 changes: 18 additions & 5 deletions lib/tebako/deploy_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
require "find"

require_relative "error"
require_relative "packager/patch_helpers"

# Tebako - an executable packager
module Tebako
Expand Down Expand Up @@ -65,11 +66,16 @@ def config(os_type, ruby_ver)
end

def deploy
system("#{gem_command} env")
install_gem("tebako-runtime")
install_gem("bundler", Tebako::BUNDLER_VERSION) if needs_bundler?

deploy_solution
Packager::PatchHelpers.with_env(deploy_env) do
unless Packager::PatchHelpers.ruby31?(@ruby_ver)
update_rubygems
patch_after_rubygems_update(@target_dir, @ruby_api_version)
end
system("#{gem_command} env")
install_gem("tebako-runtime")
install_gem("bundler", BUNDLER_VERSION) if needs_bundler?
deploy_solution
end
end

def deploy_env
Expand Down Expand Up @@ -264,6 +270,13 @@ def ncores
end
end

def patch_after_rubygems_update(target_dir, ruby_api_ver)
# Autoload cannot handle statically linked openssl extension
# Changing it to require seems to be the simplest solution
Packager::PatchHelpers.patch_file("#{target_dir}/lib/ruby/site_ruby/#{ruby_api_ver}/rubygems/openssl.rb",
{ "autoload :OpenSSL, \"openssl\"" => "require \"openssl\"" })
end

def run_with_capture(args)
puts " ... @ #{args.join(" ")}"
out, st = Open3.capture2e(*args)
Expand Down
25 changes: 11 additions & 14 deletions lib/tebako/packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

require_relative "error"
require_relative "deploy_helper"
require_relative "stripper"
require_relative "packager/pass1"
require_relative "packager/pass1a"
require_relative "packager/pass2"
Expand Down Expand Up @@ -79,14 +80,17 @@ def deploy(os_type, target_dir, pre_dir, ruby_ver, fs_root, fs_entrance, fs_moun

deploy_helper = Tebako::DeployHelper.new(fs_root, fs_entrance, fs_mount_point, target_dir, pre_dir)
deploy_helper.config(os_type, ruby_ver)
deploy_helper.deploy
Tebako::Stripper.strip(os_type, target_dir)
end

PatchHelpers.with_env(deploy_helper.deploy_env) do
unless PatchHelpers.ruby31?(ruby_ver)
deploy_helper.update_rubygems
patch_after_rubygems_update(target_dir, deploy_helper.ruby_api_version)
end
deploy_helper.deploy
end
def finalize(os_type, src_dir, app_name)
puts "-- Finalizing packaging"
exe_suffix = Packager::PatchHelpers.exe_suffix(os_type)
package_name = "#{app_name}#{exe_suffix}"
Tebako::Stripper.strip_file(File.join(src_dir, "ruby#{exe_suffix}"), package_name)

puts "Created tebako package at \"#{package_name}\""
end

# Init
Expand Down Expand Up @@ -188,13 +192,6 @@ def ruby_version(tbd)
end
ruby_version
end

def patch_after_rubygems_update(target_dir, ruby_api_ver)
# Autoload cannot handle statically linked openssl extension
# Changing it to require seems to be the simplest solution
PatchHelpers.patch_file("#{target_dir}/lib/ruby/site_ruby/#{ruby_api_ver}/rubygems/openssl.rb",
{ "autoload :OpenSSL, \"openssl\"" => "require \"openssl\"" })
end
end
end
end
4 changes: 4 additions & 0 deletions lib/tebako/packager/patch_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def get_prefix_linux(package)
out
end

def exe_suffix(ostype)
msys?(ostype) ? ".exe" : ""
end

def msys?(ostype)
ostype =~ /msys|cygwin|mingw/
end
Expand Down
5 changes: 2 additions & 3 deletions lib/tebako/packager/patch_libraries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ class << self
DARWIN_BREW_LIBS = [
["zlib", "z"], ["gdbm", "gdbm"], ["readline", "readline"], ["libffi", "ffi"],
["ncurses", "ncurses"], ["fmt", "fmt"], ["lz4", "lz4"], ["xz", "lzma"],
["libyaml", "yaml"], ["boost", "boost_chrono"],
["double-conversion", "double-conversion"]
["libyaml", "yaml"], ["boost", "boost_chrono"], ["double-conversion", "double-conversion"]
].freeze

DARWIN_BREW_LIBS_PRE_31 = [["openssl@1.1", "ssl"], ["openssl@1.1", "crypto"]].freeze

DARWIN_BREW_LIBS_31 = [["libyaml", "yaml"], ["openssl@3", "ssl"], ["openssl@3", "crypto"]].freeze
DARWIN_BREW_LIBS_31 = [["openssl@3", "ssl"], ["openssl@3", "crypto"]].freeze

DARWIN_DEP_LIBS = ["glog", "gflags", "brotlienc", "brotlidec", "brotlicommon"].freeze
# rubocop:enable Style/WordArray
Expand Down
23 changes: 16 additions & 7 deletions lib/tebako/stripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
require "fileutils"
require "find"

require_relative "packager/patch_helpers"

# Tebako - an executable packager
module Tebako
# Tebako packaging support (stripper)
module Stripper
DELETE_EXTENSIONS = %w[o lo obj a lib].freeze
STRIP_EXTENSIONS = %w[dll so].freeze
STRIP_EXTENSIONS = %w[dll so bundle dylib].freeze
BIN_FILES = %w[
bundle bundler rbs erb gem irb racc racc2y rake rdoc ri y2racc rdbg typeprof
].freeze
Expand All @@ -47,15 +49,22 @@ class << self
# from memfs or not. For debugging purposes it is very handy to have it here
def strip(ostype, src_dir)
puts " ... stripping the output"
strip_bs(ostype, src_dir)
strip_bs(src_dir)
strip_fi(ostype, src_dir)
strip_li(ostype, src_dir)
strip_li(src_dir)
end

def strip_file(file_in, file_out = nil)
params = ["strip", "-S", file_in]
params << "-o" << file_out unless file_out.nil?
out, st = Open3.capture2e(*params)
raise Tebako::Error, "Failed to strip #{file_in}:\n #{out}" unless st.exitstatus.zero?
end

private

def get_files(ostype)
exe_suffix = ostype =~ /msys/ ? ".exe" : ""
exe_suffix = Packager::PatchHelpers.exe_suffix(ostype)
files = BIN_FILES.flat_map do |f|
[f, "#{f}#{CMD_SUFFIX}", "#{f}#{BAT_SUFFIX}"]
end
Expand All @@ -64,7 +73,7 @@ def get_files(ostype)
files
end

def strip_bs(_ostype, src_dir)
def strip_bs(src_dir)
FileUtils.rm_rf([
File.join(src_dir, "share"),
File.join(src_dir, "include"),
Expand All @@ -77,15 +86,15 @@ def strip_fi(ostype, src_dir)
FileUtils.rm(files, force: true)
end

def strip_li(ostype, src_dir)
def strip_li(src_dir)
Find.find(src_dir) do |file|
next if File.directory?(file)

extension = File.extname(file).delete_prefix(".").downcase
if DELETE_EXTENSIONS.include?(extension)
FileUtils.rm(file)
elsif STRIP_EXTENSIONS.include?(extension)
system("strip \"#{file}\"") unless ostype =~ /darwin/
strip_file(file)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tebako/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
# POSSIBILITY OF SUCH DAMAGE.

module Tebako
VERSION = "0.7.3"
VERSION = "0.7.4"
end
4 changes: 2 additions & 2 deletions tests/scripts/functional-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ press_runner() {

# Check the first and the last messages expected from CMake script
assertContains "$result" "Running tebako press script"
assertContains "$result" "packaging has completed"
assertContains "$result" "Created tebako package at"
}

package_runner() {
Expand Down Expand Up @@ -178,7 +178,7 @@ test_AUC_extract() {

assertEquals 0 "${PIPESTATUS[0]}"
assertContains "$result" "Running tebako press script"
assertContains "$result" "packaging has completed"
assertContains "$result" "Created tebako package at"

./test-AUC-package --tebako-extract
assertEquals 0 "${PIPESTATUS[0]}"
Expand Down

0 comments on commit fe7fb8c

Please sign in to comment.