blob: 2a85630d1b392e5bc281d9f57760ff5f82965605 [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.
27### --download-kernel
28### Automatically create a temporary git repository and check out the
29### Android kernel source code.
30### --use-kernel-dir <DIR>
31### Do not check out the kernel source, use the kernel directory
32### pointed to by <DIR>.
33
34# Terminate the script if any command fails.
35set -eE
36
37TMPDIR=""
38ANDROID_DIR=""
39KERNEL_VERSION="android-3.10"
40KERNEL_DIR=""
41KERNEL_DOWNLOAD=0
42ARCH_LIST=("arm" "arm64" "mips" "x86")
43ANDROID_KERNEL_DIR="external/kernel-headers/original"
44SKIP_GENERATION=0
45
46function cleanup () {
47 if [[ "${TMPDIR}" =~ /tmp ]] && [[ -d "${TMPDIR}" ]]; then
48 echo "Removing temporary directory ${TMPDIR}"
49 rm -rf "${TMPDIR}"
50 TMPDIR=""
51 fi
52}
53
54function usage () {
55 grep '^###' $0 | sed -e 's/^###//'
56}
57
58function copy_hdrs () {
59 local src_dir=$1
60 local tgt_dir=$2
61 local dont_copy_dirs=$3
62
63 mkdir -p ${tgt_dir}
64
65 local search_dirs=()
66
67 # This only works if none of the filenames have spaces.
68 for file in $(ls -d ${src_dir}/* 2> /dev/null); do
69 if [[ -d "${file}" ]]; then
70 search_dirs+=("${file}")
71 elif [[ -f "${file}" ]] && [[ "${file}" =~ .h$ ]]; then
72 cp ${file} ${tgt_dir}
73 fi
74 done
75
76 if [[ "${dont_copy_dirs}" == "" ]]; then
77 for dir in "${search_dirs[@]}"; do
78 copy_hdrs "${dir}" ${tgt_dir}/$(basename ${dir})
79 done
80 fi
81}
82
83trap cleanup EXIT
84# This automatically triggers a call to cleanup.
85trap "exit 1" HUP INT TERM TSTP
86
87while [ $# -gt 0 ]; do
88 case "$1" in
89 "--skip-generation")
90 SKIP_GENERATION=1
91 ;;
92 "--download-kernel")
93 KERNEL_DOWNLOAD=1
94 ;;
95 "--use-kernel-dir")
96 if [[ $# -lt 2 ]]; then
97 echo "--use-kernel-dir requires an argument."
98 exit 1
99 fi
100 shift
101 KERNEL_DIR="$1"
102 KERNEL_DOWNLOAD=0
103 ;;
104 "-h" | "--help")
105 usage
106 exit 1
107 ;;
108 "-"*)
109 echo "Error: Unrecognized option $1"
110 usage
111 exit 1
112 ;;
113 *)
114 echo "Error: Extra arguments on the command-line."
115 usage
116 exit 1
117 ;;
118 esac
119 shift
120done
121
122ANDROID_KERNEL_DIR="${ANDROID_BUILD_TOP}/${ANDROID_KERNEL_DIR}"
123if [[ "${ANDROID_BUILD_TOP}" == "" ]]; then
124 echo "ANDROID_BUILD_TOP is not set, did you run lunch?"
125 exit 1
126elif [[ ! -d "${ANDROID_KERNEL_DIR}" ]]; then
127 echo "${ANDROID_BUILD_TOP} doesn't appear to be the root of an android tree."
128 echo " ${ANDROID_KERNEL_DIR} is not a directory."
129 exit 1
130fi
131
132if [[ ${KERNEL_DOWNLOAD} -eq 1 ]]; then
133 TMPDIR=$(mktemp -d /tmp/android_kernelXXXXXXXX)
134 cd "${TMPDIR}"
135 echo "Fetching android kernel source ${KERNEL_VERSION}"
136 git clone https://android.googlesource.com/kernel/common.git
137 cd common
138 git checkout "${KERNEL_VERSION}"
139 KERNEL_DIR="${TMPDIR}"
140elif [[ "${KERNEL_DIR}" == "" ]]; then
141 echo "Must specify one of --use-kernel-dir or --download-kernel."
142 exit 1
143elif [[ ! -d "${KERNEL_DIR}" ]] || [[ ! -d "${KERNEL_DIR}/common" ]]; then
144 echo "The kernel directory $KERNEL_DIR or $KERNEL_DIR/common does not exist."
145 exit 1
146else
147 cd "${KERNEL_DIR}/common"
148fi
149
150if [[ ${SKIP_GENERATION} -eq 0 ]]; then
151 # Build all of the generated headers.
152 for arch in "${ARCH_LIST[@]}"; do
153 echo "Generating headers for arch ${arch}"
154 make ARCH=${arch} headers_install
155 done
156fi
157
158# Copy all of the include/uapi files to the kernel headers uapi directory.
159copy_hdrs "${KERNEL_DIR}/common/include/uapi" "${ANDROID_KERNEL_DIR}/uapi"
160
161copy_hdrs "${KERNEL_DIR}/common/drivers/staging/android/uapi" \
162 "${ANDROID_KERNEL_DIR}/uapi/linux" "no-copy-dirs"
163
164copy_hdrs "${KERNEL_DIR}/common/include/generated/uapi" \
165 "${ANDROID_KERNEL_DIR}/uapi"
166
167for arch in "${ARCH_LIST[@]}"; do
168 if [[ "$arch" == "arm64" ]]; then
169 # The Android headers use aarch64 as the name of the 64 bit arm headers.
170 tgt_arch="asm-aarch64"
171 else
172 tgt_arch="asm-${arch}"
173 fi
174 # Copy arch headers first.
175 copy_hdrs "${KERNEL_DIR}/common/arch/${arch}/include/uapi" \
176 "${ANDROID_KERNEL_DIR}/uapi/${tgt_arch}"
177 # Copy the generated arch header files.
178 copy_hdrs "${KERNEL_DIR}/common/arch/${arch}/include/generated/uapi" \
179 "${ANDROID_KERNEL_DIR}/uapi/${tgt_arch}"
180done