diff options
author | Alexander Larsson <alexl@redhat.com> | 2016-02-17 09:45:12 +0100 |
---|---|---|
committer | Alexander Larsson <alexl@redhat.com> | 2016-02-17 10:26:35 +0100 |
commit | 6c5feb4e0549dec5de4fdd425231465463fb7490 (patch) | |
tree | eddc76830e0980a308329ca48cbfc94fe7a2f19e /builder | |
parent | c07b1807d19a2356a22daa8ab6b5cff9e3ef1f0e (diff) | |
download | xdg-app-6c5feb4e0549dec5de4fdd425231465463fb7490.tar.gz |
builder: Add support for --run to start a command in the build dir
Diffstat (limited to 'builder')
-rw-r--r-- | builder/builder-manifest.c | 80 | ||||
-rw-r--r-- | builder/builder-manifest.h | 5 | ||||
-rw-r--r-- | builder/xdg-app-builder-main.c | 37 |
3 files changed, 115 insertions, 7 deletions
diff --git a/builder/builder-manifest.c b/builder/builder-manifest.c index df48613..1a96af6 100644 --- a/builder/builder-manifest.c +++ b/builder/builder-manifest.c @@ -1758,3 +1758,83 @@ builder_manifest_create_platform (BuilderManifest *self, return TRUE; } + + +gboolean +builder_manifest_run (BuilderManifest *self, + BuilderContext *context, + char **argv, + int argc, + GError **error) +{ + g_autoptr(GSubprocessLauncher) launcher = NULL; + g_autoptr(GSubprocess) subp = NULL; + g_autoptr(GPtrArray) args = NULL; + g_autofree char *commandline = NULL; + g_autofree char *build_dir_path = NULL; + g_autofree char *ccache_dir_path = NULL; + g_auto(GStrv) env = NULL; + g_auto(GStrv) build_args = NULL; + int i; + + if (!gs_file_ensure_directory (builder_context_get_build_dir (context), TRUE, + NULL, error)) + return FALSE; + + args = g_ptr_array_new_with_free_func (g_free); + g_ptr_array_add (args, g_strdup ("xdg-app")); + g_ptr_array_add (args, g_strdup ("build")); + + build_dir_path = g_file_get_path (builder_context_get_build_dir (context)); + g_ptr_array_add (args, g_strdup_printf ("--bind-mount=/run/build=%s", build_dir_path)); + + if (g_file_query_exists (builder_context_get_ccache_dir (context), NULL)) + { + ccache_dir_path = g_file_get_path (builder_context_get_ccache_dir (context)); + g_ptr_array_add (args, g_strdup_printf ("--bind-mount=/run/ccache=%s", ccache_dir_path)); + } + + build_args = builder_options_get_build_args (self->build_options, context); + + if (build_args) + { + for (i = 0; build_args[i] != NULL; i++) + g_ptr_array_add (args, g_strdup (build_args[i])); + } + + env = builder_options_get_env (self->build_options, context); + if (env) + { + for (i = 0; env[i] != NULL; i++) + g_ptr_array_add (args, g_strdup_printf ("--env=%s", env[i])); + } + + /* Inherit all finish args except the filesystem ones so the + * command gets the same access as the final app */ + if (self->finish_args) + { + for (i = 0; self->finish_args[i] != NULL; i++) + { + const char *arg = self->finish_args[i]; + if (!g_str_has_prefix (arg, "--filesystem")) + g_ptr_array_add (args, g_strdup (arg)); + } + } + + g_ptr_array_add (args, g_file_get_path (builder_context_get_app_dir (context))); + + for (i = 0; i < argc; i++) + g_ptr_array_add (args, g_strdup (argv[i])); + g_ptr_array_add (args, NULL); + + commandline = g_strjoinv (" ", (char **) args->pdata); + + if (!execvp ((char *)args->pdata[0], (char **)args->pdata)) + { + g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "Unable to start xdg-app build"); + return FALSE; + } + + /* Not reached */ + return TRUE; +} diff --git a/builder/builder-manifest.h b/builder/builder-manifest.h index d983c37..4778705 100644 --- a/builder/builder-manifest.h +++ b/builder/builder-manifest.h @@ -62,6 +62,11 @@ gboolean builder_manifest_build (BuilderManifest *self, BuilderCache *cache, BuilderContext *context, GError **error); +gboolean builder_manifest_run (BuilderManifest *self, + BuilderContext *context, + char **argv, + int argc, + GError **error); void builder_manifest_checksum (BuilderManifest *self, BuilderCache *cache, BuilderContext *context); diff --git a/builder/xdg-app-builder-main.c b/builder/xdg-app-builder-main.c index 801e21a..1166b28 100644 --- a/builder/xdg-app-builder-main.c +++ b/builder/xdg-app-builder-main.c @@ -34,6 +34,7 @@ static gboolean opt_verbose; static gboolean opt_version; +static gboolean opt_run; static gboolean opt_disable_cache; static gboolean opt_download_only; static gboolean opt_build_only; @@ -51,6 +52,7 @@ static char **opt_key_ids; static GOptionEntry entries[] = { { "verbose", 'v', 0, G_OPTION_ARG_NONE, &opt_verbose, "Print debug information during command processing", NULL }, { "version", 0, 0, G_OPTION_ARG_NONE, &opt_version, "Print version information and exit", NULL }, + { "run", 0, 0, G_OPTION_ARG_NONE, &opt_run, "Run a command in the build directory", NULL }, { "ccache", 0, 0, G_OPTION_ARG_NONE, &opt_ccache, "Use ccache", NULL }, { "disable-cache", 0, 0, G_OPTION_ARG_NONE, &opt_disable_cache, "Disable cache", NULL }, { "disable-download", 0, 0, G_OPTION_ARG_NONE, &opt_disable_download, "Don't download any new sources", NULL }, @@ -216,13 +218,6 @@ main (int argc, base_dir = g_file_new_for_path (g_get_current_dir ()); app_dir = g_file_new_for_path (app_dir_path); - if (g_file_query_exists (app_dir, NULL) && !directory_is_empty (app_dir_path)) - { - g_printerr ("App dir '%s' is not empty. Please delete " - "the existing contents.\n", app_dir_path); - return 1; - } - build_context = builder_context_new (base_dir, app_dir); builder_context_set_keep_build_dirs (build_context, opt_keep_build_dirs); @@ -234,6 +229,34 @@ main (int argc, return 1; } + if (opt_run) + { + if (argc == 3) + return usage (context, "Program to run must be specified"); + + if (!g_file_query_exists (app_dir, NULL) || + directory_is_empty (app_dir_path)) + { + g_printerr ("App dir '%s' is empty or doesn't exist.\n", app_dir_path); + return 1; + } + + if (!builder_manifest_run (manifest, build_context, argv + 3, argc - 3, &error)) + { + g_printerr ("Error running %s: %s\n", argv[3], error->message); + return 1; + } + + return 0; + } + + if (g_file_query_exists (app_dir, NULL) && !directory_is_empty (app_dir_path)) + { + g_printerr ("App dir '%s' is not empty. Please delete " + "the existing contents.\n", app_dir_path); + return 1; + } + if (!builder_manifest_start (manifest, build_context, &error)) { g_print ("Failed to init: %s\n", error->message); |