Help with compiling SpiderMonkey for tvOS

I believe I have the JS project prepared but still struggling with compiling the SpiderMonkey.
@elvman and @ricardo did a lot of good work on this.

I’m using the master branch from here: https://github.com/ricardoquesada/Spidermonkey/tree/master
BTW, is this proper version?

This is how I’ve modified the build.sh:

MIN_IOS_VERSION=9.0
IOS_SDK=9.0

LIPO="xcrun -sdk AppleTVOS lipo"
STRIP="xcrun -sdk AppleTVOS strip"

../configure --with-ios-target=AppleTVOS --with-ios-version=$IOS_SDK --with-ios-min-version=$MIN_IOS_VERSION --with-ios-arch=arm64 \
           --disable-shared-js --disable-tests --disable-ion --disable-jm --disable-tm --enable-llvm-hacks --disable-methodjit --disable-monoic --disable-polyic --disable-yarr-jit \
           --enable-optimize=-O3 --with-thumb=yes --enable-strip --enable-install-strip --without-intl-api --disable-debug --disable-threadsafe \
            --disable-gcgenerational --disable-exact-rooting --enable-fembed-bitcode


if [ -e js/src/libjs_static.arm64.a ] ; then
    echo "creating fat version of the library"
    $LIPO -create -output libjs_static.a js/src/libjs_static.arm64.a
    # remove debugging info
    $STRIP -S libjs_static.a
    $LIPO -info libjs_static.a
fi

And this is how I’ve modified the js/src/configure:

case "$ios_target" in
iPhoneOS|iPhoneSimulator|AppleTVOS)

When it runs it throws long error description. What I believe is the problem is that it is compiled for iPhone target.
See the error: ld: building for iOS, but linking against dylib built for tvOS.
How can I fix it?

This is my env dump inside the configure:

ios_sdk_version=9.0
CPU_ARCH=
ios_arch=arm64
TARGET_CPU=x86_64
target_os=darwin
target_name=arm
xcode_base=/Applications/Xcode.app/Contents/Developer/Platforms
ios_toolchain=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
ios_target=AppleTVOS
ios_sdk_root=/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk

This is the complete error text. I also attach the two modified files tvos.zip (81.8 KB). The attached files contain my attempt to set TARGET_CPU=arm64 which did not work:

checking for gcc… /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
checking whether the C compiler (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk -arch arm64 -miphoneos-version-min=9.0 -I/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk/usr/include -pipe -Wno-implicit-int -Wno-return-type -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk -arch arm64 -v) works… no
configure: error: installation or configuration problem: C compiler cannot create executables.
------ config.log ------
Thread model: posix
“/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang” -cc1 -triple arm64-apple-ios9.0.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name conftest.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -target-cpu cyclone -target-feature +neon -target-feature +crc -target-feature +crypto -target-feature +zcm -target-feature +zcz -target-abi darwinpcs -target-linker-version 253.6 -v -dwarf-column-info -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/…/lib/clang/7.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk -I /Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk/usr/include -I /Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk/usr/include -Wno-implicit-int -Wno-return-type -Wno-implicit-int -Wno-return-type -fdebug-compilation-dir /pp/src/work/spiderMonkey/Spidermonkey-2015-03-18–91cf703–v34–HACKED/js/src/build-tvos -ferror-limit 19 -fmessage-length 0 -stack-protector 1 -mstackrealign -fallow-half-arguments-and-returns -fblocks -fobjc-runtime=ios-9.0.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -o /var/folders/49/yy4_svhx0s57y2d178hswnym0000gn/T/conftest-4e9429.o -x c conftest.c
clang -cc1 version 7.0.0 based upon LLVM 3.7.0svn default target x86_64-apple-darwin14.5.0
ignoring nonexistent directory “/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk/usr/local/include”
ignoring nonexistent directory “/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk/Library/Frameworks”
ignoring duplicate directory “/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk/usr/include”
ignoring duplicate directory “/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk/usr/include”
as it is a non-system directory that duplicates a system directory
#include “…” search starts here:
#include <…> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/…/lib/clang/7.0.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk/System/Library/Frameworks (framework directory)
End of search list.
“/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld” -demangle -dynamic -arch arm64 -iphoneos_version_min 9.0.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk -o conftest /var/folders/49/yy4_svhx0s57y2d178hswnym0000gn/T/conftest-4e9429.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/…/lib/clang/7.0.0/lib/darwin/libclang_rt.ios.a
ld: building for iOS, but linking against dylib built for tvOS (/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk/usr/lib/libSystem.tbd). file ‘/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk/usr/lib/libSystem.tbd’ for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
configure: failed program was:

#line 2301 “configure”
#include “confdefs.h”

main(){return(0);}
configure: error: installation or configuration problem: C compiler cannot create executables.

