From 4391924ac5eaa496aa5efcc94765823e292e8964 Mon Sep 17 00:00:00 2001 From: Kai Ninomiya Date: Tue, 17 Sep 2019 23:08:45 -0700 Subject: [PATCH] Separate GLSLANG_WEB (min-size build) and Emscripten options Allows building non-min-size builds with Emscripten. Adds ENABLE_GLSLANG_WEB_DEVEL. Moves the glslang.js files to OSDependent/Web. Small cleanups and docs update. --- CMakeLists.txt | 70 +++++++++++--------- README.md | 24 ++++--- glslang/CMakeLists.txt | 17 ++--- glslang/OSDependent/Web/CMakeLists.txt | 10 +++ glslang/{ => OSDependent/Web}/glslang.js.cpp | 11 ++- glslang/{ => OSDependent/Web}/glslang.pre.js | 0 glslang/updateGrammar | 2 +- 7 files changed, 71 insertions(+), 63 deletions(-) create mode 100644 glslang/OSDependent/Web/CMakeLists.txt rename glslang/{ => OSDependent/Web}/glslang.js.cpp (98%) rename glslang/{ => OSDependent/Web}/glslang.pre.js (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 196194d1..150ae7af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,9 +28,10 @@ option(ENABLE_SPVREMAPPER "Enables building of SPVRemapper" ON) option(ENABLE_GLSLANG_BINARIES "Builds glslangValidator and spirv-remap" ON) -option(ENABLE_GLSLANG_WEB "Reduces glslang to minumum needed for web use" OFF) -option(ENABLE_EMSCRIPTEN_SINGLE_FILE "If using emscripten, enables SINGLE_FILE build" OFF) -option(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE "If using emscripten, builds to run on Node instead of Web" OFF) +option(ENABLE_GLSLANG_WEB "Reduces glslang to minimum needed for web use" OFF) +option(ENABLE_GLSLANG_WEB_DEVEL "For ENABLE_GLSLANG_WEB builds, enables compilation error messages" OFF) +option(ENABLE_EMSCRIPTEN_SINGLE_FILE "If using Emscripten, enables SINGLE_FILE build" OFF) +option(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE "If using Emscripten, builds to run on Node instead of Web" OFF) CMAKE_DEPENDENT_OPTION(ENABLE_HLSL "Enables HLSL input support" ON "NOT ENABLE_GLSLANG_WEB" OFF) @@ -70,6 +71,9 @@ endif(ENABLE_HLSL) if(ENABLE_GLSLANG_WEB) add_definitions(-DGLSLANG_WEB) + if(ENABLE_GLSLANG_WEB_DEVEL) + add_definitions(-DGLSLANG_WEB_DEVEL) + endif(ENABLE_GLSLANG_WEB_DEVEL) endif(ENABLE_GLSLANG_WEB) if(WIN32) @@ -98,38 +102,38 @@ elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "MSVC") add_compile_options(/GR-) # Disable RTTI endif() -if(ENABLE_GLSLANG_WEB) - if(EMSCRIPTEN) - add_compile_options(-Os -fno-exceptions) - add_compile_options("SHELL: -s WASM=1") - add_compile_options("SHELL: -s WASM_OBJECT_FILES=0") - add_link_options(-Os) - add_link_options("SHELL: -s FILESYSTEM=0") - add_link_options("SHELL: --llvm-lto 1") - add_link_options("SHELL: --closure 1") - add_link_options("SHELL: -s ALLOW_MEMORY_GROWTH=1") - - add_link_options("SHELL: -s MODULARIZE=1") - if(ENABLE_EMSCRIPTEN_SINGLE_FILE) - add_link_options("SHELL: -s SINGLE_FILE=1") - endif(ENABLE_EMSCRIPTEN_SINGLE_FILE) - - if(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE) - add_link_options("SHELL: -s ENVIRONMENT=node") - add_link_options("SHELL: -s BINARYEN_ASYNC_COMPILATION=0") +if(EMSCRIPTEN) + add_compile_options(-Os -fno-exceptions) + add_compile_options("SHELL: -s WASM=1") + add_compile_options("SHELL: -s WASM_OBJECT_FILES=0") + add_link_options(-Os) + add_link_options("SHELL: -s FILESYSTEM=0") + add_link_options("SHELL: --llvm-lto 1") + add_link_options("SHELL: --closure 1") + add_link_options("SHELL: -s ALLOW_MEMORY_GROWTH=1") + + add_link_options("SHELL: -s MODULARIZE=1") + if(ENABLE_EMSCRIPTEN_SINGLE_FILE) + add_link_options("SHELL: -s SINGLE_FILE=1") + endif(ENABLE_EMSCRIPTEN_SINGLE_FILE) + + if(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE) + add_link_options("SHELL: -s ENVIRONMENT=node") + add_link_options("SHELL: -s BINARYEN_ASYNC_COMPILATION=0") + else() + add_link_options("SHELL: -s ENVIRONMENT=web,worker") + add_link_options("SHELL: -s EXPORT_ES6=1") + endif() +else() + if(ENABLE_GLSLANG_WEB) + if(MSVC) + add_compile_options(/Os /GR-) else() - add_link_options("SHELL: -s ENVIRONMENT=web,worker") - add_link_options("SHELL: -s EXPORT_ES6=1") + add_compile_options(-Os -fno-exceptions) + add_link_options(-Os) endif() - else() - if(MSVC) - add_compile_options(/Os /GR-) - else() - add_compile_options(-Os -fno-exceptions) - add_link_options(-Os) - endif() - endif(EMSCRIPTEN) -endif(ENABLE_GLSLANG_WEB) + endif(ENABLE_GLSLANG_WEB) +endif(EMSCRIPTEN) # Request C++11 if(${CMAKE_VERSION} VERSION_LESS 3.1) diff --git a/README.md b/README.md index 427cf5a5..886f091a 100755 --- a/README.md +++ b/README.md @@ -166,26 +166,30 @@ when executed from the glslang subdirectory of the glslang repository. With no arguments it builds the full grammar, and with a "web" argument, the web grammar subset (see more about the web subset in the next section). -### WASM for the the Web +### Building to WASM for the Web and Node Use the steps in [Build Steps](#build-steps), which following notes/exceptions: * For building the web subset of core glslang: - + `m4` also needs a `-DGLSLANG_WEB` argument, or simply execute `updateGrammar web` from the glslang subdirectory - + turn off the CMAKE options for `BUILD_TESTING`, `ENABLE_OPT`, and `INSTALL_GTEST`, - while turning on `ENABLE_GLSLANG_WEB` + + update the grammar: run `m4` with `-DGLSLANG_WEB`, or simply execute + `updateGrammar web` from the glslang subdirectory + + set `-DENABLE_HLSL=OFF -DBUILD_TESTING=OFF -DENABLE_OPT=OFF -DINSTALL_GTEST=OFF` + + turn on `-DENABLE_GLSLANG_WEB=ON` + + optionally, for GLSL compilation error messages, turn on `-DENABLE_GLSLANG_WEB_DEVEL=ON` * `emsdk` needs to be present in your executable search path, *PATH* for Bash-like enivironments + Instructions located [here](https://emscripten.org/docs/getting_started/downloads.html#sdk-download-and-install) -* Do not checkout SPIRV-Tools into `External` - + Does not work correctly with emscripten out of the box and we don't want it - in the build anyway. *TBD* Have build ignore SPIRV-Tools for web build -* Wrap call to `cmake` using `emconfigure` with ENABLE_GLSLANG_WEB=ON: - + e.g. For Linux, `emconfigure cmake -DCMAKE_BUILD_TYPE=Release - -DENABLE_GLSLANG_WEB=ON -DCMAKE_INSTALL_PREFIX="$(pwd)/install" ..` +* Wrap cmake call: `emcmake cmake` * To get a 'true' minimized build, make sure to use `brotli` to compress the .js and .wasm files +Example: + +```sh +emcmake cmake -DCMAKE_BUILD_TYPE=Release -DENABLE_GLSLANG_WEB=ON \ + -DENABLE_HLSL=OFF -DBUILD_TESTING=OFF -DENABLE_OPT=OFF -DINSTALL_GTEST=OFF .. +``` + Testing ------- diff --git a/glslang/CMakeLists.txt b/glslang/CMakeLists.txt index 73124c56..930893c2 100644 --- a/glslang/CMakeLists.txt +++ b/glslang/CMakeLists.txt @@ -6,6 +6,10 @@ else(WIN32) message("unknown platform") endif(WIN32) +if(EMSCRIPTEN OR ENABLE_GLSLANG_WEB) + add_subdirectory(OSDependent/Web) +endif(EMSCRIPTEN OR ENABLE_GLSLANG_WEB) + set(SOURCES MachineIndependent/glslang.m4 MachineIndependent/glslang.y @@ -114,16 +118,3 @@ if(ENABLE_GLSLANG_INSTALL) install(FILES ${file} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/glslang/${dir}) endforeach() endif(ENABLE_GLSLANG_INSTALL) - -if(ENABLE_GLSLANG_WEB) - add_executable(glslang.js glslang.js.cpp) - glslang_set_link_args(glslang.js) - target_link_libraries(glslang.js glslang SPIRV) - if(EMSCRIPTEN) - set_target_properties(glslang.js PROPERTIES - OUTPUT_NAME "glslang" - SUFFIX ".js" - LINK_FLAGS "--bind -s EXPORT_NAME=\"glslangModule\"") - em_link_pre_js(glslang.js ${CMAKE_CURRENT_SOURCE_DIR}/glslang.pre.js) - endif(EMSCRIPTEN) -endif(ENABLE_GLSLANG_WEB) diff --git a/glslang/OSDependent/Web/CMakeLists.txt b/glslang/OSDependent/Web/CMakeLists.txt new file mode 100644 index 00000000..c73c345f --- /dev/null +++ b/glslang/OSDependent/Web/CMakeLists.txt @@ -0,0 +1,10 @@ +add_executable(glslang.js "glslang.js.cpp") +glslang_set_link_args(glslang.js) +target_link_libraries(glslang.js glslang SPIRV) +if(EMSCRIPTEN) + set_target_properties(glslang.js PROPERTIES + OUTPUT_NAME "glslang" + SUFFIX ".js" + LINK_FLAGS "--bind -s EXPORT_NAME=\"glslangModule\"") + em_link_pre_js(glslang.js "${CMAKE_CURRENT_SOURCE_DIR}/glslang.pre.js") +endif(EMSCRIPTEN) diff --git a/glslang/glslang.js.cpp b/glslang/OSDependent/Web/glslang.js.cpp similarity index 98% rename from glslang/glslang.js.cpp rename to glslang/OSDependent/Web/glslang.js.cpp index 45b3d3f6..6cb93fe2 100644 --- a/glslang/glslang.js.cpp +++ b/glslang/OSDependent/Web/glslang.js.cpp @@ -35,17 +35,16 @@ #include #include +#include #ifdef __EMSCRIPTEN__ #include -#endif // __EMSCRIPTEN__ -#include +#endif -#include "../SPIRV/GlslangToSpv.h" -#include "../SPIRV/doc.h" -#include "./../glslang/Public/ShaderLang.h" +#include "../../../SPIRV/GlslangToSpv.h" +#include "../../../glslang/Public/ShaderLang.h" -#ifndef EMSCRIPTEN_KEEPALIVE +#ifndef __EMSCRIPTEN__ #define EMSCRIPTEN_KEEPALIVE #endif diff --git a/glslang/glslang.pre.js b/glslang/OSDependent/Web/glslang.pre.js similarity index 100% rename from glslang/glslang.pre.js rename to glslang/OSDependent/Web/glslang.pre.js diff --git a/glslang/updateGrammar b/glslang/updateGrammar index 37794587..9384db94 100755 --- a/glslang/updateGrammar +++ b/glslang/updateGrammar @@ -1,4 +1,4 @@ -#!/usr/bin/bash +#!/bin/bash if [ "$1" = 'web' ] then -- 2.34.1