summaryrefslogtreecommitdiff
path: root/codegen/vulkan/scripts/promote.py
blob: c6b73ba46b48cb68214c30f6ae865badfa4f653a (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
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
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/python3
#
# Copyright 2016-2021 The Khronos Group Inc.
#
# SPDX-License-Identifier: Apache-2.0

# Retroactively insert markup in show both 1.1 core features and KHR
# extensions they were promoted from.

# Usage: promote.py [-overwrite] [-out dir] [-suffix str] files
#   -overwrite updates in place (can be risky, make sure there are backups)
#   -out specifies directory to create output file in, default 'out'
#   -suffix specifies suffix to add to output files, default ''
#   files are asciidoc source files from the Vulkan spec to reflow.

# For error and file-loading interfaces only
import argparse, copy, os, pdb, re, string, sys
from reflib import *
from promoted import *

global anchor
anchor = 0

def anchorname(anchor):
    return 'promoted-' + str(anchor)

def printanchor(fp):
    # Anchor index for navigation
    global anchor

    print('[[' + anchorname(anchor) + ']]', file=fp)
    print('This anchor:', anchorname(anchor), file=fp)
    print('<<' + anchorname(anchor+1) + ', OINK>>', file=fp)
    anchor = anchor + 1

# promote a core interface and include the extension it was promoted from
#   line - matching line with 1.1 interface
#   type - 'protos', 'structs', 'flags', 'enums'
#   name - interface name
#   extension - name of extension interface was promoted from
#   fp - output filename
def promote(line, type, name, extension, fp):
    if type == 'protos':
        # printanchor(fp)
        print('ifdef::VK_VERSION_1_1[]', file=fp)
        print(line, file=fp, end='')
        print('endif::VK_VERSION_1_1[]', file=fp)
        print('', file=fp)
        print('ifdef::VK_VERSION_1_1+' + extension +
              '[or the equivalent command]', file=fp)
        print('', file=fp)
        print('ifdef::' + extension + '[]', file=fp)
        print('include::../api/' + type + '/' + name + 'KHR.txt[]', file=fp)
        print('endif::' + extension + '[]', file=fp)
        del promoted[name]
    elif type == 'structs' or type == 'enums' or type == 'flags' or type == 'handles':
        # printanchor(fp)
        print(line, file=fp, end='')
        print('', file=fp)
        print('ifdef::' + extension + '[]', file=fp)
        print('or the equivalent', file=fp)
        print('', file=fp)
        print('include::../api/' + type + '/' + name + 'KHR.txt[]', file=fp)
        print('endif::' + extension + '[]', file=fp)
        del promoted[name]
    else:
        logWarn('Unrecognized promoted type', type, 'for interface', name)
        print(line, file=fp, end='')


def promoteFile(filename, args):
    logDiag('promote: filename', filename)

    lines = loadFile(filename)
    if (lines == None):
        return

    # Output file handle and promote object for this file. There are no race
    # conditions on overwriting the input, but it's not recommended unless
    # you have backing store such as git.

    if args.overwrite:
        outFilename = filename
    else:
        outFilename = args.outDir + '/' + os.path.basename(filename) + args.suffix

    try:
        fp = open(outFilename, 'w', encoding='utf8')
        True
    except:
        logWarn('Cannot open output file', filename, ':', sys.exc_info()[0])
        return None

    lineno = 0
    for line in lines:
        lineno = lineno + 1

        matches = includePat.search(line)
        if matches != None:
            type = matches.group('type')
            name = matches.group('name')
            if name in promoted:
                extension = promoted[name]['extension']
                if extension:
                    # Promote core interface
                    promote(line, type, name, promoted[name]['extension'], fp)
                    continue
        # Fallthrough
        print(line, file=fp, end='')

    fp.close()

def promoteAllAdocFiles(folder_to_promote, args):
    for root, subdirs, files in os.walk(folder_to_promote):
        for file in files:
            if file.endswith(".txt"):
                file_path = os.path.join(root, file)
                promoteFile(file_path, args)
        for subdir in subdirs:
            sub_folder = os.path.join(root, subdir)
            print('Sub-folder = %s' % sub_folder)
            if not (subdir.lower() == "scripts") and not (subdir.lower() == "style"):
                print('   Parsing = %s' % sub_folder)
                promoteAllAdocFiles(sub_folder, args)
            else:
                print('   Skipping = %s' % sub_folder)

# Patterns used to recognize interesting lines in an asciidoc source file.
# These patterns are only compiled once.

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument('-diag', action='store', dest='diagFile',
                        help='Set the diagnostic file')
    parser.add_argument('-warn', action='store', dest='warnFile',
                        help='Set the warning file')
    parser.add_argument('-log', action='store', dest='logFile',
                        help='Set the log file for both diagnostics and warnings')
    parser.add_argument('-overwrite', action='store_true',
                        help='Overwrite input filenames instead of writing different output filenames')
    parser.add_argument('-out', action='store', dest='outDir',
                        default='out',
                        help='Set the output directory in which updated files are generated (default: out)')
    parser.add_argument('-suffix', action='store', dest='suffix',
                        default='',
                        help='Set the suffix added to updated file names (default: none)')
    parser.add_argument('files', metavar='filename', nargs='*',
                        help='a filename to promote text in')
    parser.add_argument('--version', action='version', version='%(prog)s 1.0')

    args = parser.parse_args()

    setLogFile(True,  True, args.logFile)
    setLogFile(True, False, args.diagFile)
    setLogFile(False, True, args.warnFile)

    if args.overwrite:
        logWarn('promote.py: will overwrite all input files')

    # If no files are specified, promote the entire specification chapters folder
    if len(args.files) == 0:
        folder_to_promote = os.getcwd()
        folder_to_promote += '/chapters'
        promoteAllAdocFiles(folder_to_promote, args)
    else:
        for file in args.files:
            promoteFile(file, args)

    print('At end, promoted interfaces remaining:')
    for key in promoted:
        if promoted[key]['extension'] != None:
            print('\t' + key)