I have a very peculiar dependency situation that I would like to package up in a single Stack/Cabal package: I need to build and run my program to get the input to a code-generator which produces output that needs to be linked in to... my program.
OK so in more concrete terms, here are the steps manually:
stack build
to install all dependencies, and build all non-Verilator-using executables.stack exec phase1
to run the first phase which generates, among other things, a Verilog file and a Clash .manifest
file..manifest
file from step 2, and produces C++ code and a Makefile
that can be used to drive Verilator.Makefile
generated in step 3:
Makefile
Makefile
, which produces a binary librarystack build --flag phase2
builds the second executable. This executable includes .hsc
files that process headers produced in step 2, and it links to the C++ libraries produced in step 4/2.I would like to automate this so that I can just run stack build
and all this would happen behind the scenes. Where do I even start?!
To illustrate the whole process, here is a self-contained model:
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
.In buildHook
, I basically do whatever phase1
was supposed to do (instead of leaving it in a phase1
executable), putting all generated files in places below the buildDir
of the LocalBuildInfo
argument. These generated files are C++ source files and an .hsc
file.
I then run make
in the right directory, producing some libFoo.a
.
Still in buildHook
, now the fun part starts: editing the Executable
s in the PackageDescription
.
I add the hsc
file's location to hsSourceDirs
, and the module itself to otherModules
. Since hsc2hs
requires access to the generated C++ headers, I also add the right directory to includeDirs
. For the library itself, I add to extraLibDirs
and edit options
to link statically to libFoo.a
, by passing flags directly to the linker.
The result of all this is a modified set of Executable
s, which I put back into the PackageDescription
before passing it to the default buildHook
. That one then runs hsc2hs
and ghc
to compile and link the phase2
executables.
I have put a full example project on Github. Look at Setup.hs
and clashilator/src/Clash/Clashilator/Setup.hs
to see this in action; in particular, here is the editing of the Executable
s in the 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