summaryrefslogtreecommitdiff
path: root/solenv/gbuild/UnpackedTarball.mk
blob: 9654d42fe2de69bddb0ce62361138cdc149a9875 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

# UnpackedTarget class

# Handles unpacking of a tarball

# platform
#  gb_UnpackedTarget_TARFILE_LOCATION
#   NOTE: only for commands; targets should use TARFILE_LOCATION directly

# Location of internal copies of config.{guess,sub}.
gb_UnpackedTarball_CONFIGDIR := $(GBUILDDIR)

define gb_UnpackedTarget__command_untar
$(GNUTAR) \
	-x \
	$(3) \
	-C $(UNPACKED_DIR) \
	$(STRIP_COMPONENTS)=$(UNPACKED_STRIP_COMPONENTS) \
	-f $(UNPACKED_TARBALL)
endef

define gb_UnpackedTarget__command_unzip
unzip \
	-qq \
	-d $(UNPACKED_DIR) $(UNPACKED_TARBALL) \
$(if $(filter-out 0,$(UNPACKED_STRIP_COMPONENTS)),\
	&& UNZIP_DIR=`ls $(UNPACKED_DIR)` \
	&& mv $(UNPACKED_DIR)/$$UNZIP_DIR/* $(UNPACKED_DIR) \
	&& rm -rf $(UNPACKED_DIR)/$$UNZIP_DIR \
)
endef

define gb_UnpackedTarget__command
$(call gb_Output_announce,$(notdir $(2)),$(true),UPK,1)
$(call gb_Helper_abbreviate_dirs,\
	$(if $(wildcard $(UNPACKED_DIR)),rm -rf $(UNPACKED_DIR) &&) \
	mkdir -p $(UNPACKED_DIR) && \
	$(call gb_UnpackedTarget__command_$(1),$(2),$(3),$(4)) && \
	touch $(2) \
)
endef

$(dir $(call gb_UnpackedTarget_get_target,%)).dir :
	$(if $(wildcard $(dir $@)),,mkdir -p $(dir $@))

$(call gb_UnpackedTarget_get_target,%).tar.bz2 :
	$(call gb_UnpackedTarget__command,untar,$@,$*,-j)

$(call gb_UnpackedTarget_get_target,%).tar.xz :
	$(call gb_UnpackedTarget__command,untar,$@,$*,-J)

$(call gb_UnpackedTarget_get_target,%).tar.gz :
	$(call gb_UnpackedTarget__command,untar,$@,$*,-z)

$(call gb_UnpackedTarget_get_target,%).tgz :
	$(call gb_UnpackedTarget__command,untar,$@,$*,-z)

$(call gb_UnpackedTarget_get_target,%).zip :
	$(call gb_UnpackedTarget__command,unzip,$@,$*)

$(call gb_UnpackedTarget_get_target,%).oxt :
	$(call gb_UnpackedTarget__command,unzip,$@,$*)

.PHONY : $(call gb_UnpackedTarget_get_clean_target,%)
$(call gb_UnpackedTarget_get_clean_target,%) :
	$(call gb_Output_announce,$*,$(false),UPK,1)
	$(call gb_Helper_abbreviate_dirs,\
		rm -f $(call gb_UnpackedTarget_get_target,$*) \
	)

# gb_UnpackedTarget_UnpackedTarget target outdir strip-components?
define gb_UnpackedTarget_UnpackedTarget
$(call gb_UnpackedTarget_get_target,$(1)) : UNPACKED_DIR := $(2)
$(call gb_UnpackedTarget_get_target,$(1)) : UNPACKED_TARBALL := $(gb_UnpackedTarget_TARFILE_LOCATION)/$(1)
$(call gb_UnpackedTarget_get_target,$(1)) : UNPACKED_STRIP_COMPONENTS := $(if $(strip $(3)),$(strip $(3)),1)

$(call gb_UnpackedTarget_get_target,$(1)) : $(TARFILE_LOCATION)/$(1)
$(call gb_UnpackedTarget_get_target,$(1)) :| $(dir $(call gb_UnpackedTarget_get_target,$(1))).dir

endef

# UnpackedTarball class

# Handles unpacking and patching of an external project
#
# The unpacked dir is recreated every time one of the patches, copied
# files or the makefile changes.

# This is what dmake patches use. Once all external modules are
# converted, it is better to be changed to 1.
gb_UnpackedTarball_PATCHLEVEL_DEFAULT := 2

gb_UnpackedTarball_CONVERTTODOS = \
	$(gb_AWK) 'sub("$$","\r")' $(1) > $(1).TEMP && mv $(1).TEMP $(1)
gb_UnpackedTarball_CONVERTTOUNIX = \
	tr -d '\r' < $(1) > $(1).TEMP && mv $(1).TEMP $(1)

define gb_UnpackedTarball__copy_files_impl
$(if $(1),\
	&& cp $(firstword $(1)) $(firstword $(2)) \
	$(call gb_UnpackedTarball__copy_files_impl,$(wordlist 2,$(words $(1)),$(1)),$(wordlist 2,$(words $(2)),$(2))) \
)
endef

# Drop leading &&
define gb_UnpackedTarball__copy_files_fix
$(wordlist 2,$(words $(1)),$(1))
endef

define gb_UnpackedTarball__copy_files
$(call gb_UnpackedTarball__copy_files_fix,$(call gb_UnpackedTarball__copy_files_impl,$(1),$(2)))
endef

define gb_UnpackedTarball__command
$(call gb_Output_announce,$(2),$(true),PAT,2)
$(call gb_Helper_abbreviate_dirs,\
	( \
		cd $(3) \
		$(if $(UNPACKED_IS_BIN_TARBALL),,&& \
		$(if $(UNPACKED_PRE_ACTION),\
			$(UNPACKED_PRE_ACTION) && \
		) \
		$(if $(UNPACKED_FILES),\
			mkdir -p $(sort $(dir $(UNPACKED_DESTFILES))) && \
			$(call gb_UnpackedTarball__copy_files,$(UNPACKED_FILES),$(UNPACKED_DESTFILES)) && \
		) \
		$(foreach file,$(UNPACKED_FIX_EOL),$(call gb_UnpackedTarball_CONVERTTOUNIX,$(file)) && ) \
		$(if $(UNPACKED_PATCHES),\
			for p in $(UNPACKED_PATCHES); do \
				pl=$(UNPACKED_PATCHLEVEL); \
				s=$${p##*.}; case "$$s" in [0-9]$(CLOSE_PAREN) pl="$$s"; ;; esac ; \
				$(GNUPATCH) $(UNPACKED_PATCHFLAGS) -f -s "-p$$pl" --fuzz=0 < "$$p"; \
				if test "$$?" -ne 0; then echo "Patch FAILED: $$p"; exit 1; fi;\
			done && \
		) \
		$(foreach file,$(UNPACKED_FIX_EOL),$(call gb_UnpackedTarball_CONVERTTODOS,$(file)) && ) \
		$(foreach confdir,$(UNPACKED_CONFIG_DIRS),\
			cp -f $(gb_UnpackedTarball_CONFIGDIR)/config.guess $(gb_UnpackedTarball_CONFIGDIR)/config.sub $(confdir) && \
		) \
		$(if $(UNPACKED_POST_ACTION),\
			$(UNPACKED_POST_ACTION) && \
		) \
	    $(if $(gb_KEEP_PRISTINE), \
			rm -fr $(call gb_UnpackedTarball_get_pristine_dir,$(2)) && \
			cp -r $(call gb_UnpackedTarball_get_dir,$(2)) $(call gb_UnpackedTarball_get_pristine_dir,$(2)) && \
		) \
		touch $(1) \
		)\
	) || \
	( \
		touch $(call gb_UnpackedTarball_get_preparation_target,$(2)) && \
		exit 1 \
	)
)
endef

$(dir $(call gb_UnpackedTarball_get_target,%)).dir :
	$(if $(wildcard $(dir $@)),,mkdir -p $(dir $@))

$(call gb_UnpackedTarball_get_preparation_target,%) :
	touch $@

$(call gb_UnpackedTarball_get_target,%) :
	$(call gb_UnpackedTarball__command,$@,$*,$(call gb_UnpackedTarball_get_dir,$*))

$(call gb_UnpackedTarball_get_final_target,%) :
	touch $@

.PHONY : $(call gb_UnpackedTarball_get_clean_target,%)
$(call gb_UnpackedTarball_get_clean_target,%) :
	$(call gb_Output_announce,$*,$(false),PAT,2)
	$(call gb_Helper_abbreviate_dirs,\
		rm -rf \
			$(call gb_UnpackedTarball_get_final_target,$*) \
			$(call gb_UnpackedTarball_get_target,$*) \
			$(call gb_UnpackedTarball_get_preparation_target,$*) \
			$(call gb_UnpackedTarball_get_dir,$*) \
			$(call gb_UnpackedTarball_get_pristine_dir,$*) \
	)

# Initialize unpacked tarball
define gb_UnpackedTarball_UnpackedTarball_internal
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_DESTFILES :=
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_FILES :=
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_FIX_EOL :=
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PATCHES :=
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PATCHLEVEL := $(gb_UnpackedTarball_PATCHLEVEL_DEFAULT)
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PATCHFLAGS :=
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_POST_ACTION :=
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PRE_ACTION :=
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_CONFIG_DIRS :=

$(call gb_UnpackedTarball_get_preparation_target,$(1)) : $(gb_Module_CURRENTMAKEFILE)
$(call gb_UnpackedTarball_get_preparation_target,$(1)) :| $(dir $(call gb_UnpackedTarball_get_target,$(1))).dir
$(call gb_UnpackedTarball_get_target,$(1)) : $(call gb_UnpackedTarball_get_preparation_target,$(1))
$(call gb_UnpackedTarball_get_target,$(1)) :| $(dir $(call gb_UnpackedTarball_get_target,$(1))).dir
$(call gb_UnpackedTarball_get_final_target,$(1)) : $(call gb_UnpackedTarball_get_target,$(1))

private gb_UnpackedTarball_PATTERN_RULES_$(1) :=

endef

# Define a new unpacked tarball
define gb_UnpackedTarball_UnpackedTarball
$(call gb_UnpackedTarball_UnpackedTarball_internal,$(1))

$$(eval $$(call gb_Module_register_target,$(call gb_UnpackedTarball_get_final_target,$(1)),$(call gb_UnpackedTarball_get_clean_target,$(1))))
$(call gb_Helper_make_userfriendly_targets,$(1),UnpackedTarball,$(call gb_UnpackedTarball_get_final_target,$(1)))

endef

# Convert line ending from dos to unix style for selected files
#
# This is done before applying patches, because patches expect unix
# style line ending, and the files are converted back after that. The
# files are relative to the unpacked path.
#
# gb_UnpackedTarball_fix_end_of_line unpacked file(s)
define gb_UnpackedTarball_fix_end_of_line
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_FIX_EOL += $(addprefix $(call gb_UnpackedTarball_get_dir,$(1))/,$(2))

endef


# Internal version of set_tarbal, mostly to avoid repeated invocation of $(shel
define gb_UnpackedTarball_set_tarball_internal
$(call gb_UnpackedTarget_UnpackedTarget,$(2),$(call gb_UnpackedTarball_get_dir,$(1)),$(3),$(4))
$(call gb_UnpackedTarball_get_target,$(1)) : $(call gb_UnpackedTarget_get_target,$(2))
$(call gb_UnpackedTarball_get_clean_target,$(1)) : $(call gb_UnpackedTarget_get_clean_target,$(2))
$(call gb_UnpackedTarget_get_target,$(2)) : $(call gb_UnpackedTarball_get_preparation_target,$(1))
$(if $(findstring in,$(5)),
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_IS_BIN_TARBALL := YES
$(call gb_ExternalProject_get_state_target,$(1),%) : UNPACKED_IS_BIN_TARBALL := YES)
$(if $(findstring out,$(5)),$(call gb_Module_get_target,$(4)) : $(TARFILE_LOCATION)/$(6)
$(TARFILE_LOCATION)/$(6) : $(call gb_Module_get_nonl10n_target,$(4))
	$$(call gb_Output_announce,$(6),$(true),PKB,3)
	if test ! -f "$$@" ; then cd $(call gb_UnpackedTarball_get_dir,) && $(GNUTAR) -czf "$$@" $(1)/ || $(GNUTAR) -czf "$$@" $(1)/ ; else touch "$$@" ; fi)

endef

# Set tarball name
#
# gb_UnpackedTarball_set_tarball unpacked tarball-name
define gb_UnpackedTarball_set_tarball
$(if $(USE_LIBRARY_BIN_TAR),
$(if $(4),
$(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)"),
$(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),\
$(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)")))
,
$(call gb_UnpackedTarball_set_tarball_internal,$(1),$(2),$(3),$(4),)
)
,
$(call gb_UnpackedTarball_set_tarball_internal,$(1),$(2),$(3),$(4),)
)

endef

# Set patch level to be used for all patches
#
# The default value is 3 to be able to work with current dmake patches.
#
# gb_UnpackedTarball_set_patchlevel unpacked level
define gb_UnpackedTarball_set_patchlevel
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PATCHLEVEL := $(2)

endef

# Add patch flags to be passed to patch for all patches cf. fdo#66826
#
# gb_UnpackedTarball_set_patchlevel unpacked level
define gb_UnpackedTarball_set_patchflags
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PATCHFLAGS := $(2)

endef

# Add a patch to be applied on the unpacked files
#
# gb_UnpackedTarball_add_patch unpacked patch
define gb_UnpackedTarball_add_patch
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PATCHES += $(SRCDIR)/$(2)
$(call gb_UnpackedTarball_get_preparation_target,$(1)) : $(SRCDIR)/$(2)

endef

# Add several patches at once
#
# gb_UnpackedTarball_add_patches unpacked patch(es)
define gb_UnpackedTarball_add_patches
$(foreach patch,$(2),$(call gb_UnpackedTarball_add_patch,$(1),$(patch)))

endef

# Add a file from source dir to the unpacked dir
#
# This function should not be used for overwriting existing files--use a
# patch for that purpose.
#
# gb_UnpackedTarball_add_file unpacked destfile file
define gb_UnpackedTarball_add_file
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_FILES += $(SRCDIR)/$(3)
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_DESTFILES += $(call gb_UnpackedTarball_get_dir,$(1))/$(2)
$(call gb_UnpackedTarball_get_preparation_target,$(1)) : $(SRCDIR)/$(3)

endef

# Add several files(s) from source dir to the unpacked dir
#
# The files are added into the specified subdir.
#
# gb_UnpackedTarball_add_files unpacked subdir file(s)
define gb_UnpackedTarball_add_files
$(foreach file,$(3),$(call gb_UnpackedTarball_add_file,$(1),$(2)/$(notdir $(file)),$(file)))

endef

# Set arbitrary shell command to be run during unpack
#
# The command is run at the very beginning, in freshly unpacked tarball.
# The command is run in the unpacked directory. If more than one command
# is used, care should be taken that the whole command fails if either
# of the sub-commands fails.
#
# gb_UnpackedTarball_set_pre_action unpacked shell-command
define gb_UnpackedTarball_set_pre_action
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_PRE_ACTION := $(strip $(2))

endef

# Set arbitrary shell command to be run during unpack
#
# The command is run at the very end: after patching, C++ extension
# mangling and copying additional files in. The command is run in the
# unpacked directory. If more than one command is used, care should be
# taken that the whole command fails if either of the sub-commands
# fails.
#
# NOTE: This is a bit hackish, but it is the easiest way to move files
# around or delete files (typically because the file causes build
# problems in the original location, c.f. clucene). This is doable by
# using -E with patch (we use GNU patch anyway), but it would mean an
# additional patch to maintain....
#
# gb_UnpackedTarball_set_post_action unpacked shell-command
define gb_UnpackedTarball_set_post_action
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_POST_ACTION := $(strip $(2))

endef

define gb_UnpackedTarbal__make_pattern_rule
$(call gb_UnpackedTarball_get_dir,$(1))/%$(2) :
	$$(if $$(wildcard $$@),,$$(call gb_Output_error,file $$@ does not exist in the tarball))
	touch $$@

$(eval gb_UnpackedTarball_PATTERN_RULES_$(1) += $(2))

endef

define gb_UnpackedTarbal__ensure_pattern_rule
$(if $(filter $(2),$(gb_UnpackedTarball_PATTERN_RULES_$(1))),,$(call gb_UnpackedTarbal__make_pattern_rule,$(1),$(2)))

endef

define gb_UnpackedTarbal__make_file_rule
$(call gb_UnpackedTarball_get_dir,$(1))/$(2) :
	$$(if $$(wildcard $$@),,$$(call gb_Output_error,file $$@ does not exist in the tarball))
	touch $$@

endef

# Mark a source file to be used outside of this module
#
# This results in the timestamp of the file being updated, so a possible
# change is recognized properly by other files depending on it. The
# update is run after possible post action.
#
# See description of class ExternalPackage for more information.
#
# gb_UnpackedTarball_mark_output_file unpacked file
define gb_UnpackedTarball_mark_output_file
$(call gb_UnpackedTarball_get_final_target,$(1)) : $(call gb_UnpackedTarball_get_dir,$(1))/$(2)
$(call gb_UnpackedTarball_get_dir,$(1))/$(2) : $(call gb_UnpackedTarball_get_target,$(1))
$(if $(suffix $(2)),\
	$(call gb_UnpackedTarbal__ensure_pattern_rule,$(1),$(suffix $(2))),\
	$(call gb_UnpackedTarbal__make_file_rule,$(1),$(2)) \
)

endef

# Mark several source files to be used outside of this module
#
# gb_UnpackedTarball_mark_output_files unpacked file(s)
define gb_UnpackedTarball_mark_output_files
$(foreach file,$(2),$(call gb_UnpackedTarball_mark_output_file,$(1),$(file)))

endef

# Replace project's config.{guess,sub} files by internal copies
#
# This is useful if the project's config files are outdated and don't
# allow build on some new arch. The internal copies are located at
# gb_UnpackedTarball_CONFIGDIR.
#
# If the configs are placed somewhere else than in the top-level dir of
# the project, pass the (relative) dir as second argument. (It can even
# be a list of dirs, if the project contains multiple subprojects, each
# with its own configure.)
#
# gb_UnpackedTarball_update_autoconf_configs unpacked dirs(s)?
define gb_UnpackedTarball_update_autoconf_configs
$(call gb_UnpackedTarball_get_target,$(1)) : UNPACKED_CONFIG_DIRS += $(if $(strip $(2)),$(2),.)

endef

# force the rebuild of an external target
# this only works when running as partial build.
#
%.rebuild :
	if [ -f $(call gb_UnpackedTarball_get_target,$*) ] ; then \
		touch $(call gb_UnpackedTarball_get_target,$*) ; \
		$(MAKE) ;\
	fi

%.genpatch :
	if [ -d $(call gb_UnpackedTarball_get_dir,$*) -a -d  $(call gb_UnpackedTarball_get_pristine_dir,$*) ] ; then \
		( \
		    patch_file=$$(pwd)/$*.new.patch.1; \
			cd $(call gb_UnpackedTarball_get_dir,) ; \
			diff -ur $*.org $* > $$patch_file; \
		    echo "Patch $$patch_file generated" ; \
		); \
	else \
		echo "Error: No pristine tarball available for $*" 1>&2 ; \
	fi


# vim: set noet sw=4 ts=4: