c++windowshaskellffihaskell-stack

Writing a static cpp library for a haskell stack project


I'm trying to write a debugger for windows with haskell stack. Because there is no other way to pass the right flags to CreateProcess using haskell packages I've decided to write a wrapper for it. Here is what I've done:

// process.h
#pragma once

// Headers
#include <Windows.h>

// Functions
HANDLE CreateDebuggedProcess(LPCSTR lpApplicationName);

And

//process.cpp

#include <Windows.h>

#include "process.h"

HANDLE CreateDebuggedProcess(LPCSTR lpApplicationName)
{
    STARTUPINFO startup_info = { 0 };
    PROCESS_INFORMATION process_information = { 0 };

    startup_info.cb = sizeof(startup_info);

    if (!CreateProcessA(
        lpApplicationName,
        NULL,
        NULL,
        NULL,
        FALSE,
        DEBUG_ONLY_THIS_PROCESS,
        NULL,
        NULL,
        &startup_info,
        &process_information
    ))
    {
        return INVALID_HANDLE_VALUE;
    }

    return process_information.hProcess;
}

which I compiled to DebuggedProcess.lib On the Haskell project I have:

{-# LANGUAGE ForeignFunctionInterface #-}

module Main where

import System.Win32.Types
import Foreign.C.String

main :: IO ()
main = do
    withCString "cmd.exe" c_CreateDebuggedProcess
    putStrLn "created process"

foreign import ccall "DebuggedProcess.lib CreateDebuggedProcess"
    c_CreateDebuggedProcess :: LPCSTR -> IO HANDLE

I've added the .lib file to the stack path (validated with stack path --extra-include-dirs) and added extra-libraries: [DebuggedProcess].

Yet I get the following error:

>stack build
Building all executables for `tape' once. After a successful build of all of them, only specified executables will be rebuilt.
tape-0.1.0.0: configure (lib + exe)
Configuring tape-0.1.0.0...
Cabal-simple_Z6RU0evB_2.2.0.1_ghc-8.4.3.exe: Missing dependencies on foreign
libraries:
* Missing (or bad) C libraries: DebuggedProcess, DebuggedProcess,
DebuggedProcess
This problem can usually be solved by installing the system packages that
provide these libraries (you may need the "-dev" versions). If the libraries
are already installed but in a non-standard location then you can use the
flags --extra-include-dirs= and --extra-lib-dirs= to specify where they are.If
the library files do exist, it may contain errors that are caught by the C
compiler at the preprocessing stage. In this case you can re-run configure
with the verbosity flag -v3 to see the error messages.


--  While building custom Setup.hs for package tape-0.1.0.0 using:
    C:\sr\setup-exe-cache\x86_64-windows\Cabal-simple_Z6RU0evB_2.2.0.1_ghc-8.4.3.exe --builddir=.stack-work\dist\7d103d30 configure --with-ghc=C:\Users\yotam\AppData\Local\Programs\stack\x86_64-windows\ghc-8.4.3\bin\ghc.EXE --with-ghc-pkg=C:\Users\yotam\AppData\Local\Programs\stack\x86_64-windows\ghc-8.4.3\bin\ghc-pkg.EXE --user --package-db=clear --package-db=global --package-db=C:\sr\snapshots\68fc3218\pkgdb --package-db=D:\tape\.stack-work\install\db7ce97c\pkgdb --libdir=D:\tape\.stack-work\install\db7ce97c\lib --bindir=D:\tape\.stack-work\install\db7ce97c\bin --datadir=D:\tape\.stack-work\install\db7ce97c\share --libexecdir=D:\tape\.stack-work\install\db7ce97c\libexec --sysconfdir=D:\tape\.stack-work\install\db7ce97c\etc --docdir=D:\tape\.stack-work\install\db7ce97c\doc\tape-0.1.0.0 --htmldir=D:\tape\.stack-work\install\db7ce97c\doc\tape-0.1.0.0 --haddockdir=D:\tape\.stack-work\install\db7ce97c\doc\tape-0.1.0.0 --dependency=Win32=Win32-2.6.1.0 --dependency=base=base-4.11.1.0 --extra-include-dirs=C:\Users\yotam\AppData\Local\Programs\stack\x86_64-windows\msys2-20180531\mingw64\include --extra-include-dirs=D:\tape\lib --extra-lib-dirs=C:\Users\yotam\AppData\Local\Programs\stack\x86_64-windows\msys2-20180531\mingw64\bin --extra-lib-dirs=C:\Users\yotam\AppData\Local\Programs\stack\x86_64-windows\msys2-20180531\mingw64\lib --enable-tests --enable-benchmarks
    Process exited with code: ExitFailure 1

I am clueless on how to fix this. Any help would be apreciated


Solution

  • Just use c-sources: in your .cabal file to make your "library" statically compile into the Haskell executable. Here is the example: https://github.com/commercialhaskell/stack/pull/4238/files