Skip to content

Commit

Permalink
feat: build srpm with meson
Browse files Browse the repository at this point in the history
Add a "build_srpm" meson build option. When true, meson will attempt to
build a SRPM using the current source root as the RPM source.
  • Loading branch information
subpop committed Jun 9, 2023
1 parent 5611b35 commit 25647b0
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 1 deletion.
8 changes: 7 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,13 @@ sudo go run ./cmd/yggd --config ./data/yggdrasil/config.toml

Many default paths (such as Prefix, BinDir, LocalstateDir, etc), as well as some
other compile-time constants, can be specified by providing a linker `-X` flag
argument. See the `Makefile` or `constants.go` for a complete list.
argument. See the [meson built-in
options](https://mesonbuild.com/Builtin-options.html) or
`internal/constants/constants.go` for a complete list.

It is also possible to compile `yggd` and all additional programs in this
project into an RPM suitable for installation on Fedora-based distributions. See
the README in `dist/srpm` for details.

## Debugging `yggd`

Expand Down
3 changes: 3 additions & 0 deletions dist/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if get_option('build_srpm')
subdir('srpm')
endif
19 changes: 19 additions & 0 deletions dist/srpm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
This directory includes files necessary to build an SRPM as part of the normal
`meson compile` step.

# Usage

1. Set the `build_srpm` build option to `True` when setting up the project:
`meson setup -Dbuild_srpm=True builddir`
2. Compile the `srpm` target: `meson compile srpm -C builddir`

An SRPM will be built and can be found in `builddir/dist/srpm`. This SRPM will
need to be rebuilt into a binary in order to install it. To rebuild it using
`mock` on Fedora, for example, run:

```
mock --rebuild ./builddir/dist/srpm/*.src.rpm
```

The RPM built by `mock` can be found (typically) in
`/var/run/mock/<root-name>/results`.
45 changes: 45 additions & 0 deletions dist/srpm/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
git = find_program('git')
rpmbuild = find_program('rpmbuild')
tar = find_program('tar')
bash = find_program('bash')

commit = run_command(git, 'rev-parse', 'HEAD', capture: true, check: true).stdout().strip()

if get_option('vendor')
run_command([bash, files(meson.project_source_root() / 'build-aux' / 'vendor.sh')], check: true)
endif

build_root_basename = meson.project_build_root().split('/').get(-1)
tarball = custom_target('tarball',
command: [
tar,
'--create',
'--gzip',
'--absolute-names',
'--file', '@OUTPUT@',
'--transform', 's+@0@+@1@-@2@+'.format(meson.project_source_root(), meson.project_name(), commit),
'--exclude', build_root_basename,
'--exclude', '.git',
'--exclude', '.vscode',
'--exclude', '.github',
'--exclude', '.copr',
meson.project_source_root()
],
output: '@0@-@[email protected]'.format(meson.project_name(), commit)
)

specfile = configure_file(
input: 'yggdrasil.spec.in',
output: '@BASENAME@',
configuration: {
'COMMIT': commit,
'VERSION': meson.project_version(),
}
)

run_target('srpm',
command: [rpmbuild, '-bs', '--define', 'commit @0@'.format(commit), '--define', '_srcrpmdir @0@'.format(meson.current_build_dir()), '--define', '_sourcedir @0@'.format(meson.current_build_dir()), specfile],
depends: [
tarball
]
)
107 changes: 107 additions & 0 deletions dist/srpm/yggdrasil.spec.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# This RPM spec file exists primarily to enable CI/CD pipelines and automatic
# testing of built RPMs. This RPM can be used as a suitable baseline for a
# proper distribution package, but changes should be made to suit the needs of
# the package.
#
# See https://src.fedoraproject.org/rpms/yggdrasil/blob/rawhide/f/yggdrasil.spec
# for a better example of a downstream distribution packaging example.

%bcond_without check

# https://github.com/redhatinsights/yggdrasil
%global goipath github.com/redhatinsights/yggdrasil
%global commit @COMMIT@
%global shortcommit %(c=%{commit}; echo ${c:0:7})

%if 0%{?fedora}
%gometa -f
%else
%gometa
%endif

# Manually redefine %%dist to work around an issue in COPR where the build root
# that creates the srpm does not define a value for %%dist. This should *NOT* be
# carried in downstream; this is strictly an upstream/COPR/CI workaround.
%if "%{dist}" == ""
%global dist %{distprefix}.fc%{fedora}
%endif

%if 0%{?fedora}
%global setup_flags -Dvendor=False -Dexamples=True
%else
%global setup_flags -Dvendor=True -Dexamples=True
%endif

%global common_description %{expand:
yggdrasil is a system daemon that subscribes to topics on an MQTT broker and
routes any data received on the topics to an appropriate child "worker" process,
exchanging data with its worker processes through a D-Bus message broker.}

%global golicenses LICENSE
%global godocs CONTRIBUTING.md README.md

Name: yggdrasil
Version: @VERSION@
Release: 0%{?dist}
Summary: Remote data transmission and processing client

License: GPL-3.0-only
URL: %{gourl}
Source: %{gosource}

BuildRequires: systemd-rpm-macros
BuildRequires: meson
BuildRequires: pkgconfig(dbus-1)
BuildRequires: pkgconfig(systemd)
BuildRequires: pkgconfig(bash-completion)

%description %{common_description}

%gopkg

%prep
%if 0%{?fedora}
%goprep
%else
%goprep -k
%endif

%if 0%{?fedora}
%generate_buildrequires
%go_generate_buildrequires
%endif

%build
%undefine _auto_set_build_flags
export %gomodulesmode
%{?gobuilddir:export GOPATH="%{gobuilddir}:${GOPATH:+${GOPATH}:}%{?gopath}"}
%meson %setup_flags "-Dgobuildflags=[%(echo %{expand:%gocompilerflags} | sed -e s/"^"/"'"/ -e s/" "/"', '"/g -e s/"$"/"'"/), '-tags', '"rpm_crashtraceback\ ${BUILDTAGS:-}"', '-a', '-v', '-x']" -Dgoldflags='%{?currentgoldflags} -B 0x%(head -c20 /dev/urandom|od -An -tx1|tr -d " \n") -compressdwarf=false -linkmode=external -extldflags "%{build_ldflags} %{?__golang_extldflags}"'
%meson_build

%global gosupfiles ./ipc/com.redhat.Yggdrasil1.Dispatcher1.xml ./ipc/com.redhat.Yggdrasil1.Worker1.xml
%install
%meson_install
%gopkginstall

%if %{with check}
%check
%gocheck
%endif

%files
%license LICENSE
%doc CONTRIBUTING.md README.md
%{_bindir}/*
%{_libexecdir}/%{name}/*
%config(noreplace) %{_sysconfdir}/%{name}
%{_unitdir}/*
%{_userunitdir}/*
%{_datadir}/bash-completion/completions/*
%{_datadir}/dbus-1/{interfaces,system-services,system.d}/*
%{_datadir}/doc/%{name}/*
%{_mandir}/man1/*

%gopkgfiles

%changelog
%autochangelog
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ subdir('cmd/yggd')
subdir('data')
subdir('dbus')
subdir('doc')
subdir('dist')
subdir('ipc')

if get_option('examples')
Expand Down
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ option('vendor', type: 'boolean', value: false, description: 'Bundle go module d
option('examples', type: 'boolean', value: false, description: 'Build and install the example workers')
option('gobuildflags', type: 'array', value: ['-buildmode', 'pie'], description: 'Additional build flags to be passed to the Go compiler')
option('goldflags', type: 'string', value: '', description: 'Additional linker flags to be passed to the Go compiler')
option('build_srpm', type: 'boolean', value: false, description: 'Enable SRPM builds')

0 comments on commit 25647b0

Please sign in to comment.