lin
2025-08-01 633231e833e21d5b8b1c00cb15aedb62b3b78e8f
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
#!/bin/bash
#
# Copyright (C) 2013 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
 
###  Usage: generate_uapi_headers.sh [<options>]
###
###  This script is used to get a copy of the uapi kernel headers
###  from an android kernel tree and copies them into an android source
###  tree without any processing. The script also creates all of the
###  generated headers and copies them into the android source tree.
###
###  Options:
###   --skip-generation
###     Skip the step that generates all of the include files.
###   --download-kernel <VERSION>
###     Automatically create a temporary git repository and check out the
###     linux kernel source code for the given version.
###   --use-kernel-dir <DIR>
###     Do not check out the kernel source, use the kernel directory
###     pointed to by <DIR>.
###   --verify-modified-headers-only <DIR>
###     Do not build anything, simply verify that the set of modified
###     kernel headers have not changed.
 
# Terminate the script if any command fails.
set -eE
 
TMPDIR=""
ANDROID_DIR=""
KERNEL_VERSION=""
KERNEL_REPO="git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git"
KERNEL_DIR=""
KERNEL_DOWNLOAD=0
ARCH_LIST=("arm" "arm64" "mips" "x86")
ANDROID_KERNEL_DIR="external/kernel-headers/original"
SKIP_GENERATION=0
VERIFY_HEADERS_ONLY=0
 
function cleanup () {
  if [[ "${TMPDIR}" =~ /tmp ]] && [[ -d "${TMPDIR}" ]]; then
    echo "Removing temporary directory ${TMPDIR}"
    rm -rf "${TMPDIR}"
    TMPDIR=""
  fi
}
 
function usage () {
  grep '^###' $0 | sed -e 's/^###//'
}
 
