|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright lowRISC contributors. |
| 3 | +# Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +# Get the project directory from the location of this script |
| 9 | +this_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) |
| 10 | +proj_root=$( realpath "${this_dir}/../.." ) |
| 11 | +build_dir="${proj_root}/build-site" |
| 12 | + |
| 13 | +####### |
| 14 | +# CLI # |
| 15 | +####### |
| 16 | +declare command="build" |
| 17 | +# The following are only used for "command='serve-proxy'" ------------------------| |
| 18 | +declare public_domain="" # (optional) The public-facing domain <-| |
| 19 | +declare public_port="" # (optional)The port added to the public-facing domain <-| |
| 20 | + |
| 21 | +case "$1" in |
| 22 | + "build"|"build-local"|"build-staging"|"serve"|"serve-proxy") |
| 23 | + command="$1" |
| 24 | + public_domain="${2}" |
| 25 | + public_port="${3}" |
| 26 | + ;; |
| 27 | + "help"|*) |
| 28 | + echo "USAGE: $0 <command> [public_domain] [public_port]" |
| 29 | + echo "" |
| 30 | + echo "commands:" |
| 31 | + echo " help prints this message." |
| 32 | + echo " build build the site and docs for prod" |
| 33 | + echo " build-local build the site and docs for a localhost server" |
| 34 | + echo " build-staging build the site and docs for staging.opentitan.org" |
| 35 | + echo " serve build and serve the site locally" |
| 36 | + echo " serve-proxy build and serve the site, with a public url" |
| 37 | + echo "" |
| 38 | + echo "Optional arguments [public_domain] and [public_port] are only used when command='serve-proxy'" |
| 39 | + exit 0 |
| 40 | + ;; |
| 41 | +esac |
| 42 | + |
| 43 | +################ |
| 44 | +# DEPENDENCIES # |
| 45 | +################ |
| 46 | + |
| 47 | +checkDeps () { |
| 48 | + # Check for mdbook dep |
| 49 | + if ! command -v mdbook >/dev/null; then |
| 50 | + echo "E: mdbook not found, please install from your package manager or with:" >&2 |
| 51 | + echo "E: $ cargo install mdbook" >&2 |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | + |
| 55 | + # Check for hugo dep |
| 56 | + if ! command -v hugo >/dev/null; then |
| 57 | + echo "E: hugo not found, please install from your package manager" >&2 |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | +} |
| 61 | +checkDeps |
| 62 | + |
| 63 | +################# |
| 64 | +# CONFIGURATION # |
| 65 | +################# |
| 66 | +declare base_url |
| 67 | +declare serve_port # The port used by the local webserver (using "build-docs.sh serve/serve-proxy") |
| 68 | + |
| 69 | +getURLs () { |
| 70 | + # Defaults here are for production ("build-docs.sh build") |
| 71 | + local scheme="https" |
| 72 | + local domain="opentitan.org" |
| 73 | + local port="" |
| 74 | + |
| 75 | + # Use un-encrypted localhost URLs when building/serving locally |
| 76 | + # - serve on port 9000. |
| 77 | + if [ "$command" = "build-local" ] || \ |
| 78 | + [ "$command" = "serve" ]; then |
| 79 | + scheme="http" |
| 80 | + domain="localhost" |
| 81 | + port=":9000" |
| 82 | + serve_port=":9000" |
| 83 | + fi |
| 84 | + # "serve-proxy" gives us some simple defaults for serving behind a proxy: |
| 85 | + # - set the public_domain/public_port appropriately (see $2/$3) |
| 86 | + # - serve on port 8000. |
| 87 | + if [ "$command" = "serve-proxy" ] ; then |
| 88 | + scheme="http" |
| 89 | + domain="${public_domain}" |
| 90 | + port=":${public_port}" |
| 91 | + serve_port=":8000" |
| 92 | + fi |
| 93 | + if [ "$command" = "build-staging" ] ; then |
| 94 | + scheme="https" |
| 95 | + domain="staging.opentitan.org" |
| 96 | + fi |
| 97 | + |
| 98 | + base_url="${scheme}://${domain}${port}" |
| 99 | +} |
| 100 | +getURLs |
| 101 | + |
| 102 | +# Export some environment variables that tools will pick up |
| 103 | +export HUGO_PARAMS_DOCSURL="${base_url}/book" # hugo |
| 104 | +export URL_ROOT="${base_url}/book" # earlgrey_diagram |
| 105 | + |
| 106 | +# Build up doxygen command |
| 107 | +doxygen_env="env" |
| 108 | +doxygen_env+=" SRCTREE_TOP=${proj_root}" |
| 109 | +doxygen_env+=" DOXYGEN_OUT=${build_dir}/gen" |
| 110 | +doxygen_args="${proj_root}/util/doxygen/Doxyfile" |
| 111 | + |
| 112 | +# Build up mdbook arguments |
| 113 | +mdbook_args="build" |
| 114 | +mdbook_args+=" --dest-dir ${build_dir}/book/" |
| 115 | +mdbook_args+=" ${proj_root}" |
| 116 | + |
| 117 | +mdbook_guides_args="build" |
| 118 | +mdbook_guides_args+=" --dest-dir ${build_dir}/guides/getting_started" |
| 119 | +mdbook_guides_args+=" ${proj_root}/doc/guides/getting_started" |
| 120 | + |
| 121 | +# Register the pre-processors |
| 122 | +# Each book should only be passed the preprocessors it specifies inside the book.toml |
| 123 | +# ./book.toml |
| 124 | +book_env="env" |
| 125 | +book_env+=" MDBOOK_PREPROCESSOR__TESTPLAN__COMMAND=${proj_root}/util/mdbook_testplan.py" |
| 126 | +book_env+=" MDBOOK_PREPROCESSOR__OTBN__COMMAND=${proj_root}/util/mdbook_otbn.py" |
| 127 | +book_env+=" MDBOOK_PREPROCESSOR__DOXYGEN__COMMAND=${proj_root}/util/mdbook_doxygen.py" |
| 128 | +book_env+=" MDBOOK_PREPROCESSOR__REGGEN__COMMAND=${proj_root}/util/mdbook_reggen.py" |
| 129 | +book_env+=" MDBOOK_PREPROCESSOR__WAVEJSON__COMMAND=${proj_root}/util/mdbook_wavejson.py" |
| 130 | +book_env+=" MDBOOK_PREPROCESSOR__README2INDEX__COMMAND=${proj_root}/util/mdbook_readme2index.py" |
| 131 | +book_env+=" MDBOOK_PREPROCESSOR__DASHBOARD__COMMAND=${proj_root}/util/mdbook_dashboard.py" |
| 132 | +# ./doc/guides/getting_started/book.toml |
| 133 | +book_guides_env="env" |
| 134 | +book_guides_env+=" MDBOOK_PREPROCESSOR__TOOLVERSION__COMMAND=${proj_root}/util/mdbook_toolversion.py" |
| 135 | + |
| 136 | +# Build up Hugo arguments |
| 137 | +hugo_args="" |
| 138 | +hugo_args+=" --source ${proj_root}/site/landing/" |
| 139 | +hugo_args+=" --destination ${build_dir}/" |
| 140 | +hugo_args+=" --baseURL ${base_url}" |
| 141 | + |
| 142 | +############ |
| 143 | +# BUILDING # |
| 144 | +############ |
| 145 | + |
| 146 | +buildSite () { |
| 147 | + mkdir -p "${build_dir}" |
| 148 | + mkdir -p "${build_dir}/gen/doxy" |
| 149 | + |
| 150 | + echo "Building doxygen..." |
| 151 | + pushd "${this_dir}" >/dev/null |
| 152 | + # shellcheck disable=SC2086 |
| 153 | + ${doxygen_env} doxygen ${doxygen_args} |
| 154 | + popd >/dev/null |
| 155 | + echo "Doxygen build complete." |
| 156 | + |
| 157 | + # shellcheck disable=SC2086 |
| 158 | + ${book_env} mdbook ${mdbook_args} |
| 159 | + # shellcheck disable=SC2086 |
| 160 | + ${book_guides_env} mdbook ${mdbook_guides_args} |
| 161 | + # shellcheck disable=SC2086 |
| 162 | + hugo ${hugo_args} |
| 163 | +} |
| 164 | +buildSite |
| 165 | + |
| 166 | +########### |
| 167 | +# SERVING # |
| 168 | +########### |
| 169 | + |
| 170 | +# If serving, run the python HTTP server |
| 171 | +if [ "$command" = "serve" ] || [ "$command" = "serve-proxy" ]; then |
| 172 | + echo "--------------------------------------------" |
| 173 | + echo |
| 174 | + echo "Website being served at : ${base_url}" |
| 175 | + echo |
| 176 | + echo "--------------------------------------------" |
| 177 | + python3 -m http.server -d "$build_dir" ${serve_port:1} |
| 178 | + # strip leading : |
| 179 | +fi |
0 commit comments