Clean handling of file path with spaces in Makefile

Viewed 142

I have a script that pulls in data from a server drive where I do not have the ability to alter the directory names and they all have spaces in them. I am using a Makefile to run the script (in Windows) and it is presenting a problem.

My initial workaround is having a python script run before make is called to copy the data from the server into my local folder, and it looks like this:

# grab_data.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output", help="output filepath")
args = parser.parse_args()
output_path = Path(args.output)

src = 'S:/Server Path/To Data I Need/File I Need.xlsx'
dst = output_path
shutil.copyfile(src, dst)

And I run my Makefile like this:

.PHONY : runall
runall : data/file_i_need.xlsx final_output.csv
    python grab_data.py - o data/file_i_need.xlsx

final_output.csv : data/file_i_need.xlsx processing_script.py
    python processing_script.py -i $< -o $@

I want to find some way to include the file 'S:/Server Path/To Data I Need/File I Need.xlsx' directly in the Makefile but cannot figure out what will work. Is there some other workaround that would allow me to do this?

1 Answers

Not sure what version of make you're using, but with gmake (which likely is one of the options around Windows) escaping spaces would work:

x\ yz: ab\ c
        cp "$<" "$@"

Then you get:

$ make
cp "ab c" "x yz" 

As pointed out in the comment, to not forgo it as too obvious. I've also double-quoted variables used in the recipe to make sure those are correctly passed through as a whole string. If a rule has multiple prerequisites and only one of them contains space(s), it's still the same story, you just need to make it the first prerequisite so that you can refer to "$<". If you had multiple prerequisites with spaces and wanted to refer to all of them, I fear you may be out of luck even trying to expand $^ with $(foreach ...) won't help.

Related