1#!/bin/bash
2#
3# Copyright The Mbed TLS Contributors
4# SPDX-License-Identifier: Apache-2.0
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18# Purpose
19#
20# Sets the version numbers in the source code to those given.
21#
22# Usage: bump_version.sh [ --version <version> ] [ --so-crypto <version>]
23#                           [ --so-x509 <version> ] [ --so-tls <version> ]
24#                           [ -v | --verbose ] [ -h | --help ]
25#
26
27set -e
28
29VERSION=""
30SOVERSION=""
31
32# Parse arguments
33#
34until [ -z "$1" ]
35do
36  case "$1" in
37    --version)
38      # Version to use
39      shift
40      VERSION=$1
41      ;;
42    --so-crypto)
43      shift
44      SO_CRYPTO=$1
45      ;;
46    --so-x509)
47      shift
48      SO_X509=$1
49      ;;
50    --so-tls)
51      shift
52      SO_TLS=$1
53      ;;
54    -v|--verbose)
55      # Be verbose
56      VERBOSE="1"
57      ;;
58    -h|--help)
59      # print help
60      echo "Usage: $0"
61      echo -e "  -h|--help\t\tPrint this help."
62      echo -e "  --version <version>\tVersion to bump to."
63      echo -e "  --so-crypto <version>\tSO version to bump libmbedcrypto to."
64      echo -e "  --so-x509 <version>\tSO version to bump libmbedx509 to."
65      echo -e "  --so-tls <version>\tSO version to bump libmbedtls to."
66      echo -e "  -v|--verbose\t\tVerbose."
67      exit 1
68      ;;
69    *)
70      # print error
71      echo "Unknown argument: '$1'"
72      exit 1
73      ;;
74  esac
75  shift
76done
77
78if [ "X" = "X$VERSION" ];
79then
80  echo "No version specified. Unable to continue."
81  exit 1
82fi
83
84[ $VERBOSE ] && echo "Bumping VERSION in CMakeLists.txt"
85sed -e "s/ VERSION [0-9.]\{1,\}/ VERSION $VERSION/g" < CMakeLists.txt > tmp
86mv tmp CMakeLists.txt
87
88[ $VERBOSE ] && echo "Bumping VERSION in library/CMakeLists.txt"
89sed -e "s/ VERSION [0-9.]\{1,\}/ VERSION $VERSION/g" < library/CMakeLists.txt > tmp
90mv tmp library/CMakeLists.txt
91
92if [ "X" != "X$SO_CRYPTO" ];
93then
94  [ $VERBOSE ] && echo "Bumping SOVERSION for libmbedcrypto in library/CMakeLists.txt"
95  sed -e "/mbedcrypto/ s/ SOVERSION [0-9]\{1,\}/ SOVERSION $SO_CRYPTO/g" < library/CMakeLists.txt > tmp
96  mv tmp library/CMakeLists.txt
97
98  [ $VERBOSE ] && echo "Bumping SOVERSION for libmbedcrypto in library/Makefile"
99  sed -e "s/SOEXT_CRYPTO?=so.[0-9]\{1,\}/SOEXT_CRYPTO?=so.$SO_CRYPTO/g" < library/Makefile > tmp
100  mv tmp library/Makefile
101fi
102
103if [ "X" != "X$SO_X509" ];
104then
105  [ $VERBOSE ] && echo "Bumping SOVERSION for libmbedx509 in library/CMakeLists.txt"
106  sed -e "/mbedx509/ s/ SOVERSION [0-9]\{1,\}/ SOVERSION $SO_X509/g" < library/CMakeLists.txt > tmp
107  mv tmp library/CMakeLists.txt
108
109  [ $VERBOSE ] && echo "Bumping SOVERSION for libmbedx509 in library/Makefile"
110  sed -e "s/SOEXT_X509?=so.[0-9]\{1,\}/SOEXT_X509?=so.$SO_X509/g" < library/Makefile > tmp
111  mv tmp library/Makefile
112fi
113
114if [ "X" != "X$SO_TLS" ];
115then
116  [ $VERBOSE ] && echo "Bumping SOVERSION for libmbedtls in library/CMakeLists.txt"
117  sed -e "/mbedtls/ s/ SOVERSION [0-9]\{1,\}/ SOVERSION $SO_TLS/g" < library/CMakeLists.txt > tmp
118  mv tmp library/CMakeLists.txt
119
120  [ $VERBOSE ] && echo "Bumping SOVERSION for libmbedtls in library/Makefile"
121  sed -e "s/SOEXT_TLS?=so.[0-9]\{1,\}/SOEXT_TLS?=so.$SO_TLS/g" < library/Makefile > tmp
122  mv tmp library/Makefile
123fi
124
125[ $VERBOSE ] && echo "Bumping VERSION in include/mbedtls/build_info.h"
126read MAJOR MINOR PATCH <<<$(IFS="."; echo $VERSION)
127VERSION_NR="$( printf "0x%02X%02X%02X00" $MAJOR $MINOR $PATCH )"
128cat include/mbedtls/build_info.h |                                    \
129    sed -e "s/\(# *define  *[A-Z]*_VERSION\)_MAJOR .\{1,\}/\1_MAJOR  $MAJOR/" |    \
130    sed -e "s/\(# *define  *[A-Z]*_VERSION\)_MINOR .\{1,\}/\1_MINOR  $MINOR/" |    \
131    sed -e "s/\(# *define  *[A-Z]*_VERSION\)_PATCH .\{1,\}/\1_PATCH  $PATCH/" |    \
132    sed -e "s/\(# *define  *[A-Z]*_VERSION\)_NUMBER .\{1,\}/\1_NUMBER         $VERSION_NR/" |    \
133    sed -e "s/\(# *define  *[A-Z]*_VERSION\)_STRING .\{1,\}/\1_STRING         \"$VERSION\"/" |    \
134    sed -e "s/\(# *define  *[A-Z]*_VERSION\)_STRING_FULL .\{1,\}/\1_STRING_FULL    \"mbed TLS $VERSION\"/" \
135    > tmp
136mv tmp include/mbedtls/build_info.h
137
138[ $VERBOSE ] && echo "Bumping version in tests/suites/test_suite_version.data"
139sed -e "s/version:\".\{1,\}/version:\"$VERSION\"/g" < tests/suites/test_suite_version.data > tmp
140mv tmp tests/suites/test_suite_version.data
141
142[ $VERBOSE ] && echo "Bumping PROJECT_NAME in doxygen/mbedtls.doxyfile and doxygen/input/doc_mainpage.h"
143for i in doxygen/mbedtls.doxyfile doxygen/input/doc_mainpage.h;
144do
145  sed -e "s/mbed TLS v[0-9\.]\{1,\}/mbed TLS v$VERSION/g" < $i > tmp
146  mv tmp $i
147done
148
149[ $VERBOSE ] && echo "Re-generating library/error.c"
150scripts/generate_errors.pl
151
152[ $VERBOSE ] && echo "Re-generating programs/test/query_config.c"
153scripts/generate_query_config.pl
154
155[ $VERBOSE ] && echo "Re-generating library/version_features.c"
156scripts/generate_features.pl
157
158[ $VERBOSE ] && echo "Re-generating visualc files"
159scripts/generate_visualc_files.pl
160
161