I'm working on a project that's chosen CMake as its build tool. The project is made up of several executables and since a few months back a few of them are written in Haskell. We strongly wish all executables to show the same version number when called as foo --version. Ideally that version should be recorded in one place, and ideally that place should be the top-level CMakeLists.txt (this is where the source for all the other executables get it, via the use of CMake's configure_file function).
Is there some nice way of achieving this?
Some extra information that might be useful:
- The source for each executable lives in its own dir, with its own Cabal file.
- We use
stackto build, and there is a singlestack.yamlfile that points to all directories with Haskell code.
configure_file, it should work for Haskell as well, in much the same way you do it in C. You can have a field likecpp-options: -DLIB_VERSION="${LIB_VERSION}"(where${LIB_VERSION}is whichever variable identifies the version) in the prototype of your .cabal file, and then havelibVersion :: String; libVersion = LIB_VERSIONin some Haskell source file (in which-XCPPis enabled). You can do the same in the stack.yaml file with e.g.ghc-options: "$everything": -D.., which might be easier if you have many packages. - user2407038configure_file, as I see it, is that CMake likes to split building into a separate dir so the generated files end up out-of-source. This, unfortunately, isn't always so easy to deal with in Cabal+stack. Using CPP is something I didn't think of though, I'll look into passing the information via the build command instead of via a generated file. - Magnus--cpp-options=-D..on the command line to stack/cabal, which saves you from having to generatestack.yamlor*.cabalwith configure_file - I guess depending on your particular setup, this could be easier or harder. - user2407038configure_fileexactly where to put things, but I'd rather not go against the convention and put generated files in-source (it's already a point of contention that stack leaves stuff in-source when building). Also, I kind of like that my Haskell bits are complete after check-out, i.e. I don't have to remember running CMake to generate missing bits before opening Haskell source in my editor (which integrates with intero/stack). I know, both reasons aren't very good but a solution that fulfils them both would be nice. - Magnus-cpp-options=-D..on the command line in the CMake build, and havingversion :: Maybe String; #ifdef VERSION; version = Just VERSION; #else; version = Nothing- this allows the package to be built without CMake (but then of course you have no knowledge of the version - but I think that's the best you can do). - user2407038