I wanted to check that Stack can handle Cabal’s foreign-library
stanza, so I tried to create a minimal example, as below. I read the Cabal guide and this blog post.
foo.cabal
cabal-version: 2.0
name: foo
version: 0.1.0.0
build-type: Simple
foreign-library myForeignLib
type: native-shared
options: standalone
other-modules:
Lib
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
default-language: Haskell2010
I am on Windows, so I understand options: standalone
is required, to avoid a dependency on the Haskell runtime library libHSrts
.
src\Lib.hs
module Lib
( someFunc
) where
someFunc :: IO ()
someFunc = putStrLn "someFunc"
When I try stack --verbose build --cabal-verbose
(for full information), it builds but fails at the linking stage, with (extracts, reformatted for clarity):
2023-06-17 20:51:52.244261: [info] foo> configure
2023-06-17 20:51:52.244261: [debug] Run process within D:\Users\mike\Code\Haskell\foo\: C:\sr\setup-exe-cache\x86_64-windows\Cabal-simple_9p6GVs8J_3.6.3.0_ghc-9.2.8.exe
--verbose=2
--builddir=.stack-work\dist\8a54c84f
configure
--with-ghc=C:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\ghc-9.2.8\bin\ghc-9.2.8.exe
--with-ghc-pkg=C:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\ghc-9.2.8\bin\ghc-pkg-9.2.8.exe
--user
--package-db=clear
--package-db=global
--package-db=C:\sr\snapshots\4ae63981\pkgdb
--package-db=D:\Users\mike\Code\Haskell\foo\.stack-work\install\3722b7b2\pkgdb
--libdir=D:\Users\mike\Code\Haskell\foo\.stack-work\install\3722b7b2\lib
--bindir=D:\Users\mike\Code\Haskell\foo\.stack-work\install\3722b7b2\bin
--datadir=D:\Users\mike\Code\Haskell\foo\.stack-work\install\3722b7b2\share
--libexecdir=D:\Users\mike\Code\Haskell\foo\.stack-work\install\3722b7b2\libexec
--sysconfdir=D:\Users\mike\Code\Haskell\foo\.stack-work\install\3722b7b2\etc
--docdir=D:\Users\mike\Code\Haskell\foo\.stack-work\install\3722b7b2\doc\foo-0.1.0.0
--htmldir=D:\Users\mike\Code\Haskell\foo\.stack-work\install\3722b7b2\doc\foo-0.1.0.0
--haddockdir=D:\Users\mike\Code\Haskell\foo\.stack-work\install\3722b7b2\doc\foo-0.1.0.0
--dependency=base=base-4.16.4.0
--extra-include-dirs=C:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\msys2-20221216\mingw64\include
--extra-lib-dirs=C:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\msys2-20221216\mingw64\lib
--extra-lib-dirs=C:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\msys2-20221216\mingw64\bin
--exact-configuration
--ghc-option=-fhide-source-paths
...
2023-06-17 20:51:52.737479: [info] foo> Configuring foo-0.1.0.0...
...
2023-06-17 20:51:53.556129: [info] foo> build
2023-06-17 20:51:53.556129: [debug] Run process within D:\Users\mike\Code\Haskell\foo\: C:\sr\setup-exe-cache\x86_64-windows\Cabal-simple_9p6GVs8J_3.6.3.0_ghc-9.2.8.exe
--verbose=2
--builddir=.stack-work\dist\8a54c84f
build
--ghc-options " -fdiagnostics-color=always"
2023-06-17 20:51:53.592638: [info] foo> Component build order: foreign library 'myForeignLib'
...
2023-06-17 20:51:53.619640: [info] foo> "C:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\ghc-9.2.8\bin\ghc-9.2.8.exe" "--make" "-no-link" "-fbuilding-cabal-package" "-O" "-static" "-outputdir" ".stack-work\dist\8a54c84f\build\myForeignLib\myForeignLib-tmp" "-odir" ".stack-work\dist\8a54c84f\build\myForeignLib\myForeignLib-tmp" "-hidir" ".stack-work\dist\8a54c84f\build\myForeignLib\myForeignLib-tmp" "-stubdir" ".stack-work\dist\8a54c84f\build\myForeignLib\myForeignLib-tmp" "-i" "-i.stack-work\dist\8a54c84f\build\myForeignLib\myForeignLib-tmp" "-isrc" "-i.stack-work\dist\8a54c84f\build\myForeignLib\autogen" "-i.stack-work\dist\8a54c84f\build\global-autogen" "-I.stack-work\dist\8a54c84f\build\myForeignLib\autogen" "-I.stack-work\dist\8a54c84f\build\global-autogen" "-I.stack-work\dist\8a54c84f\build\myForeignLib\myForeignLib-tmp" "-IC:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\msys2-20221216\mingw64\include" "-optP-include" "-optP.stack-work\dist\8a54c84f\build\myForeignLib\autogen\cabal_macros.h" "-hide-all-packages" "-Wmissing-home-modules" "-no-user-package-db" "-package-db" "C:\sr\snapshots\4ae63981\pkgdb" "-package-db" "D:\Users\mike\Code\Haskell\foo\.stack-work\install\3722b7b2\pkgdb" "-package-db" ".stack-work\dist\8a54c84f\package.conf.inplace" "-package-id" "base-4.16.4.0" "-XHaskell2010" "Lib" "-fhide-source-paths" "-fdiagnostics-color=always"
2023-06-17 20:51:53.670149: [info] foo> [1 of 1] Compiling Lib
2023-06-17 20:51:53.847663: [info] foo> Linking...
2023-06-17 20:51:53.848663: [info] foo> "C:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\ghc-9.2.8\bin\ghc-9.2.8.exe"
"--make"
"-fbuilding-cabal-package"
"-O"
"-shared"
"-static"
"-fPIC"
"-outputdir" ".stack-work\dist\8a54c84f\build\myForeignLib\myForeignLib-tmp"
"-odir" ".stack-work\dist\8a54c84f\build\myForeignLib\myForeignLib-tmp"
"-hidir" ".stack-work\dist\8a54c84f\build\myForeignLib\myForeignLib-tmp"
"-stubdir" ".stack-work\dist\8a54c84f\build\myForeignLib\myForeignLib-tmp"
"-i"
"-i.stack-work\dist\8a54c84f\build\myForeignLib\myForeignLib-tmp"
"-isrc"
"-i.stack-work\dist\8a54c84f\build\myForeignLib\autogen"
"-i.stack-work\dist\8a54c84f\build\global-autogen"
"-I.stack-work\dist\8a54c84f\build\myForeignLib\autogen"
"-I.stack-work\dist\8a54c84f\build\global-autogen"
"-I.stack-work\dist\8a54c84f\build\myForeignLib\myForeignLib-tmp"
"-IC:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\msys2-20221216\mingw64\include"
"-optP-include"
"-optP.stack-work\dist\8a54c84f\build\myForeignLib\autogen\cabal_macros.h"
"-lHSrts"
"-LC:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\msys2-20221216\mingw64\lib"
"-LC:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\msys2-20221216\mingw64\bin"
"-LC:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\ghc-9.2.8\lib\x86_64-windows-ghc-9.2.8\rts-1.0.2"
"-no-hs-main"
"-hide-all-packages"
"-Wmissing-home-modules"
"-no-user-package-db"
"-package-db" "C:\sr\snapshots\4ae63981\pkgdb"
"-package-db" "D:\Users\mike\Code\Haskell\foo\.stack-work\install\3722b7b2\pkgdb"
"-package-db" ".stack-work\dist\8a54c84f\package.conf.inplace"
"-package-id" "base-4.16.4.0"
"-XHaskell2010"
"Lib"
"-o" ".stack-work\dist\8a54c84f\build\myForeignLib\myForeignLib.dll"
"-fhide-source-paths"
"-fdiagnostics-color=always"
2023-06-17 20:51:53.901273: [info] foo> [1 of 1] Compiling Lib [flags changed]
2023-06-17 20:51:54.057813: [info] foo> Linking .stack-work\dist\8a54c84f\build\myForeignLib\myForeignLib.dll ...
2023-06-17 20:51:54.161423: [warn] foo> C://Users//mike//AppData//Local//Programs//stack//x86_64-windows//ghc-9.2.8//mingw//bin/ld.exe: cannot find -lHSrts
2023-06-17 20:51:54.759920: [warn] foo> collect2.exe: error: ld returned 1 exit status
2023-06-17 20:51:54.768288: [warn] foo> `gcc.exe' failed in phase `Linker'. (Exit code: 1)
That is, the linker (ld.exe
) is complaining that it cannot find -lHSrts
and I note that Cabal (the library) is passing -lHSrts
to GHC 9.2.8. I thought the options: standalone
in the Cabal file was meant to make that unnecessary. I wonder is: Stack failing to pass something required to Cabal (the library) in the configure
step? (Cabal-simple_9p6GVs8J_3.6.3.0_ghc-9.2.8.exe
is the small exectuable that Stack uses to call Cabal (the library).)
My questions are this: (1) is there something wrong with the specification of my minimal example? If yes, I would be grateful if somebody could provide me with a minimal example known to work on Windows.(2) If the minimal example is correctly specified, can anybody give me a pointer as to why the options: standalone
in foo.cabal
might not be respected? Any help would be gratefully received.