Search code examples
haxeconditional-compilation

Haxe defines with dot


In Haxe, what is the correct way to refer to a define with a dot?

For example, for the library thx.core, how to write conditional compilation against the library name?

#if thx.core

#end

Furthermore, are there general rules for special characters?


Solution

  • The #if flag syntax does not seem to handle dots. However, Compiler.getDefine() handles most characters, including the dot:

    hxml/build command: -D é'"(-è_.çà)=test

    #if "é'\"(-è_çà)"
    trace('will always be called, even without the -D');
    #end
    
    trace(haxe.macro.Compiler.getDefine("é'\"(-è_.çà)")); // test
    

    There is a workaround for dots with initialization macros, even if it is not really pretty:

    build.hxml

    -x Main.hx
    -D abc.def
    --macro Macro.parseDefines()
    

    Macro.hx

    import haxe.macro.Compiler;
    
    class Macro {
        public static macro function parseDefines():Void {
            if (Compiler.getDefine("abc.def") != null) {
                Compiler.define("abc_def");
            }
        }
    }
    

    Main.hx

    class Main {
        public static function main() {
            #if abc_def
            trace("abc.def is defined!");
            #end
        }
    }