diff --git a/.hgignore b/.hgignore index 9cec915..1b4b5a4 100644 --- a/.hgignore +++ b/.hgignore @@ -1,3 +1,5 @@ .o$ ~$ ^flexnbd$ +^build/ +^pkg/ diff --git a/Rakefile b/Rakefile index a8d47cc..89f9cf1 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,8 @@ DEBUG = true -SOURCES = %w( flexnbd ioutil readwrite serve util parse control remote ) -OBJECTS = SOURCES.map { |s| "#{s}.o" } +SOURCES = FileList['src/*.c'] +OBJECTS = SOURCES.pathmap( "%{^src,build}X.o" ) + LIBS = %w( pthread ) CCFLAGS = %w( -Wall ) LDFLAGS = [] @@ -15,52 +16,66 @@ if DEBUG end desc "Build flexnbd binary" -rule 'default' => 'flexnbd' +task :flexnbd => 'build/flexnbd' namespace "test" do desc "Run all tests" task 'run' => ["unit", "scenarios"] - + desc "Build C tests" - task 'build' => TEST_MODULES.map { |n| "tests/check_#{n}" } - + task 'build' => TEST_MODULES.map { |n| "build/tests/check_#{n}" } + desc "Run C tests" task 'unit' => 'build' do TEST_MODULES.each do |n| ENV['EF_DISABLE_BANNER'] = '1' - sh "./tests/check_#{n}" + sh "build/tests/check_#{n}" end end - + desc "Run NBD test scenarios" task 'scenarios' => 'flexnbd' do sh "cd tests; ruby nbd_scenarios" end end + def gcc_link(target, objects) + FileUtils.mkdir_p File.dirname( target ) + sh "gcc #{LDFLAGS.join(' ')} "+ LIBS.map { |l| "-l#{l}" }.join(" ")+ + " -I src" + " -o #{target} "+ objects.join(" ") end -rule 'flexnbd' => OBJECTS do |t| +rule 'build/flexnbd' => OBJECTS do |t| gcc_link(t.name, t.sources) end -rule(/tests\/check_[a-z]+$/ => [ proc { |target| [target+".o", "util.o"] } ]) do |t| - gcc_link(t.name, t.sources + [LIBCHECK]) +TEST_MODULES.each do |m| + deps = ["tests/check_#{m}.c", "build/util.o"] + maybe_obj_name = "build/#{m}.o" + + deps << maybe_obj_name if OBJECTS.include?( maybe_obj_name ) + + file "build/tests/check_#{m}" => deps do |t| + gcc_link(t.name, deps + [LIBCHECK]) + end end -rule '.o' => '.c' do |t| - sh "gcc -I. -c #{CCFLAGS.join(' ')} -o #{t.name} #{t.source} " + +OBJECTS.zip( SOURCES ).each do |o,c| + file o => c do |t| + FileUtils.mkdir_p File.dirname( o ) + sh "gcc -Isrc -c #{CCFLAGS.join(' ')} -o #{o} #{c} " + end end desc "Remove all build targets, binaries and temporary files" rule 'clean' do - sh "rm -f *~ flexnbd " + ( - OBJECTS + + sh "rm -rf *~ build " + ( TEST_MODULES.map { |n| ["tests/check_#{n}", "tests/check_#{n}.o"] }.flatten ). join(" ") diff --git a/bitset.h b/src/bitset.h similarity index 100% rename from bitset.h rename to src/bitset.h diff --git a/control.c b/src/control.c similarity index 100% rename from control.c rename to src/control.c diff --git a/control.h b/src/control.h similarity index 100% rename from control.h rename to src/control.h diff --git a/flexnbd.c b/src/flexnbd.c similarity index 100% rename from flexnbd.c rename to src/flexnbd.c diff --git a/ioutil.c b/src/ioutil.c similarity index 100% rename from ioutil.c rename to src/ioutil.c diff --git a/ioutil.h b/src/ioutil.h similarity index 100% rename from ioutil.h rename to src/ioutil.h diff --git a/nbdtypes.h b/src/nbdtypes.h similarity index 100% rename from nbdtypes.h rename to src/nbdtypes.h diff --git a/params.h b/src/params.h similarity index 100% rename from params.h rename to src/params.h diff --git a/parse.c b/src/parse.c similarity index 100% rename from parse.c rename to src/parse.c diff --git a/parse.h b/src/parse.h similarity index 100% rename from parse.h rename to src/parse.h diff --git a/readwrite.c b/src/readwrite.c similarity index 100% rename from readwrite.c rename to src/readwrite.c diff --git a/readwrite.h b/src/readwrite.h similarity index 100% rename from readwrite.h rename to src/readwrite.h diff --git a/remote.c b/src/remote.c similarity index 100% rename from remote.c rename to src/remote.c diff --git a/serve.c b/src/serve.c similarity index 100% rename from serve.c rename to src/serve.c diff --git a/util.c b/src/util.c similarity index 100% rename from util.c rename to src/util.c diff --git a/util.h b/src/util.h similarity index 100% rename from util.h rename to src/util.h diff --git a/tests/nbd_scenarios b/tests/nbd_scenarios index faa4767..7392112 100644 --- a/tests/nbd_scenarios +++ b/tests/nbd_scenarios @@ -13,39 +13,39 @@ class NBDScenarios < Test::Unit::TestCase @available_ports = [*40000..41000] - listening_ports @port1 = @available_ports.shift @port2 = @available_ports.shift - @nbd1 = FlexNBD.new("../flexnbd", @ip, @port1) - end - + @nbd1 = FlexNBD.new("../build/flexnbd", @ip, @port1) + end + def teardown @nbd1.kill rescue nil [@filename1, @filename2].each do |f| File.unlink(f) if File.exists?(f) end end - + def test_read1 writefile1("f"*64) serve1 - + [0, 12, 63].each do |num| - + assert_equal( - @nbd1.read(num*@blocksize, @blocksize), + @nbd1.read(num*@blocksize, @blocksize), @file1.read(num*@blocksize, @blocksize) ) end - + [124, 1200, 10028, 25488].each do |num| assert_equal(@nbd1.read(num, 4), @file1.read(num, 4)) end end - - # Check that we're not + + # Check that we're not # def test_writeread1 writefile1("0"*64) serve1 - + [0, 12, 63].each do |num| data = "X"*@blocksize @nbd1.write(num*@blocksize, data) @@ -53,14 +53,14 @@ class NBDScenarios < Test::Unit::TestCase assert_equal(data, @nbd1.read(num*@blocksize, data.size)) end end - + # Check that we're not overstepping or understepping where our writes end # up. # def test_writeread2 writefile1("0"*1024) serve1 - + d0 = "\0"*@blocksize d1 = "X"*@blocksize (0..63).each do |num| @@ -70,16 +70,16 @@ class NBDScenarios < Test::Unit::TestCase assert_equal(d0, @nbd1.read(((2*num)+1)*@blocksize, d0.size)) end end - + protected def serve1(*acl) @nbd1.serve(@ip, @port1, @filename1, *acl) end - + def writefile1(data) @file1 = TestFileWriter.new(@filename1, @blocksize).write(data) end - + def listening_ports `netstat -ltn`. split("\n").