Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 1 | #!/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 Ferris | 764049d | 2018-05-24 14:38:10 -0700 | [diff] [blame] | 27 | ### --download-kernel <VERSION> |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 28 | ### Automatically create a temporary git repository and check out the |
Christopher Ferris | 764049d | 2018-05-24 14:38:10 -0700 | [diff] [blame] | 29 | ### linux kernel source code for the given version. |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 30 | ### --use-kernel-dir <DIR> |
| 31 | ### Do not check out the kernel source, use the kernel directory |
| 32 | ### pointed to by <DIR>. |
Christopher Ferris | 5956b4e | 2016-07-20 12:28:56 -0700 | [diff] [blame] | 33 | ### --verify-modified-headers-only <DIR> |
| 34 | ### Do not build anything, simply verify that the set of modified |
| 35 | ### kernel headers have not changed. |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 36 | |
| 37 | # Terminate the script if any command fails. |
| 38 | set -eE |
| 39 | |
| 40 | TMPDIR="" |
| 41 | ANDROID_DIR="" |
Christopher Ferris | 764049d | 2018-05-24 14:38:10 -0700 | [diff] [blame] | 42 | KERNEL_VERSION="" |
| 43 | KERNEL_REPO="git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git" |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 44 | KERNEL_DIR="" |
| 45 | KERNEL_DOWNLOAD=0 |
| 46 | ARCH_LIST=("arm" "arm64" "mips" "x86") |
| 47 | ANDROID_KERNEL_DIR="external/kernel-headers/original" |
| 48 | SKIP_GENERATION=0 |
Christopher Ferris | 5956b4e | 2016-07-20 12:28:56 -0700 | [diff] [blame] | 49 | VERIFY_HEADERS_ONLY=0 |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 50 | |
| 51 | function cleanup () { |
| 52 | if [[ "${TMPDIR}" =~ /tmp ]] && [[ -d "${TMPDIR}" ]]; then |
| 53 | echo "Removing temporary directory ${TMPDIR}" |
| 54 | rm -rf "${TMPDIR}" |
| 55 | TMPDIR="" |
| 56 | fi |
| 57 | } |
| 58 | |
| 59 | function usage () { |
| 60 | grep '^###' $0 | sed -e 's/^###//' |
| 61 | } |
| 62 | |
| 63 | function 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 Ferris | b4091e6 | 2013-11-22 17:04:34 -0800 | [diff] [blame] | 88 | function 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 Ferris | 5956b4e | 2016-07-20 12:28:56 -0700 | [diff] [blame] | 107 | function verify_modified_hdrs () { |
Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 108 | 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 Ferris | 5956b4e | 2016-07-20 12:28:56 -0700 | [diff] [blame] | 132 | verify_modified_hdrs "${dir}" ${tgt_dir}/$(basename ${dir}) "${kernel_dir}" |
Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 133 | done |
| 134 | } |
| 135 | |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 136 | trap cleanup EXIT |
| 137 | # This automatically triggers a call to cleanup. |
| 138 | trap "exit 1" HUP INT TERM TSTP |
| 139 | |
| 140 | while [ $# -gt 0 ]; do |
| 141 | case "$1" in |
| 142 | "--skip-generation") |
| 143 | SKIP_GENERATION=1 |
| 144 | ;; |
| 145 | "--download-kernel") |
Christopher Ferris | 764049d | 2018-05-24 14:38:10 -0700 | [diff] [blame] | 146 | if [[ $# -lt 2 ]]; then |
| 147 | echo "--download-kernel requires an argument." |
| 148 | exit 1 |
| 149 | fi |
| 150 | shift |
| 151 | KERNEL_VERSION="$1" |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 152 | 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 Ferris | 5956b4e | 2016-07-20 12:28:56 -0700 | [diff] [blame] | 163 | "--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 Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 173 | "-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 |
| 189 | done |
| 190 | |
| 191 | ANDROID_KERNEL_DIR="${ANDROID_BUILD_TOP}/${ANDROID_KERNEL_DIR}" |
| 192 | if [[ "${ANDROID_BUILD_TOP}" == "" ]]; then |
| 193 | echo "ANDROID_BUILD_TOP is not set, did you run lunch?" |
| 194 | exit 1 |
| 195 | elif [[ ! -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 |
| 199 | fi |
| 200 | |
Christopher Ferris | 764049d | 2018-05-24 14:38:10 -0700 | [diff] [blame] | 201 | src_dir="linux-stable" |
Christopher Ferris | 5956b4e | 2016-07-20 12:28:56 -0700 | [diff] [blame] | 202 | |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 203 | if [[ ${KERNEL_DOWNLOAD} -eq 1 ]]; then |
| 204 | TMPDIR=$(mktemp -d /tmp/android_kernelXXXXXXXX) |
| 205 | cd "${TMPDIR}" |
Christopher Ferris | 764049d | 2018-05-24 14:38:10 -0700 | [diff] [blame] | 206 | echo "Fetching linux kernel source ${KERNEL_VERSION}" |
| 207 | git clone ${KERNEL_REPO} |
| 208 | cd ${src_dir} |
| 209 | git checkout tags/"${KERNEL_VERSION}" |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 210 | KERNEL_DIR="${TMPDIR}" |
| 211 | elif [[ "${KERNEL_DIR}" == "" ]]; then |
| 212 | echo "Must specify one of --use-kernel-dir or --download-kernel." |
| 213 | exit 1 |
Christopher Ferris | 38062f9 | 2014-07-09 15:33:25 -0700 | [diff] [blame] | 214 | elif [[ ! -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 Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 216 | exit 1 |
| 217 | else |
Christopher Ferris | 38062f9 | 2014-07-09 15:33:25 -0700 | [diff] [blame] | 218 | cd "${KERNEL_DIR}/${src_dir}" |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 219 | fi |
| 220 | |
Christopher Ferris | 764049d | 2018-05-24 14:38:10 -0700 | [diff] [blame] | 221 | if [[ ${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 |
| 227 | fi |
| 228 | |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 229 | if [[ ${SKIP_GENERATION} -eq 0 ]]; then |
Christopher Ferris | 5956b4e | 2016-07-20 12:28:56 -0700 | [diff] [blame] | 230 | # Clean up any leftover headers. |
| 231 | make distclean |
| 232 | |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 233 | # 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 |
| 238 | fi |
| 239 | |
Christopher Ferris | 5956b4e | 2016-07-20 12:28:56 -0700 | [diff] [blame] | 240 | # Completely delete the old original headers so that any deleted/moved |
| 241 | # headers are also removed. |
| 242 | rm -rf "${ANDROID_KERNEL_DIR}/uapi" |
| 243 | mkdir -p "${ANDROID_KERNEL_DIR}/uapi" |
| 244 | |
Kyunam Jo | fa343e6 | 2014-12-10 15:06:46 +0900 | [diff] [blame] | 245 | cd ${ANDROID_BUILD_TOP} |
| 246 | |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 247 | # Copy all of the include/uapi files to the kernel headers uapi directory. |
Christopher Ferris | 38062f9 | 2014-07-09 15:33:25 -0700 | [diff] [blame] | 248 | copy_hdrs "${KERNEL_DIR}/${src_dir}/include/uapi" "${ANDROID_KERNEL_DIR}/uapi" |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 249 | |
Christopher Ferris | b4091e6 | 2013-11-22 17:04:34 -0800 | [diff] [blame] | 250 | # Copy the staging files to uapi/linux. |
Christopher Ferris | 38062f9 | 2014-07-09 15:33:25 -0700 | [diff] [blame] | 251 | copy_hdrs "${KERNEL_DIR}/${src_dir}/drivers/staging/android/uapi" \ |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 252 | "${ANDROID_KERNEL_DIR}/uapi/linux" "no-copy-dirs" |
| 253 | |
Christopher Ferris | 5ed15ba | 2018-04-24 12:29:18 -0700 | [diff] [blame] | 254 | # Remove ion.h, it's not fully supported by the upstream kernel (see b/77976082). |
Christopher Ferris | 764049d | 2018-05-24 14:38:10 -0700 | [diff] [blame] | 255 | rm -f "${ANDROID_KERNEL_DIR}/uapi/linux/ion.h" |
Christopher Ferris | 5ed15ba | 2018-04-24 12:29:18 -0700 | [diff] [blame] | 256 | |
Christopher Ferris | b4091e6 | 2013-11-22 17:04:34 -0800 | [diff] [blame] | 257 | # Copy the generated headers. |
Christopher Ferris | 38062f9 | 2014-07-09 15:33:25 -0700 | [diff] [blame] | 258 | copy_hdrs "${KERNEL_DIR}/${src_dir}/include/generated/uapi" \ |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 259 | "${ANDROID_KERNEL_DIR}/uapi" |
| 260 | |
| 261 | for arch in "${ARCH_LIST[@]}"; do |
Christopher Ferris | b4091e6 | 2013-11-22 17:04:34 -0800 | [diff] [blame] | 262 | # Copy arch headers. |
Christopher Ferris | 38062f9 | 2014-07-09 15:33:25 -0700 | [diff] [blame] | 263 | copy_hdrs "${KERNEL_DIR}/${src_dir}/arch/${arch}/include/uapi" \ |
Colin Cross | d1973ca | 2014-01-21 19:50:58 -0800 | [diff] [blame] | 264 | "${ANDROID_KERNEL_DIR}/uapi/asm-${arch}" |
Christopher Ferris | b4091e6 | 2013-11-22 17:04:34 -0800 | [diff] [blame] | 265 | # Copy the generated arch headers. |
Christopher Ferris | 38062f9 | 2014-07-09 15:33:25 -0700 | [diff] [blame] | 266 | copy_hdrs "${KERNEL_DIR}/${src_dir}/arch/${arch}/include/generated/uapi" \ |
Colin Cross | d1973ca | 2014-01-21 19:50:58 -0800 | [diff] [blame] | 267 | "${ANDROID_KERNEL_DIR}/uapi/asm-${arch}" |
Christopher Ferris | b4091e6 | 2013-11-22 17:04:34 -0800 | [diff] [blame] | 268 | |
| 269 | # Special copy of generated header files from arch/<ARCH>/generated/asm that |
| 270 | # also exist in uapi/asm-generic. |
Christopher Ferris | 38062f9 | 2014-07-09 15:33:25 -0700 | [diff] [blame] | 271 | copy_if_exists "${KERNEL_DIR}/${src_dir}/include/uapi/asm-generic" \ |
| 272 | "${KERNEL_DIR}/${src_dir}/arch/${arch}/include/generated/asm" \ |
Colin Cross | d1973ca | 2014-01-21 19:50:58 -0800 | [diff] [blame] | 273 | "${ANDROID_KERNEL_DIR}/uapi/asm-${arch}/asm" |
Christopher Ferris | e994d41 | 2013-11-20 19:52:56 -0800 | [diff] [blame] | 274 | done |
Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 275 | |
Christopher Ferris | 5956b4e | 2016-07-20 12:28:56 -0700 | [diff] [blame] | 276 | # The arm types.h uapi header is not properly being generated, so copy it |
| 277 | # directly. |
| 278 | cp "${KERNEL_DIR}/${src_dir}/include/uapi/asm-generic/types.h" \ |
| 279 | "${ANDROID_KERNEL_DIR}/uapi/asm-arm/asm" |
| 280 | |
Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 281 | # Verify if modified headers have changed. |
Christopher Ferris | 5956b4e | 2016-07-20 12:28:56 -0700 | [diff] [blame] | 282 | verify_modified_hdrs "${KERNEL_DIR}/${src_dir}/include/scsi" \ |
| 283 | "${ANDROID_KERNEL_DIR}/scsi" \ |
| 284 | "${KERNEL_DIR}/${src_dir}" |
Christopher Ferris | 764049d | 2018-05-24 14:38:10 -0700 | [diff] [blame] | 285 | echo "Headers updated." |