aboutsummaryrefslogtreecommitdiff
path: root/build.sh
blob: 44c5cd5113295fc4ed5cd802e1566af410a70d71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
#
# Build automation.
#
# Usage:
#   ./build.sh [function name]
#
# Important targets are:
#   cpp-client: Build the C++ client
#   doc: build docs with Markdown
#   fastrand: build Python extension module to speed up the client simulation
#
# If no function is specified all 3 targets will be built.

set -o nounset
set -o pipefail
set -o errexit

log() {
  echo 1>&2 "$@"
}

die() {
  log "FATAL: $@"
  exit 1
}

run-markdown() {
  local md=`which markdown || echo "cat"`

  # Markdown is output unstyled; make it a little more readable.
  cat <<EOF
  <!DOCTYPE html>
  <html>
    <head>
      <meta charset="UTF-8">
      <style type="text/css">
        code { color: green; }
        pre { margin-left: 3em; }
      </style>
      <!-- INSERT LATCH JS -->
    </head>
    <body style="margin: 0 auto; width: 40em; text-align: left;">
      <!-- INSERT LATCH HTML -->
EOF

  $md "$@"

  cat <<EOF
    </body>
  </html>
EOF
}

run-dot() {
  local in=$1
  local out=$2

  local msg="dot not found (perhaps 'sudo apt-get install graphviz')"
  which dot >/dev/null || die "$msg"

  log "Running dot"
  # width, height
  dot \
    -Tpng -Gsize='2,4!' -Gdpi=300 \
    -o $out $in
}

# Scan for TODOs.  Does this belong somewhere else?
todo() {
  find . -name \*.py -o -name \*.R -o -name \*.sh -o -name \*.md \
    | xargs --verbose -- grep -w TODO
}

#
# Targets: build "doc" or "fastrand"
#

# Build dependencies: markdown tool.
doc() {
  mkdir -p _tmp _tmp/doc

  # For now, just one file.
  # TODO: generated docs
  run-markdown <README.md >_tmp/README.html
  run-markdown <doc/randomness.md >_tmp/doc/randomness.html

  run-markdown <doc/data-flow.md >_tmp/doc/data-flow.html
  run-dot doc/data-flow.dot _tmp/doc/data-flow.png

  log 'Wrote docs to _tmp'
}

# Build dependencies: Python development headers.  Most systems should have
# this.  On Ubuntu/Debian, the 'python-dev' package contains headers.
fastrand() {
  pushd tests >/dev/null
  python setup.py build
  # So we can 'import _fastrand' without installing
  ln -s --force build/*/_fastrand.so .
  ./fastrand_test.py

  log 'fastrand built and tests PASSED'
  popd >/dev/null
}

cpp-client() {
  pushd client/cpp
  mkdir --verbose -p _tmp
  make _tmp/rappor_sim  # this builds an executable using it
  popd
}

if test $# -eq 0 ; then
  cpp-client
  doc
  fastrand
else
  "$@"
fi