Skip to content
Open
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
9 changes: 7 additions & 2 deletions doc/flatpak-builder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,13 @@

<listitem><para>
Set this timestamp as SOURCE_DATE_EPOCH to perform the
build, instead of the last modification time of the manifest.
This is available since 1.3.1.
build. This is available since 1.3.1.

Since 1.5.0, SOURCE_DATE_EPOCH is set to the fixed
UNIX timestamp 1321009871 by default. Passing a value
overrides this default. Passing <literal>0</literal>
disables setting SOURCE_DATE_EPOCH entirely in the
build environment.
</para></listitem>
</varlistentry>

Expand Down
13 changes: 9 additions & 4 deletions src/builder-context.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"# This file is autogenerated by flatpak-builder.\n" \
"*\n"

#define DEFAULT_SOURCE_DATE_EPOCH G_GINT64_CONSTANT (1321009871)

struct BuilderContext
{
GObject parent;
Expand Down Expand Up @@ -90,6 +92,7 @@ struct BuilderContext
BuilderSdkConfig *sdk_config;

BuilderAsUrlPolicy as_url_policy;
BuilderSourceDateEpochMode source_date_epoch_mode;
};

typedef struct
Expand Down Expand Up @@ -656,10 +659,12 @@ builder_context_get_source_date_epoch (BuilderContext *self)
}

void
builder_context_set_source_date_epoch (BuilderContext *self,
gint64 source_date_epoch)
builder_context_set_source_date_epoch (BuilderContext *self,
BuilderSourceDateEpochMode mode,
gint64 value)
{
self->source_date_epoch = source_date_epoch;
self->source_date_epoch_mode = mode;
self->source_date_epoch = (mode == BUILDER_SOURCE_DATE_EPOCH_OVERRIDE) ? value : DEFAULT_SOURCE_DATE_EPOCH;
}

