From 76a242d822b53243ac5d25b5533fcf6a799d881e Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Mon, 21 Jun 2021 23:58:07 +0300 Subject: [PATCH] Update README and example project. --- CMakeLists.txt | 5 ----- README.md | 48 +++++++++++++++++------------------------- example/CMakeLists.txt | 43 ++++++++++++++++++++++++------------- example/js/main.js | 2 +- 4 files changed, 48 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f632b9f..d599ffe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,6 @@ cmake_minimum_required(VERSION 3.16) project(screepsxx CXX) 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_REQUIRED TRUE) @@ -27,7 +26,3 @@ if (SCREEPSXX_ENABLE_PCH) ) endif() - -if (SCREEPSXX_BUILD_EXAMPLE) - add_subdirectory(example) -endif() diff --git a/README.md b/README.md index cc1c4fe..7e962df 100644 --- a/README.md +++ b/README.md @@ -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 * (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 +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 -git clone https://github.com/UltraCoderRU/screepsxx.git -cd screepsxx mkdir build cd build -cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_BUILD_TYPE=Release .. +cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake .. +``` + +5. Build project, collect and upload artifacts. +```shell 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. diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 85af810..830b8df 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -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) -target_link_libraries(example screepsxx) -target_link_options(example PUBLIC -sMODULARIZE=1 --no-entry --bind) +project(example 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) + +# 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 -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) -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 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 example POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/js/wasm_loader.js ${CMAKE_CURRENT_SOURCE_DIR}/dist/wasm_loader.js) +# 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 ${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 ${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 ${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 -# official Screeps server using Python script in 'tools' directory. -# To do so, uncomment following lines and set SCREEPS_TOKEN environment -# variable to your Screeps API token (https://docs.screeps.com/auth-tokens.html). -# -#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}) +# Following post-build step will automatically upload artifacts to +# official Screeps server. If you want to use it, please, +# set SCREEPS_TOKEN environment variable to your Screeps API token +# (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) +add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${Python_EXECUTABLE} ${screepsxx_SOURCE_DIR}/tools/upload.py ${CMAKE_SOURCE_DIR}/dist $ENV{SCREEPS_TOKEN}) diff --git a/example/js/main.js b/example/js/main.js index 91a598c..e465b62 100644 --- a/example/js/main.js +++ b/example/js/main.js @@ -3,7 +3,7 @@ const wasm_loader = require('wasm_loader') var mod; -wasm_loader('my_loader', 'my_module').then((instance) => { +wasm_loader('app_loader', 'app_module').then((instance) => { console.log("WASM module loaded."); mod = instance; });