Skip to content
This repository was archived by the owner on Nov 1, 2020. It is now read-only.

Build native code for WebAssembly target - #4624

Merged
morganbr merged 13 commits into
dotnet:masterfrom
ph1ll:wasm_cmake_build
Oct 31, 2017
Merged

Build native code for WebAssembly target#4624
morganbr merged 13 commits into
dotnet:masterfrom
ph1ll:wasm_cmake_build

Conversation

@ph1ll

@ph1ll ph1ll commented Sep 27, 2017

Copy link
Copy Markdown
Contributor

This is very much a work in progress, but adding the PR so that changes are visible.

At the moment, the overall wasm build will fail. See #4620 for the related build scripts.

* [ ] Build sdk\libbootstrapper.bc
* [ ] Build sdk\libRuntime.bc (#4505)
* [ ] Build tools\jitinterface.bc

@dnfclas

dnfclas commented Sep 27, 2017

Copy link
Copy Markdown

@ph1ll,
Thanks for having already signed the Contribution License Agreement. Your agreement was validated by .NET Foundation. We will now review your pull request.
Thanks,
.NET Foundation Pull Request Bot

@jkotas

jkotas commented Sep 27, 2017

Copy link
Copy Markdown
Member

You only need: libbootstrappercpp.bc, libPortableRuntime.bc and libSystem.Private.CoreLib.Native.bc . The rest are not applicable for the WebAssembly target.

elseif(CLR_CMAKE_PLATFORM_ARCH_ARM)
set(ARCH_SOURCES_DIR arm)
set(ASM_SUFFIX S)
elseif(CLR_CMAKE_PLATFORM_ARCH_WASM)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not be needed.

Comment thread src/Native/jitinterface/CMakeLists.txt Outdated
SHARED
${NATIVE_SOURCES}
)
if(CMAKE_SYSTEM_NAME STREQUAL Emscripten)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exclude jitinterface in src\Native\CMakeLists.txt instead

@jkotas
jkotas requested a review from morganbr September 27, 2017 17:24
@ph1ll

ph1ll commented Sep 27, 2017

Copy link
Copy Markdown
Contributor Author

Thanks @jkotas. I have updated the cmake files to conditionally exclude the non-applicable libraries. Should make it a bit easier to focus on getting the rest to compile.

#else
uuid_generate_random(*(uuid_t*)pGuid);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Unnecessary white space change

