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