tebako 0.5.12 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (c) 2021-2023 [Ribose Inc](https://www.ribose.com).
3
+ # Copyright (c) 2021-2024 [Ribose Inc](https://www.ribose.com).
4
4
  # All rights reserved.
5
5
  # This file is a part of tebako
6
6
  #
@@ -26,6 +26,7 @@
26
26
  # POSSIBILITY OF SUCH DAMAGE.
27
27
 
28
28
  require_relative "patch_literals"
29
+ require_relative "patch_libraries"
29
30
  require_relative "patch_helpers"
30
31
 
31
32
  # Tebako - an executable packager
@@ -37,12 +38,16 @@ module Tebako
37
38
  def get_patch_map(ostype, deps_lib_dir, ruby_ver)
38
39
  patch_map = get_patch_map_base(ostype, deps_lib_dir, ruby_ver)
39
40
 
40
- C_FILES_TO_PATCH.each { |patch| patch_map.store(patch[0], patch_c_file(patch[1])) }
41
41
  patch_map.store("thread_pthread.c", LINUX_MUSL_THREAD_PTHREAD_PATCH) if ostype =~ /linux-musl/
42
- patch_map.store("common.mk", COMMON_MK_PATCH) if PatchHelpers.ruby3x?(ruby_ver)
43
42
 
44
- ostype =~ /msys/ ? patch_map.merge!(MSYS_PATCHES) : patch_map
45
- # patch_map.merge!(LINUX_PATCHES)
43
+ if ostype =~ /msys/
44
+ patch_map.merge!(get_msys_patches(ruby_ver))
45
+ elsif PatchHelpers.ruby3x?(ruby_ver)
46
+ # [TODO] Do we really need it for platforms other then Windows ??
47
+ patch_map.store("common.mk", COMMON_MK_PATCH)
48
+ end
49
+
50
+ patch_map
46
51
  end
47
52
 
48
53
  private
@@ -54,97 +59,69 @@ module Tebako
54
59
  dir_c_patch.merge!(DIR_C_BASE_PATCH)
55
60
  end
56
61
 
57
- def get_patch_map_base(ostype, deps_lib_dir, ruby_ver)
58
- {
59
- "template/Makefile.in" => template_makefile_in_patch(ostype, deps_lib_dir, ruby_ver),
60
- "main.c" => MAIN_C_PATCH,
61
- "tool/mkconfig.rb" => TOOL_MKCONFIG_RB_PATCH,
62
- "gem_prelude.rb" => GEM_PRELUDE_RB_PATCH,
63
- "dir.c" => get_dir_c_patch(ostype)
62
+ def get_dln_c_patch(ostype)
63
+ # Not using substitutions of dlxxx functions on Windows
64
+ dln_c_patch = {
65
+ "static const char funcname_prefix[sizeof(FUNCNAME_PREFIX) - 1] = FUNCNAME_PREFIX;" =>
66
+ "#{ostype =~ /msys/ ? C_FILE_SUBST_LESS : C_FILE_SUBST}\n" \
67
+ "static const char funcname_prefix[sizeof(FUNCNAME_PREFIX) - 1] = FUNCNAME_PREFIX;\n"
64
68
  }
69
+ dln_c_patch.merge!(DLN_C_MSYS_PATCH) if ostype =~ /msys/
70
+ dln_c_patch
65
71
  end
66
72
 
67
- def process_brew_libs!(libs, brew_libs)
68
- brew_libs.each { |lib| libs << "#{PatchHelpers.get_prefix_macos(lib[0]).chop}/lib/lib#{lib[1]}.a " }
73
+ def get_io_c_patch(ostype)
74
+ io_c_patch = patch_c_file("/* define system APIs */")
75
+ io_c_patch.merge!(IO_C_MSYS_PATCH) if ostype =~ /msys/
76
+ io_c_patch
69
77
  end
70
78
 
71
- def darwin_libs(deps_lib_dir, ruby_ver)
72
- libs = String.new
79
+ def get_gnumakefile_in_patch_p2(ruby_ver) # rubocop:disable Metrics/MethodLength
80
+ # For pass 2 we 'kill ruby.exp' regeneration
81
+ objext = PatchHelpers.ruby32?(ruby_ver) ? "$(OBJEXT)" : "@OBJEXT@"
82
+ {
83
+ "$(WPROGRAM): $(RUBYW_INSTALL_NAME).res.#{objext}" =>
84
+ "$(WPROGRAM): $(RUBYW_INSTALL_NAME).res.#{objext} $(WINMAINOBJ) # tebako patched",
73
85
 
74
- process_brew_libs!(libs, PatchHelpers.ruby31?(ruby_ver) ? DARWIN_BREW_LIBS_31 : DARWIN_BREW_LIBS_PRE_31)
75
- process_brew_libs!(libs, DARWIN_BREW_LIBS)
86
+ "$(MAINOBJ) $(EXTOBJS) $(LIBRUBYARG) $(LIBS) -o $@" =>
87
+ "$(WINMAINOBJ) $(EXTOBJS) $(LIBRUBYARG) $(MAINLIBS) -o $@ # tebako patched",
76
88
 
77
- DARWIN_DEP_LIBS.each { |lib| libs << "#{deps_lib_dir}/lib#{lib}.a " }
78
- <<~SUBST
79
- -ltebako-fs -ldwarfs-wr -ldwarfs -force_load #{deps_lib_dir}/libdwarfs_compression.a -lfolly -lfsst -lmetadata_thrift -lthrift_light -lxxhash \
80
- -lzstd #{libs} -ljemalloc -lc++ -lc++abi
81
- SUBST
82
- end
89
+ "RUBYDEF = $(DLL_BASE_NAME).def" => GNUMAKEFILE_IN_WINMAIN_SUBST,
83
90
 
84
- # .....................................................
85
- # Notes re linux libraries
86
- # 1) This order is important: -lgcc_eh -l:libunwind.a -l:liblzma.a lzma is used to process debug sections.
87
- # gcc_eh shall be linked before unwind to avoid duplicate symbols.
88
- # 2) -lgcc_eh assumes -static-libgcc (applied in CMakeLists.ext, RUBY_C_FLAGS)
89
- # 3) -static-libstdc++ did not work, not sure why [TODO ?]
90
- # 4) When clang is used linker links libraries specified in exensions in such way that they are linked shared
91
- # (libz, libffi, libreadline, libncurses, libtinfo, ... )
92
- # Using stuff like -l:libz.a does not help; there is a reference to libz.so anyway.
93
- # This is fixed by ext/extmk.rb patch [TODO ?]
94
- # .....................................................
95
-
96
- def linux_gnu_libs(ruby_ver)
97
- <<~SUBST
98
- -l:libtebako-fs.a -l:libdwarfs-wr.a -l:libdwarfs.a -Wl,--push-state,--whole-archive -l:libdwarfs_compression.a -Wl,--pop-state -l:libfolly.a -l:libfsst.a \
99
- -l:libmetadata_thrift.a -l:libthrift_light.a -l:libxxhash.a -l:libarchive.a -l:libfmt.a -l:libdouble-conversion.a -l:libglog.a -l:libgflags.a -l:libevent.a \
100
- -l:libiberty.a -l:libacl.a -l:libssl.a -l:libcrypto.a -l:liblz4.a -l:libz.a -l:libzstd.a -l:libbrotlienc.a -l:libbrotlidec.a -l:libbrotlicommon.a -l:libgdbm.a \
101
- -l:libreadline.a -l:libtinfo.a -l:libffi.a -l:libncurses.a -l:libjemalloc.a -l:libcrypt.a -l:libanl.a #{PatchHelpers.yaml_reference(ruby_ver)} \
102
- -l:libboost_system.a -l:libboost_chrono.a -l:libutil.a -l:libstdc++.a -lgcc_eh -l:libunwind.a -l:liblzma.a -l:librt.a -ldl -lpthread -lm
103
- SUBST
104
- end
91
+ "$(RUBY_EXP): $(LIBRUBY_A)" => "dummy.exp: $(LIBRUBY_A) # tebako patched",
105
92
 
106
- def linux_musl_libs(ruby_ver)
107
- <<~SUBST
108
- -l:libtebako-fs.a -l:libdwarfs-wr.a -l:libdwarfs.a -Wl,--push-state,--whole-archive -l:libdwarfs_compression.a -Wl,--pop-state -l:libfolly.a -l:libfsst.a \
109
- -l:libmetadata_thrift.a -l:libthrift_light.a -l:libxxhash.a -l:libfmt.a -l:libdouble-conversion.a -l:libglog.a -l:libgflags.a -l:libevent.a -l:libiberty.a \
110
- -l:libacl.a -l:libssl.a -l:libcrypto.a -l:liblz4.a -l:libz.a -l:libzstd.a -l:libbrotlienc.a -l:libbrotlidec.a -l:libbrotlicommon.a -l:libgdbm.a -l:libreadline.a \
111
- -l:libffi.a -l:libncurses.a -l:libjemalloc.a -l:libcrypt.a #{PatchHelpers.yaml_reference(ruby_ver)} -l:libboost_system.a -l:libboost_chrono.a -l:librt.a \
112
- -l:libstdc++.a -lgcc_eh -l:libunwind.a -l:liblzma.a -ldl -lpthread
113
- SUBST
93
+ "$(PROGRAM): $(RUBY_INSTALL_NAME).res.#{objext}" =>
94
+ "$(PROGRAM): $(RUBY_INSTALL_NAME).res.#{objext} $(LIBRUBY_A) # tebako patched\n" \
95
+ "$(LIBRUBY_A): $(LIBRUBY_A_OBJS) $(INITOBJS) # tebako patched\n"
96
+ }
114
97
  end
115
98
 
116
- def msys_libs(ruby_ver)
117
- <<~SUBST
118
- -l:libtebako-fs.a -l:libdwarfs-wr.a -l:libdwarfs.a -l:libfolly.a -l:libfsst.a -l:libmetadata_thrift.a -l:libthrift_light.a -l:libxxhash.a \
119
- -l:libfmt.a -l:libdouble-conversion.a -l:libglog.a -l:libgflags.a -l:libevent.a -l:libssl.a -l:libcrypto.a -l:liblz4.a -l:libz.a \
120
- -l:libzstd.a -l:libffi.a -l:libgdbm.a -l:libncurses.a -l:libjemalloc.a -l:libunwind.a -l:liblzma.a -l:libiberty.a \
121
- #{PatchHelpers.yaml_reference(ruby_ver)} -l:libstdc++.a -l:libdl.a -lole32 -loleaut32 -luuid
122
- SUBST
99
+ def get_msys_patches(ruby_ver)
100
+ {
101
+ "cygwin/GNUmakefile.in" => get_gnumakefile_in_patch_p2(ruby_ver),
102
+ "ruby.c" => RUBY_C_MSYS_PATCHES,
103
+ "win32/file.c" => WIN32_FILE_C_MSYS_PATCHES,
104
+ "win32/win32.c" => WIN32_WIN32_C_MSYS_PATCHES
105
+ }
123
106
  end
124
107
 
125
- # rubocop:disable Metrics/MethodLength
126
- def mlibs(ostype, deps_lib_dir, ruby_ver)
127
- case ostype
128
- when /linux-gnu/
129
- linux_gnu_libs(ruby_ver)
130
- when /linux-musl/
131
- linux_musl_libs(ruby_ver)
132
- when /darwin/
133
- darwin_libs(deps_lib_dir, ruby_ver)
134
- when /msys/
135
- msys_libs(ruby_ver)
136
- else
137
- raise Tebako::Error, "Unknown ostype #{ostype}"
138
- end
108
+ def get_patch_map_base(ostype, deps_lib_dir, ruby_ver)
109
+ {
110
+ "template/Makefile.in" => template_makefile_in_patch(ostype, deps_lib_dir, ruby_ver),
111
+ "tool/mkconfig.rb" => ostype =~ /msys/ ? TOOL_MKCONFIG_RB_PATCH_MSYS : TOOL_MKCONFIG_RB_PATCH,
112
+ "gem_prelude.rb" => GEM_PRELUDE_RB_PATCH,
113
+ "dir.c" => get_dir_c_patch(ostype), "dln.c" => get_dln_c_patch(ostype),
114
+ "io.c" => get_io_c_patch(ostype), "file.c" => patch_c_file("/* define system APIs */"),
115
+ "main.c" => MAIN_C_PATCH, "util.c" => patch_c_file("#ifndef S_ISDIR")
116
+ }
139
117
  end
140
- # rubocop:enable Metrics/MethodLength
141
118
 
142
119
  def mlibs_subst(ostype, deps_lib_dir, ruby_ver)
143
120
  yjit_libs = PatchHelpers.ruby32only?(ruby_ver) ? "$(YJIT_LIBS) " : ""
144
121
  {
145
122
  "MAINLIBS = #{yjit_libs}@MAINLIBS@" =>
146
123
  "# -- Start of tebako patch -- \n" \
147
- "MAINLIBS = #{yjit_libs}#{mlibs(ostype, deps_lib_dir, ruby_ver)}" \
124
+ "MAINLIBS = #{yjit_libs}#{PatchLibraries.mlibs(ostype, deps_lib_dir, ruby_ver)}" \
148
125
  "# -- End of tebako patch -- \n"
149
126
  }
150
127
  end
@@ -156,11 +133,13 @@ module Tebako
156
133
  end
157
134
 
158
135
  def template_makefile_in_patch(ostype, deps_lib_dir, ruby_ver)
159
- template_makefile_in_patch_two(ruby_ver).merge(mlibs_subst(ostype, deps_lib_dir, ruby_ver))
136
+ template_makefile_in_patch_two(ostype, ruby_ver).merge(mlibs_subst(ostype, deps_lib_dir, ruby_ver))
160
137
  end
161
138
 
162
- def template_makefile_in_patch_two(ruby_ver)
163
- if PatchHelpers.ruby31?(ruby_ver)
139
+ def template_makefile_in_patch_two(ostype, ruby_ver)
140
+ if ostype =~ /msys/
141
+ { TEMPLATE_MAKEFILE_IN_BASE_PATTERN => TEMPLATE_MAKEFILE_IN_BASE_PATCH_MSYS }
142
+ elsif PatchHelpers.ruby31?(ruby_ver)
164
143
  { TEMPLATE_MAKEFILE_IN_BASE_PATTERN => TEMPLATE_MAKEFILE_IN_BASE_PATCH }
165
144
  else
166
145
  { TEMPLATE_MAKEFILE_IN_BASE_PATTERN_PRE_3_1 => TEMPLATE_MAKEFILE_IN_BASE_PATCH_PRE_3_1 }
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2021-2024 [Ribose Inc](https://www.ribose.com).
4
+ # All rights reserved.
5
+ # This file is a part of tebako
6
+ #
7
+ # Redistribution and use in source and binary forms, with or without
8
+ # modification, are permitted provided that the following conditions
9
+ # are met:
10
+ # 1. Redistributions of source code must retain the above copyright
11
+ # notice, this list of conditions and the following disclaimer.
12
+ # 2. Redistributions in binary form must reproduce the above copyright
13
+ # notice, this list of conditions and the following disclaimer in the
14
+ # documentation and/or other materials provided with the distribution.
15
+ #
16
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+ # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18
+ # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
20
+ # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
+ # POSSIBILITY OF SUCH DAMAGE.
27
+
28
+ require_relative "patch_helpers"
29
+
30
+ # Tebako - an executable packager
31
+ module Tebako
32
+ module Packager
33
+ # Ruby patching definitions (pass2)
34
+ module PatchLibraries
35
+ class << self
36
+ # rubocop:disable Style/WordArray
37
+
38
+ # NOTE: folly provides build-in implementation of jemalloc
39
+
40
+ DARWIN_BREW_LIBS = [
41
+ ["zlib", "z"], ["gdbm", "gdbm"], ["readline", "readline"], ["libffi", "ffi"],
42
+ ["ncurses", "ncurses"], ["fmt", "fmt"], ["lz4", "lz4"], ["xz", "lzma"],
43
+ ["libyaml", "yaml"], ["boost", "boost_chrono"],
44
+ ["double-conversion", "double-conversion"]
45
+ ].freeze
46
+
47
+ DARWIN_BREW_LIBS_PRE_31 = [["openssl@1.1", "ssl"], ["openssl@1.1", "crypto"]].freeze
48
+
49
+ DARWIN_BREW_LIBS_31 = [["libyaml", "yaml"], ["openssl@3", "ssl"], ["openssl@3", "crypto"]].freeze
50
+
51
+ DARWIN_DEP_LIBS = ["glog", "gflags", "brotlienc", "brotlidec", "brotlicommon"].freeze
52
+ # rubocop:enable Style/WordArray
53
+
54
+ def process_brew_libs!(libs, brew_libs)
55
+ brew_libs.each { |lib| libs << "#{PatchHelpers.get_prefix_macos(lib[0]).chop}/lib/lib#{lib[1]}.a " }
56
+ end
57
+
58
+ def darwin_libs(deps_lib_dir, ruby_ver)
59
+ libs = String.new
60
+
61
+ process_brew_libs!(libs, PatchHelpers.ruby31?(ruby_ver) ? DARWIN_BREW_LIBS_31 : DARWIN_BREW_LIBS_PRE_31)
62
+ process_brew_libs!(libs, DARWIN_BREW_LIBS)
63
+
64
+ DARWIN_DEP_LIBS.each { |lib| libs << "#{deps_lib_dir}/lib#{lib}.a " }
65
+ <<~SUBST
66
+ -ltebako-fs -ldwarfs-wr -ldwarfs -force_load #{deps_lib_dir}/libdwarfs_compression.a -lfolly -lfsst -lmetadata_thrift -lthrift_light -lxxhash \
67
+ -lzstd #{libs} -ljemalloc -lc++ -lc++abi
68
+ SUBST
69
+ end
70
+
71
+ # .....................................................
72
+ # Notes re linux libraries
73
+ # 1) This order is important: -lgcc_eh -l:libunwind.a -l:liblzma.a lzma is used to process debug sections.
74
+ # gcc_eh shall be linked before unwind to avoid duplicate symbols.
75
+ # 2) -lgcc_eh assumes -static-libgcc (applied in CMakeLists.ext, RUBY_C_FLAGS)
76
+ # 3) -static-libstdc++ did not work, not sure why [TODO ?]
77
+ # 4) When clang is used linker links libraries specified in exensions in such way that they are linked shared
78
+ # (libz, libffi, libreadline, libncurses, libtinfo, ... )
79
+ # Using stuff like -l:libz.a does not help; there is a reference to libz.so anyway.
80
+ # This is fixed by ext/extmk.rb patch [TODO ?]
81
+ # .....................................................
82
+
83
+ def linux_common_libs
84
+ <<~SUBST
85
+ -l:libtebako-fs.a -l:libdwarfs-wr.a -l:libdwarfs.a -Wl,--push-state,--whole-archive -l:libdwarfs_compression.a -Wl,--pop-state -l:libfolly.a -l:libfsst.a \\
86
+ -l:libmetadata_thrift.a -l:libthrift_light.a -l:libxxhash.a -l:libfmt.a -l:libdouble-conversion.a -l:libglog.a -l:libgflags.a -l:libevent.a \\
87
+ SUBST
88
+ end
89
+
90
+ def common_enc_libs
91
+ "-l:liblz4.a -l:libz.a -l:libzstd.a -l:libbrotlienc.a -l:libbrotlidec.a -l:libbrotlicommon.a -l:liblzma.a"
92
+ end
93
+
94
+ def linux_gnu_libs(ruby_ver)
95
+ <<~SUBST
96
+ #{linux_common_libs} \
97
+ -l:libarchive.a -l:libiberty.a -l:libacl.a -l:libssl.a -l:libcrypto.a #{common_enc_libs} \\
98
+ -l:libgdbm.a -l:libreadline.a -l:libtinfo.a -l:libffi.a -l:libncurses.a -l:libjemalloc.a -l:libcrypt.a -l:libanl.a #{PatchHelpers.yaml_reference(ruby_ver)} \\
99
+ -l:libboost_system.a -l:libboost_chrono.a -l:libutil.a -l:libstdc++.a -lgcc_eh -l:libunwind.a -l:liblzma.a -l:librt.a -ldl -lpthread -lm
100
+ SUBST
101
+ end
102
+
103
+ def linux_musl_libs(ruby_ver)
104
+ <<~SUBST
105
+ #{linux_common_libs} \
106
+ -l:libiberty.a -l:libacl.a -l:libssl.a -l:libcrypto.a #{common_enc_libs} -l:libreadline.a \\
107
+ -l:libgdbm.a -l:libffi.a -l:libncurses.a -l:libjemalloc.a -l:libcrypt.a #{PatchHelpers.yaml_reference(ruby_ver)} -l:libboost_system.a -l:libboost_chrono.a \\
108
+ -l:librt.a -l:libstdc++.a -lgcc_eh -l:libunwind.a -l:liblzma.a -ldl -lpthread
109
+ SUBST
110
+ end
111
+
112
+ def msys_libs(ruby_ver)
113
+ <<~SUBST
114
+ -Wl,-Bstatic #{linux_common_libs} \
115
+ -l:liblz4.a -l:libz.a -l:libzstd.a -l:liblzma.a -l:libncurses.a -l:libunwind.a -l:liblzma.a -l:libiberty.a #{PatchHelpers.yaml_reference(ruby_ver)} \\
116
+ -l:libffi.a -l:libboost_system-mt.a -l:libboost_chrono-mt.a -l:libstdc++.a -l:libdl.a -static-libgcc -static-libstdc++ -l:libssl.a -l:libcrypto.a -l:libz.a \\
117
+ -l:libwinpthread.a -lcrypt32 -lshlwapi -lwsock32 -liphlpapi -limagehlp -lshlwapi -lbcrypt -lws2_32 -lole32 -loleaut32 -luuid
118
+ SUBST
119
+ end
120
+
121
+ def mlibs(ostype, deps_lib_dir, ruby_ver) # rubocop:disable Metrics/MethodLength
122
+ case ostype
123
+ when /linux-gnu/
124
+ linux_gnu_libs(ruby_ver)
125
+ when /linux-musl/
126
+ linux_musl_libs(ruby_ver)
127
+ when /darwin/
128
+ darwin_libs(deps_lib_dir, ruby_ver)
129
+ when /msys/
130
+ msys_libs(ruby_ver)
131
+ else
132
+ raise Tebako::Error, "Unknown ostype #{ostype}"
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (c) 2023 [Ribose Inc](https://www.ribose.com).
3
+ # Copyright (c) 2023-2024 [Ribose Inc](https://www.ribose.com).
4
4
  # All rights reserved.
5
5
  # This file is a part of tebako
6
6
  #
@@ -29,20 +29,14 @@
29
29
  module Tebako
30
30
  module Packager
31
31
  # Ruby patching literals (pass2)
32
- # rubocop:disable Metrics/ModuleLength
33
- module PatchLiterals
32
+ module PatchLiterals # rubocop:disable Metrics/ModuleLength
34
33
  TOOL_MKCONFIG_RB_PATCH = {
35
34
  " if fast[name]" => <<~SUBST
36
35
  # -- Start of tebako patch --
37
36
  v_head_comp = " CONFIG[\\"prefix\\"] \#{eq} "
38
37
  if v_head_comp == v[0...(v_head_comp.length)]
39
- if win32
40
- v = "\#{v[0...(v_head_comp.length)]}CONFIG[\\"RUBY_EXEC_PREFIX\\"] = '/__tebako_memfs__'
41
- "
42
- else
43
38
  v = "\#{v[0...(v_head_comp.length)]}'/__tebako_memfs__'
44
39
  "
45
- end
46
40
  end
47
41
  v_head_comp = " CONFIG[\\"RUBY_EXEC_PREFIX\\"] \#{eq} "
48
42
  if v_head_comp == v[0...(v_head_comp.length)]
@@ -54,6 +48,24 @@ module Tebako
54
48
  SUBST
55
49
  }.freeze
56
50
 
51
+ TOOL_MKCONFIG_RB_PATCH_MSYS = {
52
+ " if fast[name]" => <<~SUBST
53
+ # -- Start of tebako patch --
54
+ v_head_comp = " CONFIG[\\"prefix\\"] \#{eq} "
55
+ if v_head_comp == v[0...(v_head_comp.length)]
56
+ v = "\#{v[0...(v_head_comp.length)]}CONFIG[\\"RUBY_EXEC_PREFIX\\"] = 'A:/__tebako_memfs__'
57
+ "
58
+ end
59
+ v_head_comp = " CONFIG[\\"RUBY_EXEC_PREFIX\\"] \#{eq} "
60
+ if v_head_comp == v[0...(v_head_comp.length)]
61
+ v = "\#{v[0...(v_head_comp.length)]}'A:/__tebako_memfs__'
62
+ "
63
+ end
64
+ # -- End of tebako patch --
65
+ if fast[name]
66
+ SUBST
67
+ }.freeze
68
+
57
69
  # Alpine-specific patches https://github.com/docker-library/ruby/blob/master/3.1/alpine3.15/Dockerfile
58
70
  # -- Patch no. 1 --
59
71
  # https://github.com/docker-library/ruby/issues/196
@@ -125,6 +137,7 @@ module Tebako
125
137
  ruby_sysinit(&argc, &argv);
126
138
  /* -- Start of tebako patch -- */
127
139
  if (tebako_main(&argc, &argv) != 0) {
140
+ printf("Tebako intialization failed.");
128
141
  return -1;
129
142
  }
130
143
  /* -- End of tebako patch -- */
@@ -156,18 +169,33 @@ module Tebako
156
169
  "else if (e == EIO /* tebako patch */ && !within_tebako_memfs(path)) {"
157
170
  }.freeze
158
171
 
172
+ DLN_C_MSYS_PATCH = {
173
+ " winfile = rb_w32_mbstr_to_wstr(CP_UTF8, file, -1, NULL);" => <<~SUBST
174
+ /* -- Start of tebako patch -- */
175
+ char *f = NULL;
176
+ winfile = NULL;
177
+ if (file && within_tebako_memfs(file)) {
178
+ f = tebako_dlmap2file(file);
179
+ if (f) {
180
+ winfile = rb_w32_mbstr_to_wstr(CP_UTF8, f, -1, NULL);
181
+ free(f);
182
+ }
183
+ else {
184
+ goto failed;
185
+ }
186
+ }
187
+ else {
188
+ winfile = rb_w32_mbstr_to_wstr(CP_UTF8, file, -1, NULL);
189
+ }
190
+ /* -- End of tebako patch -- */
191
+ SUBST
192
+ }.freeze
193
+
159
194
  COMMON_MK_PATCH = {
160
195
  "ext/extinit.c: $(srcdir)/template/extinit.c.tmpl $(PREP)" =>
161
196
  "ext/extinit.c: $(srcdir)/template/extinit.c.tmpl $(PREP) $(EXTS_MK)"
162
197
  }.freeze
163
198
 
164
- C_FILES_TO_PATCH = [
165
- ["file.c", "/* define system APIs */"],
166
- ["io.c", "/* define system APIs */"],
167
- ["util.c", "#ifndef S_ISDIR"],
168
- ["dln.c", "static const char funcname_prefix[sizeof(FUNCNAME_PREFIX) - 1] = FUNCNAME_PREFIX;"]
169
- ].freeze
170
-
171
199
  TEMPLATE_MAKEFILE_IN_BASE_PATTERN_PRE_3_1 =
172
200
  "\t\t$(Q) $(PURIFY) $(CC) $(LDFLAGS) $(XLDFLAGS) $(MAINOBJ) " \
173
201
  "$(EXTOBJS) $(LIBRUBYARG) $(MAINLIBS) $(LIBS) $(EXTLIBS) $(OUTFLAG)$@"
@@ -188,14 +216,24 @@ module Tebako
188
216
  "$(EXTOBJS) $(LIBRUBYARG_STATIC) $(OUTFLAG)$@\n" \
189
217
  "# -- End of tebako patch --"
190
218
 
219
+ TEMPLATE_MAKEFILE_IN_BASE_PATCH_MSYS =
220
+ "# -- Start of tebako patch --\n" \
221
+ "\t\t$(Q) $(PURIFY) $(CC) $(EXE_LDFLAGS) $(XLDFLAGS) $(RUBY_EXP) $(MAINOBJ) " \
222
+ "$(EXTOBJS) $(LIBRUBYARG_STATIC) $(OUTFLAG)$@\n" \
223
+ "# -- End of tebako patch --"
224
+
191
225
  C_FILE_SUBST = <<~SUBST
192
226
  /* -- Start of tebako patch -- */
193
- #ifndef NO_TEBAKO_INCLUDES
194
227
  #include <tebako/tebako-config.h>
195
228
  #include <tebako/tebako-defines.h>
196
- #include <tebako/tebako-io-rb-w32.h>
197
229
  #include <tebako/tebako-io.h>
198
- #endif
230
+ /* -- End of tebako patch -- */
231
+ SUBST
232
+
233
+ C_FILE_SUBST_LESS = <<~SUBST
234
+ /* -- Start of tebako patch -- */
235
+ #include <tebako/tebako-config.h>
236
+ #include <tebako/tebako-io.h>
199
237
  /* -- End of tebako patch -- */
200
238
  SUBST
201
239
 
@@ -213,68 +251,110 @@ module Tebako
213
251
  SUBST
214
252
  }.freeze
215
253
 
216
- MSYS_PATCHES = {
217
- "ruby.c" => {
218
- "#define RUBY_RELATIVE(path, len) rb_str_buf_cat(BASEPATH(), (path), (len))" =>
219
- "#define RUBY_RELATIVE(path, len) rubylib_path_new((path), (len)) /* tebako patched */",
220
- "#define PREFIX_PATH() sopath" =>
221
- "#define PREFIX_PATH() rubylib_path_new(tebako_mount_point(), " \
222
- "strlen(tebako_mount_point())) /* tebako patched */",
223
- '#include "mjit.h"' =>
224
- "#include \"mjit.h\"\n" \
225
- "/* -- Start of tebako patch -- */\n" \
226
- "#include <tebako/tebako-main.h>\n" \
227
- "/* -- End of tebako patch -- */"
228
- },
229
- "/win32/win32.c" => {
230
- "#undef __STRICT_ANSI__" =>
231
- "#undef __STRICT_ANSI__\n" \
232
- "/* -- Start of tebako patch -- */\n" \
233
- "#define NO_TEBAKO_INCLUDES\n" \
234
- "/* -- End of tebako patch -- */"
235
- },
236
- "win32/dir.h" => {
237
- "#define opendir(s) rb_w32_opendir((s))" => "#{C_FILE_SUBST}\n#define opendir(s) rb_w32_opendir((s))"
238
- },
239
- "file.c" => {
240
- " wpath = mbstr_to_wstr(CP_UTF8, path, -1, &len);" =>
241
- "/* -- Start of tebako patch -- */\n" \
242
- "if (tebako_file_load_ok(path)) return 1;\n" \
243
- "/* -- End of tebako patch -- */\n" \
244
- "wpath = mbstr_to_wstr(CP_UTF8, path, -1, &len);",
245
- '#include "win32/file.h' =>
246
- "#include \"win32/file.h\"\n" \
247
- "/* -- Start of tebako patch -- */\n" \
248
- "#include <tebako/tebako-main.h>\n" \
249
- "/* -- End of tebako patch -- */"
250
- }
254
+ GNUMAKEFILE_IN_WINMAIN_SUBST = <<~SUBST
255
+ RUBYDEF = $(DLL_BASE_NAME).def
256
+
257
+ # Start of tebako patch
258
+ WINMAINOBJ = win32/winmain.$(OBJEXT)
259
+ $(WINMAINOBJ): win32/winmain.c
260
+ # End of tebako patch
261
+ SUBST
251
262
 
263
+ IO_C_SUBST = <<~SUBST
264
+ /* -- Start of tebako patch -- */
265
+ if (is_tebako_file_descriptor(fd)) return;
266
+ /* -- End of tebako patch -- */
267
+ flags = fcntl(fd, F_GETFD); /* should not fail except EBADF. */
268
+ SUBST
269
+
270
+ IO_C_MSYS_PATCH = {
271
+ "#define open rb_w32_uopen" => "#define open(p, f, m) tebako_open(3, (p), (f), (m))",
272
+ "(rb_w32_io_cancelable_p((fptr)->fd) ? Qnil : rb_io_wait(fptr->self, RB_INT2NUM(RUBY_IO_READABLE), Qnil))" =>
273
+ "((is_tebako_file_descriptor((fptr)->fd) || rb_w32_io_cancelable_p((fptr)->fd)) ? \\\n" \
274
+ "Qnil : rb_io_wait(fptr->self, RB_INT2NUM(RUBY_IO_READABLE), Qnil))"
252
275
  }.freeze
253
276
 
254
- LINUX_PATCHES = {
255
- "ext/extmk.rb" => {
256
- "mf.macro \"EXTLIBS\", $extlibs" => "# mf.macro \"EXTLIBS\", $extlibs tebako patched"
257
- }
277
+ FILE_C_MSYS_SUBST = <<~SUBST
278
+ /* -- Start of tebako patch -- */
279
+ if (is_tebako_file_descriptor((fptr)->fd)) return ENOTSUP;
280
+ /* -- End of tebako patch -- */
281
+ while ((int)rb_thread_io_blocking_region(rb_thread_flock, op, fptr->fd) < 0) {
282
+ SUBST
283
+
284
+ FILE_C_MSYS_PATCH = {
285
+ "while ((int)rb_thread_io_blocking_region(rb_thread_flock, op, fptr->fd) < 0) {" => FILE_C_MSYS_SUBST
286
+ }.freeze
287
+
288
+ RUBY_C_MSYS_PATH_SUBST = <<~SUBST
289
+ /* -- Start of tebako patch -- */
290
+ VALUE path = within_tebako_memfs(paths) ?
291
+ rb_str_new_cstr(paths) :
292
+ RUBY_RELATIVE(paths, len);
293
+ /* -- End of tebako patch -- */
294
+ SUBST
295
+
296
+ RUBY_C_MSYS_PATCHES = {
297
+ "#ifndef MAXPATHLEN" => "#{C_FILE_SUBST_LESS}\n#ifndef MAXPATHLEN",
298
+ "VALUE path = RUBY_RELATIVE(paths, len);" => RUBY_C_MSYS_PATH_SUBST
258
299
  }.freeze
259
300
 
260
- # rubocop:disable Style/WordArray
301
+ WIN32_FILE_C_MSYS_SUBST = <<~SUBST
302
+ /* -- Start of tebako patch -- */
303
+ if (tebako_file_load_ok(path)) return ret;
304
+ /* -- End of tebako patch -- */
305
+ wpath = mbstr_to_wstr(CP_UTF8, path, -1, &len);
306
+ SUBST
261
307
 
262
- # NOTE: folly provides build-in implementation of jemalloc
308
+ WIN32_FILE_C_MSYS_PATCHES = {
309
+ "#ifndef INVALID_FILE_ATTRIBUTES" => "#{C_FILE_SUBST_LESS}\n#ifndef INVALID_FILE_ATTRIBUTES",
310
+ "wpath = mbstr_to_wstr(CP_UTF8, path, -1, &len);" => WIN32_FILE_C_MSYS_SUBST
311
+ }.freeze
263
312
 
264
- DARWIN_BREW_LIBS = [
265
- ["zlib", "z"], ["gdbm", "gdbm"], ["readline", "readline"], ["libffi", "ffi"],
266
- ["ncurses", "ncurses"], ["fmt", "fmt"], ["lz4", "lz4"], ["xz", "lzma"],
267
- ["libyaml", "yaml"], ["boost", "boost_chrono"],
268
- ["double-conversion", "double-conversion"]
269
- ].freeze
313
+ WIN32_WIN32_C_MSYS_SUBST = <<~SUBST
314
+ /* -- Start of tebako patch -- */
315
+ if (is_tebako_cwd()) {
316
+ char* tebako_cwd = tebako_getcwd(NULL,0);
317
+ if (tebako_cwd == NULL) {
318
+ errno = ENOMEM;
319
+ return NULL;
320
+ }
321
+ len = strlen(tebako_cwd) + 1;
270
322
 
271
- DARWIN_BREW_LIBS_PRE_31 = [["openssl@1.1", "ssl"], ["openssl@1.1", "crypto"]].freeze
323
+ if (buffer) {
324
+ if (size < len) {
325
+ free(tebako_cwd);
326
+ errno = ERANGE;
327
+ return NULL;
328
+ }
329
+ }
330
+ else {
331
+ buffer = (*alloc)(len, arg);
332
+ if (!buffer) {
333
+ free(tebako_cwd);
334
+ errno = ENOMEM;
335
+ return NULL;
336
+ }
337
+ }
338
+ translate_char(tebako_cwd, (char)0x5c, '/', CP_UTF8);
339
+ strcpy(buffer, tebako_cwd);
340
+ free(tebako_cwd);
341
+ return buffer;
342
+ }
343
+ /* -- End of tebako patch -- */
272
344
 
273
- DARWIN_BREW_LIBS_31 = [["libyaml", "yaml"], ["openssl@3", "ssl"], ["openssl@3", "crypto"]].freeze
345
+ len = GetCurrentDirectoryW(0, NULL);
346
+ SUBST
347
+
348
+ WIN32_WIN32_C_MSYS_PATCHES = {
349
+ "#if defined _MSC_VER && _MSC_VER <= 1200" => "#{C_FILE_SUBST_LESS}\n#if defined _MSC_VER && _MSC_VER <= 1200",
350
+ "len = GetCurrentDirectoryW(0, NULL);" => WIN32_WIN32_C_MSYS_SUBST
351
+ }.freeze
274
352
 
275
- DARWIN_DEP_LIBS = ["glog", "gflags", "brotlienc", "brotlidec", "brotlicommon"].freeze
276
- # rubocop:enable Style/WordArray
353
+ LINUX_PATCHES = {
354
+ "ext/extmk.rb" => {
355
+ "mf.macro \"EXTLIBS\", $extlibs" => "# mf.macro \"EXTLIBS\", $extlibs tebako patched"
356
+ }
357
+ }.freeze
277
358
  end
278
- # rubocop:enable Metrics/ModuleLength
279
359
  end
280
360
  end