Update README and example project.

This commit is contained in:
Kirill Kirilenko 2021-06-21 23:58:07 +03:00
parent 74641bb584
commit 76a242d822
4 changed files with 48 additions and 50 deletions

View file

@ -3,7 +3,6 @@ cmake_minimum_required(VERSION 3.16)
project(screepsxx CXX) project(screepsxx CXX)
option(SCREEPSXX_ENABLE_PCH "Enable precompiled headers" ON) option(SCREEPSXX_ENABLE_PCH "Enable precompiled headers" ON)
option(SCREEPSXX_BUILD_EXAMPLE "Build example WASM application" OFF)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE) set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
@ -27,7 +26,3 @@ if (SCREEPSXX_ENABLE_PCH)
<emscripten/val.h> <emscripten/val.h>
) )
endif() endif()
if (SCREEPSXX_BUILD_EXAMPLE)
add_subdirectory(example)
endif()

View file

@ -9,37 +9,27 @@ It provides wrappers for the majority of classes in [Screeps API](https://docs.s
* [Ninja](https://ninja-build.org) build system * [Ninja](https://ninja-build.org) build system
* (Optional) Python 3 interpreter for artifacts uploading * (Optional) Python 3 interpreter for artifacts uploading
## Building ## Using library in CMake project
1. Copy all files from `example` directory into your project directory.
2. Place library sources somewhere inside your project directory.
If you use git, the recommended way is to add library repository as a submodule.
Or instead you can manually download and extract library files somewhere inside your project.
```shell
cd <your_project_repo>
git submodule add https://github.com/UltraCoderRU/screepsxx.git
```
3. Check top-level `CMakeLists.txt`.
You can disable automatic artifacts uploading, etc.
4. Configure project. You need to pass location of Emscripten SDK to CMake command.
Also, always use Release build type! Debug version will not help you to debug WebAssembly code, but its size most probably will exceed 5MB limit of official Screeps server.
```shell ```shell
git clone https://github.com/UltraCoderRU/screepsxx.git
cd screepsxx
mkdir build mkdir build
cd build cd build
cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=<path_to_emsdk>/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_BUILD_TYPE=Release .. cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=<path_to_emsdk>/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake ..
```
5. Build project, collect and upload artifacts.
```shell
cmake --build . cmake --build .
# Add -DSCREEPSXX_BUILD_EXAMPLE=ON to build example application (see 'example' directory)
``` ```
## Using library in CMake project
The simplest way to use screepsxx library is to add it to your project with `add_subdirectory()`:
```cmake
cmake_minimum_required(VERSION 3.16)
project(MyProject CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s STRICT=0 -s ASSERTIONS=0 -s ALLOW_MEMORY_GROWTH=1 -s ENVIRONMENT=shell -s MALLOC=emmalloc --cache ${CMAKE_BINARY_DIR}/cache")
add_subdirectory(screepsxx)
add_executable(myapp loop.cpp)
target_link_libraries(myapp screepsxx)
target_link_options(myapp PUBLIC -sMODULARIZE=1 --no-entry --bind)
```
Compiled WASM-module (`myapp.wasm`) and corresponding JavaScript module (`myapp.js`) will appear in your build directory (`CMAKE_BINARY_DIR`).
To use them inside Screeps, you need two additional JavaScript modules located in `js` directory.
Put all four files into one directory and upload them to Screeps server.
You can use Python script `tools\upload.py`, see `example/CMakeLists.txt` for details.

View file

@ -1,19 +1,32 @@
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s ENVIRONMENT=shell") cmake_minimum_required(VERSION 3.16)
add_executable(example loop.cpp) project(example CXX)
target_link_libraries(example screepsxx)
target_link_options(example PUBLIC -sMODULARIZE=1 --no-entry --bind) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s STRICT=0 -s ASSERTIONS=0 -s ALLOW_MEMORY_GROWTH=1 -s ENVIRONMENT=shell -s MALLOC=emmalloc --cache ${CMAKE_BINARY_DIR}/cache")
add_subdirectory(screepsxx)
# If you change TARGET_NAME, please, make corresponding changes in main.js.
set(TARGET_NAME app)
add_executable(${TARGET_NAME} loop.cpp)
target_link_libraries(${TARGET_NAME} screepsxx)
target_link_options(${TARGET_NAME} PUBLIC -sMODULARIZE=1 --no-entry --bind)
# Collect all artifacts in 'dist' directory # Collect all artifacts in 'dist' directory
add_custom_command(TARGET example POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/example.wasm ${CMAKE_CURRENT_SOURCE_DIR}/dist/my_module.wasm) # WASM-module and corresponding JS-module must have different base names in order to use them in Screeps, so we add suffixes.
add_custom_command(TARGET example POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/example.js ${CMAKE_CURRENT_SOURCE_DIR}/dist/my_loader.js) add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/${TARGET_NAME}.wasm ${CMAKE_SOURCE_DIR}/dist/${TARGET_NAME}_module.wasm)
add_custom_command(TARGET example POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/js/main.js ${CMAKE_CURRENT_SOURCE_DIR}/dist/main.js) add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/${TARGET_NAME}.js ${CMAKE_SOURCE_DIR}/dist/${TARGET_NAME}_loader.js)
add_custom_command(TARGET example POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/js/wasm_loader.js ${CMAKE_CURRENT_SOURCE_DIR}/dist/wasm_loader.js) add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/js/main.js ${CMAKE_SOURCE_DIR}/dist/main.js)
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/js/wasm_loader.js ${CMAKE_SOURCE_DIR}/dist/wasm_loader.js)
# You can add post-build step to automatically upload artifacts to # Following post-build step will automatically upload artifacts to
# official Screeps server using Python script in 'tools' directory. # official Screeps server. If you want to use it, please,
# To do so, uncomment following lines and set SCREEPS_TOKEN environment # set SCREEPS_TOKEN environment variable to your Screeps API token
# variable to your Screeps API token (https://docs.screeps.com/auth-tokens.html). # (https://docs.screeps.com/auth-tokens.html).
# # If you don't want to use this script, please, remove following lines.
#find_package(Python COMPONENTS Interpreter REQUIRED) find_package(Python COMPONENTS Interpreter REQUIRED)
#add_custom_command(TARGET example POST_BUILD COMMAND ${Python_EXECUTABLE} ${screepsxx_SOURCE_DIR}/tools/upload.py ${CMAKE_CURRENT_SOURCE_DIR}/dist $ENV{SCREEPS_TOKEN}) add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${Python_EXECUTABLE} ${screepsxx_SOURCE_DIR}/tools/upload.py ${CMAKE_SOURCE_DIR}/dist $ENV{SCREEPS_TOKEN})

View file

@ -3,7 +3,7 @@
const wasm_loader = require('wasm_loader') const wasm_loader = require('wasm_loader')
var mod; var mod;
wasm_loader('my_loader', 'my_module').then((instance) => { wasm_loader('app_loader', 'app_module').then((instance) => {
console.log("WASM module loaded."); console.log("WASM module loaded.");
mod = instance; mod = instance;
}); });