Class: Exefy::Generator

Inherits:
Object
  • Object
show all
Includes:
Gem::UserInteraction
Defined in:
lib/exefy.rb

Direct Known Subclasses

GeneratorFromBatch

Instance Method Summary collapse

Constructor Details

#initialize(target_path) ⇒ Generator

Returns a new instance of Generator.



35
36
37
# File 'lib/exefy.rb', line 35

def initialize(target_path)
  @exe_target = target_path
end

Instance Method Details

#compile(source, target) ⇒ Object



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

def compile(source, target)
  cflags = RbConfig::CONFIG["CFLAGS"]
  cppflags = RbConfig::CONFIG["CPPFLAGS"]

  hdr_dir = RbConfig::CONFIG["rubyhdrdir"] || RbConfig::CONFIG["includedir"]
  arch_dir = RbConfig::CONFIG["arch"]

  include_dirs = "-I#{hdr_dir}/#{arch_dir} -I#{hdr_dir}"

  cc = ENV.fetch("CC", RbConfig::CONFIG["CC"])

  system "#{cc} -c #{source} -o #{target} #{cflags} #{cppflags} #{include_dirs}"
end

#compile_resources(template, source, target) ⇒ Object



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

def compile_resources(template, source, target)
  binary_version = VERSION.gsub('.', ',') + ",0"
  File.open source, "w" do |file|
    erb = ERB.new File.read(template)
    file.puts erb.result(binding)
  end

  system "windres #{source} -o #{target}"
end

#exefy_gemObject



39
40
41
# File 'lib/exefy.rb', line 39

def exefy_gem
  process
end

#generate_executableObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/exefy.rb', line 56

def generate_executable
  log_message "Generating executable '#{Exefy.executable}'..."

  gem_root = File.expand_path("../..", __FILE__)
  template = File.join(gem_root, "templates", "gem_exe.c")
  res_template = File.join(gem_root, "templates", "gem_exe.rc.erb")

  Dir.mktmpdir do |build_dir|
    base = File.basename(template)
    res_base = File.basename(res_template)

    obj  = File.join(build_dir, base.gsub(".c", ".o"))
    res_src = File.join(build_dir, res_base.gsub(".erb", ""))
    res_obj = File.join(build_dir, res_base.gsub(".erb", ".o"))
    exe  = File.join(build_dir, base.gsub(".c", ".exe"))

    compile(template, obj)
    compile_resources(res_template, res_src, res_obj)
    link([obj, res_obj].join(" "), exe)

    # verify target directory first exists
    FileUtils.mkdir_p File.dirname(Exefy.executable)

    FileUtils.install exe, Exefy.executable
  end
end

#install_executable_stub(target) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/exefy.rb', line 47

def install_executable_stub(target)
  unless File.executable?(Exefy.executable)
    generate_executable
  end

  log_message "Creating executable as '#{File.basename(target)}'"
  FileUtils.install Exefy.executable, target
end


107
108
109
110
111
112
113
114
115
116
117
# File 'lib/exefy.rb', line 107

def link(objs, target)
  libruby_dir = RbConfig::CONFIG["libdir"]
  libruby = RbConfig::CONFIG["LIBRUBYARG"]

  libs = RbConfig::CONFIG["LIBS"]
  libs_dir = "-L#{libruby_dir} #{libruby} #{libs}"

  cc = ENV.fetch("CC", RbConfig::CONFIG["CC"])
  system "#{cc} #{objs} -o #{target} #{libs_dir}"
  system "strip #{target}"
end

#log_message(message) ⇒ Object



119
120
121
# File 'lib/exefy.rb', line 119

def log_message(message)
  say message if Gem.configuration.really_verbose
end

#processObject



43
44
45
# File 'lib/exefy.rb', line 43

def process
  install_executable_stub(@exe_target)
end