2 # Copyright (c) 2019 Google LLC. All Rights Reserved.
3 # Copyright (c) 2018 Nest Labs Inc. All Rights Reserved.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
20 # This file is the generic "header" or pre make header for all
21 # common host-specific (i.e., non-target and -toolchain-specific)
22 # tools included in any makefile used in the build tree.
35 MKDIR_P ?= $(MKDIR) -p
52 # nl-create-dir <directory>
54 # Create the specified directory, including any parent directories
58 $(NL_V_PROGRESS_MKDIR)
59 $(NL_V_AT)$(MKDIR_P) "$(1)"
63 # nl-remove-dir <directory>
65 # If the specified directory exists, then ensure all of the
66 # directories are writable by the current user, and then forcibly
67 # remove the directory and all of its contents, sleeping for five (5)
68 # seconds and failure before trying the removal again.
71 $(NL_V_PROGRESS_RMDIR)
72 $(NL_V_AT)if [ -d "$(1)" ]; then \
73 $(FIND) "$(1)" -type d ! -perm -200 -exec $(CHMOD) u+w {} ';' \
75 || { sleep 5 && $(RM) -rf "$(1)"; }; \
80 # nl-check-file-with-subroutine <subroutine prefix> <macro suffix>
82 # Check whether a file, referenced by the $(@) variable, should be
83 # updated / regenerated based on its dependencies, referenced by the
84 # $(<) variable by running the make macro check-file-<macro suffix>.
86 # The $(<) is passed as the first argument if the macro wants to process
87 # it and the prospective new output file, which the macro MUST
88 # generate, as the second.
90 # This macro will ensure that any required parent directories are created
91 # prior to invoking the subroutine <subroutine prefix>-<macro suffix>.
93 # This macro is similar to and inspired by that from Linux Kbuild and
96 # <subroutine prefix> - The subroutine name, prefixed to "-<macro suffix>",
97 # which, together with <macro suffix>, indicates the
98 # make macro to invoke to actually check the file.
100 # <macro suffix> - The name, suffixed to "<subroutine prefix>-", which
101 # together with <subroutine prefix>, indicates the
102 # make macro to invoke to actually check the file.
105 nl-check-file-N:=$(shell echo N$$$$)
106 define nl-check-file-with-subroutine
107 $(NL_V_AT)$(_NL_PROGRESS) "CHECK" "$(@)"; \
108 $(MKDIR_P) $(dir $(@)); \
109 $(call $(1)-$(2),$(<),$(@).$(nl-check-file-N)); \
110 if [ -r "$(@)" ] && \
111 $(CMP) -s "$(@)" "$(@).$(nl-check-file-N)"; then \
112 rm -f "$(@).$(nl-check-file-N)"; \
114 $(_NL_PROGRESS) "GEN" "$(@)"; \
115 mv -f "$(@).$(nl-check-file-N)" "$(@)"; \
117 endef # nl-check-file
120 # nl-check-file <macro suffix>
122 # Check whether a file, referenced by the $(@) variable, should be
123 # updated / regenerated based on its dependencies, referenced by the
124 # $(<) variable by running the make macro check-file-<macro suffix>.
126 # The $(<) is passed as the first argument if the macro wants to process
127 # it and the prospective new output file, which the macro MUST
128 # generate, as the second.
130 # This macro will ensure that any required parent directories are created
131 # prior to invoking check-file-<macro suffix>.
133 # This macro is similar to and inspired by that from Linux Kbuild and
136 # <macro suffix> - The name, suffixed to "check-file-", which indicates
137 # the make macro to invoke.
141 $(call nl-check-file-with-subroutine,nl-check-file,$(1))
142 endef # nl-check-file
147 # Create the symbolic link with the source of the $(<) variable and
148 # the destination of the $(@) variable, using the LN_S macro.
150 define nl-create-link
151 $(NL_V_PROGRESS_LN_S)
152 $(NL_V_AT)$(LN_S) $(<) $(@)
153 endef # nl-create-link