4

I am using GNU Make (v3.80) and I must deal with file names with space. (There is nothing that I can do about the file names per se.) My makefile includes "normal" rules as well as static pattern rules. How can I deal with these files without changing too much my makefile?

I have read and studied previous related questions (here, here, here, and there) but none handle static pattern rules.

Community
  • 1
  • 1

1 Answers1

4

A solution combines the "question mark" trick by Mecklenburg combined with shell commands (find and sed) and not using the automatic variable $< but rather $*.

Imagine that you want to copy any files found in a data directory into a target directory, here are the rules (including a static pattern rule). Below is the complete makefile, here are the interesting bits:

EXEDIR     = bin/vbcc-classic/AmiModRadio/
DATADIR    = $(EXEDIR)data/
DATA       = $(addprefix $(DATADIR),    $(shell find data/   -mindepth 1 -maxdepth 1 -printf "%f\n"         | sed 's/ /?/g'))

In the above, the last variable declaration, DATA contains the set of file names available in data/ with any space in their file names replaced by a ?. Each file name is prefixed by the target directory DATADIR and delimited by spaces.

space :=
space +=
replaceQuestionBySpace = $(subst ?,$(space),$1)
replaceSpaceByQuestion = $(subst $(space),?,$1)

In the above, following Mecklenburg, two functions are defined to replace spaces with question marks and vice-versa.

$(DATA) : $(DATADIR)% : $(wildcard data/%)
    cp -f -R "$(call replaceQuestionBySpace,data/$*)" "$(call replaceQuestionBySpace,$@)"

The above is the important static pattern rule: the prerequisite of the rule $(wildcard data/%) ensures that Make will not copy the files in data at each call, but only if they have changed or disappeared from $(DATADIR).

$(wildcard data/%) works because % is replaced by the stem built by $(DATA) : $(DATADIR)% and because $(DATA) contains a set of file names without space (but question marks) and prefixed by $(DATADIR).

In the recipe, question marks are replaced by spaces to get the original file names back. $< cannot be used because it would match to $(wildcard data/%), which would result in an empty string. But, because the target contains $(DATADIR) and the file names, $* can be used (the file names) at the cost of adding data/ in front.


The complete makefile for example.

#
#    AmiModRadio
#    All of Aminet modules at your fingertips
#
#
#
#    Copyright 2015, 2016 Tygre <tygre@chingu.asia>
#
#    This file is part of AmiModRadio.
#
#    AmiModRadio is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    AmiModRadio is distributed in the hope fthat it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with AmiModRadio. If not, see <http://www.gnu.org/licenses/>.
#
#
#
#    Entirely developed on an Amiga!
#    (Except for source code versioning...)
#    tygre@chingu.asia
#

# Main paths
EXEDIR     = bin/vbcc-classic/debug/
OBJECTSDIR = o/vbcc-classic/debug/

# Main paths for release ONLY
ifeq "$(strip $(filter dist, $(MAKECMDGOALS)))" "dist"
EXEDIR     = bin/vbcc-classic/AmiModRadio/
OBJECTSDIR = o/vbcc-classic/release/
endif

# Secondary paths
EXE        = $(EXEDIR)AmiModRadio
DATADIR    = $(EXEDIR)data/
ICONSDIR   = $(EXEDIR)icons/
IMAGESDIR  = $(EXEDIR)images/
MODULESDIR = $(EXEDIR)modules/

# (Re)Source files
SOURCES    = $(wildcard *.c)
OBJECTS    = $(addprefix $(OBJECTSDIR), $(SOURCES:.c=.o))
DATA       = $(addprefix $(DATADIR),    $(shell find data/   -mindepth 1 -maxdepth 1 -printf "%f\n"         | sed 's/ /?/g'))
ICONS      = $(addprefix $(ICONSDIR),   $(shell find icons/  -mindepth 1 -maxdepth 1 -printf "%f\n"         | sed 's/ /?/g'))
IMAGES     = $(addprefix $(IMAGESDIR),  $(shell find images/ -mindepth 1 -maxdepth 1 -printf "%f\n" -type d | sed 's/ /?/g'))

# compiler and linker
CC         = vbcc:bin/vc
LD         = vbcc:bin/vc



# Because Make does not support spaces properly...
space :=
space +=
replaceQuestionBySpace = $(subst ?,$(space),$1)
replaceSpaceByQuestion = $(subst $(space),?,$1)



# ----------------------------------------

# target 'all' (default target, for debug)
all :  CFLAGS += -DFORTIFY
all :  $(EXEDIR)\
       $(OBJECTSDIR)\
       $(EXE)\
       data\
       images

# target 'dist' for release
dist : mostlyclean\
       $(EXEDIR)\
       $(OBJECTSDIR)\
       $(EXE)\
       data\
       icons\
       images\
       modules\
       $(EXEDIR:/=).lha

# target 'mostlyclean'
mostlyclean :
    -rm $(EXE)
    -rm $(OBJECTSDIR)*

# target 'clean'
clean : mostlyclean
    -rm $(DATADIR)*
    -rm $(ICONSDIR)*
    -rm $(IMAGESDIR)*
    -rm $(MODULESDIR)*

# ----------------------------------------



# Directories
$(EXEDIR) :
    mkdir "$@"

$(OBJECTSDIR) :
    mkdir "$@"



# Objects and executable
$(OBJECTS) : $(OBJECTSDIR)%.o : %.c
    $(CC) $(shell vbccprefs) $(CFLAGS) -c $< -o $@

$(EXE) : $(OBJECTS)
    $(LD) $(shell vbccprefs) -o $(EXE) $(OBJECTS)



# Data
data : $(DATADIR)\
       $(DATA)\

$(DATADIR) :
    mkdir "$@"

$(DATA) : $(DATADIR)% : $(wildcard data/%)
    cp -f -R "$(call replaceQuestionBySpace,data/$*)" "$(call replaceQuestionBySpace,$@)"



# Icons
icons : $(ICONSDIR)\
        $(ICONS)\
        $(EXE).info\
        $(EXEDIR:/=).info

$(ICONSDIR) :
    mkdir "$@"

$(ICONS) : $(ICONSDIR)% : $(wildcard icons/%)
    cp -f -R "$(call replaceQuestionBySpace,icons/$*)" "$(call replaceQuestionBySpace,$@)"

$(EXE).info :
    cp -f -R "$(ICONSDIR)AmiModRadio2.tool.info" "$@"

$(EXEDIR:/=).info :
    cp -f -R "$(ICONSDIR)AmiModRadio3.drawer.info" "$@"



# Images
images : $(IMAGESDIR)\
         $(IMAGES)\

$(IMAGESDIR) :
    mkdir "$@"

$(IMAGES) : $(IMAGESDIR)% : $(wildcard images/%)
    cp -f -R "$(call replaceQuestionBySpace,images/$*)" "$(call replaceQuestionBySpace,$@)"



# Modules
modules : $(MODULESDIR)

$(MODULESDIR) :
    mkdir "$@"



# LHA archive of 'dist'
$(EXEDIR:/=).lha : PARENTDIR = $(dir $(patsubst %/,%,$(EXEDIR)))
$(EXEDIR:/=).lha : EXENAME   = $(notdir $(patsubst %/,%,$(EXEDIR)))
$(EXEDIR:/=).lha :
    rm -f          RAM:$(EXENAME).lha
    /C/LHA -r -e a RAM:$(EXENAME).lha $(PARENTDIR) $(EXENAME).info $(EXENAME)/*