Skip to content

Commit

Permalink
chore: refactor pass2 patching
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx committed Feb 7, 2025
1 parent fe04f30 commit d27d0f2
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 163 deletions.
5 changes: 3 additions & 2 deletions lib/tebako/packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
require_relative "stripper"
require_relative "packager/pass1_patch"
require_relative "packager/pass1a_patch"
require_relative "packager/pass2"
require_relative "packager/pass2_patch"
require_relative "packager/patch_helpers"

# Tebako - an executable packager
Expand Down Expand Up @@ -150,7 +150,8 @@ def pass1a(ruby_source_dir)
def pass2(ostype, ruby_source_dir, deps_lib_dir, ruby_ver)
puts "-- Running pass2 script"

do_patch(Pass2.get_patch_map(ostype, deps_lib_dir, ruby_ver), ruby_source_dir)
patch = Pass2Patch.new(ostype, deps_lib_dir, ruby_ver).patch_map
do_patch(patch, ruby_source_dir)
end

# Stash
Expand Down
150 changes: 0 additions & 150 deletions lib/tebako/packager/pass2.rb

This file was deleted.

154 changes: 154 additions & 0 deletions lib/tebako/packager/pass2_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# frozen_string_literal: true

# Copyright (c) 2021-2024 [Ribose Inc](https://www.ribose.com).
# All rights reserved.
# This file is a part of tebako
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

require_relative "patch_literals"
require_relative "patch_main"
require_relative "patch_libraries"
require_relative "patch_helpers"
require_relative "patch_buildsystem"

# Tebako - an executable packager
module Tebako
module Packager
# Ruby patching definitions (pass2)
class Pass2Patch < Patch
def initialize(ostype, deps_lib_dir, ruby_ver)
super()
@ostype = ostype
@scmb = ScenarioManagerBase.new(@ostype)
@deps_lib_dir = deps_lib_dir
@ruby_ver = ruby_ver
end

def patch_map
patch_map = patch_map_base
patch_map.store("thread_pthread.c", LINUX_MUSL_THREAD_PTHREAD_PATCH) if @scmb.musl?
if @scmb.msys?
patch_map.merge!(msys_patches)
elsif @ruby_ver.ruby3x?
patch_map.store("common.mk", COMMON_MK_PATCH)

Check warning on line 53 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L48-L53

Added lines #L48 - L53 were not covered by tests
end
extend_patch_map_r33(patch_map)
patch_map.store("prism_compile.c", PRISM_PATCHES) if @ruby_ver.ruby34?
patch_map

Check warning on line 57 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L55-L57

Added lines #L55 - L57 were not covered by tests
end

private

include Tebako::Packager::PatchBuildsystem
include Tebako::Packager::PatchLiterals
def extend_patch_map_r33(patch_map)
if @ruby_ver.ruby33? || @scmb.msys?
patch_map.store("config.status",

Check warning on line 66 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L65-L66

Added lines #L65 - L66 were not covered by tests
get_config_status_patch(@ostype, @deps_lib_dir, @ruby_ver))
end
patch_map

Check warning on line 69 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L69

Added line #L69 was not covered by tests
end

def dir_c_patch
pattern = ScenarioManagerBase.new.msys? ? "/* define system APIs */" : "#ifdef HAVE_GETATTRLIST"
patch = PatchHelpers.patch_c_file_pre(pattern)
patch.merge!(DIR_C_BASE_PATCH)
patch

Check warning on line 76 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L73-L76

Added lines #L73 - L76 were not covered by tests
end

def dln_c_patch
pattern = "#ifndef dln_loaderror"

Check warning on line 80 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L80

Added line #L80 was not covered by tests
# Not using substitutions of dlxxx functions on Windows
patch = {
pattern => "#{@scmb.msys? ? C_FILE_SUBST_LESS : C_FILE_SUBST}\n#{pattern}\n"

Check warning on line 83 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L83

Added line #L83 was not covered by tests
}

if @scmb.msys?
patch.merge!(@ruby_ver.ruby32? ? DLN_C_MSYS_PATCH : DLN_C_MSYS_PATCH_PRE32)

Check warning on line 87 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L86-L87

Added lines #L86 - L87 were not covered by tests
end

patch

Check warning on line 90 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L90

Added line #L90 was not covered by tests
end

def io_c_msys_patch
patch = @ruby_ver.ruby32? ? IO_C_MSYS_PATCH : IO_C_MSYS_PATCH_PRE_32
patch.merge(IO_C_MSYS_BASE_PATCH)

Check warning on line 95 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L94-L95

Added lines #L94 - L95 were not covered by tests
end

def io_c_patch
patch = PatchHelpers.patch_c_file_pre("/* define system APIs */")
patch.merge!(io_c_msys_patch) if @scmb.msys?
patch

