【问题标题】:Makefile .tar: Flatten the source files' directory structureMakefile .tar:展平源文件的目录结构
【发布时间】:2021-09-13 20:54:56
【问题描述】:

我想收集一些源文件并使用 Makefile 对它们进行 tar。源文件分布如下:

  project
    │
    ├── Makefile
    │
    ├── folder(name is arbitrary)
    │    │
    │    ├── test1.txt
    │    ├── test2.txt
    │    .
    │    .
    │    .
    │ 
    ├── source.cpp
    │
    ├── source2.cpp
    │
    ├── source.h
    .
    .

现在,当我在根目录(项目)中调用 make 时,我想要一个扁平的 tarball 文件,其中包含该 tarball 文件根目录下的所有源文件,如下所示:

 myTarball.tar.gz
    │
    ├── Makefile
    │
    ├── source.cpp
    │
    ├── source2.cpp
    │
    ├── source.h
    │
    ├── test1.txt
    │
    ├── test2.txt
    │
    .
    .
    .

当前的 make 文件如下所示:

FILES=$($(wildcard Makefile *.h *.hpp *.cpp test*.txt */test*.txt))

$(NAME): $(FILES)
    COPYFILE_DISABLE=true tar -vczf $(NAME) $(FILES)

我不熟悉 makefile 和 bash,但我尽我所能玩弄并进行了一些搜索,但一无所获。当前的只能生成类似

  NAME.tar.gz
    │
    ├── Makefile
    │
    ├── folder
    │    │
    │    ├── test1.txt
    │    ├── test2.txt
    │    .
    │    .
    │    .
    │ 
    ├── source.cpp
    │
    ├── source2.cpp
    │
    ├── source.h
    .
    .

与文件夹结构。我想我可能会先将文件夹中的所有源文件复制到根目录,然后将它们 tar 后删除。有没有更好的方法来优雅地做到这一点?

【问题讨论】:

    标签: bash shell makefile


    【解决方案1】:

    好吧,您可以这样做,但如果您使用的是 GNU tar,那么让 tar 为您完成工作可能比先将文件复制到临时位置更简单。

    这样的事情应该可以工作:

    $(NAME): $(FILES)
            COPYFILE_DISABLE=true tar -vczf $(NAME) --transform='s,.*/,,' $(FILES)
    

    这将从生成的 tar 文件中的文件中删除所有目录。请参阅--transform 选项的手册。

    【讨论】:

    • 感谢您的帮助!它确实解决了问题,乍一看,日志仍然在前缀中显示文件夹的名称,令人困惑。但是我解压tar文件后,居然已经被扁平化了!
    • 没错。您要添加到 tar 文件中的文件仍然需要通过 tar 定位,因此它们需要路径名。但是 tar 文件的 contents 不包含路径,因此当您提取 tar 文件时,它们就消失了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 2013-07-07
    • 2019-05-30
    相关资源
    最近更新 更多