1#!/bin/bash
2#
3# SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
4#
5# SPDX-License-Identifier: Apache-2.0
6#
7# Licensed under the Apache License, Version 2.0 (the License); you may
8# not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an AS IS BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19# Version: 1.0
20# Date: 2022-05-31
21# This bash script generates CMSIS-Driver Documentation:
22#
23# Pre-requisites:
24# - bash shell (for Windows: install git for Windows)
25# - doxygen 1.9.2
26# - git
27# - gh cli
28
29set -o pipefail
30
31DIRNAME=$(dirname $(realpath $0))
32DOXYGEN=$(which doxygen)
33REQ_DXY_VERSION="1.9.2"
34REQUIRED_GEN_PACK_LIB="0.5.1"
35
36############ gen-pack library ###########
37
38function install_lib() {
39  local URL="https://github.com/Open-CMSIS-Pack/gen-pack/archive/refs/tags/v$1.tar.gz"
40  echo "Downloading gen_pack lib to '$2'"
41  mkdir -p "$2"
42  curl -L "${URL}" -s | tar -xzf - --strip-components 1 -C "$2" || exit 1
43}
44
45function load_lib() {
46  if [[ -d ${GEN_PACK_LIB} ]]; then
47    . "${GEN_PACK_LIB}/gen-pack"
48    return 0
49  fi
50  local GLOBAL_LIB="/usr/local/share/gen-pack/${REQUIRED_GEN_PACK_LIB}"
51  local USER_LIB="${HOME}/.local/share/gen-pack/${REQUIRED_GEN_PACK_LIB}"
52  if [[ ! -d "${GLOBAL_LIB}" && ! -d "${USER_LIB}" ]]; then
53    echo "Required gen_pack lib not found!" >&2
54    install_lib "${REQUIRED_GEN_PACK_LIB}" "${USER_LIB}"
55  fi
56
57  if [[ -d "${GLOBAL_LIB}" ]]; then
58    . "${GLOBAL_LIB}/gen-pack"
59  elif [[ -d "${USER_LIB}" ]]; then
60    . "${USER_LIB}/gen-pack"
61  else
62    echo "Required gen-pack lib is not installed!" >&2
63    exit 1
64  fi
65}
66
67load_lib
68find_git
69find_ghcli
70
71#########################################
72
73if [[ ! -f "${DOXYGEN}" ]]; then
74  echo "Doxygen not found!" >&2
75  echo "Did you miss to add it to PATH?"
76  exit 1
77else
78  version=$("${DOXYGEN}" --version | sed -E 's/.*([0-9]+\.[0-9]+\.[0-9]+).*/\1/')
79  echo "DOXYGEN is ${DOXYGEN} at version ${version}"
80  if [[ "${version}" != "${REQ_DXY_VERSION}" ]]; then
81    echo " >> Version is different from ${REQ_DXY_VERSION} !" >&2
82  fi
83fi
84
85if [ -z "$VERSION" ]; then
86  VERSION_FULL=$(git_describe "v")
87  VERSION=${VERSION_FULL%+*}
88else
89  VERSION_FULL=${VERSION}
90fi
91
92echo "Generating documentation ..."
93
94pushd $DIRNAME > /dev/null
95
96rm -rf ${DIRNAME}/../Documentation/html
97sed -e "s/{projectNumber}/${VERSION}/" "${DIRNAME}/nn.dxy.in" \
98  > "${DIRNAME}/nn.dxy"
99
100git_changelog -p "v" -f html 1> history.txt 2>/dev/null
101
102echo "${DOXYGEN} nn.dxy"
103"${DOXYGEN}" nn.dxy
104popd > /dev/null
105
106if [[ $2 != 0 ]]; then
107  cp -f "${DIRNAME}/templates/search.css" "${DIRNAME}/../Documentation/html/search/"
108fi
109
110projectName=$(grep -E "PROJECT_NAME\s+=" "${DIRNAME}/nn.dxy" | sed -r -e 's/[^"]*"([^"]+)"/\1/')
111datetime=$(date -u +'%a %b %e %Y %H:%M:%S')
112year=$(date -u +'%Y')
113if [[ "${year}" != "2022" ]]; then
114  year="2022-${year}"
115fi
116sed -e "s/{datetime}/${datetime}/" "${DIRNAME}/templates/footer.js.in" \
117  | sed -e "s/{year}/${year}/" \
118  | sed -e "s/{projectName}/${projectName}/" \
119  | sed -e "s/{projectNumber}/${VERSION}/" \
120  | sed -e "s/{projectNumberFull}/${VERSION_FULL}/" \
121  > "${DIRNAME}/../Documentation/html/footer.js"
122
123exit 0
124