xref: /aosp_15_r20/external/zstd/build/meson/GetZstdLibraryVersion.py (revision 01826a4963a0d8a59bc3812d29bdf0fb76416722)
1*01826a49SYabin Cui#!/usr/bin/env python3
2*01826a49SYabin Cui# #############################################################################
3*01826a49SYabin Cui# Copyright (c) 2018-present    lzutao <taolzu(at)gmail.com>
4*01826a49SYabin Cui# All rights reserved.
5*01826a49SYabin Cui#
6*01826a49SYabin Cui# This source code is licensed under both the BSD-style license (found in the
7*01826a49SYabin Cui# LICENSE file in the root directory of this source tree) and the GPLv2 (found
8*01826a49SYabin Cui# in the COPYING file in the root directory of this source tree).
9*01826a49SYabin Cui# #############################################################################
10*01826a49SYabin Cuiimport re
11*01826a49SYabin Cui
12*01826a49SYabin Cui
13*01826a49SYabin Cuidef find_version_tuple(filepath):
14*01826a49SYabin Cui  version_file_data = None
15*01826a49SYabin Cui  with open(filepath) as fd:
16*01826a49SYabin Cui    version_file_data = fd.read()
17*01826a49SYabin Cui
18*01826a49SYabin Cui  patterns = r"""#\s*define\s+ZSTD_VERSION_MAJOR\s+([0-9]+)
19*01826a49SYabin Cui#\s*define\s+ZSTD_VERSION_MINOR\s+([0-9]+)
20*01826a49SYabin Cui#\s*define\s+ZSTD_VERSION_RELEASE\s+([0-9]+)
21*01826a49SYabin Cui"""
22*01826a49SYabin Cui  regex = re.compile(patterns, re.MULTILINE)
23*01826a49SYabin Cui  version_match = regex.search(version_file_data)
24*01826a49SYabin Cui  if version_match:
25*01826a49SYabin Cui    return version_match.groups()
26*01826a49SYabin Cui  raise Exception("Unable to find version string")
27*01826a49SYabin Cui
28*01826a49SYabin Cui
29*01826a49SYabin Cuidef main():
30*01826a49SYabin Cui  import argparse
31*01826a49SYabin Cui  parser = argparse.ArgumentParser(description='Print zstd version from lib/zstd.h')
32*01826a49SYabin Cui  parser.add_argument('file', help='path to lib/zstd.h')
33*01826a49SYabin Cui  args = parser.parse_args()
34*01826a49SYabin Cui  version_tuple = find_version_tuple(args.file)
35*01826a49SYabin Cui  print('.'.join(version_tuple))
36*01826a49SYabin Cui
37*01826a49SYabin Cui
38*01826a49SYabin Cuiif __name__ == '__main__':
39*01826a49SYabin Cui  main()
40