Class: CastOff::Compiler::CodeManager

Inherits:
Object
  • Object
show all
Extended by:
Util
Includes:
Util, RbConfig
Defined in:
lib/cast_off/compile/code_manager.rb

Defined Under Namespace

Classes: CompilationTarget

Constant Summary collapse

CastOffDir =
"#{ENV["HOME"]}/.CastOff"
BaseDir =
"#{CastOffDir}/#{base_directory_name()}"
FILEPATH_LIMITED =
CONFIG["host_os"].match(/mswin/)
FILEPATH_LIMIT =
255
@@program_name =
File.basename($PROGRAM_NAME)
@@program_sign =
generate_signiture(@@program_name)
@@compiled_methods_fetch_str =
nil
@@compiled_methods_load_str =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

bug, dlog, todo, vlog

Constructor Details

#initialize(filepath, line_no) ⇒ CodeManager

Returns a new instance of CodeManager.



144
145
146
147
148
149
# File 'lib/cast_off/compile/code_manager.rb', line 144

def initialize(filepath, line_no)
  @filepath = filepath
  @line_no = line_no
  @compilation_target = nil
  set_path()
end

Instance Attribute Details

#compilation_targetObject (readonly)

Returns the value of attribute compilation_target.



13
14
15
# File 'lib/cast_off/compile/code_manager.rb', line 13

def compilation_target
  @compilation_target
end

#signitureObject (readonly)

Returns the value of attribute signiture.



13
14
15
# File 'lib/cast_off/compile/code_manager.rb', line 13

def signiture
  @signiture
end

Class Method Details

.base_directory_nameObject



19
20
21
22
# File 'lib/cast_off/compile/code_manager.rb', line 19

def self.base_directory_name()
  p = generate_signiture(File.expand_path(__FILE__))
  p.gsub(/_lib_ruby_gems_1_9_1_gems_/, '.').gsub(/_lib_cast_off_compile_code_manager_rb$/, '')
end

.clearObject



46
47
48
49
50
51
# File 'lib/cast_off/compile/code_manager.rb', line 46

def self.clear()
  dir = program_dir()
  vlog("delete #{dir}")
  FileUtils.remove_entry_secure(dir)
  dir
end

.compiled_method_exist?(filepath, line_no) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/cast_off/compile/code_manager.rb', line 61

def self.compiled_method_exist?(filepath, line_no)
  return false unless filepath && line_no >= 0
  base_sign = generate_signiture("#{filepath}_#{line_no}")
  File.exist?("#{BaseDir}/#{@@program_sign}/#{base_sign}/loadable")
end

.delete_from_compiled(entry) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/cast_off/compile/code_manager.rb', line 53

def self.delete_from_compiled(entry)
  compiled = load_autocompiled()
  return false unless compiled
  return false unless compiled.delete(entry)
  dump_auto_compiled(compiled)
  return true
end

.dump_auto_compiled(compiled) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cast_off/compile/code_manager.rb', line 93

def self.dump_auto_compiled(compiled)
  dir = program_dir()
  path = "#{dir}/.compiled_methods"
  File.open("#{path}", 'wb:us-ascii') do |f|
    Marshal.dump(compiled.map{|c| c.first}, f) # dump only classes
  end
  @@compiled_methods_fetch_str = nil
  path = "#{dir}/compiled_methods"
  File.open("#{path}", 'wb:us-ascii') do |f|
    Marshal.dump(compiled, f)
  end
  @@compiled_methods_load_str = nil
  load_autocompiled()
end

.generate_signiture(unique_string) ⇒ Object



15
16
17
# File 'lib/cast_off/compile/code_manager.rb', line 15

def self.generate_signiture(unique_string)
  unique_string.gsub(/\.|\/|-|:/, "_")
end

.load_autocompiledObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cast_off/compile/code_manager.rb', line 69

def self.load_autocompiled()
  begin
    dir = program_dir()
    # fetch classes
    path = "#{dir}/.compiled_methods"
    return nil unless File.exist?(path)
    if !@@compiled_methods_fetch_str
      @@compiled_methods_fetch_str = File.open(path, 'rb:us-ascii').read() 
      @@compiled_methods_fetch_str.untaint # FIXME
    end
    Marshal.load(@@compiled_methods_fetch_str)
    # load compiled methods information
    path = "#{dir}/compiled_methods"
    return nil unless File.exist?(path)
    if !@@compiled_methods_load_str
      @@compiled_methods_load_str = File.open(path, 'rb:us-ascii').read() 
      @@compiled_methods_load_str.untaint # FIXME
    end
    Marshal.load(@@compiled_methods_load_str)
  rescue ArgumentError, NameError
    false
  end
end

.program_dirObject



40
41
42
43
44
# File 'lib/cast_off/compile/code_manager.rb', line 40

def self.program_dir()
  dir = "#{BaseDir}/#{@@program_sign}"
  FileUtils.mkdir(dir) unless File.exist?(dir)
  dir
end

Instance Method Details

#check_lengthObject

Raises:

  • (UnsupportedError)


