Skip to content

Commit

Permalink
Outputting to mutliple files.
Browse files Browse the repository at this point in the history
  • Loading branch information
v0dro committed Apr 30, 2018
1 parent c7f15d1 commit beeec5e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/rubex/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def generate(file)
make: options[:install], debug: options[:debug]
end

desc 'install PATH', 'run "make" utility to generate a shared object file required for C extensions'
desc 'install PATH',
'run "make" utility to generate a shared object file required for C extensions'
def install(path)
Rubex::Compiler.run_make path
end
Expand Down
19 changes: 14 additions & 5 deletions lib/rubex/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ def compile path,
debug: false,
source_dir: nil,
files: nil
tree = ast path, source_dir: source_dir, test: test
target_name = extract_target_name path
supervisor = generate_code tree, target_name
ext = extconf target_name, target_dir: target_dir
CONFIG.flush
CONFIG.debug = debug
CONFIG.add_link "m" # link cmath libraries

target_name = extract_target_name path
CONFIG.add_dep target_name

tree = ast path, source_dir: source_dir, test: test
supervisor = generate_code tree, target_name
ext = extconf target_name, target_dir: target_dir

if test && !multi_file
return [tree, supervisor.code(target_name), ext,
supervisor.header(target_name)]
Expand Down Expand Up @@ -86,6 +88,13 @@ def extconf target_name, target_dir: nil
extconf << "require 'mkmf'\n"
extconf << "$libs += \" #{CONFIG.link_flags}\"\n"
extconf << "$CFLAGS += \" -g \"\n" if CONFIG.debug

srcs_config = CONFIG.srcs.map { |s| "\"#{s}\""}.join(',')
extconf << "$srcs = [#{srcs_config}]\n"

objs_config = CONFIG.objs.map { |o| "\"#{o}\""}.join(',')
extconf << "$objs = [#{objs_config}]\n"

extconf << "create_makefile('#{path}/#{target_name}')\n"
extconf
end
Expand Down
12 changes: 11 additions & 1 deletion lib/rubex/compiler_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ module Rubex
# the generated C code. This includes file names, compiler flags and other
# options required by Rubex.
class CompilerConfig
attr_accessor :debug
attr_accessor :debug, :srcs, :objs

def initialize
@links = []
@srcs = []
@objs = []
end

def add_link link_str
Expand All @@ -19,6 +21,14 @@ def link_flags

def flush
@links = []
@srcs = []
@objs = []
end

# add dependency on file so extconf will recognise it.
def add_dep(file_name)
@srcs << "#{file_name}.c"
@objs << "#{file_name}.o"
end
end
end
1 change: 1 addition & 0 deletions lib/rubex/parser.racc
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,7 @@ def parse_required_file_and_load_ast file_name
raise Rubex::FileNotFoundError,
"cannot find file #{file_name} in #{@source_dir}"
end
Rubex::Compiler::CONFIG.add_dep(File.basename(file_name).split('.')[0])
parser = Rubex::Parser.new
parser.parse(full_name, @source_dir, true)
file_node_ast = parser.do_parse
Expand Down
1 change: 1 addition & 0 deletions lib/rubex/parser.racc.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions spec/multi_file_programs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
context "Case: #{test_case}" do
before do
@dir = dir_str test_case
@t_dir = @dir + "/#{test_case}"
@main_file = path_str test_case
@file_names = ["a.rubex", "b.rubex", "multi_file_programs.rubex"]
end
Expand All @@ -19,13 +20,16 @@
context ".compile", hell: true do
it "compiles to valid C file" do
t,c,e = Rubex::Compiler.compile(@main_file + '.rubex', files: @file_names,
source_dir: @dir, test: true, multi_file: true)
source_dir: @dir, test: true, multi_file: true,
target_dir: @t_dir)
end
end

context "black box testing", hell: true do
it "compiles and checks for valid output" do
setup_and_teardown_multiple_compiled_files(@main_file + '.rubex', @dir, @file_names) do |dir|
setup_and_teardown_multiple_compiled_files(
@main_file + '.rubex', @dir, @t_dir, @file_names) do |dir|

require_relative "#{dir}/#{test_case}.#{os_extension}"
expect(C.new("hello ruby").bar).to eq("hello ruby")
expect(D.new("ruby ").foo).to eq("ruby hello world")
Expand Down
10 changes: 5 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ def setup_and_teardown_compiled_files test_case, example=nil, &block
end
end

def setup_and_teardown_multiple_compiled_files test_case, source_dir, files, &block
def setup_and_teardown_multiple_compiled_files test_case, source_dir, t_dir, files, &block
Rubex::Compiler.compile(test_case, source_dir: source_dir, files: files,
target_dir: source_dir)
target_dir: t_dir)
begin
block.call(source_dir)
block.call(t_dir)
ensure
Dir.chdir(source_dir) do
Dir.chdir(t_dir) do
FileUtils.rm(
Dir.glob("#{source_dir}/*.{c,h,so,o,bundle,dll}") + ["Makefile", "extconf.rb"], force: true)
Dir.glob("#{t_dir}/*.{c,h,so,o,bundle,dll}") + ["Makefile", "extconf.rb"], force: true)
end
end
end
Expand Down

0 comments on commit beeec5e

Please sign in to comment.