[TEST v2 1/1] [WIP] Try compile linux on macOS with the help of nix
[email protected] Sun, 12 Apr 2026 21:13:58 +0800
| Newsgroups | org.kernel.vger.linux-newbie |
|---|---|
| Message-ID | <aa6e50c518a1873551235553c49db0428e336314.1775999068.git.jw910731@gmail.com> |
From: jw910731 <[email protected]> Signed-off-by: Wen-Yuan Wu <[email protected]> --- flake.lock | 82 ++++++++++++++++++++++++++++++++ flake.nix | 43 +++++++++++++++++ scripts/Kconfig.include | 2 +- scripts/install.sh | 2 +- scripts/macos-include/byteswap.h | 4 ++ scripts/macos-include/elf.h | 24 ++++++++++ scripts/macos-include/endian.h | 8 ++++ scripts/mod/file2alias.c | 3 ++ usr/gen_init_cpio.c | 4 +- 9 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 scripts/macos-include/byteswap.h create mode 100644 scripts/macos-include/elf.h create mode 100644 scripts/macos-include/endian.h diff --git a/flake.lock b/flake.lock new file mode 100644 index 000000000..e6c1f5f67 --- /dev/null +++ b/flake.lock @@ -0,0 +1,82 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1772963539, + "narHash": "sha256-9jVDGZnvCckTGdYT53d/EfznygLskyLQXYwJLKMPsZs=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "9dcb002ca1690658be4a04645215baea8b95f31d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay", + "utils": "utils" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1773115373, + "narHash": "sha256-bfK9FJFcQth6f3ydYggS5m0z2NRGF/PY6Y2XgZDJ6pg=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "1924b4672a2b8e4aee6e6652ec2e59a8d3c5648e", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 000000000..b2199f6eb --- /dev/null +++ b/flake.nix @@ -0,0 +1,43 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + utils.url = "github:numtide/flake-utils"; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs = { + nixpkgs.follows = "nixpkgs"; + }; + }; + }; + outputs = { self, nixpkgs, utils, rust-overlay }: utils.lib.eachDefaultSystem (system: + let + overlays = [ (import rust-overlay) ]; + pkgs = import nixpkgs { + inherit system overlays; + }; + pkgsCross = pkgs.pkgsCross.aarch64-multiplatform; + in + { + devShell = pkgsCross.mkShell { + nativeBuildInputs = with pkgsCross; [ + gnumake + ncurses + flex + bison + lld + rustc + rust-bindgen + rustfmt + clippy + pahole + ]; + buildInputs = []; + HOSTCC="${pkgs.lib.getBin pkgs.clang}/bin/clang"; + HOSTLD="${pkgs.lib.getBin pkgs.lld}/bin/lld"; + HOSTCFLAGS="-I${pkgs.lib.getLib pkgs.libelf}/include -Iscripts/macos-include"; + NIX_CFLAGS_COMPILE="-march=armv8-a+crypto"; + RUST_LIB_SRC = "${pkgsCross.rust.packages.stable.rustPlatform.rustLibSrc}"; + }; + } + ); +} \ No newline at end of file diff --git a/scripts/Kconfig.include b/scripts/Kconfig.include index 33193ca6e..c3f38006c 100644 --- a/scripts/Kconfig.include +++ b/scripts/Kconfig.include @@ -54,7 +54,7 @@ as-version := $(shell,set -- $(as-info) && echo $2) # Get the linker name, version, and error out if it is not supported. ld-info := $(shell,$(srctree)/scripts/ld-version.sh $(LD)) -$(error-if,$(success,test -z "$(ld-info)"),Sorry$(comma) this linker is not supported.) +$(error-if,$(success,test -z "$(ld-info)"),Sorry$(comma) this linker $(LD) is not supported.) ld-name := $(shell,set -- $(ld-info) && echo $1) ld-version := $(shell,set -- $(ld-info) && echo $2) diff --git a/scripts/install.sh b/scripts/install.sh index 05d62ac51..b99183bb8 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -6,7 +6,7 @@ # Adapted from code in arch/i386/boot/Makefile by H. Peter Anvin # Common code factored out by Masahiro Yamada -set -e +set -xe # Make sure the files actually exist for file in "${KBUILD_IMAGE}" System.map diff --git a/scripts/macos-include/byteswap.h b/scripts/macos-include/byteswap.h new file mode 100644 index 000000000..fd97ed5e1 --- /dev/null +++ b/scripts/macos-include/byteswap.h @@ -0,0 +1,4 @@ +#pragma once +#define bswap_16 __builtin_bswap16 +#define bswap_32 __builtin_bswap32 +#define bswap_64 __builtin_bswap64 diff --git a/scripts/macos-include/elf.h b/scripts/macos-include/elf.h new file mode 100644 index 000000000..5d42ed858 --- /dev/null +++ b/scripts/macos-include/elf.h @@ -0,0 +1,24 @@ +#pragma once +#include <libelf/gelf.h> + +#define STT_SPARC_REGISTER 3 +#define R_386_32 1 +#define R_386_PC32 2 +#define R_MIPS_HI16 5 +#define R_MIPS_LO16 6 +#define R_MIPS_26 4 +#define R_MIPS_32 2 +#define R_ARM_ABS32 2 +#define R_ARM_REL32 3 +#define R_ARM_PC24 1 +#define R_ARM_CALL 28 +#define R_ARM_JUMP24 29 +#define R_ARM_THM_JUMP24 30 +#define R_ARM_THM_PC22 10 +#define R_ARM_MOVW_ABS_NC 43 +#define R_ARM_MOVT_ABS 44 +#define R_ARM_THM_MOVW_ABS_NC 47 +#define R_ARM_THM_MOVT_ABS 48 +#define R_ARM_THM_JUMP19 51 +#define R_AARCH64_ABS64 257 +#define R_AARCH64_PREL64 260 diff --git a/scripts/macos-include/endian.h b/scripts/macos-include/endian.h new file mode 100644 index 000000000..6a27c4a0b --- /dev/null +++ b/scripts/macos-include/endian.h @@ -0,0 +1,8 @@ +#pragma once +#include <machine/endian.h> +#include <libkern/OSByteOrder.h> + +#define le16toh(x) OSSwapLittleToHostInt16(x) +#define le32toh(x) OSSwapLittleToHostInt32(x) +#define le64toh(x) OSSwapLittleToHostInt64(x) +#define EM_AARCH64 183 diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c index 4e99393a3..98aee9a4d 100644 --- a/scripts/mod/file2alias.c +++ b/scripts/mod/file2alias.c @@ -16,7 +16,10 @@ #include "list.h" #include "xalloc.h" +#define _UUID_T +#define uuid_t int #include "modpost.h" +#undef uuid_t #include "devicetable-offsets.h" /* We use the ELF typedefs for kernel_ulong_t but bite the bullet and diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c index b7296edc6..809602912 100644 --- a/usr/gen_init_cpio.c +++ b/usr/gen_init_cpio.c @@ -457,7 +457,7 @@ static int cpio_mkfile(const char *name, const char *location, goto error; if (size) { - this_read = copy_file_range(file, NULL, outfd, NULL, size, 0); + // this_read = copy_file_range(file, NULL, outfd, NULL, size, 0); if (this_read > 0) { if (this_read > size) goto error; @@ -674,7 +674,7 @@ int main (int argc, char *argv[]) break; case 'o': outfd = open(optarg, - O_WRONLY | O_CREAT | O_LARGEFILE | O_TRUNC, + O_WRONLY | O_CREAT | O_TRUNC, 0600); if (outfd < 0) { fprintf(stderr, "failed to open %s\n", optarg); -- 2.51.2