Builtin commands in makepp
awk, &cat, chgrp, &chmod, chown, &cp, &cut, &echo, &expr, false, &grep, head, &install, &ln, &mkdir, &mv, m4, &perl, &preprocess, &printf, &rm, rmdir, &sed, &sort, tail, &template, &touch, tr, &uninstall, &uniq, &yes
There is a special Shell-like possibility to call built-in commands in a rule.
The only metacharacters recognized are comment signs, backslashes, single and
double quotes. Only one command may be given per line, and I/O redirection is
not available (see -i and -o below instead).
These commands start with &, which is the function character in Perl and
not a valid first character in Shell. If no builtin command of that name can
be found, this is also the syntax for calling an external script within the
Perl instance performing the rule. See run.
These commands, as well as your self defined ones and perl scripts can also be
called as a make function, returning the standard output. The newlines are
converted to spaces, except when evaluated within a define statement.
FIRST-WORDS ;= $(&cut -d' ' -f0 $(FILES))
When these commands are not indented as rule actions, they get performed while reading the makefile. You can also access these commands stand-alone, e.g. if you need some features not available in the Unix counterpart, via the makeppbuiltin command.
These commands are mostly based on the GNU variant. But many options (like --backup, --interactive or --recursive) don't really make sense in a makefile. So, even though they'd be easy to implement in Perl, they have been left out. Also many Unix commands offer a variety of options that cover fairly complicated cases (e.g. sort field specifications) while still being inherently limited. Allowing access to Perl, which is present anyway, gives much more power here.
Lists of filenames may be empty, making it safe to call these commands with an
unchecked list. Options in their short form may be glued together as in
-ab instead of -a -b. Arguments to options are only shown for the long
form, but they also apply to the short form. In the long form they may be
given either glued on with an = sign or separately. In the short form they
may be given either glued on directly or separately. A few options are common
to several builtins, though the short form is sometimes not available:
Read the file and parse it as possibly quoted options on one or several lines.
Force the creation of the file(s) intended by the parameters, even if a
different kind of file or empty directory of that name already exists. This
must precede the -o, --output=filename option if it is to have any effect
on that.
Start the Shell command(s) and pipe the output into the builtin. There may
optionally be a trailing | character, to indicate this is a pipe. With
this option no filenames need to be given. But if you want to perform the
builtin on both files and the pipe output, you must use - as a filename for
the pipe output. This option is necessary because there is no redirection
syntax.
If an --inpipe Shell command fails, that also causes the current builtin to
fail.
Write the output to this file, rather than stdout. Filename may have any of these forms:
Simply write to file.
Append to (not necessarily) existing file.
Also open the file for input, allowing inplace editing. With this option
variant no input filenames need to be given. But if you want to perform the
builtin on more files, you must use - as an input filename for this one.
In fact the ouptut gets written to a temporary file which gets moved to
filename at the end.
Pipe the builtin's output to the Shell command(s).
This option is necessary because there is no redirection syntax.
If an --output Shell command fails, that also causes the current builtin to
fail.
Locally sets $/ for the current builtin. This splits input into records of
length number rather than line by line. If number is zero, each input
file as a whole is one record.
Locally sets $/ for the current builtin. This splits input on string
rather than line by line.
Generate #line NO "FILE" and #line NO lines,
understood by many C-like languages.
Document the changes to the file system. This must precede other options if it is to document their effect. If you pass this option to makepp itself, it is as if you had given it for every single builtin command.
There are two motivations for having builtin commands in makepp. The first is
to offer a set of utilities, which, unlike Shell commands, are guaranteed to
work the same everywhere, like &echo -n or &mkdir -p, and saving you the
hassle of finding the path to &install and figuring out its wildly varying
options. In a compilation environment, it's useful to have the --synclines
option, which normally only m4 provides, on all filters.
The other is a question of efficiency. In general costly fork/execs should be avoided where reasonably possible. On Unix emulations like Cygwin or BS2000/Posix, this becomes a noticeable win. But, even on Linux, when the makepp test suite was converted from external commands to builtins, there was an overall saving of 3% user CPU usage and 15% system CPU usage. (The tests are of course heavy on primitive actions and hardly call the compiler.)
Consistency is also an issue, though we're not going to reform Unix. Normally
commands have various nuances of regular expressions. And many invent sort of
languages, each different of course, for doing something (e.g. expr, sed
...), or complex options for specifying fields, delimiters, columns
(e.g. cut, sort ...).
Here instead, anything fancy simply gets handled by Perl, giving both consistency across all commands, and far more power than a whole bunch of options. Better yet, any Perlcode these commands run for you, gets run in the package of the Makefile. So, rather than stuff Perl code into the rule action, you can define functions and variables and use them within the commands:
sub my_filter {
# Return true iff $_ is desirable
}
%.out: %.in Makeppfile
&grep &my_filter $(input) -o $(output)
If you use perl functions or variables in your commands, makepp does not recognize this as a dependency. It is generally safer to tell makepp everything, so rules which use Perl elements should depend on the makefile or module providing those elements, as shown in the above example.
On the other hand ingnorance may be desirable if you have a program that mixes
programatic and configuration aspects in one file. An example would be a WSDL
file containing both a web service interface definition and an IP address.
You could preprocess this file with the &template command to patch in the
configuration, but not let makepp notice.
Not built in, but &sed is comparable.
Concatenates all the files into a single one.
Standard options: -A, --args-file, --arguments-file=filename, -f, --force,
-i, --inpipe=shellcommand, -I, --infail, -o, --output=filename, -O, --outfail,
-S, --synclines, -v, --verbose
These commands are mostly not portable! They will either quietly do nothing
or fail, depending on the system. Generally only root may perform these
operations, which is why they are only available through the &install
command.
Sets mode for all given files. Mode must be an octal string.
Standard options: -A, --args-file, --arguments-file=filename, -v, --verbose
Copy sourcefile to destfile, one sourcefile to current directory or multiple sourcefiles to destdir with the same name.
Standard options: -A, --args-file, --arguments-file=filename, -f, --force, -v,
--verbose
Try to link the files. If that fails, copy.
Print selected parts of lines from each file or selected lines, counting across all files. The output is separated by the delimiter which defaults to TAB for fields and empty string for characters.
Standard options: -A, --args-file, --arguments-file=filename, --force, -i,
--inpipe=shellcommand, -I, --infail, -o, --output=filename, -O, --outfail, -r,
--record-size=number, --separator=string, -S, --synclines, -v, --verbose
Print all the characters specified by list. List may be any Perl
expression returning a list of integers. The integers can be either positive,
starting at zero to count from the beginning, or negative to count from the
end. Unlike Unix cut, the order you request is respected.
Unlike in Perl's slice operator where a ``..'' range must be either positive or
negative, &cut allows starting with a positive and ending with a negative.
But this is only available if your expression consists only of numbers, commas
and ``..''. E.g. 1..-2 means everything but the first (0) and the last (-1).
The list expression can look at the whole line in $_. Changes to that will
be ignored, however, because when this expression is evaluated the line has
already been split to Perl's autosplit variable @::F. The numbers you
return are in fact indices to that list.
Set a new delimiter for input fields and output. Unlike Unix cut, this may
have any length.
Treat \ as normal literals for -p, --printf=format.
Print all the groups specified by list. List is as described under
-c, --characters=list. Note that this hides the standard option -f
which must be given as --force.
Print all the lines specified by list. List is as described under -c,
--characters=list with one major difference: The first line has number 1,
there is no line 0. This is definitely inefficient for big files, if you have
a mixed positive to negative range in your list, as it reads everything to
memory. Otherwise Perl could optimize this, but I don't know if it does.
Print only matching lines, i.e. ones which have enough characters or fields.
This implies --only-delimited, which is why you will miss single-field
lines with --fields=0.
Apply format (with \escapes) to all fields or characters.
Print only lines containing delimiters.
&cut -c 10-20,-5,25- $(input) &cut -d: --fields 0,4 --printf='%10s is %s\n' /etc/passwd
Writes all strings to stdout or the given outfile. Both &echo and &yes
add a newline at the end. The strings, or for &printf the format, may
contain \ escapes, as they are known from C or modern Unix or Shell
echo. They are however as in Perl double-quotes, which means some
differences, like that a single trailing \ is not allowed. Perl has a few
more interesting escapes, but the ones you might expect to do something
different are:
Is a control character ^A.
Upcases the following letter.
Upcases the rest, or upto the next \L if found.
Is the character value of the given Hex code. Note that numeric codes are not portable to EBCDIC platforms!
Unlike Unix yes, &yes is exactly like &echo, except that it repeats
the output for as long as it can, typically until an --output '| command'
terminates. And, if &yes has no arguments, it defaults to y.
Standard options: -A, --args-file, --arguments-file=filename, -f, --force, -o,
--output=filename, -O, --outfail, -v, --verbose
Treat \ as normal literals.
Do not add a newline after the last string. (Not understood by &printf.)
Print the scalar value of perlcode, which may be written as one or several
arguments. Note that builtin commands are not parsed by the Shell, so *,
( or > are not special. But string quotes are parsed by makepp, so
Perl strings must be quoted twice, unless you want to use barewords. If the
value is false, this fails. Note that, unlike in Unix expr Perl's index
function starts at 0 (false) and returns -1 (true) for failure.
Standard options: -A, --args-file, --arguments-file=filename, -f, --force, -o,
--output=filename, -O, --outfail, -v, --verbose
Do not add a newline after the output.
&expr ($(VAR) - 3) * 2 < 1 && -1 || 1 &expr "$(VAR) - 3 * 2 < 1 ? 'joy' : 'sorrow'" -o $(output) -&expr $(VAR) - 3 * 2 -o >>$(output)
Not very constructive and thus not built in, but &expr with no
argument or 0 is comparable.
All the files get read line by line (unless you gave a --separator option),
and perlcode gets evaluated for each line, before it gets printed. &sed
is similar to perl -pe, while &grep only outputs those lines for which
perlcode returns a true value. &perl is similar to perl -ne, only
outputting whatever you explicitly print in the perlcode. The line content
is available in $_, which may be modified.
Of these three, only &grep will fail if it outputs nothing. Note that
there is no ignore-case option, since you would do that with /regexp/i.
Standard options: -A, --args-file, --arguments-file=filename, -f, --force, -i,
--inpipe=shellcommand, -I, --infail, -o, --output=filename, -O, --outfail, -r,
--record-size=number, -s, --separator=string, -S, --synclines, --verbose
The option --synclines only makes sence with &perl if you use
&Makecmds::print to output $_. Only &grep has extra options:
Suppress normal output; instead print a count of matching lines. With the -v,
--invert-match option (see below), count non-matching lines.
Output only the name of those files with matches. When this is combined with
-v, --invert-match, output the name of files with lines that don't match (a
bit absurdly but compatible with Unix -vl). When this is combined with a
doubled -vv, output the name of files with no matches.
Invert the sense of matching, to select non-matching lines. Note that this
hides the standard option -v which must be given as --verbose.
An optional waste basket for collecting the rejected lines. This is not only for debugging your selection code, but also for splitting your input in two. As with the normal output, you may modify $_ before returning false.
&sed s/foo/bar/ f1 f2 f3 -o outfile # like sed s/foo/bar/ f1 f2 f3 >outfile &sed '$$_ = uc' f1 f2 f3 -o outfile # like tr '[:lower:]' '[:upper:]' f1 f2 f3 &grep '$$. % 3' f1 f2 f3 -o outfile # eliminate every 3rd line &grep -c /match/i f1 f2 f3 # count the lines matching 'match' to STDOUT
These are not provided, but you can achieve the same result with &grep or
&cut --lines:
&grep 1..10 file # first ten lines &grep 10..eof file # all lines from tenth onwards &cut --lines -10..-1 file # last ten lines
Note that 1..10 in &grep is Perl's line number flip-flop operator, which
annoyingly starts at 1. Don't start at 0, or the flip-flop will never become
true.
Move or rename sourcefile to destfile, or multiple sourcefiles to destdir with the same name. This is the preferred way of transferring build results to their final installation locations.
Every file system modification performed by &install gets logged to the end
of the file pointed to by the environment variable $INSTALL_LOG, or, if
that is not set but we are under a directory with a RootMakeppfile(.mk), to
a file of .install_log in that directory, or else to that file in the
current directory. You may want to delete the logfile before a series of
&install invocations.
Standard options: -A, --args-file, --arguments-file=filename, -v, --verbose
Copy the files rather than moving them. This is preferable, as it doesn't force makepp to rebuild the file next time. But it is not the default, for compatibility with other install programs.
In the third form form of this command create all the given directories and any necessary parent directories.
Change the group ownership of the destination files. The group may be given by name or numerically.
Try to link the files. If that fails, copy.
Use filename instead of normal logfile.
Sets mode for all destination files or directories. Mode must be an octal string.
Change the ownership of the destination files. The owner may be given by name or numerically.
Creates symbolic links instead of moving. These options are passed to
&ln and are described there.
Calls the strip utility, which must be in the $PATH, on the destination
files.
Link sourcefile to destfile, one sourcefile to current directory or multiple sourcefiles to destdir with the same name.
Standard options: -A, --args-file, --arguments-file=filename, -f, --force, -v,
--verbose
This is what you always wanted ln -s to do. Create symbolic rather than
hard links, not to the strings specified, but really to the given files.
Create symbolic rather than hard links.
Note that on various file or operating systems, this operation is not
supported. To get at least some sort of result, &ln can copy the files for
you instead (not directories though). To achieve this, you need to export the
following variable before calling makepp:
&ln --resolve or --symbolic will copy the files instead of creating a
symbolic link.
&ln will copy the files instead of creating a hard link.
All invocations of &ln will copy the files instead of creating either kind
of link.
Create the directories.
Standard options: -A, --args-file, --arguments-file=filename, -f, --force, -v,
--verbose
Sets mode for all created directories, irrespective of the umask. Mode must be an octal string. If this is not given, the mode defaults to 755.
Also create any necessary parent directories. Ignore directory creation failure due to the directory already existing (even if it was created concurrently by another process).
Move or rename sourcefile to destfile, one sourcefile to current directory or multiple sourcefiles to destdir with the same name.
Standard options: -A, --args-file, --arguments-file=filename, -f, --force, -v,
--verbose
Not built in, but &preprocess is, and
&template is almost as powerful.
This preprocesses the files exactly the same way makepp does for makefiles.
This is more powerful than &template but syntactically not
suited to files with lots of $-signs, like Makefiles or scripts.
Conditional statements, as well as the
statements include/_include (which here neither build the file nor
search upwards), perl/makeperl/perl_begin or sub/makesub, or
any statements you define within the file, are processed. Empty and comment
lines are eliminated.
But, instead of learning build rules, it will output all remaining lines after
$(...) expression expansion. To prevent statement from being recognizeded
as such, you can precede them with an empty expression $(). The same
applies to lines you want to stay empty or which shall retain a leading
comment sign. Likewise, if a trailing backslash is not to join a line with
the next, put $() after it.
A normal line gets output as is. A line with $(MAKEEXPRESSIONS) gets expanded and output. ifdef WANTTHIS # does not get output whether defined or not might not get output endif include some files _include some files that might not exist # or -include $()include empty expression prevents keyword from being recognized. # Comment lines and empty lines get swallowed. $()# Unless they get masked with an empty expression. $() Empty expression prevents \$() backslash continuation from being recognized.
might give:
A normal line gets output as is. A line with whatever gets expanded and output. lots of slurped in content here... include empty expression prevents keyword from being recognized. # Unless they get masked with an empty expression. Empty expression prevents \ backslash continuation from being recognized.
Standard options: -A, --args-file, --arguments-file=filename, -f, --force, -o,
--output=filename, -O, --outfail, -S, --synclines, -v, --verbose
Also treat assignments within the files as makepp would. Alas such lines
can't be masked with an empty $(), because it is legal to construct
variable names with expressions. This additionally recognizes the statements
define, export/unexport and override (these can be masked with
$()).
This allows preallocation of the variable values, including long ones not easily
passed in a command. The passed expression may be any Perl code that returns
a hash reference. This is merged with any other variables passed to the command,
including from another --hashref option.
Delete files if you have directory write permission. This is what Unix
rm -f would delete, since it has a special protection for interactive use
not needed in a Makefile.
Standard options: -A, --args-file, --arguments-file=filename, -v, --verbose
This prevents complaining about inexistent files. That is a side effect this option has in Unix, and the only one that makes sense here.
In addition to the given files, this also deletes the meta information makepp stores about them in the .makepp directory. Thus makepp forgets all it ever knew about the given files. If the .makepp directory becomes empty after this, it too is deleted.
This will also delete given directories, but only if they are empty. To
facilitate this, it will delete directories last, in the order of descending
depth. So you can use ** expressions to delete whole hierarchies. Here's
an example to be found in many top level make files. Note that there is a
makeppclean utility that can do this more efficiently.
$(phony cleanold):
&rm -fm $(only-stale **/*)
$(phony clean): cleanold
&rm -f $(wildcard **/*.[ao])
$(phony distclean): clean
&rm -fm $(only-targets **/*)
Not built in, but &rm can handle this.
Sorts all files together in lexicographic order. This is inefficient for rather big files, because it happens completely in memory. It will fail if the combined size of all files exceeds the memory you are entitled to.
Standard options: -A, --args-file, --arguments-file=filename, -f, --force, -i,
--inpipe=shellcommand, -I, --infail, -o, --output=filename, -O, --outfail,
--record-size=number, -s, --separator=string, -v, --verbose
perlcode represents a Perl sort block, with the two sorting candidates in
$a and $b.
This sorts sorts numerically on the beginnings of records. Leading whitespace
is skipped. You can use --transform and --detransform if the numbers
are not at the beginning.
Output the results in the reverse order. Note that this hides the standard
option -r which must be given as --record-size.
If you have a complex code, sorting gets more and more expensive in proportion to the number of records n, because the code gets called O(n log(n)) times. To avoid that, you can allow Perl to concentrate on sorting, by first modifying the strings, such that complicated search criteria extraction happens once per record, and modifying them back, once they are sorted.
If these options are given, the --transform perlcode gets mapped to the
records in $_ one after another, and can modify them. After sorting, the
--detransform perlcode gets mapped to the modified records in $_ one
after another, and can modify them back. You will usually use neither or both
of these options, unless you want to output modified lines.
Turning the strings into a structure of extracted sort criteria, which your
--compare perlcode can pick up is known as the Schwartzian Transform
(ST). Packing everything into the string itself, so that no --compare
perlcode is needed, allowing the whole sorting to happen without performing
expensive Perl code, is known as the Guttmann-Rosler Transform (GRT). You can
find tips by searching for those names on the web.
# Expensively sort numerical expressions by value ($$ protects $ from makepp expansion) &sort --compare 'eval( $$a ) <=> eval( $$b )' $(input) -o >>$(output) # ST for case insensitive sorting &sort -t '$$_ = [lc, $$_]' -c '$$a->[0] cmp $$b->[0]' -d '$$_->[1]' $(input) -o >>$(output) # GRT using modification functions defined elsewhere in the Makeppfile &sort -t &transform -d &detransform $(input) -o >>$(output)
After sorting, eliminate duplicates. These are either identical lines, or if
the --compare option is given, ones which that perlcode reports as
equivalent.
This is a macro preprocessor, not quite as powerful as m4, but covers more
than is found in many makefiles. See &preprocess for a
more powerful alternative. Any normal text goes through unchanged. It
replaces all occurences of @macro@, @macro(arg1,arg2...)@ or everything
between @@macro@@, @@macro(arg1,arg2...)@@ and @@ with definition.
If there are args, they replace $1 through $9 or ${number} in
definition. One level of macro nesting is possible in that the args in
parenthesis may contain plain @macro@ invocations, as in @f(@x@)@, where
@x@ gets expanded before being replaced into the body of f.
The simple @...@ cases are single line, but may mask a trailing newline if
the closing @ is immediately followed by a backslash. The multiline
@@...@@ cases must also fit on one line, but the corresponding @@ may be
on a different line. This is useful if you have a workaround code block in an
unprocessed script, which is to get replaced with the configured code.
In addition to passing macro definitions on the command line, you can also put
@macro=definition@ or @macro?=definition@ into the file. The latter
only takes effect if the macro was not defined, presumably on the command
line. You can also call @{ Perlcode }@ or @@{ Perlcode }@@ ... @@
in the file. The Perl variable $ARGV contains the name of the current
input file. If you call @macro { Perlcode }@, then you define a new
macro, the body of which is a perl sub. The arguments, if there are any, get
passed in as @_.
@m1=some definition@\
@m2=foo $1 bar@\
@middle_of_arg=iddl@\
@m1@ @m2(m@middle_of_arg@e)@
@@m2(many lines)@@
...
@@ plain text 1 + 2 = @{ 1 + 2 }@
becomes
some definition foo middle bar foo many lines bar plain text 1 + 2 = 3
Standard options: -A, --args-file, --arguments-file=filename, -f, --force,
-i, --inpipe=shellcommand, -I, --infail, -o, --output=filename, -O, --outfail,
-S, --synclines, -v, --verbose
This allows preallocation of the macro values, including long ones not easily
passed in a command. The passed expression may be any Perl code that returns
a hash reference. This is merged with any other macros passed to the command,
including from another --hashref option. A hash value may also be a code
reference, in that case the function gets called, as with @macro {
I<Perlcode> }@ definitions.
This replaces @ before and after var with prefix and suffix
respectively. The first character is the separator and need not be a slash.
This replaces @@ before and after var and at the end of the block with
prefix, suffix and afterprefix respectively. If aftersuffix is
also given, the var name must get repeated before it. The first character
is the separator and need not be a slash. E.g. an XML-ish
--simple=|<|/>| --multiline=|<|>|</|>|
Replace only instances of macros which are actually defined.
Updates the modification and access timestamps of each file to now. If the file doesn't exist, it gets created.
Standard options: -A, --args-file, --arguments-file=filename, -v, --verbose
Uninstall files previously installed by &install. The
filenames are logfiles written by &install. If none are given, nor an
--inpipe option, reads the default logfile of &install.
Standard options: -A, --args-file, --arguments-file=filename, -i,
--inpipe=shellcommand, -I, --infail, -v, --verbose
Discard all but one of successive equal lines.
Standard options: -A, --args-file, --arguments-file=filename, -f, --force, -i,
--inpipe=shellcommand, -I, --infail, -o, --output=filename, -O, --outfail, -r,
--record-size=number, -s, --separator=string, -S, --synclines, -v, --verbose
This Perlcode gets the previous and current lines in $a and $b and
shall return true if it considers the two lines equal.
&uniq --compare='lc( $$a ) eq lc $$b' $(inputs) -o $(output)
Not built in, but &sed can handle this.