From 951ab80623a9f413e1fc9ccf83307c60b9cf9dd5 Mon Sep 17 00:00:00 2001 From: Ammar Ratnani Date: Sat, 5 Aug 2023 20:50:27 -0400 Subject: [PATCH] Initial pass of getting `newlib` to compile --- .gitignore | 5 ++ config.sub | 3 +- flake.lock | 130 ++++++++++++++++++++++++++++++++++++++++++ flake.nix | 89 +++++++++++++++++++++++++++++ newlib/configure.host | 2 + 5 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.gitignore b/.gitignore index 2bee2a5..96289b5 100644 --- a/.gitignore +++ b/.gitignore @@ -46,3 +46,8 @@ core !core/ lost+found + +Makefile +build/ + +.vscode diff --git a/config.sub b/config.sub index 63c1f1c..1a9de79 100755 --- a/config.sub +++ b/config.sub @@ -1255,7 +1255,8 @@ case $cpu-$vendor in | x86 | x86_64 | xc16x | xgate | xps100 \ | xstormy16 | xtensa* \ | ymp \ - | z8k | z80) + | z8k | z80 \ + | lc_3.2) ;; *) diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..0e09345 --- /dev/null +++ b/flake.lock @@ -0,0 +1,130 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1689068808, + "narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "inputs": { + "systems": "systems_2" + }, + "locked": { + "lastModified": 1681202837, + "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "llvm-lc-3-2": { + "inputs": { + "flake-utils": "flake-utils_2", + "nixpkgs-23-05": "nixpkgs-23-05" + }, + "locked": { + "lastModified": 1690145561, + "narHash": "sha256-5NAIxipLfBY6wTERWkMF3UQba13tne3K8oyHg0F3B8k=", + "ref": "refs/heads/main", + "rev": "71662f33fd45b1175687353513765d384807622d", + "revCount": 220, + "type": "git", + "url": "ssh://git@github.com/lc-3-2/llvm.git" + }, + "original": { + "type": "git", + "url": "ssh://git@github.com/lc-3-2/llvm.git" + } + }, + "nixpkgs-23-05": { + "locked": { + "lastModified": 1685836969, + "narHash": "sha256-TzFCJwaKE9jexmAsG7bf5kBJnE5xpTtnFUwHQdqe0Nk=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "6144bbe8950c9ebc07ceab98ba3f9af46536e73b", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "release-23.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-23-05_2": { + "locked": { + "lastModified": 1691264946, + "narHash": "sha256-E2ISuYveRsbxfcXupkHXFN6UaVgwv/X4fR/Au1yTCgc=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "4612af6e1c8b3eda45f5cb00806b64abe806bdad", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "release-23.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "llvm-lc-3-2": "llvm-lc-3-2", + "nixpkgs-23-05": "nixpkgs-23-05_2" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..79613a3 --- /dev/null +++ b/flake.nix @@ -0,0 +1,89 @@ +{ + description = "Newlib for LC-3.2 Development and Build Environment"; + + inputs = { + nixpkgs-23-05.url = "github:NixOS/nixpkgs/release-23.05"; + flake-utils.url = "github:numtide/flake-utils"; + + # We have to use SSH here since the repository is private + llvm-lc-3-2.url = "git+ssh://git@github.com/lc-3-2/llvm.git"; + }; + + outputs = { self, nixpkgs-23-05, flake-utils, llvm-lc-3-2 }@inputs : + flake-utils.lib.eachSystem ["x86_64-linux"] (system: + let + name = "newlib-lc-3.2-dev"; + pkgs = nixpkgs-23-05.legacyPackages.${system}; + inherit (pkgs) stdenv; + + nativeBuildInputs = [ + pkgs.texinfo + + # Usually, we would add custom packages with an overlay, but that adds a + # lot of complexity. Thus, we fetch the package manually. + llvm-lc-3-2.packages.${system}.lc-3-2 + ]; + + in { + + packages = rec { + default = newlib-lc-3-2; + + newlib-lc-3-2 = stdenv.mkDerivation { + inherit name nativeBuildInputs; + + src = self; + + configurePhase = '' + mkdir ./build/ + pushd ./build/ + + CC_FOR_TARGET="clang" \ + CXX_FOR_TARGET="clang++" \ + AS_FOR_TARGET="llvm-mc" \ + LD_FOR_TARGET="ld.lld" \ + AR_FOR_TARGET="llvm-ar" \ + RANLIB_FOR_TARGET="llvm-ranlib" \ + STRIP_FOR_TARGET="llvm-strip" \ + OBJCOPY_FOR_TARGET="llvm-objcopy" \ + OBJDUMP_FOR_TARGET="llvm-objdump" \ + READELF_FOR_TARGET="llvm-readelf" \ + NM_FOR_TARGET="llvm-nm" \ + DLLTOOL_FOR_TARGET="llvm-dlltool" \ + LIPO_FOR_TARGET="llvm-lipo" \ + WINDRES_FOR_TARGET="llvm-windres" \ + CFLAGS_FOR_TARGET="-nostdlib -D__IEEE_LITTLE_ENDIAN -v" \ + ../configure \ + --host=x86_64-pc-linux-gnu --target=lc_3.2 \ + --prefix=$out \ + --disable-multilib + + popd + ''; + + buildPhase = "make -C ./build/"; + installPhase = "make -C ./build/ install"; + }; + }; + + devShells = rec { + default = newlib-lc-3-2; + + newlib-lc-3-2 = pkgs.mkShell { + inherit name nativeBuildInputs; + + # Add autotools in case we need to modify and regenerate. We don't + # mark this as a nativeBuildInput for the package since this is only + # done when we push changes. + buildInputs = [ + pkgs.autoconf269 + pkgs.automake115x + ]; + + shellHook = '' + export PS1="(${name}) [\u@\h \W]\$ " + ''; + }; + }; + }); +} diff --git a/newlib/configure.host b/newlib/configure.host index 0a1ba28..bb3da3c 100644 --- a/newlib/configure.host +++ b/newlib/configure.host @@ -377,6 +377,8 @@ case "${host_cpu}" in newlib_cflags="${newlib_cflags} -DMISSING_SYSCALL_NAMES" newlib_cflags="${newlib_cflags} -ffunction-sections -fdata-sections " ;; + lc_3.2) + ;; *) echo '***' "Newlib does not support CPU ${host_cpu}" 1>&2 exit 1