基于Haxe编译器版本的条件编译?

时间:2015-07-22 14:25:59

标签: haxe conditional-compilation

Haxe检查版本号时条件编译的确切语法是什么?

根据haxe --help-defines,haxe编译器版本的haxedef是“haxe-ver”,我认为它在代码中变为“haxe_ver”。

所以我想检查版本号是否至少为3.2.0。我最初尝试过:

#if (haxe_ver >= 3.2.0)

但这似乎不起作用。然后我试了一下:

#if !haxe_ver < 3.2.0

这似乎是编译,但我想确定。

2 个答案:

答案 0 :(得分:6)

嗯,这不是答案,但我确实看到很多不同的风格通过我的haxe libs:

haxe-3.2.0/std/cpp/zip/Uncompress.hx:2: #if (haxe_ver < 3.4)
haxe-3.1.3/std/neko/zip/Uncompress.hx:2: #if (haxe_ver < 3.2)
haxe-3.1.3/lib/openfl/3,0,0-beta,3/openfl/Vector.hx:767: #if (haxe_ver > 3.101)
haxe-3.1.3/lib/openfl/3,0,0-beta,3/docs/ImportAll.hx:926: #if (haxe_ver >= "3.2")
haxe-3.1.3/lib/actuate/1,8,3/motion/actuators/GenericActuator.hx:61:  #if (haxe_209 || haxe3)

快速测试:

class Ver {

  macro public static function get_ver():haxe.macro.Expr {
    var rtn = haxe.macro.Context.definedValue("haxe_ver");
    return {expr: EConst(CString(rtn)) , pos : haxe.macro.Context.currentPos()};
  }

  static function main() {
    trace("haxe_ver: "+get_ver());
#if (haxe_ver > "3.1.3")
    trace("haxe_ver > \"3.1.3\" - true");
#else
    trace("haxe_ver > \"3.1.3\" - false");
#end

#if (haxe_ver > 3.130)
    trace("haxe_ver > 3.130 - true");
#else
    trace("haxe_ver > 3.130 - false");
#end

#if (haxe_ver >= 3.20)
    trace("haxe_ver >= 3.20 - true");
#else
    trace("haxe_ver >= 3.20 - false");
#end

#if (!haxe_ver < 3.10)
    trace("!haxe_ver < 3.10 - true");
#else
    trace("!haxe_ver < 3.10 - false");
#end

#if (!(haxe_ver < 3.10))
    trace("!(haxe_ver < 3.10) - true");
#else
    trace("!(haxe_ver < 3.10) - false");
#end

  }
}

使用Haxe 3.2进行编译:

Ver.hx:9: haxe_ver: 3.2
Ver.hx:10: haxe_ver > "3.1.3" - true
Ver.hx:16: haxe_ver > 3.130 - true
Ver.hx:22: haxe_ver >= 3.20 - true
Ver.hx:30: !haxe_ver < 3.10 - false
Ver.hx:34: !(haxe_ver < 3.10) - true

使用Haxe 3.1.3编译:

Ver.hx:9: haxe_ver: 3.103
Ver.hx:10: haxe_ver > "3.1.3" - true
Ver.hx:18: haxe_ver > 3.130 - false
Ver.hx:24: haxe_ver >= 3.20 - false
Ver.hx:30: !haxe_ver < 3.10 - false
Ver.hx:34: !(haxe_ver < 3.10) - true

看来:haxe_ver是一个只有一个.的字符串,并且正在进行字符串比较。您可以在引号中附上您的版本,但有三个陷阱

  • 如果您使用多个.,您将会放弃比较。不要使用&#34; 3.1.3&#34;
  • 如果您使用逻辑而不是!,则应使用括号
  • 3.1.3(至少在我的环境中)报告haxe_ver = 3.103

这些是安全的:

#if (haxe_ver > 3.130)
#if (haxe_ver <= 3.130)
#if (haxe_ver < "3.200")
#if (!(haxe_ver < 3.1))

如果它有用,here's宏来打印所有定义:

import haxe.macro.Expr;
import haxe.macro.Context;

// Note: Context.getDefines() requires Haxe 3.2 or later
class Main {

  // - - - - - - - - - - - - - - - - - - - - - - - - - -
  macro public static function get_defines():Expr {
    var rtn = "Defines ("+
      (Context.defined("macro")?"macro":"standard")+" pass):\n";
    var defines:Map<String,String> = Context.getDefines();
    for (key in defines.keys()) {
      rtn += "-D "+key+"="+defines.get(key)+"\n";
    }
    trace(rtn); // compile-time trace
    return {expr: EConst(CString(rtn)) , pos : Context.currentPos()};
  };
  private static var __invoke_defines = get_defines();
  // - - - - - - - - - - - - - - - - - - - - - - - - - -

  static function main() {
  }
}

哪个输出:

>haxe Main.hx -main Main -D foo=3
Main.hx:15: Defines (macro pass):
-D haxe_ver=3.2
-D macro=1
-D sys=1
-D foo=3
-D dce=std
-D hxcpp_api_level=321
-D true=1
-D cross=1
-D neko=1
-D haxe3=1

Main.hx:15: Defines (standard pass):
-D haxe_ver=3.2
-D sys=1
-D foo=3
-D dce=std
-D hxcpp_api_level=321
-D true=1
-D cross=1
-D haxe3=1

答案 1 :(得分:3)

Haxe支持条件编译中的字符串比较,如:

#if ("a" < "b")

haxe_ver define是一个字符串,所以你可以像这样比较它:

#if (haxe_ver < "3.2.0")

由于是字符串比较,请注意更改位数会产生有趣的结果。例如,“10”小于“2”,因为它以“1”开头。只要你的变量是统一的,你应该没事(“10和”02“有效)

您可以将haxe_ver与数字进行比较(即使它实际上是一个字符串),但我认为这只会在第一个小数位之前有效?

#if (haxe_ver < 3.2)

我认为字符串比较比较安全:)

#if (haxe_ver < "3.2")
相关问题