Skip to content

Commit 286d248

Browse files
committed
builder-main: Enable ccache by default if it exists in SDK
The old `--ccache` argument is deprecated and replaced by a `--no-ccache` argument. It will be removed in some future unstable versions. Now it is enabled by default if `ccache` exists in SDK or `--ccache` is explicitly passed while disabled if it does not exist or `--no-ccache` is explicitly passed. In the first case, this also improves the previous behaviour and prevents blindly creating dangling symlinks when `ccache` does not actually exist in the SDK but someone passed `--ccache` anyway. See: #743 Fixes: #582
1 parent 6a632f0 commit 286d248

6 files changed

Lines changed: 87 additions & 15 deletions

File tree

doc/flatpak-builder.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,20 @@
367367
<term><option>--ccache</option></term>
368368

369369
<listitem><para>
370-
Enable use of ccache in the build (needs ccache in the sdk). The default ccache folder can be
371-
overridden by setting the environment variable CCACHE_DIR.
370+
This option is deprecated since 1.5.0 and kept for compatibility
371+
as ccache is enabled by default if it exists in the SDK.
372+
</para></listitem>
373+
</varlistentry>
374+
375+
<varlistentry>
376+
<term><option>--no-ccache</option></term>
377+
378+
<listitem><para>
379+
Disable use of ccache in the build.
380+
381+
ccache is enabled by default if it exists in the
382+
SDK. The default ccache folder can be overridden by
383+
setting the CCACHE_DIR environment variable.
372384
</para></listitem>
373385
</varlistentry>
374386

src/builder-context.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,38 @@ builder_context_get_sdk_config (BuilderContext *self)
12281228
return self->sdk_config;
12291229
}
12301230

