How to handle files with spaces in a Makefile?

Therefore, some anonymous developers decided to use a ridiculous agreement on the use of spaces in the names of folders containing their source files. I would change these folders so as not to use spaces, but unfortunately I am not making the rules here, so this is not an option (although I would like it to be).

LUAC = luac SRC_DIR = . SOURCE = \ stupid/naming\ convention/a.lua \ stupid/naming\ convention/very\ annoying/b.lua \ vpath .lua $(SRC_DIR) OUT_DIR = ../out/ OUTPUT = $(patsubst %.lua, $(OUT_DIR)/%.luac, $(SOURCE)) all: $(OUTPUT) $(OUT_DIR)/%.luac: %.lua $(LUAC) "$<" mv luac.out " $@ " .PHONY: all 

Simple makefile. All he wanted to do was compile all the Lua files that I have and put them in the output directory.

No matter what I do, it continues to split the SOURCE line into spaces in the folder, so I end up with a nice error, for example:

 make: *** No rule to make target `stupid/naming ', needed by `all'. Stop. 

Is there any way to fix this without renaming folders?

Thanks in advance.

+4
source share
3 answers

Very short, but IMO is ultimately correct, the answer is that make (not just GNU make, but all POSIX-style implementations) does not support paths that contain spaces. If you want to use make, your "anonymous developers" simply cannot use them. If they insist that this is an absolute requirement, you should completely switch to another build tool that supports spaces in file names.

Yes, it’s hardly possible to create a makefile that works with names containing spaces, but you will essentially have to rewrite all your makefiles from scratch, and you won’t be able to use many GNU functions to make your makefiles long, difficult to reading and difficult to maintain.

Just tell them to get over. Or, if they really cannot, try creating their workspace in the path name without any spaces in the names, and then create a symlink containing spaces indicating the real workspace (otherwise this will not work in all situations).

+2
source

Unfortunately, GNU Make functions that deal with a spatially separated list do not respect space evacuation . The one exception is the wildcard .

Edit:

Here is my workaround:

 LUAC = luac SRC_DIR = . SOURCE = \ stupid/naming\ convention/a.lua \ stupid/naming\ convention/very\ annoying/b.lua \ vpath .lua $(SRC_DIR) OUT_DIR = ../out/ OUTPUT = $(patsubst %.lua,%.luac,$(SOURCE)) all: $(OUTPUT) %.luac: %.lua $(LUAC) "$<" mv luac.out " $@ "" .PHONY: all 

I tried to output it like this first:

 %.luac: %.lua @echo "$<" @echo " $@ "" 

The result is as follows:

 stupid/naming convention/a.lua ../out/stupid/naming convention/a.luac stupid/naming convention/very annoying/b.lua ../out/stupid/naming convention/very annoying/b.luac 
0
source

If you look at this excellent post: http://www.cmcrossroads.com/article/gnu-make-meets-file-names-spaces-them , the author suggests that this is basically a daunting task. But its substitution functions can make you move if you really cannot escape the gaps.

Putting this into your makefile will look like this (sorry if I changed some of your paths, but this works on my Cygwin installation):

 LUAC = luac s+ = $(subst \\ ,+,$1) +s = $(subst +,\ ,$1) SRC_DIR = . SOURCE := stupid/naming\\ convention/a.lua SOURCE := $(call s+,$(SOURCE)) vpath .lua $(SRC_DIR) OUT_DIR = out/ OUTPUT = $(patsubst %.lua, $(OUT_DIR)/%.luac, $(SOURCE)) all: $(call +s,$(OUTPUT)) $(OUT_DIR)/%.luac: %.lua $(LUAC) "$<" mv luac.out " $@ " .PHONY: all 

I know that is not a complete answer, but perhaps encouraging that it is really possible. But I agree with other posters that if you can avoid spaces at all, it will be much easier for you to live!

0
source

Source: https://habr.com/ru/post/1494454/


All Articles