简体   繁体   English

GHC版本检入代码

[英]GHC version check in code

I'm contributing to Alex , and it obviously depends on a lot of libraries and should compile for a lot of versions. 我正在为Alex做贡献,它显然取决于很多库,应该编译很多版本。

I need to use a function that is only available from GHC 7.6.1 to handle an error better. 我需要使用只能从GHC 7.6.1获得的函数来更好地处理错误。 So I want to use an #if ... to import said function, else, I'll deal with the error differently. 所以我想使用#if ...来导入所述函数,否则,我会以不同的方式处理错误。

I've seen some: 我见过一些:

#if __GLASGOW_HASKELL__ >= 610
import Control.Exception ( bracketOnError )
#endif

So I did: 所以我做了:

#if __GLASGOW_HASKELL__ >= 761
import Text.Read ( readMaybe )
#endif

Thinking that 761 is an alias to GHC version 7.6.1 , when I build the cabal package and try it out, the function doesn't get imported, even though I use The Glorious Glasgow Haskell Compilation System, version 7.8.4 . 认为761是GHC版本7.6.1的别名,当我构建cabal包并尝试它时,即使我使用Glorious Glasgow Haskell编译系统版本7.8.4 ,该函数也不会被导入。

So after using a program to try it out, I find out that 7.8.1 identifies in __GLASGOW_HASKELL__ as 708 . 所以在使用程序试用之后,我发现7.8.1__GLASGOW_HASKELL__标识为708

{-# LANGUAGE CPP #-}
module Main where

#if __GLASGOW_HASKELL__ == 708
ver = "==708"
#else
ver = "/=708"
#endif

main = putStrLn $ ver

And running it: 运行它:

$ runhaskell if.hs
==708

How can I know what value should I use for 7.6.1 , or is there a better way to deal with this? 我怎么知道7.6.1应该使用什么值,还是有更好的方法来解决这个问题?

That's described in section 6.11.3.1 of GHC's users guide: 这在GHC用户指南的第6.11.3.1节中有所描述:

For version xyz of GHC, the value of __GLASGOW_HASKELL__ is the integer ⟨xyy⟩ (if ⟨y⟩ is a single digit, then a leading zero is added, so for example in version 6.2 of GHC, __GLASGOW_HASKELL__==602 ). 对于GHC的xyz版本, __GLASGOW_HASKELL__的值是整数⟨xyy⟩(如果⟨y⟩是单个数字,则添加前导零,因此例如在GHC的版本6.2中, __GLASGOW_HASKELL__==602 )。 More information in GHC version numbering policy . 有关GHC版本编号策略的更多信息。

So for 7.6.1 , you would check __GLASGOW_HASKELL__ >= 706 . 因此对于7.6.1 ,您将检查__GLASGOW_HASKELL__ >= 706 The reason for this are versions like 7.10.x . 原因是7.10.x这样的版本。

Read the fine documentation : 阅读精美的文档

For version xyz of GHC, the value of __GLASGOW_HASKELL__ is the integer xyy (if y is a single digit, then a leading zero is added, so for example in version 6.2 of GHC, __GLASGOW_HASKELL__ ==602). 对于GHC的xyz版本, __GLASGOW_HASKELL__的值是整数xyy(如果y是单个数字,则添加前导零,因此例如在GHC的版本6.2中, __GLASGOW_HASKELL__ == 602)。 More information in Section 1.4, “GHC version numbering policy”. 有关详细信息,请参见第1.4节“GHC版本编号策略”。

With any luck, __GLASGOW_HASKELL__ will be undefined in all other implementations that support C-style pre-processing. 运气好的话, __GLASGOW_HASKELL__在所有其他支持C风格预处理的实现中都是未定义的。

(For reference: the comparable symbols for other systems are: __HUGS__ for Hugs, __NHC__ for nhc98, and __HBC__ for hbc.) (供参考:其他系统的可比符号为: __HUGS__代表Hugs, __NHC__代表nhc98, __HBC__代表hbc。)

NB. NB。 This macro is set when pre-processing both Haskell source and C source, including the C source generated from a Haskell module (ie .hs, .lhs, .c and .hc files). 在预处理Haskell源和C源时设置此宏,包括从Haskell模块生成的C源(即.hs,.lhs,.c和.hc文件)。

As Daniel Wagner indicated, the most correct way to check for package version is usually to use a Cabal MIN_VERSION macro. 正如Daniel Wagner所指出的,检查包版本的最正确方法通常是使用Cabal MIN_VERSION宏。 For example, you could use 例如,你可以使用

#if MIN_VERSION_base(4,6,0)

to determine if the base package is at least version 4.6.0, which is the earliest version with the function you seek. 确定base软件包是否至少是4.6.0版本,这是您使用所需功能的最早版本。

The base package is a little weird. base包有点奇怪。 It was used both by GHC and by the now-defunct Hugs and NHC implementations. 它由GHC和现已解散的Hugs和NHC实施使用。 Using the Cabal macro was then a more portable way to check the base version. 然后使用Cabal宏以更便携的方式检查base版本。 These days, GHC is the only one using base , so the portability argument is a little less clear, but that approach also has the advantage of checking the major and minor version numbers. 目前,GHC是唯一使用base ,因此可移植性论点不太清楚,但这种方法还具有检查主要版本号和次要版本号的优势。

Since base versions are very tightly tied to GHC versions, you can define a reasonable fall-back form of MIN_VERSION_base for compiling without Cabal, using __GLASGOW_HASKELL__ to estimate the base version. 由于base版本与GHC版本紧密相关,因此您可以定义合理的后备形式MIN_VERSION_base以便在没有Cabal的情况下进行编译,使用__GLASGOW_HASKELL__来估算base版本。 The current containers head conditionally defines such a fall-back. 当前的containers头部有条件地限定了这种后退。

Update 更新

As of GHC 8, the compiler itself has taken over the job of defining MIN_VERSION macros. 从GHC 8开始,编译器本身已经接管了定义MIN_VERSION宏的工作。 This is great, because you now get to use these macros whether or not you build with Cabal. 这很棒,因为无论是否使用Cabal构建,您现在都可以使用这些宏。 No more ugly approximations! 没有更丑陋的近似!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM