blob: 9d5b02dee9882b5787126bbae2704ae5491d0cb8 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001#!/usr/bin/env python
2#
Christopher Ferris5956b4e2016-07-20 12:28:56 -07003import sys, cpp, kernel, glob, os, re, getopt, clean_header, subprocess, shutil
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004from defaults import *
5from utils import *
6
Elliott Hughesc4c2e242019-04-11 14:09:30 -07007def Usage():
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08008 print """\
Christopher Ferrisd12c3322015-09-15 14:13:17 -07009 usage: %(progname)s [kernel-original-path] [kernel-modified-path]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080010
11 this program is used to update all the auto-generated clean headers
12 used by the Bionic C library. it assumes the following:
13
Christopher Ferrisd12c3322015-09-15 14:13:17 -070014 - a set of source kernel headers is located in
15 'external/kernel-headers/original', relative to the current
16 android tree
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080017
Christopher Ferrisd12c3322015-09-15 14:13:17 -070018 - a set of manually modified kernel header files located in
19 'external/kernel-headers/modified', relative to the current
20 android tree
21
22 - the clean headers will be placed in 'bionic/libc/kernel/arch-<arch>/asm',
Christopher Ferris5956b4e2016-07-20 12:28:56 -070023 'bionic/libc/kernel/android', etc..
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080024""" % { "progname" : os.path.basename(sys.argv[0]) }
25 sys.exit(0)
26
Elliott Hughesc4c2e242019-04-11 14:09:30 -070027def ProcessFiles(updater, original_dir, modified_dir, src_rel_dir, update_rel_dir):
Christopher Ferris15d3fef2017-02-24 12:26:48 -080028 # Delete the old headers before updating to the new headers.
29 update_dir = os.path.join(get_kernel_dir(), update_rel_dir)
30 shutil.rmtree(update_dir)
31 os.mkdir(update_dir, 0755)
32
33 src_dir = os.path.normpath(os.path.join(original_dir, src_rel_dir))
34 src_dir_len = len(src_dir) + 1
35 mod_src_dir = os.path.join(modified_dir, src_rel_dir)
36 update_dir = os.path.join(get_kernel_dir(), update_rel_dir)
37
38 kernel_dir = get_kernel_dir()
39 for root, _, files in os.walk(src_dir):
40 for file in sorted(files):
41 _, ext = os.path.splitext(file)
42 if ext != ".h":
43 continue
44 src_file = os.path.normpath(os.path.join(root, file))
45 rel_path = src_file[src_dir_len:]
46 # Check to see if there is a modified header to use instead.
47 if os.path.exists(os.path.join(mod_src_dir, rel_path)):
48 src_file = os.path.join(mod_src_dir, rel_path)
49 src_str = os.path.join("<modified>", src_rel_dir, rel_path)
50 else:
51 src_str = os.path.join("<original>", src_rel_dir, rel_path)
52 dst_file = os.path.join(update_dir, rel_path)
53 new_data = clean_header.cleanupFile(dst_file, src_file, rel_path)
54 if not new_data:
55 continue
56 updater.readFile(dst_file)
57 ret_val = updater.editFile(dst_file, new_data)
58 if ret_val == 0:
59 state = "unchanged"
60 elif ret_val == 1:
61 state = "edited"
62 else:
63 state = "added"
64 update_path = os.path.join(update_rel_dir, rel_path)
65 print "cleaning %s -> %s (%s)" % (src_str, update_path, state)
66
Elliott Hughesc4c2e242019-04-11 14:09:30 -070067
68# This lets us support regular system calls like __NR_write and also weird
69# ones like __ARM_NR_cacheflush, where the NR doesn't come at the start.
70def make__NR_name(name):
71 if name.startswith('__ARM_NR_'):
72 return name
73 else:
74 return '__NR_%s' % (name)
75
76
77# Scan Linux kernel asm/unistd.h files containing __NR_* constants
78# and write out equivalent SYS_* constants for glibc source compatibility.
79def GenerateGlibcSyscallsHeader(updater):
80 libc_root = '%s/bionic/libc/' % os.environ['ANDROID_BUILD_TOP']
81
82 # Collect the set of all syscalls for all architectures.
83 syscalls = set()
84 pattern = re.compile(r'^\s*#\s*define\s*__NR_([a-z_]\S+)')
85 for unistd_h in ['kernel/uapi/asm-generic/unistd.h',
86 'kernel/uapi/asm-arm/asm/unistd.h',
87 'kernel/uapi/asm-arm/asm/unistd-common.h',
88 'kernel/uapi/asm-arm/asm/unistd-eabi.h',
89 'kernel/uapi/asm-arm/asm/unistd-oabi.h',
90 'kernel/uapi/asm-x86/asm/unistd_32.h',
91 'kernel/uapi/asm-x86/asm/unistd_64.h',
92 'kernel/uapi/asm-x86/asm/unistd_x32.h']:
93 for line in open(os.path.join(libc_root, unistd_h)):
94 m = re.search(pattern, line)
95 if m:
96 nr_name = m.group(1)
97 if 'reserved' not in nr_name and 'unused' not in nr_name:
98 syscalls.add(nr_name)
99
100 # Create a single file listing them all.
101 # Note that the input files include #if trickery, so even for a single
102 # architecture we don't know exactly which ones are available.
103 # https://b.corp.google.com/issues/37110151
104 content = '/* Generated file. Do not edit. */\n'
105 content += '#pragma once\n'
106
107 for syscall in sorted(syscalls):
108 nr_name = make__NR_name(syscall)
109 content += '#if defined(%s)\n' % nr_name
110 content += ' #define SYS_%s %s\n' % (syscall, nr_name)
111 content += '#endif\n'
112
113 updater.editFile('%s/include/bits/glibc-syscalls.h' % libc_root, content)
114
115
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116try:
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700117 optlist, args = getopt.getopt(sys.argv[1:], '')
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118except:
Christopher Ferris15d3fef2017-02-24 12:26:48 -0800119 # Unrecognized option
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700120 sys.stderr.write("error: unrecognized option\n")
Elliott Hughesc4c2e242019-04-11 14:09:30 -0700121 Usage()
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700123if len(optlist) > 0 or len(args) > 2:
Elliott Hughesc4c2e242019-04-11 14:09:30 -0700124 Usage()
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800125
Christopher Ferris15d3fef2017-02-24 12:26:48 -0800126if len(args) > 0:
Glenn Kastenc61f9902011-12-19 11:27:50 -0800127 original_dir = args[0]
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200128else:
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700129 original_dir = get_kernel_headers_original_dir()
Christopher Ferris15d3fef2017-02-24 12:26:48 -0800130
131if len(args) > 1:
132 modified_dir = args[1]
133else:
134 modified_dir = get_kernel_headers_modified_dir()
135
136if not os.path.isdir(original_dir):
137 panic("The kernel directory %s is not a directory\n" % original_dir)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700139if not os.path.isdir(modified_dir):
Christopher Ferris15d3fef2017-02-24 12:26:48 -0800140 panic("The kernel modified directory %s is not a directory\n" % modified_dir)
Christopher Ferris38062f92014-07-09 15:33:25 -0700141
Christopher Ferris15d3fef2017-02-24 12:26:48 -0800142updater = BatchFileUpdater()
Elliott Hughesc4c2e242019-04-11 14:09:30 -0700143
Christopher Ferris15d3fef2017-02-24 12:26:48 -0800144# Process the original uapi headers first.
Elliott Hughesc4c2e242019-04-11 14:09:30 -0700145ProcessFiles(updater, original_dir, modified_dir, "uapi", "uapi"),
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700146
Christopher Ferris15d3fef2017-02-24 12:26:48 -0800147# Now process the special files.
Elliott Hughesc4c2e242019-04-11 14:09:30 -0700148ProcessFiles(updater, original_dir, modified_dir, "scsi", os.path.join("android", "scsi", "scsi"))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800149
Christopher Ferris15d3fef2017-02-24 12:26:48 -0800150updater.updateGitFiles()
Elliott Hughesc4c2e242019-04-11 14:09:30 -0700151
152# Now re-generate the <bits/glibc-syscalls.h> from the new uapi headers.
153updater = BatchFileUpdater()
154GenerateGlibcSyscallsHeader(updater)
155updater.updateGitFiles()