# -*- mode: ruby; coding: utf-8 -*- require 'rbconfig' task :ext => 'ext:build' %w[clean].each do |t| task t.intern => "ext:#{t}" end namespace :ext do rbconf = RbConfig::CONFIG mod = MODULE modname = env :MODNAME, mod.name.downcase dlname = env :DLNAME, 'native' dlext = env :DLEXT, rbconf['DLEXT'] || 'so' extdir = env :EXTDIR, "ext/#{modname}" libdir = env :LIBDIR, "lib/#{modname}" ruby = env :RUBY, 'ruby' make = env :MAKE, 'make' cxx = env :CXX, rbconf['CXX'] || 'g++' defs = env :DEFS, [] debug = env :DEBUG, false incdirs = env :INCDIRS, [] cxxflags = env(:CXXFLAGS, rbconf['CXXFLAGS']).dup outname = "#{dlname}.#{dlext}" extout = File.join extdir, outname libout = File.join libdir, outname srcs = FileList["#{extdir}/**/*.cpp"] incroot = rbconf['rubyhdrdir'] incdirs += [ incroot, "#{incroot}/#{RUBY_PLATFORM}", '/opt/local/include', '/opt/include' ] incdirs.uniq! defs << '_DEBUG' if debug defs << 'NDEBUG' unless debug defs << 'WIN32' if win32? defs << 'COCOA' if cocoa? defs << $~[0].upcase if RUBY_PLATFORM =~ /mswin|ming|cygwin|darwin/i cxxflags << ' ' << rbconf['debugflags'] if debug cxxflags << ' --stdlib=libc++' if clang? cxxflags << defs.map{|s| " -D#{s}"}.join cxxflags << incdirs.map{|s| " -I#{s}"}.join extconf = File.join extdir, 'extconf.rb' makefile = File.join extdir, 'Makefile' depend = File.join extdir, 'depend.mf' task :build => libout task :clean do sh %( cd #{extdir} && #{make} clean ) if File.exist? makefile sh %( rm -rf #{makefile} #{depend} #{libout} ) end file libout => extout do sh %( cp #{extout} #{libout} ) end file extout => [:lib, makefile] do sh %( cd #{extdir} && #{make} ) end file makefile => [extconf, depend] do sh %( cd #{extdir} && #{ruby} #{File.basename extconf} ) end file depend => srcs do inc = incdirs.map{|s| " -I#{s}"}.join src = srcs.map{|cpp| File.basename cpp}.join ' ' dep = File.basename depend sh %( cd #{extdir} && #{cxx} -M #{cxxflags} #{inc} #{src} > #{dep} ) end end# :ext