add file type autodetection for the unpack command and nuke PKG_CAT:= in lots of...
[openwrt-10.03/.git] / include / unpack.mk
1 ext=$(word $(words $(subst ., ,$(1))),$(subst ., ,$(1)))
2
3 # unpacking files with +s may break on some platforms. this typically emits error code 2
4 ifneq ($(HOST_OS),Linux)
5   HOST_TAR:=trapret 2 $(TAR)
6 else
7   HOST_TAR:=$(TAR)
8 endif
9 TAR_CMD:=$(HOST_TAR) -C $(PKG_BUILD_DIR)/.. $(TAR_OPTIONS)
10 UNZIP_CMD:=unzip -d $(PKG_BUILD_DIR)/.. $(DL_DIR)/$(PKG_SOURCE)
11
12 ifeq ($(PKG_SOURCE),)
13   PKG_UNPACK ?= true
14 endif
15 ifeq ($(strip $(PKG_UNPACK)),)
16   ifeq ($(strip $(PKG_CAT)),)
17     # try to autodetect file type
18     EXT:=$(call ext,$(PKG_SOURCE))
19     EXT1:=$(EXT)
20
21     ifeq ($(filter gz tgz,$(EXT)),$(EXT))
22       EXT:=$(call ext,$(PKG_SOURCE:.$(EXT)=))
23       UNPACK:=$(ZCAT) $(DL_DIR)/$(PKG_SOURCE) |
24     endif       
25     ifeq ($(filter bzip2 bz2 bz tbz2 tbz,$(EXT)),$(EXT))
26       EXT:=$(call ext,$(PKG_SOURCE:.$(EXT)=))
27       UNPACK:=bzcat $(DL_DIR)/$(PKG_SOURCE) |
28     endif
29     ifeq ($(filter tgz tbz tbz2,$(EXT1)),$(EXT1))
30       $(error FOO: $(filter tgz tbz tbz2,$(EXT1)),$(EXT1))
31       EXT:=tar
32     endif
33     UNPACK ?= cat $(DL_DIR)/$(PKG_SOURCE) |
34     ifeq ($(EXT),tar)
35       PKG_UNPACK:=$(UNPACK) $(TAR_CMD)
36     endif
37     ifeq ($(EXT),cpio)
38       PKG_UNPACK:=$(UNPACK) (cd $(PKG_BUILD_DIR)/..; cpio -i -d)
39     endif
40     ifeq ($(EXT),zip)
41       PKG_UNPACK:=$(UNZIP_CMD)
42     endif
43   endif
44  
45   # compatibility code for packages that set PKG_CAT
46   ifeq ($(strip $(PKG_CAT)$(PKG_UNPACK)),)
47     # use existing PKG_CAT
48     PKG_UNPACK:=$(PKG_CAT) $(DL_DIR)/$(PKG_SOURCE) | $(TAR_CMD)
49     ifeq ($(PKG_CAT),unzip)
50       PKG_UNPACK:=$(UNZIP_CMD)
51     endif
52     # replace zcat with $(ZCAT), because some system have it as gzcat
53     ifeq ($(PKG_CAT),zcat)
54       PKG_UNPACK:=$(ZCAT) $(DL_DIR)/$(PKG_SOURCE) | $(TAR_CMD)
55     endif
56   endif
57   ifneq ($(strip $(CRLF_WORKAROUND)),)
58     PKG_UNPACK += && find $(PKG_BUILD_DIR) -type f -print0 | xargs -0 perl -pi -e 's!\r$$$$!!g'
59   endif
60 endif