From 804a39c63b8d0d40a90cf8c033fb75f48cf84e34 Mon Sep 17 00:00:00 2001
From: Juan RP <xtraeme@gmail.com>
Date: Thu, 9 Jan 2014 11:41:25 +0100
Subject: [PATCH] Added XBPS_ARCH environment var to override uname(2) machine
 result.

---
 bin/xbps-install/xbps-install.8 | 12 +++++++++---
 bin/xbps-query/xbps-query.8     | 12 +++++++++---
 bin/xbps-rindex/xbps-rindex.8   | 18 ++++++++++++------
 include/xbps.h.in               |  5 +++--
 lib/initend.c                   | 12 ++++++++----
 5 files changed, 41 insertions(+), 18 deletions(-)

diff --git a/bin/xbps-install/xbps-install.8 b/bin/xbps-install/xbps-install.8
index 368b6cb9..6535ad90 100644
--- a/bin/xbps-install/xbps-install.8
+++ b/bin/xbps-install/xbps-install.8
@@ -1,4 +1,4 @@
-.Dd December 4, 2013
+.Dd January 9, 2014
 .Os Void Linux
 .Dt xbps-install 8
 .Sh NAME
@@ -87,11 +87,17 @@ Assume yes to all questions and avoid interactive questions.
 Shows the XBPS version.
 .Sh ENVIRONMENT
 .Bl -tag -width XBPS_TARGET_ARCH
-.It Sy XBPS_TARGET_ARCH
+.It Sy XBPS_ARCH
 Overrides
 .Xr uname 2
-machine result with this value. Useful to install packages of a different
+machine result with this value. Useful to install packages with a fake
 architecture.
+.It Sy XBPS_TARGET_ARCH
+Sets the target architecture to this value. This variable differs from
+.Sy XBPS_ARCH
+in that it allows you to install packages partially, because
+configuration phase is skipped (the target binaries might not be compatible with
+the native architecture).
 .Sh FILES
 .Bl -tag -width /var/db/xbps/.<pkgname>.plist
 .It Ar /etc/xbps/xbps.conf
diff --git a/bin/xbps-query/xbps-query.8 b/bin/xbps-query/xbps-query.8
index 246c08c7..ba012c0a 100644
--- a/bin/xbps-query/xbps-query.8
+++ b/bin/xbps-query/xbps-query.8
@@ -1,4 +1,4 @@
-.Dd December 4, 2013
+.Dd January 9, 2014
 .Os Void Linux
 .Dt xbps-query 8
 .Sh NAME
@@ -155,11 +155,17 @@ Show the reverse dependencies for
 .Ar PKG .
 .Sh ENVIRONMENT
 .Bl -tag -width XBPS_TARGET_ARCH
-.It Sy XBPS_TARGET_ARCH
+.It Sy XBPS_ARCH
 Overrides
 .Xr uname 2
-machine result with this value. Useful to install packages of a different
+machine result with this value. Useful to install packages with a fake
 architecture.
+.It Sy XBPS_TARGET_ARCH
+Sets the target architecture to this value. This variable differs from
+.Sy XBPS_ARCH
+in that it allows you to install packages partially, because
+configuration phase is skipped (the target binaries might not be compatible with
+the native architecture).
 .Sh FILES
 .Bl -tag -width /var/db/xbps/.<pkgname>.plist
 .It Ar /etc/xbps/xbps.conf
diff --git a/bin/xbps-rindex/xbps-rindex.8 b/bin/xbps-rindex/xbps-rindex.8
index 4a7c67eb..aaacf4da 100644
--- a/bin/xbps-rindex/xbps-rindex.8
+++ b/bin/xbps-rindex/xbps-rindex.8
@@ -1,4 +1,4 @@
-.Dd November 2, 2013
+.Dd January 9, 2014
 .Os Void Linux
 .Dt xbps-rindex 8
 .Sh NAME
@@ -51,14 +51,20 @@ argument not set, it defaults to
 .Sy ~/.ssh/id_rsa .
 .Sh ENVIRONMENT
 .Bl -tag -width XBPS_TARGET_ARCH
+.It Sy XBPS_ARCH
+Overrides
+.Xr uname 2
+machine result with this value. Useful to install packages with a fake
+architecture.
+.It Sy XBPS_TARGET_ARCH
+Sets the target architecture to this value. This variable differs from
+.Sy XBPS_ARCH
+in that it allows you to install packages partially, because
+configuration phase is skipped (the target binaries might not be compatible with
+the native architecture).
 .It Sy XBPS_PASSPHRASE
 If this is set, it will use this passphrase for the RSA private key when signing
 a repository. Otherwise it will ask you to enter the passphrase on the terminal.
-.It Sy XBPS_TARGET_ARCH
-Overrides
-.Xr uname 2
-machine result with this value. Useful to install packages of a different
-architecture.
 .Sh SEE ALSO
 .Xr xbps-create 8 ,
 .Xr xbps-dgraph 8 ,
diff --git a/include/xbps.h.in b/include/xbps.h.in
index 22ba1e4c..92ec18c9 100644
--- a/include/xbps.h.in
+++ b/include/xbps.h.in
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2008-2013 Juan Romero Pardines.
+ * Copyright (c) 2008-2014 Juan Romero Pardines.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -561,7 +561,8 @@ struct xbps_handle {
 	/**
 	 * @var native_arch
 	 *
-	 * Machine architecture, as returned by uname(2)::machine.
+	 * Machine architecture, defaults to uname(2)::machine
+	 * if XBPS_ARCH is not set from environment.
 	 */
 	char native_arch[16];
 	/**
diff --git a/lib/initend.c b/lib/initend.c
index 00ef1a4d..75aa8ebf 100644
--- a/lib/initend.c
+++ b/lib/initend.c
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011-2013 Juan Romero Pardines.
+ * Copyright (c) 2011-2014 Juan Romero Pardines.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -89,7 +89,7 @@ xbps_init(struct xbps_handle *xhp)
 	};
 	struct utsname un;
 	char *buf;
-	const char *repodir;
+	const char *repodir, *native_arch;
 	int rv, cc, cch;
 	bool syslog_enabled = false;
 
@@ -170,8 +170,12 @@ xbps_init(struct xbps_handle *xhp)
 	}
 
 	xhp->target_arch = getenv("XBPS_TARGET_ARCH");
-	uname(&un);
-	strlcpy(xhp->native_arch, un.machine, sizeof(xhp->native_arch));
+	if ((native_arch = getenv("XBPS_ARCH")) != NULL) {
+		strlcpy(xhp->native_arch, native_arch, sizeof(xhp->native_arch));
+	} else {
+		uname(&un);
+		strlcpy(xhp->native_arch, un.machine, sizeof(xhp->native_arch));
+	}
 	assert(xhp->native_arch);
 
 	if (xhp->cfg == NULL) {