blob: bce8e7fd3c2ce282159236217c14fcc76e863ddc [file] [log] [blame]
The Android Open Source Project88b60792009-03-03 19:28:42 -08001#
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
Dan Willemsen78c40be2018-10-17 16:50:49 -070020# 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
Ying Wang534fcd72013-03-01 16:45:35 -080025_all_dist_src_dst_pairs :=
Dan Willemsen78c40be2018-10-17 16:50:49 -070026
The Android Open Source Project88b60792009-03-03 19:28:42 -080027# 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#
Dan Willemsena3f66322020-05-28 15:46:33 -070031# $(1): a list of goals (e.g. droid, sdk, ndk). These must be PHONY
The Android Open Source Project88b60792009-03-03 19:28:42 -080032# $(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
Dan Willemsenc904e482018-10-20 16:59:41 -070037$(if $(strip $(2)), \
38 $(eval _all_dist_goals += $$(1))) \
The Android Open Source Project88b60792009-03-03 19:28:42 -080039$(foreach file,$(2), \
Dan Willemsen78c40be2018-10-17 16:50:49 -070040 $(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))))
The Android Open Source Project88b60792009-03-03 19:28:42 -080046endef
47
Bob Badourd227fac2022-06-14 18:18:45 +000048.PHONY: shareprojects
Bob Badourd227fac2022-06-14 18:18:45 +000049
50define __share-projects-rule
51$(1) : PRIVATE_TARGETS := $(2)
Bob Badourd227fac2022-06-14 18:18:45 +000052$(1): $(2) $(COMPLIANCE_LISTSHARE)
53 $(hide) rm -f $$@
54 mkdir -p $$(dir $$@)
Bob Badour40d38b42022-06-22 00:58:39 +000055 $$(if $$(strip $$(PRIVATE_TARGETS)),OUT_DIR=$(OUT_DIR) $(COMPLIANCE_LISTSHARE) -o $$@ $$(PRIVATE_TARGETS),touch $$@)
Bob Badourd227fac2022-06-14 18:18:45 +000056endef
57
Bob Badour40d38b42022-06-22 00:58:39 +000058# build list of projects to share in $(1) for meta_lic in $(2)
Bob Badourd227fac2022-06-14 18:18:45 +000059#
60# $(1): the intermediate project sharing file
Bob Badour40d38b42022-06-22 00:58:39 +000061# $(2): the license metadata to base the sharing on
Bob Badourd227fac2022-06-14 18:18:45 +000062define _share-projects-rule
Bob Badour40d38b42022-06-22 00:58:39 +000063$(eval $(call __share-projects-rule,$(1),$(2)))
Bob Badourd227fac2022-06-14 18:18:45 +000064endef
65
Bob Badourdce80ab2022-06-14 18:19:30 +000066.PHONY: alllicensetexts
67
68define __license-texts-rule
69$(2) : PRIVATE_GOAL := $(1)
70$(2) : PRIVATE_TARGETS := $(3)
71$(2) : PRIVATE_ROOTS := $(4)
72$(2) : PRIVATE_ARGUMENT_FILE := $(call intermediates-dir-for,METAPACKAGING,licensetexts)/$(2)/arguments
73$(2): $(3) $(TEXTNOTICE)
74 $(hide) rm -f $$@
75 mkdir -p $$(dir $$@)
76 mkdir -p $$(dir $$(PRIVATE_ARGUMENT_FILE))
77 $$(if $$(strip $$(PRIVATE_TARGETS)),$$(call dump-words-to-file,\
78 -product="$$(PRIVATE_GOAL)" -title="$$(PRIVATE_GOAL)" \
79 $$(addprefix -strip_prefix ,$$(PRIVATE_ROOTS)) \
80 -strip_prefix=$(PRODUCT_OUT)/ -strip_prefix=$(HOST_OUT)/\
81 $$(PRIVATE_TARGETS),\
82 $$(PRIVATE_ARGUMENT_FILE)))
83 $$(if $$(strip $$(PRIVATE_TARGETS)),OUT_DIR=$(OUT_DIR) $(TEXTNOTICE) -o $$@ @$$(PRIVATE_ARGUMENT_FILE),touch $$@)
84endef
85
Bob Badour40d38b42022-06-22 00:58:39 +000086# build list of projects to share in $(2) for meta_lic in $(3) for dist goals $(1)
87# Strip `out/dist/` used as proxy for 'DIST_DIR'
Bob Badourdce80ab2022-06-14 18:19:30 +000088#
Bob Badour40d38b42022-06-22 00:58:39 +000089# $(1): the name of the dist goals
Bob Badourdce80ab2022-06-14 18:19:30 +000090# $(2): the intermediate project sharing file
Bob Badour40d38b42022-06-22 00:58:39 +000091# $(3): the license metadata to base the sharing on
Bob Badourdce80ab2022-06-14 18:19:30 +000092define _license-texts-rule
Bob Badour40d38b42022-06-22 00:58:39 +000093$(eval $(call __license-texts-rule,$(1),$(2),$(3),out/dist/))
Bob Badourdce80ab2022-06-14 18:19:30 +000094endef
95
Bob Badour40d38b42022-06-22 00:58:39 +000096###########################################################
97## License metadata build rule for dist target $(1) with meta_lic $(2) copied from $(3)
98###########################################################
99define _dist-target-license-metadata-rule
100$(strip $(eval _meta :=$(2)))
101$(strip $(eval _dep:=))
102# 0p is the indicator for a non-copyrightable file where no party owns the copyright.
103# i.e. pure data with no copyrightable expression.
104# If all of the sources are 0p and only 0p, treat the copied file as 0p. Otherwise, all
105# of the sources must either be 0p or originate from a single metadata file to copy.
106$(strip $(foreach s,$(strip $(3)),\
107 $(eval _dmeta:=$(ALL_TARGETS.$(s).META_LIC))\
108 $(if $(strip $(_dmeta)),\
109 $(if $(filter-out 0p,$(_dep)),\
110 $(if $(filter-out $(_dep) 0p,$(_dmeta)),\
111 $(error cannot copy target from multiple modules: $(1) from $(_dep) and $(_dmeta)),\
112 $(if $(filter 0p,$(_dep)),$(eval _dep:=$(_dmeta)))),\
113 $(eval _dep:=$(_dmeta))\
114 ),\
115 $(eval TARGETS_MISSING_LICENSE_METADATA += $(s) $(1)))))
116
117
118ifeq (0p,$(strip $(_dep)))
119# Not copyrightable. No emcumbrances, no license text, no license kind etc.
120$(_meta): PRIVATE_CONDITIONS := unencumbered
121$(_meta): PRIVATE_SOURCES := $(3)
122$(_meta): PRIVATE_INSTALLED := $(1)
123# use `$(1)` which is the unique and relatively short `out/dist/$(target)`
124$(_meta): PRIVATE_ARGUMENT_FILE := $(call intermediates-dir-for,METAPACKAGING,notice)/$(1)/arguments
125$(_meta): $(BUILD_LICENSE_METADATA)
126$(_meta) :
127 rm -f $$@
128 mkdir -p $$(dir $$@)
129 mkdir -p $$(dir $$(PRIVATE_ARGUMENT_FILE))
130 $$(call dump-words-to-file,\
131 $$(addprefix -c ,$$(PRIVATE_CONDITIONS))\
132 $$(addprefix -s ,$$(PRIVATE_SOURCES))\
133 $$(addprefix -t ,$$(PRIVATE_TARGETS))\
134 $$(addprefix -i ,$$(PRIVATE_INSTALLED)),\
135 $$(PRIVATE_ARGUMENT_FILE))
136 OUT_DIR=$(OUT_DIR) $(BUILD_LICENSE_METADATA) \
137 @$$(PRIVATE_ARGUMENT_FILE) \
138 -o $$@
139
140else ifneq (,$(strip $(_dep)))
141# Not a missing target, copy metadata and `is_container` etc. from license metadata file `$(_dep)`
142$(_meta): PRIVATE_DEST_TARGET := $(1)
143$(_meta): PRIVATE_SOURCE_TARGETS := $(3)
144$(_meta): PRIVATE_SOURCE_METADATA := $(_dep)
145# use `$(1)` which is the unique and relatively short `out/dist/$(target)`
146$(_meta): PRIVATE_ARGUMENT_FILE := $(call intermediates-dir-for,METAPACKAGING,copynotice)/$(1)/arguments
147$(_meta) : $(_dep) $(COPY_LICENSE_METADATA)
148 rm -f $$@
149 mkdir -p $$(dir $$@)
150 mkdir -p $$(dir $$(PRIVATE_ARGUMENT_FILE))
151 $$(call dump-words-to-file,\
152 $$(addprefix -i ,$$(PRIVATE_DEST_TARGET))\
153 $$(addprefix -s ,$$(PRIVATE_SOURCE_TARGETS))\
154 $$(addprefix -d ,$$(PRIVATE_SOURCE_METADATA)),\
155 $$(PRIVATE_ARGUMENT_FILE))
156 OUT_DIR=$(OUT_DIR) $(COPY_LICENSE_METADATA) \
157 @$$(PRIVATE_ARGUMENT_FILE) \
158 -o $$@
159
160endif
161endef
162
163# use `out/dist/` as a proxy for 'DIST_DIR'
Bob Badourd227fac2022-06-14 18:18:45 +0000164define _add_projects_to_share
Bob Badour40d38b42022-06-22 00:58:39 +0000165$(strip $(eval _mdir := $(call intermediates-dir-for,METAPACKAGING,meta)/out/dist)) \
Bob Badourdce80ab2022-06-14 18:19:30 +0000166$(strip $(eval _idir := $(call intermediates-dir-for,METAPACKAGING,shareprojects))) \
167$(strip $(eval _tdir := $(call intermediates-dir-for,METAPACKAGING,licensetexts))) \
Bob Badour40d38b42022-06-22 00:58:39 +0000168$(strip $(eval _allt := $(sort $(foreach goal,$(_all_dist_goal_output_pairs),$(call word-colon,2,$(goal)))))) \
169$(foreach target,$(_allt), \
170 $(eval _goals := $(sort $(foreach dg,$(filter %:$(target),$(_all_dist_goal_output_pairs)),$(call word-colon,1,$(dg))))) \
171 $(eval _srcs := $(sort $(foreach sdp,$(filter %:$(target),$(_all_dist_src_dst_pairs)),$(call word-colon,1,$(sdp))))) \
172 $(eval $(call _dist-target-license-metadata-rule,out/dist/$(target),$(_mdir)/out/dist/$(target).meta_lic,$(_srcs))) \
173 $(eval _f := $(_idir)/$(target).shareprojects) \
174 $(eval _n := $(_tdir)/$(target).txt) \
175 $(eval $(call dist-for-goals,$(_goals),$(_f):shareprojects/$(target).shareprojects)) \
176 $(eval $(call dist-for-goals,$(_goals),$(_n):licensetexts/$(target).txt)) \
177 $(eval $(call _share-projects-rule,$(_f),$(foreach t, $(filter-out $(TARGETS_MISSING_LICENSE_METADATA),out/dist/$(target)),$(_mdir)/$(t).meta_lic))) \
178 $(eval $(call _license-texts-rule,$(_goals),$(_n),$(foreach t,$(filter-out $(TARGETS_MISSING_LICENSE_METADATA),out/dist/$(target)),$(_mdir)/$(t).meta_lic))) \
Bob Badourd227fac2022-06-14 18:18:45 +0000179)
180endef
181
Dan Willemsen78c40be2018-10-17 16:50:49 -0700182#------------------------------------------------------------------
183# To be used at the end of the build to collect all the uses of
184# dist-for-goals, and write them into a file for the packaging step to use.
The Android Open Source Project88b60792009-03-03 19:28:42 -0800185
Dan Willemsen78c40be2018-10-17 16:50:49 -0700186# $(1): The file to write
187define dist-write-file
188$(strip \
Bob Badourd227fac2022-06-14 18:18:45 +0000189 $(call _add_projects_to_share)\
190 $(if $(strip $(ANDROID_REQUIRE_LICENSE_METADATA)),\
191 $(foreach target,$(sort $(TARGETS_MISSING_LICENSE_METADATA)),$(warning target $(target) missing license metadata))\
192 $(if $(strip $(TARGETS_MISSING_LICENSE_METADATA)),\
193 $(if $(filter true error,$(ANDROID_REQUIRE_LICENSE_METADATA)),\
194 $(error $(words $(sort $(TARGETS_MISSING_LICENSE_METADATA))) targets need license metadata))))\
195 $(foreach t,$(sort $(ALL_NON_MODULES)),$(call record-missing-non-module-dependencies,$(t))) \
196 $(eval $(call report-missing-licenses-rule)) \
197 $(eval $(call report-all-notice-library-names-rule)) \
Dan Willemsen78c40be2018-10-17 16:50:49 -0700198 $(KATI_obsolete_var dist-for-goals,Cannot be used after dist-write-file) \
199 $(foreach goal,$(sort $(_all_dist_goals)), \
200 $(eval $$(goal): _dist_$$(goal))) \
201 $(shell mkdir -p $(dir $(1))) \
202 $(file >$(1).tmp, \
203 DIST_GOAL_OUTPUT_PAIRS := $(sort $(_all_dist_goal_output_pairs)) \
204 $(newline)DIST_SRC_DST_PAIRS := $(sort $(_all_dist_src_dst_pairs))) \
205 $(shell if ! cmp -s $(1).tmp $(1); then \
206 mv $(1).tmp $(1); \
207 else \
208 rm $(1).tmp; \
209 fi))
The Android Open Source Project88b60792009-03-03 19:28:42 -0800210endef
211
Dan Willemsen78c40be2018-10-17 16:50:49 -0700212.KATI_READONLY := dist-for-goals dist-write-file