How to prompt user for [y/n] in my makefile (with pure make syntaxis)?

K.Mulier

I am writing a makefile that should work on both Windows and Linux. So wherever possible, I avoid OS-specific shell commands.

Here is a snippet from my makefile with the clean function at the end:

# OS specific part
# -----------------
ifeq ($(OS),Windows_NT)
    RM = del /F /Q
    RMDIR = -RMDIR /S /Q
    MKDIR = -mkdir
    ERRIGNORE = 2>NUL || (exit 0)
    SEP=\\
else
    RM = rm -rf 
    RMDIR = rm -rf 
    MKDIR = mkdir -p
    ERRIGNORE = 2>/dev/null
    SEP=/
endif
PSEP = $(strip $(SEP))

# Definitions for nullstring and space
# -------------------------------------
nullstring :=
space := $(nullstring) #End

# Lists of all files and folders to keep or remove
# -------------------------------------------------
buildFiles_toKeep := ...    # I wrote some scripts to
buildDirs_toKeep := ...     # automatically generate
buildFiles_toRemove := ...  # these lists. But that would lead
buildDirs_toRemove := ...   # us too far here.


.PHONY: clean
clean:
    @echo.
    @echo ----------------------------------------------------------
    @echo.$(space)        __         **************    $(space)
    @echo.$(space)      __\ \___     * make clean *    $(space)
    @echo.$(space)      \ _ _ _ \    **************    $(space)
    @echo.$(space)       \_`_`_`_\                     $(space)
    @echo.$(space)                                     $(space)
    @echo.$(space)Keep these files:
    @echo.$(space)  $(buildFiles_toKeep)
    @echo.$(space)
    @echo $(space)Keep these directories:
    @echo.$(space)  $(buildDirs_toKeep)
    @echo.$(space)
    @echo.$(space)Remove these files:
    @echo.$(space)  $(buildFiles_toRemove)
    $(RM) $(buildFiles_toRemove)
    @echo.
    @echo.$(space)Remove these directories:
    @echo.$(space)  $(buildDirs_toRemove)
    $(RMDIR) $(buildDirs_toRemove)
    @echo.
@echo ----------------------------------------------------------

This makefile works great. Both on Windows and Linux, it replaces $(RM) and $(RMDIR) with the proper shell commands to delete files and folders. But I would like to prompt the user such that he/she can press Y or N. I would not want to delete files he/she wants to keep. I've tried to insert some batch-commands into the recipees of the clean target that prompt the user for input. But the prompt is not shown. Perhaps because GNU make defers the input stream.

I wonder if it is possible to generate a [Y/N] prompt with 'pure' make syntaxis (no OS-specific shell commands). I know that the make language has its limitations. Perhaps a clever solution can work on one operating system (eg. Linux) and be ported with minimal overhead to another (eg. Windows).

Anyone an idea?


EDIT :

I got referred to this link: How do I get `make` to prompt the user for a password and store it in a Makefile variable?

Gnu make will create the variable PASSWORD by prompting the user:

$ cat Makefile 
PASSWORD ?= $(shell bash -c 'read -s -p "Password: " pwd; echo $$pwd')

This way of prompting the user for input works fine as long as you want the prompt to appear at the moment your makefile is parsed. But my case is different. I want to prompt the user when the makefile is already running, in other words, when the recipees in the makefile are executing.


EDIT :

Prompting the user won't be possible when you run make multi-threaded. So I'm perfectly fine with calling the clean function single-threaded:

    >> make clean -j1

After all, the clean function doesn't take long to finish. I do not intend to prompt the user for anything in the build function, so that can be executed multi-threaded :-)

    >> make all -j8 --output-sync=target
user657267

Conceptually make is a very simple application with one purpose - build a tree of dependencies, and remake stuff that has a newer ancestor. Interaction shouldn't be a factor so make doesn't provide it natively, ideally make should already have all the information it needs. If you really need to you can work around this with shell, !=, or even provide your own extension using guile or load.

This doesn't really apply to the clean rule though, because clean doesn't remake anything in the first place, it's just a quick hack to allow you to conveniently express a non-make operation using make syntax.

Personally I don't see the value of prompting the user on file deletion, unless you're about to delete things you aren't responsible for, which is already an antipattern in itself.

If you're absolutely positive you need this then wrap the clean recipe in a script and provide two versions for bash and windows. You could also just assume that anyone running GNU make on windows is already using MSYS2, Cygwin, or MS's sanctioned release of bash for Windows 10, and forgo the cmd / powershell script altogether.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Makefiles: reading a file with 'pure make syntaxis' (no shell commands)

From Dev

How to make my Makefile compatible with Cygwin? (install: invalid user ‘root’)

From Dev

How do I get `make` to prompt the user for a password and store it in a Makefile variable?

From Dev

How to i make my prompt alert accordingly to if the user has answered what i've got pre typed in the prompt?

From Dev

How to make my Linux tcsh prompt bold?

From Dev

How to make this program go back to prompt until the user exits

From Dev

How to make an exit in switch case implementation with user prompt?

From Dev

How to make an exit in switch case implementation with user prompt?

From Dev

How can I override flags in my Makefile by modifying the make command

From Dev

Make FreeRDP prompt user for username and password?

From Dev

Make FreeRDP prompt user for username and password?

From Dev

how to make WebClient prompt for credentials

From Dev

how to make terminal prompt messages?

From Dev

How can I prompt user to make a Facebook post without using Facebook SDK?

From Dev

Using PETSc on makefile within my user-defined makefile

From Dev

How do I close my prompt when the user presses the ESC button?

From Dev

In Ruby, how can I print my next prompt on the same line after user input?

From Dev

How do I close my prompt when the user presses the ESC button?

From Dev

How can I make my terminal's command prompt shorter to increase line realestate?

From Dev

How "make" command locates makefile

From Dev

How to make a proper C makefile

From Dev

How to make @current_user available in my views?

From Dev

how to make my user defined function be a builtin function?

From Dev

How do I make my nextLine() wait for the user's input?

From Dev

How to make my user's blogs visible in search engines?

From Dev

How to make user replicate my code, by making a new category?

From Dev

How to make a target in make that is itself named 'makefile'?

From Dev

How to make a target in make that is itself named 'makefile'?

From Dev

How to make this tooltip like this with pure javascript

Related Related

  1. 1

    Makefiles: reading a file with 'pure make syntaxis' (no shell commands)

  2. 2

    How to make my Makefile compatible with Cygwin? (install: invalid user ‘root’)

  3. 3

    How do I get `make` to prompt the user for a password and store it in a Makefile variable?

  4. 4

    How to i make my prompt alert accordingly to if the user has answered what i've got pre typed in the prompt?

  5. 5

    How to make my Linux tcsh prompt bold?

  6. 6

    How to make this program go back to prompt until the user exits

  7. 7

    How to make an exit in switch case implementation with user prompt?

  8. 8

    How to make an exit in switch case implementation with user prompt?

  9. 9

    How can I override flags in my Makefile by modifying the make command

  10. 10

    Make FreeRDP prompt user for username and password?

  11. 11

    Make FreeRDP prompt user for username and password?

  12. 12

    how to make WebClient prompt for credentials

  13. 13

    how to make terminal prompt messages?

  14. 14

    How can I prompt user to make a Facebook post without using Facebook SDK?

  15. 15

    Using PETSc on makefile within my user-defined makefile

  16. 16

    How do I close my prompt when the user presses the ESC button?

  17. 17

    In Ruby, how can I print my next prompt on the same line after user input?

  18. 18

    How do I close my prompt when the user presses the ESC button?

  19. 19

    How can I make my terminal's command prompt shorter to increase line realestate?

  20. 20

    How "make" command locates makefile

  21. 21

    How to make a proper C makefile

  22. 22

    How to make @current_user available in my views?

  23. 23

    how to make my user defined function be a builtin function?

  24. 24

    How do I make my nextLine() wait for the user's input?

  25. 25

    How to make my user's blogs visible in search engines?

  26. 26

    How to make user replicate my code, by making a new category?

  27. 27

    How to make a target in make that is itself named 'makefile'?

  28. 28

    How to make a target in make that is itself named 'makefile'?

  29. 29

    How to make this tooltip like this with pure javascript

HotTag

Archive