Windows, doctest and auxiliary FFI wrappers?

The Network.DNS library contains a bit of helper C-code for determining the default DNS servers on Windows. This code is linked in by Cabal, when building object code for the library on Windows. The library also has 140 "doctest" examples in the haddock comments, which I'd like to test on Windows via AppVeyor. I'm not having much luck making the dependency on the cbits visible to the GHCi invocations executed by "Test.Doctest". Does anyone have some experience getting this sort of thing to work? What I've tried: 1. Add "-liphlpapi" into the "doctest" command argument list, this proved necessary, but not sufficient. 2. Build a separate internal library (supported in Cabal 2.0 and later), which is a dependency of the main library and links in the C glue code, but this proved to be a significant rabbit hole... In fact approach 2 seems like the more promising one, but it runs into a substantial obstacles: a. Building internal libraries requires a "Simple" Cabal build, but the cabal-doctest package which automates compiling doctests, requires a "Custom" build type. :-( b. A Simple build, means abandoning cabal-doctest, and using "doctest" directly, but the new sandbox builds place package artefacts in locations that the command-line invoked by "Test.DocTest.doctest" does not know how to find. It appears that the "Custom" build from cabal-doctest is there for a good reason: it hooks into Cabal's build process, and has access to enough metatata, to generate wrapper code that invokes doctest with an argument list of the form: -XCPP -i -i<DISTDIR>/build/<ARCHDIR>/<GHCVER>/<PKG>-<VER>/build/autogen -i<DISTDIR>/build/<ARCHDIR>/<GHCVER>/<PKG>-<VER>/build -i<SRCDIR> -package-env=- -hide-all-packages -no-user-package-db -package-db=<CABALGLOBALSTORE>/<GHCVER>/package.db -package-db=<DISTDIR>/packagedb/<GHCVER> -package-db=<DISTDIR>/build/<ARCHDIR>/<GHCVER>/<PKG>-<VER>/package.conf.inplace -optP-include -optP<DISTDIR>/build/<ARCHDIR>/<GHCVER>/<PKG>-<VER>/build/autogen/cabal_macros.h ...visible packages... ...modules to doctest... which are needed for the extracted doctest fragments to find their dependencies. Hard-coding such arguments into the "Simple" build seems to work, but is hardly maintainable. I've not yet found an interface in the Cabal library that would divulge these magic strings as a function of the "pkg.cabal" file, something of the shape: cabalMagicGhcArgs :: FilePath -> IO [String] would make it possible to compute the arguments at runtime (rather than build-time) in a Simple build, without having to use a Custom build to machine-generate wrapper code. Other than the above, I'm out of ideas for now, any help much appreciated. -- Viktor.

On Tue, Oct 15, 2019 at 03:47:46AM -0400, Viktor Dukhovni wrote:
What I've tried:
2. Build a separate internal library (supported in Cabal 2.0 and later), which is a dependency of the main library and links in the C glue code, but this proved to be a significant rabbit hole...
Following up on my own post, for the record I should note that in fact the second approach work out with GHC 8.[468].x and Cabal 2.x through the magic of .ghc.environment.* files. With Cabal 3.0, these are no longer written by default, but it suffices to create a cabal.project file containing just: packages: . write-ghc-environment-files: always The crux of my issue seems to be that this does not work with older GHC releases (e.g. 8.0.2 and 8.2.2) and perhaps the flag was not set by the Cabal used in AppVeyor on Windows or some other nit got in the way. I now have doctests compiling and running with GHC 8.[468] on Linux and so far just 8.6.5 on Windows. The custom C-code dependency was, as hoped for, satisfied by the addition of the internal library package to the doctest command arguments, I only had to hardcode the format of the "inplace" package-id. https://github.com/kazu-yamamoto/dns/blob/master/test2/doctests.hs Sadly, on Windows the doctests mostly end up hanging and timing out after an hour, but that's a separate issue, perhaps https://gitlab.haskell.org/ghc/ghc/issues/16917 -- Viktor.
participants (1)
-
Viktor Dukhovni