Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional compilation based on Haxe compiler version?

What is the exact syntax for conditional compilation in Haxe checking against the version number?

According to haxe --help-defines the haxedef for haxe compiler version is "haxe-ver" which I assume becomes "haxe_ver" in code.

So I want to check if the version number is at least 3.2.0. I initially tried:

#if (haxe_ver >= 3.2.0)

but that didn't seem to work. Then I tried:

#if !haxe_ver < 3.2.0

And that seemed to compile, but I want to be sure.

like image 859
larsiusprime Avatar asked Aug 23 '26 14:08

larsiusprime


2 Answers

Well, it's not an answer, but I do see lots of different styles grepping through my 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)

And quick test:

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

  }
}

Compiled using 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

Compiled using 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

So it appears: haxe_ver is a string with only one ., and it's string comparison that's happening. You can enclose your version in quotes, but there are three gotchas:

  • if you use more than one . you'll throw off the comparison. DON'T use "3.1.3"
  • if you use logical not ! you should use parentheses
  • 3.1.3 (at least in my environment) reports haxe_ver = 3.103

These are safe:

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

Also if it's helpful, here's a macro to print all the defines:

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() {
  }
}

Which outputs:

>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
like image 156
Jeff Ward Avatar answered Aug 26 '26 23:08

Jeff Ward


Haxe supports string comparison in conditional compilation, like:

#if ("a" < "b")

The haxe_ver define is a string, so you would compare it like this:

#if (haxe_ver < "3.2.0")

Since it is string comparison, be careful that changing the number of digits will have funny results. For example, "10" is less than "2" because it starts with "1". So long as your variables are uniform, you should be fine though ("10 and "02" works)

You might be able to compare haxe_ver as a number (even though it is really a string), but I would think that would only work until the first decimal place?

#if (haxe_ver < 3.2)

I think that string comparison comparison would be safer :)

#if (haxe_ver < "3.2")
like image 40
Joshua Granick Avatar answered Aug 27 '26 00:08

Joshua Granick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!