Class: Kompo::Tasks

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/kompo.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option, dir) ⇒ Tasks

Returns a new instance of Tasks.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/kompo.rb', line 80

def initialize(option, dir)
  @option = option
  @ruby_src_dir = File.expand_path(ruby_src_path || File.join(dir, 'ruby'))
  @work_dir = dir
  @fs = Fs.new

  @ruby_bin = File.join(ruby_src_path || File.join(dir, 'dest_dir', 'bin'), 'ruby')
  @ruby_pc = File.join(ruby_src_path || File.join(dir, 'dest_dir', 'lib', 'pkgconfig'), get_ruby_pc_name)
  @extinit_o = File.join(ruby_src_dir, 'ext', 'extinit.o')
  @encinit_o = File.join(ruby_src_dir, 'enc', 'encinit.o')
  @lib_ruby_static_dir = ruby_src_path || File.join(dir, 'dest_dir', 'lib')

  @std_libs = []
  @gem_libs = []
  @exts_libs = []
end

Instance Attribute Details

#bundle_rubyObject (readonly)

Returns the value of attribute bundle_ruby.



75
76
77
# File 'lib/kompo.rb', line 75

def bundle_ruby
  @bundle_ruby
end

#bundle_setupObject (readonly)

Returns the value of attribute bundle_setup.



75
76
77
# File 'lib/kompo.rb', line 75

def bundle_setup
  @bundle_setup
end

#encinit_oObject (readonly)

Returns the value of attribute encinit_o.



75
76
77
# File 'lib/kompo.rb', line 75

def encinit_o
  @encinit_o
end

#extinit_oObject (readonly)

Returns the value of attribute extinit_o.



75
76
77
# File 'lib/kompo.rb', line 75

def extinit_o
  @extinit_o
end

#exts_libsObject (readonly)

Returns the value of attribute exts_libs.



75
76
77
# File 'lib/kompo.rb', line 75

def exts_libs
  @exts_libs
end

#fsObject (readonly)

Returns the value of attribute fs.



75
76
77
# File 'lib/kompo.rb', line 75

def fs
  @fs
end

#lib_ruby_static_dirObject (readonly)

Returns the value of attribute lib_ruby_static_dir.



75
76
77
# File 'lib/kompo.rb', line 75

def lib_ruby_static_dir
  @lib_ruby_static_dir
end

#ruby_binObject (readonly)

Returns the value of attribute ruby_bin.



75
76
77
# File 'lib/kompo.rb', line 75

def ruby_bin
  @ruby_bin
end

#ruby_pcObject (readonly)

Returns the value of attribute ruby_pc.



75
76
77
# File 'lib/kompo.rb', line 75

def ruby_pc
  @ruby_pc
end

#ruby_src_dirObject (readonly)

Returns the value of attribute ruby_src_dir.



75
76
77
# File 'lib/kompo.rb', line 75

def ruby_src_dir
  @ruby_src_dir
end

#taskObject (readonly)

Returns the value of attribute task.



75
76
77
# File 'lib/kompo.rb', line 75

def task
  @task
end

#work_dirObject (readonly)

Returns the value of attribute work_dir.



75
76
77
# File 'lib/kompo.rb', line 75

def work_dir
  @work_dir
end

Class Method Details

.cd_work_dir(option) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/kompo.rb', line 117

def self.cd_work_dir(option)
  Dir.mktmpdir do |dir|
    task = new(option.build, dir)
    task.valid?
    FileUtils.cd(dir)

    yield task
  end
end

Instance Method Details

#bundle_installObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/kompo.rb', line 162

def bundle_install
  if cache_bundle_path
    FileUtils.cp_r(cache_bundle_path, work_dir)
    @bundle_setup = File.join(cache_bundle_path, 'bundler', 'setup.rb')
    @bundle_ruby = File.join(cache_bundle_path, 'ruby')
  else
    File.write('./bundler', File.read(`which bundle`.chomp).split("\n").tap { _1[0] = "#!#{ruby_bin}" }.join("\n"))
    FileUtils.chmod(0755, './bundler')

    command = [
      './bundler',
      'install',
      "--standalone=#{use_group}",
    ].join(' ')

    exec_command command, 'bundle install'

    @bundle_setup = File.join(work_dir, 'bundle', 'bundler', 'setup.rb')
    @bundle_ruby = File.join(work_dir, 'bundle', 'ruby')
  end
end

#clone_ruby_srcObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/kompo.rb', line 127

