1#! /bin/sh
2# Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the Apache License 2.0 (the "License").  You may not use
5# this file except in compliance with the License.  You can obtain a copy
6# in the file LICENSE in the source distribution or at
7# https://www.openssl.org/source/license.html
8
9# These functions load, manipulate and store the current version information
10# for OpenSSL 3.0 and on.
11# They are meant to be minimalistic for easy refactoring depending on OpenSSL
12# version.
13#
14# Version information is stored in the following variables:
15#
16# |MAJOR|, |MINOR|, |PATCH| are the three parts of a version number.
17# |MAJOR| is to be increased for new major releases, |MINOR| for new
18# minor releases, and |PATCH| for update releases.
19#
20# |SERIES| tells what release series the current version belongs to, and
21# is composed from |MAJOR| and |MINOR|.
22# |VERSION| tells what the current version is, and is composed from |MAJOR|,
23# |MINOR| and |PATCH|.
24# |TYPE| tells what state the source is in.  It may have an empty value
25# for released source, or 'dev' for "in development".
26# |PRE_LABEL| may be "alpha" or "beta" to signify an ongoing series of
27# alpha or beta releases.  |PRE_NUM| is a pre-release counter for the
28# alpha and beta release series, but isn't necessarily strictly tied
29# to the prerelease label.
30#
31# Scripts loading this file are not allowed to manipulate these
32# variables directly.  They must use functions such as fixup_version()
33# below, or next_release_state(), found in release-state-fn.sh.
34
35# These functions depend on |SOURCEDIR|, which must have the intended
36# OpenSSL source directory as value.
37
38get_version () {
39    eval $(git cat-file blob HEAD:VERSION.dat)
40    VERSION="$MAJOR.$MINOR.$PATCH"
41    SERIES="$MAJOR.$MINOR"
42    TYPE=$( echo "$PRE_RELEASE_TAG" \
43                | sed -E \
44                      -e 's|^dev$|dev|' \
45                      -e 's|^alpha([0-9]+)(-(dev))?$|\3|' \
46                      -e 's|^beta([0-9]+)(-(dev))?$|\3|' )
47    PRE_LABEL=$( echo "$PRE_RELEASE_TAG" \
48                     | sed -E \
49                           -e 's|^dev$||' \
50                           -e 's|^alpha([0-9]+)(-(dev))?$|alpha|' \
51                           -e 's|^beta([0-9]+)(-(dev))?$|beta|' )
52    PRE_NUM=$( echo "$PRE_RELEASE_TAG" \
53                   | sed -E \
54                         -e 's|^dev$|0|' \
55                         -e 's|^alpha([0-9]+)(-(dev))?$|\1|' \
56                         -e 's|^beta([0-9]+)(-(dev))?$|\1|' )
57    _BUILD_METADATA=''
58    if [ -n "$PRE_RELEASE_TAG" ]; then _PRE_RELEASE_TAG="-${PRE_RELEASE_TAG}"; fi
59    if [ -n "$BUILD_METADATA" ]; then _BUILD_METADATA="+${BUILD_METADATA}"; fi
60}
61
62# $1 is one of "alpha", "beta", "final", "", or "minor"
63fixup_version () {
64    local new_label="$1"
65
66    case "$new_label" in
67        alpha | beta )
68            if [ "$new_label" != "$PRE_LABEL" ]; then
69                PRE_LABEL="$new_label"
70                PRE_NUM=1
71            elif [ "$TYPE" = 'dev' ]; then
72                PRE_NUM=$(expr $PRE_NUM + 1)
73            fi
74            ;;
75        final | '' )
76            if [ "$TYPE" = 'dev' ]; then
77                PATCH=$(expr $PATCH + 1)
78            fi
79            PRE_LABEL=
80            PRE_NUM=0
81            ;;
82        minor )
83            if [ "$TYPE" = 'dev' ]; then
84                MINOR=$(expr $MINOR + 1)
85                PATCH=0
86            fi
87            PRE_LABEL=
88            PRE_NUM=0
89            ;;
90    esac
91
92    VERSION="$MAJOR.$MINOR.$PATCH"
93    SERIES="$MAJOR.$MINOR"
94}
95
96set_version () {
97    case "$TYPE+$PRE_LABEL+$PRE_NUM" in
98        *++* )
99            PRE_RELEASE_TAG="$TYPE"
100            ;;
101        dev+* )
102            PRE_RELEASE_TAG="$PRE_LABEL$PRE_NUM-dev"
103            ;;
104        +* )
105            PRE_RELEASE_TAG="$PRE_LABEL$PRE_NUM"
106            ;;
107    esac
108    if [ -n "$PRE_RELEASE_TAG" ]; then _PRE_RELEASE_TAG="-${PRE_RELEASE_TAG}"; fi
109    cat > "$SOURCEDIR/VERSION.dat" <<EOF
110MAJOR=$MAJOR
111MINOR=$MINOR
112PATCH=$PATCH
113PRE_RELEASE_TAG=$PRE_RELEASE_TAG
114BUILD_METADATA=$BUILD_METADATA
115RELEASE_DATE="$RELEASE_DATE"
116SHLIB_VERSION=$SHLIB_VERSION
117EOF
118}
119