[libvirt] [PATCH] spec: Guess rhel/fedora macros from dist

--- If anyone knows how to make rpm to evaluate %(...) immediately and not at every appearance of rhel/fedora, that would be really cool. libvirt.spec.in | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-) diff --git a/libvirt.spec.in b/libvirt.spec.in index 02be928..bfc9206 100644 --- a/libvirt.spec.in +++ b/libvirt.spec.in @@ -1,5 +1,11 @@ # -*- rpm-spec -*- +# if neither fedora nor rhel was defined, try to guess them from %{dist} +%if !0%{?rhel} && !0%{?fedora} +%define rhel %(echo "%{?dist}" | sed -ne 's/^\\.el\\([0-9]\\+\\).*/\\1/p') +%define fedora %(echo "%{?dist}" | sed -ne 's/^\\.fc\\?\\([0-9]\\+\\).*/\\1/p') +%endif + # A client only build will create a libvirt.so only containing # the generic RPC driver, and test driver and no libvirtd # Default to a full server + client build -- 1.7.4.1

On 03/16/2011 05:54 AM, Jiri Denemark wrote:
--- If anyone knows how to make rpm to evaluate %(...) immediately and not at every appearance of rhel/fedora, that would be really cool.
It looks like %{lua: expression} is the trick to do an immediate evaluation; my problem is that I don't know the appropriate lua expression to replace your sed script. http://www.rpm.org/wiki/PackagerDocs/RpmLua
+++ b/libvirt.spec.in @@ -1,5 +1,11 @@ # -*- rpm-spec -*-
+# if neither fedora nor rhel was defined, try to guess them from %{dist} +%if !0%{?rhel} && !0%{?fedora} +%define rhel %(echo "%{?dist}" | sed -ne 's/^\\.el\\([0-9]\\+\\).*/\\1/p') +%define fedora %(echo "%{?dist}" | sed -ne 's/^\\.fc\\?\\([0-9]\\+\\).*/\\1/p') +%endif
This looks reasonable as is, but like you said, it would be nicer to compute it once. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

On 03/16/2011 08:34 AM, Eric Blake wrote:
On 03/16/2011 05:54 AM, Jiri Denemark wrote:
--- If anyone knows how to make rpm to evaluate %(...) immediately and not at every appearance of rhel/fedora, that would be really cool.
It looks like %{lua: expression} is the trick to do an immediate evaluation; my problem is that I don't know the appropriate lua expression to replace your sed script.
http://www.rpm.org/wiki/PackagerDocs/RpmLua
+++ b/libvirt.spec.in @@ -1,5 +1,11 @@ # -*- rpm-spec -*-
+# if neither fedora nor rhel was defined, try to guess them from %{dist} +%if !0%{?rhel} && !0%{?fedora} +%define rhel %(echo "%{?dist}" | sed -ne 's/^\\.el\\([0-9]\\+\\).*/\\1/p') +%define fedora %(echo "%{?dist}" | sed -ne 's/^\\.fc\\?\\([0-9]\\+\\).*/\\1/p') +%endif
This looks reasonable as is, but like you said, it would be nicer to compute it once.
This works for one-shot evaluation: # if neither fedora nor rhel was defined, try to guess them from %{dist} %if !0%{?rhel} && !0%{?fedora} %define rhel %{lua: v=string.match(rpm.expand("%{?dist}"), "^%.el([0-9]+).*"); if v ~= nil then print(v) end} %define fedora %{lua: v=string.match(rpm.expand("%{?dist}"), "^%.fc?([0-9]+).*"); if v ~= nil then print(v) end} %endif It creates a temporary lua variable which is either nil or the matched substring, and prints only the matched substring. I tested it with lots of %{echo:...} and various manipulations of %define/%undefine. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

