# -*- coding: utf-8 -*- require "rake/clean" require "rake/loaders/makefile" CC = "g++" cflags = "-g -O2 -fno-default-inline -Wall -g" TOPDIR = "." ldflags = "-DHAVE_CONFIG_H " ldflags += "-L./test/gtest/gtest-all" includes = " " includes += "-I. -I." # lists of compile dependency objects TOP_DIR = File.expand_path "." SRCS = FileList[File.join(TOP_DIR, "src/**/*.cpp"), File.join(TOP_DIR, "lib/**/*.cpp")] SRCS_ALL = FileList[SRCS, File.join(TOP_DIR, "src/**/*.h"), File.join(TOP_DIR, "lib/**/*.h")] OBJS = SRCS.ext("o") DEPS = SRCS.ext("mf") TEST_SRCS = FileList[File.join(TOP_DIR, "test/*.cpp")] TEST_OBJECTS = TEST_SRCS.ext("o") TEST_OBJECTS.include(OBJS) TEST_DEPS = TEST_SRCS.ext("mf") TEST_PROGRAMS = TEST_SRCS.ext("") EXEEXT = "" # lists of clean objects CLEAN.include(OBJS, TEST_OBJECTS, DEPS) CLOBBER.include(TEST_PROGRAMS) task "default" => ["compile"] task "test" => ["compile", "test_execute"] desc "Compile all sources " task "compile" => OBJS do |t| end DEPS.each { |f| depname = File.split(f) depname[1] = File.basename(depname[1], ".mf") + ".cpp" desc "make #{f} dependencies" file "#{f}" => [File.join(depname)] } DEPS.each { |f| import f} rule "_test#{EXEEXT}" => TEST_OBJECTS do |t| base = File.split(t.name); base[1] = File.basename(base[1], EXEEXT) + ".o" sh "#{CC} -o #{File.expand_path(t.name)} #{ldflags} #{cflags} #{includes} #{OBJS.join(' ')} ./test/gtest/gtest-all.o #{File.join(base)} " end rule '.o' => '.cpp' do |t| depname = File.split(t.source) depname[1] = File.basename(depname[1], ".cpp") + ".mf" sh "#{CC} #{ldflags} #{cflags} #{includes} -c #{t.source} -o #{t.name}" end rule '.mf' => '.cpp' do |t| depname = File.split(t.source) depname[1] = File.basename(depname[1], ".cpp") + ".mf" sh "#{CC} -MM -MF #{File.join(depname[0], depname[1])} #{ldflags} #{cflags} #{includes} #{t.source}" import File.join(depname[0], depname[1]) end desc "run all test program in `TEST_PROGRAMS`" task :test_execute => TEST_PROGRAMS do |t| t.prerequisites.each { |f| sh "#{f} --gtest_color=yes" } end # Makefile形式のデータを、複数行から一行形式に変換します。 def replace_make_to_rake(make_base_file, to_file) file_data = File.read make_base_file file_data = file_data.gsub(/\r\n|\r|\n/, "\n") file_data = file_data.gsub(/\\\s+/, " ") file_data = file_data.split "\n" open(to_file, "a") do |d| (file_data.select {|f| !f.empty? }).each { |x| d << x << "\n"} end end