Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/iwasm/app-samples/hello-world-cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required (VERSION 3.5)
project(hello_world)

add_library(print print.c)

add_executable(hello_world main.c)
target_link_libraries(hello_world print)
20 changes: 20 additions & 0 deletions core/iwasm/app-samples/hello-world-cmake/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

mkdir build
cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=../../../../test-tools/toolchain/wamr_toolchain.cmake
make
26 changes: 26 additions & 0 deletions core/iwasm/app-samples/hello-world-cmake/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "stdio.h"

void print_line(char* str);

int main()
{
print_line("Hello World!");
print_line("Wasm Micro Runtime");
return 0;
}
23 changes: 23 additions & 0 deletions core/iwasm/app-samples/hello-world-cmake/print.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "stdio.h"
#include "string.h"

void print_line(char* str)
{
printf("%s\n", str);
}
20 changes: 20 additions & 0 deletions doc/building.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,26 @@ clang-8 --target=wasm32 -O3 -Wl,--initial-memory=131072,--allow-undefined,--expo

You will get ```test.wasm``` which is the WASM app binary.

## Use cmake

If you have a cmake project, you can cross compile your project by using the toolchain provided by WAMR, the compiler used by WAMR toolchain is `clang-8`.

We can generate a `CMakeLists.txt` file for `test.c`:
```cmake
cmake_minimum_required (VERSION 3.5)
project(hello_world)
add_executable(hello_world test.c)
```
It is quite simple to build this project by cmake:
```Bash
mkdir build && cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=$WAMR_ROOT/test-tools/toolchain/wamr_toolchain.cmake
make
```
You will get ```hello_world``` which is the WASM app binary.

For more details about wamr toolchain, please refer to [test-tools/toolchain](../test-tools/toolchain/README.md).

## Using Docker

