class Malachite::Compiler

Public Class Methods

new() click to toggle source
# File lib/malachite/compiler.rb, line 8
def initialize
  @compiled_file = path_to_compiled_file
  @compiled_header = path_to_compiled_header
end

Public Instance Methods

compile() click to toggle source
# File lib/malachite/compiler.rb, line 13
def compile
  return @compiled_file if File.exist?(@compiled_file) && File.exist?(@compiled_header)

  compile!
end

Private Instance Methods

compile!() click to toggle source
# File lib/malachite/compiler.rb, line 21
def compile!
  modify_source_files_in_tmp

  raise Malachite::BuildError, 'Nothing to build, there are no Go files in tmp' if modified_go_files == []

  stdout, stderr, status = Open3.capture3({ 'CGO_ENABLED' => '1' }, 'go', 'build', '-buildmode=c-shared', '-o', @compiled_file, *modified_go_files)
  raise Malachite::BuildError, "Unable to Build Shared Library: #{stdout} #{stderr}" unless status.success?
  raise Malachite::BuildError, 'Unable to Build Header File' unless File.exist?(@compiled_header)
  raise Malachite::BuildError, 'Unable to Build Shared Object' unless File.exist?(@compiled_file)

  path_to_compiled_file
end
main_boilerplate() click to toggle source
# File lib/malachite/compiler.rb, line 53
def main_boilerplate
  File.read(File.expand_path('main.go.tmpl', __dir__))
end
modified_go_files() click to toggle source
# File lib/malachite/compiler.rb, line 45
def modified_go_files
  Dir["#{Rails.root.join('tmp')}/*.go"]
end
modify_source_files_in_tmp() click to toggle source
# File lib/malachite/compiler.rb, line 34
def modify_source_files_in_tmp
  source_files.each do |f|
    Malachite::FileCompiler.new(f).compile
  end

  File.open(Rails.root.join('tmp', 'main.go').to_s, 'w') do |file|
    file.puts main_boilerplate
    file.close
  end
end
path_to_compiled_file() click to toggle source
# File lib/malachite/compiler.rb, line 57
def path_to_compiled_file
  Rails.root.join('tmp', 'malachite.so').to_s
end
path_to_compiled_header() click to toggle source
# File lib/malachite/compiler.rb, line 61
def path_to_compiled_header
  Rails.root.join('tmp', 'malachite.h').to_s
end
source_files() click to toggle source
# File lib/malachite/compiler.rb, line 49
def source_files
  Dir["#{Rails.root.join('app', 'go')}/**/*.go"].reject { |f| f['test'] }
end