package kernel-source {
	description = "unpatched Linux kernel source tree";
	website = "https://kernel.org";
	exclude = true;

	version* = "6.12.90";
	output = remoteTar {
		url = "https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/"+
			"snapshot/linux-"+version+".tar.gz";
		checksum = "l9d_2veqbj3RVDp8L6vhV7j2DOLQtzQUqnzPFuJBR_VD8a3k4mvBQRR1-LE-SWZJ";
		compress = gzip;
	};
}

package kernel-headers {
	description = "an installation of kernel headers";
	website = "https://kernel.org";

	source = kernel-source;

	files = {
		// updated manually for API changes
		"version.h";
	};
	toyboxEarly = true;

	exec = make {
		skipConfigure = true;
		make = [
			"-f /usr/src/kernel-headers/Makefile",
			"O=/tmp/kbuild",
			"LLVM=1",
			`HOSTLDFLAGS="${LDFLAGS:-''}"`,
			"INSTALL_HDR_PATH=/work/system",
			"headers_install",
		];
		skipCheck = true;
		install = `
cat \
	/usr/src/version.h > \
	/work/system/include/linux/version.h
`;
	};

	checksum = arch {
		default = "";

		amd64 = "FkIf0_SD4g3mK_VWa9FJURjLS66YmknYTh49uMDlDSnoQpq6BqBymAm47joDwdWi";
		arm64 = "bJ6ZLJhDR3K9So9zLh5s31hQPm_27gyl7XDVmCgdvyJGKDnffCKdtlE0HuvEbvye";
		riscv64 = "bbZCVNS1ryP-XoW9Gwx3ugvhvOA6d5UuO6gE7-Gv82g_bQN1hk4GG0SXBo-otyqS";
	};
	inputs = [ rsync ];
}

package kernel {
	description = "the generic Rosa OS linux kernel";
	website = "https://kernel.org";
	anitya = 375621;

	source = kernel-source;
	patches = [
		"f54a91f5337cd918eb86cf600320d25b6cfd8209.patch",
		"6ce6fbfddc5b127e4f57c3b5bfdcf40239a4fc2f.patch",
	];
	files = {
		".config": arch {
			amd64 = "kernel_amd64.config";
			arm64 = "kernel_arm64.config";
			riscv64 = "kernel_riscv64.config";
		};

		".installkernel": "installkernel";
		".depmod": "depmod";
	};

	env = [
		"PATH=/system/sbin",
	];

	early = `
install -Dm0400 \
	/usr/src/.config \
	/tmp/kbuild/.config
install -Dm0500 \
	/usr/src/.installkernel \
	/sbin/installkernel
`;

	exclusive = true;

	exec = make {
		omitDefaults = true;
		skipConfigure = true;

		// Build, install, and boot kernel before running kselftest on it.
		skipCheck = true;

		make = [
			"-f /usr/src/kernel/Makefile",
			"O=/tmp/kbuild",

			"LLVM=1",
			"KBUILD_BUILD_VERSION='1-Rosa'",
			"KBUILD_BUILD_TIMESTAMP='2106-02-07 06:28:15 UTC'",
			"KBUILD_BUILD_USER=kbuild",
			"KBUILD_BUILD_HOST=localhost",
			"all",
		];

		install = `
# kernel is not aware of kmod moduledir
install -Dm0500 \
	/usr/src/.depmod \
	/sbin/depmod

make \
	` + jobsFlagE + ` \
	-f /usr/src/kernel/Makefile \
	O=/tmp/kbuild \
	LLVM=1 \
	INSTALL_PATH=/work \
	install \
	INSTALL_MOD_PATH=/work/system \
	DEPMOD=/sbin/depmod \
	modules_install
rm -v "/work/system/lib/modules/$(make -f /usr/src/kernel/Makefile kernelversion)/build"
`;
	};

	inputs = [
		flex,
		bison,
		m4,
		tar,
		perl,
		bc,
		sed,
		gawk,
		coreutils,
		diffutils,
		python,

		xz,
		gzip,
		kmod,
		elfutils,
		util-linux,
		kernel-headers,
	];
}

package gen_init_cpio {
	description = "a program in the kernel source tree for creating initramfs archive";
	source = kernel-source;

	enterSource = true;
	exec = generic {
		inPlace = true;
		build = `
mkdir -p /work/system/bin/
cc -o /work/system/bin/gen_init_cpio usr/gen_init_cpio.c
`;
	};
}
