From ecc14f003baf00bea275b55aa6ed7f845c5936b8 Mon Sep 17 00:00:00 2001 From: Anton Lavrik Date: Fri, 6 Jul 2018 23:02:23 -0700 Subject: [PATCH] Add "piqi compile -M", similar to cpp -M In addition, put names of all included files into compiled spec generated by "piqi compile". To be used by e.g. "piqic-erlang -M" to generate the full list of build dependencies. --- piqi/piqi.piqic.piqi | 10 ++++++++++ piqilib/piqi.ml | 9 ++++++++- piqilib/piqi_boot.ml | 28 ++++++++++++++++++++++------ piqilib/piqi_impl_piqi.ml | 7 ++++++- piqilib/piqi_piqi.ml | 7 ++++++- src/compile.ml | 26 ++++++++++++++++++++++++++ 6 files changed, 78 insertions(+), 9 deletions(-) diff --git a/piqi/piqi.piqic.piqi b/piqi/piqi.piqic.piqi index 125bfd92..395ad176 100644 --- a/piqi/piqi.piqic.piqi +++ b/piqi/piqi.piqic.piqi @@ -29,6 +29,16 @@ .type string .optional ] + + .with.field [ + % file names of all included Piqi modules + % + % we basically need them for generating "piqi compile -M" output + + .name included-file + .type string + .repeated + ] ] diff --git a/piqilib/piqi.ml b/piqilib/piqi.ml index 0a53ca82..ee82508b 100644 --- a/piqilib/piqi.ml +++ b/piqilib/piqi.ml @@ -2288,7 +2288,14 @@ let piqi_to_piqobj (* piqi compile mode? TODO: this is a hack; we need a more explicit way to * determine whether it is a piqi compile mode *) if add_codes - then piqi_spec.P.file <- piqi.P.file; + then ( + piqi_spec.P.file <- piqi.P.file; + + (* remove the module itself from the list of included deps (it is always + * at the end of the list) *) + let included_piqi = List.filter (fun x -> x != piqi) piqi.P.included_piqi in + piqi_spec.P.included_file <- List.map (fun x-> some_of x.P.file) included_piqi; + ); let ast = piqi_to_ast piqi_spec ~is_external_mode:false in let ast = transform_piqi_ast ast in diff --git a/piqilib/piqi_boot.ml b/piqilib/piqi_boot.ml index 439e882f..1bbbba02 100644 --- a/piqilib/piqi_boot.ml +++ b/piqilib/piqi_boot.ml @@ -974,6 +974,14 @@ let piqi_spec = protobuf_name = None; code = Some 62639740l; protobuf_packed = false; json_name = None; json_omit_missing = None; getopt_letter = None; getopt_doc = None; + internal = false; proto_name = None; wire_packed = false}; + {Piqi_impl_piqi.Field.name = Some "included-file"; + typename = Some "string"; mode = `repeated; default = None; + deprecated = false; piqtype = None; unparsed_piq_ast = None; + piq_format = None; piq_positional = None; piq_flag_default = None; + piq_alias = None; protobuf_name = None; code = Some 35129965l; + protobuf_packed = false; json_name = None; + json_omit_missing = None; getopt_letter = None; getopt_doc = None; internal = false; proto_name = None; wire_packed = false}]; parent = None; wire_field = []; is_func_param = false; unparsed_piq_ast = None; piq_positional = None; @@ -1114,8 +1122,8 @@ let piqi_spec = resolved_func = []; extended_func = []; included_piqi = []; original_piqi = None; ast = None; is_embedded = None; unparsed_piq_ast = None; protobuf_custom = []; protobuf_package = None; - file = None; includ = []; extend = []; proto_custom = []; - proto_package = None} + file = None; included_file = []; includ = []; extend = []; + proto_custom = []; proto_package = None} let piqi_lang = {Piqi_impl_piqi.Piqi.modname = Some "piqi-lang"; @@ -2214,6 +2222,14 @@ let piqi_lang = protobuf_packed = false; json_name = None; json_omit_missing = None; getopt_letter = None; getopt_doc = None; internal = false; proto_name = None; wire_packed = false}; + {Piqi_impl_piqi.Field.name = Some "included-file"; + typename = Some "string"; mode = `repeated; default = None; + deprecated = false; piqtype = None; unparsed_piq_ast = None; + piq_format = None; piq_positional = None; piq_flag_default = None; + piq_alias = None; protobuf_name = None; code = Some 35129965l; + protobuf_packed = false; json_name = None; + json_omit_missing = None; getopt_letter = None; getopt_doc = None; + internal = false; proto_name = None; wire_packed = false}; {Piqi_impl_piqi.Field.name = None; typename = Some "include"; mode = `repeated; default = None; deprecated = false; piqtype = None; unparsed_piq_ast = None; piq_format = None; @@ -2521,8 +2537,8 @@ let piqi_lang = resolved_func = []; extended_func = []; included_piqi = []; original_piqi = None; ast = None; is_embedded = None; unparsed_piq_ast = None; protobuf_custom = []; protobuf_package = None; - file = None; includ = []; extend = []; proto_custom = []; - proto_package = None} + file = None; included_file = []; includ = []; extend = []; + proto_custom = []; proto_package = None} let piq = {Piqi_impl_piqi.Piqi.modname = Some "piq"; @@ -2761,6 +2777,6 @@ let piq = extended_import = []; resolved_func = []; extended_func = []; included_piqi = []; original_piqi = None; ast = None; is_embedded = None; unparsed_piq_ast = None; protobuf_custom = []; protobuf_package = None; - file = None; includ = []; extend = []; proto_custom = []; - proto_package = None} + file = None; included_file = []; includ = []; extend = []; + proto_custom = []; proto_package = None} diff --git a/piqilib/piqi_impl_piqi.ml b/piqilib/piqi_impl_piqi.ml index 8473c18f..40135e5c 100644 --- a/piqilib/piqi_impl_piqi.ml +++ b/piqilib/piqi_impl_piqi.ml @@ -267,6 +267,7 @@ and Piqi: mutable protobuf_custom: string list; mutable protobuf_package: string option; mutable file: string option; + mutable included_file: string list; mutable includ: Piqi_impl_piqi.includ list; mutable extend: Piqi_impl_piqi.extend list; mutable proto_custom: string list; @@ -775,6 +776,7 @@ and parse_piqi x = let _unparsed_piq_ast, x = Piqirun.parse_optional_field 1 parse_uint x in let _ast = None in let _modname, x = Piqirun.parse_optional_field 13841580 parse_word x in + let _included_file, x = Piqirun.parse_repeated_field 35129965 parse_string x in let _imported_typedef = [] in let _file, x = Piqirun.parse_optional_field 62639740 parse_string x in let _extended_func = [] in @@ -803,6 +805,7 @@ and parse_piqi x = Piqi.unparsed_piq_ast = _unparsed_piq_ast; Piqi.ast = _ast; Piqi.modname = _modname; + Piqi.included_file = _included_file; Piqi.imported_typedef = _imported_typedef; Piqi.file = _file; Piqi.extended_func = _extended_func; @@ -1257,6 +1260,7 @@ and gen__piqi code x = refer x; let _unparsed_piq_ast = Piqirun.gen_optional_field 1 gen__uint x.Piqi.unparsed_piq_ast in let _modname = Piqirun.gen_optional_field 13841580 gen__word x.Piqi.modname in + let _included_file = Piqirun.gen_repeated_field 35129965 gen__string x.Piqi.included_file in let _file = Piqirun.gen_optional_field 62639740 gen__string x.Piqi.file in let _protobuf_custom = Piqirun.gen_repeated_field 112352691 gen__string x.Piqi.protobuf_custom in let _extend = Piqirun.gen_repeated_field 119198170 gen__extend x.Piqi.extend in @@ -1268,7 +1272,7 @@ and gen__piqi code x = let _protobuf_package = Piqirun.gen_optional_field 376215364 gen__string x.Piqi.protobuf_package in let _proto_custom = Piqirun.gen_repeated_field 405875126 gen__string x.Piqi.proto_custom in let _typedef = Piqirun.gen_repeated_field 416823115 gen__typedef x.Piqi.typedef in - Piqirun.gen_record code (_unparsed_piq_ast :: _modname :: _file :: _protobuf_custom :: _extend :: _import :: _custom_field :: _includ :: _proto_package :: _func :: _protobuf_package :: _proto_custom :: _typedef :: []) + Piqirun.gen_record code (_unparsed_piq_ast :: _modname :: _included_file :: _file :: _protobuf_custom :: _extend :: _import :: _custom_field :: _includ :: _proto_package :: _func :: _protobuf_package :: _proto_custom :: _typedef :: []) and gen__import code x = refer x; @@ -1546,6 +1550,7 @@ and default_piqi () = Piqi.unparsed_piq_ast = None; Piqi.ast = None; Piqi.modname = None; + Piqi.included_file = []; Piqi.imported_typedef = []; Piqi.file = None; Piqi.extended_func = []; diff --git a/piqilib/piqi_piqi.ml b/piqilib/piqi_piqi.ml index fef548d1..66f53473 100644 --- a/piqilib/piqi_piqi.ml +++ b/piqilib/piqi_piqi.ml @@ -178,6 +178,7 @@ and Piqi: mutable protobuf_custom: string list; mutable protobuf_package: string option; mutable file: string option; + mutable included_file: string list; } end = Piqi and Import: @@ -523,6 +524,7 @@ and parse_piqi_list x = and parse_piqi x = let x = Piqirun.parse_record x in let _modname, x = Piqirun.parse_optional_field 13841580 parse_word x in + let _included_file, x = Piqirun.parse_repeated_field 35129965 parse_string x in let _file, x = Piqirun.parse_optional_field 62639740 parse_string x in let _protobuf_custom, x = Piqirun.parse_repeated_field 112352691 parse_string x in let _import, x = Piqirun.parse_repeated_field 142778725 parse_import x in @@ -533,6 +535,7 @@ and parse_piqi x = Piqirun.check_unparsed_fields x; { Piqi.modname = _modname; + Piqi.included_file = _included_file; Piqi.file = _file; Piqi.protobuf_custom = _protobuf_custom; Piqi.import = _import; @@ -805,6 +808,7 @@ and gen__piqi_list code x = and gen__piqi code x = let _modname = Piqirun.gen_optional_field 13841580 gen__word x.Piqi.modname in + let _included_file = Piqirun.gen_repeated_field 35129965 gen__string x.Piqi.included_file in let _file = Piqirun.gen_optional_field 62639740 gen__string x.Piqi.file in let _protobuf_custom = Piqirun.gen_repeated_field 112352691 gen__string x.Piqi.protobuf_custom in let _import = Piqirun.gen_repeated_field 142778725 gen__import x.Piqi.import in @@ -812,7 +816,7 @@ and gen__piqi code x = let _func = Piqirun.gen_repeated_field 340962072 gen__func x.Piqi.func in let _protobuf_package = Piqirun.gen_optional_field 376215364 gen__string x.Piqi.protobuf_package in let _typedef = Piqirun.gen_repeated_field 416823115 gen__typedef x.Piqi.typedef in - Piqirun.gen_record code (_modname :: _file :: _protobuf_custom :: _import :: _custom_field :: _func :: _protobuf_package :: _typedef :: []) + Piqirun.gen_record code (_modname :: _included_file :: _file :: _protobuf_custom :: _import :: _custom_field :: _func :: _protobuf_package :: _typedef :: []) and gen__import code x = let _modname = Piqirun.gen_required_field 13841580 gen__word x.Import.modname in @@ -992,6 +996,7 @@ and default_piqi_list () = and default_piqi () = { Piqi.modname = None; + Piqi.included_file = []; Piqi.file = None; Piqi.protobuf_custom = []; Piqi.import = []; diff --git a/src/compile.ml b/src/compile.ml index 1433a485..c35d0571 100644 --- a/src/compile.ml +++ b/src/compile.ml @@ -25,6 +25,7 @@ open C let input_format = ref "" let output_format = ref "" let input_self_spec = ref "" +let flag_M = ref false (* NOTE: "piqi compile" command-line interface should be idealy as stable @@ -48,6 +49,10 @@ let arg__self_spec = "--self-spec", Arg.Set_string input_self_spec, "<.pb file> input self-spec in .pb format; use '-' for stdin" +let arg_M = + "-M", Arg.Set flag_M, + "generate one line with 'filename: ', similar to 'cpp -M'" + let speclist = Piqi_compile.getopt_speclist @ [ @@ -55,9 +60,15 @@ let speclist = Piqi_compile.getopt_speclist @ arg_f; arg_t; arg__self_spec; + arg_M; ] +let escape_makefile_filename s = + (* TODO, XXX: escape special characters or at least spaces in file names *) + s + + let compile self_spec piqi och = trace "getting all imported dependencies\n"; let piqi_list = @@ -89,6 +100,21 @@ let compile self_spec piqi och = piqi_list in trace "writing output\n"; + + if !flag_M then ( + let deps = Piqi.get_piqi_deps piqi ~only_imports:false in + (* remove the module itself from the list of included deps (it is always + * at the end of the list) *) + let deps = List.filter (fun x -> x != piqi) deps in + let output_piqi_filename piqi = + output_string och (escape_makefile_filename (some_of piqi.P.file)) + in + output_piqi_filename piqi; + output_string och ": "; + List.iter (fun x -> output_piqi_filename x; output_string och " ") deps; + ); + + if not !flag_M then match !output_format with | "piq" | "" -> (* NOTE: instead of creating piqi-list, writing modules in regular .piq