42
42
key () ->
43
43
gpb .
44
44
45
- proto_compile (Config , _AppFile , _ProtoFiles ) ->
45
+ proto_compile (Config , _AppFile , ProtoFiles ) ->
46
46
% % Check for gpb library -- if it's not present, fail
47
47
% % since we have.proto files that need building
48
48
case gpb_is_present () of
49
49
true ->
50
- rebar_base_compiler :run (Config , [],
51
- " src" , " .proto" ,
52
- " src" , " .erl" ,
53
- fun compile_gpb /3 ,
54
- [{check_last_mod , true }]);
50
+ UserGpbOpts = user_gpb_opts (Config ),
51
+ lists :foreach (
52
+ fun (ProtoFile ) ->
53
+ GpbOpts = UserGpbOpts ++ default_dest_opts ()
54
+ ++ default_include_opts (ProtoFile ),
55
+
56
+ case needs_compile (ProtoFile , GpbOpts ) of
57
+ true ->
58
+ compile_gpb (ProtoFile , GpbOpts );
59
+ false ->
60
+ ok
61
+ end
62
+ end ,
63
+ ProtoFiles );
55
64
false ->
56
65
? ERROR (" The gpb library is not present in code path!\n " , []),
57
66
? FAIL
58
67
end .
59
68
60
69
proto_clean (Config , _AppFile , ProtoFiles ) ->
61
- GpbOpts = gpb_opts (Config ),
62
- MPrefix = proplists :get_value (module_name_prefix , GpbOpts , " " ),
63
- MSuffix = proplists :get_value (module_name_suffix , GpbOpts , " " ),
70
+ GpbOpts = user_gpb_opts (Config ) ++ default_dest_opts (),
64
71
rebar_file_utils :delete_each (
65
- [beam_relpath ( MPrefix , F , MSuffix ) || F <- ProtoFiles ]
66
- ++ [erl_relpath ( MPrefix , F , MSuffix ) || F <- ProtoFiles ]
67
- ++ [hrl_relpath ( MPrefix , F , MSuffix ) || F <- ProtoFiles ]),
72
+ [beam_file ( F , GpbOpts ) || F <- ProtoFiles ]
73
+ ++ [erl_file ( F , GpbOpts ) || F <- ProtoFiles ]
74
+ ++ [hrl_file ( F , GpbOpts ) || F <- ProtoFiles ]),
68
75
ok .
69
76
70
77
% % ===================================================================
@@ -82,17 +89,27 @@ proto_info(help, compile) ->
82
89
proto_info (help , clean ) ->
83
90
? CONSOLE (" " , []).
84
91
85
- gpb_opts (Config ) ->
86
- rebar_config :get_local (Config , gpb_opts , []).
87
-
88
92
gpb_is_present () ->
89
93
code :which (gpb ) =/= non_existing .
90
94
91
- compile_gpb (Source , _Target , Config ) ->
95
+ user_gpb_opts (Config ) ->
96
+ rebar_config :get_local (Config , gpb_opts , []).
97
+
98
+ default_dest_opts () ->
99
+ [{o_erl , " src" }, {o_hrl , " include" }].
100
+
101
+ default_include_opts (Source ) ->
102
+ SourceFullPath = filename :absname (Source ),
103
+ [{i ,filename :dirname (SourceFullPath )}].
104
+
105
+ needs_compile (ProtoFile , GpbOpts ) ->
106
+ Erl = erl_file (ProtoFile , GpbOpts ),
107
+ Hrl = hrl_file (ProtoFile , GpbOpts ),
108
+ filelib :last_modified (Erl ) < filelib :last_modified (ProtoFile ) orelse
109
+ filelib :last_modified (Hrl ) < filelib :last_modified (ProtoFile ).
110
+
111
+ compile_gpb (Source , GpbOpts ) ->
92
112
SourceFullPath = filename :absname (Source ),
93
- DefaultDestOpts = [{o_erl , " src" }, {o_hrl , " include" }],
94
- SelfIncludeOpt = [{i ,filename :dirname (SourceFullPath )}],
95
- GpbOpts = gpb_opts (Config ) ++ DefaultDestOpts ++ SelfIncludeOpt ,
96
113
ok = filelib :ensure_dir (filename :join (" ebin" , " dummy" )),
97
114
ok = filelib :ensure_dir (filename :join (" include" , " dummy" )),
98
115
? CONSOLE (" Compiling ~s \n " , [Source ]),
@@ -104,16 +121,28 @@ compile_gpb(Source, _Target, Config) ->
104
121
? FAIL
105
122
end .
106
123
107
- beam_relpath ( Prefix , Proto , Suffix ) ->
108
- proto_filename_to_relpath (" ebin" , Prefix , Proto , Suffix , " .beam" ).
124
+ beam_file ( ProtoFile , GpbOpts ) ->
125
+ proto_filename_to_path (" ebin" , ProtoFile , " .beam" , GpbOpts ).
109
126
110
- erl_relpath (Prefix , Proto , Suffix ) ->
111
- proto_filename_to_relpath (" src" , Prefix , Proto , Suffix , " .erl" ).
127
+ erl_file (ProtoFile , GpbOpts ) ->
128
+ ErlOutDir = get_erl_outdir (GpbOpts ),
129
+ proto_filename_to_path (ErlOutDir , ProtoFile , " .erl" , GpbOpts ).
112
130
113
- hrl_relpath (Prefix , Proto , Suffix ) ->
114
- proto_filename_to_relpath (" include" , Prefix , Proto , Suffix , " .hrl" ).
131
+ hrl_file (ProtoFile , GpbOpts ) ->
132
+ HrlOutDir = get_hrl_outdir (GpbOpts ),
133
+ proto_filename_to_path (HrlOutDir , ProtoFile , " .hrl" , GpbOpts ).
115
134
116
- proto_filename_to_relpath (Dir , Prefix , Proto , Suffix , NewExt ) ->
117
- BaseNoExt = filename :basename (Proto , " .proto" ),
135
+ proto_filename_to_path (Dir , ProtoFile , NewExt , GpbOpts ) ->
136
+ BaseNoExt = filename :basename (ProtoFile , " .proto" ),
137
+ Prefix = proplists :get_value (module_name_prefix , GpbOpts , " " ),
138
+ Suffix = proplists :get_value (module_name_suffix , GpbOpts , " " ),
118
139
filename :join ([Dir , Prefix ++ BaseNoExt ++ Suffix ++ NewExt ]).
119
140
141
+ get_erl_outdir (Opts ) ->
142
+ proplists :get_value (o_erl , Opts , get_outdir (Opts )).
143
+
144
+ get_hrl_outdir (Opts ) ->
145
+ proplists :get_value (o_hrl , Opts , get_outdir (Opts )).
146
+
147
+ get_outdir (Opts ) ->
148
+ proplists :get_value (o , Opts , " ." ).
0 commit comments