使用 C++ 和 stack/cabal 编译 Haskell 包

Compiling Haskell package using c++ and stack/cabal

有一个包裁剪器 http://hackage.haskell.org/package/clipper 我想用它来检测复杂多边形的交集。它是 C++ 包的 FFI。如果你 运行

它工作正常

cabal build --with-gcc=/usr/bin/g++

但并非如此。有什么方法可以将该 gcc 选项放入 cabal 文件中,或者让我的堆栈项目与 g++ 建立依赖关系?

由于某种原因设置 $PATH 不起作用:

%  cabal build --with-gcc=g++
Building clipper-0.0.1...
Preprocessing library clipper-0.0.1...
[1 of 1] Compiling Algebra.Clipper  ( dist/build/Algebra/Clipper.hs, dist/build/Algebra/Clipper.o )
In-place registering clipper-0.0.1...

%  PATH=$(pwd):$PATH gcc
g++: fatal error: no input files
compilation terminated.

%  PATH=$(pwd):$PATH cabal build 
Building clipper-0.0.1...
Preprocessing library clipper-0.0.1...
In file included from Clipper.hsc:27:0:
cbits/clipper.hpp:29:18: fatal error: vector: Dosiero aŭ dosierujo ne ekzistas
compilation terminated.
compiling dist/build/Algebra/Clipper_hsc_make.c failed (exit code 1)
command was: /usr/bin/gcc -c dist/build/Algebra/Clipper_hsc_make.c -o dist/build/Algebra/Clipper_hsc_make.o -fno-stack-protector -D__GLASGOW_HASKELL__=710 -Dlinux_BUILD_OS=1 -Dx86_64_BUILD_ARCH=1 -Dlinux_HOST_OS=1 -Dx86_64_HOST_ARCH=1 -Icbits -Idist/build/autogen -include dist/build/autogen/cabal_macros.h -I/usr/local/haskell/ghc-7.10.2-x86_64/lib/ghc-7.10.2/base_GDytRqRVSUX7zckgKqJjgw/include -I/usr/local/haskell/ghc-7.10.2-x86_64/lib/ghc-7.10.2/integ_2aU3IZNMF9a7mQ0OzsZ0dS/include -I/usr/local/haskell/ghc-7.10.2-x86_64/lib/ghc-7.10.2/include -I/usr/local/haskell/ghc-7.10.2-x86_64/lib/ghc-7.10.2/include/

同样,按照下面@ErikR 的建议更改 Setup.hs 也没有帮助。

% runghc Setup.hs build
"Hello, I am running"
fomg
BuildFlags
  { buildProgramPaths = []
  , buildProgramArgs = []
  , buildDistPref = Flag "dist"
  , buildVerbosity = Flag Normal
  , buildNumJobs = NoFlag
  , buildArgs = []
 }
fimg
BuildFlags
  { buildProgramPaths = [ ( "gcc" , "/usr/bin/g++" ) ]
  , buildProgramArgs = []
  , buildDistPref = Flag "dist"
  , buildVerbosity = Flag Normal
  , buildNumJobs = NoFlag
  , buildArgs = []
  }
Building clipper-0.0.1...
Preprocessing library clipper-0.0.1...
In file included from Clipper.hsc:27:0:
(etc)

请注意,它在 buildHook 行崩溃,因此为了打印标志,我需要更改顺序。

试试这个更简单的自定义 Setup.hs 文件:

import Distribution.Simple
import System.Environment

main = do
  args <- getArgs
  defaultMainArgs $ ["--with-gcc=c++"] ++ args

原答案

一些想法:

(1) 创建一个名为 gcc 的包装器脚本,它会调用 g++。将脚本放在您的 PATH 的早期,以便 运行ning gcc 将 运行 您的脚本而不是真正的 gcc。也许您只有在构建 clipper 程序包时才会使用此包装器脚本。

(2)写自定义Setup.hs.

  1. 将clipper.cabal文件中的build-type字段修改为Custom而不是Simple
  2. 将 Setup.hs 文件替换为:

    import Distribution.Simple
    import Distribution.Simple.Setup
    import Distribution.Simple.LocalBuildInfo
    import Distribution.PackageDescription
    import Text.Show.Pretty
    
    main = do
      let hooks = simpleUserHooks
    
          myBuild :: PackageDescription -> LocalBuildInfo -> UserHooks -> BuildFlags -> IO ()
          myBuild pkgd buildinfo uhooks flags = do
            putStrLn $ ppShow flags
            let flags' = flags { buildProgramPaths = [("gcc","g++")] ++ (buildProgramPaths flags) }
            buildHook hooks pkgd buildinfo uhooks flags'
            putStrLn $ ppShow flags'
          hooks' = hooks { buildHook = myBuild }
    
      defaultMainWithHooks hooks'
    

注意:Text.Show.Pretty 的导入不是必需的,因此您可以删除它,如果您没有安装它,ppShow 会调用。

上面的Setup.hs应该和用--with-gcc=g++调用cabal有同样的效果。