summaryrefslogtreecommitdiffstats
path: root/src/pmdk/utils/SRCVERSION.ps1
blob: 8e6e43639874d5a0ea31b26f8a38f33cf3c4cf11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2016-2020, Intel Corporation

#
# SRCVERSION.PS1 -- script to create SCRVERSION macro and generate srcversion.h
#

#
# Windows dll versioning supports only fixed number of fields. The most
# important are MAJOR, MINOR and REVISION. We have 3-compoment releases
# (e.g. 1.5.1) with release candidates, so we have to encode this information
# into this fixed number of fields. That's why we abuse REVISION to encode both
# 3rd component and rc status.
# REVISION = 3RDCOMP * 1000 + (!is_rc) * 100 + rc.
#
# Examples:
# +---------------------+-----+-----+--------+-----+------+-------+----------+
# |git describe --long  |MAJOR|MINOR|REVISION|BUILD|BUGFIX|PRIVATE|PRERELEASE|
# +---------------------+-----+-----+--------+-----+------+-------+----------+
# |1.5-rc2-0-12345678   |    1|    5|       2|    0| false|  false|      true|
# |1.5-rc3-6-12345678   |    1|    5|       3|    6| false|   true|      true|
# |1.5-0-12345678       |    1|    5|     100|    0| false|  false|     false|
# |1.5-6-123345678      |    1|    5|     100|    6| false|   true|     false|
# |1.5.2-rc1-0-12345678 |    1|    5|    2001|    0|  true|  false|      true|
# |1.5.2-rc4-6-12345678 |    1|    5|    2004|    6|  true|   true|      true|
# |1.5.2-0-12345678     |    1|    5|    2100|    0|  true|  false|     false|
# |1.5.2-6-12345678     |    1|    5|    2100|    6|  true|   true|     false|
# +---------------------+-----+-----+--------+-----+------+-------+----------+
#

$scriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition
$file_path = $scriptPath + "\..\src\windows\include\srcversion.h"
$git_version_file = $scriptPath + "\..\GIT_VERSION"
$version_file = $scriptPath + "\..\VERSION"
$git = Get-Command -Name git -ErrorAction SilentlyContinue

if (Test-Path $file_path) {
    $old_src_version = Get-Content $file_path | `
        Where-Object { $_ -like '#define SRCVERSION*' }
} else {
    $old_src_version = ""
}

$git_version = ""
$git_version_hash = ""

if (Test-Path $git_version_file) {
    $git_version = Get-Content $git_version_file
    if ($git_version -eq "`$Format:%h`$") {
        $git_version = ""
    } else {
        $git_version_hash = $git_version
    }
}

$PRERELEASE = $false
$BUGFIX = $false
$PRIVATE = $true
$CUSTOM = $false

if ($null -ne $args[0]) {
    $version = $args[0]
    $ver_array = $version.split("-+")
} elseif (Test-Path $version_file) {
    $version = Get-Content $version_file
    $ver_array = $version.split("-+")
} elseif ($git_version_hash -ne "") {
    $MAJOR = 0
    $MINOR = 0
    $REVISION = 0
    $BUILD = 0

    $version = $git_version_hash
    $CUSTOM = $true
    $version_custom_msg = "#define VERSION_CUSTOM_MSG `"$git_version_hash`""
} elseif ($null -ne $git) {
    $version = $(git describe)
    $ver_array = $(git describe --long).split("-+")
} else {
    $MAJOR = 0
    $MINOR = 0
    $REVISION = 0
    $BUILD = 0

    $version = "UNKNOWN_VERSION"
    $CUSTOM = $true
    $version_custom_msg = "#define VERSION_CUSTOM_MSG `"UNKNOWN_VERSION`""
}

if ($null -ne $ver_array) {
    $ver_dots = $ver_array[0].split(".")
    $MAJOR = $ver_dots[0]
    $MINOR = $ver_dots[1]
    if ($ver_dots.length -ge 3) {
        $REV = $ver_dots[2]
        $BUGFIX = $true
    } else {
        $REV = 0
    }

    $REVISION = 1000 * $REV
    $BUILD = $ver_array[$ver_array.length - 2]

    if ($ver_array.length -eq 4) {
        # <MAJOR>.<MINOR>[.<BUGFIX>]-<SUFFIX><REVISION>-<BUILD>-<HASH>

        if ($ver_array[1].StartsWith("rc")) {
            # <MAJOR>.<MINOR>[.<BUGFIX>]-rc<REVISION>-<BUILD>-<HASH>
            $REVISION += $ver_array[1].Substring("rc".Length)
            $PRERELEASE = $true
            $version = "$($ver_array[0])-$($ver_array[1])+git$($ver_array[2]).$($ver_array[3])"
        } else {
            # <MAJOR>.<MINOR>[.<BUGFIX>]-<SOMETHING>-<BUILD>-<HASH>
            throw "Unknown version format"
        }
    } else {
        # <MAJOR>.<MINOR>[.<BUGFIX>]-<BUILD>-<HASH>
        $REVISION += 100
        $version = "$($ver_array[0])+git$($ver_array[1]).$($ver_array[2])"
    }

    if ($BUILD -eq 0) {
        # it is not a (pre)release build
        $PRIVATE = $false
    }
}

$src_version = "#define SRCVERSION `"$version`""

if ($old_src_version -eq $src_version) {
    exit 0
}

Write-Output "updating source version: $version"
Write-Output $src_version > $file_path

Write-Output "#ifdef RC_INVOKED" >> $file_path

Write-Output "#define MAJOR $MAJOR" >> $file_path
Write-Output "#define MINOR $MINOR" >> $file_path
Write-Output "#define REVISION $REVISION" >> $file_path
Write-Output "#define BUILD $BUILD" >> $file_path

if ($PRERELEASE) {
    Write-Output "#define PRERELEASE 1"  >> $file_path
}
if ($BUGFIX) {
    Write-Output "#define BUGFIX 1"  >> $file_path
}
if ($PRIVATE) {
    Write-Output "#define PRIVATE 1"  >> $file_path
}
if ($CUSTOM) {
    Write-Output "#define CUSTOM 1"  >> $file_path
    Write-Output $version_custom_msg  >> $file_path
}

Write-Output "#endif" >> $file_path