blob: dbd179bd7ef5416675e1e60d3f836d27823f5fc2 [file] [log] [blame]
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* This program processes Renderscript function definitions described in spec files.
18 * For each spec file provided on the command line, it generates a corresponding
19 * Renderscript header (*.rsh) which is meant for inclusion in client scripts.
20 *
21 * This program also generates Junit test files to automatically test each of the
22 * functions using randomly generated data. We create two files for each function:
23 * - a Renderscript file named Test{Function}.rs,
24 * - a Junit file named Test{function}.java, which calls the above RS file.
25 *
26 * Finally, this program generates HTML documentation files.
27 *
28 * This program takes an optional -v parameter, the RS version to target the
29 * test files for. The header file will always contain all the functions.
30 *
31 * This program contains five main classes:
32 * - SpecFile: Represents on spec file.
33 * - Function: Each instance represents a function, like clamp. Even though the
34 * spec file contains many entries for clamp, we'll only have one clamp instance.
35 * - FunctionSpecification: Defines one of the many variations of the function. There's
36 * a one to one correspondance between FunctionSpecification objects and entries in the
37 * spec file. Strings that are parts of a FunctionSpecification can include placeholders,
38 * which are "#1", "#2", "#3", and "#4". We'll replace these by values before
39 * generating the files.
40 * - Permutation: A concrete version of a specification, where all placeholders have
41 * been replaced by actual values.
42 * - ParameterDefinition: A definition of a parameter of a concrete function.
43 *
44 * The format of the .spec files is described below. Line that starts with # are comments.
45 * Replace the {} sections with your own contents. [] indicates optional parts.
46 *
47 * It should start with a header as follows:
48 *
49 * header:
50 * summary: {A one line string describing this section.}
51 * description:
52 * {Multiline description. Can include HTML. References to constants, types,
53 * and functions can be created by prefixing with a '@'.}
54 * [include:
55 * { Multiline code lines to be included as-is in the generated header file.}]
56 * end:
57 *
58 * Constants are defined as follows:
59 *
60 * constant: {The name of the constant.}
61 * [version: {Starting API level} [ {Last API level that supports this.}]
62 * [size: {32 or 64. Used if this is available only for 32 or 64 bit code.}]
63 * value: {The value of the constant.}
64 * [hidden:] ...If present, don't document the constant. Omit the following two fields.
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -070065 * [deprecated: [{Deprecation message.}] ... This is deprecated. Compiler will issue a wrning.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070066 * summary: {A one line string describing this section.}
67 * description:
68 * {Multiline description. Can include HTML. References to constants, types,
69 * and functions can be created by prefixing with a '@'.}
70 * end:
71 *
72 * Types can either be simple types, structs, or enums. They have the format:
73 *
74 * type: {The typedef name of the type.}
75 * [version: {Starting API level} [ {Last API level that supports this.}]
76 * [size: {32 or 64. Used if this is available only for 32 or 64 bit code.}]
77 * simple: {The C declaration that this type is the typedef equivalent.}
78 * [hidden:] ...If present, don't document the type. Omit the following two fields.
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -070079 * [deprecated: [{Deprecation message.}] ... This is deprecated. Compiler will issue a wrning.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070080 * summary: {A one line string describing this section.}
81 * description:
82 * {Multiline description. Can include HTML. References to constants, types,
83 * and functions can be created by prefixing with a '@'.}
84 * end:
85 *
86 * type: {The typedef name of the type.}
87 * [version: {Starting API level} [ {Last API level that supports this.}]
88 * [size: {32 or 64. Used if this is available only for 32 or 64 bit code.}]
89 * struct: [{The name that will appear right after the struct keyword}]
90 * field: {Type and name of the field}[, "{One line documentation of the field}"]
91 * field: ... Same for all the other fields of the struct.
92 * [attrib: {Attributes of the struct.}]
93 * [hidden:] ...If present, don't document the type. Omit the following two fields.
94 * summary: {A one line string describing this section.}
95 * description:
96 * {Multiline description. Can include HTML. References to constants, types,
97 * and functions can be created by prefixing with a '@'.}
98 * end:
99 *
100 * type: {The typedef name of the type.}
101 * [version: {Starting API level} [ {Last API level that supports this.}]
102 * [size: {32 or 64. Used if this is available only for 32 or 64 bit code.}]
103 * enum: [{The name that will appear right after the enum keyword}]
104 * value: {Type and name of the field}[, "{One line documentation of the field}"]
105 * value: ... Same for all the other values of the enum.
106 * [hidden:] ...If present, don't document the type. Omit the following two fields.
107 * summary: {A one line string describing this section.}
108 * description:
109 * {Multiline description. Can include HTML. References to constants, types,
110 * and functions can be created by prefixing with a '@'.}
111 * end:
112
113 * Functions have the following format:
114 *
115 * function: {The name of the function.}
116 * [version: {Starting API level} [ {Last API level that supports this.}]
117 * [size: {32 or 64. Used if this is available only for 32 or 64 bit code.}]
118 * [attrib: {Attributes of the function.}]
119 * [w: {A comma separated list of width supported. Only 1, 2, 3, 4 are supported.
120 * [t: {A comma separated list of the types supported.}]]
121 * ... Up to four w: or t: can be defined. The order matter. These will be replace
122 * ... the #1, #2, #3, #4 that can be found in the rest of the specification.
123 * ret: [{The return type} [, "{One line documentation of the return}"]]
124 * [arg: {Type}[, {Name}][, {ParameterEntry.testOption}][, "{One line documentation of the field}"]]
125 * [arg: ... Same for all the other arguments of the function.]
126 * [hidden:] ... If present, don't include in the HTML documentation.
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -0700127 * [deprecated: [{Deprecation message.}] ... This is deprecated. Compiler will issue a wrning.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700128 * summary: {A one line string describing this section.}
129 * description:
130 * {Multiline description. Can include HTML. References to constants, types,
131 * and functions can be created by prefixing with a '@'.}
132 * [inline:
133 * {Multiline code that implements this function inline.}]
134 * [test: {How to test this function. See FunctionSpecification::mTest.}]
135 * end:
136 */
137
138#include <stdio.h>
139#include <cctype>
140#include <cstdlib>
141#include <fstream>
142#include <functional>
143#include <iostream>
144#include <memory>
145#include <sstream>
146#include <strings.h>
147
148#include "Generator.h"
149#include "Scanner.h"
150#include "Specification.h"
151#include "Utilities.h"
152
153using namespace std;
154
155static bool parseCommandLine(int argc, char* argv[], int* versionOfTestFiles,
156 vector<string>* specFileNames) {
157 for (int i = 1; i < argc; i++) {
158 if (argv[i][0] == '-') {
159 if (argv[i][1] == 'v') {
160 i++;
161 if (i < argc) {
162 char* end;
163 *versionOfTestFiles = strtol(argv[i], &end, 10);
164 if (*end != '\0') {
165 cerr << "Error. Can't parse the version number" << argv[i] << "\n";
166 return false;
167 }
168 } else {
169 cerr << "Missing version number after -v\n";
170 return false;
171 }
172 } else {
173 cerr << "Unrecognized flag %s\n" << argv[i] << "\n";
174 return false;
175 }
176 } else {
177 specFileNames->push_back(argv[i]);
178 }
179 }
180 if (specFileNames->size() == 0) {
181 cerr << "No spec file specified\n";
182 return false;
183 }
184 return true;
185}
186
187int main(int argc, char* argv[]) {
188 // If there's no restriction, generated test files for the very highest version.
189 int versionOfTestFiles = 999999;
190 vector<string> specFileNames;
191 if (!parseCommandLine(argc, argv, &versionOfTestFiles, &specFileNames)) {
192 cout << "Usage: gen_runtime spec_file [spec_file...] [-v version_of_test_files]\n";
193 return -1;
194 }
195 bool success = true;
196 for (auto i : specFileNames) {
197 if (!systemSpecification.readSpecFile(i)) {
198 success = false;
199 }
200 }
201 if (success) {
202 success = systemSpecification.generateFiles(versionOfTestFiles);
203 }
204 return success ? 0 : -2;
205}