Control: tags -1 patch

Niels Thykier:
> [...]
> 
> Ok.
> 
> I am considering to do it as a part of a new compat level.  That way,
> there will be no surprises to people, who already setup their packaging
> to fiddle with HOME to something expected.
> 
> Do we need a switch/toggle for disabling this behaviour? A la:
> 
>   DEB_(MAINT_)BUILD_OPTIONS=homedir=(managed|as-provided)
> 
> Or is a new temporary writable homedir universally always what we want?
> 
> Thanks,
> ~Niels
> 

Attached is a patch of how it could look for compat 13 (sans
documentation).  I assume that HOME and XDG_* is only relevant for
upstream build time operations by default (so it is only done as a part
of the dh_auto_* actions in the patch).

Review welcome.

Thanks,
~Niels
diff --git a/lib/Debian/Debhelper/Dh_Buildsystems.pm b/lib/Debian/Debhelper/Dh_Buildsystems.pm
index d0e69d8c..80f70c3b 100644
--- a/lib/Debian/Debhelper/Dh_Buildsystems.pm
+++ b/lib/Debian/Debhelper/Dh_Buildsystems.pm
@@ -204,7 +204,7 @@ sub buildsystems_init {
 	);
 	$args{options}{$_} = $options{$_} foreach keys(%options);
 	Debian::Debhelper::Dh_Lib::init(%args);
-	Debian::Debhelper::Dh_Lib::set_buildflags();
+	Debian::Debhelper::Dh_Lib::setup_buildenv();
 	set_parallel($max_parallel);
 }
 
diff --git a/lib/Debian/Debhelper/Dh_Lib.pm b/lib/Debian/Debhelper/Dh_Lib.pm
index 47cca1fb..6ae6507b 100644
--- a/lib/Debian/Debhelper/Dh_Lib.pm
+++ b/lib/Debian/Debhelper/Dh_Lib.pm
@@ -2310,6 +2310,40 @@ sub get_source_date_epoch {
 
 # Sets environment variables from dpkg-buildflags. Avoids changing
 # any existing environment variables.
+sub setup_buildenv {
+	set_buildflags();
+	if (not compat(12)) {
+		setup_home_and_xdg_dirs();
+	}
+}
+
+sub setup_home_and_xdg_dirs {
+	my $home_dir = generated_file('_source', 'home', 0);
+	my $xdg_rundir = generated_file('_source', 'xdg-runtime-dir', 0);
+	my $creating_rundir = -d $xdg_rundir ? 0 : 1;
+	my @paths = (
+		$home_dir,
+		$xdg_rundir,
+	);
+	my @clear_env = qw(
+		XDG_CACHE_HOME
+		XDG_CONFIG_DIRS
+		XDG_CONFIG_HOME
+		XDG_DATA_HOME
+		XDG_DATA_DIRS
+	);
+	install_dir(@paths);
+	if ($creating_rundir) {
+		chmod(0700, $xdg_rundir) == 1 or warning("chmod(0700, \"$xdg_rundir\") failed: $! (ignoring)");
+	}
+	for my $envname (@clear_env) {
+		delete($ENV{$envname});
+	}
+	$ENV{'HOME'} = $home_dir;
+	$ENV{'XDG_RUNTIME_DIR'} = $xdg_rundir;
+	return;
+}
+
 sub set_buildflags {
 	return if $ENV{DH_INTERNAL_BUILDFLAGS};
 	$ENV{DH_INTERNAL_BUILDFLAGS}=1;

Reply via email to