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.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kompo.rb', line 61

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_pc = File.join(ruby_src_path || File.join(dir, 'dest_dir', 'lib', 'pkgconfig'), 'ruby.pc')
  @ruby_bin = File.join(ruby_src_path || File.join(dir, 'dest_dir', 'bin'), 'ruby')
  @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 = []
end

Instance Attribute Details

#bundle_rubyObject (readonly)

Returns the value of attribute bundle_ruby.



56
57
58
# File 'lib/kompo.rb', line 56

def bundle_ruby
  @bundle_ruby
end

#bundle_setupObject (readonly)

Returns the value of attribute bundle_setup.



56
57
58
# File 'lib/kompo.rb', line 56

def bundle_setup
  @bundle_setup
end

#encinit_oObject (readonly)

Returns the value of attribute encinit_o.



56
57
58
# File 'lib/kompo.rb', line 56

def encinit_o
  @encinit_o
end

#extinit_oObject (readonly)

Returns the value of attribute extinit_o.



56
57
58
# File 'lib/kompo.rb', line 56

def extinit_o
  @extinit_o
end

#fsObject (readonly)

Returns the value of attribute fs.



56
57
58
# File 'lib/kompo.rb', line 56

def fs
  @fs
end

#lib_ruby_static_dirObject (readonly)

Returns the value of attribute lib_ruby_static_dir.



56
57
58
# File 'lib/kompo.rb', line 56

def lib_ruby_static_dir
  @lib_ruby_static_dir
end

#ruby_binObject (readonly)

Returns the value of attribute ruby_bin.



56
57
58
# File 'lib/kompo.rb', line 56

def ruby_bin
  @ruby_bin
end

#ruby_pcObject (readonly)

Returns the value of attribute ruby_pc.



56
57
58
# File 'lib/kompo.rb', line 56

def ruby_pc
  @ruby_pc
end

#ruby_src_dirObject (readonly)

Returns the value of attribute ruby_src_dir.



56
57
58
# File 'lib/kompo.rb', line 56

def ruby_src_dir
  @ruby_src_dir
end

#taskObject (readonly)

Returns the value of attribute task.



56
57
58
# File 'lib/kompo.rb', line 56

def task
  @task
end

#work_dirObject (readonly)

Returns the value of attribute work_dir.



56
57
58
# File 'lib/kompo.rb', line 56

def work_dir
  @work_dir
end

Class Method Details

.cd_work_dir(option) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/kompo.rb', line 85

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



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 130

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'
    ].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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/kompo.rb', line 95

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_exts_dir}"
      ].join(' ')
      exec_command command, 'configure'

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

#copy_to_dest_dirObject



222
223
224
225
# File 'lib/kompo.rb', line 222

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

#fs_cliObject



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

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



120
121
122
123
124
125
126
127
128
# File 'lib/kompo.rb', line 120

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

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

#make_main_cObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/kompo.rb', line 164

def make_main_c
  require 'erb'

  exts = []
  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
      objs = File.read(makefile).scan(/OBJS = (.*\.o)/).join(' ')
      command = ['make', '-C', dir_name, objs, '--always-make'].join(' ')
      exec_command command, 'make'
      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

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

#packingObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/kompo.rb', line 200

def packing
  command = [
    'gcc',
    '-O3',
    '-Wall',
    'main.c',
    'exts/**/*.o',
    'fs.o',
    "#{lib_ruby_static_dir.nil? ? '' : '-L' + lib_ruby_static_dir}",
    "#{lib_kompo_dir.nil? ? '' : '-L' + lib_kompo_dir}",
    get_ruby_header,
    get_exts,
    '-lkompo',
    '-lruby-static',
    get_libs,
    '-o',
    output
  ].join(' ')

  exec_command command, 'Packing'
end

#valid?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
# File 'lib/kompo.rb', line 77

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