xref: /open-nvidia-gpu/nv-compiler.sh (revision 758b4ee8)
1#!/bin/sh
2#  SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3#  SPDX-License-Identifier: MIT
4#
5#  Permission is hereby granted, free of charge, to any person obtaining a
6#  copy of this software and associated documentation files (the "Software"),
7#  to deal in the Software without restriction, including without limitation
8#  the rights to use, copy, modify, merge, publish, distribute, sublicense,
9# and/or sell copies of the Software, and to permit persons to whom the
10# Software is furnished to do so, subject to the following conditions:
11#
12#  The above copyright notice and this permission notice shall be included in
13#  all copies or substantial portions of the Software.
14#
15#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18#  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21#  DEALINGS IN THE SOFTWARE.
22#
23
24set -e
25
26get_compiler_type()
27{
28    printf "#if defined(__clang__)
29            clang
30            #elif defined(__GNUC__)
31            gcc
32            #elif defined(__INTEL_COMPILER)
33            icc
34            #else
35            unknown
36            #endif" | $1 -E -P -
37}
38
39get_original_version()
40{
41    printf "#if defined(__clang__)
42            __clang_major__ __clang_minor__ __clang_patchlevel__
43            #elif defined(__GNUC__)
44            __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__
45            #elif defined(__INTEL_COMPILER)
46            __INTEL_COMPILER __INTEL_COMPILER_UPDATE
47            #endif" | $1 -E -P -
48}
49
50get_canonical_version()
51{
52    type=$(get_compiler_type $1)
53    set -- $(get_original_version $1)
54
55    # get the version based on the type
56    if [ "$type" = "unknown" ]; then
57       echo >&2 "unknown compiler: bailing out"
58       exit 1
59    elif [ "$type" = "icc" ]; then
60       echo >&2 "icc is not supported"
61       exit 1
62    else
63      major=$1
64      minor=$2
65    fi
66    patch=$3
67    echo $(($3 + $2 * 100 + $1 * 10000))
68}
69
70if [ "$1" = "type" ]; then
71    echo $(get_compiler_type $2)
72elif [ "$1" = "version_is_at_least" ]; then
73    if [ -z "$3" ]; then
74      echo >&2 "minimum compiler version cannot be empty"
75      exit 1
76    fi
77    version=$(get_canonical_version $2)
78    if [ "$version" -gt $(($3-1)) ]; then
79      echo "1"
80    fi
81fi
82
83