4

Both vcpkg and Emscripten require to set CMAKE_TOOLCHAIN_FILE to

vcpkg/scripts/buildsystems/vcpkg.cmake and emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake respectively.

How do i do that? or what is the best way to get them both working together?

right now i have a naive attempt:

set(CMAKE_TOOLCHAIN_FILE "/Users/screen-photo-to-text/vcpkg/scripts/buildsystems/vcpkg.cmake" "/Users/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake")

But it get Could not find toolchain file error and i can't find a way to get this working

Alejandro Camba
  • 978
  • 10
  • 25
  • 2
    `CMAKE_TOOLCHAIN_FILE` takes a path to a single file. You'll have to write your own which maybe just uses the `include` function to include both. – fdk1342 Jul 23 '20 at 21:15
  • There is `VCPKG_CHAINLOAD_TOOLCHAIN_FILE` where maybe you could pass `Emscripten.cmake`, not sure if it will work correctly though. – stoper Oct 16 '20 at 11:59

2 Answers2

3

To my delight, recently vcpkg received Emscripten support - see PR.

One can install packages like so:

vcpkg install zlib:wasm32-emscripten

Usage is pretty usual standard, for example CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
project(zpipe CXX)
find_package(ZLIB REQUIRED)
add_executable(zpipe zpipe.cpp)
target_link_libraries(zpipe ZLIB::ZLIB)

The tricky part is still as in question how to combine two toolchains. This invocation works for me:

mkdir build
cd build
emcmake "c:\Program files\CMake\bin\cmake" .. "-G" "Ninja" "-DCMAKE_MAKE_PROGRAM=F:/vcpkg/downloads/tools/ninja/1.10.1-windows/ninja.exe" "-DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=%EMSDK%/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake" "-DVCPKG_TARGET_TRIPLET=wasm32-emscripten" "-DCMAKE_TOOLCHAIN_FILE=F:/vcpkg/scripts/buildsystems/vcpkg.cmake" "-DCMAKE_BUILD_TYPE=Release"
emmake ninja

As usually, this needs first Emscripten environment variables set (e.g. with emsdk_env.bat).

Failing to provide this asked second toolchain will result with errors like wasm-ld: error: unknown argument: --out-implib

If for whatever reason (e.g. not absolute path) emcmake can't find the CMake executable, it may result with errors like FileNotFoundError: [WinError 2] The system cannot find the file specified

In case of CMake Error: CMake was unable to find a build program corresponding to "Ninja". CMAKE_MAKE_PROGRAM is not set., as hinted, the CMAKE_MAKE_PROGRAM needs to be set pointing to ninja executable.

stoper
  • 156
  • 2
  • 12
0

You set the VCPKG_CHAINLOAD_TOOLCHAIN_FILE variable (along with VCPKG_TARGET_TRIPLET)

cmake^ 
  "-DCMAKE_TOOLCHAIN_FILE=%VCPKG%/scripts/buildsystems/vcpkg.cmake"^
  "-DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=%EMSDK%/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake"^  
  "-DVCPKG_TARGET_TRIPLET=wasm32-emscripten"

Along with your preferred build settings of course. I'm not sure you need emcmake for this but between this and the above answer choose whatever works. This worked for me and apparently others too.

edit: Apparently emcmake sets the CMAKE_TOOLCHAIN_FILE as your Emscripten.cmake for you (I learned to set it manually so I was wondering where that came from...). I'm not sure it's necessary or how it is even viable in this case, but again use what works.