xref: /llvm-project/llvm/utils/release/get-llvm-version.sh (revision 14837aff058f9a2d32b8277debe619d8eb1995a1)
1#!/usr/bin/env bash
2#===-- get-llvm-version.sh - Get LLVM Version from sources -----------------===#
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7#
8#===------------------------------------------------------------------------===#
9#
10# Extract the current LLVM version from the CMake files.
11#
12#===------------------------------------------------------------------------===#
13
14cmake_file=$(dirname $0)/../../../cmake/Modules/LLVMVersion.cmake
15function usage() {
16    echo "usage: `basename $0`"
17    echo ""
18    echo "Calling this script with now options will output the full version: e.g. 19.1.0"
19    echo " --cmake-file      Path to cmake file with the version (default: $cmake_file)
20    echo " You can use at most one of the following options:
21    echo " --major           Print the major version."
22    echo " --minor           Print the minor version."
23    echo " --patch           Print the patch version."
24}
25
26print=""
27
28while [ $# -gt 0 ]; do
29    case $1 in
30        --cmake-file )
31            shift
32    	    cmake_file="$1"
33    	    ;;
34        --major)
35            if [ -n "$print" ]; then
36                echo "Only one of --major, --minor, --patch is allowed"
37                exit 1
38            fi
39            print="major"
40            ;;
41        --minor)
42            if [ -n "$print" ]; then
43                echo "Only one of --major, --minor, --patch is allowed"
44                exit 1
45            fi
46            print="minor"
47            ;;
48        --patch)
49            if [ -n "$print" ]; then
50                echo "Only one of --major, --minor, --patch is allowed"
51                exit 1
52            fi
53            print="patch"
54            ;;
55        --help | -h | -\? )
56            usage
57            exit 0
58            ;;
59        * )
60            echo "unknown option: $1"
61            usage
62            exit 1
63            ;;
64    esac
65    shift
66done
67
68major=`grep -o 'LLVM_VERSION_MAJOR[[:space:]]\+\([0-9]\+\)' $cmake_file  | grep -o '[0-9]\+'`
69minor=`grep -o 'LLVM_VERSION_MINOR[[:space:]]\+\([0-9]\+\)' $cmake_file  | grep -o '[0-9]\+'`
70patch=`grep -o 'LLVM_VERSION_PATCH[[:space:]]\+\([0-9]\+\)' $cmake_file  | grep -o '[0-9]\+'`
71
72case $print in
73    major)
74        echo "$major"
75        ;;
76    minor)
77        echo "$minor"
78        ;;
79    patch)
80        echo "$patch"
81        ;;
82    *)
83        echo "$major.$minor.$patch"
84        ;;
85esac
86
87