1231+
gboolean
1232+
builder_context_ccache_available_in_sdk (BuilderContext *self,
1233+
const char *sdk_path)
1234+
{
1235+
static const char ccache_path[] = "files/bin/ccache";
1236+
1237+
glnx_autofd int root_dfd = -1;
1238+
glnx_autofd int fd = -1;
1239+
struct stat st;
1240+
1241+
if (!glnx_opendirat (AT_FDCWD, sdk_path, TRUE, &root_dfd, NULL))
1242+
return FALSE;
1243+
1244+
fd = glnx_chaseat (root_dfd,
1245+
ccache_path,
1246+
GLNX_CHASE_RESOLVE_BENEATH |
1247+
GLNX_CHASE_MUST_BE_REGULAR,
1248+
NULL);
1249+
if (fd < 0)
1250+
return FALSE;
1251+
1252+
if (fstat (fd, &st) < 0)
1253+
return FALSE;
1254+
1255+
if ((st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)
1256+
return FALSE;
1257+
1258+
g_print ("Found ccache at %s/%s\n", sdk_path, ccache_path);
1259+
1260+
return TRUE;
1261+
}
1262+
12311263
gboolean
12321264
builder_context_create_state_dir (BuilderContext *self,
12331265
GError **error)

src/builder-context.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ const char * builder_context_get_opt_mirror_screenshots_url (BuilderContext *
195195

196196
BuilderSdkConfig * builder_context_get_sdk_config (BuilderContext *self);
197197

198+
gboolean builder_context_ccache_available_in_sdk (BuilderContext *self,
199+
const char *sdk_path);
200+
198201
gboolean builder_context_create_state_dir (BuilderContext *self,
199202
GError **error);
200203

src/builder-main.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ static gboolean opt_show_deps;
5454
static gboolean opt_show_manifest;
5555
static gboolean opt_disable_download;
5656
static gboolean opt_disable_updates;
57-
static gboolean opt_ccache;
57+
static gboolean opt_ccache; /* deprecated, kept for compat */
58+
static gboolean opt_no_ccache;
5859
static gboolean opt_require_changes;
5960
static gboolean opt_keep_build_dirs;
6061
static gboolean opt_delete_build_dirs;
@@ -102,7 +103,8 @@ static GOptionEntry entries[] = {
102103
{ "add-tag", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_add_tags, "Add a tag to the build", "TAG"},
103104
{ "remove-tag", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_remove_tags, "Remove a tag from the build", "TAG"},
104105
{ "run", 0, 0, G_OPTION_ARG_NONE, &opt_run, "Run a command in the build directory (see --run --help)", NULL },
105-
{ "ccache", 0, 0, G_OPTION_ARG_NONE, &opt_ccache, "Use ccache", NULL },
106+
{ "ccache", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &opt_ccache, "Use ccache (deprecated as it is auto-enabled when available in SDK)", NULL },
107+
{ "no-ccache", 0, 0, G_OPTION_ARG_NONE, &opt_no_ccache, "Disable ccache use", NULL },
106108
{ "disable-cache", 0, 0, G_OPTION_ARG_NONE, &opt_disable_cache, "Disable cache lookups", NULL },
107109
{ "disable-tests", 0, 0, G_OPTION_ARG_NONE, &opt_disable_tests, "Don't run tests", NULL },
108110
{ "disable-rofiles-fuse", 0, 0, G_OPTION_ARG_NONE, &opt_disable_rofiles, "Disable rofiles-fuse use", NULL },
@@ -158,7 +160,8 @@ static GOptionEntry run_entries[] = {
158160
{ "run", 0, 0, G_OPTION_ARG_NONE, &opt_run, "Run a command in the build directory", NULL },
159161
{ "log-session-bus", 0, 0, G_OPTION_ARG_NONE, &opt_log_session_bus, N_("Log session bus calls"), NULL },
160162
{ "log-system-bus", 0, 0, G_OPTION_ARG_NONE, &opt_log_system_bus, N_("Log system bus calls"), NULL },
161-
{ "ccache", 0, 0, G_OPTION_ARG_NONE, &opt_ccache, "Use ccache", NULL },
163+
{ "ccache", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &opt_ccache, "Use ccache (deprecated as it is auto-enabled when available in SDK)", NULL },
164+
{ "no-ccache", 0, 0, G_OPTION_ARG_NONE, &opt_no_ccache, "Disable ccache use", NULL },
162165
{ "state-dir", 0, 0, G_OPTION_ARG_FILENAME, &opt_state_dir, "Use this directory for state instead of .flatpak-builder", "PATH" },
163166
{ NULL }
164167
};
@@ -690,12 +693,6 @@ main (int argc,
690693
builder_context_set_stop_at (build_context, opt_stop_at);
691694
}
692695

693-
if (!builder_context_set_enable_ccache (build_context, opt_ccache, &error))
694-
{
695-
g_printerr ("Can't initialize ccache use: %s\n", error->message);
696-
return 1;
697-
}
698-
699696
if (opt_from_git)
700697
{
701698
g_autofree char *manifest_dirname = g_path_get_dirname (manifest_rel_path);
@@ -954,6 +951,25 @@ main (int argc,
954951
return 1;
955952
}
956953

954+
if (!opt_no_ccache)
955+
{
956+
gboolean want_ccache = FALSE;
957+
const char *sdk_path = builder_manifest_get_sdk_path (manifest);
958+
959+
if (sdk_path != NULL)
960+
want_ccache = builder_context_ccache_available_in_sdk (build_context, sdk_path);
961+
962+
if (!want_ccache && opt_ccache)
963+
g_printerr ("Warning: --ccache passed but ccache not found in SDK, ignoring\n");
964+
965+
if (want_ccache &&
966+
!builder_context_set_enable_ccache (build_context, TRUE, &error))
967+
{
968+
g_printerr ("Can't initialize ccache use: %s\n", error->message);
969+
return 1;
970+
}
971+
}
972+
957973
if (!opt_finish_only &&
958974
!opt_export_only &&
959975
!opt_disable_download &&

src/builder-manifest.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ struct BuilderManifest
7373
char *runtime_version;
7474
char *sdk;
7575
char *sdk_commit;
76+
char *sdk_path;
7677
char *var;
7778
char *base;
7879
char *base_commit;
@@ -196,6 +197,7 @@ builder_manifest_finalize (GObject *object)
196197
g_free (self->runtime_version);
197198
g_free (self->sdk);
198199
g_free (self->sdk_commit);
200+
g_free (self->sdk_path);
199201
g_free (self->base);
200202
g_free (self->base_commit);
201203
g_free (self->base_version);
@@ -1481,6 +1483,12 @@ builder_manifest_get_runtime_version (BuilderManifest *self)
14811483
return self->runtime_version ? self->runtime_version : "master";
14821484
}
14831485

1486+
const char *
1487+
builder_manifest_get_sdk_path (BuilderManifest *self)
1488+
{
1489+
return self->sdk_path;
1490+
}
1491+
14841492
const char *
14851493
builder_manifest_get_branch (BuilderManifest *self,
14861494
BuilderContext *context)
@@ -1691,7 +1699,6 @@ builder_manifest_start (BuilderManifest *self,
16911699
{
16921700
g_autofree char *arch_option = NULL;
16931701
g_autoptr(GHashTable) names = g_hash_table_new (g_str_hash, g_str_equal);
1694-
g_autofree char *sdk_path = NULL;
16951702
const char *stop_at;
16961703

16971704
self->source_date_epoch = builder_context_get_source_date_epoch (context);
@@ -1718,9 +1725,10 @@ builder_manifest_start (BuilderManifest *self,
17181725
builder_manifest_get_runtime_version (self),
17191726
self->sdk_commit);
17201727

1721-
sdk_path = flatpak_info_show_path (self->sdk, builder_manifest_get_runtime_version (self), context);
1722-
if (sdk_path != NULL &&
1723-
!builder_context_load_sdk_config (context, sdk_path, error))
1728+
g_clear_pointer (&self->sdk_path, g_free);
1729+
self->sdk_path = flatpak_info_show_path (self->sdk, builder_manifest_get_runtime_version (self), context);
1730+
if (self->sdk_path != NULL &&
1731+
!builder_context_load_sdk_config (context, self->sdk_path, error))
17241732
return FALSE;
17251733

17261734
self->runtime_commit = flatpak (NULL, "info", arch_option, "--show-commit", self->runtime,

src/builder-manifest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ char * builder_manifest_get_debug_id (BuilderManifest *self);
5757
char * builder_manifest_get_sources_id (BuilderManifest *self);
5858
const char * builder_manifest_get_id_platform (BuilderManifest *self);
5959
char * builder_manifest_get_locale_id_platform (BuilderManifest *self);
60+
const char * builder_manifest_get_sdk_path (BuilderManifest *self);
6061
BuilderOptions *builder_manifest_get_build_options (BuilderManifest *self);
6162
GList * builder_manifest_get_modules (BuilderManifest *self);
6263
GList * builder_manifest_get_add_extensions (BuilderManifest *self);

0 commit comments

Comments
 (0)