Search code examples
haxe

How to get path to "haxe.exe" from macro/eval?


I would like to spawn a child process from haxe eval script (or macro). This child process is also an instance of haxe, compiling the code downloaded/generated by parent. Obviously, i can simply use

Sys.command('haxe -cpp $buildDir -main CoreMain $coreDefines');

but i would like to use exact path to haxe.exe (haxe on *nix) if possible. So far i haven't found API to get this path from eval or macro. Am i missing something or there is no such API?


Solution

  • I don't know about any API options for getting to path to the haxe binary, but there was meant to be a HAXEPATH environment variable that you could refer to with Sys.getEnv()?

    Otherwise you might be able to startup a process, find the path, and write it to a file:

    function getHaxeBinPath () : String {
        var path = switch ( Sys.systemName () ) {
            case "Windows":
                // Windows Script.
            case "Linux":
                Sys.command ( "echo $( which haxe ) >> exePath.txt" );
                Sys.io.File.getContent ( "exePath.txt" );
            case "BSD":
                // BSD Script.
            case "Mac":
                // Macintosh Script.
            default:
                throw "Unknown Operating System!";
        }
    
        if ( sys.FileSystem.exists ( "exePath.txt" ) {
            sys.FileSystem.deleteFile ( "exePath.txt" );
        }
    
        return path;
    }
    

    but then you'd need a script for each operating system and account for versions of things. If you decide to go with this, I think Windows has a WHERE command you might be able to use, but it's not really much I know about.

    This doesn't seem like it'd work on *nix (because of needing to log in/out for environment variables to update), but you might be able to set an environment variable in the process and then get it with Sys.getEnv()? Same problem with making a custom thing for each OS though.