Cabal预处理符号

时间:2012-07-26 15:30:52

标签: haskell build cabal

  

可能重复:
  Haskell: Correct practice to specify version in source?

是否可以将cabal元数据导出到我的代码?使用autotools,我在configure.ac中定义了程序版本,并将其作为我的代码的定义。在cabal中,目前,我必须在两个地方保存程序版本 - 在.cabal和--version。任何优雅的解决方案?

1 个答案:

答案 0 :(得分:1)

让我用shell会话回答这个问题:

~ $ cd /tmp/
/tmp $ mkdir test
/tmp $ cd test/
/tmp/test $ cabal init
Package name? [default: test] 
Package version? [default: 0.1.0.0] 
Please choose a license:
 * 1) (none)
   2) GPL-2
   3) GPL-3
   4) LGPL-2.1
   5) LGPL-3
   6) BSD3
   7) MIT
   8) PublicDomain
   9) AllRightsReserved
  10) Other (specify)
Your choice? [default: (none)] 
Author name? [default: Joachim Breitner] 
Maintainer email? [default: mail@joachim-breitner.de] 
Project homepage URL? 
Project synopsis? 
Project category:
 * 1) (none)
   2) Codec
   3) Concurrency
   4) Control
   5) Data
   6) Database
   7) Development
   8) Distribution
   9) Game
  10) Graphics
  11) Language
  12) Math
  13) Network
  14) Sound
  15) System
  16) Testing
  17) Text
  18) Web
  19) Other (specify)
Your choice? [default: (none)] 
What does the package build:
   1) Library
   2) Executable
Your choice? 2
Include documentation on what each field means (y/n)? [default: n] 

Guessing dependencies...

Generating LICENSE...
Warning: unknown license type, you must put a copy in LICENSE yourself.
Generating Setup.hs...
Generating test.cabal...

Warning: no synopsis given. You should edit the .cabal file and add one.
You may want to edit the .cabal file and add a Description field.
/tmp/test $ vim *.cabal
/tmp/test $ cat *.cabal
name:                test
version:             0.1.0.0
license-file:        LICENSE
author:              Joachim Breitner
maintainer:          mail@joachim-breitner.de
build-type:          Simple
cabal-version:       >=1.8

executable test
  main-is: Main.hs
  build-depends:       base ==4.5.*

/tmp/test $ echo 'main = return ()' > Main.hs
/tmp/test $ cabal configure && cabal build
Resolving dependencies...
Configuring test-0.1.0.0...
Warning: The 'license-file' field refers to the file 'LICENSE' which does not
exist.
Building test-0.1.0.0...
Preprocessing executable 'test' for test-0.1.0.0...
[1 of 1] Compiling Main             ( Main.hs, dist/build/test/test-tmp/Main.o )
Linking dist/build/test/test ...
/tmp/test $ find dist/
dist/
dist/package.conf.inplace
dist/setup-config
dist/build
dist/build/autogen
dist/build/autogen/Paths_test.hs
dist/build/autogen/cabal_macros.h
dist/build/test
dist/build/test/test-tmp
dist/build/test/test-tmp/Main.o
dist/build/test/test-tmp/Main.hi
dist/build/test/test
/tmp/test $ grep -i version dist/build/autogen/Paths_test.hs
    version,
import Data.Version (Version(..))
version :: Version
version = Version {versionBranch = [0,1,0,0], versionTags = []}
/tmp/test $ echo 'import Paths_test' > Main.hs
/tmp/test $ echo 'main = print version' >> Main.hs
/tmp/test $ cabal build
Building test-0.1.0.0...
Preprocessing executable 'test' for test-0.1.0.0...
[1 of 2] Compiling Paths_test       ( dist/build/autogen/Paths_test.hs, dist/build/test/test-tmp/Paths_test.o )
[2 of 2] Compiling Main             ( Main.hs, dist/build/test/test-tmp/Main.o )
Linking dist/build/test/test ...
/tmp/test $ ./dist/build/test/test
Version {versionBranch = [0,1,0,0], versionTags = []}

(TL; DR:cabal生成一个名为Paths_<pkgname>的模块,用于定义常量version :: Version。)