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

lar*_*ime 3 haxe conditional-compilation

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

根据haxe --help-defineshaxedef为haxe编译器版本是"haxe-ver",我假设在代码中变成"haxe_ver".

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

#if (haxe_ver >= 3.2.0)

但这似乎不起作用.然后我尝试了:

#if !haxe_ver < 3.2.0

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

Jef*_*ard 7

嗯,这不是一个答案,但我确实看到很多不同的风格通过我的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)
Run Code Online (Sandbox Code Playgroud)

并快速测试:

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

  }
}
Run Code Online (Sandbox Code Playgroud)

使用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
Run Code Online (Sandbox Code Playgroud)

使用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
Run Code Online (Sandbox Code Playgroud)

所以看起来:haxe_ver是一个只有一个.字符串的字符串,它正在发生字符串比较.你可以用引号括起你的版本,但有三个问题:

  • 如果你使用多个,.你就会放弃比较.不要使用"3.1.3"
  • 如果您使用逻辑!,则应使用括号
  • 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))
Run Code Online (Sandbox Code Playgroud)

此外,如果它有用,这里是一个宏来打印所有定义:

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() {
  }
}
Run Code Online (Sandbox Code Playgroud)

哪个输出:

>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
Run Code Online (Sandbox Code Playgroud)