Commit Graph

416 Commits

Author SHA1 Message Date
Panu Matilainen deaebd0c89 Add optional total/proc/thread arguments to %{getncpus} macro
"total" equals calling with no arguments, "proc" and "thread" consider
further constraints, what is implemented here is heuristics based on
available physical memory and address-space and %_smp_tasksize_proc /
%_smp_tasksize_thread tunables.

Change the previous %getncpus related tests to use %getconfdir instead,
they are testing unexpected arguments behavior for this type of macro,
not %getncpus itself. Add a test for the actual functionality: if nproc
is available, test that our total matches with that, and that defining
tasksize to total memory only allocates one thread. Optimally we'd
test separately for 32bit address space limitations but that gets tough
when we have no idea where this will be executed.
2023-03-30 13:58:04 +03:00
Panu Matilainen fd2f743b3e Fix macro scoping level on re-entry from %[] expresssion (#2354)
This is the same issue as commit 1767bc4fd8
was with Lua, and so the same fix works: restore the nesting level
from the macro context when re-entering macro engine from %[]
expression. Analysis and suggested fix by Michael Schroeder,
reproducer from Miro Hrončok.

Add tests for both %[] and %{expr:...}, although the latter isn't
affected because the expression is macro-expanded beforehand.

Fixes: #2354
2023-01-20 07:58:48 +02:00
Panu Matilainen cad5affacd Add shorthand macros for some conventient Lua string operations
Despite all the Lua magic we already do, it's annoyingly often the case
that shelling out is easier (or at least shorter) than doing the same in
Lua (substrings, length etc)

Add shorthand macros %gsub, %len, %lower, %rep, %reverse, %sub and
%upper which simply wrap the corresponding Lua string.* functions for
convenience.
2022-09-15 10:32:08 +03:00
Panu Matilainen 48d0fa954c Test for preprocessor definition rather than value consistently everywhere
This is not supposed to actually change anything as such but
inconsistency is bad and causes other kinds of headaches.
2022-06-15 16:24:51 +03:00
Panu Matilainen 10804d32d9 Use rpmuncompress to handle %{uncompress:...}
This means that finally there's just one place that when adding support
for new compress formats, there's in theory there's just one place to
update. Reality is a little more complicated, but hey...

Adjust tests a bit, and rather test functionality than command output
because those reflect command paths which we can't easily adjust now.
2022-04-04 10:04:25 +03:00
Michael Schroeder 718295d91e Support multiple args in %quote and make split/unsplit available to lua
This allows to manipulate an argument array from lua.
2021-11-18 09:44:44 +02:00
Michael Schroeder 3c793a653d Simplify shrink macro implementation
Less code and easier to understand.
2021-11-08 11:10:25 +02:00
Michael Schroeder efc3f10754 Do not always strdup the argument in doFoo
Only a small subset of the doFoo macros needs to modify the
argument, so only do the strdup call in them.
2021-11-08 11:10:25 +02:00
Michael Schroeder 162b2d3a33 Do not setup macro args for literal macros
They cannot make use of the arguments, so it makes no sense to set
them up.
2021-11-08 11:10:25 +02:00
Michael Schroeder 48825725e6 Remove duplicated body copying code
Put the name first in the string arena so that the macro body
copying code is identical for both macro setting cases.
2021-11-08 11:10:25 +02:00
Michael Schroeder 263f70b204 Split new macro table entry generation into newEntry()
No functional changes, just to make the code easier to understand.
2021-11-08 11:10:25 +02:00
Michael Schroeder 26767d4646 Allow an optional argument for the %verbose macro
This improves compatibility with old rpm versions. If an argument
is present, the macro expands to it in verbose mode and to an
empty string if not in verbose mode.
2021-10-26 16:12:29 +03:00
Michael Schroeder bfbd0b3e7f Support non-parametric builtins again
Macros like %getncpus should not treat the following text as
macro arguments. E.g. 'make -j %getncpus all' should work and
not complain about an unexpected argument.

We still treat arguments passed via %{foo:arg} or %{foo arg} as an
error for builtin macros. (They are silently ignored for user
defined macros.)
2021-10-26 10:01:56 +03:00
Michael Schroeder c00d85fb5e Make %{define foo body} not use the free-field parsing
This means no special new line processing and '\' unescaping. Thus
"%{define foo body}" works similar to "%define foo {body}" except
that the parsing in spec files works because copyNextLineFromOFI()
knows how to deal with macros.
2021-10-26 10:01:56 +03:00
Michael Schroeder 374c0edac0 Special case the non-parametric and the free-field macro expansion
This makes the code easier to understand and also has the advantage
that "parsed" will be NULL for the normal cases.

No functual changes.
2021-10-26 10:01:56 +03:00
Michael Schroeder 243d01ff46 Rename doExpandThisMacro to doMacro
The doXXX functions now have the same argument signature and I
never liked the doExpandThisMacro name.
2021-10-26 10:01:56 +03:00
Michael Schroeder 1d1f461f80 Add a "parsed" argument to the doXXX() functions
doXXX() used to return the parsed characters for ME_PARSE macros
and the caller added them to the "parsed" variable.

We now directly pass parsed as argument and let the doXXX()
functions do the adding. This will allow us to differentiate
between "%define foo bar" and "%{define foo bar}" in a
future commit.

No functual changes.
2021-10-26 10:01:56 +03:00
Michael Schroeder 80f025863f Fix consistency issues in macro expansion for builtin macros
The builtin macros used to expand the arguments themselfs. This
works for the %{foo:bar} syntax, but it means that for %{foo bar}
the argument is expanded twice: once before the argument is split
and then in the macro function.

Simplify the code by always doing the expansion when creating
the argument vector.
2021-10-26 10:01:56 +03:00
Michael Schroeder 79e8c46b12 Get rid of the ME_BUILTIN macro
Always set ME_FUNC for builtin functions.
2021-10-26 10:01:56 +03:00
Panu Matilainen 5e97c0a3ac Clean up shescape macro implementation
Realizing that we have dynamic string buffer available in the macro
engine... so we don't need a dumb worst case malloc and all the other
crap in the old tag format era implementation.
2021-10-08 14:36:27 +02:00
Panu Matilainen f64ad44ccd Add %{shescape:...} macro for single quoting with escapes for the shell
This is exactly the same as :shescape query format, hence the name.
2021-09-24 14:55:44 +02:00
Panu Matilainen b88f43b9a3 Explicitly protect automatic macros from being redefined and undefined
All current automatic macros are named in a way that do not pass the
name check in validName(), but that's kinda implicit. Specifically
test for ME_AUTO along with ME_BUILTIN.

No functional change due to the implicit protection from the naming.
2021-09-01 10:35:04 +03:00
Panu Matilainen 80ca959e32 Drop arbitrary macro name minimum length limit (RhBug:1994223)
Traditionally rpm has required user defined macro names to be at least
three characters long, but outlaws all sorts of useful names like %cc
for no particularly good reason - on unix a *lot* of commands are two
characters, and then there are programming languages named R and whatnot.
For macros starting with underscore, require one additional character
though so `r` is okay and so is `_r` but plain `_` is not.

The name validation and error reporting is buggy in multiple ways but not
in the mood to chase all those now, this is just the bare minimum change.
2021-09-01 10:35:04 +03:00
Panu Matilainen 847c6f062c Ignore comment line contents in macro files (#1659)
Previously %{ and similar in macro file comment line would cause the
line continuation logic to trigger and silently eat macro definitions
up to the next empty line. Since 75275a87cf
we permit empty lines inside macro definitions, which would cause the
whole remaining file to be silently skipped (RhBug:1953910)

Only ever parse macro file lines starting with %, and add a test for
the case.

Actual patch by Michael Schroeder, testcase by undersigned.

Fixes: #1659
2021-04-29 14:32:16 +03:00
Michael Schroeder 75275a87cf Fix logic error in macro file reader
The old logic always finished adding lines if an empty line
was encountered, because the check that should guard against
accessing outside of the read buffer was done at the wrong
level.

Also simplify the code by letting 'q' point to the new line and
not before the newline, which also fixes potential undefined
behaviour.

Also get rid of the unused 'nread' variable.
2021-03-26 13:44:48 +02:00
Panu Matilainen 7f3fdf6868 Make Lua a hard requirement for rpm
More and more macros, scriptlets and other functionality has been getting
built around Lua, to the point that it has in practice been required for
several years now.

Maintaining the pretense of being optional comes at a cost in holding
back developments and having to check for that theoretical special
case. Lets make it a hard requirement and embrace it some more!
2021-02-09 13:57:27 +02:00
Panu Matilainen f67d239ccd Actually report the involved macro names in macro tracebacks
Should make macro debugging a little saner, including but not limited
to Lua.

Fixes: #545
2021-01-29 13:54:54 +01:00
Panu Matilainen 5bfb49d6b7 Conditionalize macro traceback dump on rpm verbosity level
Only dump out macro tracebacks in verbose mode, which incidentally
rpmbuild normally runs in.

Sadly our tracebacks are not as useful as they could be, but improving
that is a separate topic...
2021-01-29 13:54:54 +01:00
Panu Matilainen c145a6c86a Revert "Redirect macro stack backtrace printing to debug log."
The rationale behind the change was to suppress the excessive output
when trying to expand a recursive macro definition (RhBug:613010) but
this is not so good as it effectively suppresses *all* backtrace output,
include those requested by %trace where the other half is printed to
stderr and the other half in the debug log because of the change.

Besides making things consistent, this also avoids deadlock on
`rpm -vv --eval '%trace'`.

This reverts commit 7f220202f2.

Fixes: #1418
2021-01-29 13:54:54 +01:00
Panu Matilainen c072ef6bb8 Let %define and %global take name and body as separate arguments
doDefine() wants macro file-style "name body" string as an argument, but
when called through rpmExpandThisMacro() you'd expect to put the name
and body in separate arguments. Properly handling this in the macro engine
guts is more than I'm prepared to do at the moment, but we can detect
the multiple arguments case and turn the arguments into something that
doDefine groks.

This will matter when we add support for calling macros as native Lua
elements.
2021-01-27 14:32:34 +02:00
Panu Matilainen 43148cb9a7 Change %dump macro to "func" style now that we can
%dump macro takes no arguments of any kind, it just eats the rest of
the line which doesn't make any sense whatsoever. So while this
theoretically changes %dump behavior, the behavior actually changed
a few commits back with the calling syntax unification: %dump never
took actual arguments, so it'll now just error out if there's text
on the same line where it previously ignored that. Unless you use
%{dump} that is.
2020-11-26 12:07:25 +02:00
Panu Matilainen aec8bdb53a Change %undefine macro to "func" style now that we can
%undefine parsing its own argument makes no sense whatsoever,
it's just a name and nothing special. Now that the calling syntax
is no longer an issue, make it sane. No functional changes.
2020-11-26 12:07:25 +02:00
Panu Matilainen 78bb7369a3 Specify number of expected arguments for builtins in numbers
No functional changes here, but going forward this gives us more
flexibility than the ME_HAVEARG. We could also extend this to support
mandatory arguments to user defined macros too.
2020-11-26 12:07:25 +02:00
Panu Matilainen cfaf1c6292 Unify builtin macro calling now that we can
Now that both types use identical function signature, we can trivially
unify the calling code for both, simplifying things somewhat, make
the non-braced syntax for built-in macros work, and regain type safety.
2020-11-26 12:07:25 +02:00
Panu Matilainen 1c964243b5 Change func-macro types to return a number (always zero)
No functional changes, but look mom, we have identical signature for
both builtin macro types!
2020-11-26 12:07:25 +02:00
Panu Matilainen f5f9764e6d Change builtin "parse" type macros to take their arguments via an argv
No functional changes here and not sure if/how these would ever take
multiple arguments, but there's another treasure at the end of
this particular rainbow...
2020-11-26 12:07:25 +02:00
Panu Matilainen 3d3f717d28 Change builtin "func" type macros to take their arguments via an argv
No functional changes here, but opens the door for builts accepting
multiple arguments.
2020-11-26 12:07:25 +02:00
Panu Matilainen 1e3f40b14e Put an insulation layer between source line parsing and macro arguments
Change parse-type macros to return number of consumed characters rather
than a pointer to the parsed string to separate the two.
2020-11-26 12:07:25 +02:00
Panu Matilainen 1de248d440 Unify builtin and user-defined parametric macro calling syntax
Accept both %{foo:arg} and %{foo arg} notation for both builtins
and user-defined parametric macros, the former only ever has one
argument whereas the latter can have multiple.

"parse" type builtins such as %define are trickier and only work with
traditional syntax for now.
2020-11-26 12:07:25 +02:00
Panu Matilainen 814a1247cd Support testing for builtin Lua with a macro conditional
Only define builtin %{lua:...} if actually compiled with the support.
This loses the "informative" error message about the situation but
I think ability to test feature presence is more useful and important.
2020-11-09 13:59:47 +02:00
Panu Matilainen 04ab1f5129 Move the builtins table below the referenced functions to lose clutter
No code or functionality changes, just shuffling code to get rid of
redundant declarations.
2020-11-09 13:59:47 +02:00
Panu Matilainen ea6431c521 Make builtin macros honor generic macro conditionals 2020-11-09 13:59:47 +02:00
Panu Matilainen 1d83ec55d6 Lose now unnecessary chkexist and negate arguments to builtin macros
No functional changes.
2020-11-09 13:59:47 +02:00
Panu Matilainen 413c85f131 Drop support for undocumented %{!trace} builtin syntax
We can always add some other syntax to handle this if needed.
2020-11-09 13:59:47 +02:00
Panu Matilainen f955bc69d0 Drop undocumented special conditional syntax from %{load:...} builtin
%{load:...} supported strange conditional syntaxes that conflict
with general macro syntax. Drop support for these undocumented
variants, people can use %{exists:...} to test for file existence
before trying to load a macro file.
2020-11-09 13:59:47 +02:00
Panu Matilainen 7b25e19803 Add %{exists:...} builtin macro for testing file existence 2020-11-09 13:59:47 +02:00
Panu Matilainen d649c2aa3b Change builtin %{verbose:...} to return a simple 0/1 boolean
%{verbose:...} supported a special syntax of negation with %{!verbose}
but this conflicts with general macro syntax. Now that we have
expressions we can easily handle arbitrary conditions using generic
syntax but %verbose semantics need to change for that: lose the argument
and just return a simple boolean reflecting whether rpm is in verbose
mode or not.

This is obviously an incompatible change, but few things outside rpm itself
should care about this anyhow.

Update the sole user (build scriptlets) to use an expression instead
(whee, the first actual in-tree user!), documentation and testcases
accordingly.
2020-11-09 13:59:47 +02:00
Panu Matilainen 561c59db1b Convert some obvious cases to use rstrndup() instead of manual work
The net win in terms of LoC is not that big but it does make things
that little more readable and obvious.
2020-11-09 09:28:54 +02:00
Panu Matilainen fc31fec661 Pass their own macro entry as an argument (ie self) to all builtins
No intended functional changes, but it's one argument less to pass
around, brings builtins one step closer to how user defined
macros are called and is a step towards supporting options to
builtins as well.
2020-11-04 10:19:40 +02:00
Panu Matilainen ded0282b05 Lose now unnecessary name length member in builtin macro table 2020-11-04 10:19:40 +02:00