Skip to content

Commit d63dc15

Browse files
committed
add rake rubex:clobber task. Add tests for checking sanity with external C structs
1 parent 18abe1c commit d63dc15

File tree

5 files changed

+111
-16
lines changed

5 files changed

+111
-16
lines changed

lib/rubex/compiler.rb

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class << self
2727
# programs.
2828
# @param files [Array[String]] nil An Array specifying the file names to
2929
# compile w.r.t the source_dir directory.
30+
# @param invoker [Symbol] nil Determines whether the invoker is a command line
31+
# program or a rake task. Affect build directory for rake task.
3032
#
3133
# TODO: The path can be relative to the source_dir if source_dir is specified.
3234
def compile path,
@@ -37,7 +39,8 @@ def compile path,
3739
make: false,
3840
debug: false,
3941
source_dir: nil,
40-
files: nil
42+
files: nil,
43+
invoker: nil
4144
CONFIG.flush
4245
CONFIG.debug = debug
4346
CONFIG.add_link "m" # link cmath libraries
@@ -54,8 +57,9 @@ def compile path,
5457
elsif test && multi_file
5558
return [tree, supervisor, ext]
5659
end
57-
write_files target_name, supervisor, ext, target_dir: target_dir, force: force
58-
full_path = build_path(target_dir, target_name)
60+
write_files target_name, supervisor, ext, target_dir: target_dir, force: force,
61+
invoker: invoker
62+
full_path = build_path(target_dir, target_name, invoker: invoker)
5963
load_extconf full_path
6064
run_make full_path if make
6165
end
@@ -120,10 +124,12 @@ def extract_target_name path
120124
# @param directory [String] nil Target directory in which files are to be placed.
121125
# @param force [Boolean] false Recreate the target directory and rewrite the
122126
# files whether they are already present or not.
123-
def write_files target_name, supervisor, ext, target_dir: nil, force: false
124-
path = build_path(target_dir, target_name)
127+
# @param invoker [Symbol|NilClass] nil Determines the build directory based on whether
128+
# it being invoked by a rake task or cmd.
129+
def write_files target_name, supervisor, ext, target_dir: nil, force: false, invoker: nil
130+
path = build_path(target_dir, target_name, invoker: invoker)
125131
FileUtils.rm_rf(path) if force && Dir.exist?(path)
126-
Dir.mkdir(path) unless Dir.exist?(path)
132+
FileUtils.mkdir_p(path) unless Dir.exist?(path)
127133

128134
write_to_file "#{path}/#{Rubex::COMMON_UTILS_FILE}.h",
129135
supervisor.header(Rubex::COMMON_UTILS_FILE).to_s
@@ -147,10 +153,19 @@ def run_make path
147153
end
148154
end
149155

150-
def build_path directory, target_name
156+
# Create build path for outputting .c and .h files. Will check whether the calling
157+
# process is a rake task or cmd.
158+
#
159+
# @param directory [String] The directory inside which this build path will exist.
160+
# @param target_name [String] Name of the folder inside the directory.
161+
# @param invoker [Symbol|NilClass] nil The calling program. Can be :rake or :cmd.
162+
def build_path directory, target_name, invoker: nil
151163
directory = (directory ? directory.to_s : Dir.pwd)
152164
unless directory.end_with?(target_name)
153-
directory += "/#{target_name}"
165+
directory += "/#{target_name}"
166+
if invoker == :rake
167+
directory += "/build"
168+
end
154169
end
155170
directory
156171
end

lib/rubex/rake_task.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,19 @@ def define_compile_tasks
4141
desc "Compile a Rubex file into a shared object."
4242
task :compile do
4343
file_name = "#{@ext_dir}/#{@name}#{@source_pattern[1..-1]}"
44-
Rubex::Compiler.compile file_name, target_dir: "#{@ext_dir}"
44+
Rubex::Compiler.compile file_name, target_dir: "#{@ext_dir}", invoker: :rake
45+
end
46+
47+
desc "Delete all generated files inside build/ folder."
48+
task :clobber do
49+
path = "#{@ext_dir}/#{@name}/build"
50+
Dir.chdir(path) do
51+
FileUtils.rm(
52+
Dir.glob(
53+
"#{path}/*.{c,h,so,o,bundle,dll}") + ["Makefile", "extconf.rb"], force: true
54+
)
55+
end
56+
FileUtils.rmdir(path)
4557
end
4658
end
4759
Rake::ExtensionTask.new(@name)

