blob: 0175083febd9676607564fe0cb45301b3550f4b1 [file] [log] [blame]
Joey Armstrongf128de82023-09-08 17:05:18 -04001#!/bin/bash
Joey Armstrong0003f1a2023-10-18 16:40:23 -04002# -*- makefile -*-
3# -----------------------------------------------------------------------
4# Copyright 2023 Open Networking Foundation (ONF) and the ONF Contributors
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17# -----------------------------------------------------------------------
18# SPDX-FileCopyrightText: 2023 Open Networking Foundation (ONF) and the ONF Contributors
19# SPDX-License-Identifier: Apache-2.0
20# -----------------------------------------------------------------------
21## Intent: This script will update a repository makefiles/ directory
22## by creating a hierarchy that will allow using library makefiles
23## and per-repository makefiles.
Joey Armstrongfedf45e2023-09-20 11:40:01 -040024## -----------------------------------------------------------------------
Joey Armstrongf128de82023-09-08 17:05:18 -040025
Joey Armstrong0003f1a2023-10-18 16:40:23 -040026##-------------------##
27##---] GLOBALS [---##
28##-------------------##
Joey Armstrongf128de82023-09-08 17:05:18 -040029set -euo pipefail
Joey Armstronga2db6cd2023-11-30 12:16:18 -050030# declare -g -i debug=1
Joey Armstrongf128de82023-09-08 17:05:18 -040031
Joey Armstrong0003f1a2023-10-18 16:40:23 -040032## -----------------------------------------------------------------------
33## Intent: Display a message with formatting
34## -----------------------------------------------------------------------
35function banner()
36{
37 cat <<EOM
Joey Armstrongfedf45e2023-09-20 11:40:01 -040038
Joey Armstrong0003f1a2023-10-18 16:40:23 -040039** -----------------------------------------------------------------------
40** $*
41** -----------------------------------------------------------------------
42EOM
43 return
44}
45
46## -----------------------------------------------------------------------
47## Intent: Display an error mesage then exit with status
48## -----------------------------------------------------------------------
49function error()
50{
51 echo "ERROR: $*"
52 exit 1
53}
54
Joey Armstronga2db6cd2023-11-30 12:16:18 -050055## -----------------------------------------------------------------------
56## Intent: Archive current directory before we begin
57## -----------------------------------------------------------------------
58function archive_sandbox()
59{
60 local abs="$(realpath --canonicalize-existing '.')"
61 local dir="${abs##*/}"
62 local ts="$(date '+%Y%m%d%H%M%S')"
63 local prefix="../${dir}-all/backups"
Joey Armstrong0003f1a2023-10-18 16:40:23 -040064
Joey Armstronga2db6cd2023-11-30 12:16:18 -050065 banner "Archive current directory ($dir)"
Joey Armstrong0003f1a2023-10-18 16:40:23 -040066
Joey Armstronga2db6cd2023-11-30 12:16:18 -050067# make sterile >/dev/null # nuke lingering .venv/ installs
68# make clean-all >/dev/null # nuke lingering .venv/ installs
Joey Armstrong0003f1a2023-10-18 16:40:23 -040069
Joey Armstronga2db6cd2023-11-30 12:16:18 -050070 declare -a targs=()
71 targs+=('--create')
72
73 ## Set archive compression level
74 local compress='bzip2'
75 local ext
76 case "$compress" in
77 bzip2) targs+=('--bzip2'); ext='bz2' ;;
78 gzip) targs+=('--gzip'); ext='tgz' ;;
79 zstd) targs+=('--zstd'); ext='zst' ;;
80 *) error "Detected invalid compression [$compress]" ;;
81 esac
82
83 local out="${prefix}/${ts}.${ext}"
84
85 targs+=('--file' "$out")
86
87 mkdir -p "$prefix"
88 tar "${targs[@]}" '.'
89 /bin/ls -l "$out"
90 return
91}
92
93## -----------------------------------------------------------------------
94## Intent: Install feature enabling makefile.
95## -----------------------------------------------------------------------
96function install_config_mk
97{
98 local dst='makefiles/config.mk'
99 if [[ -f "$dst" ]]; then
100 :
101 elif [[ -f 'config.mk' ]]; then
102 git mv config.mk "$dst"
103 else
104 rsync -v --checksum makefiles/onf-lib/config.mk "$dst"
105 fi
106
107 return
108}
109
110## -----------------------------------------------------------------------
111## Intent: Re-home existing local makefiles/ into makefiles/local
112## -----------------------------------------------------------------------
113function patch_detection()
114{
115 [[ ! -d makefiles ]] && return
116
117 ## Migration patches should be simple and plentiful.
118 if [[ ! -d makefiles-orig ]]; then
119 cat <<EOM
Joey Armstrong0003f1a2023-10-18 16:40:23 -0400120
121* -----------------------------------------------------------------------
122* Replacing a repository makefile directory is deployed
123* by creating a few independent patches.
124* -----------------------------------------------------------------------
125 1) Rename the repository-specific makefiles directory.
126 1a) git mv makefiles makefiles-orig
127 1b) Update Makefile to "include makefiles-orig"
128
129 2) Create makefiles/
130 2a) add repo:onf-make as a submodule.
131 2b) create makefiles/local/
132 2c) relocate config files
133
134 3) Update Makefile to include makefiles/include.mk
135
136EOM
Joey Armstronga2db6cd2023-11-30 12:16:18 -0500137 exit 1
138 fi
139 return
140}
141
142##----------------##
143##---] MAIN [---##
144##----------------##
145while [[ $# -gt 0 ]]; do
146 arg=$1; shift
147 case "$arg" in
148 debug) declare -g -i debug=1 ;;
149 *) error "Detected invalid switch [$arg]" ;;
150 esac
151done
152
153## Avoid trashing a work-in-progress
154path='makefiles/local/include.mk'
155[[ -e "$path" ]] && { error "Detected upgrade path: $path"; }
156
157archive_sandbox
158patch_detection
Joey Armstrongfedf45e2023-09-20 11:40:01 -0400159
Joey Armstrongf128de82023-09-08 17:05:18 -0400160mkdir -p makefiles
Joey Armstrong0003f1a2023-10-18 16:40:23 -0400161pushd makefiles || { error 'pushd makefiles failed'; }
Joey Armstrongf128de82023-09-08 17:05:18 -0400162
Joey Armstrong0003f1a2023-10-18 16:40:23 -0400163banner 'Adding repo:onf-make (library makefiles) as a submodule'
Joey Armstrongf128de82023-09-08 17:05:18 -0400164git submodule add 'https://github.com/opencord/onf-make.git' onf-lib
165
Joey Armstrong0003f1a2023-10-18 16:40:23 -0400166banner 'Install library/local loader include.mk'
167rsync -v --checksum onf-lib/makefiles_include_mk.ex include.mk
Joey Armstrongf128de82023-09-08 17:05:18 -0400168
Joey Armstrong0003f1a2023-10-18 16:40:23 -0400169banner 'Create project specific directory makefiles/local'
Joey Armstrongf128de82023-09-08 17:05:18 -0400170mkdir -p local
171touch local/include.mk
Joey Armstrongf128de82023-09-08 17:05:18 -0400172
Joey Armstrong0003f1a2023-10-18 16:40:23 -0400173popd || { error 'popd makefiles failed'; }
Joey Armstrongf128de82023-09-08 17:05:18 -0400174
Joey Armstrong0003f1a2023-10-18 16:40:23 -0400175banner 'Prep work for pending checkin'
176git add makefiles/include.mk
177git add makefiles/local/include.mk
Joey Armstronga2db6cd2023-11-30 12:16:18 -0500178install_config_mk
Joey Armstrong0003f1a2023-10-18 16:40:23 -0400179git add makefiles
Joey Armstronga2db6cd2023-11-30 12:16:18 -0500180git status
Joey Armstrongfedf45e2023-09-20 11:40:01 -0400181
Joey Armstrongf128de82023-09-08 17:05:18 -0400182# [EOF]