NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.93k stars 13.95k forks source link

Emscripten missing some required npm dependencies for size optimizations #323598

Open JackWBoynton opened 3 months ago

JackWBoynton commented 3 months ago

Describe the bug

When trying to compile a minimal C++ example to JS with emscripten via the nix package I get:

em++: error: html-minifier-terser was not found! Please run "npm install" in Emscripten root directory to set up npm dependencies
make[2]: *** [CMakeFiles/EmscriptenNixDev.dir/build.make:98: EmscriptenNixDev.html] Error 1
make[2]: *** Deleting file 'EmscriptenNixDev.html'
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/EmscriptenNixDev.dir/all] Error 2
make: *** [Makefile:136: all] Error 2

It looks like emscripten tries to run html-minifier-terser from the node_modules/.bin directory which does not exist relative to the emscripten store in share/emscripten/node_modules.

the relevant emcc.py line: https://github.com/emscripten-core/emscripten/blob/423a77f9be2a26d5a86dd9e4aec4e095cd73fb70/emcc.py#L3724

Steps To Reproduce

Steps to reproduce the behavior:

  1. Simple main.cpp:
    #include <emscripten.h>
    int main() { return 0; }
  2. CMakeLists.txt:
    
    cmake_minimum_required(VERSION 3.5)
    project(EmscriptenNixDev)

set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # clangd

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -flto") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -O3")

set(CMAKE_EXECUTABLE_SUFFIX ".html") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s USE_GLFW=3 --shell-file ${CMAKE_CURRENT_SOURCE_DIR}/index.html -sWASM=1 -s ASYNCIFY=1" ) add_executable(EmscriptenNixDev main.cpp)

install(TARGETS EmscriptenNixDev RUNTIME DESTINATION bin)

4. Fresh nix env
```sh
docker run -it --rm -v .:/app docker.nix-community.org/nixpkgs/cachix-flakes
  1. Add emscripten + deps
    NIX_PATH=nixpkgs=channel:nixos-unstable nix-shell -p emscripten python3 cmake
  2. Build
    mkdir build
    cd build
    emcmake cmake ..
  3. Output
    [ 50%] Building CXX object CMakeFiles/EmscriptenNixDev.dir/main.cpp.o
    [100%] Linking CXX executable EmscriptenNixDev.html
    cache:INFO: generating system asset: symbol_lists/5788af184bd9f224858e62d950018500aedeccd3.json... (this will be cached in "/nix/store/cacfb89l26hya9wzfhjn52w2cyfgbgkg-emscripten-3.1.51/share/emscripten/cache/symbol_lists/5788af184bd9f224858e62d950018500aedeccd3.json" for subsequent builds)
    cache:INFO:  - ok
    cache:INFO: generating system library: sysroot/lib/wasm32-emscripten/lto/libGL-webgl2.a... (this will be cached in "/nix/store/cacfb89l26hya9wzfhjn52w2cyfgbgkg-emscripten-3.1.51/share/emscripten/cache/sysroot/lib/wasm32-emscripten/lto/libGL-webgl2.a" for subsequent builds)
    system_libs:INFO: compiled 4 inputs in 0.12s
    cache:INFO:  - ok
    cache:INFO: generating system library: sysroot/lib/wasm32-emscripten/lto/libstubs.a... (this will be cached in "/nix/store/cacfb89l26hya9wzfhjn52w2cyfgbgkg-emscripten-3.1.51/share/emscripten/cache/sysroot/lib/wasm32-emscripten/lto/libstubs.a" for subsequent builds)
    system_libs:INFO: compiled 2 inputs in 0.12s
    cache:INFO:  - ok
    cache:INFO: generating system library: sysroot/lib/wasm32-emscripten/lto/libc.a... (this will be cached in "/nix/store/cacfb89l26hya9wzfhjn52w2cyfgbgkg-emscripten-3.1.51/share/emscripten/cache/sysroot/lib/wasm32-emscripten/lto/libc.a" for subsequent builds)
    system_libs:INFO: compiled 1025 inputs in 5.80s
    cache:INFO:  - ok
    cache:INFO: generating system library: sysroot/lib/wasm32-emscripten/lto/libc++abi-noexcept.a... (this will be cached in "/nix/store/cacfb89l26hya9wzfhjn52w2cyfgbgkg-emscripten-3.1.51/share/emscripten/cache/sysroot/lib/wasm32-emscripten/lto/libc++abi-noexcept.a" for subsequent builds)
    system_libs:INFO: compiled 16 inputs in 0.87s
    cache:INFO:  - ok
    em++: error: html-minifier-terser was not found! Please run "npm install" in Emscripten root directory to set up npm dependencies
    make[2]: *** [CMakeFiles/EmscriptenNixDev.dir/build.make:98: EmscriptenNixDev.html] Error 1
    make[2]: *** Deleting file 'EmscriptenNixDev.html'
    make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/EmscriptenNixDev.dir/all] Error 2
    make: *** [Makefile:136: all] Error 2

Expected behavior

This works perfectly after adding -g2; however results in a huge binary size.

Notify maintainers

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

[nix-shell:/app/build]# nix-shell -p nix-info --run "nix-info -m"
this path will be fetched (0.00 MiB download, 0.00 MiB unpacked):
  /nix/store/w20x34n94ha7vx6c4y7z7zw05iaxp0fz-nix-info
copying path '/nix/store/w20x34n94ha7vx6c4y7z7zw05iaxp0fz-nix-info' from 'https://cache.nixos.org'...
 - system: `"aarch64-linux"`
 - host os: `Linux 5.15.49-linuxkit-pr`
 - multi-user?: `yes`
 - sandbox: `no`
 - version: `nix-env (Nix) 2.18.3`
 - nixpkgs: `/nix/store/qlsv39yndwdp0hjvwy1bz4z4sbfvx9cp-source`

Add a :+1: reaction to issues you find important.

arximboldi commented 1 month ago

Alternative workaround:

    -s MINIFY_HTML=0

This doesn't result in a huge binary.