COMMUNITY

Compliling multiple Haxe code files to Emscripten via javascript

Hi

I’ve spent some time on the Internet but can not find a solution. What is the workflow to compile multiple Haxe code files to Emscripten via javascript?

It’s unclear what you are trying to do, what do you mean by “emscripten via javascript”?

If you want to generate javascript from haxe you can use the javascript target -js myfile.js see https://haxe.org/manual/target-javascript-getting-started.html you don’t need emscripten at all.

If you need to use hxcpp it can target emscripten with -D emscripten,
I’ve never tried it so you might need other things, Haxe and Emscripten may have some information.

Thanks ibilon,

That was what I was looking to target haxe code to emscripten.

I just assumed that you will need to convert the haxe code to javascript then some how compile the javascript to emscripten.

Thanks again.

Emscripten is only a toolchain to go from llvm to js/web assembly, you don’t target it.

So I think you mean you want to target web assembly?

In that case yes you need to use hxcpp and emscripten to do that.
https://groups.google.com/d/msg/haxelang/Pcm38LPFjW0/Q3-WXk2oBQAJ has some steps to show how to do that, though it might be a little outdated.
See Build a .wasm Class using WebAssembly for some more information.

THanks
Using the web assembly example I’m getting the following error
python: can’t open file ‘1/emcc’: [Errno 2] No such file or directory

Have anyone come across it before|?

Thanks

What steps did you do?


# Windows maybe needs this define
-D EMSCRIPTEN_SDK

# If you want the .html file showing how to embed the wasm
-D HXCPP_LINK_EMSCRIPTEN_EXT=.html

# Tell hxcpp to use emscripten-toolchain.xml
-D emscripten