The last method availble is using [Docker](https://www.docker.com/). We assume you've already configured Docker (see Platform section above) and have a running interactive shell. Currently the Dockerfile only supports compiling apps with clang, with Emscripten planned for the future.
Expand Down
16 changes: 16 additions & 0 deletions test-tools/toolchain/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Cross Compile Toolchain for Wasm Micro Runtime
This folder contains sysroot and toolchain files for building wasm application by using cmake.

## Build a project
To build a C project into wasm, you may use the toolchain file provided here as `wamr_toolchain.cmake`:
```Bash
cmake /path/to/CMakeLists.txt -DCMAKE_TOOLCHAIN_FILE=/path/to/wamr_toolchain.cmake
make
```

## Generate a toolchain for your runtime
If you extend more APIs of wasm runtime by using `EXPORT_WASM_API` API, we also provide a tool which allow you to generate the toolchain for your runtime:
```Bash
./generate_toolchain.py -o out_dir -f api_list_file
```
A toolchain which enables your extended APIs should be generated in the path you specified.
124 changes: 124 additions & 0 deletions test-tools/toolchain/generate_toolchain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/usr/bin/python3

# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
import shutil
from optparse import OptionParser

import re

optParser = OptionParser()
optParser.add_option("-o", "", dest="output_dir", help="the output path of sysroot")
optParser.add_option("-f", "--api_file", dest="api_file_name",
help="append user defined APIs to toolchain")
(options, args) = optParser.parse_args()
optParser.usage = "%prog [options] output_dir"

sysroot_path = os.path.join(os.getcwd(), "sysroot")
if not os.path.isdir(sysroot_path):
print("Error: No sysroot folder in current path.")
exit(0)

if options.output_dir == None:
out_dir = sysroot_path
else:
out_dir = os.path.join(os.path.abspath(options.output_dir), "sysroot")

def check_sysroot():
if out_dir != sysroot_path and not os.path.isdir(out_dir):
try:
shutil.copytree(sysroot_path,out_dir)
except:
print('Error while copy sysroot')

def Search_API(pattern, file):
f = open(file, 'r')
content = f.read()
f.close()
p = re.compile(pattern, re.S | re.M)
return p.findall(content)

def fill_defined_symbols():
wamr_root = os.path.join(os.getcwd(), "../..")
user_lib_dir = os.path.join(wamr_root, "core/iwasm/lib/native/extension")

defined_symbols = []
# WAMR extension APIs
for lib in os.listdir(user_lib_dir):
for file in os.listdir(os.path.join(user_lib_dir, lib)):
if re.match(r'.*.inl', file):
defined_symbols += Search_API(r'EXPORT_WASM_API[(](.*?)[)]', os.path.join(user_lib_dir, lib, file))

# Base lib APIs
defined_symbols += Search_API(r'EXPORT_WASM_API[(](.*?)[)]',
os.path.join(wamr_root, "core/iwasm/lib/native/base", "base_lib_export.c"))

# libc
defined_symbols += Search_API(r'REG_NATIVE_FUNC[(]env, _(.*?)[)]',
os.path.join(wamr_root, "core/iwasm/lib/native/libc", "libc_wrapper.c"))

# User defined APIs
if options.api_file_name != None:
api_file = open(options.api_file_name)
APIs = api_file.read().split('\n')
if APIs != None and APIs != []:
defined_symbols += APIs

defined_symbols = [i for i in defined_symbols if len(i) != 0]
symbol_file = open(os.path.join(out_dir, "share/defined-symbols.txt"), 'w')
symbol_file.write('\n'.join(defined_symbols))
symbol_file.close()

def generate_toolchain_file():
cmake_content = """
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_PROCESSOR wasm32)
SET (CMAKE_SYSROOT {})

SET (CMAKE_C_FLAGS "-nostdlib" CACHE INTERNAL "")
SET (CMAKE_C_COMPILER_TARGET "wasm32")
SET (CMAKE_C_COMPILER "clang-8")

SET (CMAKE_CXX_FLAGS "-nostdlib" CACHE INTERNAL "")
SET (CMAKE_CXX_COMPILER_TARGET "wasm32")
SET (CMAKE_CXX_COMPILER "clang++-8")

SET (CMAKE_EXE_LINKER_FLAGS "-Wl,--no-entry,--export-all,--allow-undefined-file=${{CMAKE_SYSROOT}}/share/defined-symbols.txt" CACHE INTERNAL "")
SET (CMAKE_LINKER "/usr/bin/wasm-ld-8" CACHE INTERNAL "")

SET (CMAKE_AR "/usr/bin/llvm-ar-8" CACHE INTERNAL "")
SET (CMAKE_NM "/usr/bin/llvm-nm-8" CACHE INTERNAL "")
SET (CMAKE_OBJDUMP "/usr/bin/llvm-objdump-8" CACHE INTERNAL "")
SET (CMAKE_RANLIB "/usr/bin/llvm-ranlib-8" CACHE INTERNAL "")
""".format(out_dir)
f = open(os.path.join(out_dir, "..", "wamr_toolchain.cmake"), 'w')
f.write(cmake_content)
f.close()

def main():
check_sysroot()
fill_defined_symbols()
generate_toolchain_file()

print("Successfully generate wamr toolchain")
print("Now you can use this command to build your cmake based project:")
print("cmake /path/to/CMakeLists.txt -DCMAKE_TOOLCHAIN_FILE={}"
.format(os.path.abspath(os.path.join(out_dir, "..", "wamr_toolchain.cmake"))))

if __name__ == '__main__':
main()

31 changes: 31 additions & 0 deletions test-tools/toolchain/sysroot/include/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef _WAMR_LIBC_ASSERT_H
#define _WAMR_LIBC_ASSERT_H

#ifdef __cplusplus
extern "C" {
#endif




#ifdef __cplusplus
}
#endif

#endif
31 changes: 31 additions & 0 deletions test-tools/toolchain/sysroot/include/ctype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef _WAMR_LIBC_CTYPE_H
#define _WAMR_LIBC_CTYPE_H

#ifdef __cplusplus
extern "C" {
#endif




#ifdef __cplusplus
}
#endif

#endif
31 changes: 31 additions & 0 deletions test-tools/toolchain/sysroot/include/errno.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef _WAMR_LIBC_ERRNO_H
#define _WAMR_LIBC_ERRNO_H

#ifdef __cplusplus
extern "C" {
#endif




#ifdef __cplusplus
}
#endif

#endif
31 changes: 31 additions & 0 deletions test-tools/toolchain/sysroot/include/fcntl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef _WAMR_LIBC_FCNTL_H
#define _WAMR_LIBC_FCNTL_H

#ifdef __cplusplus
extern "C" {
#endif




#ifdef __cplusplus
}
#endif

#endif
Loading