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