Skip to content

Commit

Permalink
mamake: unified function for including files (re: f58153d)
Browse files Browse the repository at this point in the history
Mamfiles currently include other files with MAM code upon 'setv CC'
to read the compiler probe results from lib/probe/C/mam/HASHEDPATH,
and upon 'bind -lfoo' to read the dependency rules for libfoo from
lib/mam/foo.

To ensure current and future consistency, this commit consolidates
the include functionality in an include() function that also saves
and restores the automatic variables.

This functionality is now also exposed to the MAM language in the
form of a new 'incl' command. It's currently unused, but may come
in handy one day, and it's so trivial it seems silly not to add it.
  • Loading branch information
McDutchie committed Nov 26, 2024
1 parent 03ace33 commit 2757793
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
8 changes: 8 additions & 0 deletions src/cmd/INIT/README-mamake.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,14 @@ iteration *variable*.

`loop` requires that the Mamfile be seekable (i.e.: not a pipe).

### Including a MAM file ###

`incl` *filename*

The `incl` command reads a file with MAM commands from *filename* as if
those commands had appeared in place of the `incl` command, except that the
automatic variables are saved before and restored after reading.

## Parallel processing ##

As of strict level 5, `mamake` supports parallel building using the new
Expand Down
60 changes: 38 additions & 22 deletions src/cmd/INIT/mamake.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* coded for portability
*/

#define RELEASE_DATE "2024-11-23"
#define RELEASE_DATE "2024-11-26"
static char id[] = "\n@(#)$Id: mamake (ksh 93u+m) " RELEASE_DATE " $\0\n";

#if _PACKAGE_ast
Expand Down Expand Up @@ -1758,6 +1758,34 @@ static void run(Rule_t *r, char *s)
drop(buf);
}

/*
* include file in rule r
*/

static int include(Rule_t *r, Makestate_t *stp, char *file, int pushflags)
{
int rv;
if (rv = push(file, NULL, pushflags))
{
/* save automatic variables */
char *s1 = auto_prev->value, *s2 = auto_allprev->value, *s3 = auto_updprev->value;
auto_prev->value = auto_allprev->value = auto_updprev->value = empty;
/* read the file */
report(-1, file, "include", NULL);
make(r, stp);
pop();
/* restore automatic variables */
if (auto_prev->value != empty)
free(auto_prev->value);
if (auto_allprev->value != empty)
free(auto_allprev->value);
if (auto_updprev->value != empty)
free(auto_updprev->value);
auto_prev->value = s1, auto_allprev->value = s2, auto_updprev->value = s3;
}
return rv;
}

/*
* generate (if necessary) and read the MAM probe information
* done on the first `setv CC ...'
Expand Down Expand Up @@ -1803,7 +1831,7 @@ static void probe(Rule_t *r, Makestate_t *stp)
s = use(buf);
/* generate probe info if it is nonexistent or older than mamprobe */
output_time = stat(s, &st) ? 0 : st.st_mtime;
if (output_time < cmd_time || !push(s, NULL, 0))
if (output_time < cmd_time || !include(r, stp, s, 0))
{
Buf_t *tmp = buffer();
append(tmp, cmd);
Expand All @@ -1814,13 +1842,10 @@ static void probe(Rule_t *r, Makestate_t *stp)
if (execute(NULL, use(tmp)))
error_out("cannot generate probe info", s);
drop(tmp);
if (!push(s, NULL, 0))
error_out("cannot read probe info", s);
include(r, stp, s, STREAM_MUST);
}
free(cmd);
drop(buf);
make(r, stp);
pop();
}

/*
Expand Down Expand Up @@ -2194,22 +2219,7 @@ static void make(Rule_t *r, Makestate_t *parentstate)
append(buf, state.installroot);
append(buf, "/lib/mam/");
append(buf, libname);
s = use(buf);
if (push(s, NULL, 0))
{
char *s1 = auto_prev->value, *s2 = auto_allprev->value, *s3 = auto_updprev->value;
auto_prev->value = auto_allprev->value = auto_updprev->value = empty;
report(-1, s, "bind: include", NULL);
make(r, &st);
if (auto_prev->value != empty)
free(auto_prev->value);
if (auto_allprev->value != empty)
free(auto_allprev->value);
if (auto_updprev->value != empty)
free(auto_updprev->value);
auto_prev->value = s1, auto_allprev->value = s2, auto_updprev->value = s3;
pop();
}
include(r, &st, use(buf), 0);
}
continue;

Expand Down Expand Up @@ -2294,6 +2304,12 @@ static void make(Rule_t *r, Makestate_t *parentstate)
}
continue;

case KEY('i','n','c','l'):
if (!*t || *v)
error_out("syntax error", u);
include(r, &st, t, STREAM_MUST);
continue;

case KEY('l','o','o','p'):
{
off_t saveoff;
Expand Down

0 comments on commit 2757793

Please sign in to comment.