blob: 331a957966cf7c80ccf3f99cc44c92ffe22d1f43 [file] [log] [blame]
Elliott Hughes6b586e72021-04-15 13:39:08 -07001#!/usr/bin/env python3
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002#
Christopher Ferrisac7ec112021-04-19 13:50:16 -07003import sys, cpp, kernel, glob, os, re, getopt, clean_header, 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():
Christopher Ferrisac7ec112021-04-19 13:50:16 -07008 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..
Christopher Ferrisac7ec112021-04-19 13:50:16 -070024""" % { "progname" : os.path.basename(sys.argv[0]) })
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080025 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)
Christopher Ferris3fc4e112022-08-05 13:38:09 -070030 for root, dirs, files in os.walk(update_dir, topdown=True):
31 for entry in files:
32 # BUILD is a special file that needs to be preserved.
33 if entry == "BUILD":
34 continue
35 os.remove(os.path.join(root, entry))
36 for entry in dirs:
37 shutil.rmtree(os.path.join(root, entry))
Christopher Ferris15d3fef2017-02-24 12:26:48 -080038
39 src_dir = os.path.normpath(os.path.join(original_dir, src_rel_dir))
40 src_dir_len = len(src_dir) + 1
41 mod_src_dir = os.path.join(modified_dir, src_rel_dir)
42 update_dir = os.path.join(get_kernel_dir(), update_rel_dir)
43
44 kernel_dir = get_kernel_dir()
45 for root, _, files in os.walk(src_dir):
46 for file in sorted(files):
47 _, ext = os.path.splitext(file)
48 if ext != ".h":
49 continue
50 src_file = os.path.normpath(os.path.join(root, file))
51 rel_path = src_file[src_dir_len:]
52 # Check to see if there is a modified header to use instead.
53 if os.path.exists(os.path.join(mod_src_dir, rel_path)):
54 src_file = os.path.join(mod_src_dir, rel_path)
55 src_str = os.path.join("<modified>", src_rel_dir, rel_path)
56 else:
57 src_str = os.path.join("<original>", src_rel_dir, rel_path)
58 dst_file = os.path.join(update_dir, rel_path)
59 new_data = clean_header.cleanupFile(dst_file, src_file, rel_path)
60 if not new_data:
61 continue
62 updater.readFile(dst_file)
63 ret_val = updater.editFile(dst_file, new_data)
64 if ret_val == 0:
65 state = "unchanged"
66 elif ret_val == 1:
67 state = "edited"
68 else:
69 state = "added"
70 update_path = os.path.join(update_rel_dir, rel_path)
Christopher Ferrisac7ec112021-04-19 13:50:16 -070071 print("cleaning %s -> %s (%s)" % (src_str, update_path, state))
Christopher Ferris15d3fef2017-02-24 12:26:48 -080072
Elliott Hughesc4c2e242019-04-11 14:09:30 -070073
74# This lets us support regular system calls like __NR_write and also weird
75# ones like __ARM_NR_cacheflush, where the NR doesn't come at the start.
76def make__NR_name(name):
77 if name.startswith('__ARM_NR_'):
78 return name
79 else:
80 return '__NR_%s' % (name)
81
82
83# Scan Linux kernel asm/unistd.h files containing __NR_* constants
84# and write out equivalent SYS_* constants for glibc source compatibility.
85def GenerateGlibcSyscallsHeader(updater):
86 libc_root = '%s/bionic/libc/' % os.environ['ANDROID_BUILD_TOP']
87
88 # Collect the set of all syscalls for all architectures.
89 syscalls = set()
90 pattern = re.compile(r'^\s*#\s*define\s*__NR_([a-z_]\S+)')
Elliott Hughes80246ff2024-10-03 17:41:29 +000091 for unistd_h in glob.glob('%s/kernel/uapi/asm-*/asm/unistd*.h' % libc_root):
92 for line in open(unistd_h):
Elliott Hughesc4c2e242019-04-11 14:09:30 -070093 m = re.search(pattern, line)
94 if m:
95 nr_name = m.group(1)
96 if 'reserved' not in nr_name and 'unused' not in nr_name:
97 syscalls.add(nr_name)
98
99 # Create a single file listing them all.
100 # Note that the input files include #if trickery, so even for a single
101 # architecture we don't know exactly which ones are available.
102 # https://b.corp.google.com/issues/37110151
103 content = '/* Generated file. Do not edit. */\n'
104 content += '#pragma once\n'
105
106 for syscall in sorted(syscalls):
107 nr_name = make__NR_name(syscall)
108 content += '#if defined(%s)\n' % nr_name
109 content += ' #define SYS_%s %s\n' % (syscall, nr_name)
110 content += '#endif\n'
111
Christopher Ferrisfa59a102019-08-05 12:58:49 -0700112 syscall_file = os.path.join(libc_root, 'include/bits/glibc-syscalls.h')
113 updater.readFile(syscall_file)
114 updater.editFile(syscall_file, content)
Elliott Hughesc4c2e242019-04-11 14:09:30 -0700115
116
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117try:
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700118 optlist, args = getopt.getopt(sys.argv[1:], '')
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119except:
Christopher Ferris15d3fef2017-02-24 12:26:48 -0800120 # Unrecognized option
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700121 sys.stderr.write("error: unrecognized option\n")
Elliott Hughesc4c2e242019-04-11 14:09:30 -0700122 Usage()
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800123
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700124if len(optlist) > 0 or len(args) > 2:
Elliott Hughesc4c2e242019-04-11 14:09:30 -0700125 Usage()
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126
Christopher Ferris15d3fef2017-02-24 12:26:48 -0800127if len(args) > 0:
Glenn Kastenc61f9902011-12-19 11:27:50 -0800128 original_dir = args[0]
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200129else:
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700130 original_dir = get_kernel_headers_original_dir()
Christopher Ferris15d3fef2017-02-24 12:26:48 -0800131
132if len(args) > 1:
133 modified_dir = args[1]
134else:
135 modified_dir = get_kernel_headers_modified_dir()
136
137if not os.path.isdir(original_dir):
138 panic("The kernel directory %s is not a directory\n" % original_dir)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800139
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700140if not os.path.isdir(modified_dir):
Christopher Ferris15d3fef2017-02-24 12:26:48 -0800141 panic("The kernel modified directory %s is not a directory\n" % modified_dir)
Christopher Ferris38062f92014-07-09 15:33:25 -0700142
Christopher Ferris15d3fef2017-02-24 12:26:48 -0800143updater = BatchFileUpdater()
Elliott Hughesc4c2e242019-04-11 14:09:30 -0700144
Christopher Ferris15d3fef2017-02-24 12:26:48 -0800145# Process the original uapi headers first.
Elliott Hughesc4c2e242019-04-11 14:09:30 -0700146ProcessFiles(updater, original_dir, modified_dir, "uapi", "uapi"),
Christopher Ferrisd12c3322015-09-15 14:13:17 -0700147
Christopher Ferris15d3fef2017-02-24 12:26:48 -0800148# Now process the special files.
Elliott Hughesc4c2e242019-04-11 14:09:30 -0700149ProcessFiles(updater, original_dir, modified_dir, "scsi", os.path.join("android", "scsi", "scsi"))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800150
Christopher Ferrisac7ec112021-04-19 13:50:16 -0700151# Copy all of the files.
152updater.updateFiles()
Elliott Hughesc4c2e242019-04-11 14:09:30 -0700153
154# Now re-generate the <bits/glibc-syscalls.h> from the new uapi headers.
155updater = BatchFileUpdater()
156GenerateGlibcSyscallsHeader(updater)
Christopher Ferrisac7ec112021-04-19 13:50:16 -0700157updater.updateFiles()