Skip to content

Commit

Permalink
Configure Meson as the build tool
Browse files Browse the repository at this point in the history
The meson.build file is partially based on the gtk-rust-template present
at https://gitlab.gnome.org/World/Rust/gtk-rust-template, although
because I am going step by step, I still haven't enabled most of the
data/ dependencies like blueprint-compiler, glib-compile-resources or
glib-compile-schemas. They will be done in separate commits.

In this commit, therefore:

* The README file is updated to reflect how to use Meson to build the
  application. It is clunky, but I bet that it will save in the long
  term.

* A meson.build file is configured to use cargo in order to compile the
  source code. Apparently Meson likes to vendor dependencies, so
  triggering `ninja build` will cause dependencies to be re-downloaded
  and compiled, even if `cargo build` was run before.

* Some support scripts like the dist-vendor.sh script or the git hook
  that prevents committing code if the `cargo fmt` command does not
  approve the code.
  • Loading branch information
danirod committed Mar 28, 2024
1 parent 1f02ca6 commit c55fe8a
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 11 deletions.
59 changes: 48 additions & 11 deletions README.md
Expand Up @@ -39,8 +39,10 @@ On future versions, more capabilities will be added:

## How to build

> These instructions will change very soon once Meson is introduced. Don't get
> very used to them because they will change.
Note that compiling via `cargo build` and `cargo run` is not supported anymore,
since more build steps are required in order to compile the application
(translate .blp files into .ui files...). While it may work if you run the
commands on your own, it won't be pretty.

Currently, to build the application you'll have to make sure that the required
libraries are installed on your system.
Expand All @@ -49,18 +51,53 @@ libraries are installed on your system.
* gtk >= 4.14
* gtksourceview >= 5.12

To compile the application, run `cargo build`.
### Meson

To optimize the application, run `cargo build --release`.
Make sure that you have Meson in your system. For instance,

The binary will be generated at the proper subdirectory inside `target`, or you
can run it using `cargo run`.
```sh
sudo apt install meson
sudo dnf install meson
sudo pacman -S meson
```

Also, if you want to modify the Blueprint files, you are going to need
**blueprint-compiler >= 0.10**. You might be able to get it from your package
manager or you can [build it from sources][bp]. Invoke the compiler and make
sure you update the .ui files from the .blp source files before compiling the
application.
Then use the following commands to build and install the application

```sh
meson setup build
ninja -C build
ninja -C build install
```

To avoid installing system-wide the application, you can use a prefix:

```sh
meson setup build --prefix=~/.local
ninja -C build
ninja -C build install
```

During development mode, you may also prefer to install into the development
directory. Because the application may depend on files generated by Meson that
are installed into specific locations, this may be a required step if you
suddenly cannot run the application due to missing schemas or resources.

```sh
meson setup build --prefix=install
ninja -C build
ninja -C build install
install/bin/cartero
```

**If you plan on contributing to the project**, use the development profile.

```sh
meson setup build -Dprofile=development
```

It will also configure a Git hook so that the source code is checked prior to
authoring a Git commit. The hook runs `cargo fmt` to assert that the code is
formatted. Read `hooks/pre-commit.hook` to inspect what the script does.

## Contributing

Expand Down
15 changes: 15 additions & 0 deletions build-aux/dist-vendor.sh
@@ -0,0 +1,15 @@
#!/bin/sh
# Since Meson invokes this script as
# "/bin/sh .../dist-vendor.sh DIST SOURCE_ROOT" we can't rely on bash features
set -eu
export DIST="$1"
export SOURCE_ROOT="$2"

cd "$SOURCE_ROOT"
mkdir "$DIST"/.cargo
cargo vendor > "$DIST/.cargo/config"
# Don't combine the previous and this line with a pipe because we can't catch
# errors with "set -o pipefail"
sed -i 's/^directory = ".*"/directory = "vendor"/g' "$DIST/.cargo/config"
# Move vendor into dist tarball directory
mv vendor "$DIST"
57 changes: 57 additions & 0 deletions hooks/pre-commit.hook
@@ -0,0 +1,57 @@
#!/bin/sh
# Source: https://gitlab.gnome.org/GNOME/fractal/blob/master/hooks/pre-commit.hook

