-
Notifications
You must be signed in to change notification settings - Fork 2
/
propagate
executable file
·83 lines (72 loc) · 2.11 KB
/
propagate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
#
# Propagate include files down the inheritance tree
#
set -e -u -o pipefail -o noclobber
shopt -s nullglob
# Parent dir link regex
PARENT_DIR_LINK_RE='^\.\./\.\./[a-z0-9_]+$'
# .h (C) include file format string
INCLUDE_FORMAT_H="\
#include <%s>
"
# .mk (make) include file format string
INCLUDE_FORMAT_MK="\
include %s
"
# .S.inc (Assembly) include file format string
INCLUDE_FORMAT_S_INC="\
.include \"%s\"
"
# An include file glob -> format string dictionary
declare -r -A GLOB_FORMATS=(
["*.h"]="$INCLUDE_FORMAT_H"
["*.mk"]="$INCLUDE_FORMAT_MK"
["*.S.inc"]="$INCLUDE_FORMAT_S_INC"
)
# A dir -> child dirs dictionary
declare -A DIR_CHILDREN=()
# A dir -> parent dirs dictionary
declare -A DIR_PARENTS=()
# Collect parent -> child and child->parent links
for DIR_PARENT_DIR_LINK in */.parents/*; do
DIR="${DIR_PARENT_DIR_LINK%/.parents/*}"
PARENT_DIR_LINK=`readlink "$DIR_PARENT_DIR_LINK"`
if [[ $PARENT_DIR_LINK =~ $PARENT_DIR_LINK_RE ]]; then
PARENT_DIR="${PARENT_DIR_LINK#../../}"
DIR_CHILDREN[$PARENT_DIR]+="${DIR_CHILDREN[$PARENT_DIR]:+ }$DIR"
DIR_PARENTS[$DIR]+="${DIR_PARENTS[$DIR]:+ }$PARENT_DIR"
else
echo "Invalid link $DIR_PARENT_DIR_LINK: $PARENT_DIR_LINK" >&2
echo "Expecting the link to match $PARENT_DIR_LINK_RE" >&2
exit 1
fi
done
# Propagate directory files into its children
# Args: dir
function propagate()
{
local -r dir="$1"
local child_dir
local glob
local dir_file
local child_file
for child_dir in ${DIR_CHILDREN[$dir]:-}; do
for glob in "${!GLOB_FORMATS[@]}"; do
for dir_file in "$dir/"$glob; do
child_file="$child_dir${dir_file#$dir}"
if [ ! -e "$child_file" ]; then
printf "${GLOB_FORMATS[$glob]}" "../$dir_file" \
> "$child_file"
fi
done
done
propagate "$child_dir"
done
}
# For each directory with children and without parents
for DIR in "${!DIR_CHILDREN[@]}"; do
if [ -z "${DIR_PARENTS[$DIR]:-}" ]; then
propagate "$DIR"
fi
done