Back-Link: https://www.hasenzaehn.ch/StaticHtml/wiki.html
Ich nutze Conan im wasm Projekt als Paketmanager, um externe Bibliotheken wie nlohmann/json.hpp ins Projekt einzubinden.
Conan liefert mir fertige CMake-Targets wie:
nlohmann_jsonspdlogGoogleTestEin Conan-Profil beschreibt, wie Pakete gebaut werden sollen – also Compiler, Build-Typ, Zielplattform und Umgebungsvariablen.
„Conan profiles allow users to set a complete configuration set for settings, options, environment variables (for build time and runtime context), tool requirements, and configuration variables in a file.“
nlohmann/json.hpp?Der Header <nlohmann/json.hpp> ist der zentrale Einstiegspunkt der nlohmann-json-Bibliothek, einer header-only C++-Bibliothek zum Arbeiten mit JSON.
Ich brauche diese Bibliothek, weil ich JSON-Objekte von der materialProject API abhole und sie serialisieren bzw. deserialisieren muss.
In meiner Projektstruktur liegt ein conanfile.py direkt im Bibliotheksordner:
.
├── clion_wasm_env
├── CMakeLists.txt
├── ...
├── conan
│ └── profiles
│ ├── emscripten
│ └── native
├── libraries
│ ├── material_data_lib
│ │ ├── CMakeLists.txt
│ │ ├── ...
│ │ ├── conanfile.py
│ │ ├── ...
│ └── parser_lib
├── main.cpp
├── qml
│ └── main.qml
└── resources.qrc
Ist das okay ?
conan install . arbeitet immer relativ zum Verzeichnis des conanfile.py.conanfile.py automatisch und bietet an, den Conan-Install-Step in den CMake-Workflow einzubinden.nlohmann, spdlogcpr/libcurl, sondern für die Lösung mit Qt::NetworkAlles wurde im CLion-Terminal ausgeführt (ohne dass ich CMake Options im CMake-Profil selber gesetzt habe).
Aber man kann die Options unten natürlich reinkopieren.
Die Profile liegen hier:
~/Dokumente/Webseite/MaterialStack/conan/profiles
.
|__emscripten
|__native
[settings]
os=Emscripten
arch=wasm
compiler=emcc
compiler.version=3.1.56
compiler.libcxx=libc++
compiler.cppstd=gnu23
build_type=Release
[options]
spdlog/*:header_only=True
[conf]
tools.build:compiler_executables={"c":"/home/USERACC/emsdk/upstream/emscripten/emcc",\
"cpp":"/home/USERACC/emsdk/upstream/emscripten/em++",\
"ar":"/home/USERACC/emsdk/upstream/emscripten/emar",\
"ranlib":"/home/USERACC/emsdk/upstream/emscripten/emranlib"}
[settings]
os=Linux
arch=x86_64
compiler=gcc
compiler.version=13
compiler.libcxx=libstdc++11
compiler.cppstd=gnu23
build_type=Release
[options]
spdlog/*:header_only=True
gtest/*:shared=False
Das muss man in jeder neuen Shell machen, bzw. immer wenn man CLion neu öffnet:
source ~/emsdk/emsdk_env.sh
embuilder build libGL
# embuilder:INFO: building libGL
# embuilder:INFO: ...success. Took (0.00s)
rm -rf build_wasm
mkdir -p build_wasm && cd build_wasm
conan install ../libraries/material_data_lib \
-pr:h=../conan/profiles/emscripten \
-pr:b=../conan/profiles/native \
-of . \
--build=missing
cmake -S .. -B . -G Ninja \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE=/home/USERACC/Qt/6.8.3/wasm_singlethread/lib/cmake/Qt6/qt.toolchain.cmake \
-DQT_HOST_PATH=/home/USERACC/Qt/6.8.3/gcc_64 \
-DQt6_DIR=/home/USERACC/Qt/6.8.3/wasm_singlethread/lib/cmake/Qt6 \
-DCMAKE_PREFIX_PATH="$(pwd)/build/Release/generators;$(pwd)/build/Release;${CMAKE_PREFIX_PATH}" \
-Dnlohmann_json_DIR="$(pwd)/build/Release/generators" \
-Dspdlog_DIR="$(pwd)/build/Release/generators" \
-Dfmt_DIR="$(pwd)/build/Release/generators" \
-DBUILD_TESTING=OFF
Anstatt lange -D Zeilen als CMake Options einzugeben, kann man ein CMakePresets.json im Projekt-Root anlegen.
Dort definiert man sowohl das Native- als auch das Wasm-Preset inklusive aller Qt- und Conan-Variablen.
{
"version": 7,
"configurePresets": [
{
"name": "wasm",
"displayName": "WASM (Qt 6.8.3 / emscripten 3.1.56)",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build_wasm",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_TOOLCHAIN_FILE": "$env{QT_WASM_TOOLCHAIN}",
"QT_HOST_PATH": "$env{QT_HOST_PATH}",
"Qt6_DIR": "$env{QT_WASM_QT6_DIR}",
"CMAKE_PREFIX_PATH": "${sourceDir}/build_wasm/build/Release/generators;${sourceDir}/build_wasm/build/Release",
"nlohmann_json_DIR": "${sourceDir}/build_wasm/build/Release/generators",
"spdlog_DIR": "${sourceDir}/build_wasm/build/Release/generators",
"fmt_DIR": "${sourceDir}/build_wasm/build/Release/generators",
"BUILD_TESTING": "OFF",
"CMAKE_EXE_LINKER_FLAGS": "-sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sFULL_ES2=1"
}
},
{
"name": "native",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build_native",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
],
"buildPresets": [
{ "name": "wasm", "configurePreset": "wasm" },
{ "name": "native", "configurePreset": "native" }
]
}
CMakeUserPresets.json ist lokal und soll nicht commited werden -> in gitignore schreiben!!!
Dort kommen die maschinenspezifischen Qt-Pfade rein.
{
"version": 7,
"configurePresets": [
{
"name": "wasm-local",
"inherits": "wasm",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "/home/USERACC/Qt/6.8.3/wasm_singlethread/lib/cmake/Qt6/qt.toolchain.cmake",
"QT_HOST_PATH": "/home/USERACC/Qt/6.8.3/gcc_64",
"Qt6_DIR": "/home/USERACC/Qt/6.8.3/wasm_singlethread/lib/cmake/Qt6"
}
}
]
}
Nach dem conan install Befehl kann man vom Projekt-Root (MaterialStack) aus so bauen:
cmake --list-presets=all
cmake --preset wasm-local
cmake --build --preset wasm-local -j