summaryrefslogtreecommitdiffstats
path: root/util/scripts/rm_unused_code
blob: d4b42b62971d0e841fe65e15d8eca477696cf1e6 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/bash
set -e

VERSION="1.00"

PROGRAM=$0
PROGNAME="$(basename "${PROGRAM}")"

MODIFIED_FILES=()
CLEAN_DIR_LIST=(configs Documentation payloads spd src util)
KEEP_FILES=(util/kconfig/)
REQUIRED_MAKEFILES="util/testing\|util/crossgcc\|payloads/coreinfo\|payloads/nvramcui\|payloads/libpayload\|payloads/external/tint\|util/amdfwtool\|util/ectool\|util/futility\|util/intelmetool\|util/inteltool\|util/intelvbttool\|til/post\|util/superiotool"
VERBOSE=

# Text STYLE variables
BOLD="\033[1m"
RED='\033[38;5;9m'
GREEN='\033[38;5;2m'
NO_COLOR='\033[0m'

################################################################################

usage() {
cat << EOF
The ${PROGNAME} script is used to create a git patch that removes all files
not used in a single build.  It does this by creating a temporary directory
and configuring it to show the last time a file was accessed.  It then sets
the time on all files back to midnight on 2021-01-01 and then does a full
build. Because all Kconfig and Makefiles are accessed during the built,
it then creates a new Kconfig file containing all of the old Kconfigs.
The next step is to delete all of the files that have an access time still
set to 2021.  The final step of the cleaning process is to recursively remove
any Makefile that is alone in a directory by itself. The script then makes
a commit and creates a patch.

  Usage: ${PROGNAME} [options]

Options:
 -b | --blddir <dir>   Set /tmp/<dir> as the build directory
 -h | --help            Print usage and exit
 -D | --debug           Print debug information.  Use -DD to show all commands
 -V | --version         Print the version and exit
      --nocolor         Don't print color codes
EOF
}

_echo_color() {
  local color="$1"
  local text="$2"
  local newline="$3"
  if [[ ${newline} == "0" ]]; then
    printf "${color}%s${NO_COLOR}" "${text}"
  else
    printf "${color}%s${NO_COLOR}\n" "${text}"
  fi
}

_echo_error() {
  _echo_color "${RED}" "$*" 1 >&2
}

show_version() {
  echo
  _echo_color "${BOLD}${GREEN}" "${PROGNAME} version ${VERSION}"
  echo
}

get_args() {
  if ! args="$(getopt -l version,help,debug,nocolor,blddir: -o b:DhV -- "$@")"; then
    usage
    exit 1
  fi

  eval set -- "${args}"

  while true; do
    case "$1" in
    -b | --blddir)
      shift
      BLD_DIR="/tmp/$1"
      ;;
    -D | --debug)
      # -d prints extra debug info
      # -dd prints all script steps
      if [ -n "${VERBOSE}" ]; then
        set -x
      else
        VERBOSE="V=1"
      fi
      ;;
    -h | --help)
      usage
      exit 0
      ;;
    --nocolor)
      BOLD=""
      RED=""
      GREEN=""
      NO_COLOR=""
      ;;
    -V | --version) exit 0 ;;
    --)
      shift
      break
      ;;
    *)
      _echo_error "Unknown argument '$1'"
      usage
      exit 1
      ;;
    esac
    shift
  done

  if [[ -n $1 ]]; then
    _echo_error "Unknown command '$1'"
    usage
    exit 1
  fi

  BLD_DIR="${BLD_DIR:-$(mktemp -d)}"
}

recursively_rm_dir_onlyfile() {
  local dir=$1
  local beforecount
  local aftercount

  while true; do
    if [[ ! -d ${dir} ]]; then
      break
    fi
    beforecount="$(find "${dir}" | wc -l)"
    while read -r file; do
	# Don't delete any of the makefiles required for building.
      if echo "${file}" | grep -q "${REQUIRED_MAKEFILES}"; then
        break
      fi
      # Remove the directory if a makefile is the only file present.
      if [[ "$(cd "${file}" && find . -maxdepth 1 | grep -v "./Makefile")" == "." ]]; then
        rm -rf "${file}"
      fi
    done < <(find "${dir}" -depth -type d)
    if [[ ! -d ${dir} ]]; then
      break
    fi
    find "${dir}" -type d -empty -delete
    if [[ ! -d ${dir} ]]; then
      break
    fi
    aftercount="$(find "${dir}" | wc -l)"
    if [[ ${aftercount} -eq ${beforecount} ]]; then
      break
    fi
  done
}

verify_atime_enabled() {
  local testfile
  # Make sure the build directory is mounted correctly
  if [ ! -d "${BLD_DIR}" ]; then
    mkdir "${BLD_DIR}"
  fi
  if ! grep -q "${BLD_DIR}" /proc/mounts; then
    echo "Mounting the ${BLD_DIR} directory with atime enabled"
    sudo mount -t tmpfs -o rw,relatime tmpfs "${BLD_DIR}"
  elif ! grep "${BLD_DIR}" /proc/mounts | grep -q relatime; then
    echo "Remounting the ${BLD_DIR} directory with relatime enabled"
    sudo mount -o remount,relatime "${BLD_DIR}"
  fi

  testfile="$(mktemp -p "${BLD_DIR}")"
  touch -a --date="2020-01-01 00:00:00" "${testfile}"
  if ! stat "${testfile}" | grep -q "Access: 2020-01-01"; then
    _echo_error "Error: could not set access time."
    sudo umount "${BLD_DIR}"
    rm -rf "${BLD_DIR}"
    exit 1
  fi
  rm -f "${testfile}"
}

