1# Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC")
2#
3# This Source Code Form is subject to the terms of the Mozilla Public
4# License, v. 2.0. If a copy of the MPL was not distributed with this
5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7'''
8The script takes a C++ file with constant definitions and creates a
9header file for the constants. It, however, does not understand C++
10syntax, it only does some heuristics to guess what looks like
11a constant and strips the values.
12
13The purpose is just to save some work with keeping both the source and
14header. The source syntax must be limited already, because it's used to
15generate the python module (by the
16lib/python/isc/util/pythonize_constants.py script).
17'''
18
19import sys
20import re
21
22if len(sys.argv) != 3:
23    sys.stderr.write("Usage: python3 ./const2hdr.py input.cc output.h\n")
24    sys.exit(1)
25
26[filename_in, filename_out] = sys.argv[1:3]
27
28preproc = re.compile('^#')
29constant = re.compile('^([a-zA-Z].*?[a-zA-Z_0-9]+)\\s*=.*;')
30
31with open(filename_in) as file_in, open(filename_out, "w") as file_out:
32    file_out.write("// This file is generated from " + filename_in + "\n" +
33                   "// by the const2hdr.py script.\n" +
34                   "// Do not edit, all changes will be lost.\n\n")
35    for line in file_in:
36        if preproc.match(line):
37            # There's only one preprocessor line in the .cc file. We abuse
38            # that to position the top part of the header.
39            file_out.write("#ifndef BIND10_COMMON_DEFS_H\n" +
40                           "#define BIND10_COMMON_DEFS_H\n" +
41                           "\n" +
42                           "// \\file " + filename_out + "\n" +
43'''// \\brief Common shared constants\n
44// This file contains common definitions of constants used across the sources.
45// It includes, but is not limited to the definitions of messages sent from
46// one process to another. Since the names should be self-explanatory and
47// the variables here are used mostly to synchronize the same values across
48// multiple programs, separate documentation for each variable is not provided.
49''')
50            continue
51        # Extract the constant. Remove the values and add "extern"
52        line = constant.sub('extern \\1;', line)
53
54        file_out.write(line)
55
56    file_out.write("#endif\n")
57