@pepeek
yes, that is the correct github repository.
I haven’t compiled spidermonkey for tvOS, but yes… they way I would compile it is by changing the the js/src/build-ios/build.sh script file

Do you have any clue why is it compiling for iOS instead of tvOS?
Which variable shall I change?
How is it controlled anyway, is it this?

...clang" -cc1 -triple arm64-apple-ios9.0.0 ...

?

Thanks!

@pepeek sorry… I haven’t tried doing that for tvOS yet.

I’ve just tested, and the precompiled libraries that we are shipping with cocos2d-x, are working ok with JS

Which version of the precompiled libraries are you talking about? They should be compiled specifically for tvOS platform (not iOS) arm64 and enabled bitcode. Thanks in advance for the direct link. It would be a bliss to have one :wink:

@pepeek:
good point.

I’m using the ones that are downloaded automatically from github v3 branch. Which I guess were compiled from “master” branch from my Spidermonkey branch.

I haven’t tested it on a real hardware, only with the simulator. But we are using other precompiled libraries as well… the linker raises a warning about that, and said that future versions won’t support “unknown” binaries but I think tvOS 9.1 is Ok… but if it doesn’t work, please let me know.
Thanks.

What xCode version are you using? I’ve got this warning on the beta before tvos went official. The release version Version 7.2 (7C68) already threats these warnings as errors.

Nevertheless, even if Xcode compiles this correctly, it gets rejected by automated test after the submission. You get error via email like this:

Invalid Executable - The executable 'VPK_FULL_TV.app/VPK_FULL_TV' does not contain bitcode

So it is important to compile all the libraries with proper target/platform with the bitcode.
It is what @elvman did for most libs except for SpiderMonkey:
bin: https://github.com/elvman/cocos2d-x-3rd-party-libs-bin/tree/tvos
src: https://github.com/elvman/cocos2d-x-3rd-party-libs-src/tree/tvos

@pepeek:

Ok. thanks!

@elvman: could you send a PR so that we can include your changes in cocos2d-x-3rd-party-libs-src? Thanks.

I know I’m a bit persistent but would you have a hint how to modify the SpiderMonkey compilation script to compile it for tvos platform? I was not able to modify it properly. It is the very last bit to have Cocos2d-JS working for tvos.

@pepeek:
yesterday I compiled “master” branch from SpiderMonkey without any issues by using --with-ios-target=AppleTVOS.

What problem did you have with that? Did it compile it using bitcode? But I haven’t tested it.

Are you certain that you’ve truly compiled it for tvos? My reason for asking is that the js/src/configure file has case only for this:

case "$ios_target" in
iPhoneOS|iPhoneSimulator)

If you provide other unknown value in --with-ios-target= then it silently successfully compiles the lib but for default platform (thus not for required tvos).

I’ve tried to modify the script to make it work but that is my struggle which is described in detail in first post in this thread.

Please let me know if I’m wrong and you somehow managed to compile it properly.
You can test your output easily via file utility. It should tell you this: for architecture arm64.

There is another test for bitcode presence:

$ otool -l libName.o | grep __LLVM

Running the above command, if the library contains bitcode you will see segname __LLVM output.

@pepeek

Probably what I compiled wasn’t a valid tvOS binary. I’ll try again. thanks.

Hi, @pepeek

I think it’s not so straight forward to build spidermonkey with bitcode support. @WenHai have successfully build it for iOS with bitcode support, the result can be found here:

https://github.com/cocos2d/cocos2d-x-3rd-party-libs-bin/blob/cocos-runtime/ios/libs/libjs_static.a

But I’m not familiar with what he has done. I will contact him to reply in this topic.

@pepeek You can make xcodebuild build SprideMonkey with bitcode by adding CFLAGS with CFLAGS="-fembed-bitcode". The configuration file path is ‘build/autoconf/ios.m4’.

I put the build result here: http://git.oschina.net/linwenhai/runtime-external/tree/master/ios/libs/libjs_static.a

This is great. Where do I find the ios.m4 file?
If you’ve managed to add the bitcode then switching the target from iOS to tvOS should not be that difficult. Right? :wink:
It is easy in xCode but I’ve got completely lost in the original SpiderMonkey build files, see the beginning of this post.

In build/autoconf/ios.m4

Did you have any chance to try to compile it for tvOS target?
Would you give me at least some hints?
I’ve tried it before but with many compilation errors which I could not resolve.
Do you think that there is fundamental problem or is it just me being dumb?

My assumption was “just” replace the ios target with tvos but then the whole complicated configuration structure somehow broke…

Any hint of yours is greatly appreciated!

Does it include spidermonkey (JavaScript interpreter) compiled for tvOS?
I did not find it. Thanks.