blob: 5bf1cf326544ee79ff6d56e058386d238fd796d5 [file] [log] [blame]
Christopher Ferrise994d412013-11-20 19:52:56 -08001#!/bin/bash
2#
3# Copyright (C) 2013 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17### Usage: generate_uapi_headers.sh [<options>]
18###
19### This script is used to get a copy of the uapi kernel headers
20### from an android kernel tree and copies them into an android source
21### tree without any processing. The script also creates all of the
22### generated headers and copies them into the android source tree.
23###
24### Options:
25### --skip-generation
26### Skip the step that generates all of the include files.
Christopher Ferris764049d2018-05-24 14:38:10 -070027### --download-kernel <VERSION>
Christopher Ferrise994d412013-11-20 19:52:56 -080028### Automatically create a temporary git repository and check out the
Christopher Ferris764049d2018-05-24 14:38:10 -070029### linux kernel source code for the given version.
Christopher Ferrise994d412013-11-20 19:52:56 -080030### --use-kernel-dir <DIR>
31### Do not check out the kernel source, use the kernel directory
32### pointed to by <DIR>.
Christopher Ferris5956b4e2016-07-20 12:28:56 -070033### --verify-modified-headers-only <DIR>
34### Do not build anything, simply verify that the set of modified
35### kernel headers have not changed.
Christopher Ferrise994d412013-11-20 19:52:56 -080036
37# Terminate the script if any command fails.
38set -eE
39
40TMPDIR=""
41ANDROID_DIR=""
Christopher Ferris764049d2018-05-24 14:38:10 -070042KERNEL_VERSION=""
43KERNEL_REPO="git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git"
Christopher Ferrise994d412013-11-20 19:52:56 -080044KERNEL_DIR=""
45KERNEL_DOWNLOAD=0
46ARCH_LIST=("arm" "arm64" "mips" "x86")
47ANDROID_KERNEL_DIR="external/kernel-headers/original"
48SKIP_GENERATION=0
Christopher Ferris5956b4e2016-07-20 12:28:56 -070049VERIFY_HEADERS_ONLY=0
Christopher Ferrise994d412013-11-20 19:52:56 -080050
51function cleanup () {
52 if [[ "${TMPDIR}" =~ /tmp ]] && [[ -d "${TMPDIR}" ]]; then
53 echo "Removing temporary directory ${TMPDIR}"
54 rm -rf "${TMPDIR}"
55 TMPDIR=""
56 fi
57}
58
59function usage () {
60 grep '^###' $0 | sed -e 's/^###//'
61}
62
63function copy_hdrs () {
64 local src_dir=$1
65 local tgt_dir=$2
66 local dont_copy_dirs=$3
67
68 mkdir -p ${tgt_dir}
69
70 local search_dirs=()
71
72 # This only works if none of the filenames have spaces.
73 for file in $(ls -d ${src_dir}/* 2> /dev/null); do
74 if [[ -d "${file}" ]]; then
75 search_dirs+=("${file}")
76 elif [[ -f "${file}" ]] && [[ "${file}" =~ .h$ ]]; then
77 cp ${file} ${tgt_dir}
78 fi
79 done
80
81 if [[ "${dont_copy_dirs}" == "" ]]; then
82 for dir in "${search_dirs[@]}"; do
83 copy_hdrs "${dir}" ${tgt_dir}/$(basename ${dir})
84 done
85 fi
86}
87
Christopher Ferrisb4091e62013-11-22 17:04:34 -080088function copy_if_exists () {
89 local check_dir=$1
90 local src_dir=$2
91 local tgt_dir=$3
92
93 mkdir -p ${tgt_dir}
94
95 # This only works if none of the filenames have spaces.
96 for file in $(ls -d ${src_dir}/* 2> /dev/null); do
97 if [[ -f "${file}" ]] && [[ "${file}" =~ .h$ ]]; then
98 # Check that this file exists in check_dir.
99 header=$(basename ${file})
100 if [[ -f "${check_dir}/${header}" ]]; then
101 cp ${file} ${tgt_dir}
102 fi
103 fi
104 done
105}
106
Christopher Ferris5956b4e2016-07-20 12:28:56 -0700107function verify_modified_hdrs () {
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700108 local src_dir=$1
109 local tgt_dir=$2
110 local kernel_dir=$3
111
112 local search_dirs=()
113
114 # This only works if none of the filenames have spaces.
115 for file in $(ls -d ${src_dir}/* 2> /dev/null); do
116 if [[ -d "${file}" ]]; then
117 search_dirs+=("${file}")
118 elif [[ -f "${file}" ]] && [[ "${file}" =~ .h$ ]]; then
119 tgt_file=${tgt_dir}/$(basename ${file})
120 if [[ -e ${tgt_file} ]] && ! diff "${file}" "${tgt_file}" > /dev/null; then
121 if [[ ${file} =~ ${kernel_dir}/*(.+) ]]; then
122 echo "New version of ${BASH_REMATCH[1]} found in kernel headers."
123 else
124 echo "New version of ${file} found in kernel headers."
125 fi
126 echo "This file needs to be updated manually."
127 fi
128 fi
129 done
130
131 for dir in "${search_dirs[@]}"; do
Christopher Ferris5956b4e2016-07-20 12:28:56 -0700132 verify_modified_hdrs "${dir}" ${tgt_dir}/$(basename ${dir}) "${kernel_dir}"
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700133 done
134}
135
Christopher Ferrise994d412013-11-20 19:52:56 -0800136trap cleanup EXIT
137# This automatically triggers a call to cleanup.
138trap "exit 1" HUP INT TERM TSTP
139
140while [ $# -gt 0 ]; do
141 case "$1" in
142 "--skip-generation")
143 SKIP_GENERATION=1
144 ;;
145 "--download-kernel")
Christopher Ferris764049d2018-05-24 14:38:10 -0700146 if [[ $# -lt 2 ]]; then
147 echo "--download-kernel requires an argument."
148 exit 1
149 fi
150 shift
151 KERNEL_VERSION="$1"
Christopher Ferrise994d412013-11-20 19:52:56 -0800152 KERNEL_DOWNLOAD=1
153 ;;
154 "--use-kernel-dir")
155 if [[ $# -lt 2 ]]; then
156 echo "--use-kernel-dir requires an argument."
157 exit 1
158 fi
159 shift
160 KERNEL_DIR="$1"
161 KERNEL_DOWNLOAD=0
162 ;;
Christopher Ferris5956b4e2016-07-20 12:28:56 -0700163 "--verify-modified-headers-only")
164 if [[ $# -lt 2 ]]; then
165 echo "--verify-modified-headers-only requires an argument."
166 exit 1
167 fi
168 shift
169 KERNEL_DIR="$1"
170 KERNEL_DOWNLOAD=0
171 VERIFY_HEADERS_ONLY=1
172 ;;
Christopher Ferrise994d412013-11-20 19:52:56 -0800173 "-h" | "--help")
174 usage
175 exit 1
176 ;;
177 "-"*)
178 echo "Error: Unrecognized option $1"
179 usage
180 exit 1
181 ;;
182 *)
183 echo "Error: Extra arguments on the command-line."
184 usage
185 exit 1
186 ;;
187 esac
188 shift
189done
190
191ANDROID_KERNEL_DIR="${ANDROID_BUILD_TOP}/${ANDROID_KERNEL_DIR}"
192if [[ "${ANDROID_BUILD_TOP}" == "" ]]; then
193 echo "ANDROID_BUILD_TOP is not set, did you run lunch?"
194 exit 1
195elif [[ ! -d "${ANDROID_KERNEL_DIR}" ]]; then
196 echo "${ANDROID_BUILD_TOP} doesn't appear to be the root of an android tree."
197 echo " ${ANDROID_KERNEL_DIR} is not a directory."
198 exit 1
199fi
200
Christopher Ferris764049d2018-05-24 14:38:10 -0700201src_dir="linux-stable"
Christopher Ferris5956b4e2016-07-20 12:28:56 -0700202
Christopher Ferrise994d412013-11-20 19:52:56 -0800203if [[ ${KERNEL_DOWNLOAD} -eq 1 ]]; then
204 TMPDIR=$(mktemp -d /tmp/android_kernelXXXXXXXX)
205 cd "${TMPDIR}"
Christopher Ferris764049d2018-05-24 14:38:10 -0700206 echo "Fetching linux kernel source ${KERNEL_VERSION}"
207 git clone ${KERNEL_REPO}
208 cd ${src_dir}
209 git checkout tags/"${KERNEL_VERSION}"
Christopher Ferrise994d412013-11-20 19:52:56 -0800210 KERNEL_DIR="${TMPDIR}"
211elif [[ "${KERNEL_DIR}" == "" ]]; then
212 echo "Must specify one of --use-kernel-dir or --download-kernel."
213 exit 1
Christopher Ferris38062f92014-07-09 15:33:25 -0700214elif [[ ! -d "${KERNEL_DIR}" ]] || [[ ! -d "${KERNEL_DIR}/${src_dir}" ]]; then
215 echo "The kernel directory $KERNEL_DIR or $KERNEL_DIR/${src_dir} does not exist."
Christopher Ferrise994d412013-11-20 19:52:56 -0800216 exit 1
217else
Christopher Ferris38062f92014-07-09 15:33:25 -0700218 cd "${KERNEL_DIR}/${src_dir}"
Christopher Ferrise994d412013-11-20 19:52:56 -0800219fi
220
Christopher Ferris764049d2018-05-24 14:38:10 -0700221if [[ ${VERIFY_HEADERS_ONLY} -eq 1 ]]; then
222 # Verify if modified headers have changed.
223 verify_modified_hdrs "${KERNEL_DIR}/${src_dir}/include/scsi" \
224 "${ANDROID_KERNEL_DIR}/scsi" \
225 "${KERNEL_DIR}/${src_dir}"
226 exit 0
227fi
228
Christopher Ferrise994d412013-11-20 19:52:56 -0800229if [[ ${SKIP_GENERATION} -eq 0 ]]; then
Christopher Ferris5956b4e2016-07-20 12:28:56 -0700230 # Clean up any leftover headers.
231 make distclean
232
Christopher Ferrise994d412013-11-20 19:52:56 -0800233 # Build all of the generated headers.
234 for arch in "${ARCH_LIST[@]}"; do
235 echo "Generating headers for arch ${arch}"
236 make ARCH=${arch} headers_install
237 done
238fi
239
Christopher Ferris5956b4e2016-07-20 12:28:56 -0700240# Completely delete the old original headers so that any deleted/moved
241# headers are also removed.
242rm -rf "${ANDROID_KERNEL_DIR}/uapi"
243mkdir -p "${ANDROID_KERNEL_DIR}/uapi"
244
Kyunam Jofa343e62014-12-10 15:06:46 +0900245cd ${ANDROID_BUILD_TOP}
246
Christopher Ferrise994d412013-11-20 19:52:56 -0800247# Copy all of the include/uapi files to the kernel headers uapi directory.
Christopher Ferris38062f92014-07-09 15:33:25 -0700248copy_hdrs "${KERNEL_DIR}/${src_dir}/include/uapi" "${ANDROID_KERNEL_DIR}/uapi"
Christopher Ferrise994d412013-11-20 19:52:56 -0800249
Christopher Ferrisb4091e62013-11-22 17:04:34 -0800250# Copy the staging files to uapi/linux.
Christopher Ferris38062f92014-07-09 15:33:25 -0700251copy_hdrs "${KERNEL_DIR}/${src_dir}/drivers/staging/android/uapi" \
Christopher Ferrise994d412013-11-20 19:52:56 -0800252 "${ANDROID_KERNEL_DIR}/uapi/linux" "no-copy-dirs"
253
Christopher Ferris5ed15ba2018-04-24 12:29:18 -0700254# Remove ion.h, it's not fully supported by the upstream kernel (see b/77976082).
Christopher Ferris764049d2018-05-24 14:38:10 -0700255rm -f "${ANDROID_KERNEL_DIR}/uapi/linux/ion.h"
Christopher Ferris5ed15ba2018-04-24 12:29:18 -0700256
Christopher Ferrisb4091e62013-11-22 17:04:34 -0800257# Copy the generated headers.
Christopher Ferris38062f92014-07-09 15:33:25 -0700258copy_hdrs "${KERNEL_DIR}/${src_dir}/include/generated/uapi" \
Christopher Ferrise994d412013-11-20 19:52:56 -0800259 "${ANDROID_KERNEL_DIR}/uapi"
260
261for arch in "${ARCH_LIST[@]}"; do
Christopher Ferrisb4091e62013-11-22 17:04:34 -0800262 # Copy arch headers.
Christopher Ferris38062f92014-07-09 15:33:25 -0700263 copy_hdrs "${KERNEL_DIR}/${src_dir}/arch/${arch}/include/uapi" \
Colin Crossd1973ca2014-01-21 19:50:58 -0800264 "${ANDROID_KERNEL_DIR}/uapi/asm-${arch}"
Christopher Ferrisb4091e62013-11-22 17:04:34 -0800265 # Copy the generated arch headers.
Christopher Ferris38062f92014-07-09 15:33:25 -0700266 copy_hdrs "${KERNEL_DIR}/${src_dir}/arch/${arch}/include/generated/uapi" \
Colin Crossd1973ca2014-01-21 19:50:58 -0800267 "${ANDROID_KERNEL_DIR}/uapi/asm-${arch}"
Christopher Ferrisb4091e62013-11-22 17:04:34 -0800268
269 # Special copy of generated header files from arch/<ARCH>/generated/asm that
270 # also exist in uapi/asm-generic.
Christopher Ferris38062f92014-07-09 15:33:25 -0700271 copy_if_exists "${KERNEL_DIR}/${src_dir}/include/uapi/asm-generic" \
272 "${KERNEL_DIR}/${src_dir}/arch/${arch}/include/generated/asm" \
Colin Crossd1973ca2014-01-21 19:50:58 -0800273 "${ANDROID_KERNEL_DIR}/uapi/asm-${arch}/asm"
Christopher Ferrise994d412013-11-20 19:52:56 -0800274done
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700275
Christopher Ferris5956b4e2016-07-20 12:28:56 -0700276# The arm types.h uapi header is not properly being generated, so copy it
277# directly.
278cp "${KERNEL_DIR}/${src_dir}/include/uapi/asm-generic/types.h" \
279 "${ANDROID_KERNEL_DIR}/uapi/asm-arm/asm"
280
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700281# Verify if modified headers have changed.
Christopher Ferris5956b4e2016-07-20 12:28:56 -0700282verify_modified_hdrs "${KERNEL_DIR}/${src_dir}/include/scsi" \
283 "${ANDROID_KERNEL_DIR}/scsi" \
284 "${KERNEL_DIR}/${src_dir}"
Christopher Ferris764049d2018-05-24 14:38:10 -0700285echo "Headers updated."