Use relative paths for extra-lib-dirs on cabal

China☆狼群 提交于 2021-02-07 06:00:07

问题


I have a C library "myboo" which has Makefile. I want to make a wrapper of this library. I don't want to install it into /usr/local since "myboo" is not a major module. Additionally it is recommended that I build "myboo" not as a dynamic library but as a static library.

I make custom Setup.py to build "myboo";

main :: IO ()
main = defaultMainWithHooks simpleUserHooks {
           preBuild = \a b -> makeLib a b >> preBuild simpleUserHooks a b
           }

makeLib :: Args -> BuildFlags -> IO ()
makeLib _ flags = do
  let verbosity = fromFlag $ buildVerbosity flags
  cflags <- lookupEnv "CFLAGS" >>= return . maybe "" id
  setEnv "CFLAGS" $ "-fPIC" ++ (' ' : cflags)
  rawSystemExit verbosity "env" ["make", "--directory=myboo", "libmyboo.a"]

And I arrange myboo.cabal to link my haskell codes to C library;

library
  exposed-modules:     MyBoo
  build-depends:       base >=4.7 && <4.8
  hs-source-dirs:      src
  default-language:    Haskell2010
  include-dirs: myboo
  extra-libraries: myboo
  extra-lib-dirs: myboo

When I run "cabal build", I got following messages.

myboo-0.1.0.0: library-dirs: myboo is a relative path which makes no sense (as
there is nothing for it to be relative to). You can make paths relative to the
package database itself by using ${pkgroot}. (use --force to override)

If I write "extra-lib-dirs: /absolute/path/to/working/dir/myboo", it seems that it works well. But it's not good way because /absolute/... is just a working directory.

How should I fix above error messages? My environment is here;

% ghc --version
The Glorious Glasgow Haskell Compilation System, version 7.8.2
% cabal --version
cabal-install version 1.20.0.2
using version 1.20.0.0 of the Cabal library
% cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04 LTS"

回答1:


You can write your own Setup.hs and setup Distribution.Simple.confHook. In your hook function, modify Distribution.PackageDescription.extraLibDirs to include the directory.

Note that you also need to change the build-type to Custom in your cabal file.

Here is a link to Setup.hs in which I did what I wrote.




回答2:


A little bit late, but...

Now you can use relative path to set extra-include-dirs and extra-lib-dirs options in stack.yaml. For example, for some package A, it would look like this:

extra-include-dirs:
- ../my-c-lib/src

extra-lib-dirs:
- ../my-c-lib/build

assuming both A and my-c-lib are stored in the same folder.

That's it. It doesn't require you to set LD_LIBRARY_PATH variable (until you run executable). But, if you have a package B which uses A, you have to add A's extra-..-dirs to B's stack.yaml. Otherwise you'll get Missing C library error. Also, LD_LIBRARY_PATH must be set here in order to prevent can't load .so/.DLL error during B's building.

Basically it's a workaround for building local packages. I wouldn't use it for public package because it forces a user to either modify stack.yaml or to store dependend packages in the same folder.



来源:https://stackoverflow.com/questions/24444675/use-relative-paths-for-extra-lib-dirs-on-cabal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!