| #!/bin/bash |
| # Parses Kconfig lines like |
| # CONFIG_DEBUG=y >=6.1 |
| # # CONFIG_DEBUG is not set <6.1 |
| # and only adds them to the build if the kernel is 6.1 or greater |
| # Regular lines are unchanged |
| version="$1" |
| shift |
| declare -A plats |
| for plat in $1; do |
| plats["$plat"]=1 |
| done |
| shift |
| # Disallow "x.y" versions as they don't sort correctly |
| is_xy_ver() { |
| [[ "$1" =~ ^[0-9]+[.][0-9]+$ ]] |
| } |
| if is_xy_ver "$version"; then |
| echo "Linux version is unsupported $version" >&2 |
| exit 1 |
| fi |
| for file in "$@"; do |
| echo "Trimming $file to $version" >&2 |
| exec {nfile}<>"$file.tmp" |
| while read -r line; do |
| larr=($line) |
| if [[ ${larr[0]} == '#' ]]; then |
| key="${larr[1]}" |
| val='n' |
| larr=("${larr[@]:5}") |
| else |
| key="${larr[0]%%=*}" |
| val="${larr[0]#*=}" |
| larr=("${larr[@]:1}") |
| fi |
| skip= |
| for const in "${larr[@]}"; do |
| if [[ "${const:0:1}" == '#' ]]; then |
| break |
| elif [[ ${const:0:2} == '>=' ]]; then |
| if is_xy_ver "${const:2}"; then |
| echo "Constraint version is unsupported ${const:2}: $line" >&2 |
| exit 1 |
| fi |
| expected="${const:2}"$'\n'"$version" |
| if [[ $expected != $(echo "$expected" | sort -V) ]]; then |
| skip=1 |
| break |
| fi |
| elif [[ ${const:0:1} == '>' ]]; then |
| if is_xy_ver "${const:1}"; then |
| echo "Constraint version is unsupported ${const:1}: $line" >&2 |
| exit 1 |
| fi |
| expected="${const:1}"$'\n'"$version" |
| if [[ ${const:1} == $version || $expected != $(echo "$expected" | sort -V) ]]; then |
| skip=1 |
| break |
| fi |
| elif [[ ${const:0:2} == '<=' ]]; then |
| if is_xy_ver "${const:2}"; then |
| echo "Constraint version is unsupported ${const:2}: $line" >&2 |
| exit 1 |
| fi |
| expected="$version"$'\n'"${const:2}" |
| if [[ $expected != $(echo "$expected" | sort -V) ]]; then |
| skip=1 |
| break |
| fi |
| elif [[ ${const:0:1} == '<' ]]; then |
| if is_xy_ver "${const:1}"; then |
| echo "Constraint version is unsupported ${const:1}: $line" >&2 |
| exit 1 |
| fi |
| expected="$version"$'\n'"${const:1}" |
| if [[ ${const:1} == $version || $expected != $(echo "$expected" | sort -V) ]]; then |
| skip=1 |
| break |
| fi |
| elif [[ $const =~ ^[a-zA-Z0-9] ]]; then |
| OLDIFS="$IFS" |
| IFS=',' |
| match= |
| for plat in $const; do |
| if [[ -v plats["$plat"] ]]; then |
| match=1 |
| break |
| fi |
| done |
| IFS="$OLDIFS" |
| if [[ -z "$match" ]]; then |
| skip=1 |
| break |
| fi |
| else |
| echo "Unexpected constraint '$const' at line: $line" >&2 |
| exit 1 |
| fi |
| done |
| if [[ -z $skip ]]; then |
| echo "$key=$val" >&$nfile |
| fi |
| done < <(grep '^\(# \)\?CONFIG_' "$file") |
| exec {nfile}>&- |
| mv "$file.tmp" "$file" |
| done |