ZipArchive.gmk revision 2335:108814a7bae0
1238104Sdes#
2238104Sdes# Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
3238104Sdes# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4238104Sdes#
5238104Sdes# This code is free software; you can redistribute it and/or modify it
6238104Sdes# under the terms of the GNU General Public License version 2 only, as
7238104Sdes# published by the Free Software Foundation.  Oracle designates this
8238104Sdes# particular file as subject to the "Classpath" exception as provided
9238104Sdes# by Oracle in the LICENSE file that accompanied this code.
10238104Sdes#
11238104Sdes# This code is distributed in the hope that it will be useful, but WITHOUT
12238104Sdes# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13238104Sdes# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14238104Sdes# version 2 for more details (a copy is included in the LICENSE file that
15238104Sdes# accompanied this code).
16238104Sdes#
17238104Sdes# You should have received a copy of the GNU General Public License version
18238104Sdes# 2 along with this work; if not, write to the Free Software Foundation,
19238104Sdes# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20238104Sdes#
21238104Sdes# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22238104Sdes# or visit www.oracle.com if you need additional information or have any
23238104Sdes# questions.
24238104Sdes#
25238104Sdes
26238104Sdesifndef _ZIP_ARCHIVE_GMK
27238104Sdes_ZIP_ARCHIVE_GMK := 1
28238104Sdes
29238104Sdesifeq (,$(_MAKEBASE_GMK))
30238104Sdes  $(error You must include MakeBase.gmk prior to including ZipArchive.gmk)
31238104Sdesendif
32238104Sdes
33238104Sdes# Setup make rules for creating a zip archive.
34238104Sdes#
35238104Sdes# Parameter 1 is the name of the rule. This name is used as variable prefix,
36238104Sdes# and the targets generated are listed in a variable by that name.
37238104Sdes#
38238104Sdes# Remaining parameters are named arguments. These include:
39238104Sdes#   SRC
40238104Sdes#   ZIP
41238104Sdes#   INCLUDES
42238104Sdes#   INCLUDE_FILES
43238104Sdes#   EXCLUDES
44238104Sdes#   EXCLUDE_FILES
45238104Sdes#   SUFFIXES
46238104Sdes#   EXTRA_DEPS
47238104Sdes#   ZIP_OPTIONS extra options to pass to zip
48238104SdesSetupZipArchive = $(NamedParamsMacroTemplate)
49238104Sdesdefine SetupZipArchiveBody
50238104Sdes
51238104Sdes  # To avoid running find over too large sets of files, which causes make to crash
52238104Sdes  # on some configurations (cygwin), use INCLUDES and INCLUDE_FILES to build a set
53238104Sdes  # of directories to run find in, if available.
54238104Sdes  ifneq ($$($1_INCLUDES)$$($1_INCLUDE_FILES),)
55238104Sdes    $1_FIND_LIST := $$(wildcard $$(foreach i,$$($1_SRC), \
56238104Sdes        $$(addprefix $$i/,$$($1_INCLUDES) $$($1_INCLUDE_FILES))))
57238104Sdes  else
58238104Sdes    $1_FIND_LIST := $$($1_SRC)
59238104Sdes  endif
60238104Sdes
61238104Sdes  # Find all files in the source tree.
62238104Sdes  $1_ALL_SRCS := $$(call not-containing,_the.,$$(call CacheFind,$$($1_FIND_LIST)))
63238104Sdes
64238104Sdes  # Filter on suffixes if set
65238104Sdes  ifneq ($$($1_SUFFIXES),)
66238104Sdes    $1_ALL_SRCS := $$(filter $$(addprefix %, $$($1_SUFFIXES)), $$($1_ALL_SRCS))
67266114Sdes  endif
68266114Sdes
69266114Sdes  ifneq ($$($1_INCLUDES),)
70238104Sdes    ifneq ($$($1_SUFFIXES),)
71238104Sdes      $1_ZIP_INCLUDES := $$(foreach s,$$($1_SUFFIXES), \
72238104Sdes          $$(addprefix -i$(SPACE)$(DQUOTE),$$(addsuffix /*$$s$(DQUOTE),$$($1_INCLUDES))))
73238104Sdes    else
74238104Sdes      $1_ZIP_INCLUDES := $$(addprefix -i$(SPACE)$(DQUOTE),$$(addsuffix /*$(DQUOTE),$$($1_INCLUDES)))
75238104Sdes    endif
76238104Sdes  else
77238104Sdes    ifneq ($$($1_SUFFIXES),)
78238104Sdes      $1_ZIP_INCLUDES := $$(foreach s,$$($1_SUFFIXES), \
79238104Sdes          $$(addprefix -i$(SPACE)$(DQUOTE),*$$s$(DQUOTE)))
80238104Sdes    endif
81238104Sdes  endif
82238104Sdes  ifneq ($$($1_INCLUDE_FILES),)
83238104Sdes    $1_ZIP_INCLUDES += $$(addprefix -i$(SPACE),$$($1_INCLUDE_FILES))
84238104Sdes  endif
85238104Sdes  ifneq ($$($1_EXCLUDES),)
86238104Sdes    $1_ZIP_EXCLUDES := $$(addprefix -x$(SPACE)$(DQUOTE),$$(addsuffix /*$(DQUOTE),$$($1_EXCLUDES)))
87238104Sdes    $1_SRC_EXCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_EXCLUDES))))
88238104Sdes    $1_ALL_SRCS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_ALL_SRCS))
89238104Sdes  endif
90238104Sdes  ifneq ($$($1_EXCLUDE_FILES),)
91238104Sdes    # Cannot precompute ZIP_EXCLUDE_FILES as it is dependent on which src root is being
92238104Sdes    # zipped at the moment.
93238104Sdes    $1_SRC_EXCLUDE_FILES := $$(addprefix %, $$($1_EXCLUDE_FILES)) $$($1_EXCLUDE_FILES)
94238104Sdes    $1_ALL_SRCS := $$(filter-out $$($1_SRC_EXCLUDE_FILES), $$($1_ALL_SRCS))
95238104Sdes  endif
96238104Sdes
97238104Sdes  # Use a slightly shorter name for logging, but with enough path to identify this zip.
98238104Sdes  $1_NAME:=$$(subst $$(OUTPUT_ROOT)/,,$$($1_ZIP))
99238104Sdes
100238104Sdes  # Now $1_ALL_SRCS should contain all sources that are going to be put into the zip.
101238104Sdes  # I.e. the zip -i and -x options should match the filtering done in the makefile.
102238104Sdes  # Explicitly excluded files can be given with absolute path. The patsubst solution
103238104Sdes  # isn't perfect but the likelyhood of an absolute path to match something in a src
104238104Sdes  # dir is very small.
105238104Sdes  # If zip has nothing to do, it returns 12 and would fail the build. Check for 12
106238104Sdes  # and only fail if it's not.
107238104Sdes  $$($1_ZIP) : $$($1_ALL_SRCS) $$($1_EXTRA_DEPS)
108238104Sdes	$(MKDIR) -p $$(@D)
109238104Sdes	$(ECHO) Updating $$($1_NAME)
110238104Sdes	$$(foreach i,$$($1_SRC),(cd $$i && $(ZIPEXE) -qru $$($1_ZIP_OPTIONS) $$@ . $$($1_ZIP_INCLUDES) \
111238104Sdes	    $$($1_ZIP_EXCLUDES) -x \*_the.\* \
112238104Sdes	    $$(addprefix -x$(SPACE), $$(patsubst $$i/%,%, $$($1_EXCLUDE_FILES))) \
113238104Sdes	    || test "$$$$?" = "12" )$$(NEWLINE)) true
114238104Sdes	$(TOUCH) $$@
115238104Sdes
116238104Sdes  # Add zip to target list
117238104Sdes  $1 += $$($1_ZIP)
118238104Sdesendef
119238104Sdes
120238104Sdesendif # _ZIP_ARCHIVE_GMK
121238104Sdes