Is this a good way to become root in a makefile?

Is this a good way to become root in a makefile?

SHELL = / bin / sh
INSTDIR = / usr / bin /
OBJS = main.o file.o gen.o stat.o

program1: $ (OBJS)
        gcc -o program1 $ (OBJS)

main.o: main.c file.h gen.h stat.h
        gcc -c main.c

file.o: file.c file.h
        gcc -c file.c

gen.o: gen.c gen.h
        gcc -c gen.c

stat.o: stat.c stat.h
        gcc -c stat.c

clean:
        rm -f $ (OBJS) program1

install:
        @if [-f program1]; then \
                if [$$ (id -u) -eq 0]; then \
                        cp program1 $ (INSTDIR) && \
                        echo "Installed in $ (INSTDIR)"; \
                else \
                        (sudo cp program1 $ (INSTDIR) 2> / dev / null && \
                        echo "Installed in $ (INSTDIR)") || \
                        (echo 'No sudo on this machine, trying su.' \
                        && su -c "cp program1 $ (INSTDIR)" && \
                        echo "Installed in $ (INSTDIR)"); \
                fi; \
        else \
                echo "There was no program to install, run make." ; \
        fi

uninstall:
        @if [-f $ (INSTDIR) program1]; then \
                if [$$ (id -u) -eq 0]; then \
                        rm $ (INSTDIR) program1 && \
                        echo "Uninstalled in $ (INSTDIR)"; \
                else \
                        (sudo rm $ (INSTDIR) program1 2> / dev / null && \
                        echo "Uninstalled in $ (INSTDIR)") || \
                        (echo 'No sudo on this machine, trying su.' \
                        && su -c "rm $ (INSTDIR) program1" && \
                        echo "Uninstalled in $ (INSTDIR)"); \
                fi; \
        else \
                echo "There was no program to remove." ; \
        fi
+2


source to share


1 answer


NOT!



First, usually users sudo make install

. Secondly, you have not thought about cases where the program is installed in a user directory that does not always require root privileges. For example, packaging tools use this feature and do not have root privileges.

+19


source







All Articles