Skip to content

Add MicroHs support. #864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft

Add MicroHs support. #864

wants to merge 18 commits into from

Conversation

dmjio
Copy link
Owner

@dmjio dmjio commented Apr 10, 2025

We'd like to add support for @augustss's MicroHs project as a first class citizen, to explore its cross-compilation capabilities (w/ emscripten, and other platforms).

This PR fetches MicroHs at master and builds using the recommended Makefile workflow. We make install into /nix/store during the installPhase, along with other tools. This PR introduces 3 derivations to facilitate the process of getting a miso MicroHS build into miso's CI.

  1. The microhs nix package places cpphs, mhs and mcabal inside of $out/bin. We also copy lib, generated, src, boards, docs, into $out (aka /nix/store/).

  2. We now acquire mcabal, mhs and cpphs through the make C bootstrapping install, and have a derivation for mhs built w/ GHC (separately).

  3. We have created a targets.conf file that lives in the /nix/store that mhs now uses to set CC and CC-FLAGS correctly.

  4. microhs-env is a shell environment (cd miso/micro && nix-shell) that includes both of the above packages. Using nix-shell we can now enter an environment where mhs is present on $PATH and points to the correct $MHSDIR, along with all the other build tools present (cpphs, mcabal).

To test, clone miso and call nix-shell --argstr pkg micro --run 'mhs --version'

  • Build MicroHS into miso's nixpkgs (mcabal,mhs,cpphs)
  • Create 3 derivations for microhs (microhs,microhs-env,microhs-wrapper)
  • Introduce micro/ subdirectory with a new miso.cabal that MicroHS supports
  • Add MicroHs shell environment to shell.nix (command above), edit: adds nodejs and emscripten too
  • Ensure MicroHs works on Darwin OSX
  • Ensure emscripten builds C files on Darwin (Emscripten tries to write to /nix/store NixOS/nixpkgs#139943 (comment))
  • Compile and run fibs Example.hs in CI for x86
  • Ensure emscripten JS payloads run in QuickJS
  • Compile and run fibs Example.hs in CI for JS (even w/ -temscripten gcc is still chosen (and targets.conf has both).
  • Use mcabal install to fetch and build all of miso's dependencies (WIP)
  • Explore adding support for a JS FFI (foreign import javascript& js-sources:) via emscripten (this might require modifications to mhs and mcabal).
  • Place miso's MicroHS build under CI using both emscripten and native
  • Consider upstreaming MicroHs into nixpkgs proper (with pkgsCross support) -- using the traditional haskell nixpkgs workflow

For JSFFI support (for browser targets) w/ emscripten research the following

@dmjio dmjio force-pushed the micro-miso branch 3 times, most recently from 841f4e7 to 5b4fd94 Compare April 10, 2025 05:33
dmjio added 10 commits April 14, 2025 13:07
We'd like to add support for the [MicroHs project](https://github.com/augustss/MicroHs) as a first class
citizen, to explore its cross-compilation capabilities (w/ emscripten, and other platforms).

This PR fetches MicroHs at master and builds (with ghc910)
using the recommended Makefile workflow. We "make install" into
/nix/store during the installPhase.

1) The 'microhs' package that places cpphs, mcabal inside of $out/bin,
mhs is placed inside of $out/lib/bin. We also copy lib, generated,
boards, docs, into $out (aka /nix/store/).

2) 'microhs-wrapper' package. This is a shell script that ensure `mhs`
is present on PATH, also ensures the $MSHDIR variable points to the
MicroHs /lib that exists in the /nix/store. "export MHSDIR=${self.microhs}"

3) 'microhs-env' shell environment that includes both of the above
packages. Using `nix-shell` we can now enter an environment where
`mhs` is present on PATH and points to the correct $MHSDIR, along with
all the other build tools present (cpphs, mcabal).

To test, clone miso and call "nix-shell --argstr pkg micro --run 'mhs --version'"

- [x] Build MicroHS into miso's nixpkgs (mcabal,mhs,cpphs)
- [x] Create 3 derivations for microhs (microhs,microhs-env,microhs-wrapper)
- [x] Introduce micro/ subdirectory with a new 'miso.cabal' that MicroHS supports
- [x] Add MicroHs shell environment to shell.nix (command above)
- [ ] Use 'mcabal install' to fetch and build all of miso's dependencies (TODO)
- [ ] Explore adding support for a JS FFI via emscripten (this might require modifications to mcabal or mhs to support "foreign import javascript" syntax)
- [ ] Create install script with nix that uses mcabal to install all deps. and builds miso. Place miso's MicroHS build under CI using both emscripten and native
- [ ] Consider upstreaming MicroHs into nixpkgs proper (with pkgsCross support)

For JSFFI support (for browser targets) w/ emscripten consider the following docs

  - https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#calling-javascript-from-c-c
- Add src/ because the RTS gets compiled during compilation.
- Clean-up bash
Use this for CI testing (for now, until miso builds).
- Use compiler generated targets from Makefile
We need to ensure we support QuickJS on Darwin/OSX
NixOS/nixpkgs#139943 (comment)

We need to ensure that ~/.emscripten_cache is created, populated and EM_CACHE is
set.
@dmjio dmjio force-pushed the micro-miso branch 2 times, most recently from 8c59252 to ffdf202 Compare April 15, 2025 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant