From 23c26617cbd073847c5f6d819157773d9272b41c Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Thu, 4 Sep 2014 22:15:02 -0700 Subject: [PATCH] Avoid building as root with `sudo make install` When running `sudo make install`, we only want to run the actual install as root, the building of the documentation and the distribution folder should happen as the non-root user. Related to #13728. --- mk/install.mk | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/mk/install.mk b/mk/install.mk index ddbbac495433d..88b451f661af2 100644 --- a/mk/install.mk +++ b/mk/install.mk @@ -14,16 +14,34 @@ else MAYBE_DISABLE_VERIFY= endif -install: dist-install-dir-$(CFG_BUILD) | tmp/empty_dir +install: +ifeq (root user, $(USER) $(patsubst %,user,$(SUDO_USER))) +# Build the dist as the original user + $(Q)sudo -u "$$SUDO_USER" $(MAKE) prepare_install +else + $(Q)$(MAKE) prepare_install +endif $(Q)cd tmp/empty_dir && sh ../../tmp/dist/$(PKG_NAME)-$(CFG_BUILD)/install.sh --prefix="$(DESTDIR)$(CFG_PREFIX)" --libdir="$(DESTDIR)$(CFG_LIBDIR)" --mandir="$(DESTDIR)$(CFG_MANDIR)" "$(MAYBE_DISABLE_VERIFY)" -# Remove tmp files while we can because they may have been created under sudo +# Remove tmp files because it's a decent amount of disk space $(Q)rm -R tmp/dist -uninstall: dist-install-dir-$(CFG_BUILD) | tmp/empty_dir +prepare_install: dist-install-dir-$(CFG_BUILD) | tmp/empty_dir + +uninstall: +ifeq (root user, $(USER) $(patsubst %,user,$(SUDO_USER))) +# Build the dist as the original user + $(Q)sudo -u "$$SUDO_USER" $(MAKE) prepare_uninstall +else + $(Q)$(MAKE) prepare_uninstall +endif $(Q)cd tmp/empty_dir && sh ../../tmp/dist/$(PKG_NAME)-$(CFG_BUILD)/install.sh --uninstall --prefix="$(DESTDIR)$(CFG_PREFIX)" --libdir="$(DESTDIR)$(CFG_LIBDIR)" --mandir="$(DESTDIR)$(CFG_MANDIR)" -# Remove tmp files while we can because they may have been created under sudo +# Remove tmp files because it's a decent amount of disk space $(Q)rm -R tmp/dist +prepare_uninstall: dist-install-dir-$(CFG_BUILD) | tmp/empty_dir + +.PHONY: install prepare_install uninstall prepare_uninstall + tmp/empty_dir: mkdir -p $@