27
27
28
28
Directory <tpl-src> must contain:
29
29
- options_template.cpp
30
+ - options_public_template.cpp
30
31
- module_template.cpp
31
32
- module_template.h
32
33
@@ -183,6 +184,11 @@ def concat_format(s, objs):
183
184
return '\n ' .join ([s .format (** o .__dict__ ) for o in objs ])
184
185
185
186
187
+ def get_module_headers (modules ):
188
+ """Render includes for module headers"""
189
+ return concat_format ('#include "{header}"' , modules )
190
+
191
+
186
192
def get_holder_fwd_decls (modules ):
187
193
"""Render forward declaration of holder structs"""
188
194
return concat_format (' struct Holder{id_cap};' , modules )
@@ -237,6 +243,19 @@ def get_predicates(option):
237
243
return ['opts.handler().{}("{}", option, value);' .format (x , optname )
238
244
for x in option .predicates ]
239
245
246
+
247
+ def get_getall (module , option ):
248
+ """Render snippet to add option to result of getAll()"""
249
+ if option .type == 'bool' :
250
+ return 'res.push_back({{"{}", opts.{}.{} ? "true" : "false"}});' .format (
251
+ option .long_name , module .id , option .name )
252
+ elif is_numeric_cpp_type (option .type ):
253
+ return 'res.push_back({{"{}", std::to_string(opts.{}.{})}});' .format (
254
+ option .long_name , module .id , option .name )
255
+ else :
256
+ return '{{ std::stringstream ss; ss << opts.{}.{}; res.push_back({{"{}", ss.str()}}); }}' .format (
257
+ module .id , option .name , option .long_name )
258
+
240
259
class Module (object ):
241
260
"""Options module.
242
261
@@ -705,25 +724,26 @@ def add_getopt_long(long_name, argument_req, getopt_long):
705
724
'required' if argument_req else 'no' , value ))
706
725
707
726
708
- def codegen_all_modules (modules , build_dir , dst_dir , tpl_options_h , tpl_options_cpp ):
727
+ def codegen_all_modules (modules , build_dir , dst_dir , tpl_options_h ,
728
+ tpl_options_cpp , tpl_options_public ):
709
729
"""
710
- Generate code for all option modules (options.cpp, options_holder.h ).
730
+ Generate code for all option modules (options.cpp).
711
731
"""
712
732
713
733
headers_module = [] # generated *_options.h header includes
714
734
headers_handler = set () # option includes (for handlers, predicates, ...)
715
735
getopt_short = [] # short options for getopt_long
716
736
getopt_long = [] # long options for getopt_long
717
- options_smt = [] # all options names accessible via {set,get}-option
737
+ options_getall = [] # options for options::getAll()
718
738
options_getoptions = [] # options for Options::getOptions()
719
739
options_handler = [] # option handler calls
720
- defaults = [] # default values
721
- custom_handlers = [] # custom handler implementations assign/assignBool
722
740
help_common = [] # help text for all common options
723
741
help_others = [] # help text for all non-common options
724
742
setoption_handlers = [] # handlers for set-option command
725
743
getoption_handlers = [] # handlers for get-option command
726
744
745
+ assign_impls = []
746
+
727
747
sphinxgen = SphinxGenerator ()
728
748
729
749
for module in modules :
@@ -809,7 +829,7 @@ def codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h, tpl_options_
809
829
cond = ' || ' .join (
810
830
['key == "{}"' .format (x ) for x in sorted (keys )])
811
831
812
- setoption_handlers .append ('if ({}) {{' .format (cond ))
832
+ setoption_handlers .append (' if ({}) {{' .format (cond ))
813
833
if option .type == 'bool' :
814
834
setoption_handlers .append (
815
835
TPL_CALL_ASSIGN_BOOL .format (
@@ -824,15 +844,15 @@ def codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h, tpl_options_
824
844
name = option .name ,
825
845
option = 'key' ))
826
846
elif option .handler :
827
- h = 'handler-> {handler}("{smtname}", key'
847
+ h = ' opts. handler(). {handler}("{smtname}", key'
828
848
if argument_req :
829
849
h += ', optionarg'
830
850
h += ');'
831
851
setoption_handlers .append (
832
852
h .format (handler = option .handler , smtname = option .long_name ))
833
853
834
- setoption_handlers .append ('return;' )
835
- setoption_handlers .append ('}' )
854
+ setoption_handlers .append (' return;' )
855
+ setoption_handlers .append (' }' )
836
856
837
857
if option .name :
838
858
getoption_handlers .append (
@@ -880,80 +900,51 @@ def codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h, tpl_options_
880
900
cases .append (' break;' )
881
901
options_handler .extend (cases )
882
902
883
- optname = option .long
884
- # collect options available to the SMT-frontend
885
- if optname :
886
- options_smt .append ('"{}",' .format (optname ))
887
-
888
903
if option .name :
889
904
# Build options for options::getOptions()
890
- if optname :
891
- # collect SMT option names
892
- options_smt .append ('"{}",' .format (optname ))
893
-
894
- if option .type == 'bool' :
895
- s = 'opts.push_back({{"{}", {}.{} ? "true" : "false"}});' .format (
896
- optname , module .id , option .name )
897
- elif is_numeric_cpp_type (option .type ):
898
- s = 'opts.push_back({{"{}", std::to_string({}.{})}});' .format (
899
- optname , module .id , option .name )
900
- else :
901
- s = '{{ std::stringstream ss; ss << {}.{}; opts.push_back({{"{}", ss.str()}}); }}' .format (
902
- module .id , option .name , optname )
903
- options_getoptions .append (s )
905
+ if option .long_name :
906
+ options_getall .append (get_getall (module , option ))
904
907
905
908
906
909
# Define handler assign/assignBool
907
910
if not mode_handler :
908
911
if option .type == 'bool' :
909
- custom_handlers .append (TPL_ASSIGN_BOOL .format (
912
+ assign_impls .append (TPL_ASSIGN_BOOL .format (
910
913
module = module .id ,
911
914
name = option .name ,
912
915
handler = handler ,
913
916
predicates = '\n ' .join (predicates )
914
917
))
915
918
elif option .short or option .long :
916
- custom_handlers .append (TPL_ASSIGN .format (
919
+ assign_impls .append (TPL_ASSIGN .format (
917
920
module = module .id ,
918
921
name = option .name ,
919
922
handler = handler ,
920
923
predicates = '\n ' .join (predicates )
921
924
))
922
925
923
- # Default option values
924
- default = option .default if option .default else ''
925
- # Prepend enum name
926
- if option .mode and option .type not in default :
927
- default = '{}::{}' .format (option .type , default )
928
- defaults .append ('{}({})' .format (option .name , default ))
929
- defaults .append ('{}WasSetByUser(false)' .format (option .name ))
930
-
931
- write_file (dst_dir , 'options.h' , tpl_options_h .format (
932
- holder_fwd_decls = get_holder_fwd_decls (modules ),
933
- holder_mem_decls = get_holder_mem_decls (modules ),
934
- holder_ref_decls = get_holder_ref_decls (modules ),
935
- ))
936
-
937
- write_file (dst_dir , 'options.cpp' , tpl_options_cpp .format (
938
- headers_module = '\n ' .join (headers_module ),
939
- headers_handler = '\n ' .join (sorted (list (headers_handler ))),
940
- holder_mem_copy = get_holder_mem_copy (modules ),
941
- holder_mem_inits = get_holder_mem_inits (modules ),
942
- holder_ref_inits = get_holder_ref_inits (modules ),
943
- custom_handlers = '\n ' .join (custom_handlers ),
944
- module_defaults = ',\n ' .join (defaults ),
945
- help_common = '\n ' .join (help_common ),
946
- help_others = '\n ' .join (help_others ),
947
- cmdline_options = '\n ' .join (getopt_long ),
948
- options_short = '' .join (getopt_short ),
949
- options_handler = '\n ' .join (options_handler ),
950
- option_value_begin = g_getopt_long_start ,
951
- option_value_end = g_getopt_long_start + len (getopt_long ),
952
- options_smt = '\n ' .join (options_smt ),
953
- options_getoptions = '\n ' .join (options_getoptions ),
954
- setoption_handlers = '\n ' .join (setoption_handlers ),
955
- getoption_handlers = '\n ' .join (getoption_handlers )
956
- ))
926
+ data = {
927
+ 'holder_fwd_decls' : get_holder_fwd_decls (modules ),
928
+ 'holder_mem_decls' : get_holder_mem_decls (modules ),
929
+ 'holder_ref_decls' : get_holder_ref_decls (modules ),
930
+ 'headers_module' : get_module_headers (modules ),
931
+ 'headers_handler' : '\n ' .join (sorted (list (headers_handler ))),
932
+ 'holder_mem_inits' : get_holder_mem_inits (modules ),
933
+ 'holder_ref_inits' : get_holder_ref_inits (modules ),
934
+ 'holder_mem_copy' : get_holder_mem_copy (modules ),
935
+ 'cmdline_options' : '\n ' .join (getopt_long ),
936
+ 'help_common' : '\n ' .join (help_common ),
937
+ 'help_others' : '\n ' .join (help_others ),
938
+ 'options_handler' : '\n ' .join (options_handler ),
939
+ 'options_short' : '' .join (getopt_short ),
940
+ 'assigns' : '\n ' .join (assign_impls ),
941
+ 'options_getall' : '\n ' .join (options_getall ),
942
+ 'getoption_handlers' : '\n ' .join (getoption_handlers ),
943
+ 'setoption_handlers' : '\n ' .join (setoption_handlers ),
944
+ }
945
+ write_file (dst_dir , 'options.h' , tpl_options_h .format (** data ))
946
+ write_file (dst_dir , 'options.cpp' , tpl_options_cpp .format (** data ))
947
+ write_file (dst_dir , 'options_public.cpp' , tpl_options_public .format (** data ))
957
948
958
949
if os .path .isdir ('{}/docs/' .format (build_dir )):
959
950
sphinxgen .render ('{}/docs/' .format (build_dir ), 'options_generated.rst' )
@@ -1093,6 +1084,7 @@ def mkoptions_main():
1093
1084
# Read source code template files from source directory.
1094
1085
tpl_module_h = read_tpl (src_dir , 'module_template.h' )
1095
1086
tpl_module_cpp = read_tpl (src_dir , 'module_template.cpp' )
1087
+ tpl_options_public = read_tpl (src_dir , 'options_public_template.cpp' )
1096
1088
tpl_options_h = read_tpl (src_dir , 'options_template.h' )
1097
1089
tpl_options_cpp = read_tpl (src_dir , 'options_template.cpp' )
1098
1090
@@ -1113,8 +1105,7 @@ def mkoptions_main():
1113
1105
codegen_module (module , dst_dir , tpl_module_h , tpl_module_cpp )
1114
1106
1115
1107
# Create options.cpp in destination directory
1116
- codegen_all_modules (modules , build_dir , dst_dir , tpl_options_h , tpl_options_cpp )
1117
-
1108
+ codegen_all_modules (modules , build_dir , dst_dir , tpl_options_h , tpl_options_cpp , tpl_options_public )
1118
1109
1119
1110
1120
1111
if __name__ == "__main__" :
0 commit comments