1#
2# Copyright (C) 2007 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17# When specifying "dist", the user has asked that we copy the important
18# files from this build into DIST_DIR.
19
20# list of all goals that depend on any dist files
21_all_dist_goals :=
22# pairs of goal:distfile
23_all_dist_goal_output_pairs :=
24# pairs of srcfile:distfile
25_all_dist_src_dst_pairs :=
26
27# Other parts of the system should use this function to associate
28# certain files with certain goals.  When those goals are built
29# and "dist" is specified, the marked files will be copied to DIST_DIR.
30#
31# $(1): a list of goals  (e.g. droid, sdk, ndk). These must be PHONY
32# $(2): the dist files to add to those goals.  If the file contains ':',
33#       the text following the colon is the name that the file is copied
34#       to under the dist directory.  Subdirs are ok, and will be created
35#       at copy time if necessary.
36define dist-for-goals
37$(if $(strip $(2)), \
38  $(eval _all_dist_goals += $$(1))) \
39$(foreach file,$(2), \
40  $(eval src := $(call word-colon,1,$(file))) \
41  $(eval dst := $(call word-colon,2,$(file))) \
42  $(if $(dst),,$(eval dst := $$(notdir $$(src)))) \
43  $(eval _all_dist_src_dst_pairs += $$(src):$$(dst)) \
44  $(foreach goal,$(1), \
45    $(eval _all_dist_goal_output_pairs += $$(goal):$$(dst))))
46endef
47
48define add_file_name_tag_suffix
49$(basename $(notdir $1))-FILE_NAME_TAG_PLACEHOLDER$(suffix $1)
50endef
51
52# This function appends suffix FILE_NAME_TAG_PLACEHOLDER from the input file
53# $(1): a list of goals  (e.g. droid, sdk, ndk). These must be PHONY
54# $(2): the dist files to add to those goals.
55define dist-for-goals-with-filenametag
56$(if $(strip $(2)), \
57  $(foreach file,$(2), \
58    $(call dist-for-goals,$(1),$(file):$(call add_file_name_tag_suffix,$(file)))))
59endef
60.PHONY: shareprojects
61
62define __share-projects-rule
63$(1) : PRIVATE_TARGETS := $(2)
64$(1): $(2) $(COMPLIANCE_LISTSHARE)
65	$(hide) rm -f $$@
66	mkdir -p $$(dir $$@)
67	$$(if $$(strip $$(PRIVATE_TARGETS)),OUT_DIR=$(OUT_DIR) $(COMPLIANCE_LISTSHARE) -o $$@ $$(PRIVATE_TARGETS),touch $$@)
68endef
69
70# build list of projects to share in $(1) for meta_lic in $(2)
71#
72# $(1): the intermediate project sharing file
73# $(2): the license metadata to base the sharing on
74define _share-projects-rule
75$(eval $(call __share-projects-rule,$(1),$(2)))
76endef
77
78.PHONY: alllicensetexts
79
80define __license-texts-rule
81$(2) : PRIVATE_GOAL := $(1)
82$(2) : PRIVATE_TARGETS := $(3)
83$(2) : PRIVATE_ROOTS := $(4)
84$(2) : PRIVATE_ARGUMENT_FILE := $(call intermediates-dir-for,METAPACKAGING,licensetexts)/$(2)/arguments
85$(2): $(3) $(TEXTNOTICE)
86	$(hide) rm -f $$@
87	mkdir -p $$(dir $$@)
88	mkdir -p $$(dir $$(PRIVATE_ARGUMENT_FILE))
89	$$(if $$(strip $$(PRIVATE_TARGETS)),$$(call dump-words-to-file,\
90            -product="$$(PRIVATE_GOAL)" -title="$$(PRIVATE_GOAL)" \
91            $$(addprefix -strip_prefix ,$$(PRIVATE_ROOTS)) \
92            -strip_prefix=$(PRODUCT_OUT)/ -strip_prefix=$(HOST_OUT)/\
93            $$(PRIVATE_TARGETS),\
94            $$(PRIVATE_ARGUMENT_FILE)))
95	$$(if $$(strip $$(PRIVATE_TARGETS)),OUT_DIR=$(OUT_DIR) $(TEXTNOTICE) -o $$@ @$$(PRIVATE_ARGUMENT_FILE),touch $$@)
96endef
97
98# build list of projects to share in $(2) for meta_lic in $(3) for dist goals $(1)
99# Strip `out/dist/` used as proxy for 'DIST_DIR'
100#
101# $(1): the name of the dist goals
102# $(2): the intermediate project sharing file
103# $(3): the license metadata to base the sharing on
104define _license-texts-rule
105$(eval $(call __license-texts-rule,$(1),$(2),$(3),out/dist/))
106endef
107
108###########################################################
109## License metadata build rule for dist target $(1) with meta_lic $(2) copied from $(3)
110###########################################################
111define _dist-target-license-metadata-rule
112$(strip $(eval _meta :=$(2)))
113$(strip $(eval _dep:=))
114# 0p is the indicator for a non-copyrightable file where no party owns the copyright.
115# i.e. pure data with no copyrightable expression.
116# If all of the sources are 0p and only 0p, treat the copied file as 0p. Otherwise, all
117# of the sources must either be 0p or originate from a single metadata file to copy.
118$(strip $(foreach s,$(strip $(3)),\
119  $(eval _dmeta:=$(ALL_TARGETS.$(s).META_LIC))\
120  $(if $(strip $(_dmeta)),\
121    $(if $(filter-out 0p,$(_dep)),\
122      $(if $(filter-out $(_dep) 0p,$(_dmeta)),\
123        $(error cannot copy target from multiple modules: $(1) from $(_dep) and $(_dmeta)),\
124        $(if $(filter 0p,$(_dep)),$(eval _dep:=$(_dmeta)))),\
125      $(eval _dep:=$(_dmeta))\
126    ),\
127    $(eval TARGETS_MISSING_LICENSE_METADATA += $(s) $(1)))))
128
129
130ifeq (0p,$(strip $(_dep)))
131# Not copyrightable. No emcumbrances, no license text, no license kind etc.
132$(_meta): PRIVATE_CONDITIONS := unencumbered
133$(_meta): PRIVATE_SOURCES := $(3)
134$(_meta): PRIVATE_INSTALLED := $(1)
135# use `$(1)` which is the unique and relatively short `out/dist/$(target)`
136$(_meta): PRIVATE_ARGUMENT_FILE := $(call intermediates-dir-for,METAPACKAGING,notice)/$(1)/arguments
137$(_meta): $(BUILD_LICENSE_METADATA)
138$(_meta) :
139	rm -f $$@
140	mkdir -p $$(dir $$@)
141	mkdir -p $$(dir $$(PRIVATE_ARGUMENT_FILE))
142	$$(call dump-words-to-file,\
143	    $$(addprefix -c ,$$(PRIVATE_CONDITIONS))\
144	    $$(addprefix -s ,$$(PRIVATE_SOURCES))\
145	    $$(addprefix -t ,$$(PRIVATE_TARGETS))\
146	    $$(addprefix -i ,$$(PRIVATE_INSTALLED)),\
147	    $$(PRIVATE_ARGUMENT_FILE))
148	OUT_DIR=$(OUT_DIR) $(BUILD_LICENSE_METADATA) \
149	  @$$(PRIVATE_ARGUMENT_FILE) \
150	  -o $$@
151
152else ifneq (,$(strip $(_dep)))
153# Not a missing target, copy metadata and `is_container` etc. from license metadata file `$(_dep)`
154$(_meta): PRIVATE_DEST_TARGET := $(1)
155$(_meta): PRIVATE_SOURCE_TARGETS := $(3)
156$(_meta): PRIVATE_SOURCE_METADATA := $(_dep)
157# use `$(1)` which is the unique and relatively short `out/dist/$(target)`
158$(_meta): PRIVATE_ARGUMENT_FILE := $(call intermediates-dir-for,METAPACKAGING,copynotice)/$(1)/arguments
159$(_meta) : $(_dep) $(COPY_LICENSE_METADATA)
160	rm -f $$@
161	mkdir -p $$(dir $$@)
162	mkdir -p $$(dir $$(PRIVATE_ARGUMENT_FILE))
163	$$(call dump-words-to-file,\
164	    $$(addprefix -i ,$$(PRIVATE_DEST_TARGET))\
165	    $$(addprefix -s ,$$(PRIVATE_SOURCE_TARGETS))\
166	    $$(addprefix -d ,$$(PRIVATE_SOURCE_METADATA)),\
167	    $$(PRIVATE_ARGUMENT_FILE))
168	OUT_DIR=$(OUT_DIR) $(COPY_LICENSE_METADATA) \
169	  @$$(PRIVATE_ARGUMENT_FILE) \
170	  -o $$@
171
172endif
173endef
174
175# use `out/dist/` as a proxy for 'DIST_DIR'
176define _add_projects_to_share
177$(strip $(eval _mdir := $(call intermediates-dir-for,METAPACKAGING,meta)/out/dist)) \
178$(strip $(eval _idir := $(call intermediates-dir-for,METAPACKAGING,shareprojects))) \
179$(strip $(eval _tdir := $(call intermediates-dir-for,METAPACKAGING,licensetexts))) \
180$(strip $(eval _allt := $(sort $(foreach goal,$(_all_dist_goal_output_pairs),$(call word-colon,2,$(goal)))))) \
181$(foreach target,$(_allt), \
182  $(eval _goals := $(sort $(foreach dg,$(filter %:$(target),$(_all_dist_goal_output_pairs)),$(call word-colon,1,$(dg))))) \
183  $(eval _srcs := $(sort $(foreach sdp,$(filter %:$(target),$(_all_dist_src_dst_pairs)),$(call word-colon,1,$(sdp))))) \
184  $(eval $(call _dist-target-license-metadata-rule,out/dist/$(target),$(_mdir)/out/dist/$(target).meta_lic,$(_srcs))) \
185  $(eval _f := $(_idir)/$(target).shareprojects) \
186  $(eval _n := $(_tdir)/$(target).txt) \
187  $(eval $(call dist-for-goals,$(_goals),$(_f):shareprojects/$(target).shareprojects)) \
188  $(eval $(call dist-for-goals,$(_goals),$(_n):licensetexts/$(target).txt)) \
189  $(eval $(call _share-projects-rule,$(_f),$(foreach t, $(filter-out $(TARGETS_MISSING_LICENSE_METADATA),out/dist/$(target)),$(_mdir)/$(t).meta_lic))) \
190  $(eval $(call _license-texts-rule,$(_goals),$(_n),$(foreach t,$(filter-out $(TARGETS_MISSING_LICENSE_METADATA),out/dist/$(target)),$(_mdir)/$(t).meta_lic))) \
191)
192endef
193
194#------------------------------------------------------------------
195# To be used at the end of the build to collect all the uses of
196# dist-for-goals, and write them into a file for the packaging step to use.
197
198# $(1): The file to write
199define dist-write-file
200$(strip \
201  $(call _add_projects_to_share)\
202  $(if $(strip $(ANDROID_REQUIRE_LICENSE_METADATA)),\
203    $(foreach target,$(sort $(TARGETS_MISSING_LICENSE_METADATA)),$(warning target $(target) missing license metadata))\
204    $(if $(strip $(TARGETS_MISSING_LICENSE_METADATA)),\
205      $(if $(filter true error,$(ANDROID_REQUIRE_LICENSE_METADATA)),\
206        $(error $(words $(sort $(TARGETS_MISSING_LICENSE_METADATA))) targets need license metadata))))\
207  $(foreach t,$(sort $(ALL_NON_MODULES)),$(call record-missing-non-module-dependencies,$(t))) \
208  $(eval $(call report-missing-licenses-rule)) \
209  $(eval $(call report-all-notice-library-names-rule)) \
210  $(KATI_obsolete_var dist-for-goals,Cannot be used after dist-write-file) \
211  $(foreach goal,$(sort $(_all_dist_goals)), \
212    $(eval $$(goal): _dist_$$(goal))) \
213  $(shell mkdir -p $(dir $(1))) \
214  $(file >$(1).tmp, \
215    DIST_GOAL_OUTPUT_PAIRS := $(sort $(_all_dist_goal_output_pairs)) \
216    $(newline)DIST_SRC_DST_PAIRS := $(sort $(_all_dist_src_dst_pairs))) \
217  $(shell if ! cmp -s $(1).tmp $(1); then \
218            mv $(1).tmp $(1); \
219          else \
220            rm $(1).tmp; \
221          fi))
222endef
223
224.KATI_READONLY := dist-for-goals dist-write-file dist-for-goals-with-filenametag
225