Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Hello Word compiling from Swift to JavaScript using Emscripten

Given the simplest of swift files:

println("lol")

It's trivial to run this on the command line with xcrun swift -i lol.swift or compile to an executable with xcrun swift lol.swift -o lol, but how about a simple proof of concept for emscripten?

I haven't used emscripten before, but got a hello world example using C++ working from http://kripken.github.io/emscripten-site/docs/getting_started/Tutorial.html, and wanted to compile my Swift code too.

I tried

xcrun swift lol.swift -emit-bc -o lol.bc
emcc lol.bc

But get

Value:   %1 = call { i8*, i64, i64 } @_TFSS37_convertFromBuiltinUTF16StringLiteralfMSSFTBp17numberOfCodeUnitsBw_SS(i8* bitcast ([4 x i16]* @0 to i8*), i64 3)
LLVM ERROR: Unrecognized struct value
Traceback (most recent call last):
  File "/Users/glen/Downloads/emsdk_portable/emscripten/1.16.0/emcc", line 1540, in <module>
shared.Building.llvm_opt(final, link_opts)
  File "/Users/glen/Downloads/emsdk_portable/emscripten/1.16.0/tools/shared.py", line 1267, in llvm_opt
assert os.path.exists(target), 'Failed to run llvm optimizations: ' + output
AssertionError: Failed to run llvm optimizations:

Thoughts?

like image 905
geelen Avatar asked Jun 10 '14 13:06

geelen


1 Answers

The problem is LLVM can't find a types/symbols, used in that call, during the linking process. These symbols are most likely specific to the swift framework. If you run emcc with the -v option you can get more debug information. You might consider also providing --llvm-opts hinting where that information can be found.

I ran xcrun swift -v test.swift to see what command is actually executed.

Swift version 1.0 (swift-600.0.34.4.5)
Target: x86_64-apple-darwin13.2.0
/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file test.swift -enable-objc-attr-requires-objc-module -target x86_64-apple-darwin13.2.0 -module-name test -color-diagnostics -o     /var/folders/69/l9w0zkqn38s1td4_gm5c__km0000gn/T/test-d800d3.o
/usr/bin/ld /var/folders/69/l9w0zkqn38s1td4_gm5c__km0000gn/T/test-d800d3.o -force_load /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_macosx.a -lSystem -arch x86_64 -L /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -rpath /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -macosx_version_min 10.9.0 -no_objc_category_merging -o test

You might consider exploring how to apply these linking options to emscripten to get what you want. There will not be any documentation on this, because I don't think they intended swift to be used like this.

like image 180
wbennett Avatar answered Oct 20 '22 17:10

wbennett