summaryrefslogtreecommitdiff
path: root/tools/enter-kernel-dev
blob: beec83568c1d123d6e8916972a755383b40b500a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env bash

set -e

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

# When this script is used as is, SCRIPT_DIR is ok. If it's used as
# part of kernelDevTools, we have to inject the path to the flake.
if [ -z "$FLAKE_DIR" ]; then
   FLAKE_DIR="$SCRIPT_DIR"
fi

gcc_flag=false
kernel_version=""

# Parse command-line options with optional positional parameter handling
TEMP=$(getopt -o 'h:' --longoptions 'gcc,help' -n 'script_name' -- "$@")

eval set -- "$TEMP"

# Extract options
while true; do
  case "$1" in
    --gcc)
      gcc_flag=true
      shift
      ;;
    -h | --help)
      echo "Usage: $0 [--gcc] [KERNEL_VERSION]"
      exit 0
      ;;
    --)
      shift
      # Check if there's an argument after --
      if [[ -n "$1" ]]; then
        kernel_version="$1"
      fi
      break
      ;;
    *)
      echo "Error: Invalid option"
      echo "Usage: $0 [--gcc] [KERNEL_VERSION]"
      exit 1
      ;;
  esac
done

if [ -z "$kernel_version" ]; then

    latest_release=$(git tag --contains=HEAD | grep -oE 'v[0-9]+\.[0-9]+' | sort | head -n1)

    if [ -z "$latest_release" ]; then
	tput bold
	echo -n "Failed to automatically recognize kernel version."
	tput sgr0
	echo " This could have different reasons:"
	echo
	echo "0. This is a development version of Linux and not part of any release yet. Try manually selecting a version:"
	echo "   \$ enter-kernel-dev v6.10"
	echo
	echo "1. This is not a Git checkout of the Linux kernel sources. To checkout Linux, try:"
	echo "   \$ linux-checkout linux-src"
	echo
	echo "2. The repository has no remote that contains the version tags. Try:"
	echo "   \$ git remote add stable git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git"
	echo "   \$ git fetch stable"
	exit 1
    fi

    kernel_version="$latest_release"
fi

if ! echo $kernel_version | grep -qE 'v[0-9]+\.[0-9]+'; then
    tput bold
    echo "Invalid kernel version format: $kernel_version"
    tput sgr0
    echo
    echo "Kernel versions should look like: v6.10"
    exit 1
fi

ATTRIBUTE="linux_$(echo "$kernel_version" | tr -d 'v' | tr '.' '_')"

if $gcc_flag; then
    ATTRIBUTE=${ATTRIBUTE}_gcc
fi

echo -n "Entering environment "
tput bold
echo -n "$ATTRIBUTE"
tput sgr0			# reset
echo ". This might take a moment. Exit via ^D."

# TODO It would be nice to gracefully fail if we don't have an
# environment for a particular kernel version yet.

exec nix develop "$FLAKE_DIR"\#"$ATTRIBUTE" --command "$SHELL"