spec/external_c_struct_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'spec_helper'
2+
3+
describe Rubex do
4+
test_case = 'external_c_struct'
5+
6+
context "Case: #{test_case}" do
7+
before do
8+
@path = path_str test_case
9+
end
10+
11+
context ".ast" do
12+
it "generates the AST" do
13+
t = Rubex::Compiler.ast(@path + '.rubex')
14+
end
15+
end
16+
17+
context ".compile" do
18+
it "compiles to valid C file" do
19+
t,c,e = Rubex::Compiler.compile(@path + '.rubex', test: true)
20+
end
21+
end
22+
23+
context "Black Box testing" do
24+
it "compiles and checks for valid output" do
25+
setup_and_teardown_compiled_files(test_case) do |dir|
26+
require_relative "#{dir}/#{test_case}.#{os_extension}"
27+
28+
expect(HelloFile.new.foo).to eq(false)
29+
end
30+
end
31+
end
32+
end
33+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
lib "<stdio.h>"
2+
struct FILE
3+
end
4+
5+
alias FILE = struct FILE
6+
end
7+
8+
struct hello
9+
FILE f
10+
end
11+
12+
class HelloFile
13+
def foo
14+
FILE a
15+
end
16+
end

spec/rake_task_spec.rb

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
end
3333
end
3434

35-
describe "rake" do
35+
describe "rake", hell: true do
3636
context "rubex:compile" do
3737
before do
3838
Rake::Task.clear
@@ -46,18 +46,18 @@
4646
end
4747
Rake::Task["rubex:compile"].invoke
4848

49-
expect(File.exist?("#{ext_path}/#{name}/#{name}.c")).to eq(true)
50-
expect(File.exist?("#{ext_path}/#{name}/extconf.rb")).to eq(true)
49+
build_path = "#{ext_path}/#{name}/build"
50+
expect(File.exist?("#{build_path}/#{name}.c")).to eq(true)
51+
expect(File.exist?("#{build_path}/extconf.rb")).to eq(true)
5152

5253
# delete generated files
53-
dir = "#{ext_path}/#{name}"
54-
Dir.chdir(dir) do
54+
Dir.chdir(build_path) do
5555
FileUtils.rm(
5656
Dir.glob(
57-
"#{dir}/*.{c,h,so,o,bundle,dll}") + ["Makefile", "extconf.rb"], force: true
57+
"#{build_path}/*.{c,h,so,o,bundle,dll}") + ["Makefile", "extconf.rb"], force: true
5858
)
5959
end
60-
FileUtils.rmdir(dir)
60+
FileUtils.rmdir(build_path)
6161
end
6262

6363
it "compiles a multiple file program" do
@@ -69,6 +69,25 @@
6969
end
7070
end
7171

72+
context "rake:clobber" do
73+
before do
74+
Rake::Task.clear
75+
end
76+
77+
it "clobbers generated files inside build/ directory." do
78+
ext_path = "#{Dir.pwd}/spec/fixtures/rake_task/single_file"
79+
name = "test"
80+
Rubex::RakeTask.new(name) do
81+
ext ext_path
82+
end
83+
Rake::Task["rubex:compile"].invoke
84+
Rake::Task["rubex:clobber"].invoke
85+
86+
expect(!File.exist?("#{ext_path}/#{name}/build/#{name}.c")).to eq(true)
87+
expect(!File.exist?("#{ext_path}/#{name}/build/extconf.rb")).to eq(true)
88+
end
89+
end
90+
7291
context "rake:compile:install" do
7392
before do
7493
Rake::Task.clear

0 commit comments

Comments
 (0)