我有一个非常特殊的依赖情况,我想把它打包在一个StackCabal包中:我需要构建并运行我的程序,以获得代码生成器的输入,而代码生成器的输出需要链接到......我的程序。
好吧,那么用更具体的术语来说,以下是手工操作的步骤。
stack build
安装所有的依赖关系 并建立所有不使用Verilator的可执行文件。stack exec phase1
运行第一阶段,生成Verilog文件和Clash文件。.manifest
文件。.manifest
文件,并产生C++代码和一个由步骤2产生的 Makefile
可用于驱动Verilator。Makefile
在步骤3中生成。Makefile
Makefile
,产生一个二进制库stack build --flag phase2
构建第二个可执行文件。这个可执行文件包括 .hsc
文件,处理步骤2中产生的头文件,并链接到步骤42中产生的C++库。我想把这个过程自动化,这样我就可以直接执行 stack build
而这一切都会发生在幕后。我从哪里开始呢?
为了说明整个过程,这里有一个自带的模型。
package.yaml
name: clashilator-model
version: 0
category: acme
dependencies:
- base
- directory
source-dirs:
- src
flags:
phase2:
manual: True
default: False
executables:
phase1:
main: phase1.hs
phase2:
main: phase2.hs
when:
- condition: flag(phase2)
then:
source-dirs:
- src
- _build/generated
extra-libraries: stdc++
extra-lib-dirs: _build/compiled
ghc-options:
-O3 -fPIC -pgml g++
-optl-Wl,--allow-multiple-definition
-optl-Wl,--whole-archive -optl-Wl,-Bstatic
-optl-Wl,-L_build/compiled -optl-Wl,-lImpl
-optl-Wl,-Bdynamic -optl-Wl,--no-whole-archive
build-tools: hsc2hs
include-dirs: _build/generated
else:
buildable: false
src/phase1.hs
import System.Directory
main :: IO ()
main = do
createDirectoryIfMissing True "_build/generated"
writeFile "_build/generated/Interface.hsc" hsc
writeFile "_build/generated/Impl.h" h
writeFile "_build/generated/Impl.c" c
writeFile "_build/Makefile" makeFile
makeFile = unlines
[ "compiled/libImpl.a: compiled/Impl.o"
, "\trm -f $@"
, "\tmkdir -p compiled"
, "\tar rcsT $@ $^"
, ""
, "compiled/Impl.o: generated/Impl.c generated/Impl.h"
, "\tmkdir -p compiled"
, "\t$(COMPILE.c) $(OUTPUT_OPTION) $<"
]
hsc = unlines
[ "module Interface where"
, "import Foreign.Storable"
, "import Foreign.Ptr"
, ""
, "data FOO = FOO Int deriving Show"
, ""
, "#include \"Impl.h\""
, ""
, "foreign import ccall unsafe \"bar\" bar :: Ptr FOO -> IO ()"
, "instance Storable FOO where"
, " alignment _ = #alignment FOO"
, " sizeOf _ = #size FOO"
, " peek ptr = FOO <$> (#peek FOO, fd1) ptr"
, " poke ptr (FOO x) = (#poke FOO, fd1) ptr x"
]
h = unlines
[ "#pragma once"
, ""
, "typedef struct{ int fd1; } FOO;"
]
c = unlines
[ "#include \"Impl.h\""
, "#include <stdio.h>"
, ""
, "void bar(FOO* arg)"
, "{ printf(\"bar: %d\\n\", arg->fd1); }"
]
src/phase2.hs
import Interface
import Foreign.Marshal.Utils
main :: IO ()
main = with (FOO 42) bar
stack build
stack run phase1
make -C _build
stack build --flag clashilator-model:phase2
stack exec phase2
Setup.hs
.在 buildHook
我基本上是随心所欲 phase1
应该做的事情(而不是把它留在一个地方。phase1
可执行文件),将所有生成的文件放在低于 buildDir
的 LocalBuildInfo
参数。这些生成的文件是C++源文件和一个 .hsc
文件。
然后我运行 make
在正确的目录下,产生一些 libFoo.a
.
还在 buildHook
现在,有趣的部分开始了:编辑 Executable
在 PackageDescription
.
我加上 hsc
文件的位置到 hsSourceDirs
和模块本身 otherModules
. 由于 hsc2hs
需要访问生成的C++头文件,我也将正确的目录添加到了 includeDirs
. 对于图书馆本身,我添加到 extraLibDirs
并编辑 options
以静态链接到 libFoo.a
,通过直接向链接器传递标志。
所有这一切的结果是一套修改后的 Executable
的,我把它放回 PackageDescription
在将其传递给默认的 buildHook
. 然后那个人就跑了 hsc2hs
和 ghc
编制和链接 phase2
可执行文件。
我已经把一个 Github上的完整示例项目. 你看 Setup.hs
和 clashilator/src/Clash/Clashilator/Setup.hs
来看看它的运作;特别是,这里是编辑的。Executable
在 PackageDescription
:
-- TODO: Should we also edit `Library` components?
buildVerilator :: LocalBuildInfo -> BuildFlags -> [FilePath] -> String -> IO (Executable -> Executable)
buildVerilator localInfo buildFlags srcDir mod = do
let outDir = buildDir localInfo
(verilogDir, manifest) <- clashToVerilog localInfo buildFlags srcDir mod
let verilatorDir = "_verilator"
Clashilator.generateFiles (".." </> verilogDir) (outDir </> verilatorDir) manifest
-- TODO: bake in `pkg-config --cflags verilator`
() <- cmd (Cwd (outDir </> verilatorDir)) "make"
let incDir = outDir </> verilatorDir </> "src"
libDir = outDir </> verilatorDir </> "obj"
lib = "VerilatorFFI"
let fixupOptions f (PerCompilerFlavor x y) = PerCompilerFlavor (f x) (f y)
linkFlags =
[ "-fPIC"
, "-pgml", "g++"
, "-optl-Wl,--whole-archive"
, "-optl-Wl,-Bstatic"
, "-optl-Wl,-l" <> lib
, "-optl-Wl,-Bdynamic"
, "-optl-Wl,--no-whole-archive"
]
fixupExe = foldr (.) id $
[ includeDirs %~ (incDir:)
, extraLibDirs %~ (libDir:)
, options %~ fixupOptions (linkFlags++)
, hsSourceDirs %~ (incDir:)
, otherModules %~ (fromString lib:)
]
return fixupExe