forked from microsoft/DirectXShaderCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_version.py
More file actions
163 lines (136 loc) · 6.73 KB
/
gen_version.py
File metadata and controls
163 lines (136 loc) · 6.73 KB
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
159
160
161
162
# Added generating of new version for each DX Compiler build.
# There are 3 kinds of version:
# 1. **Official build**
# Built by using `hctbuild -official`. The version is based on the current DXIL version, latest official
# release and a number of commits since then. The format is `dxil_major.dxil_minor.release_no.commit_count`.
# For example a current official version would be something like `1.5.1905.42`. The latest release
# information is read from `utils\version\latest-release.json`. The `1905` corresponds to `dxil-2019-05-16`
# release branch and `42` is the number of commits since that release branch was created. For main branch
# the `commit_count` will be incremented by 10000 to distinguish it from stabilized official release branch
# builds. So the current official version of main would be someting like `1.5.1905.10042`.
# 2. **Dev build**
# Build by using `hctbuild` with no other version-related option.
# The format is `dxil_major.dxil_minor.0.commit_count` where commit_count is the number of total commits
# since the beginning of the project.
# 3. **Fixed version build**
# Build by using `hctbuild -fv`. Enables overriding of the version information. The fixed version is
# read from `utils\version\version.inc`. Location of the version file can be overriden by `-fvloc` option
# on `hctbuild`.
# In addition to the numbered version the product version string on the binaries will also include branch
# name and last commit sha - `"1.5.1905.10042 (main, 47e31c8a)"`. This product version string is included
# in `dxc -?` output.
import argparse
import json
import os
import re
import subprocess
def get_output_of(cmd):
enlistment_root=os.path.dirname(os.path.abspath( __file__ ))
output = subprocess.check_output(cmd, cwd=enlistment_root)
return output.decode('ASCII').strip()
def is_dirty():
diff = get_output_of(["git", "diff", "HEAD", "--shortstat"])
return len(diff.strip()) != 0
def get_last_commit_sha():
try:
sha = get_output_of(["git", "rev-parse", "--short", "HEAD"])
if is_dirty():
sha += "-dirty"
return sha
except subprocess.CalledProcessError:
return "00000000"
def get_current_branch():
try:
return get_output_of(["git", "rev-parse", "--abbrev-ref", "HEAD"])
except subprocess.CalledProcessError:
return "private"
def get_commit_count(sha):
try:
return get_output_of(["git", "rev-list", "--count", sha])
except subprocess.CalledProcessError:
return 0
def read_latest_release_info():
latest_release_file = os.path.join(os.path.dirname(os.path.abspath( __file__)), "latest-release.json")
with open(latest_release_file, 'r') as f:
return json.load(f)
class VersionGen():
def __init__(self, options):
self.options = options
self.latest_release_info = read_latest_release_info()
self.current_branch = get_current_branch()
self.rc_version_field_4_cache = None
def tool_name(self):
return self.latest_release_info.get("toolname",
"dxcoob" if self.options.official else "dxc(private)")
def rc_version_field_1(self):
return self.latest_release_info["version"]["major"]
def rc_version_field_2(self):
minor = self.latest_release_info["version"]["minor"]
# If this is a preview branch, we use the preview_minor field as the
# minor version in the RC file. This allows us to have a different
# minor version for preview releases, without changing the highest
# released minor version.
minor = self.latest_release_info["version"].get("preview_minor", minor)
return minor
def rc_version_field_3(self):
return self.latest_release_info["version"]["rev"] if self.options.official else "0"
def rc_version_field_4(self):
if self.rc_version_field_4_cache is None:
base_commit_count = 0
if self.options.official:
base_commit_count = int(get_commit_count(self.latest_release_info["sha"]))
current_commit_count = int(get_commit_count("HEAD"))
distance_from_base = current_commit_count - base_commit_count
if (self.current_branch == "main"):
distance_from_base += 10000
self.rc_version_field_4_cache = str(distance_from_base)
return self.rc_version_field_4_cache
def quoted_version_str(self):
return '"{}.{}.{}.{}"'.format(
self.rc_version_field_1(),
self.rc_version_field_2(),
self.rc_version_field_3(),
self.rc_version_field_4())
def product_version_str(self):
if (self.options.no_commit_sha):
return self.quoted_version_str()
pv = '"{}.{}.{}.{} '.format(
self.rc_version_field_1(),
self.rc_version_field_2(),
self.rc_version_field_3(),
self.rc_version_field_4())
if (self.current_branch != "HEAD"):
pv += '({}, {})"'.format(self.current_branch, get_last_commit_sha())
else:
pv += '({})"'.format(get_last_commit_sha())
return pv
def print_define(self, name, value):
print('#ifdef {}'.format(name))
print('#undef {}'.format(name))
print('#endif')
print('#define {} {}'.format(name, value))
print()
def print_version(self):
print('#pragma once')
print()
self.print_define('RC_COMPANY_NAME', '"Microsoft(r) Corporation"')
self.print_define('RC_VERSION_FIELD_1', self.rc_version_field_1())
self.print_define('RC_VERSION_FIELD_2', self.rc_version_field_2())
self.print_define('RC_VERSION_FIELD_3', self.rc_version_field_3() if self.options.official else "0")
self.print_define('RC_VERSION_FIELD_4', self.rc_version_field_4())
self.print_define('RC_FILE_VERSION', self.quoted_version_str())
self.print_define('RC_FILE_DESCRIPTION', '"DirectX Compiler - Out Of Band"')
self.print_define('RC_COPYRIGHT', '"(c) Microsoft Corporation. All rights reserved."')
self.print_define('RC_PRODUCT_NAME', '"Microsoft(r) DirectX for Windows(r) - Out Of Band"')
self.print_define('RC_PRODUCT_VERSION', self.product_version_str())
self.print_define('HLSL_TOOL_NAME', '"{}"'.format(self.tool_name()))
self.print_define('HLSL_VERSION_MACRO', 'HLSL_TOOL_NAME " " RC_FILE_VERSION')
self.print_define('HLSL_LLVM_IDENT', 'HLSL_TOOL_NAME " " RC_PRODUCT_VERSION')
def main():
p = argparse.ArgumentParser("gen_version")
p.add_argument("--no-commit-sha", action='store_true')
p.add_argument("--official", action="store_true")
args = p.parse_args()
VersionGen(args).print_version()
if __name__ == '__main__':
main()