ZipArchive.gmk revision 1379:6af7f86fc626
1130803Smarcel#
2130803Smarcel# Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
3130803Smarcel# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4130803Smarcel#
5130803Smarcel# This code is free software; you can redistribute it and/or modify it
6130803Smarcel# under the terms of the GNU General Public License version 2 only, as
7130803Smarcel# published by the Free Software Foundation.  Oracle designates this
8130803Smarcel# particular file as subject to the "Classpath" exception as provided
9130803Smarcel# by Oracle in the LICENSE file that accompanied this code.
10130803Smarcel#
11130803Smarcel# This code is distributed in the hope that it will be useful, but WITHOUT
12130803Smarcel# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13130803Smarcel# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14130803Smarcel# version 2 for more details (a copy is included in the LICENSE file that
15130803Smarcel# accompanied this code).
16130803Smarcel#
17130803Smarcel# You should have received a copy of the GNU General Public License version
18130803Smarcel# 2 along with this work; if not, write to the Free Software Foundation,
19130803Smarcel# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20130803Smarcel#
21130803Smarcel# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22130803Smarcel# or visit www.oracle.com if you need additional information or have any
23130803Smarcel# questions.
24130803Smarcel#
25130803Smarcel
26130803Smarcelifndef _ZIP_ARCHIVE_GMK
27130803Smarcel_ZIP_ARCHIVE_GMK := 1
28130803Smarcel
29130803Smarcelifeq (,$(_MAKEBASE_GMK))
30130803Smarcel  $(error You must include MakeBase.gmk prior to including ZipArchive.gmk)
31130803Smarcelendif
32130803Smarcel
33130803Smarcel# Setup make rules for creating a zip archive.
34130803Smarcel#
35130803Smarcel# Parameter 1 is the name of the rule. This name is used as variable prefix,
36130803Smarcel# and the targets generated are listed in a variable by that name.
37130803Smarcel#
38130803Smarcel# Remaining parameters are named arguments. These include:
39130803Smarcel#   SRC
40130803Smarcel#   ZIP
41130803Smarcel#   INCLUDES
42130803Smarcel#   INCLUDE_FILES
43130803Smarcel#   EXCLUDES
44130803Smarcel#   EXCLUDE_FILES
45130803Smarcel#   SUFFIXES
46130803Smarcel#   EXTRA_DEPS
47130803Smarcel#   ZIP_OPTIONS extra options to pass to zip
48130803SmarcelSetupZipArchive = $(NamedParamsMacroTemplate)
49130803Smarceldefine SetupZipArchiveBody
50130803Smarcel
51130803Smarcel  # To avoid running find over too large sets of files, which causes make to crash
52130803Smarcel  # on some configurations (cygwin), use INCLUDES and INCLUDE_FILES to build a set
53130803Smarcel  # of directories to run find in, if available.
54130803Smarcel  ifneq ($$($1_INCLUDES)$$($1_INCLUDE_FILES),)
55130803Smarcel    $1_FIND_LIST := $$(wildcard $$(foreach i,$$($1_SRC), \
56130803Smarcel        $$(addprefix $$i/,$$($1_INCLUDES) $$($1_INCLUDE_FILES))))
57130803Smarcel  else
58130803Smarcel    $1_FIND_LIST := $$($1_SRC)
59130803Smarcel  endif
60130803Smarcel
61130803Smarcel  # Find all files in the source tree.
62130803Smarcel  $1_ALL_SRCS := $$(call not-containing,_the.,$$(call CacheFind,$$($1_FIND_LIST)))
63130803Smarcel
64130803Smarcel  # Filter on suffixes if set
65130803Smarcel  ifneq ($$($1_SUFFIXES),)
66130803Smarcel    $1_ALL_SRCS := $$(filter $$(addprefix %, $$($1_SUFFIXES)), $$($1_ALL_SRCS))
67130803Smarcel  endif
68130803Smarcel
69130803Smarcel  ifneq ($$($1_INCLUDES),)
70130803Smarcel    ifneq ($$($1_SUFFIXES),)
71130803Smarcel      $1_ZIP_INCLUDES := $$(foreach s,$$($1_SUFFIXES), \
72130803Smarcel          $$(addprefix -i$(SPACE)$(DQUOTE),$$(addsuffix /*$$s$(DQUOTE),$$($1_INCLUDES))))
73130803Smarcel    else
74130803Smarcel      $1_ZIP_INCLUDES := $$(addprefix -i$(SPACE)$(DQUOTE),$$(addsuffix /*$(DQUOTE),$$($1_INCLUDES)))
75130803Smarcel    endif
76130803Smarcel  endif
77130803Smarcel  ifneq ($$($1_INCLUDE_FILES),)
78130803Smarcel    $1_ZIP_INCLUDES += $$(addprefix -i$(SPACE),$$($1_INCLUDE_FILES))
79130803Smarcel  endif
80130803Smarcel  ifneq ($$($1_EXCLUDES),)
81130803Smarcel    $1_ZIP_EXCLUDES := $$(addprefix -x$(SPACE)$(DQUOTE),$$(addsuffix /*$(DQUOTE),$$($1_EXCLUDES)))
82130803Smarcel    $1_SRC_EXCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_EXCLUDES))))
83130803Smarcel    $1_ALL_SRCS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_ALL_SRCS))
84130803Smarcel  endif
85130803Smarcel  ifneq ($$($1_EXCLUDE_FILES),)
86130803Smarcel    # Cannot precompute ZIP_EXCLUDE_FILES as it is dependent on which src root is being
87130803Smarcel    # zipped at the moment.
88130803Smarcel    $1_SRC_EXCLUDE_FILES := $$(addprefix %, $$($1_EXCLUDE_FILES)) $$($1_EXCLUDE_FILES)
89130803Smarcel    $1_ALL_SRCS := $$(filter-out $$($1_SRC_EXCLUDE_FILES), $$($1_ALL_SRCS))
90130803Smarcel  endif
91130803Smarcel
92130803Smarcel  # Use a slightly shorter name for logging, but with enough path to identify this zip.
93130803Smarcel  $1_NAME:=$$(subst $$(OUTPUT_ROOT)/,,$$($1_ZIP))
94130803Smarcel
95130803Smarcel  # Now $1_ALL_SRCS should contain all sources that are going to be put into the zip.
96130803Smarcel  # I.e. the zip -i and -x options should match the filtering done in the makefile.
97130803Smarcel  # Explicitly excluded files can be given with absolute path. The patsubst solution
98130803Smarcel  # isn't perfect but the likelyhood of an absolute path to match something in a src
99130803Smarcel  # dir is very small.
100130803Smarcel  # If zip has nothing to do, it returns 12 and would fail the build. Check for 12
101130803Smarcel  # and only fail if it's not.
102130803Smarcel  $$($1_ZIP) : $$($1_ALL_SRCS) $$($1_EXTRA_DEPS)
103130803Smarcel	$(MKDIR) -p $$(@D)
104130803Smarcel	$(ECHO) Updating $$($1_NAME)
105130803Smarcel	$$(foreach i,$$($1_SRC),(cd $$i && $(ZIP) -qru $$($1_ZIP_OPTIONS) $$@ . $$($1_ZIP_INCLUDES) \
106130803Smarcel	    $$($1_ZIP_EXCLUDES) -x \*_the.\* \
107130803Smarcel	    $$(addprefix -x$(SPACE), $$(patsubst $$i/%,%, $$($1_EXCLUDE_FILES))) \
108130803Smarcel	    || test "$$$$?" = "12" )$$(NEWLINE)) true
109130803Smarcel	$(TOUCH) $$@
110130803Smarcel
111130803Smarcel  # Add zip to target list
112130803Smarcel  $1 += $$($1_ZIP)
113130803Smarcelendef
114130803Smarcel
115130803Smarcelendif # _ZIP_ARCHIVE_GMK
116130803Smarcel