blob: c3ee13e5f7541c98aacd628f6d42414c4e69ccff [file] [log] [blame]
Caveh Jalalife95f832023-05-01 21:07:54 -07001#!/usr/bin/env bash
2#
3# SPDX-License-Identifier: GPL-2.0-only
4
5#
6# Imports ec_commands.h and ec_cmd_api.h from the cros EC repo
7# and updates the copyright header for coreboot.
8#
9
10set -u
11
12scratch_file=''
13coreboot_top=''
14pname=''
15
16cleanup()
17{
18 if [[ -n "${scratch_file}" ]]; then
19 rm -f -- "${scratch_file}"
20 scratch_file=''
21 fi
22}
23
24trap cleanup EXIT
25
26usage()
27{
28 cat <<- __EOF
29 Usage: ${pname}: [top of cros EC repo]
30
31 Imports ec_commands.h and ec_cmd_api.h from the cros EC repo
32 and updates the copyright header for coreboot.
33 __EOF
34}
35
36#
37# Remove the original ChromiumOS copyright so we can insert the SPDX
38# license identifier. Preserve the original number of lines in the file
39# so embedded #line directives are correct.
40#
41
42update_copyright()
43{
44 local spdx='/* SPDX-License-Identifier: BSD-3-Clause */'
45 local f=$1
46
47 # replace existing copyright with empty lines
48 sed -i -e '/Copyright.*Chromium/,/^$/{s/^.*$//}' "${f}"
49 # now add the SPDX header
50 sed -i -e "1s@^\$@${spdx}@" "${f}"
51}
52
53get_file()
54{
55 local ec_path="$1"
56 local ec_file="$2"
57 local dst_path="$3"
58 local log
59
60 if [[ ! -f "${ec_path}/${ec_file}" ]]; then
61 echo "${ec_path}/${ec_file} does not exist" 2>&1
62 return 1
63 fi
64
65 scratch_file=$(mktemp)
66
67 cp "${ec_path}/${ec_file}" "${scratch_file}"
68
69 update_copyright "${scratch_file}"
70 cp "${scratch_file}" "${dst_path}"
71
72 rm -f "${scratch_file}"
73 scratch_file=''
74
75 log=$(git -C "${ec_path}" log --oneline -1 "${ec_file}")
76 echo "The original ${ec_file} version in the EC repo is:"
77 echo " ${log}"
78}
79
80main()
81{
82 local dst_path
83 local ec_path
84
85 pname=$(basename "$0")
86
87 if [[ $# != 1 ]]; then
88 usage
89 exit 1
90 fi
91
92 ec_path="$1"
93 if [[ ! -d "${ec_path}" ]]; then
94 echo "${pname}: could not find ${ec_path}" 1>&2
95 exit 1
96 fi
97
98 coreboot_top=$(git rev-parse --show-toplevel)
99 if [[ ! -d "${coreboot_top}" ]]; then
100 echo "${pname}: could not determine coreboot top" 1>&2
101 exit 1
102 fi
103
104 dst_path="${coreboot_top}/src/ec/google/chromeec"
105
106 cat <<- __EOF
107 Suggested commit message:
108
109 Generated using ${pname} [EC-DIR].
110
111 __EOF
112
113 get_file "${ec_path}" include/ec_commands.h "${dst_path}/ec_commands.h"
114 get_file "${ec_path}" include/ec_cmd_api.h "${dst_path}/ec_cmd_api.h"
115 echo
116}
117
118main "$@"