1#!/bin/sh
2# SPDX-License-Identifier: LGPL-2.1
3
4if [ $# -ne 3 ] ; then
5	[ $# -eq 1 ] && hostarch=$1 || hostarch=`uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86/`
6	linux_header_dir=tools/include/uapi/linux
7	header_dir=tools/include/uapi/asm-generic
8	arch_header_dir=tools/arch/${hostarch}/include/uapi/asm
9else
10	linux_header_dir=$1
11	header_dir=$2
12	arch_header_dir=$3
13fi
14
15linux_mman=${linux_header_dir}/mman.h
16arch_mman=${arch_header_dir}/mman.h
17
18# those in egrep -vw are flags, we want just the bits
19
20printf "static const char *mmap_flags[] = {\n"
21regex='^[[:space:]]*#[[:space:]]*define[[:space:]]+MAP_([[:alnum:]_]+)[[:space:]]+(0x[[:xdigit:]]+)[[:space:]]*.*'
22egrep -q $regex ${arch_mman} && \
23(egrep $regex ${arch_mman} | \
24	sed -r "s/$regex/\2 \1 \1 \1 \2/g"	| \
25	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef MAP_%s\n#define MAP_%s %s\n#endif\n")
26egrep -q $regex ${linux_mman} && \
27(egrep $regex ${linux_mman} | \
28	egrep -vw 'MAP_(UNINITIALIZED|TYPE|SHARED_VALIDATE)' | \
29	sed -r "s/$regex/\2 \1 \1 \1 \2/g" | \
30	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef MAP_%s\n#define MAP_%s %s\n#endif\n")
31([ ! -f ${arch_mman} ] || egrep -q '#[[:space:]]*include[[:space:]]+.*uapi/asm-generic/mman.*' ${arch_mman}) &&
32(egrep $regex ${header_dir}/mman-common.h | \
33	egrep -vw 'MAP_(UNINITIALIZED|TYPE|SHARED_VALIDATE)' | \
34	sed -r "s/$regex/\2 \1 \1 \1 \2/g"	| \
35	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef MAP_%s\n#define MAP_%s %s\n#endif\n")
36([ ! -f ${arch_mman} ] || egrep -q '#[[:space:]]*include[[:space:]]+.*uapi/asm-generic/mman.h>.*' ${arch_mman}) &&
37(egrep $regex ${header_dir}/mman.h | \
38	sed -r "s/$regex/\2 \1 \1 \1 \2/g"	| \
39	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef MAP_%s\n#define MAP_%s %s\n#endif\n")
40printf "};\n"
41