1 # -*- coding: utf-8 -*-
3 require "rake/loaders/makefile"
6 cflags = "-g -O2 -fno-default-inline -Wall -g"
10 ldflags = "-DHAVE_CONFIG_H "
11 ldflags += "-L./test/gtest/gtest-all"
16 # lists of compile dependency objects
17 TOP_DIR = File.expand_path "."
18 SRCS = FileList[File.join(TOP_DIR, "src/**/*.cpp"), File.join(TOP_DIR, "lib/**/*.cpp")]
19 SRCS_ALL = FileList[SRCS, File.join(TOP_DIR, "src/**/*.h"), File.join(TOP_DIR, "lib/**/*.h")]
22 TEST_SRCS = FileList[File.join(TOP_DIR, "test/*.cpp")]
23 TEST_OBJECTS = TEST_SRCS.ext("o")
24 TEST_OBJECTS.include(OBJS)
25 TEST_DEPS = TEST_SRCS.ext("mf")
26 TEST_PROGRAMS = TEST_SRCS.ext("")
30 # lists of clean objects
31 CLEAN.include(OBJS, TEST_OBJECTS, DEPS)
32 CLOBBER.include(TEST_PROGRAMS)
34 task "default" => ["compile"]
35 task "test" => ["compile", "test_execute"]
37 desc "Compile all sources "
38 task "compile" => OBJS do |t|
42 depname = File.split(f)
43 depname[1] = File.basename(depname[1], ".mf") + ".cpp"
44 desc "make #{f} dependencies"
45 file "#{f}" => [File.join(depname)]
48 DEPS.each { |f| import f}
50 rule "_test#{EXEEXT}" => TEST_OBJECTS do |t|
51 base = File.split(t.name);
52 base[1] = File.basename(base[1], EXEEXT) + ".o"
53 sh "#{CC} -o #{File.expand_path(t.name)} #{ldflags} #{cflags} #{includes} #{OBJS.join(' ')} ./test/gtest/gtest-all.o #{File.join(base)} "
56 rule '.o' => '.cpp' do |t|
57 depname = File.split(t.source)
58 depname[1] = File.basename(depname[1], ".cpp") + ".mf"
59 sh "#{CC} #{ldflags} #{cflags} #{includes} -c #{t.source} -o #{t.name}"
62 rule '.mf' => '.cpp' do |t|
63 depname = File.split(t.source)
64 depname[1] = File.basename(depname[1], ".cpp") + ".mf"
65 sh "#{CC} -MM -MF #{File.join(depname[0], depname[1])} #{ldflags} #{cflags} #{includes} #{t.source}"
66 import File.join(depname[0], depname[1])
69 desc "run all test program in `TEST_PROGRAMS`"
70 task :test_execute => TEST_PROGRAMS do |t|
71 t.prerequisites.each { |f|
72 sh "#{f} --gtest_color=yes"
76 # Makefile形式のデータを、複数行から一行形式に変換します。
77 def replace_make_to_rake(make_base_file, to_file)
78 file_data = File.read make_base_file
80 file_data = file_data.gsub(/\r\n|\r|\n/, "\n")
81 file_data = file_data.gsub(/\\\s+/, " ")
82 file_data = file_data.split "\n"
83 open(to_file, "a") do |d|
84 (file_data.select {|f| !f.empty? }).each { |x| d << x << "\n"}