Merge pull request #41 from blackducksw/ubuntu_14
[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 RUBY_HEADER_DIR=`ruby -rmkmf -e 'print RbConfig::expand(CONFIG["rubyhdrdir"])'`
118 rbconfig_arch=`ruby -rmkmf -e 'print RbConfig::expand(CONFIG["arch"])'`
119 RUBY_CONFIG_DIR="$RUBY_HEADER_DIR/$rbconfig_arch"
120
121 build_ruby_bindings()
122 {
123         echo "Generating Ruby bindings for $arch"
124         sh -c "swig -ruby -o ruby/ohcount_wrap.c ruby/ohcount.i" || exit 1
125         mkdir -p ruby/$arch
126   sh -c "$cc $RB_SHARED ruby/ohcount_wrap.c $files -o ruby/$arch/$RB_SHARED_NAME \
127     -I$RUBY_HEADER_DIR -I$RUBY_CONFIG_DIR \
128     -lpcre -lmagic" || exit 1
129   sh -c "cd test/unit/ruby && ruby ruby_test.rb" || exit 1
130 }
131
132 if [ $# -eq 0 ] || [ $1 == "all" ]
133 then
134   build_ohcount
135   build_test_suite
136   run_test_suite
137   build_ruby_bindings
138   echo $success
139 elif [ $1 == "shared" ]
140 then
141   build_shared
142   echo "Build successful; $SHARED_NAME is in src/"
143 elif [ $1 == "ohcount" ]
144 then
145   build_ohcount
146   echo "Build successful; ohcount is in bin/"
147 elif [ $1 == "tests" ]
148 then
149   build_test_suite
150   run_test_suite
151 elif [ $1 == "ruby" ]
152 then
153   build_ruby_bindings
154   echo "Build successful; $RB_SHARED_NAME is in ruby/$arch"
155 elif [ $1 == "clean" ]
156 then
157   rm -f bin/ohcount
158   rm -f test/unit/run_tests
159   rm -f src/parser.o
160   rm -f src/parsers/*.h
161   rm -f src/hash/*.h
162   rm -f src/hash/*.c
163   rm -f src/$SHARED_NAME
164   rm -f ruby/$RB_SHARED_NAME
165   rm -rf ruby/$arch/*
166   rm -f ruby/ohcount_wrap.c
167 else
168   echo "Usage: build [all|ohcount|shared|tests|ruby|clean]"
169 fi