Skip to content

Commit

Permalink
mamake: add nice PS4 to shell actions, demystifying log output
Browse files Browse the repository at this point in the history
The shell action trace in the log file has always been hard to
follow. Shell action commands are traced without any context. For
people unfamiliar with the build system (currently everyone but a
handful of people), it is unclear where these come from.

mamake now sets a PS4 tracing prompt for each shell action that
shows the Mamfile that the shell action originates from as well as
the line number range of the corresponding make...done block.

It would be nice if we could trace the exact line number of each
'exec' statement, but that is not possible with the basic design of
this system; 'exec' statements are combined into a script buffer
and that is executed as a whole upon reaching 'done'. Since 'exec'
statements can begin at any point in the block and need not even be
contiguous to belong to the same script, exact line number
information is lost.

However, showing the make...done block range also has advantages;
it makes it more intuitive to understand the build system. Here is
a sample fragment:

+ (libast/Mamfile:239-242) /bin/cp -f FEATURE/sys ast_sys.h
+ (libast/Mamfile:158-250) iffe -v -X ast -X std -c 'cc  -D_BL[...]
iffe: test results in FEATURE/stdio are up to date
+ (libast/Mamfile:157-254) /bin/cp -f FEATURE/stdio ast_stdio.h

Note how the line number ranges show three different shell scripts
belonging to three nested blocks.

src/cmd/INIT/mamake.c:
- Add a 'line' member to the Rule_t struct.
- rule(): When defining a rule r (e.g. upon 'make'), store the
  current line number in r->line.
- run(): When running a shell action, emit a PS4 assignment set a
  nice PS4 showing the last component of the parent directory and
  the current file name (generally Mamfile), r->line (the 'make'
  line), and the current line (the 'done' line).
  • Loading branch information
McDutchie committed Feb 5, 2024
1 parent 8c3ab86 commit 7a73364
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
22 changes: 21 additions & 1 deletion src/cmd/INIT/mamake.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ typedef struct Rule_s /* rule item */
int flags; /* RULE_* flags */
int making; /* currently make()ing */
unsigned long time; /* modification time */
unsigned long line; /* starting line in Mamfile */
} Rule_t;

typedef struct Stream_s /* input file stream stack */
Expand Down Expand Up @@ -651,6 +652,7 @@ rule(char* name)
if (!(r = newof(0, Rule_t, 1, 0)))
report(3, "out of memory [rule]", name, 0);
r->name = (char*)search(state.rules, name, r);
r->line = state.sp ? state.sp->line : 0;
}
return r;
}
Expand Down Expand Up @@ -1274,7 +1276,25 @@ run(Rule_t* r, char* s)
);
/* show trace for the shell action commands */
if (!(r->flags & RULE_notrace))
append(buf,"set -x\n");
{
char *cp;
/* construct a nice PS4 */
append(buf,"PS4='+ (");
cp = state.pwd + strlen(state.pwd) - 1;
while (*cp != '/' && *cp != '\'' && cp > state.pwd)
cp--;
append(buf, cp + 1);
add(buf, '/');
append(buf, state.file);
if (r->line && state.sp)
{
char nbuf[64];
sprintf(nbuf, ":%lu-%lu", r->line, state.sp->line);
append(buf, nbuf);
}
append(buf,") '\n"
"set -x\n");
}
}
if (state.view)
{
Expand Down
24 changes: 12 additions & 12 deletions src/lib/libast/Mamfile
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ make install virtual
exec - iffe ${IFFEFLAGS} -v -X ast -X std -c "${CC} ${mam_cc_FLAGS} ${CCFLAGS} -I. ${LDFLAGS}" run features/map.c
done
exec - ${STDCP} -f FEATURE/map ast_map.h
done
done
done
done
done
done ast_map.h
done ast_common.h
done std/bytesex.h
done std/endian.h
done FEATURE/param
make FEATURE/aso
prev features/aso
exec - iffe ${IFFEFLAGS} -v -X ast -X std -c "${CC} ${mam_cc_FLAGS} ${CCFLAGS} ${LDFLAGS}" run features/aso
Expand Down Expand Up @@ -149,11 +149,11 @@ make install virtual
prev FEATURE/common
prev FEATURE/lib
prev FEATURE/standards
done
done features/limits.c
exec - iffe ${IFFEFLAGS} -v -X ast -X std -c "${CC} ${mam_cc_FLAGS} ${CCFLAGS} -I. -Icomp -Iinclude -Istd ${LDFLAGS}" run features/limits.c
done
done FEATURE/limits
exec - ${STDCP} -f FEATURE/limits ast_limits.h
done
done ast_limits.h
make ast_stdio.h dontcare
make FEATURE/stdio
prev features/stdio
Expand Down Expand Up @@ -246,12 +246,12 @@ make install virtual
done
prev ast_common.h
prev ast_standards.h
done
done
done include/ast_std.h
done FEATURE/stdio
exec - ${STDCP} -f FEATURE/stdio ast_stdio.h
prev include/sfio_s.h
prev include/ast_std.h
done
done ast_stdio.h
make ast_nl_types.h
make FEATURE/nl_types
prev features/nl_types
Expand Down Expand Up @@ -302,7 +302,7 @@ make install virtual
exec - 0) echo ' -lm' >> ast.req ;;
exec - *) touch ast.req ;;
exec - esac
done
done ast.req
make state.o
make misc/state.c
make include/ast.h implicit
Expand Down

0 comments on commit 7a73364

Please sign in to comment.