install_rustfmt() {
if ! which rustup >/dev/null 2>&1; then
curl https://sh.rustup.rs -sSf | sh -s -- -y
export PATH=$PATH:$HOME/.cargo/bin
if ! which rustup >/dev/null 2>&1; then
echo "Failed to install rustup. Performing the commit without style checking."
exit 0
fi
fi

if ! rustup component list|grep rustfmt >/dev/null 2>&1; then
echo "Installing rustfmt…"
rustup component add rustfmt
fi
}

if ! which cargo >/dev/null 2>&1 || ! cargo fmt --help >/dev/null 2>&1; then
echo "Unable to check the project’s code style, because rustfmt could not be run."

if [ ! -t 1 ]; then
# No input is possible
echo "Performing commit."
exit 0
fi

echo ""
echo "y: Install rustfmt via rustup"
echo "n: Don't install rustfmt and perform the commit"
echo "Q: Don't install rustfmt and abort the commit"

echo ""
while true
do
printf "%s" "Install rustfmt via rustup? [y/n/Q]: "; read yn < /dev/tty
case $yn in
[Yy]* ) install_rustfmt; break;;
[Nn]* ) echo "Performing commit."; exit 0;;
[Qq]* | "" ) echo "Aborting commit."; exit 1 >/dev/null 2>&1;;
* ) echo "Invalid input";;
esac
done

fi

echo "--Checking style--"
cargo fmt --all -- --check
if test $? != 0; then
echo "--Checking style fail--"
echo "Please fix the above issues, either manually or by running: cargo fmt --all"

exit 1
else
echo "--Checking style pass--"
fi
65 changes: 65 additions & 0 deletions meson.build
@@ -0,0 +1,65 @@
# Copyright 2024 the Cartero authors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later

project(
'cartero',
'rust',
version: '0.1',
meson_version: '>= 0.59',
license: 'GPL-3.0-or-later',
)

base_id = 'es.danirod.Cartero'

dependency('glib-2.0', version: '>= 2.80')
dependency('gtk4', version: '>= 4.14')
dependency('gtksourceview-5', version: '>= 5.12')

cargo = find_program('cargo', required: true)

version = meson.project_version()
prefix = get_option('prefix')
bindir = prefix / get_option('bindir')

if get_option('profile') == 'development'
profile = 'Devel'
vcs_tag = run_command('git', 'rev-parse', '--short', 'HEAD', check: false).stdout().strip()
if vcs_tag == ''
version_suffix = '-devel'
else
version_suffix = '-@0@'.format(vcs_tag)
endif
application_id = '@0@.@1@'.format(base_id, profile)
else
profile = ''
version_suffix = ''
application_id = base_id
endif

meson.add_dist_script(
'build-aux/dist-vendor.sh',
meson.project_build_root() / 'meson-dist' / meson.project_name() + '-' + version,
meson.project_source_root()
)

if get_option('profile') == 'development'
# Setup pre-commit hook for ensuring coding style is always consistent
message('Setting up git pre-commit hook..')
run_command('cp', '-f', 'hooks/pre-commit.hook', '.git/hooks/pre-commit', check: false)
endif

subdir('src')
10 changes: 10 additions & 0 deletions meson_options.txt
@@ -0,0 +1,10 @@
option(
'profile',
type: 'combo',
choices: [
'default',
'development'
],
value: 'default',
description: 'The build profile for Cartero. One of "default" or "development".'
)
48 changes: 48 additions & 0 deletions src/meson.build
@@ -0,0 +1,48 @@
# Copyright 2024 the Cartero authors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later

cargo_options = [ '--manifest-path', meson.project_source_root() / 'Cargo.toml' ]
cargo_options += [ '--target-dir', meson.project_build_root() / 'src' ]

if get_option('profile') == 'default'
cargo_options += [ '--release' ]
rust_target = 'release'
message('Building in release mode')
else
rust_target = 'debug'
message('Building in debug mode')
endif

cargo_env = [ 'CARGO_HOME=' + meson.project_build_root() / 'cargo-home' ]

cargo_build = custom_target(
'cargo-build',
build_by_default: true,
build_always_stale: true,
output: meson.project_name(),
console: true,
install: true,
install_dir: bindir,
command: [
'env',
cargo_env,
cargo, 'build',
cargo_options,
'&&',
'cp', 'src' / rust_target / meson.project_name(), '@OUTPUT@',
]
)

0 comments on commit c55fe8a

Please sign in to comment.