-cpp out
-main Main
````Preformatted text`

@:buildXml(" <linker id='exe' exe='emcc'> <flag value='-s' /> <flag value='WASM=1' /> </linker> ") class Main { static function main() { trace('Hello Web Assembly'); } }


`source /path/to/emsdk_env.bat`

Then compile:

`haxe build.hxml`

So it fails on haxe build.hxml?

And you should do /path/to/emsdk_env.bat, source is a linux thing, see https://emscripten.org/docs/getting_started/downloads.html

Yes, it failed on build.hxml

I’m on windows.

I’ve run the build.hxml from the same folder as he .hx file.

I’ve run emsdk_env.bat and make sure emcc is working. I can double check path layout for emcc.

Any other suggestion?

Has anyone got this to work?

Thanks

Typical :slight_smile:
After a good night sleep. The problem is now resolved.

I comment out each define option until it build without any errors.

It fails on:-

Windows maybe needs this define

#-D EMSCRIPTEN_SDK

Could some explain the EMSCRIPTEN_SDK ?

Thanks all your feedback.

@:buildXml("
<linker id='exe' exe='emcc'>
	<flag value='-s' />
	<flag value='WASM=1' />
</linker>
")

Could someone explain the above code that need to be added to the .hx file (I assumed all .hx files).

My next experiment is to compile multiple .hx file from the design patterns example.

Hi

I’m still getting failed build error:-

OS: Windows 7 x64
Haxe: V4

Set the following tools as active:
vs-tool-0.9.4
releases-fastcomp-048cf9424790cc525a7ea6da340820aae226f3b9-64bit
node-8.9.1-64bit
python-2.7.13.1-64bit
java-8.152-64bit

emscripten: emsdk V1.38x latest version

Visual studio: v2017

Following the instruction from
https://webassembly.org/getting-started/developers-guide/
to install emsdk

Main.hx

//@:buildXml("
//	<linker id='exe' exe='emcc'>
//	<flag value='-s' />
//	<flag value='WASM=1' />
//	</linker>
//")

/**
 * ...
 * @author ds
 */
class Main 
{
	
	static function main() 
	{
		trace("Hello Wolrd CPP");
	}
	
}

Ran command

Haxe -main Main -cpp cpp -D emscripten

It returns the following error:-

Haxe -main Main -cpp cpp -D emscripten
haxelib run hxcpp Build.xml haxe -Demscripten=“1” -Dhaxe3=“1” -Dhaxe4=“1” -Dhaxe
_ver=“4.000” -Dhxcpp_api_level=“400” -Dsource-header=“Generated by Haxe 4.0.0-rc
.2+77068e10c” -Dstatic=“1” -Dtarget.name=“cpp” -Dtarget.static=“true” -Dtarget.s
ys=“true” -Dtarget.threaded=“true” -Dtarget.utf16=“true” -Dutf16=“1” -I"F:\Haxe
Toolkit\haxe\extraLibs/" -I"" -I"F:\HaxeToolkit\haxe\std/cpp/_std/" -I"F:\
HaxeToolkit\haxe\std/"
Creating E:/HelloWolrdCpp/src/cpp/obj/emscripten/__pch/haxe/hxcpp
.h.gch…
python: can’t open file ‘/emcc’: [Errno 2] No such file or directory
Error: Could not create PCH
Error: Build failed

Any ideas ?

Thanks

The @:buildXml is used to pass flags to the compiler/linker, here to make it create a wasm file.
This has to be on only one class, which needs to be in the compilation, so the main class is a good place.

No idea if -D EMSCRIPTEN_SDK is still needed, maybe not.

That sounds like a missing env var, did you do /path/to/emsdk_env.bat? I think you need to do that each time you open the terminal.

If that still doesn’t work maybe the hxcpp toolchain need an update, you should open an issue on https://github.com/HaxeFoundation/hxcpp

Thanks ibilon for looking into his.

I’ll raise issue ticket with Hxcpp. Also, I’ll look into getting it to work on a Linux box or maybe use HTML5 :slight_smile:

Hi

Have anyone come across shared:error user32.lib no such file.

Thanks

Correct me if I’m wrong:-

To convert/compile Haxe code to emscripten
Add the -D emscripten and -D emscripten_ext=.hml to the build.hxml
Add link information for emcc to the Main.hx file
Run Haxe build.hxml
It will compile the Haxe code.
Then run Link Main.exe
Then generate main.html file

In my case, it fails on link Main.exe, and return an error share lib user32.lib not find. ( I have already added the directory location for he user32.lib to the path.)

Anyone else experiencing the same problem on Windows 7 x64?

@fhalo please don’t duplicate your topic, I know you don’t get answers but basically nobody use haxe/emscripten, because you can just target javascript
do you really need to target emscripten? do you have native libraries you need to use?

What do you mean by that, the link path or the PATH env var? How did you try ?

Sorry about that it wasn’t my intention.

Path:

I use the following command:-

set PATH = %PATH%;c:\path-to-user32.lib\ (can’t remeber the full path name)

So, the new workflow to run Haxe code in the web is to compile to javascript ( run in HTML5 )?

The reason why I was trying to get emscripten to work because it provides better performance. Also, emscripten support wasm. So, Haxe no longer support emscripten?

That’s the path env var, it contains the path to the executables for the terminal, it’s not made for the libraries.
You’d need something to add to the ^@:buildXml I think.

Using the js target always has been the way :wink:
I doubt you’d see real performance gain using emscripten anyway, the javascript engine are really really good, and it’s a lot faster to compile to js.

It does support emscripten, afaik nme use it (at least partially?) but it’s not the best documented way.

:slight_smile:
Thanks for the feedback. I’ll start to experiment with javascript. But, I looked at some examples, it seems I may have to make a lot of change to he haxe code to display a simple hello world message. My aim to write a Haxe code once, so that I can run it in a browser,Desktop, Linux, mobile device & Mac.

I’ll research more in @BulldXml and get a better understanding on how it works.

I’ll try not to duplicate this call again. :slight_smile:

Thanks