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)

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.



95
96
97
98
99
100
# File 'lib/cast_off/compile/code_manager.rb', line 95

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)


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

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

.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

.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)


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

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



244
245
246
247
# File 'lib/cast_off/compile/code_manager.rb', line 244

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



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

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

#compile_c_source(c_source, conf, dep) ⇒ Object



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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/cast_off/compile/code_manager.rb', line 173

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



160
161
162
# File 'lib/cast_off/compile/code_manager.rb', line 160

def compiled_binary()
  @dstbin
end

#create_dstdirObject



59
60
61
62
# File 'lib/cast_off/compile/code_manager.rb', line 59

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

#do_atomicallyObject



148
149
150
151
152
153
154
# File 'lib/cast_off/compile/code_manager.rb', line 148

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



224
225
226
227
# File 'lib/cast_off/compile/code_manager.rb', line 224

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

#fetch_versionObject



73
74
75
76
77
78
79
80
81
# File 'lib/cast_off/compile/code_manager.rb', line 73

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



300
301
302
303
304
305
306
307
# File 'lib/cast_off/compile/code_manager.rb', line 300

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



286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/cast_off/compile/code_manager.rb', line 286

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)


220
221
222
# File 'lib/cast_off/compile/code_manager.rb', line 220

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

#load_annotationObject



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

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



249
250
251
252
253
254
# File 'lib/cast_off/compile/code_manager.rb', line 249

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)


279
280
281
282
283
284
# File 'lib/cast_off/compile/code_manager.rb', line 279

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



256
257
258
259
260
261
262
263
# File 'lib/cast_off/compile/code_manager.rb', line 256

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



229
230
231
# File 'lib/cast_off/compile/code_manager.rb', line 229

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

#remove_binary_if_raised(&b) ⇒ Object



164
165
166
167
168
169
170
171
# File 'lib/cast_off/compile/code_manager.rb', line 164

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



265
266
267
268
269
270
# File 'lib/cast_off/compile/code_manager.rb', line 265

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



233
234
235
236
237
238
239
240
241
242
# File 'lib/cast_off/compile/code_manager.rb', line 233

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cast_off/compile/code_manager.rb', line 102

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)


156
157
158
# File 'lib/cast_off/compile/code_manager.rb', line 156

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

#version_upObject



64
65
66
67
68
69
70
71
# File 'lib/cast_off/compile/code_manager.rb', line 64

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