function copy_hdrs () {
  local src_dir=$1
  local tgt_dir=$2
  local dont_copy_dirs=$3
 
  mkdir -p ${tgt_dir}
 
  local search_dirs=()
 
  # This only works if none of the filenames have spaces.
  for file in $(ls -d ${src_dir}/* 2> /dev/null); do
    if [[ -d "${file}" ]]; then
      search_dirs+=("${file}")
    elif [[ -f  "${file}" ]] && [[ "${file}" =~ .h$ ]]; then
      cp ${file} ${tgt_dir}
    fi
  done
 
  if [[ "${dont_copy_dirs}" == "" ]]; then
    for dir in "${search_dirs[@]}"; do
      copy_hdrs "${dir}" ${tgt_dir}/$(basename ${dir})
    done
  fi
}
 
function copy_if_exists () {
  local check_dir=$1
  local src_dir=$2
  local tgt_dir=$3
 
  mkdir -p ${tgt_dir}
 
  # This only works if none of the filenames have spaces.
  for file in $(ls -d ${src_dir}/* 2> /dev/null); do
    if [[ -f  "${file}" ]] && [[ "${file}" =~ .h$ ]]; then
      # Check that this file exists in check_dir.
      header=$(basename ${file})
      if [[ -f "${check_dir}/${header}" ]]; then
        cp ${file} ${tgt_dir}
      fi
    fi
  done
}
 
function verify_modified_hdrs () {
  local src_dir=$1
  local tgt_dir=$2
  local kernel_dir=$3
 
  local search_dirs=()
 
  # This only works if none of the filenames have spaces.
  for file in $(ls -d ${src_dir}/* 2> /dev/null); do
    if [[ -d "${file}" ]]; then
      search_dirs+=("${file}")
    elif [[ -f  "${file}" ]] && [[ "${file}" =~ .h$ ]]; then
      tgt_file=${tgt_dir}/$(basename ${file})
      if [[ -e ${tgt_file} ]] && ! diff "${file}" "${tgt_file}" > /dev/null; then
        if [[ ${file} =~ ${kernel_dir}/*(.+) ]]; then
          echo "New version of ${BASH_REMATCH[1]} found in kernel headers."
        else
          echo "New version of ${file} found in kernel headers."
        fi
        echo "This file needs to be updated manually."
      fi
    fi
  done
 
  for dir in "${search_dirs[@]}"; do
    verify_modified_hdrs "${dir}" ${tgt_dir}/$(basename ${dir}) "${kernel_dir}"
  done
}
 
trap cleanup EXIT
# This automatically triggers a call to cleanup.
trap "exit 1" HUP INT TERM TSTP
 
while [ $# -gt 0 ]; do
  case "$1" in
    "--skip-generation")
      SKIP_GENERATION=1
      ;;
    "--download-kernel")
      if [[ $# -lt 2 ]]; then
        echo "--download-kernel requires an argument."
        exit 1
      fi
      shift
      KERNEL_VERSION="$1"
      KERNEL_DOWNLOAD=1
      ;;
    "--use-kernel-dir")
      if [[ $# -lt 2 ]]; then
        echo "--use-kernel-dir requires an argument."
        exit 1
      fi
      shift
      KERNEL_DIR="$1"
      KERNEL_DOWNLOAD=0
      ;;
    "--verify-modified-headers-only")
      if [[ $# -lt 2 ]]; then
        echo "--verify-modified-headers-only requires an argument."
        exit 1
      fi
      shift
      KERNEL_DIR="$1"
      KERNEL_DOWNLOAD=0
      VERIFY_HEADERS_ONLY=1
      ;;
    "-h" | "--help")
      usage
      exit 1
      ;;
    "-"*)
      echo "Error: Unrecognized option $1"
      usage
      exit 1
      ;;
    *)
      echo "Error: Extra arguments on the command-line."
      usage
      exit 1
      ;;
  esac
  shift
done
 
ANDROID_KERNEL_DIR="${ANDROID_BUILD_TOP}/${ANDROID_KERNEL_DIR}"
if [[ "${ANDROID_BUILD_TOP}" == "" ]]; then
  echo "ANDROID_BUILD_TOP is not set, did you run lunch?"
  exit 1
elif [[ ! -d "${ANDROID_KERNEL_DIR}" ]]; then
  echo "${ANDROID_BUILD_TOP} doesn't appear to be the root of an android tree."
  echo "  ${ANDROID_KERNEL_DIR} is not a directory."
  exit 1
fi
 
src_dir="linux-stable"
 
if [[ ${KERNEL_DOWNLOAD} -eq 1 ]]; then
  TMPDIR=$(mktemp -d /tmp/android_kernelXXXXXXXX)
  cd "${TMPDIR}"
  echo "Fetching linux kernel source ${KERNEL_VERSION}"
  git clone ${KERNEL_REPO}
  cd ${src_dir}
  git checkout tags/"${KERNEL_VERSION}"
  KERNEL_DIR="${TMPDIR}"
elif [[ "${KERNEL_DIR}" == "" ]]; then
  echo "Must specify one of --use-kernel-dir or --download-kernel."
  exit 1
elif [[ ! -d "${KERNEL_DIR}" ]] || [[ ! -d "${KERNEL_DIR}/${src_dir}" ]]; then
  echo "The kernel directory $KERNEL_DIR or $KERNEL_DIR/${src_dir} does not exist."
  exit 1
else
  cd "${KERNEL_DIR}/${src_dir}"
fi
 
if [[ ${VERIFY_HEADERS_ONLY} -eq 1 ]]; then
  # Verify if modified headers have changed.
  verify_modified_hdrs "${KERNEL_DIR}/${src_dir}/include/scsi" \
                       "${ANDROID_KERNEL_DIR}/scsi" \
                       "${KERNEL_DIR}/${src_dir}"
  exit 0
fi
 
if [[ ${SKIP_GENERATION} -eq 0 ]]; then
  # Clean up any leftover headers.
  make distclean
 
  # Build all of the generated headers.
  for arch in "${ARCH_LIST[@]}"; do
    echo "Generating headers for arch ${arch}"
    make ARCH=${arch} headers_install
  done
fi
 
# Completely delete the old original headers so that any deleted/moved
# headers are also removed.
rm -rf "${ANDROID_KERNEL_DIR}/uapi"
mkdir -p "${ANDROID_KERNEL_DIR}/uapi"
 
cd ${ANDROID_BUILD_TOP}
 
# Copy all of the include/uapi files to the kernel headers uapi directory.
copy_hdrs "${KERNEL_DIR}/${src_dir}/include/uapi" "${ANDROID_KERNEL_DIR}/uapi"
 
# Copy the staging files to uapi/linux.
copy_hdrs "${KERNEL_DIR}/${src_dir}/drivers/staging/android/uapi" \
          "${ANDROID_KERNEL_DIR}/uapi/linux" "no-copy-dirs"
 
# Remove ion.h, it's not fully supported by the upstream kernel (see b/77976082).
rm -f "${ANDROID_KERNEL_DIR}/uapi/linux/ion.h"
 
# Copy the generated headers.
copy_hdrs "${KERNEL_DIR}/${src_dir}/include/generated/uapi" \
          "${ANDROID_KERNEL_DIR}/uapi"
 
for arch in "${ARCH_LIST[@]}"; do
  # Copy arch headers.
  copy_hdrs "${KERNEL_DIR}/${src_dir}/arch/${arch}/include/uapi" \
            "${ANDROID_KERNEL_DIR}/uapi/asm-${arch}"
  # Copy the generated arch headers.
  copy_hdrs "${KERNEL_DIR}/${src_dir}/arch/${arch}/include/generated/uapi" \
            "${ANDROID_KERNEL_DIR}/uapi/asm-${arch}"
 
  # Special copy of generated header files from arch/<ARCH>/generated/asm that
  # also exist in uapi/asm-generic.
  copy_if_exists "${KERNEL_DIR}/${src_dir}/include/uapi/asm-generic" \
                 "${KERNEL_DIR}/${src_dir}/arch/${arch}/include/generated/asm" \
                 "${ANDROID_KERNEL_DIR}/uapi/asm-${arch}/asm"
done
 
# The arm types.h uapi header is not properly being generated, so copy it
# directly.
cp "${KERNEL_DIR}/${src_dir}/include/uapi/asm-generic/types.h" \
   "${ANDROID_KERNEL_DIR}/uapi/asm-arm/asm"
 
# Verify if modified headers have changed.
verify_modified_hdrs "${KERNEL_DIR}/${src_dir}/include/scsi" \
                     "${ANDROID_KERNEL_DIR}/scsi" \
                     "${KERNEL_DIR}/${src_dir}"
echo "Headers updated."