134
135
136
137
138
139
140
141
142
# File 'lib/cast_off/compile/code_manager.rb', line 134

def check_length()
  return unless FILEPATH_LIMITED
  raise(UnsupportedError.new(<<-EOS)) if @longpath.length > FILEPATH_LIMIT

Failed to generate signiture for #{@filepath}:#{@line_no}.
Signiture is generated from filepath and line_no.
Max length of signiture is #{FILEPATH_LIMIT} in this environment.
  EOS
end

#clear_base_configurationObject



293
294
295
296
# File 'lib/cast_off/compile/code_manager.rb', line 293

def clear_base_configuration()
  return unless File.exist?(@base_configuration_path)
  FileUtils.remove_entry_secure(@base_configuration_path)
end

#compilation_target_is_a(target, mid, singleton) ⇒ Object



193
194
195
# File 'lib/cast_off/compile/code_manager.rb', line 193

def compilation_target_is_a(target, mid, singleton)
  @compilation_target = CompilationTarget.new(target, mid, singleton)
end

#compile_c_source(c_source, conf, dep) ⇒ Object



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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/cast_off/compile/code_manager.rb', line 222

def compile_c_source(c_source, conf, dep)
  base = load_base_configuration()
  FileUtils.remove_entry_secure(@dstdir, true)
  create_dstdir()
  File.open("#{@dstdir}/#{@dstfile}", 'w'){|f| f.write(c_source)}
  gen_makefile(@dstdir, @dstfile)
  gen_header_files(@dstdir)
  Dir.chdir(@dstdir) do
    File.open(@dstfile, 'wb:us-ascii') do |f|
      f.write(c_source.dup.force_encoding('US-ASCII'))
    end
    if CONFIG["host_os"].match(/mswin/)
      # windows
      makecmd = 'nmake'
    else
      # linux, freebsd, solaris
      makecmd = 'make'
    end
    log = `#{makecmd} 2>&1`
    if $? != 0
      dlog(c_source)
      raise(CompileError.new(<<-EOS))

