Branch libreoffice-5-0-4
[LibreOffice.git] / solenv / gbuild / UnpackedTarball.mk
blob49403a474a767eeb930040527a015cbbd36fdc46
1 # -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 # UnpackedTarget class
12 # Handles unpacking of a tarball
14 # platform
15 # gb_UnpackedTarget_TARFILE_LOCATION
16 # NOTE: only for commands; targets should use TARFILE_LOCATION directly
18 define gb_UnpackedTarget__command_untar
19 $(GNUTAR) \
20 -x \
21 $(3) \
22 -C $(UNPACKED_DIR) \
23 $(STRIP_COMPONENTS)=$(UNPACKED_STRIP_COMPONENTS) \
24 -f $(UNPACKED_TARBALL)
25 endef
27 define gb_UnpackedTarget__command_unzip
28 unzip \
29 -qq \
30 -d $(UNPACKED_DIR) $(UNPACKED_TARBALL) \
31 $(if $(filter-out 0,$(UNPACKED_STRIP_COMPONENTS)),\
32 && UNZIP_DIR=`ls $(UNPACKED_DIR)` \
33 && mv $(UNPACKED_DIR)/$$UNZIP_DIR/* $(UNPACKED_DIR) \
34 && rm -rf $(UNPACKED_DIR)/$$UNZIP_DIR \
36 endef
38 define gb_UnpackedTarget__command
39 $(call gb_Output_announce,$(notdir $(2)),$(true),UPK,1)
40 $(call gb_Helper_abbreviate_dirs,\
41 $(if $(wildcard $(UNPACKED_DIR)),rm -rf $(UNPACKED_DIR) &&) \
42 mkdir -p $(UNPACKED_DIR) && \
43 $(call gb_UnpackedTarget__command_$(1),$(2),$(3),$(4)) && \
44 touch $(2) \
46 endef
48 $(dir $(call gb_UnpackedTarget_get_target,%)).dir :
49 $(if $(wildcard $(dir $@)),,mkdir -p $(dir $@))
51 $(call gb_UnpackedTarget_get_target,%).tar.bz2 :
52 $(call gb_UnpackedTarget__command,untar,$@,$*,-j)
54 $(call gb_UnpackedTarget_get_target,%).tar.xz :
55 $(call gb_UnpackedTarget__command,untar,$@,$*,-J)
57 $(call gb_UnpackedTarget_get_target,%).tar.gz :
58 $(call gb_UnpackedTarget__command,untar,$@,$*,-z)
60 $(call gb_UnpackedTarget_get_target,%).tgz :
61 $(call gb_UnpackedTarget__command,untar,$@,$*,-z)
63 $(call gb_UnpackedTarget_get_target,%).zip :
64 $(call gb_UnpackedTarget__command,unzip,$@,$*)
66 $(call gb_UnpackedTarget_get_target,%).oxt :
67 $(call gb_UnpackedTarget__command,unzip,$@,$*)
69 .PHONY : $(call gb_UnpackedTarget_get_clean_target,%)
70 $(call gb_UnpackedTarget_get_clean_target,%) :
71 $(call gb_Output_announce,$*,$(false),UPK,1)
72 $(call gb_Helper_abbreviate_dirs,\
73 rm -f $(call gb_UnpackedTarget_get_target,$*) \
76 # gb_UnpackedTarget_UnpackedTarget target outdir strip-components?
77 define gb_UnpackedTarget_UnpackedTarget
78 $(call gb_UnpackedTarget_get_target,$(1)) : UNPACKED_DIR := $(2)
79 $(call gb_UnpackedTarget_get_target,$(1)) : UNPACKED_TARBALL := $(gb_UnpackedTarget_TARFILE_LOCATION)/$(1)
80 $(call gb_UnpackedTarget_get_target,$(1)) : UNPACKED_STRIP_COMPONENTS := $(if $(strip $(3)),$(strip $(3)),1)
82 $(call gb_UnpackedTarget_get_target,$(1)) : $(TARFILE_LOCATION)/$(1)
83 $(call gb_UnpackedTarget_get_target,$(1)) :| $(dir $(call gb_UnpackedTarget_get_target,$(1))).dir
85 endef
87 # UnpackedTarball class
89 # Handles unpacking and patching of an external project
91 # The unpacked dir is recreated every time one of the patches, copied
92 # files or the makefile changes.
94 # This is what dmake patches use. Once all external modules are
95 # converted, it is better to be changed to 1.
96 gb_UnpackedTarball_PATCHLEVEL_DEFAULT := 2
98 gb_UnpackedTarball_CONVERTTODOS = \
99 $(gb_AWK) 'sub("$$","\r")' $(1) > $(1).TEMP && mv $(1).TEMP $(1)
100 gb_UnpackedTarball_CONVERTTOUNIX = \
101 tr -d '\r' < $(1) > $(1).TEMP && mv $(1).TEMP $(1)
103 define gb_UnpackedTarball__copy_files_impl
104 $(if $(1),\
105 && cp $(firstword $(1)) $(firstword $(2)) \
106 $(call gb_UnpackedTarball__copy_files_impl,$(wordlist 2,$(words $(1)),$(1)),$(wordlist 2,$(words $(2)),$(2))) \
108 endef
110 # Drop leading &&
111 define gb_UnpackedTarball__copy_files_fix
112 $(wordlist 2,$(words $(1)),$(1))
113 endef
115 define gb_UnpackedTarball__copy_files
116 $(call gb_UnpackedTarball__copy_files_fix,$(call gb_UnpackedTarball__copy_files_impl,$(1),$(2)))
117 endef
119 define gb_UnpackedTarball__command
120 $(call gb_Output_announce,$(2),$(true),PAT,2)
121 $(call gb_Helper_abbreviate_dirs,\
123 cd $(3) \
124 $(if $(UNPACKED_IS_BIN_TARBALL),,&& \
125 $(if $(UNPACKED_PRE_ACTION),\
126 $(UNPACKED_PRE_ACTION) && \
128 $(if $(UNPACKED_FILES),\
129 mkdir -p $(sort $(dir $(UNPACKED_DESTFILES))) && \
130 $(call gb_UnpackedTarball__copy_files,$(UNPACKED_FILES),$(UNPACKED_DESTFILES)) && \
132 $(foreach file,$(UNPACKED_FIX_EOL),$(call gb_UnpackedTarball_CONVERTTOUNIX,$(file)) && ) \
133 $(if $(UNPACKED_PATCHES),\
134 for p in $(UNPACKED_PATCHES); do \
135 pl=$(UNPACKED_PATCHLEVEL); \
136 s=$${p##*.}; case "$$s" in [0-9]$(CLOSE_PAREN) pl="$$s"; ;; esac ; \
137 $(GNUPATCH) $(UNPACKED_PATCHFLAGS) -f -s "-p$$pl" --fuzz=0 < "$$p"; \
138 if test "$$?" -ne 0; then echo "Patch FAILED: $$p"; exit 1; fi;\
139 done && \
141 $(foreach file,$(UNPACKED_FIX_EOL),$(call gb_UnpackedTarball_CONVERTTODOS,$(file)) && ) \
142 $(if $(UNPACKED_POST_ACTION),\
143 $(UNPACKED_POST_ACTION) && \
145 $(if $(gb_KEEP_PRISTINE), \
146 rm -fr $(call gb_UnpackedTarball_get_pristine_dir,$(2)) && \
147 cp -r $(call gb_UnpackedTarball_get_dir,$(2)) $(call gb_UnpackedTarball_get_pristine_dir,$(2)) && \
149 touch $(1) \
151 ) || \
153 touch $(call gb_UnpackedTarball_get_preparation_target,$(2)) && \
154 exit 1 \
157 endef
159 $(dir $(call gb_UnpackedTarball_get_target,%)).dir :
160 $(if $(wildcard $(dir $@)),,mkdir -p $(dir $@))
162 $(call gb_UnpackedTarball_get_preparation_target,%) :
163 touch $@
165 $(call gb_UnpackedTarball_get_target,%) :
166 $(call gb_UnpackedTarball__command,$@,$*,$(call gb_UnpackedTarball_get_dir,$*))
168 $(call gb_UnpackedTarball_get_final_target,%) :
169 touch $@
171 .PHONY : $(call gb_UnpackedTarball_get_clean_target,%)
172 $(call gb_UnpackedTarball_get_clean_target,%) :
173 $(call gb_Output_announce,$*,$(false),PAT,2)
174 $(call gb_Helper_abbreviate_dirs,\
175 rm -rf \
176 $(call gb_UnpackedTarball_get_final_target,$*) \
177 $(call gb_UnpackedTarball_get_target,$*) \
178 $(call gb_UnpackedTarball_get_preparation_target,$*) \
179 $(call gb_UnpackedTarball_get_dir,$*) \
180 $(call gb_UnpackedTarball_get_pristine_dir,$*) \
183 # Initialize unpacked tarball
184 define gb_UnpackedTarball_UnpackedTarball_internal
185 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_DESTFILES :=
186 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_FILES :=
187 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_FIX_EOL :=
188 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PATCHES :=
189 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PATCHLEVEL := $(gb_UnpackedTarball_PATCHLEVEL_DEFAULT)
190 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PATCHFLAGS :=
191 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_POST_ACTION :=
192 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PRE_ACTION :=
194 $(call gb_UnpackedTarball_get_preparation_target,$(1)) : $(gb_Module_CURRENTMAKEFILE)
195 $(call gb_UnpackedTarball_get_preparation_target,$(1)) :| $(dir $(call gb_UnpackedTarball_get_target,$(1))).dir
196 $(call gb_UnpackedTarball_get_target,$(1)) : $(call gb_UnpackedTarball_get_preparation_target,$(1))
197 $(call gb_UnpackedTarball_get_target,$(1)) :| $(dir $(call gb_UnpackedTarball_get_target,$(1))).dir
198 $(call gb_UnpackedTarball_get_final_target,$(1)) : $(call gb_UnpackedTarball_get_target,$(1))
200 private gb_UnpackedTarball_PATTERN_RULES_$(1) :=
202 endef
204 # Define a new unpacked tarball
205 define gb_UnpackedTarball_UnpackedTarball
206 $(call gb_UnpackedTarball_UnpackedTarball_internal,$(1))
208 $$(eval $$(call gb_Module_register_target,$(call gb_UnpackedTarball_get_final_target,$(1)),$(call gb_UnpackedTarball_get_clean_target,$(1))))
209 $(call gb_Helper_make_userfriendly_targets,$(1),UnpackedTarball,$(call gb_UnpackedTarball_get_final_target,$(1)))
211 endef
213 # Convert line ending from dos to unix style for selected files
215 # This is done before applying patches, because patches expect unix
216 # style line ending, and the files are converted back after that. The
217 # files are relative to the unpacked path.
219 # gb_UnpackedTarball_fix_end_of_line unpacked file(s)
220 define gb_UnpackedTarball_fix_end_of_line
221 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_FIX_EOL += $(addprefix $(call gb_UnpackedTarball_get_dir,$(1))/,$(2))
223 endef
226 # Internal version of set_tarbal, mostly to avoid repeated invocation of $(shel
227 define gb_UnpackedTarball_set_tarball_internal
228 $(call gb_UnpackedTarget_UnpackedTarget,$(2),$(call gb_UnpackedTarball_get_dir,$(1)),$(3),$(4))
229 $(call gb_UnpackedTarball_get_target,$(1)) : $(call gb_UnpackedTarget_get_target,$(2))
230 $(call gb_UnpackedTarball_get_clean_target,$(1)) : $(call gb_UnpackedTarget_get_clean_target,$(2))
231 $(call gb_UnpackedTarget_get_target,$(2)) : $(call gb_UnpackedTarball_get_preparation_target,$(1))
232 $(if $(findstring in,$(5)),
233 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_IS_BIN_TARBALL := YES
234 $(call gb_ExternalProject_get_state_target,$(1),%) : UNPACKED_IS_BIN_TARBALL := YES)
235 $(if $(findstring out,$(5)),$(call gb_Module_get_target,$(4)) : $(TARFILE_LOCATION)/$(6)
236 $(TARFILE_LOCATION)/$(6) : $(call gb_Module_get_nonl10n_target,$(4))
237 $$(call gb_Output_announce,$(6),$(true),PKB,3)
238 if test ! -f "$$@" ; then cd $(call gb_UnpackedTarball_get_dir,) && $(GNUTAR) -czf "$$@" $(1)/ || $(GNUTAR) -czf "$$@" $(1)/ ; else touch "$$@" ; fi)
240 endef
242 # Set tarball name
244 # gb_UnpackedTarball_set_tarball unpacked tarball-name
245 define gb_UnpackedTarball_set_tarball
246 $(if $(USE_LIBRARY_BIN_TAR),
247 $(if $(4),
248 $(if $(shell "$(SRCDIR)/solenv/bin/bin_library_info.sh" -l "$(gb_UnpackedTarget_TARFILE_LOCATION)" -o "$(4)" -b "$(BUILDDIR)" -s "$(SRCDIR)" -t "$(2)" -m verify -p "$(OS)_$(CPUNAME)"),
249 $(call gb_UnpackedTarball_set_tarball_internal,$(1),$(shell "$(SRCDIR)/solenv/bin/bin_library_info.sh" -l "$(gb_UnpackedTarget_TARFILE_LOCATION)" -o "$(4)" -b "$(BUILDDIR)" -s "$(SRCDIR)" -t "$(2)" -m verify -p "$(OS)_$(CPUNAME)"),$(3),$(4),in),\
250 $(call gb_UnpackedTarball_set_tarball_internal,$(1),$(2),$(3),$(4),out,$(shell "$(SRCDIR)/solenv/bin/bin_library_info.sh" -l "$(gb_UnpackedTarget_TARFILE_LOCATION)" -o "$(4)" -b "$(BUILDDIR)" -s "$(SRCDIR)" -t "$(2)" -m name -p "$(OS)_$(CPUNAME)")))
252 $(call gb_UnpackedTarball_set_tarball_internal,$(1),$(2),$(3),$(4),)
255 $(call gb_UnpackedTarball_set_tarball_internal,$(1),$(2),$(3),$(4),)
258 endef
260 # Set patch level to be used for all patches
262 # The default value is 3 to be able to work with current dmake patches.
264 # gb_UnpackedTarball_set_patchlevel unpacked level
265 define gb_UnpackedTarball_set_patchlevel
266 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PATCHLEVEL := $(2)
268 endef
270 # Add patch flags to be passed to patch for all patches cf. fdo#66826
272 # gb_UnpackedTarball_set_patchlevel unpacked level
273 define gb_UnpackedTarball_set_patchflags
274 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PATCHFLAGS := $(2)
276 endef
278 # Add a patch to be applied on the unpacked files
280 # gb_UnpackedTarball_add_patch unpacked patch
281 define gb_UnpackedTarball_add_patch
282 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PATCHES += $(SRCDIR)/$(2)
283 $(call gb_UnpackedTarball_get_preparation_target,$(1)) : $(SRCDIR)/$(2)
285 endef
287 # Add several patches at once
289 # gb_UnpackedTarball_add_patches unpacked patch(es)
290 define gb_UnpackedTarball_add_patches
291 $(foreach patch,$(2),$(call gb_UnpackedTarball_add_patch,$(1),$(patch)))
293 endef
295 # Add a file from source dir to the unpacked dir
297 # This function should not be used for overwriting existing files--use a
298 # patch for that purpose.
300 # gb_UnpackedTarball_add_file unpacked destfile file
301 define gb_UnpackedTarball_add_file
302 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_FILES += $(SRCDIR)/$(3)
303 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_DESTFILES += $(call gb_UnpackedTarball_get_dir,$(1))/$(2)
304 $(call gb_UnpackedTarball_get_preparation_target,$(1)) : $(SRCDIR)/$(3)
306 endef
308 # Add several files(s) from source dir to the unpacked dir
310 # The files are added into the specified subdir.
312 # gb_UnpackedTarball_add_files unpacked subdir file(s)
313 define gb_UnpackedTarball_add_files
314 $(foreach file,$(3),$(call gb_UnpackedTarball_add_file,$(1),$(2)/$(notdir $(file)),$(file)))
316 endef
318 # Set arbitrary shell command to be run during unpack
320 # The command is run at the very beginning, in freshly unpacked tarball.
321 # The command is run in the unpacked directory. If more than one command
322 # is used, care should be taken that the whole command fails if either
323 # of the sub-commands fails.
325 # gb_UnpackedTarball_set_pre_action unpacked shell-command
326 define gb_UnpackedTarball_set_pre_action
327 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PRE_ACTION := $(strip $(2))
329 endef
331 # Set arbitrary shell command to be run during unpack
333 # The command is run at the very end: after patching, C++ extension
334 # mangling and copying additional files in. The command is run in the
335 # unpacked directory. If more than one command is used, care should be
336 # taken that the whole command fails if either of the sub-commands
337 # fails.
339 # NOTE: This is a bit hackish, but it is the easiest way to move files
340 # around or delete files (typically because the file causes build
341 # problems in the original location, c.f. clucene). This is doable by
342 # using -E with patch (we use GNU patch anyway), but it would mean an
343 # additional patch to maintain....
345 # gb_UnpackedTarball_set_post_action unpacked shell-command
346 define gb_UnpackedTarball_set_post_action
347 $(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_POST_ACTION := $(strip $(2))
349 endef
351 define gb_UnpackedTarbal__make_pattern_rule
352 $(call gb_UnpackedTarball_get_dir,$(1))/%$(2) :
353 $$(if $$(wildcard $$@),,$$(call gb_Output_error,file $$@ does not exist in the tarball))
354 touch $$@
356 $(eval gb_UnpackedTarball_PATTERN_RULES_$(1) += $(2))
358 endef
360 define gb_UnpackedTarbal__ensure_pattern_rule
361 $(if $(filter $(2),$(gb_UnpackedTarball_PATTERN_RULES_$(1))),,$(call gb_UnpackedTarbal__make_pattern_rule,$(1),$(2)))
363 endef
365 define gb_UnpackedTarbal__make_file_rule
366 $(call gb_UnpackedTarball_get_dir,$(1))/$(2) :
367 $$(if $$(wildcard $$@),,$$(call gb_Output_error,file $$@ does not exist in the tarball))
368 touch $$@
370 endef
372 # Mark a source file to be used outside of this module
374 # This results in the timestamp of the file being updated, so a possible
375 # change is recognized properly by other files depending on it. The
376 # update is run after possible post action.
378 # See description of class ExternalPackage for more information.
380 # gb_UnpackedTarball_mark_output_file unpacked file
381 define gb_UnpackedTarball_mark_output_file
382 $(call gb_UnpackedTarball_get_final_target,$(1)) : $(call gb_UnpackedTarball_get_dir,$(1))/$(2)
383 $(call gb_UnpackedTarball_get_dir,$(1))/$(2) : $(call gb_UnpackedTarball_get_target,$(1))
384 $(if $(suffix $(2)),\
385 $(call gb_UnpackedTarbal__ensure_pattern_rule,$(1),$(suffix $(2))),\
386 $(call gb_UnpackedTarbal__make_file_rule,$(1),$(2)) \
389 endef
391 # Mark several source files to be used outside of this module
393 # gb_UnpackedTarball_mark_output_files unpacked file(s)
394 define gb_UnpackedTarball_mark_output_files
395 $(foreach file,$(2),$(call gb_UnpackedTarball_mark_output_file,$(1),$(file)))
397 endef
399 # force the rebuild of an external target
400 # this only works when running as partial build.
402 %.rebuild :
403 if [ -f $(call gb_UnpackedTarball_get_target,$*) ] ; then \
404 touch $(call gb_UnpackedTarball_get_target,$*) ; \
405 $(MAKE) ;\
408 %.genpatch :
409 if [ -d $(call gb_UnpackedTarball_get_dir,$*) -a -d $(call gb_UnpackedTarball_get_pristine_dir,$*) ] ; then \
411 patch_file=$$(pwd)/$*.new.patch.1; \
412 cd $(call gb_UnpackedTarball_get_dir,) ; \
413 diff -ur $*.org $* > $$patch_file; \
414 echo "Patch $$patch_file generated" ; \
415 ); \
416 else \
417 echo "Error: No pristine tarball available for $*" 1>&2 ; \
421 # vim: set noet sw=4 ts=4: