3 $Id: readmakefile.y 23128 2016-07-01 11:58:21Z coelho $
5 Copyright 1989-2016 MINES ParisTech
7 This file is part of PIPS.
9 PIPS is free software: you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
14 PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with PIPS. If not, see <http://www.gnu.org/licenses/>.
25 /* Some modifications are made to save the current makefile (s.a. files
26 * pipsmake/readmakefile.y pipsmake.h )
28 * They only occure between following tags:
41 %token COMPILATION_UNIT
46 %token PRE_TRANSFORMATION
47 %token POST_TRANSFORMATION
54 %type <virtual> virtual
62 #include "pips_config.h"
76 #include "properties.h"
77 #include "top-level.h"
82 static makefile pipsmakefile = makefile_undefined;
83 static hash_table activated = NULL;
89 virtual_resource virtual;
100 pipsmakefile = make_makefile(NIL, NIL);
102 hash_table_free(activated), activated=NULL;
103 activated = hash_table_make(hash_string, 0);
108 { rule_phase($2) = $1; $$ = $2; }
111 deps: deps dir virtuals
113 if ($2 == REQUIRED) {
115 gen_nconc(rule_required($1), $3);
117 else if ($2 == PRODUCED) {
119 gen_nconc(rule_produced($1), $3);
121 else if ($2 == PRESERVED) {
123 gen_nconc(rule_preserved($1), $3);
125 else if ($2 == MODIFIED) {
127 gen_nconc(rule_modified($1), $3);
129 else if ($2 == PRE_TRANSFORMATION) {
130 rule_pre_transformation($1) =
131 gen_nconc(rule_pre_transformation($1), $3);
133 else if ($2 == POST_TRANSFORMATION) {
134 rule_post_transformation($1) =
135 gen_nconc(rule_post_transformation($1), $3);
138 pips_internal_error("unknown dir: %d", $2);
143 { $$ = make_rule(string_undefined, NIL, NIL, NIL, NIL, NIL,NIL); }
155 { $$ = PRE_TRANSFORMATION; }
156 | POST_TRANSFORMATION
157 { $$ = POST_TRANSFORMATION; }
160 virtuals: virtuals virtual
161 { $$ = gen_nconc($1, CONS(VIRTUAL_RESOURCE, $2, NIL)); }
166 virtual: owner DOT resource
167 { $$ = make_virtual_resource($3, $1); }
171 { $$ = make_owner(is_owner_program, UU); }
173 { $$ = make_owner(is_owner_module, UU); }
175 { $$ = make_owner(is_owner_main, UU); }
178 /*$$ = make_owner(is_owner_common, UU);*/
182 { $$ = make_owner(is_owner_callees, UU); }
184 { $$ = make_owner(is_owner_callers, UU); }
186 { $$ = make_owner(is_owner_all, UU); }
188 { $$ = make_owner(is_owner_select, UU); }
190 { $$ = make_owner(is_owner_compilation_unit, UU); }
194 { $$ = strupper($1, $1); }
198 { $$ = strupper($1, $1); }
202 void yyerror_lex_part(const char *);
203 void yyerror(const char * s)
207 fprintf(stderr, "[readmakefile] unparsed text:\n");
208 while ((c = getc(pipsmake_in)) != EOF) putc(c, stderr);
209 pips_internal_error("reading pips makefile failed");
213 fprint_virtual_resources(FILE *fd, const char* dir, list lrv)
215 FOREACH(VIRTUAL_RESOURCE, vr, lrv)
217 owner o = virtual_resource_owner(vr);
218 string n = virtual_resource_name(vr);
220 switch (owner_tag(o)) {
221 case is_owner_program:
222 fprintf(fd, " %s program.%s\n", dir, n);
224 case is_owner_module:
225 fprintf(fd, " %s module.%s\n", dir, n);
228 fprintf(fd, " %s main.%s\n", dir, n);
230 case is_owner_callees:
231 fprintf(fd, " %s callees.%s\n", dir, n);
233 case is_owner_callers:
234 fprintf(fd, " %s callers.%s\n", dir, n);
237 fprintf(fd, " %s all.%s\n", dir, n);
239 case is_owner_select:
240 fprintf(fd, " %s select.%s\n", dir, n);
242 case is_owner_compilation_unit:
243 fprintf(fd, " %s compilation_unit.%s\n", dir, n);
246 pips_internal_error("bad owner tag (%d)\n", owner_tag(o));
251 void fprint_makefile(FILE *fd, makefile m)
253 FOREACH(RULE, r, makefile_rules(m))
255 fprintf(fd, "%s\n", rule_phase(r));
256 fprint_virtual_resources(fd, "\t!", rule_pre_transformation(r));
257 fprint_virtual_resources(fd, "\t<", rule_required(r));
258 fprint_virtual_resources(fd, "\t>", rule_produced(r));
259 fprint_virtual_resources(fd, "\t=", rule_preserved(r));
260 fprint_virtual_resources(fd, "\t#", rule_modified(r));
268 if (pipsmakefile == makefile_undefined)
270 debug_on("PIPSMAKE_DEBUG_LEVEL");
272 pipsmake_in = fopen_config(PIPSMAKE_RC, NULL, "PIPS_PIPSMAKERC");
276 safe_fclose(pipsmake_in, "PIPS_PIPSMAKERC");
278 ifdebug(8) fprint_makefile(stderr, pipsmakefile);
287 /* this function returns the rule that defines builder pname */
288 rule find_rule_by_phase(const char* pname)
290 rule fr = rule_undefined;
291 makefile m = parse_makefile();
293 pips_debug(9, "searching rule for phase %s\n", pname);
295 /* walking thru rules */
296 FOREACH(RULE, r, makefile_rules(m)) {
297 if (same_string_p(rule_phase(r), pname)) {
306 void add_rule(rule r)
308 string pn = rule_phase(r);
309 bool active_phase = false;
311 // Check resources produced by this rule
312 FOREACH(VIRTUAL_RESOURCE, vr, rule_produced(r)) {
313 string vrn = virtual_resource_name(vr);
316 // We activated this rule to produce this resource only if
317 // this resource is not already produced
318 if ((phase = hash_get(activated, vrn)) == HASH_UNDEFINED_VALUE) {
319 pips_debug(1, "Default function for %s is %s\n", vrn, pn);
322 hash_put(activated, vrn, pn);
324 makefile_active_phases(pipsmakefile) =
325 CONS(STRING, strdup(pn), makefile_active_phases(pipsmakefile));
327 else pips_debug(1, "Function %s not activated\n", pn);
330 // Check resources required for this rule if it is an active one
332 FOREACH(VIRTUAL_RESOURCE, vr, rule_required(r)) {
333 string vrn = virtual_resource_name(vr);
334 owner vro = virtual_resource_owner(vr);
337 // We must use a resource already defined
338 if ( owner_callers_p(vro) || owner_callees_p(vro) ) {}
340 phase = hash_get(activated, vrn);
341 if (phase == HASH_UNDEFINED_VALUE)
343 if (get_bool_property("PIPSMAKE_WARNINGS"))
345 "%s: phase %s requires an undefined resource %s\n",
346 PIPSMAKE_RC, pn, vrn);
348 // If we use a resource, another function should have produced it
349 else if (strcmp(phase, pn) == 0) {
351 "%s: phase %s cannot be active for the %s resource\n",
352 PIPSMAKE_RC, phase, vrn);
355 pips_debug(1, "Required resource %s is checked OK for Function %s\n",
361 // FI: no check of rule name unicity; e.g. double declaration of sesamify
362 rule or = find_rule_by_phase(rule_phase(r));
363 if(rule_undefined_p(or))
364 makefile_rules(pipsmakefile) = gen_nconc(makefile_rules(pipsmakefile),
367 pips_internal_error("Double declaration of phase \"%s\"\n"
368 "Check pipsmake-rc.tex or pipsmake.rc\n",
372 makefile open_makefile(const char* name)
377 mkf_name = build_pgm_makefile(name);
378 fd = fopen(mkf_name, "r");
380 if (!makefile_undefined_p(pipsmakefile))
382 free_makefile(pipsmakefile);
383 pipsmakefile = makefile_undefined;
384 pips_debug(1, "current makefile erased\n");
389 pipsmakefile = read_makefile(fd);
390 safe_fclose(fd, mkf_name);
394 pips_user_warning("pipsmake file not found in database...\n");
395 pipsmakefile = parse_makefile();
403 save_makefile(const char* name)
405 char * mkf_name = build_pgm_makefile(name);
406 FILE * fd = safe_fopen(mkf_name, "w");
407 write_makefile(fd, pipsmakefile);
408 safe_fclose(fd, mkf_name);
409 pips_debug(1, "makefile written on %s\n", mkf_name);
414 close_makefile(const char* name)
417 free_makefile(pipsmakefile), pipsmakefile = makefile_undefined;