Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mamake: add %{VAR|script} and %{VAR@script} expansions
The Mamfiles still had one remaining human maintenance problem -- when building object files from source files in a loop (see 4eaf25c), the base names of all the object files needed to be repeated even though those are known from the list of source files. To make it possible to maintain a "single point of truth" for source and object file basenames while maximising flexibility, this commit adds two new variable expansion types to mamake: In %{VAR|S}, the value of VAR is split into fields by whitespace and the one-line sh(1) script S is executed with each field written as a separate line to its standard input. The value of the expansion is the output of S with each newline changed back into a space, except that a terminating newline (if any) is discarded. This mechanism allows easy editing of variable values containing multiple pathnames using line-oriented utilities thet read from standard input, such as sed(1) or grep(1). In %{VAR@S}, the value of VAR is split into fields by whitespace and each field becomes a positional parameter (starting at $1) in the one-line sh(1) script S. The value of the expansion is the output of S with each newline converted into a space, except that a terminating newline (if any) is discarded. This allows easy processing of one or more pathnames using shell commands that take arguments, such as basename(1) or printf(1), or shell loops. This commit applies some other tweaks to mamake as well. src/cmd/INIT/mamake.c (main changes): - Simplify standards macros. Tested on macOS, Linux, *BSD, Solaris. - buffer(): Do not reinitialise the 'end' member when reusing a previously dropped buffer. Since its allocation is never shrunk after growing it, 'end' should simply be left alone. - append(): Remove unused str==0 reset functionality. - substitute(): - Scan for operator characters at the second character after %{/${ instead of the first. This simplifies things. - Recognise | and @ as operators for new %{...} expansions. - case '@': Added. Split fields into argv[] by whitespace. The script execution and output reading code is shared with '|'. - case '|': Added. Write value to temp file, converting whitespace to newlines. Execute script using our chosen shell and I/O redirected. Read output back, converting each newline but the last to a space. - execute(): Change to execute_v() which takes an additional list of arguments in argv; this is to support %{VAR@S}. Reimplement execute() as a special case of execute_v(). - make(): - Instead of expanding variables in specific arguments as needed, expand all variables in the entire line in one go, before separating the command name, argument word and operand string. This is necessary becasue expansions may now contain spaces, which broke the previous way. The entire mamake program now contains only one expand() call. - 'bind' command: It is a botch for 'bind' to potentially affect the automatic variables. Save the automatic variables before including generated dependency rules and restore them after. - 'make' command: No longer wait for all background jobs to finish before executing a shell action. This was not needed and did harm, as it stalled parallel builds. From now on, virtual rules are never run in parallel with *subsequent* shell actions (this is needed for recursion shell actions to work correctly) but previous shell actions may still run in parallel with it. src/cmd/INIT/Mamfile, src/lib/libcmd/Mamfile, src/lib/libdll/Mamfile, src/cmd/ksh93/Mamfile: - Refactor to implement the simplification made possible by the new features. C source file declarations are enclosed in a virtual rule so all their names (automatically saved up in %{^}) can be copied to a new variable and used with one of the new expansion types later. - In ksh93/Mamfile, this even allows collapsing four compile loops in one, as we can now deal with .c files in four different subdirectories using a basename(1) invocation. src/lib/libast/Mamfile: - No changes. The dependencies and compiler invocations are too messy to have everything compile in a loop with identical compiler invocations. Further refactoring may be possible, but needs to be done carefully and that is for a separate commit. - Remove an outdated 'slow' comment (re: 6b66c18). src/cmd/INIT/utils/Mamfile_rm_unused_vars: - Fix to work properly with special expansions and new-style %{...} expansions.
- Loading branch information