aboutsummaryrefslogtreecommitdiff
path: root/contrib/cmake/parse-version.m4.py
blob: 3dfb31fbf8c3498f4315cd09331719f8dca74dcb (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
#
#  OpenVPN -- An application to securely tunnel IP networks
#             over a single UDP port, with support for SSL/TLS-based
#             session authentication and key exchange,
#             packet encryption, packet authentication, and
#             packet compression.
#
#  Copyright (C) 2022-2023 OpenVPN Inc <sales@openvpn.net>
#  Copyright (C) 2022-2022 Lev Stipakov <lev@lestisoftware.fi>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2
#  as published by the Free Software Foundation.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

# Usage: ./parse-version.m4.py m4file [directory]
# Read <m4file>, extract all lines looking like M4 define(), and translate
# them into CMake style set(). Those are then written out to file
# <directory>/version.cmake.
# Intended to be used on top-level version.m4 file.

import os
import re
import sys

def main():
    assert len(sys.argv) > 1
    version_path = sys.argv[1]
    output = []
    with open(version_path, 'r') as version_file:
        for line in version_file:
            match = re.match(r'[ \t]*define\(\[(.*)\],[ \t]*\[(.*)\]\)[ \t]*', line)
            if match is not None:
                output.append(match.expand(r'set(\1 \2)'))
    out_path = os.path.join("%s" %  (sys.argv[2] if len(sys.argv) > 2 else "."), "version.cmake")

    prev_content = ""
    try:
        with open(out_path, "r") as out_file:
            prev_content = out_file.read()
    except:
        # file doesn't exist
        pass

    content = "\n".join(output) + "\n"
    if prev_content != content:
        print("Writing %s" % out_path)
        with open(out_path, "w") as out_file:
            out_file.write(content)
    else:
        print("Content of %s hasn't changed" % out_path)

if __name__ == "__main__":
    main()