Conditional compilation based on Haxe compiler version? -
what exact syntax conditional compilation in haxe checking against version number?
according haxe --help-defines
haxedef haxe compiler version "haxe-ver" assume becomes "haxe_ver" in code.
so want check if version number @ least 3.2.0. tried:
#if (haxe_ver >= 3.2.0)
but didn't seem work. tried:
#if !haxe_ver < 3.2.0
and seemed compile, want sure.
well, it's not answer, see lots of different styles grepping through 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 appears: haxe_ver
string 1 .
, , it's string comparison that's happening. can enclose version in quotes, there 3 gotchas:
- if use more 1
.
you'll throw off comparison. don't use "3.1.3" - if use logical not
!
should use parentheses - 3.1.3 (at least in environment) reports
haxe_ver = 3.103
these 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 macro print 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(); (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
Comments
Post a Comment