// Change the byte order of the Data1, 2 and 3, since the uuid_generate_random
// generates them with big endian while GUIDS need to have them in little endian.
pGuid->Data1 = SWAP32(pGuid->Data1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think this swapping should be done for uuid_generate as well.

extern "C" void CoreLibNative_CreateGuid(GUID* pGuid)
{
#if HAVE_LIBUUID_H
#ifdef _WASM_

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to do this using HAVE_XXX autogenerated defines for portability, not hard coding it to WASM.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

My only concern here would be that uuid_generate_random will always provide version 4 UUIDs, where as uuid_generate will fall back to version 1 UUIDs, based on current time and MAC address. Where as we know Emscripten implements uuid_generate using version 4 UUIDs, this could result in information leakage on other platforms in the future.

Comment thread src/Native/CMakeLists.txt Outdated
add_subdirectory(jitinterface)

# We don't need jitinterface for WASM.
if(NOT CMAKE_SYSTEM_NAME STREQUAL Emscripten)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we rather use CLR_CMAKE_PLATFORM_ARCH_WASM for these checks? (It would be self-explanatory at least - no need to have We don't need jitinterface for WASM. comment)

Comment thread src/Native/Bootstrap/CMakeLists.txt Outdated

add_subdirectory(base)
# We don't need base for WASM.
if(NOT CMAKE_SYSTEM_NAME STREQUAL Emscripten)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have the WASM build variable available here? Based on some of the other comments, it's not clear if emcmake is a long term plan, so it might be better to switch based on the target rather than the cmake name

Comment thread src/Native/CMakeLists.txt Outdated

if(CMAKE_SYSTEM_NAME STREQUAL Emscripten)
set(CLR_CMAKE_PLATFORM_UNIX 1)
set(CLR_CMAKE_PLATFORM_UNIX_TARGET_WASM 1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebAssembly isn't really UNIX, would it make sense for this variable to just be called CLR_CMAKE_PLATFORM_WASM ?

@ph1ll

ph1ll commented Sep 30, 2017

Copy link
Copy Markdown
Contributor Author

I have rebased this on master so that the build scripts are present.

@jkotas

jkotas commented Sep 30, 2017

Copy link
Copy Markdown
Member

OSX build failed with:

error: Don't know how to generate UUID on this platform

Other failures are infrastructure issues - not related to your change.

@ph1ll

ph1ll commented Oct 2, 2017

Copy link
Copy Markdown
Contributor Author

I dont have an OS X machine for testing on unfortunately. I have instead switched to a different method of checking for the uuid functions in the uuid/uuid.h header that doesnt require linking the right libraries for the check. Hopefully this unbreaks the build on OS X.

@morganbr

morganbr commented Oct 3, 2017

Copy link
Copy Markdown
Contributor

@dotnet-bot build and test Windows_NT Debug and CoreCLR tests please
@dotnet-bot build and test Windows_NT Release please

@morganbr

morganbr commented Oct 3, 2017

Copy link
Copy Markdown
Contributor

@ph1ll Looks like you're clean on all OSes now.

#include <uuid/uuid.h>
int main()
{
#ifndef uuid_generate_random

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hack does not look like improvement. I would rather take the WASM ifdef that you had originally than this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were you happy with the check_function_exists usage as below? This fails on OS X but I should imagine it is case of linking the correct library for the check. I don't have a OS X machine to test with at the moment, but if you bear with me I can try and sort something out.

set(CMAKE_REQUIRED_LIBRARIES uuid)
check_function_exists(uuid_generate_random HAVE_UUID_GENERATE_RANDOM)
check_function_exists(uuid_generate HAVE_UUID_GENERATE)
set(CMAKE_REQUIRED_LIBRARIES)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the check_function_exists usage looked much better.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ph1ll , if you need to commit and watch the CI results a couple times to figure out OS X, that's totally fine. I'll also try to get a build on a Mac soon, but it's probably easier for you to just iterate with CI.

@morganbr

Copy link
Copy Markdown
Contributor

@ph1ll, is there anything you need help with? I'd really love to get the runtime compiling 😄

@ph1ll

ph1ll commented Oct 18, 2017

Copy link
Copy Markdown
Contributor Author

@morganbr Sorry I've had a couple of busy weeks with work and haven't had a chance to do anymore yet I am afraid. I have some time next week, but I certainly do not want to hold anything up. I've added you to as a collaborator to my fork, but also feel free to take what is already here if you want to get things moving.

At the moment, there is the cmake OSX issue to look at, and about 40 or so initial build errors for libPortableRuntime. Some of the build errors relate to simple defines, but I haven't had a chance to go beyond that at the moment.

@morganbr

Copy link
Copy Markdown
Contributor

I've got libPortableRuntime and libRuntime building using these changes and I can link it to the wasm test program. I had to touch a lot of files in the runtime, so I'll create a separate PR. I only needed a couple tiny changes to your build system changes, so hopefully we can just deal with the merge conflict and UUID issue to merge this change.

@morganbr

Copy link
Copy Markdown
Contributor

I tried to point to libc for UUID for Mac, but that seems to have broken both Mac and Linux (Emscripten still works). Any ideas @ph1ll or @jkotas?

@jkotas

jkotas commented Oct 30, 2017

Copy link
Copy Markdown
Member

The libraries are specified without libc prefix, so it would be just c, not libc.

If it still does not work, I think the following should be good enough:

if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
   set(CMAKE_REQUIRED_LIBRARIES uuid)
endif()
check_function_exists(uuid_generate_random HAVE_UUID_GENERATE_RANDOM) 
check_function_exists(uuid_generate HAVE_UUID_GENERATE) 
set(CMAKE_REQUIRED_LIBRARIES) 

@morganbr

Copy link
Copy Markdown
Contributor

Changing libc to c didn't work, trying the conditional required libraries check now.

if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(CMAKE_REQUIRED_LIBRARIES uuid)
endif()
unset(HAVE_UUID_GENERATE_RANDOM CACHE)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These unset commands should not be needed.

@morganbr morganbr changed the title WIP: Build native code for WebAssembly target Build native code for WebAssembly target Oct 31, 2017
@morganbr

Copy link
Copy Markdown
Contributor

Thanks for getting most of this together, @ph1ll! When you're less busy, there's lots more that we'd love your help with if you're interested.

@morganbr
morganbr merged commit 15ce20d into dotnet:master Oct 31, 2017
@ph1ll

ph1ll commented Oct 31, 2017

Copy link
Copy Markdown
Contributor Author

@morganbr I think you did the hard part, sorry again for not being able to get it finished up. Will definitely try and help out where I can.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants