是否可以使用Cabal构建Haskell项目并从环境变量控制GHC标志?
在我的project.cabal文件中有一个“GHC-Options”部分,它控制着标志,但我很乐意从命令行中另外设置它们。
如果你只有开/关标志,你应该使用Cabal flags。
在cabal文件的顶层使用build-type: Configure
。
编写一个configure
脚本(即名为“configure”的程序包顶层的shell脚本 - 它不必是autotools样式)。它应该读取环境变量并生成一个名为package.buildinfo的文件,其中“package”被适当地替换。
该文件的格式在cabal用户指南中理论上有描述。该文件应该只包含构建信息字段,如cabal用户指南的3.3.2.9节所述。即,build-depends
,other-modules
,hs-source-dirs
,default-extensions
,other-extensions
,extensions
,build-tool-depends
,build-tools
,buildable
,ghc-options
,ghc-prof-options
,ghc-shared-options
,includes
,install-includes
,include-dirs
,c-sources
,cxx-sources
,asm-sources
,cmm-sources
,js-sources
,extra-libraries
,extra-ghci-libraries
,extra-bundled-libraries
,extra-lib-dirs
,cc-options
,cpp-options
,cxx-options
,ld-options
,pkgconfig-depends
,frameworks
,extra-frameworks-dirs
和mixins
字段进入缩进部分。它们不需要缩进。这些将与您的cabal文件的library
部分中的等效字段合并。 (“用户指南”描述了包含executable
部分的功能,但我现在无法使它们正常工作。)
因此,如果您希望代码中的字符串反映$PATH
的内容,请设置您的cabal文件并编写如下配置脚本:
#!/bin/bash
echo 'cpp-options: -DCOMPILE_TIME_PATH="'$PATH'"' > my-package.buildinfo
然后你可以写一个.hs文件
{-# LANGUAGE CPP #-}
module WhyNot where
compileTimePath :: String
compileTimePath = COMPILE_TIME_PATH
Cabal用户指南:https://www.haskell.org/cabal/users-guide/developing-packages.html