From 7c8279408e6a359ae63d4690ba6dad57e06de89f Mon Sep 17 00:00:00 2001 From: Hamish Mackenzie Date: Fri, 31 May 2024 16:17:04 +1200 Subject: [PATCH 1/2] Minimal TH test for windows DLL --- test/default.nix | 1 + test/th-dlls-minimal/apps/Main.hs | 9 ++++ test/th-dlls-minimal/cabal.project | 3 ++ test/th-dlls-minimal/default.nix | 43 ++++++++++++++++++++ test/th-dlls-minimal/src/Lib.hs | 8 ++++ test/th-dlls-minimal/test-clib/Makefile | 16 ++++++++ test/th-dlls-minimal/test-clib/test.c | 5 +++ test/th-dlls-minimal/test-lib/src/MyLib.hs | 11 +++++ test/th-dlls-minimal/test-lib/test-lib.cabal | 15 +++++++ test/th-dlls-minimal/th-dlls-minimal.cabal | 21 ++++++++++ 10 files changed, 132 insertions(+) create mode 100644 test/th-dlls-minimal/apps/Main.hs create mode 100644 test/th-dlls-minimal/cabal.project create mode 100644 test/th-dlls-minimal/default.nix create mode 100644 test/th-dlls-minimal/src/Lib.hs create mode 100644 test/th-dlls-minimal/test-clib/Makefile create mode 100644 test/th-dlls-minimal/test-clib/test.c create mode 100644 test/th-dlls-minimal/test-lib/src/MyLib.hs create mode 100644 test/th-dlls-minimal/test-lib/test-lib.cabal create mode 100644 test/th-dlls-minimal/th-dlls-minimal.cabal diff --git a/test/default.nix b/test/default.nix index 066d3a5aa..c4112872d 100644 --- a/test/default.nix +++ b/test/default.nix @@ -211,6 +211,7 @@ let githash = haskell-nix.callPackage ./githash { inherit compiler-nix-name evalPackages; testSrc = testSrcWithGitDir; }; c-ffi = callTest ./c-ffi { inherit util; }; th-dlls = callTest ./th-dlls { inherit util; }; + th-dlls-minimal = callTest ./th-dlls-minimal { inherit util; }; external-static-plugin = callTest ./external-static-plugin {}; exe-dlls = callTest ./exe-dlls { inherit util; }; exe-lib-dlls = callTest ./exe-lib-dlls { inherit util; }; diff --git a/test/th-dlls-minimal/apps/Main.hs b/test/th-dlls-minimal/apps/Main.hs new file mode 100644 index 000000000..9d2b297f0 --- /dev/null +++ b/test/th-dlls-minimal/apps/Main.hs @@ -0,0 +1,9 @@ +{-# LANGUAGE TemplateHaskell #-} +module Main where + +import Language.Haskell.TH + +main :: IO () +main = putStrLn "Hello, Haskell!" + +[d|y = 0|] diff --git a/test/th-dlls-minimal/cabal.project b/test/th-dlls-minimal/cabal.project new file mode 100644 index 000000000..1bc58ef68 --- /dev/null +++ b/test/th-dlls-minimal/cabal.project @@ -0,0 +1,3 @@ +packages: + ./. + ./test-lib diff --git a/test/th-dlls-minimal/default.nix b/test/th-dlls-minimal/default.nix new file mode 100644 index 000000000..23c56c1b1 --- /dev/null +++ b/test/th-dlls-minimal/default.nix @@ -0,0 +1,43 @@ +# Test building TH code that needs DLLs when cross compiling for windows +{ stdenv, lib, util, project', haskellLib, recurseIntoAttrs, testSrc, compiler-nix-name, evalPackages, buildPackages }: + +with lib; + +let + project = externalInterpreter: project' { + inherit compiler-nix-name evalPackages; + src = testSrc "th-dlls-minimal"; + cabalProjectLocal = builtins.readFile ../cabal.project.local; + modules = [ + ({pkgs, ...}: { + packages.test-lib.components.library.libs = mkForce [ + (pkgs.stdenv.mkDerivation { + name = "test-clib"; + version = "1.0"; + src = testSrc "th-dlls-minimal/test-clib"; + }) + ]; + }) + ({pkgs, ...}: lib.optionalAttrs externalInterpreter { + packages.th-dlls-minimal.components.library.ghcOptions = [ "-fexternal-interpreter" ]; + })]; + }; + + packages = (project false).hsPkgs; + packages-ei = (project true).hsPkgs; + +in recurseIntoAttrs { + # This test is just for windows currently (the full th-dlls test runs on other platforms) + meta.disabled = !stdenv.hostPlatform.isWindows; + + ifdInputs = { + inherit (project true) plan-nix; + }; + + build = packages.th-dlls-minimal.components.library; + build-profiled = packages.th-dlls-minimal.components.library.profiled; + just-template-haskell = packages.th-dlls-minimal.components.exes.just-template-haskell; + build-ei = packages-ei.th-dlls-minimal.components.library; + build-profiled-ei = packages-ei.th-dlls-minimal.components.library.profiled; + just-template-haskell-ei = packages-ei.th-dlls-minimal.components.exes.just-template-haskell; +} diff --git a/test/th-dlls-minimal/src/Lib.hs b/test/th-dlls-minimal/src/Lib.hs new file mode 100644 index 000000000..ffd9b4fb3 --- /dev/null +++ b/test/th-dlls-minimal/src/Lib.hs @@ -0,0 +1,8 @@ +{-# LANGUAGE TemplateHaskell #-} +module Lib where + +import Control.Monad.IO.Class (liftIO) +import Language.Haskell.TH.Syntax (Exp(..), Lit(..)) +import MyLib (someFunc) + +a = $(liftIO (LitE . IntegerL . fromIntegral <$> someFunc)) diff --git a/test/th-dlls-minimal/test-clib/Makefile b/test/th-dlls-minimal/test-clib/Makefile new file mode 100644 index 000000000..9a19ba0bc --- /dev/null +++ b/test/th-dlls-minimal/test-clib/Makefile @@ -0,0 +1,16 @@ +test.o: test.c + $(CC) -o test.o -c test.c + +test.a: test.o + $(AR) -rs test-clib.a test.o + +test.dll: test.o + $(CC) -o test-clib.dll -shared test.o + +.PHONY: install +install: test.a test.dll + mkdir $(out)/bin + mkdir $(out)/lib + mv test-clib.dll $(out)/bin + mv test-clib.a $(out)/lib + diff --git a/test/th-dlls-minimal/test-clib/test.c b/test/th-dlls-minimal/test-clib/test.c new file mode 100644 index 000000000..fc089c6c0 --- /dev/null +++ b/test/th-dlls-minimal/test-clib/test.c @@ -0,0 +1,5 @@ +int f() +{ + return 42; +} + diff --git a/test/th-dlls-minimal/test-lib/src/MyLib.hs b/test/th-dlls-minimal/test-lib/src/MyLib.hs new file mode 100644 index 000000000..522cff440 --- /dev/null +++ b/test/th-dlls-minimal/test-lib/src/MyLib.hs @@ -0,0 +1,11 @@ +{-# LANGUAGE ForeignFunctionInterface #-} +module MyLib (someFunc) where + +import Foreign.C.Types (CInt(..)) + +foreign import ccall "f" c_f :: IO CInt + +someFunc :: IO CInt +someFunc = do + putStrLn "someFunc called" + c_f diff --git a/test/th-dlls-minimal/test-lib/test-lib.cabal b/test/th-dlls-minimal/test-lib/test-lib.cabal new file mode 100644 index 000000000..43a414447 --- /dev/null +++ b/test/th-dlls-minimal/test-lib/test-lib.cabal @@ -0,0 +1,15 @@ +cabal-version: >=1.10 +name: test-lib +version: 0.1.0.0 +license: PublicDomain +author: Hamish Mackenzie +maintainer: Hamish.K.Mackenzie@gmail.com +build-type: Simple + +library + build-depends: base + exposed-modules: MyLib + hs-source-dirs: src + extra-libraries: test-clib + default-language: Haskell2010 + diff --git a/test/th-dlls-minimal/th-dlls-minimal.cabal b/test/th-dlls-minimal/th-dlls-minimal.cabal new file mode 100644 index 000000000..f5fa06b8b --- /dev/null +++ b/test/th-dlls-minimal/th-dlls-minimal.cabal @@ -0,0 +1,21 @@ +cabal-version: >=1.10 +name: th-dlls-minimal +version: 0.1.0.0 +license: PublicDomain +author: Hamish Mackenzie +maintainer: Hamish.K.Mackenzie@gmail.com +build-type: Simple + +library + build-depends: base + , template-haskell + , test-lib + exposed-modules: Lib + hs-source-dirs: src + default-language: Haskell2010 + +executable just-template-haskell + build-depends: base, template-haskell + hs-source-dirs: apps + default-language: Haskell2010 + main-is: Main.hs From 2d530526c77fa0d50d87ba23bdde9c71b7b9dc83 Mon Sep 17 00:00:00 2001 From: Hamish Mackenzie Date: Tue, 4 Jun 2024 17:50:23 +1200 Subject: [PATCH 2/2] Use `--no-load-call` for windows cross compilation TH proxy & interpreter --- flake.lock | 6 +++--- overlays/mingw_w64.nix | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/flake.lock b/flake.lock index 64950e7da..129b68167 100644 --- a/flake.lock +++ b/flake.lock @@ -364,11 +364,11 @@ "iserv-proxy": { "flake": false, "locked": { - "lastModified": 1710581758, - "narHash": "sha256-UNUXGiKLGUv1TuQumV70rfjCJERP4w8KZEDxsMG0RHc=", + "lastModified": 1717479972, + "narHash": "sha256-7vE3RQycHI1YT9LHJ1/fUaeln2vIpYm6Mmn8FTpYeVo=", "owner": "stable-haskell", "repo": "iserv-proxy", - "rev": "50ea210590ab0519149bfd163d5ba199be925fb6", + "rev": "2ed34002247213fc435d0062350b91bab920626e", "type": "github" }, "original": { diff --git a/overlays/mingw_w64.nix b/overlays/mingw_w64.nix index b23fe4cd3..becc817aa 100644 --- a/overlays/mingw_w64.nix +++ b/overlays/mingw_w64.nix @@ -53,10 +53,10 @@ let # Not sure why this `unset` helps. It might avoids some kind of overflow issue. We see `wine` fail to start when building `cardano-wallet-cli` test `unit`. unset pkgsHostTargetAsString unset LINK_DLL_FOLDERS - WINEDLLOVERRIDES="winemac.drv=d" WINEDEBUG=warn-all,fixme-all,-menubuilder,-mscoree,-ole,-secur32,-winediag WINEPREFIX=$TMP ${wine}/bin/wine64 $REMOTE_ISERV/${interpreter.exeName} tmp $PORT $ISERV_ARGS & + WINEDLLOVERRIDES="winemac.drv=d" WINEDEBUG=warn-all,fixme-all,-menubuilder,-mscoree,-ole,-secur32,-winediag WINEPREFIX=$TMP ${wine}/bin/wine64 $REMOTE_ISERV/${interpreter.exeName} tmp $PORT --no-load-call $ISERV_ARGS & (>&2 echo "---| ${interpreter.exeName} should have started on $PORT") RISERV_PID="$!" - ISERV_TARGET=WINE ${iserv-proxy}/bin/iserv-proxy $@ 127.0.0.1 "$PORT" $PROXY_ARGS + ISERV_TARGET=WINE ${iserv-proxy}/bin/iserv-proxy $@ 127.0.0.1 "$PORT" --no-load-call $PROXY_ARGS (>&2 echo "---> killing ${interpreter.exeName}...") kill $RISERV_PID '';