I am trying to compile a haskell package without using cabal
Given a correct .conf file, this seems to work
cd src; ghc --make -dynamic -shared -fPIC -package-name adventlib System/IO/Advent.hs System/IO/Test.hs -osuf dyn_o -hisuf dyn_hi -o libHSadventlib-ghc8.6.5.so
cd src; ghc -c --make -package-name adventlib System/IO/Advent.hs System/IO/Test.hs
ar cqs src/libHSadventlib.a src/System/IO/*.o
ghc --make src/MainTest.hs
ghc --make -dynamic src/MainTest.hs -o src/MainTest_dyn
The last two lines test that I can compile an executable binary and link the library both statically and dynamically.
Reading the docs it seems that it should be possible to use -dynamic-too to combine the first two lines in a single ghc run. However, I haven't managed to make that work.
The next line produces the static and dynamic object files, but doesn't create the so file:
cd src; ghc -c --make -dynamic-too -fPIC -package-name adventlib System/IO/Advent.hs System/IO/Test.hs
I can link the so file afterwards with ghc, but then I need to add all the package dependencies manually, missing much of the benefit of using --make
If I remove the -c flag, to make ghc run the link stage like this
cd src; ghc --make -shared -dynamic-too -fPIC -package-name adventlib System/IO/Advent.hs System/IO/Test.hs -o libHSadventlib.a -dyno libHSadventlib-ghc8.6.5.so
Then it seems to fail linking the static library (I guess it is trying to link it as dynamic but using static objects):
cd src; ghc --make -shared -dynamic-too -fPIC -package-name adventlib System/IO/Advent.hs System/IO/Test.hs -o libHSadventlib.a -dyno libHSadventlib-ghc8.6.5.so
[1 of 3] Compiling System.IO.Advent ( System/IO/Advent.hs, System/IO/Advent.o )
[2 of 3] Compiling System.IO.TestInternal ( System/IO/TestInternal.hs, System/IO/TestInternal.o )
[3 of 3] Compiling System.IO.Test ( System/IO/Test.hs, System/IO/Test.o )
Linking libHSadventlib.a ...
/nix/store/0hr45a0pzlh51hhcgynmfjpzff9d3ddv-binutils-2.31.1/bin/ld: /nix/store/gdpi6mrz1wcgmvpnfm9i9la9lpsb8lag-unliftio-0.2.12/lib/ghc-8.6.5/x86_64-linux-ghc-8.6.5/unliftio-0.2.12-Au2Yw1nUjiS94bY0JG3imp/libHSunliftio-0.2.12-Au2Yw1nUjiS94bY0JG3imp.a(Environment.o): relocation R_X86_64_32S against undefined symbol `stg_ap_p_info' can not be used when making a shared object; recompile with -fPIC
/nix/store/0hr45a0pzlh51hhcgynmfjpzff9d3ddv-binutils-2.31.1/bin/ld: /nix/store/7dx9j6hiscwr1a2nq9bjj91p33s9nqgg-unliftio-core-0.1.2.0/lib/ghc-8.6.5/x86_64-linux-ghc-8.6.5/unliftio-core-0.1.2.0-DmlZdkLzX278vkyONsp8WQ/libHSunliftio-core-0.1.2.0-DmlZdkLzX278vkyONsp8WQ.a(Unlift.o): relocation R_X86_64_32S against `.text.unliftiozmcorezm0zi1zi2zi0zmDmlZZdkLzzX278vkyONsp8WQ_ControlziMonadziIOziUnlift_zdp1MonadUnliftIO_info' can not be used when making a shared object; recompile with -fPIC
... etc ...
Am I missing something or doesn't dynamic-too work to create shared libraries?
I have the full working experiment here , for reference.