Skip to content

Instantly share code, notes, and snippets.

@kwk
Last active April 20, 2023 19:03
Show Gist options
  • Save kwk/01b313ecdf029a1c6b705031e3a560e2 to your computer and use it in GitHub Desktop.
Save kwk/01b313ecdf029a1c6b705031e3a560e2 to your computer and use it in GitHub Desktop.
Dynamic macro overriding

README

This will install a file called /usr/lib/rpm/macros.d/macros.myapp which will redefine some macros coming from redhat-rpm-config and from /usr/lib/rpm/macros.

make rpm
cmake_minimum_required(VERSION 3.13.4)
project(myapp)
add_executable(myapp myapp.cpp)
target_link_libraries(myapp)
install(TARGETS myapp RUNTIME DESTINATION bin)
Copyright 2023 Konrad Kleine
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
# Prepare variables
TMP = $(CURDIR)/tmp
VERSION = $(shell grep ^Version myapp.spec | sed 's/.* //')
PACKAGE = myapp-$(VERSION)
FILES = LICENSE myapp.cpp \
myapp.spec CMakeLists.txt \
myapp-regen-macros.sh
.PHONY: source, tarball, rpm, srpm, clean
source:
mkdir -p $(TMP)/SOURCES
mkdir -p $(TMP)/$(PACKAGE)
cp -a $(FILES) $(TMP)/$(PACKAGE)
tarball: source
cd $(TMP) && tar vcfj ../$(PACKAGE).tar.bz2 $(PACKAGE)
rpm: tarball
fedpkg --release f37 --name myapp local -- --noclean
srpm: tarball
fedpkg --release f37 --name myapp srpm
clean:
rm -rf $(TMP) $(PACKAGE)* x86_64 .build-*.log
#include <iostream>
int main(int argc, char *argv[]) {
std::cout << "Hello, World!" << std::endl;
return 0;
}
# See https://docs.fedoraproject.org/en-US/packaging-guidelines/#_compiler_macros
%global toolchain clang
Name: myapp
Version: 1.0.0
Release: 1%{?dist}
Summary: A simple "Hello, World!" application.
License: Apache-2.0
URL: https://github.com/kwk/hello-world
Source0: myapp-%{version}.tar.bz2
Source1: myapp-regen-macros.sh
%global rrcdir /usr/lib/rpm/redhat
BuildRequires: clang
BuildRequires: cmake
BuildRequires: git
%description
A simple "Hello, World!" application.
%prep
# Not strictly necessary but allows working on file names instead
# of source numbers in install section
%setup -c -T
cp -p %{sources} .
%autosetup -S git
%build
%cmake -DCMAKE_BUILD_TYPE=Release
%cmake_build
%{_sourcedir}/myapp-regen-macros.sh > %{_sourcedir}/macros.myapp
%install
%cmake_install
mkdir -p %{buildroot}/%{rrcdir}
mkdir -p %{buildroot}/%{_rpmmacrodir}
install -p -m0644 -D %{_sourcedir}/macros.myapp %{buildroot}%{_rpmmacrodir}/macros.myapp
install -p -m 755 -t %{buildroot}%{rrcdir} %{_sourcedir}/myapp-regen-macros.sh
%check
test "`%{buildroot}/%{_bindir}/myapp`" = "Hello, World!"
# This will be called when redhat-rpm-config is installed (which it usually is)
# or it is updated. This even is executed when myapp is installed.
%triggerin -- redhat-rpm-config
echo "Called %%triggerin scriptlet for myapp"
cp --backup %{_rpmmacrodir}/macros.myapp %{_rpmmacrodir}/macros.myapp.bak
%{rrcdir}/myapp-regen-macros.sh > %{_rpmmacrodir}/macros.myapp
%files
%license LICENSE
%{_rpmmacrodir}/macros.myapp
%{_bindir}/myapp
%{rrcdir}/myapp-regen-macros.sh
%attr(0755,-,-) %{rrcdir}/myapp-regen-macros.sh
%changelog
* Wed Mar 1 2023 John Doe <[email protected]> - 1.0.0-1
- Building step1
#!/usr/bin/bash
# This script redefines the macros that we need to overwrite based on the macro
# bodies that we anticipate.
# Adds a backslash to each new line unless it is a %{nil} line
function add_backslash() {
sed '/^%{nil}/!s,$, \\,'
}
rpm --eval "%%set_build_flags %{macrobody:set_build_flags}" \
| add_backslash
echo "%{?__llvm_pgo_instrumented_build_flags}"
echo ""
echo ""
rpm --eval "%%__spec_build_pre %{macrobody:__spec_build_pre}" \
| add_backslash
echo "%{?__llvm_pgo_instrumented_spec_build_pre}"
echo ""
echo ""
cat <<EOF
# Overriding __spec_build_post macro from /usr/lib/rpm/macros
%__spec_build_post \
%{?__llvm_pgo_instrumented_spec_build_post} \
%{___build_post}
EOF
echo ""
echo ""
rpm --eval "%%__os_install_post %{macrobody:__os_install_post}" \
| add_backslash \
| sed 's/%{nil}/%{__llvm_pgo_instrumented_os_install_post} \\\n%{nil}/'
echo ""
echo ""
rpm --eval "%%install %{macrobody:install}" \
| add_backslash \
| sed 's/%%install/%{__llvm_pgo_instrumented_install} \\\n%%install/'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment