概要
C言語やC++で開発した実行ファイルやライブラリファイルのビルドに
毎回 Makefile を手動で生成したり、修正するのが面倒臭くなったので、
Ruby でコンパイルオプションを可変に定義した Makefile を自動生成したり、
ビルドコマンドをカスタマイズできるようにしてみた。
moon_rabbit のインストール
gem install moon_rabbit
単純な使い方
main.c
#include <stdio.h>
int main( void ){
printf( "main\n" );
return 0;
}
Makefile.rb
require "moon_rabbit"
include MoonRabbit
Makefile.new{
path "Makefile"
compiler "gcc"
main_target "main"
src "main.c"
compile_option "-g -Wall -O2"
# Makefile を生成
output
}
main.c と Makefile.rb を用意し、
ruby Makefile.rb
とするだけで、
main をビルドするためのMakefileが生成され、
make
で main が生成される。
Rakefile との組み合わせ
Rakefile と組み合わせることで、
複数の Makefile の生成やビルドが容易になる。
libtest.c
#include <stdio.h>
#include "libtest.h"
int add( int value1, int value2 ){
return value1 + value2;
}
libtest.h
int add( int value1, int value2 );
main.c
#include <stdio.h>
#include "libtest.h"
int main(){
printf( "1 + 2 = %d\n", add( 1, 2 ) );
return 0;
}
Makefiles.rb
require "moon_rabbit"
include MoonRabbit
common_makefile = Makefile.new{
compiler "gcc"
obj_dir "obj"
compile_option "-g -Wall -O2"
}
Makefiles.add Makefile.new{
add common_makefile
path "Makefile.libtest"
main_target "libtest.a"
src "libtest.c"
}
Makefiles.add Makefile.new{
add common_makefile
path "Makefile.main"
main_target "main"
src "main.c"
static_lib "libtest.a"
}
Rakefile
require "moon_rabbit"
require "moon_rabbit/Rakefile"
require "./Makefiles"
rake output
rake build