update_codebase() {
  local tempconfig
  tempconfig="$(mktemp)"
  if [ ! -f "${BLD_DIR}/COPYING" ]; then
    echo "Downloading coreboot tree"
    git clone https://review.coreboot.org/coreboot.git "${BLD_DIR}"
    make -C "${BLD_DIR}" build/xcompile
  fi

  # Start from a completely clean tree or we'll miss anything that
  # doesn't need to be rebuilt.  Save the config if it exists.
  if [[ -f .config ]]; then
    mv .config "${tempconfig}"
  fi
  _echo_color "${GREEN}" "Cleaning coreboot tree"
  make -s -C "${BLD_DIR}" distclean
  if [[ -f ${tempconfig} ]]; then
    mv "${tempconfig}" .config
  fi

  # force a refresh of all submodules
  _echo_color "${GREEN}" "Refreshing all submodules..."
  git submodule update --recursive --remote --init --checkout
}

save_kconfig() (
  cd "${BLD_DIR}" && util/lint/kconfig_lint -w -p -o kconfig.tmp
)

update_times() {
  _echo_color "${GREEN}" "Updating access time of all files"
  git ls-files | xargs touch -a -m -t 202001010000
  if ! stat "${BLD_DIR}/COPYING" | grep -q "Access: 2020-01-01"; then
    _echo_error "Error: could not set access time."
    _echo_error "       One of the following processes may be accessing it."
    fuser -uvm "${BLD_DIR}/COPYING"
    exit 1
  fi
}

mark_files_to_keep() {
  for file in "${KEEP_FILES[@]}"; do
    find "${BLD_DIR}/${file}" -depth -exec touch {} \;
  done
}

build_platform() {
  local extra_text="$1"
  _echo_color "${GREEN}" "Building platform ${extra_text}"
  if [[ ! -f "${BLD_DIR}/.config" ]]; then
    if [[ -n ${CONFIG_FILE} ]]; then
      cp "${CONFIG_FILE}" "${BLD_DIR}/.config"
    fi
    echo "CONFIG_PAYLOAD_NONE=y" >>"${BLD_DIR}/.config"
  fi

  make -C "${BLD_DIR}" -s clean UPDATED_SUBMODULES=1 BUILD_TIMELESS=1
  make -C "${BLD_DIR}" -s olddefconfig
  make -C "${BLD_DIR}" -s UPDATED_SUBMODULES=1 BUILD_TIMELESS=1 ${VERBOSE}
  HASH="$(sha256sum build/coreboot.rom)"
  make -C "${BLD_DIR}" -s clean UPDATED_SUBMODULES=1 BUILD_TIMELESS=1
}

show_modified() {
  readarray MODIFIED_FILES < <(find "${BLD_DIR}" -atime -1 -type f -path ./.git -prune)
  echo "Files changed: ${#MODIFIED_FILES[@]}"
}

remove_kconfigs() {
  # Dump all Kconfigs into a single file so that directories
  # can be removed, while maintaining the entire Kconfig
  # structure.
  find "${BLD_DIR}/src" -name 'Kconfig*' -delete
  mv "${BLD_DIR}/kconfig.tmp" "${BLD_DIR}/src/Kconfig"
}

remove_unused() {
  local dir
  # Most files can be removed simply by looking at the time, but
  # all Kconfig and Makefiles in the entire tree are accessed
  # whether they're used or not.
  remove_kconfigs

  echo
  _echo_color "${GREEN}" "Checking access time and removing unused files in:"
  for dir in "${CLEAN_DIR_LIST[@]}"; do
    printf "%s\n" "${BLD_DIR}/${dir}"
    # find and remove all files without updated times.
    find "${BLD_DIR}/${dir}" -atime +5 -type f -delete

    recursively_rm_dir_onlyfile "${BLD_DIR}/${dir}"
  done
  printf "\n\n"
}

create_patch() {
  _echo_color "${GREEN}" "Creating patch"
  (
    cd "${BLD_DIR}"
    git add -A
    git commit -m "remove unused files" --no-verify
    git format-patch HEAD^
  )
}

main() {
  show_version
  get_args "$@"

  verify_atime_enabled
  update_codebase
  save_kconfig
  update_times
  mark_files_to_keep
  build_platform "to mark used files"
  OLDHASH="${HASH}"
  HASH=""
  #show_modified
  remove_unused
  create_patch
  build_platform "to verify the build still works"
  NEWHASH="${HASH}"

  echo
  _echo_color "${GREEN}" "Checksums:"
  echo "Old: ${OLDHASH}"
  echo "New: ${NEWHASH}"
}

main "$@"