On Wed, Mar 16, 2011 at 09:31:13 -0600, Eric Blake wrote:
This works for one-shot evaluation:
# if neither fedora nor rhel was defined, try to guess them from %{dist} %if !0%{?rhel} && !0%{?fedora} %define rhel %{lua: v=string.match(rpm.expand("%{?dist}"), "^%.el([0-9]+).*"); if v ~= nil then print(v) end} %define fedora %{lua: v=string.match(rpm.expand("%{?dist}"), "^%.fc?([0-9]+).*"); if v ~= nil then print(v) end} %endif
Hmm, this is nice in avoiding forks but it unfortunately doesn't seem to work on RHEL-5: error: lua script failed: [string "<lua>"]:1: attempt to call field `match' (a nil value) 1< (empty) 0< %if 0 error: line 18: %if 0 Jirka

On 03/16/2011 10:23 AM, Jiri Denemark wrote:
On Wed, Mar 16, 2011 at 09:31:13 -0600, Eric Blake wrote:
This works for one-shot evaluation:
# if neither fedora nor rhel was defined, try to guess them from %{dist} %if !0%{?rhel} && !0%{?fedora} %define rhel %{lua: v=string.match(rpm.expand("%{?dist}"), "^%.el([0-9]+).*"); if v ~= nil then print(v) end} %define fedora %{lua: v=string.match(rpm.expand("%{?dist}"), "^%.fc?([0-9]+).*"); if v ~= nil then print(v) end} %endif
Hmm, this is nice in avoiding forks but it unfortunately doesn't seem to work on RHEL-5:
error: lua script failed: [string "<lua>"]:1: attempt to call field `match' (a nil value) 1< (empty) 0< %if 0 error: line 18: %if 0
Indeed - 'yum deplist rpm | grep lua' on a RHEL 5 box shows that rpm was not built with lua support back then. So we can't use lua; any solution will have to stick with what is supported in RHEL 5. Does %{expand:...} do what we want? Otherwise, I'm out of ideas for avoiding a subshell for every time the macro is expanded. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

On 03/16/2011 05:54 AM, Jiri Denemark wrote:
--- If anyone knows how to make rpm to evaluate %(...) immediately and not at every appearance of rhel/fedora, that would be really cool.
Figured it out without lua: Use %{expand:} to only encounter %define after the shell substitution has completed (that is, instead of defining a macro to be a shell substitution, we use shell substitution to conditionally define a macro to be a constant value). # If neither fedora nor rhel was defined, try to guess them from %{dist} %if !0%{?rhel} && !0%{?fedora} %{expand:%(echo "%{?dist}" | sed -ne 's/^\.el\([0-9]\+\).*/%%define rhel \1/p')} %{expand:%(echo "%{?dist}" | sed -ne 's/^\.fc\?\([0-9]\+\).*/%%define fedora \1/p')} %endif Works on RHEL 5, too, since it doesn't use lua. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

On Wed, Mar 16, 2011 at 11:37:33 -0600, Eric Blake wrote:
On 03/16/2011 05:54 AM, Jiri Denemark wrote:
--- If anyone knows how to make rpm to evaluate %(...) immediately and not at every appearance of rhel/fedora, that would be really cool.
Figured it out without lua: Use %{expand:} to only encounter %define after the shell substitution has completed (that is, instead of defining a macro to be a shell substitution, we use shell substitution to conditionally define a macro to be a constant value).
# If neither fedora nor rhel was defined, try to guess them from %{dist} %if !0%{?rhel} && !0%{?fedora} %{expand:%(echo "%{?dist}" | sed -ne 's/^\.el\([0-9]\+\).*/%%define rhel \1/p')} %{expand:%(echo "%{?dist}" | sed -ne 's/^\.fc\?\([0-9]\+\).*/%%define fedora \1/p')} %endif
Works on RHEL 5, too, since it doesn't use lua.
Ah, nice. I played with %{expand:} a bit but didn't dedicate it enough time to get this far. ACK to this version Jirka

On 03/16/2011 11:53 AM, Jiri Denemark wrote:
# If neither fedora nor rhel was defined, try to guess them from %{dist} %if !0%{?rhel} && !0%{?fedora} %{expand:%(echo "%{?dist}" | sed -ne 's/^\.el\([0-9]\+\).*/%%define rhel \1/p')} %{expand:%(echo "%{?dist}" | sed -ne 's/^\.fc\?\([0-9]\+\).*/%%define fedora \1/p')} %endif
Works on RHEL 5, too, since it doesn't use lua.
Ah, nice. I played with %{expand:} a bit but didn't dedicate it enough time to get this far.
ACK to this version
Pushed. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

When building for an older distro, it's convenient to just tell rpmbuild to define dist (for example, to .el6_0), rather than also remembering to define rhel to 6. * libvirt.spec.in: Guess %{rhel} based on %{dist}. Based on an idea by Jiri Denemark. --- In patch form: libvirt.spec.in | 8 ++++++++ 1 files changed, 8 insertions(+), 0 deletions(-) diff --git a/libvirt.spec.in b/libvirt.spec.in index 45a8fe0..1946a15 100644 --- a/libvirt.spec.in +++ b/libvirt.spec.in @@ -1,5 +1,13 @@ # -*- rpm-spec -*- +# If neither fedora nor rhel was defined, try to guess them from %{dist} +%if !0%{?rhel} && !0%{?fedora} +%{expand:%(echo "%{?dist}" | \ + sed -ne 's/^\.el\([0-9]\+\).*/%%define rhel \1/p')} +%{expand:%(echo "%{?dist}" | \ + sed -ne 's/^\.fc\?\([0-9]\+\).*/%%define fedora \1/p')} +%endif + # A client only build will create a libvirt.so only containing # the generic RPC driver, and test driver and no libvirtd # Default to a full server + client build -- 1.7.4

On Wed, Mar 16, 2011 at 12:54:38PM +0100, Jiri Denemark wrote:
--- If anyone knows how to make rpm to evaluate %(...) immediately and not at every appearance of rhel/fedora, that would be really cool.
libvirt.spec.in | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-)
diff --git a/libvirt.spec.in b/libvirt.spec.in index 02be928..bfc9206 100644 --- a/libvirt.spec.in +++ b/libvirt.spec.in @@ -1,5 +1,11 @@ # -*- rpm-spec -*-
+# if neither fedora nor rhel was defined, try to guess them from %{dist} +%if !0%{?rhel} && !0%{?fedora} +%define rhel %(echo "%{?dist}" | sed -ne 's/^\\.el\\([0-9]\\+\\).*/\\1/p') +%define fedora %(echo "%{?dist}" | sed -ne 's/^\\.fc\\?\\([0-9]\\+\\).*/\\1/p') +%endif + # A client only build will create a libvirt.so only containing # the generic RPC driver, and test driver and no libvirtd # Default to a full server + client build
What is the particular reason we need this ? %dist is set by redhat-rpm-macros, which also sets %rhel/%fedora, so I would not expect to have one but not the other Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

On Thu, Mar 17, 2011 at 11:20:15 +0000, Daniel P. Berrange wrote:
On Wed, Mar 16, 2011 at 12:54:38PM +0100, Jiri Denemark wrote:
+# if neither fedora nor rhel was defined, try to guess them from %{dist} +%if !0%{?rhel} && !0%{?fedora} +%define rhel %(echo "%{?dist}" | sed -ne 's/^\\.el\\([0-9]\\+\\).*/\\1/p') +%define fedora %(echo "%{?dist}" | sed -ne 's/^\\.fc\\?\\([0-9]\\+\\).*/\\1/p') +%endif + # A client only build will create a libvirt.so only containing # the generic RPC driver, and test driver and no libvirtd # Default to a full server + client build
What is the particular reason we need this ? %dist is set by redhat-rpm-macros, which also sets %rhel/%fedora, so I would not expect to have one but not the other
The reason is %rhel is not set at least on RHEL-5 and we want to have a single spec file which is able to build the package correctly there. Not that I was able to decode where the hack redhat-rpm-config tries to set anything like %dist or %rhel/%fedora but the macros are apparently set somewhere since, e.g., on RHEL-6 one can build a package with make rpm. This doesn't work on RHEL-5 without explicit --define "rhel 5". Jirka
participants (3)
-
Daniel P. Berrange
-
Eric Blake
-
Jiri Denemark