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