I want to collect some source files and tar them by using Makefile. The source files are scattering as follows:
project
│
├── Makefile
│
├── folder(name is arbitrary)
│ │
│ ├── test1.txt
│ ├── test2.txt
│ .
│ .
│ .
│
├── source.cpp
│
├── source2.cpp
│
├── source.h
.
.
Now, I want to have a flattened tarball file that includes every source files just under the root directory of this tarball file when I call make
in the root directory(project), like this:
myTarball.tar.gz
│
├── Makefile
│
├── source.cpp
│
├── source2.cpp
│
├── source.h
│
├── test1.txt
│
├── test2.txt
│
.
.
.
The current make file looks something like this:
FILES=$($(wildcard Makefile *.h *.hpp *.cpp test*.txt */test*.txt))
$(NAME): $(FILES)
COPYFILE_DISABLE=true tar -vczf $(NAME) $(FILES)
I'm not familiar with makefile and bash, but I try my best to play around and do some searching but found nothing. The current one can only generate something like
NAME.tar.gz
│
├── Makefile
│
├── folder
│ │
│ ├── test1.txt
│ ├── test2.txt
│ .
│ .
│ .
│
├── source.cpp
│
├── source2.cpp
│
├── source.h
.
.
with the folder structure. I think I might first copy all source files in the folder to the root directory, and delete them after tar them up. Is there a better way to do this elegantly?
CodePudding user response:
Well, you can do it that way but if you're using GNU tar then it's probably simpler to just let tar do the work for you rather than copying files around to a temporary location first.
Something like this should work:
$(NAME): $(FILES)
COPYFILE_DISABLE=true tar -vczf $(NAME) --transform='s,.*/,,' $(FILES)
which will remove all directories from files inside the generated tar file. See the manual for the --transform
option.