Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add Cargo buildsystem #564

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/builder-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1515,8 +1515,10 @@ builder_module_build_helper (BuilderModule *self,
const char *make_cmd = NULL;
const char *test_arg = NULL;

gboolean autotools = FALSE, cmake = FALSE, cmake_ninja = FALSE, meson = FALSE, simple = FALSE, qmake = FALSE;
gboolean autotools = FALSE, cmake = FALSE, cmake_ninja = FALSE, meson = FALSE, simple = FALSE, qmake = FALSE, cargo = FALSE;
g_autoptr(GFile) configure_file = NULL;
g_autoptr(GFile) cargo_file = NULL;
g_autoptr(GFile) cargo_lock_file = NULL;
g_autoptr(GFile) build_dir = NULL;
g_autofree char *build_dir_relative = NULL;
gboolean has_configure = FALSE;
Expand Down Expand Up @@ -1585,6 +1587,8 @@ builder_module_build_helper (BuilderModule *self,
simple = TRUE;
else if (!strcmp (self->buildsystem, "qmake"))
qmake = TRUE;
else if (!strcmp (self->buildsystem, "cargo"))
cargo = TRUE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will need to be documented. See data/flatpak-manifest.schema.json and doc/flatpak-manifest.xml

else
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "module %s: Invalid buildsystem: \"%s\"",
Expand Down Expand Up @@ -1634,6 +1638,22 @@ builder_module_build_helper (BuilderModule *self,
return FALSE;
}
}
else if (cargo)
{
cargo_file = find_file_with_extension (source_subdir, "Cargo.toml");
TingPing marked this conversation as resolved.
Show resolved Hide resolved
if (cargo_file == NULL)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "module: %s: Can't find Cargo.toml", self->name);
return FALSE;
}

cargo_lock_file = find_file_with_extension (source_subdir, "Cargo.lock");
if (cargo_lock_file == NULL)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "module: %s: Can't find Cargo.lock", self->name);
return FALSE;
}
}
else if (autotools)
{
configure_file = g_file_get_child (source_subdir, "configure");
Expand Down Expand Up @@ -1741,6 +1761,10 @@ builder_module_build_helper (BuilderModule *self,
configure_cmd = "meson";
configure_final_arg = g_strdup ("..");
}
else if (cargo)
{
// nothing to do - Cargo does not have a configure step
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so how do you select features?
or building a specific binary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Just to be more specific about this, do you mean switches like:

  • Profile: cargo build --release / --debug
  • Target: cargo build --target <architecture>
  • Cargo features: cargo build --features <feature>,<feature>,...

}
else
{
configure_cmd = "../configure";
Expand Down Expand Up @@ -1872,6 +1896,11 @@ builder_module_build_helper (BuilderModule *self,
}
else if (simple)
make_cmd = NULL;
else if (cargo)
{
make_cmd = "cargo";
test_arg = "check";
}
else
{
make_cmd = "make";
Expand Down
6 changes: 6 additions & 0 deletions tests/test-cargo-without-lockfile/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"

[dependencies]
5 changes: 5 additions & 0 deletions tests/test-cargo-without-lockfile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# test-cargo-without-lockfile

This is an example of a Cargo project with no Cargo.lock file.

In this case, flatpak-builder should fail, and tell the user to generate the Cargo.lock file first.
3 changes: 3 additions & 0 deletions tests/test-cargo-without-lockfile/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
})
7 changes: 7 additions & 0 deletions tests/test-cargo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/test-cargo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"

[dependencies]
5 changes: 5 additions & 0 deletions tests/test-cargo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# test-cargo

This is an example of a valid Cargo project (with a Cargo.lock file).

In this case, flatpak-builder should succeed.
10 changes: 10 additions & 0 deletions tests/test-cargo/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
app-id: example
runtime: org.test.Platform
sdk: org.test.Sdk
command: test-cargo
modules:
- name: app
buildsystem: cargo
sources:
- type: dir
path: .
3 changes: 3 additions & 0 deletions tests/test-cargo/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}