Failed to compile c source: status = (#{$?})
---------- Error ----------
#{log}
      EOS
    else
      dlog(c_source, 2)
      dlog(log, 2)
    end
    remove_binary_if_raised do
      File.open(@conffile, 'wb:us-ascii') do |f|
        bug() unless conf
        conf.dump(f)
      end
      File.open(@depfile, 'wb:us-ascii') do |f|
        bug() unless dep.instance_of?(Dependency)
        dep.dump(f)
      end
    end
    bug() unless @version.is_a?(Integer)
    File.open(@versionfile, 'w'){|f| f.write(@version)}
    save_base_configuration(base) if base
  end
end

#compiled_binaryObject



209
210
211
# File 'lib/cast_off/compile/code_manager.rb', line 209

def compiled_binary()
  @dstbin
end

#create_dstdirObject



108
109
110
111
# File 'lib/cast_off/compile/code_manager.rb', line 108

def create_dstdir()
  FileUtils.mkdir(@dstdir) unless File.exist?(@dstdir)
  FileUtils.touch(@lockpath) unless File.exist?(@lockpath)
end

#do_atomicallyObject



197
198
199
200
201
202
203
# File 'lib/cast_off/compile/code_manager.rb', line 197

def do_atomically()
  bug() unless block_given?
  File.open(@lockpath, "w") do |f|
    f.flock(File::LOCK_EX)
    yield
  end
end

#dump_development_mark(conf) ⇒ Object



273
274
275
276
# File 'lib/cast_off/compile/code_manager.rb', line 273

def dump_development_mark(conf)
  bug() unless conf
  FileUtils.touch(@development_mark_path) if conf.development?
end

#fetch_versionObject



122
123
124
125
126
127
128
129
130
# File 'lib/cast_off/compile/code_manager.rb', line 122

def fetch_version()
  bug() unless File.exist?(@dstdir)
  if File.exist?(@versionpath)
    version = File.read(@versionpath)
  else
    version = 0
  end
  version.to_i
end

#gen_header_files(dir) ⇒ Object



349
350
351
352
353
354
355
356
# File 'lib/cast_off/compile/code_manager.rb', line 349

def gen_header_files(dir)
  Dir.chdir(dir) do
    Headers.each_pair do |k, v|
      File.open(k, 'wb:binary'){|f| f.write(v)}
      FileUtils.touch("insns.inc")
    end
  end
end

#gen_makefile(dir, file) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/cast_off/compile/code_manager.rb', line 335

def gen_makefile(dir, file)
  Dir.chdir(dir) do
    File.open('extconf.rb', 'wb:us-ascii') do |f|
      f.write <<-EOS
  require 'mkmf'
  create_makefile("#{File.basename(file, ".c")}")
    EOS
    end
    runruby = CONFIG["prefix"] + "/bin/" + CONFIG["ruby_install_name"] 
    dlog(`#{runruby} extconf.rb`, 2)
    bug("failed to generate extconf.rb: status = (#{$?})") if $? != 0
  end
end

#last_configuration_enabled_development?Boolean

Returns:

  • (Boolean)


269
270
271
# File 'lib/cast_off/compile/code_manager.rb', line 269

def last_configuration_enabled_development?
  File.exist?(@development_mark_path)
end

#load_annotationObject



321
322
323
324
325
326
# File 'lib/cast_off/compile/code_manager.rb', line 321

def load_annotation()
  return nil unless File.exist?(@annotation_path)
  ann_str = File.open(@annotation_path, 'rb:us-ascii').read()
  ann_str.untaint # FIXME
  Marshal.load(ann_str)
end

#load_base_configurationObject



298
299
300
301
302
303
# File 'lib/cast_off/compile/code_manager.rb', line 298

def load_base_configuration()
  return nil unless File.exist?(@base_configuration_path)
  conf_str = File.open(@base_configuration_path, 'rb:us-ascii').read()
  conf_str.untaint # FIXME
  Configuration.load(conf_str)
end

#load_dependencyObject

Raises:

  • (LoadError)


328
329
330
331
332
333
# File 'lib/cast_off/compile/code_manager.rb', line 328

def load_dependency()
  raise(LoadError.new("method dependency file is not exist")) unless File.exist?(@deppath)
  str = File.open(@deppath, 'rb:us-ascii').read()
  str.untaint # FIXME
  Dependency.load(str)
end

#load_last_configurationObject



305
306
307
308
309
310
311
312
# File 'lib/cast_off/compile/code_manager.rb', line 305

def load_last_configuration()
  return nil unless File.exist?("#{@dstdir}/#{@conffile}")
  exist_conf_str = File.open("#{@dstdir}/#{@conffile}", 'rb:us-ascii').read()
  # exist_conf_str が tainted であるため、Marshal.load で読み込んだ class も tainted になってしまう
  # RDoc だと、それのせいで Insecure: can't modify array となる
  exist_conf_str.untaint # FIXME
  Configuration.load(exist_conf_str)
end

#loadableObject



278
279
280
# File 'lib/cast_off/compile/code_manager.rb', line 278

def loadable()
  FileUtils.touch(@loadable_mark_path)
end

#remove_binary_if_raised(&b) ⇒ Object



213
214
215
216
217
218
219
220
# File 'lib/cast_off/compile/code_manager.rb', line 213

def remove_binary_if_raised(&b)
  begin
    b.call()
  rescue => e
    FileUtils.remove_entry_secure(@dstbin) if File.exist?(@dstbin)
    raise(e)
  end
end

#save_annotation(ann) ⇒ Object



314
315
316
317
318
319
# File 'lib/cast_off/compile/code_manager.rb', line 314

def save_annotation(ann)
  bug() unless ann.is_a?(Hash)
  remove_binary_if_raised do
    File.open("#{@annotation_path}", 'wb:us-ascii'){|f| Marshal.dump(ann, f)}
  end
end

#save_base_configuration(conf) ⇒ Object



282
283
284
285
286
287
288
289
290
291
# File 'lib/cast_off/compile/code_manager.rb', line 282

def save_base_configuration(conf)
  begin
    File.open(@base_configuration_path, 'wb:us-ascii') do |f|
      bug() unless conf.instance_of?(Configuration)
      conf.dump(f)
    end
  rescue => e
    vlog("failed to update base configuration: #{e}")
  end
end

#set_pathObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cast_off/compile/code_manager.rb', line 151

def set_path()
  base_sign = CodeManager.generate_signiture("#{@filepath}_#{@line_no}")
  dir = CodeManager.program_dir()
  @dstdir = "#{dir}/#{base_sign}"
  @lockpath = "#{@dstdir}/lock"
  @versionfile = "version"
  @versionpath = "#{@dstdir}/#{@versionfile}"
  create_dstdir()
  @version = fetch_version()
  @signiture = "#{base_sign}_ver#{@version}"
  @dstfile = "#{@signiture}.c"
  @depfile = "#{@signiture}.mdep"
  @deppath = "#{@dstdir}/#{@depfile}"
  @conffile = "#{@signiture}.conf"
  @base_configuration_path = "#{@dstdir}/#{base_sign}.base.conf"
  @annotation_path = "#{@dstdir}/#{@signiture}.ann"
  @development_mark_file = "development"
  @development_mark_path = "#{@dstdir}/#{@development_mark_file}"
  @loadable_mark_path = "#{@dstdir}/loadable"
  @dstbin = "#{@dstdir}/#{@signiture}.#{CONFIG['DLEXT']}"
  @longpath = @base_configuration_path # FIXME
  check_length()
end

#target_file_updated?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/cast_off/compile/code_manager.rb', line 205

def target_file_updated?
  not (File.exist?(@dstbin) && File.mtime(@filepath).tv_sec < File.mtime(@dstbin).tv_sec)
end

#version_upObject



113
114
115
116
117
118
119
120
# File 'lib/cast_off/compile/code_manager.rb', line 113

def version_up()
  v = File.exist?(@versionpath) ? File.read(@versionpath).to_i : @version
  bug() if v < 0
  v = [@version, v].max + 1
  dlog("version: #{@version} => #{v}")
  File.open(@versionpath, 'w'){|f| f.write(v)}
  set_path()
end