[CHANGE] build ruby bindings per architecture; useful for Ohloh since we run differen...
[ohcount] / build
1 #!/bin/bash
2 # Build script for Ohcount.
3 # Written by Mitchell Foral. mitchell<att>caladbolg.net.
4
5 # Options
6 # Change these for your system configuration.
7 if [ `uname` != "Darwin" ]
8 then
9   # Linux
10   INC_DIR=
11   LIB_DIR=
12   # You shouldn't have to change the following.
13   CFLAGS=
14   WARN="-Wall -Wno-pointer-to-int-cast -Wno-parentheses"
15   SHARED=-shared
16   SHARED_NAME=libohcount.so
17   RB_SHARED=-shared
18   RB_SHARED_NAME=ohcount.so
19 else
20   # Mac OSX
21   INC_DIR=/opt/local/include
22   LIB_DIR=/opt/local/lib
23   # You shouldn't have to change the following.
24   CFLAGS=-fno-common
25   WARN="-Wall -Wno-parentheses -O3 -Oz"
26   SHARED="-dynamiclib -L$LIB_DIR -lpcre"
27   SHARED_NAME=libohcount.dylib
28   RB_SHARED="-dynamic -bundle -lruby"
29   RB_SHARED_NAME=ohcount.bundle
30 fi
31
32 # C compiler and flags
33 cc="gcc -fPIC -g $CFLAGS $WARN -I$INC_DIR -L$LIB_DIR"
34
35 # Ohcount source files
36 files="src/sourcefile.c \
37        src/detector.c \
38        src/licenses.c \
39        src/parser.o \
40        src/loc.c \
41        src/diff.c \
42        src/parsed_language.c \
43        src/hash/language_hash.c"
44
45 # If any src/hash/*.gperf file is newer than the header files (which were
46 # presumably generated together), regenerate the headers.
47 build_hash_headers()
48 {
49   if [[ -z `ls src/hash/ | grep "_hash.h$"` ||
50         ! -z `find src/hash/*.gperf -newer src/hash/parser_hash.h` ]]
51   then
52     echo "Generating hash headers"
53     sh -c "cd src/hash/ && ./generate_headers" || exit 1
54   fi
55 }
56
57 # If src/parser.o does not exist, or if there are Ragel parsers or parser
58 # header files newer than the existing parser.o, recompile parser.o.
59 build_parser_o()
60 {
61   if [[ ! -f src/parser.o ||
62         ! -z `find src/parsers/*.{h,rl} -newer src/parser.o` ]]
63   then
64     bash -c "cd src/parsers/ && bash ./compile" || exit 1
65     echo "Building src/parser.c (will take a while)"
66     bash -c "$cc -c src/parser.c -o src/parser.o" || exit 1
67   fi
68 }
69
70 build_shared()
71 {
72   build_hash_headers
73   build_parser_o
74   if [[ ! -f src/$SHARED_NAME ||
75         ! -z `find src/*.{h,c} -newer src/$SHARED_NAME` ]]
76   then
77     echo "Building shared library"
78     sh -c "$cc $SHARED $files -o src/$SHARED_NAME" || exit 1
79   fi
80 }
81
82 build_ohcount()
83 {
84   build_hash_headers
85   build_parser_o
86   echo "Building Ohcount"
87   mkdir -p bin/
88   sh -c "$cc src/ohcount.c $files -o bin/ohcount -lpcre" || exit 1
89 }
90
91 build_test_suite()
92 {
93   build_hash_headers
94   build_parser_o
95   echo "Building test suite"
96   sh -c "$cc test/unit/all_tests.c $files -o test/unit/run_tests -lpcre" \
97     || exit 1
98 }
99
100 run_test_suite()
101 {
102   echo "Running test suite"
103   sh -c "cd test/unit/ && ./run_tests"
104 }
105
106 build_ruby_bindings()
107 {
108         arch=`ruby -rmkmf -e 'print Config::expand(CONFIG["arch"])'`
109         echo "Generating Ruby bindings for $arch"
110         sh -c "swig -ruby -o ruby/ohcount_wrap.c ruby/ohcount.i" || exit 1
111         mkdir -p ruby/$arch
112   sh -c "$cc $RB_SHARED ruby/ohcount_wrap.c $files -o ruby/$arch/$RB_SHARED_NAME \
113     -I`ruby -rmkmf -e 'print Config::expand(CONFIG["archdir"])'` \
114     -lpcre" || exit 1
115   sh -c "cd test/unit/ruby && ruby ruby_test.rb" || exit 1
116 }
117
118 if [ $# -eq 0 ] || [ $1 == "all" ]
119 then
120   build_ohcount
121   build_test_suite
122   run_test_suite
123   echo $success
124 elif [ $1 == "shared" ]
125 then
126   build_shared
127   echo "Build successful; $SHARED_NAME is in src/"
128 elif [ $1 == "ohcount" ]
129 then
130   build_ohcount
131   echo "Build successful; ohcount is in bin/"
132 elif [ $1 == "tests" ]
133 then
134   build_test_suite
135   run_test_suite
136 elif [ $1 == "ruby" ]
137 then
138   build_ruby_bindings
139   echo "Build successful; $RB_SHARED_NAME is in ruby/"
140 elif [ $1 == "clean" ]
141 then
142   rm bin/ohcount
143   rm test/unit/run_tests
144   rm src/parser.o
145   rm src/parsers/*.h
146   rm src/hash/*.h
147   rm src/hash/*.c
148   rm src/$SHARED_NAME
149   rm ruby/$RB_SHARED_NAME
150 else
151   echo "Usage: build [all|ohcount|shared|tests|ruby|clean]"
152 fi