const char *
Expand Down Expand Up @@ -1140,7 +1145,7 @@ char **
builder_context_extend_env_pre (BuilderContext *self,
char **envp)
{
if (self->source_date_epoch != 0)
if (self->source_date_epoch_mode != BUILDER_SOURCE_DATE_EPOCH_UNSET)
{
g_autofree char *s_d_e = g_strdup_printf ("%" G_GUINT64_FORMAT, self->source_date_epoch);
envp = g_environ_setenv (envp, "SOURCE_DATE_EPOCH", s_d_e, FALSE);
Expand Down
12 changes: 10 additions & 2 deletions src/builder-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ typedef enum {
BUILDER_AS_URL_POLICY_FULL,
} BuilderAsUrlPolicy;

typedef enum {
BUILDER_SOURCE_DATE_EPOCH_DEFAULT,
BUILDER_SOURCE_DATE_EPOCH_OVERRIDE,
BUILDER_SOURCE_DATE_EPOCH_UNSET,
} BuilderSourceDateEpochMode;


/* Same as SOUP_HTTP_URI_FLAGS, means all possible flags for http uris */

#if GLIB_CHECK_VERSION (2, 68, 0)
Expand Down Expand Up @@ -92,8 +99,9 @@ const char * builder_context_get_default_branch (BuilderContext *self);
void builder_context_set_default_branch (BuilderContext *self,
const char *default_branch);
gint64 builder_context_get_source_date_epoch (BuilderContext *self);
void builder_context_set_source_date_epoch (BuilderContext *self,
gint64 source_date_epoch);
void builder_context_set_source_date_epoch (BuilderContext *self,
BuilderSourceDateEpochMode mode,
gint64 value);
const char * builder_context_get_stop_at (BuilderContext *self);
void builder_context_set_stop_at (BuilderContext *self,
const char *module);
Expand Down
16 changes: 10 additions & 6 deletions src/builder-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
#include "builder-utils.h"
#include "builder-git.h"

#define SOURCE_DATE_EPOCH_DISABLE G_GINT64_CONSTANT (0) /* Disable setting SOURCE_DATE_EPOCH entirely */
#define SOURCE_DATE_EPOCH_DEFAULT G_GINT64_CONSTANT (-1) /* Default when --override-source-date-epoch is not passed */

static gboolean opt_verbose;
static gboolean opt_version;
static gboolean opt_run;
Expand Down Expand Up @@ -88,7 +91,7 @@ static char *opt_installation;
static gboolean opt_log_session_bus;
static gboolean opt_log_system_bus;
static gboolean opt_yes;
static gint64 opt_source_date_epoch = -1;
static gint64 opt_source_date_epoch = SOURCE_DATE_EPOCH_DEFAULT;
static gchar *opt_as_url_policy = NULL;

static GOptionEntry entries[] = {
Expand Down Expand Up @@ -490,7 +493,6 @@ main (int argc,
int i, first_non_arg, orig_argc;
int argnr;
char *p;
struct stat statbuf;
gsize manifest_length;

setlocale (LC_ALL, "");
Expand Down Expand Up @@ -767,10 +769,12 @@ main (int argc,
return 1;
}

if (opt_source_date_epoch > -1)
builder_context_set_source_date_epoch (build_context, opt_source_date_epoch);
else if (stat (flatpak_file_get_path_cached (manifest_file), &statbuf) == 0)
builder_context_set_source_date_epoch (build_context, (gint64)statbuf.st_mtime);
if (opt_source_date_epoch == SOURCE_DATE_EPOCH_DISABLE)
builder_context_set_source_date_epoch (build_context, BUILDER_SOURCE_DATE_EPOCH_UNSET, 0);
else if (opt_source_date_epoch == SOURCE_DATE_EPOCH_DEFAULT)
builder_context_set_source_date_epoch (build_context, BUILDER_SOURCE_DATE_EPOCH_DEFAULT, 0);
else
builder_context_set_source_date_epoch (build_context, BUILDER_SOURCE_DATE_EPOCH_OVERRIDE, opt_source_date_epoch);

manifest_sha256 = g_compute_checksum_for_string (G_CHECKSUM_SHA256, manifest_contents, -1);

Expand Down
1 change: 1 addition & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test_names = [
'test-builder-flags',
'test-builder-cleanup',
'test-builder-licence-paths',
'test-builder-src-date-epoch',
]

tap_test = find_program(
Expand Down
101 changes: 101 additions & 0 deletions tests/test-builder-src-date-epoch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/bin/bash
#
# Copyright (C) 2026 Boudhayan Bhattacharya <bbhtt@bbhtt.in>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

set -euo pipefail

. $(dirname $0)/libtest.sh

skip_without_fuse

echo "1..3"

setup_repo
install_repo
setup_sdk_repo
install_sdk_repo

cd "$TEST_DATA_DIR"

run_build () {
local manifest=$1
${FLATPAK_BUILDER} --force-clean appdir "$manifest" >&2
}

cat > test-src-date-epoch-default.json <<'EOF'
{
"app-id": "org.test.src_date_epoch_default",
"runtime": "org.test.Platform",
"sdk": "org.test.Sdk",
"modules": [{
"name": "test",
"buildsystem": "simple",
"build-commands": [
"echo ${SOURCE_DATE_EPOCH} > /app/sde_out"
]
}]
}
EOF

run_build test-src-date-epoch-default.json

assert_file_has_content appdir/files/sde_out '^1321009871$'

echo "ok source-date-epoch default fixed epoch is set"

cat > test-src-date-epoch-override.json <<'EOF'
{
"app-id": "org.test.src_date_epoch_override",
"runtime": "org.test.Platform",
"sdk": "org.test.Sdk",
"modules": [{
"name": "test",
"buildsystem": "simple",
"build-commands": [
"echo ${SOURCE_DATE_EPOCH} > /app/sde_out"
]
}]
}
EOF

${FLATPAK_BUILDER} --force-clean --override-source-date-epoch=1234567890 appdir test-src-date-epoch-override.json >&2

assert_file_has_content appdir/files/sde_out '^1234567890$'

echo "ok source-date-epoch override value is used"

cat > test-src-date-epoch-disable.json <<'EOF'
{
"app-id": "org.test.src_date_epoch_disable",
"runtime": "org.test.Platform",
"sdk": "org.test.Sdk",
"modules": [{
"name": "test",
"buildsystem": "simple",
"build-commands": [
"echo ${SOURCE_DATE_EPOCH:-unset} > /app/sde_out"
]
}]
}
EOF

${FLATPAK_BUILDER} --force-clean --override-source-date-epoch=0 appdir test-src-date-epoch-disable.json >&2

assert_file_has_content appdir/files/sde_out '^unset$'

echo "ok source-date-epoch is unset when disabled with 0"