emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.36k stars 3.25k forks source link

Any way to set the ENV enviroment variables during compilation? #22071

Open hly2019 opened 3 weeks ago

hly2019 commented 3 weeks ago

Version of emscripten/emsdk: 3.1.54

Hi, may I enquire if there is any way to set the environment variables during compilation? I do see the way to set the environments in JavaScript code here https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#environment-variables, but I'm just curious if it could be set up during compilation. I tried to use set_property in CMake but it seems can't work.

Thank you very much!

sbc100 commented 3 weeks ago

The simplest way to do this today would probably to specify --pre-js <filename>.js with a file that modiies the global ENV map.

See https://github.com/emscripten-core/emscripten/blob/1b01a9ef2b60184eb70616bbb294cf33d011bbb2/src/library.js#L413-L415

I would advice doing something like:

Module['preRun'] = () => {
   ENV[XXX] = YYY;  <-- your settings here.
}