8
8
#
9
9
# ==-------------------------------------------------------------------------==#
10
10
11
-
12
11
import yaml
13
12
import argparse
14
-
15
13
from pathlib import Path
16
14
from header import HeaderFile
15
+ from gpu_headers import GpuHeaderFile as GpuHeader
17
16
from class_implementation .classes .macro import Macro
18
17
from class_implementation .classes .type import Type
19
18
from class_implementation .classes .function import Function
22
21
from class_implementation .classes .object import Object
23
22
24
23
25
- def yaml_to_classes (yaml_data ):
24
+ def yaml_to_classes (yaml_data , header_class , entry_points = None ):
26
25
"""
27
26
Convert YAML data to header classes.
28
27
29
28
Args:
30
29
yaml_data: The YAML data containing header specifications.
30
+ header_class: The class to use for creating the header.
31
+ entry_points: A list of specific function names to include in the header.
31
32
32
33
Returns:
33
34
HeaderFile: An instance of HeaderFile populated with the data.
34
35
"""
35
36
header_name = yaml_data .get ("header" )
36
- header = HeaderFile (header_name )
37
+ header = header_class (header_name )
37
38
38
39
for macro_data in yaml_data .get ("macros" , []):
39
40
header .add_macro (Macro (macro_data ["macro_name" ], macro_data ["macro_value" ]))
@@ -49,12 +50,15 @@ def yaml_to_classes(yaml_data):
49
50
)
50
51
51
52
functions = yaml_data .get ("functions" , [])
53
+ if entry_points :
54
+ entry_points_set = set (entry_points )
55
+ functions = [f for f in functions if f ["name" ] in entry_points_set ]
52
56
sorted_functions = sorted (functions , key = lambda x : x ["name" ])
53
57
guards = []
54
58
guarded_function_dict = {}
55
59
for function_data in sorted_functions :
56
60
guard = function_data .get ("guard" , None )
57
- if guard == None :
61
+ if guard is None :
58
62
arguments = [arg ["type" ] for arg in function_data ["arguments" ]]
59
63
attributes = function_data .get ("attributes" , None )
60
64
standards = function_data .get ("standards" , None )
@@ -105,19 +109,21 @@ def yaml_to_classes(yaml_data):
105
109
return header
106
110
107
111
108
- def load_yaml_file (yaml_file ):
112
+ def load_yaml_file (yaml_file , header_class , entry_points ):
109
113
"""
110
114
Load YAML file and convert it to header classes.
111
115
112
116
Args:
113
- yaml_file: The path to the YAML file.
117
+ yaml_file: Path to the YAML file.
118
+ header_class: The class to use for creating the header (HeaderFile or GpuHeader).
119
+ entry_points: A list of specific function names to include in the header.
114
120
115
121
Returns:
116
- HeaderFile: An instance of HeaderFile populated with the data from the YAML file .
122
+ HeaderFile: An instance of HeaderFile populated with the data.
117
123
"""
118
124
with open (yaml_file , "r" ) as f :
119
125
yaml_data = yaml .safe_load (f )
120
- return yaml_to_classes (yaml_data )
126
+ return yaml_to_classes (yaml_data , header_class , entry_points )
121
127
122
128
123
129
def fill_public_api (header_str , h_def_content ):
@@ -207,7 +213,14 @@ def increase_indent(self, flow=False, indentless=False):
207
213
print (f"Added function { new_function .name } to { yaml_file } " )
208
214
209
215
210
- def main (yaml_file , h_def_file , output_dir , add_function = None ):
216
+ def main (
217
+ yaml_file ,
218
+ output_dir = None ,
219
+ h_def_file = None ,
220
+ add_function = None ,
221
+ entry_points = None ,
222
+ export_decls = False ,
223
+ ):
211
224
"""
212
225
Main function to generate header files from YAML and .h.def templates.
213
226
@@ -216,41 +229,50 @@ def main(yaml_file, h_def_file, output_dir, add_function=None):
216
229
h_def_file: Path to the .h.def template file.
217
230
output_dir: Directory to output the generated header file.
218
231
add_function: Details of the function to be added to the YAML file (if any).
232
+ entry_points: A list of specific function names to include in the header.
233
+ export_decls: Flag to use GpuHeader for exporting declarations.
219
234
"""
220
-
221
235
if add_function :
222
236
add_function_to_yaml (yaml_file , add_function )
223
237
224
- header = load_yaml_file (yaml_file )
225
-
226
- with open (h_def_file , "r" ) as f :
227
- h_def_content = f .read ()
238
+ header_class = GpuHeader if export_decls else HeaderFile
239
+ header = load_yaml_file (yaml_file , header_class , entry_points )
228
240
229
241
header_str = str (header )
230
- final_header_content = fill_public_api (header_str , h_def_content )
231
242
232
- output_file_name = Path (h_def_file ).stem
233
- output_file_path = Path (output_dir ) / output_file_name
234
-
235
- with open (output_file_path , "w" ) as f :
236
- f .write (final_header_content )
243
+ if output_dir :
244
+ output_file_path = Path (output_dir )
245
+ if output_file_path .is_dir ():
246
+ output_file_path /= f"{ Path (yaml_file ).stem } .h"
247
+ else :
248
+ output_file_path = Path (f"{ Path (yaml_file ).stem } .h" )
249
+
250
+ if not export_decls and h_def_file :
251
+ with open (h_def_file , "r" ) as f :
252
+ h_def_content = f .read ()
253
+ final_header_content = fill_public_api (header_str , h_def_content )
254
+ with open (output_file_path , "w" ) as f :
255
+ f .write (final_header_content )
256
+ else :
257
+ with open (output_file_path , "w" ) as f :
258
+ f .write (header_str )
237
259
238
260
print (f"Generated header file: { output_file_path } " )
239
261
240
262
241
263
if __name__ == "__main__" :
242
- parser = argparse .ArgumentParser (
243
- description = "Generate header files from YAML and .h.def templates"
244
- )
264
+ parser = argparse .ArgumentParser (description = "Generate header files from YAML" )
245
265
parser .add_argument (
246
266
"yaml_file" , help = "Path to the YAML file containing header specification"
247
267
)
248
- parser .add_argument ("h_def_file" , help = "Path to the .h.def template file" )
249
268
parser .add_argument (
250
269
"--output_dir" ,
251
- default = "." ,
252
270
help = "Directory to output the generated header file" ,
253
271
)
272
+ parser .add_argument (
273
+ "--h_def_file" ,
274
+ help = "Path to the .h.def template file (required if not using --export_decls)" ,
275
+ )
254
276
parser .add_argument (
255
277
"--add_function" ,
256
278
nargs = 6 ,
@@ -264,6 +286,21 @@ def main(yaml_file, h_def_file, output_dir, add_function=None):
264
286
),
265
287
help = "Add a function to the YAML file" ,
266
288
)
289
+ parser .add_argument (
290
+ "--e" , action = "append" , help = "Entry point to include" , dest = "entry_points"
291
+ )
292
+ parser .add_argument (
293
+ "--export-decls" ,
294
+ action = "store_true" ,
295
+ help = "Flag to use GpuHeader for exporting declarations" ,
296
+ )
267
297
args = parser .parse_args ()
268
298
269
- main (args .yaml_file , args .h_def_file , args .output_dir , args .add_function )
299
+ main (
300
+ args .yaml_file ,
301
+ args .output_dir ,
302
+ args .h_def_file ,
303
+ args .add_function ,
304
+ args .entry_points ,
305
+ args .export_decls ,
306
+ )
0 commit comments