gpt4 book ai didi

makefile - 使用 GNU Make 处理带空格的文件名

转载 作者:行者123 更新时间:2023-12-05 04:14:11 29 4
gpt4 key购买 nike

我正在使用 GNU Make (v3.80),我必须处理带有空格的文件名。 (对于文件名本身我无能为力。)我的makefile 包括"normal" rules。以及static pattern rules .我如何改变我的makefile 来处理这些文件?

我已经阅读并研究了以前的相关问题(herehereherethere),但没有处理静态模式规则。

最佳答案

一个解决方案结合了 "question mark" trick by Mecklenburg结合 shell 命令(find 和 sed)和 not 使用自动变量 $<而是$* .

假设您想复制在 data 中找到的任何文件目录到目标目录,这里是规则(包括静态模式规则)。以下是完整的 makefile ,这里是有趣的部分:

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

在上面,最后一个变量声明,DATA包含 data/ 中可用的文件名集将文件名中的任何空格替换为 ? .每个文件名都以目标目录为前缀 DATADIR并以空格分隔。

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

在上面,在 Mecklenburg 之后,定义了两个函数来用问号替换空格,反之亦然。

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

以上是重要的静态模式规则:规则的前提$(wildcard data/%)确保 Make 不会复制 data 中的文件在每次通话中,但前提是它们已从 $(DATADIR) 中更改或消失.

$(wildcard data/%)工作是因为 %$(DATA) : $(DATADIR)% 构建的词干取代因为$(DATA)包含一组没有空格(但有问号)并以 $(DATADIR) 为前缀的文件名.

在配方中,问号被空格替换以恢复原始文件名。 $<不能使用,因为它会匹配 $(wildcard data/%) ,这将导致一个空字符串。但是,因为目标包含 $(DATADIR)和文件名,$*可以以添加 data/ 为代价使用(文件名)在前面。


完整 makefile例如。

#
# 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)/*

关于makefile - 使用 GNU Make 处理带空格的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35617602/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com