Skip to content

Commit 23c2661

Browse files
committed
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.
1 parent 5248b11 commit 23c2661

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

mk/install.mk

+22-4
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,34 @@ else
1414
MAYBE_DISABLE_VERIFY=
1515
endif
1616

17-
install: dist-install-dir-$(CFG_BUILD) | tmp/empty_dir
17+
install:
18+
ifeq (root user, $(USER) $(patsubst %,user,$(SUDO_USER)))
19+
# Build the dist as the original user
20+
$(Q)sudo -u "$$SUDO_USER" $(MAKE) prepare_install
21+
else
22+
$(Q)$(MAKE) prepare_install
23+
endif
1824
$(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)"
19-
# Remove tmp files while we can because they may have been created under sudo
25+
# Remove tmp files because it's a decent amount of disk space
2026
$(Q)rm -R tmp/dist
2127

22-
uninstall: dist-install-dir-$(CFG_BUILD) | tmp/empty_dir
28+
prepare_install: dist-install-dir-$(CFG_BUILD) | tmp/empty_dir
29+
30+
uninstall:
31+
ifeq (root user, $(USER) $(patsubst %,user,$(SUDO_USER)))
32+
# Build the dist as the original user
33+
$(Q)sudo -u "$$SUDO_USER" $(MAKE) prepare_uninstall
34+
else
35+
$(Q)$(MAKE) prepare_uninstall
36+
endif
2337
$(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)"
24-
# Remove tmp files while we can because they may have been created under sudo
38+
# Remove tmp files because it's a decent amount of disk space
2539
$(Q)rm -R tmp/dist
2640

41+
prepare_uninstall: dist-install-dir-$(CFG_BUILD) | tmp/empty_dir
42+
43+
.PHONY: install prepare_install uninstall prepare_uninstall
44+
2745
tmp/empty_dir:
2846
mkdir -p $@
2947

0 commit comments

Comments
 (0)