DEV Community

Eric F 🇺🇦
Eric F 🇺🇦

Posted on

RPM: dynamic macro for %_topdir

When building RPM packages with rpmbuild and you run rpmdev-setuptree to setup the build environment. The default macro for %_topdir is %{getenv:HOME}/rpmbuild.

If you don't want to use the default location, you can define that with --define on the command line, or define the macro in ~/.rpmmacros.

To use a custom path I've seen suggestions to use $(pwd) - which is a great way to select current path. However… If you then run rpmdev-setuptree in your home folder, forgetting about the macro. All folders end up all over your home folder.

So, here's an example to set a macro in ~/.rpmmacros to make it more dynamic - and to prevent your custom macro to run in HOME.

# RPM build
# ------------------------------------------
# rpmdev-setuptree:
#   - If run in HOME: use standard folder layout
#     Ex: HOME/rpmbuild
#   - if not: use PWD from current dir
#     Ex1: PWD/rpmbuild,
#     Ex2: $ mkdir project && cd rpmbuild/project
#          $ rpmdev-setuptree
#          HOME/rpmbuild/project/{SOURCES,SPECS,etc}
# This is mainly a protection, to prevent PWD to create
# the folders directly in HOME
%_topdir %([[ %(pwd) == %{getenv:HOME} ]] && \
    echo %{getenv:HOME}/rpmbuild || echo %(pwd))
Enter fullscreen mode Exit fullscreen mode

So, now I can use it in better and more organized way. I put all in .rpm/rpmbuild/, and all projects can live side-by-side in there.

# Example
$ cd .rpm/rpmbuild/
$ mkdir baz && cd baz
$ rpmdev-setuptree
$ mkdir X_NOTES && touch X_NOTES/__notes.txt
$ cd ..
$ ls
foo bar baz
Enter fullscreen mode Exit fullscreen mode

This setup works really great for me. The xtra X_NOTES folder is just something I find useful to keep notes in, backups, original files, logs from failed builds, etc. Whatever you dont want to put in the other folders. Keeping it a bit cleaner.

Hope you found it useful.


// Happy hacking… 👍

· Eric

Top comments (0)