def clone_ruby_src
  if ruby_src_path.nil?
    command = ['git', '-C', "#{work_dir}", 'clone', '-b', "#{ruby_version}", '--single-branch', '--depth=1', 'https://github.com/ruby/ruby.git'].join(' ')
    exec_command command, 'git clone ruby.git'

    Dir.chdir(ruby_src_dir) do
      exec_command (File.exist?("#{ruby_src_dir}/autogen.sh") ? './autogen.sh' : "autoconf"), 'autoxxx'

      command = [
        './configure',
        "--prefix=#{work_dir}/dest_dir",
        "--disable-install-doc",
        "--disable-install-rdoc",
        "--disable-install-capi",
        "--with-static-linked-ext",
        "--with-ruby-pc=ruby.pc",
        "--with-ext=#{get_ruby_exts_dir}"
      ].join(' ')
      exec_command command, 'configure'

      exec_command ['make', 'install'].join(' '), 'build target version ruby'
    end
  end
end

#copy_to_dest_dirObject



274
275
276
277
# File 'lib/kompo.rb', line 274

def copy_to_dest_dir
  command = ['cp', '-f', output, dest_dir].join(' ')
  exec_command command, 'Copy to dest dir'
end

#fs_cliObject



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/kompo.rb', line 184

def fs_cli
  command = [
    komop_cli,
    context,
    args.join(' '),
    get_load_paths,
    "--entrypoint=#{entrypoint}",
  ].join(' ')

  exec_command command, 'kompo-cli'
end

#get_from_ruby_pc(option) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/kompo.rb', line 152

def get_from_ruby_pc(option)
  command = [
    'pkg-config',
    "#{option}",
    "#{ruby_pc}"
  ].join(' ')

  exec_command(command, 'pkg-config', true)
end

#get_ruby_pc_nameObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/kompo.rb', line 97

def get_ruby_pc_name
  return 'ruby.pc' unless ruby_src_path

  command = [
    ruby_bin,
    '-e',
    "'puts RbConfig::CONFIG[\"ruby_pc\"]'",
  ].join(' ')

  exec_command command, 'get ruby.pc name', true
end

#make_main_cObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/kompo.rb', line 196

def make_main_c
  require 'erb'

  exts = []
  if gemfile
    Dir.glob(File.join(bundle_ruby, get_semantic_ruby_version, 'gems/**/extconf.rb')).each do |makefile_dir|
      dir_name = File.dirname(makefile_dir)
      makefile = File.join(dir_name, 'Makefile')
      if File.exist?(cargo_toml = File.join(dir_name, 'Cargo.toml'))
        command = [
          'cargo',
          'rustc',
          '--release',
          '--crate-type=staticlib',
          '--target-dir',
          'target',
          "--manifest-path=#{cargo_toml}",
        ].join(' ')
        exec_command command, 'cargo build'
        copy_targets = Dir.glob(File.join(dir_name, 'target/release/*.a'))
      else
        copy_targets = []
        Dir.chdir(dir_name) {|path|
          command = [
            ruby_bin,
            'extconf.rb',
          ].join(' ')

          exec_command command, 'ruby extconf.rb'

          objs = File.read('./Makefile').match(/OBJS = (.*\.o)/)[1]

          command = ['make', objs, '--always-make'].join(' ')

          exec_command command, 'make OBJS'

          @exts_libs += File.read('./Makefile').match(/^libpath = (.*)/)[1].split(' ')

          copy_targets = objs.split(' ').map { File.join(dir_name, _1) }
        }
      end

      dir = FileUtils.mkdir_p('exts/' + File.basename(dir_name)).first
      FileUtils.cp(copy_targets, dir)
      prefix = File.read(makefile).scan(/target_prefix = (.*)/).join.delete_prefix('/')
      target_name = File.read(makefile).scan(/TARGET_NAME = (.*)/).join
      exts << [File.join(prefix, "#{target_name}.so").delete_prefix('/'), "Init_#{target_name}"]
    end
  end

  File.write("main.c", ERB.new(File.read(File.join(__dir__, 'main.c.erb'))).result(binding))
end

#packingObject



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/kompo.rb', line 249

def packing
  command = [
    'gcc',
    '-O3',
    '-Wall',
    get_ruby_header,
    "#{lib_ruby_static_dir.nil? ? '' : '-L' + lib_ruby_static_dir}",
    "#{lib_kompo_dir.nil? ? '' : '-L' + lib_kompo_dir}",
    "#{exts_libs.uniq.select{_1.start_with?('/')}.map{"-L#{_1}"}.join(' ')}",
    'main.c',
    '-Wl,--start-group',
    Dir.glob('exts/**/*.o').join(' '),
    'fs.o',
    get_ruby_exts,
    '-lkompo',
    '-lruby-static',
    get_libs,
    '-Wl,--end-group',
    '-o',
    output
  ].join(' ')

  exec_command command, 'Packing'
end

#valid?Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
# File 'lib/kompo.rb', line 109

def valid?
  raise "kompo-cli not found. Please install 'kompo-cli'." unless komop_cli
  raise "libkompo_fs.a not found. Please install 'kompo-cli'." unless lib_kompo_dir
  raise "Entrypoint not found: '#{entrypoint}'. Please specify the entry file path with '-e' or '--entrypoint' option." unless File.exist?(entrypoint)

  true
end