Added gestalt_test.rb helper and run it on build of Ruby extension.
[ohcount] / build
1 #!/bin/sh
2 # Build script for Ohcount.
3 # Written by Mitchell Foral. mitchell<att>caladbolg.net.
4
5 # C compiler and flags
6 cc="gcc -fPIC -g -Wall -Wno-pointer-to-int-cast -Wno-parentheses"
7
8 # Ohcount source files
9 files="src/sourcefile.c \
10        src/detector.c \
11        src/licenses.c \
12        src/parser.o \
13        src/loc.c \
14        src/diff.c \
15        src/parsed_language.c \
16        src/hash/language_hash.c"
17
18 # If any src/hash/*.gperf file is newer than the header files (which were
19 # presumably generated together), regenerate the headers.
20 build_hash_headers()
21 {
22   if [[ -z `ls src/hash/ | grep "_hash.h$"` ||
23         ! -z `find src/hash/*.gperf -newer src/hash/parser_hash.h` ]]
24   then
25     echo "Generating hash headers"
26     sh -c "cd src/hash/ && ./generate_headers" || exit 1
27   fi
28 }
29
30 # If src/parser.o does not exist, or if there are Ragel parsers or parser
31 # header files newer than the existing parser.o, recompile parser.o.
32 build_parser_o()
33 {
34   if [[ ! -f src/parser.o ||
35         ! -z `find src/parsers/*.{h,rl} -newer src/parser.o` ]]
36   then
37     sh -c "cd src/parsers/ && ./compile" || exit 1
38     echo "Building src/parser.c (will take a while)"
39     sh -c "$cc -c src/parser.c -o src/parser.o" || exit 1
40   fi
41 }
42
43 build_shared()
44 {
45   build_hash_headers
46   build_parser_o
47   if [[ ! -f src/libohcount.so ||
48         ! -z `find src/*.{h,c} -newer src/libohcount.so` ]]
49   then
50     echo "Building shared library"
51     sh -c "$cc -shared $files -o src/libohcount.so" || exit 1
52   fi
53 }
54
55 build_ohcount()
56 {
57   build_hash_headers
58   build_parser_o
59   echo "Building Ohcount"
60   mkdir -p bin/
61   sh -c "$cc src/ohcount.c $files -o bin/ohcount -lpcre" || exit 1
62 }
63
64 build_test_suite()
65 {
66   build_hash_headers
67   build_parser_o
68   echo "Building test suite"
69   sh -c "$cc test/unit/all_tests.c $files -o test/unit/run_tests -lpcre" \
70     || exit 1
71 }
72
73 run_test_suite()
74 {
75   echo "Running test suite"
76   sh -c "cd test/unit/ && ./run_tests"
77 }
78
79 build_ruby_bindings()
80 {
81   echo "Generating Ruby bindings"
82   sh -c "swig -ruby -o ruby/ohcount_wrap.c ruby/ohcount.i" || exit 1
83   sh -c "$cc -shared ruby/ohcount_wrap.c $files -o ruby/ohcount.so \
84     -I/usr/lib/ruby/1.8/x86_64-linux/ -lpcre" || exit 1
85   sh -c "cd test/unit/gestalt && ruby gestalt_test.rb"
86 }
87
88 if [ $# -eq 0 ] || [ $1 == "all" ]
89 then
90   build_ohcount
91   build_test_suite
92   run_test_suite
93   echo $success
94 elif [ $1 == "shared" ]
95 then
96   build_shared
97   echo "Build successful; libohcount.so is in src/"
98 elif [ $1 == "ohcount" ]
99 then
100   build_ohcount
101   echo "Build successful; ohcount is in bin/"
102 elif [ $1 == "tests" ]
103 then
104   build_test_suite
105   run_test_suite
106 elif [ $1 == "ruby" ]
107 then
108   build_ruby_bindings
109   echo "Build successful; ohcount.so is in ruby/"
110 elif [ $1 == "clean" ]
111 then
112   rm src/libohcount.so
113   rm bin/ohcount
114   rm test/unit/run_tests
115   rm src/parser.o
116   rm src/parsers/*.h
117   rm src/hash/*.h
118   rm src/hash/*.c
119 else
120   echo "Usage: build [all|ohcount|shared|tests|ruby|clean]"
121 fi