Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
243 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
option( | ||
'profile', | ||
type: 'combo', | ||
choices: [ | ||
'default', | ||
'development' | ||
], | ||
value: 'default', | ||
description: 'The build profile for Cartero. One of "default" or "development".' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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@', | ||
] | ||
) |