Joey Armstrong | a532539 | 2024-04-02 13:22:12 -0400 | [diff] [blame] | 1 | # -*- makefile -*- |
| 2 | # ----------------------------------------------------------------------- |
| 3 | # Copyright 2017-2024 Open Networking Foundation (ONF) Contributors |
| 4 | # |
| 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 |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 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. |
| 16 | # ----------------------------------------------------------------------- |
| 17 | # SPDX-FileCopyrightText: 2017-2024 Open Networking Foundation Contributors |
| 18 | # SPDX-License-Identifier: Apache-2.0 |
| 19 | # ----------------------------------------------------------------------- |
| 20 | # Intent: This makefile is intended for extremely limited use: |
| 21 | # o A temp directory is created for internal makefile use. |
| 22 | # o Installers, downloads, etc. |
| 23 | # o Allowing garbage to persist in /tmp is not desirable behavior. |
| 24 | # o Create breadcrumbs for the sterile target to find and remove later. |
| 25 | # ----------------------------------------------------------------------- |
| 26 | |
| 27 | ##-------------------## |
| 28 | ##---] GLOBALS [---## |
| 29 | ##-------------------## |
| 30 | lf-mk-tmp := $(shell mktemp --directory -t 'repo-LF-make.XXXXXXXXXX') |
| 31 | lf-mk-tmp-name := $(notdir $(lf-mk-tmp)) |
| 32 | |
| 33 | lf-mk-user-meta ?= $(dir $(lf-mk-tmp)/)$(USER).mk.meta |
| 34 | lf-mk-user-mktemp ?= $(lf-mk-user-meta)/mktemp |
| 35 | |
| 36 | lf-mk-tmp-create := $(lf-mk-tmp)/.exists |
| 37 | lf-mk-meta-create := $(lf-mk-user-mktemp)/$(lf-mk-tmp-name) |
| 38 | |
| 39 | ## ----------------------------------------------------------------------- |
| 40 | ## ----------------------------------------------------------------------- |
| 41 | .PHONY: lf-tempdir |
| 42 | lf-tempdir : lf-mk-meta-create |
| 43 | |
| 44 | ## ----------------------------------------------------------------------- |
| 45 | ## Intent: Create tempdir |
| 46 | ## o Create a temp directory |
| 47 | ## o Create a timestamp file to record time of creation |
| 48 | ## ----------------------------------------------------------------------- |
| 49 | .PHONY: lf-mk-tmp-create |
| 50 | lf-mk-tmp-create : $(lf-mk-tmp-create) |
| 51 | $(lf-mk-tmp-create) : |
| 52 | |
| 53 | $(if $(DEBUG),$(call banner-enter,$@)) |
| 54 | @mkdir -p $(dir $@) |
| 55 | @touch $@ |
| 56 | $(if $(DEBUG),$(call banner-leave,$@)) |
| 57 | |
| 58 | ## ----------------------------------------------------------------------- |
| 59 | ## Intent: Create a timestamp file for temp directory cleanup: |
| 60 | ## o Create a storage directory |
| 61 | ## o Record timestamp file used by temp directory creation |
| 62 | ## ----------------------------------------------------------------------- |
| 63 | .PHONY: lf-mk-meta-create |
| 64 | lf-mk-meta-create : lf-mk-tmp-create $(lf-mk-meta-create) |
| 65 | $(lf-mk-meta-create) : |
| 66 | |
| 67 | $(if $(DEBUG),$(call banner-enter,$@)) |
| 68 | @mkdir -p $(dir $@) |
| 69 | @printf "$(lf-mk-tmp-create)" >> "$(lf-mk-user-mktemp)/$(lf-mk-tmp-name)" |
| 70 | $(if $(DEBUG),$(call banner-leave,$@)) |
| 71 | |
| 72 | ## ----------------------------------------------------------------------- |
| 73 | ## Intent: Remove temp directories created by a makefile run. |
| 74 | ## o Automated directory cleanup is not easily supported by make. |
| 75 | ## o Use breadcrumbs dropped during tempdir creation for removal. |
| 76 | ## o Only consider subdirs older than the current makefile run. |
| 77 | ## o Only consider subdirs older than +n days (persistent cron jobs). |
| 78 | ## ----------------------------------------------------------------------- |
| 79 | sterile-timestamp = $(shell date '+%Y-%m-%d' -d '3 days ago') |
| 80 | sterile :: |
| 81 | |
| 82 | # Create a reference file to remove lingering stale directories |
| 83 | $(HELP)touch --date "$(sterile-timestamp)" "$(lf-mk-tmp-create)-stale" |
| 84 | |
| 85 | $(HIDE)find '$(dir $(lf-mk-tmp))' -maxdepth 1 -name 'repo-LF-make*' -type d -print0 \ |
| 86 | | $(xargs-cmd0) -I'{}' find {} -name '.exists' -not -newer "$(lf-mk-tmp-create)-stale" -print0 \ |
| 87 | | $(xargs-n1-clean) dirname --zero \ |
| 88 | | $(xargs-n1-clean) $(RM) -vr |
| 89 | |
| 90 | # Remove late: target dep (sterile : clean) would remove breadcrumbs |
| 91 | @$(MAKE) --no-print-directory clean |
| 92 | |
| 93 | ## ----------------------------------------------------------------------- |
| 94 | ## Intent : Remove mktemp for the current invocation |
| 95 | ## ----------------------------------------------------------------------- |
| 96 | clean :: |
| 97 | $(RM) -r "$(lf-mk-tmp)" |
| 98 | |
| 99 | # [EOF] |