Skip to content

Commit

Permalink
Add "piqi compile -M", similar to cpp -M
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alavrik committed Aug 14, 2018
1 parent aa4c1cc commit ecc14f0
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 9 deletions.
10 changes: 10 additions & 0 deletions piqi/piqi.piqic.piqi
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
]


Expand Down
9 changes: 8 additions & 1 deletion piqilib/piqi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 22 additions & 6 deletions piqilib/piqi_boot.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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}

7 changes: 6 additions & 1 deletion piqilib/piqi_impl_piqi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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 = [];
Expand Down
7 changes: 6 additions & 1 deletion piqilib/piqi_piqi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -805,14 +808,15 @@ 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
let _custom_field = Piqirun.gen_repeated_field 162247646 gen__word x.Piqi.custom_field in
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
Expand Down Expand Up @@ -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 = [];
Expand Down
26 changes: 26 additions & 0 deletions src/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,16 +49,26 @@ 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: <list of depenencies>', similar to 'cpp -M'"


let speclist = Piqi_compile.getopt_speclist @
[
Main.arg_o;
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 =
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ecc14f0

Please sign in to comment.