Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
95 lines (69 sloc) 1.91 KB
require 'rake/clean'
module Rake
class Task
attr_accessor :task_flags
def task_flags
@task_flags ||= []
@task_flags
end
end
end
### configuration
CFLAGS="-g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG"
PREFIX="/usr/local"
BUILD_ENV={}
### sources
SOURCES=Rake::FileList.new "src/**/*.c","src/*.c"
OBJECTS=SOURCES.ext "o"
TESTS=Rake::FileList.new("tests/*_tests.c").ext ''
PROGRAMS=Rake::FileList.new("bin/*.c").ext ""
### tasks
TARGET=file "build/liblcthw.a"
SO_TARGET=TARGET.name.ext "so"
CLEAN.include(OBJECTS, TESTS, PROGRAMS)
CLEAN.include("tests/tests.log")
CLEAN.include("**/*.dSYM")
CLOBBER.include("build")
directory "build"
task :default => :all
desc "Build everything and run tests."
task :all => [TARGET, SO_TARGET, :tests, *PROGRAMS]
desc "Install library."
task :install => :all do
install_dir = File.join PREFIX, "lib"
execute "install -d #{install_dir}"
execute "install #{TARGET} #{install_dir}"
end
file TARGET => ["build", *OBJECTS] do |t|
execute "ar rcs #{t} #{OBJECTS}"
execute "ranlib #{t}"
end
file SO_TARGET => [TARGET, *OBJECTS] do |t|
execute "cc -shared -o #{t} #{OBJECTS}"
end
TESTS.concat(PROGRAMS).each do |f|
task = file f
task.task_flags = [TARGET]
end
desc "Run tests"
task :tests => [TARGET, *TESTS] do
execute "sh ./tests/runtests.sh"
end
desc "Run tests under valgrind."
task :valgrind do
BUILD_ENV["VALGRIND"] = "valgrind --log-file=/tmp/valgrind-%p.log"
Rake::Task["tests"].invoke
end
### make compatibility rules
rule ".o" => ".c" do |t|
execute "cc #{CFLAGS} -c #{t.source} -o #{t.name}"
end
rule /^((?!\w*\.\w*).)*$/ => ".c" do |t|
deps = t.prerequisites
objects = deps.select { |d| d.pathmap == ".o" }
execute "cc #{CFLAGS} #{t.task_flags.join ' '} #{t.source} #{objects.join ' '} -o #{t.name}"
end
def execute(cmd)
puts(cmd)
system(BUILD_ENV, cmd, out: $stdout)
end