Rakefiles

Jul 1, 2013 at 19:00

I thought I would play around with CLang 3.2 on Kubuntu, and needed a quick way to build my code without going through the 1970/80/90s pain of messing around with makefiles and all the associated tab-space nonsense. Rakefiles seemed a good fit.

See this link for the original code and explanation http://nserror.me/2012/01/rake-your-objective-c-life-easier/

require 'rake'
require 'rake/task'
require 'rake/clean'

# constants

COMPILER = "clang++"
LINKER = "clang++"
COMPILER_FLAGS = ['-std=c++11'].join(' ')

PRODUCTS = {
    'my_exe' => 'main.cpp'
}

SRC = FileList['**/*.cpp']
OBJ = SRC.ext('.o')
LINKER_FLAGS = [''].join(' ')


rule '.o' => ['.cpp'] do |t|
    #  -c : Only run preprocess, compile, and assemble steps
    sh "#{COMPILER} #{COMPILER_FLAGS} -c -o #{t.name} #{t.source}"
end

PRODUCTS.each do |product, source|
    object_files = OBJ - (PRODUCTS.values - [source]).map{ |f| f.ext('.o') }
    desc 'Build exe for ' + product
    file product => object_files do |t|
        sh "#{LINKER} #{LINKER_FLAGS} #{object_files} -o #{t.name}"
    end
end

task :default => PRODUCTS.first

CLEAN.include("**/*.o")
CLOBBER.include(PRODUCTS.keys)