Check warning on line 101 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L99-L101

Added lines #L99 - L101 were not covered by tests
end

def util_c_patch
if @ruby_ver.ruby31?
PatchHelpers.patch_c_file_post("#endif /* !HAVE_GNU_QSORT_R */")

Check warning on line 106 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L105-L106

Added lines #L105 - L106 were not covered by tests
else
PatchHelpers.patch_c_file_pre("#ifndef S_ISDIR")

Check warning on line 108 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L108

Added line #L108 was not covered by tests
end
end

def tool_mkconfig_rb_patch
subst = @scmb.msys? ? TOOL_MKCONFIG_RB_SUBST_MSYS : TOOL_MKCONFIG_RB_SUBST

Check warning on line 113 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L113

Added line #L113 was not covered by tests
{
" if fast[name]" => subst

Check warning on line 115 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L115

Added line #L115 was not covered by tests
}
end

def msys_patches
{
"cygwin/GNUmakefile.in" => get_gnumakefile_in_patch_p2(@ruby_ver),

Check warning on line 121 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L121

Added line #L121 was not covered by tests
"ruby.c" => RUBY_C_MSYS_PATCHES,
"win32/file.c" => WIN32_FILE_C_MSYS_PATCHES,
"win32/win32.c" => WIN32_WIN32_C_MSYS_PATCHES
}
end

def patch_map_base
{
"template/Makefile.in" => template_makefile_in_patch,

Check warning on line 130 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L130

Added line #L130 was not covered by tests
"tool/mkconfig.rb" => tool_mkconfig_rb_patch,
"dir.c" => dir_c_patch, "dln.c" => dln_c_patch,
"io.c" => io_c_patch, "main.c" => PatchMain.get_main_c_patch(@ruby_ver),
"file.c" => PatchHelpers.patch_c_file_pre("/* define system APIs */"),
"util.c" => util_c_patch
}
end

def mlibs_subst
yjit_libs = @ruby_ver.ruby32only? ? "$(YJIT_LIBS) " : ""

Check warning on line 140 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L140

Added line #L140 was not covered by tests
{
"MAINLIBS = #{yjit_libs}@MAINLIBS@" =>

Check warning on line 142 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L142

Added line #L142 was not covered by tests
"# -- Start of tebako patch -- \n" \
"MAINLIBS = #{yjit_libs}#{PatchLibraries.mlibs(@ostype, @deps_lib_dir, @ruby_ver, true)}\n" \
"# -- End of tebako patch -- \n"
}
end

def template_makefile_in_patch
template_makefile_in_patch_two(@ruby_ver).merge(mlibs_subst)

Check warning on line 150 in lib/tebako/packager/pass2_patch.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/packager/pass2_patch.rb#L150

Added line #L150 was not covered by tests
end
end
end
end
22 changes: 12 additions & 10 deletions lib/tebako/scenario_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ module Tebako

# A couple of static Scenario definitions
class ScenarioManagerBase
def initialize
@linux = RUBY_PLATFORM =~ /linux/ ? true : false
@macos = RUBY_PLATFORM =~ /darwin/ ? true : false
@msys = RUBY_PLATFORM =~ /msys|mingw|cygwin/ ? true : false

@fs_mount_point = if @msys
"A:/__tebako_memfs__"
else
"/__tebako_memfs__"
end
def initialize(ostype = RUBY_PLATFORM)
@ostype = ostype
@linux = @ostype =~ /linux/ ? true : false
@musl = @ostype =~ /linux-musl/ ? true : false
@macos = @ostype =~ /darwin/ ? true : false
@msys = @ostype =~ /msys|mingw|cygwin/ ? true : false

@fs_mount_point = @msys ? "A:/__tebako_memfs__" : "/__tebako_memfs__"
@exe_suffix = @msys ? ".exe" : ""
end

Expand Down Expand Up @@ -90,6 +88,10 @@ def msys?
@msys
end

def musl?
@musl

Check warning on line 92 in lib/tebako/scenario_manager.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/scenario_manager.rb#L92

Added line #L92 was not covered by tests
end

def ncores
if @ncores.nil?
if @macos
Expand Down
2 changes: 1 addition & 1 deletion spec/packager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
let(:patch_map) { { "file1" => "patch1", "file2" => "patch2" } }

before do
allow(Tebako::Packager::Pass2).to receive(:get_patch_map).and_return(patch_map)
allow_any_instance_of(Tebako::Packager::Pass2Patch).to receive(:patch_map).and_return(patch_map)
allow(Tebako::Packager).to receive(:do_patch)
end

Expand Down

0 comments on commit d27d0f2

Please sign in to comment.