You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cache sanity file llvm as relative path from emscripten root #15053
I have created some build scripts that allow me to build a completely self-contained emscripten install (incl, binaryen, llvm, deps, etc). I personally prefer this approach to the emsdk. However there is pretty much one last thing I need, and that is to be able to move the directory around without needing to regenerate the cache.
So far the cache sanity file (to decide on cache regeneration) looks like:
You may be able to tell from the path that my build system includes a llvm distribution inside the emscripten directory to encapsulate it and make it more "portable" (from a file system perspective).
The issue is that if I move this export directory around on disk, this will not match with the sanity file and it will all regenerate.
My current solution is to just set EM_CACHE in a wrapper script and allow each user of the package to generate their own, however it is a little messy (and wasteful). I would rather the cache to be within the emscripten folder along with everything else.
Is there some way that I can just ignore the check and use the cache regardless. I know for a fact that it will be suited to the rest of the toolchain because it is safely stored inside and not accessible by any other versions.
I have attached my emcc wrapper script so you can see the variables I am passing. emcc.txt
Apologies if this is confusing. I also understand it is a little different to how you guys are intending to package it.
Yes, what you are doing seems reasonable. There are a couple of solutions that you might be able to use today.
Ship the sdk without the emscripten/cache/sanity.txt file. On first usage this file will be created but the cache will not be cleared.
If the cache directory is read-only from a user perspective you can set add FROZEN_CACHE = True to the emscripten config file. I assume you are shipping a pre-configured emscripten config file with your installation, just like the emsdk does? This will mark the cache as read-only and also disable the sanity checks:
This also means that if any of your users end up needs a library configuration that hasn't been built yet they will hit this assertion rather than get a permission error trying to write a new library to the cache:
Perfect. I will probably go the FROZEN_CACHE route then because I believe it gets round the issue of that sanity.txt being created i.e upon first run and then being subsequently moved.
Oddly enough I think I tried deleting the sanity.txt and that still did result in a cache being rebuilt. I will try again and report back.
As a note, I plan to generate this cache as part of the emscripten build process. I think I notice a change from previous releases, now when running, i.e: emcc main.c, it seems to build the cache for everything. Before it seemed to be piecemeal as and when you needed it. I.e C++ standard library, then debug, then sockets, etc. Hopefully I have observed this correctly because admittedly it does make the process of generating the whole cache much easier (possibly why this change was introduced).
That said, I think there was also a command to explicitly build the entire cache. I will find that from my older notes.
Also, yes I do ship a preconfigured config file. Really it is just a proxy to pass through environment variables from the wrapper script (a bit more flexible).
Also, yes I do ship a preconfigured config file. Really it is just a proxy to pass through environment variables from the wrapper script (a bit more flexible).
If you want to use the environment variables to configure emscripten you can now adays just set EM_XXX in the environment. e.g. EM_BINARYEN and EM_LLVM. This works for all config settings. I guess this means you would end up with just and empty config file :)
Also, the EMSCRIPTEN_ROOT and JAVA config variables are most likely not needed (emscripten doesn't use the former and closure compiler is the only user of java and it will just find java in the PATH by default).
It does seem a shame the a wrapper script is needed. If you need to create a wrapper script it suggests we could be doing something better to make emscripten easy to configure. Can you explain why you need it?
Perhaps you ran in the same issue that emsdk had which was specifying paths relative to the config file itself. If so perhaps you could use the solution that we found there and do something like this:
As for the wrapper scripts, I originally used them back when emscripten couldn't always find python (OpenBSD doesn't symlink i.e python3.8 to python) and when EMSCRIPTEN_ROOT was required. However I generally kept with it because the emscripten directory has a lot of non-executable files in (Makefile, README, etc) and I didn't really want to put that kind of folder with those kinds of contents in PATH. It didn't seem right). However I don't think there is anything on your end to improve, I might check out the Makefile install rule and see if that cleans things up a bit.
I see. Yes the lack of python in the user's PATH and the desire not to put other files from top-level of emscripten in your PATH both seems like good reasons to use a wrapper script.
However, I would hope that the wrapper script would only need to set EM_PYTHON, and everything else could be specified in the config file. In general, I would try to avoid using environment variables for configuration if you can.
And yes, the install step, if when combined with a way to set name of the python executable, might allow your to remove the extra wrapper I think
shared:INFO: (Emscripten: Running sanity checks)
Traceback (most recent call last):
File "/opt/emscripten/em++.py", line 14, in <module>
sys.exit(emcc.run(sys.argv))
File "/opt/emscripten/emcc.py", line 1031, in run
shared.check_sanity()
File "/usr/local/lib/python3.8/contextlib.py", line 75, in inner
return func(*args, **kwds)
File "/opt/emscripten/tools/shared.py", line 438, in check_sanity
utils.write_file(sanity_file, expected)
File "/opt/emscripten/tools/utils.py", line 88, in write_file
with open(file_path, 'w') as fh:
PermissionError: [Errno 13] Permission denied: '/opt/emscripten/cache/sanity.txt'
What I propose would be to gracefully continue if the sanity.txt file cannot be written to. After all, the cache is in there and all it was going to do if it did have permissions was to write out that sanity.txt file anyway (it wouldn't even rebuild the cache).
The FROZEN_CACHE is close and does avoid this error but then prevents any building of stdlibs and ports. The --force argument of embuilder also doesn't seem to override this variable (even though it looks like it should in the code). So embuilder can't seem to explicitly build the cache with this variable set.
Any additional stdlibs or ports that aren't in this read-only cache can then go in $HOME/.emscripten_cache as an overlay. This one possibly could best benefit from storing a path, in case you have multiple emscripten installs.
In the above example, how come the cp -r can write to /opt/emscripten but emcc command cannot? Does the cp command run with sudo?
Of your two suggestions the first one is fairly simple to implement (don't error out writing sanity.txt).. and indeed FROZEN_CACHE would achieve that.
However as you point out it doesn't solve the problem of actually wanting to be able to write to the cache. Your suggestion two different caches would indeed solve the problem but it does add a fair amount of complexity. This complexity reaches right down into the llvm/clang itself since the sysroot lives in the cache and we pass the sysroot to clang using --sysroot .. clang doesn't support this notion of a secodary or overlay sysroot today.
Creating a secondary, overlay sysroot IMHO would add a fair amount of complexity. I'm not ruling it out, but its a complexity and normal toolchains don't have.
I'd like to find a solution that matches normal toolchains perhaps. In your case you are trying to create read-only installation in /opt/emscripten that is read-only and shared between multiple users. Just like the system toolchain in /usr/bin and /usr/lib its not possible for normal users install new libraries here. They would need either a writable /usr/local/lib (which on some system is writable by all users), or they would need to install somewhere outside the sysroot and use explicit -L and -I flags.
Of course if gcc and clang we designed today they might includes $HOME/.local/lib and $HOME/.local/include by default in their include and lib paths, but that is generally not how they work.
Again, I think the simplest solution would be this: If you want a read-only sysroot you better make sure its fully populated, or that user who want to populate it use sudo when they do so.
Let me try to three options:
Read-only sysroot that come pre-populated. Cache is frozen, but is complete so no need write to it.
Read-only sysroot that comes partially populated. Cache is frozen but can be written to sudo ./embuidler if needed.
Read-only sysroot that comes partially populated, with implicit user-overlay in $HOME. ./embuilder writes to the $HOME and sudo ./embuilder writes to read-only sysroot.
(1) is fully working today. (2) probably most works but might need a patch so that sudo overrides FROZEN_CACHE. We could imply FROZEN_CACHE if the cache is read-only to avoid the need to the explicit setting?. (3) is adds a fair amount of complexity and is non-standard as far as I can tell.
Ah, the cp is run as root. I did prefix it with # rather than $ but admittedly it was very subtle!
Yeah, going over it now I do understand that overlay system is complex and non-standard. Typically this is less of an issue with the system compiler because we build software normally and then make install it into place. Whereas with Emscripten we use the compiler frontend itself to fetch the ports and build them. Half like a compiler, half like a package manager. Possibly this is where some weirdness comes from.
OK, I can certainly live with it fully populated (minus the ports) via using embuilder build SYSTEM. Then any ports I can install manually or via running embuilder via doas / sudo. (As an aside, if it is possible to automatically fetch and archive the source files for the ports without building, so I can create offline distfiles, then I will just specify FULL on embuilder. I will check the Emscripten scripts)
So yes I think option 2) could be ideal. For now I can patch around it using the wrapper (or the config python file) by checking if the cache folder is owned by the executing user and setting FROZEN_CACHE based on that.
Regarding the "distfile" thing for ports, it sounds like you want to be able to do embuilder build ALL without a network connection, or something like that? We don't have any mechanism for that today. Is there some reason you can't use the network at the point when you do embuilder build ALL.
Some packaging systems (in particular the FreeBSD and OpenBSD ports collections) do not allow for downloading files outside of the early fetch stage. And unfortunately that stage is too early to be able to build the ports (no binaryen or llvm built yet). Ideally this would be done in the build stage.
It isn't a deal breaker (for my uses). However it is annoying enough that I am happy to roll up my sleeves and perhaps add a embuilder fetch feature?
Currently for the emscripten node_modules I create a temporary emscripten prefix do something like:
npm ci --ignore-scripts
tar -czf ../distfiles/emscripten-node_modules.tar.gz node_modules
Activity
sbc100 commentedon Sep 16, 2021
Hi Karsten,
Yes, what you are doing seems reasonable. There are a couple of solutions that you might be able to use today.
emscripten/cache/sanity.txtfile. On first usage this file will be created but the cache will not be cleared.FROZEN_CACHE = Trueto the emscripten config file. I assume you are shipping a pre-configured emscripten config file with your installation, just like the emsdk does? This will mark the cache as read-only and also disable the sanity checks:emscripten/tools/shared.py
Lines 402 to 405 in 3407afb
This also means that if any of your users end up needs a library configuration that hasn't been built yet they will hit this assertion rather than get a permission error trying to write a new library to the cache:
The way
emscripten/tools/cache.py
Lines 39 to 43 in 3407afb
osen commentedon Sep 16, 2021
Many thanks for your response and suggestions.
Perfect. I will probably go the FROZEN_CACHE route then because I believe it gets round the issue of that sanity.txt being created i.e upon first run and then being subsequently moved.
Oddly enough I think I tried deleting the sanity.txt and that still did result in a cache being rebuilt. I will try again and report back.
As a note, I plan to generate this cache as part of the emscripten build process. I think I notice a change from previous releases, now when running, i.e:
emcc main.c, it seems to build the cache for everything. Before it seemed to be piecemeal as and when you needed it. I.e C++ standard library, then debug, then sockets, etc. Hopefully I have observed this correctly because admittedly it does make the process of generating the whole cache much easier (possibly why this change was introduced).That said, I think there was also a command to explicitly build the entire cache. I will find that from my older notes.
osen commentedon Sep 16, 2021
Also, yes I do ship a preconfigured config file. Really it is just a proxy to pass through environment variables from the wrapper script (a bit more flexible).
It just contains the following.
sbc100 commentedon Sep 16, 2021
If you want to use the environment variables to configure emscripten you can now adays just set
EM_XXXin the environment. e.g.EM_BINARYENandEM_LLVM. This works for all config settings. I guess this means you would end up with just and empty config file :)Also, the
EMSCRIPTEN_ROOTandJAVAconfig variables are most likely not needed (emscripten doesn't use the former and closure compiler is the only user of java and it will just find java in the PATH by default).sbc100 commentedon Sep 16, 2021
It does seem a shame the a wrapper script is needed. If you need to create a wrapper script it suggests we could be doing something better to make emscripten easy to configure. Can you explain why you need it?
sbc100 commentedon Sep 16, 2021
Perhaps you ran in the same issue that emsdk had which was specifying paths relative to the config file itself. If so perhaps you could use the solution that we found there and do something like this:
osen commentedon Sep 16, 2021
Ah, I am simply out of date. This should indeed simplify things. Thanks for sharing that. I could only see mention of EM_LLVM_ROOT here: https://emscripten.org/docs/tools_reference/emcc.html (Granted, these docs may not always be up to date with how quick this project progresses). I will also remove EMSCRIPTEN_ROOT. I am glad it now calculates its path.
As for JAVA. OpenBSD doesn't actually put Java in PATH so I do have some plans to scan for it (it is always installed in /usr/local/jdkXXX). I already had to patch the npm closure compiler to get it to use Java rather than the native Linux binaries it defaults to if not macOS or Windows (https://github.com/google/closure-compiler-npm/blob/master/packages/google-closure-compiler/lib/utils.js#L34)
As for the wrapper scripts, I originally used them back when emscripten couldn't always find python (OpenBSD doesn't symlink i.e python3.8 to python) and when EMSCRIPTEN_ROOT was required. However I generally kept with it because the emscripten directory has a lot of non-executable files in (Makefile, README, etc) and I didn't really want to put that kind of folder with those kinds of contents in PATH. It didn't seem right). However I don't think there is anything on your end to improve, I might check out the Makefile install rule and see if that cleans things up a bit.
sbc100 commentedon Sep 16, 2021
I see. Yes the lack of
pythonin the user'sPATHand the desire not to put other files from top-level of emscripten in yourPATHboth seems like good reasons to use a wrapper script.However, I would hope that the wrapper script would only need to set
EM_PYTHON, and everything else could be specified in the config file. In general, I would try to avoid using environment variables for configuration if you can.And yes, the install step, if when combined with a way to set name of the python executable, might allow your to remove the extra wrapper I think
osen commentedon Sep 17, 2021
OK, I have had a further play and potentially there might be a change to consider.
If the following commands are run (I do not have FROZEN_CACHE in config):
This results in:
What I propose would be to gracefully continue if the sanity.txt file cannot be written to. After all, the cache is in there and all it was going to do if it did have permissions was to write out that sanity.txt file anyway (it wouldn't even rebuild the cache).
The FROZEN_CACHE is close and does avoid this error but then prevents any building of stdlibs and ports. The --force argument of embuilder also doesn't seem to override this variable (even though it looks like it should in the code). So embuilder can't seem to explicitly build the cache with this variable set.
Any additional stdlibs or ports that aren't in this read-only cache can then go in
$HOME/.emscripten_cacheas an overlay. This one possibly could best benefit from storing a path, in case you have multiple emscripten installs.sbc100 commentedon Sep 17, 2021
In the above example, how come the
cp -rcan write to/opt/emscriptenbutemcccommand cannot? Does thecpcommand run withsudo?Of your two suggestions the first one is fairly simple to implement (don't error out writing sanity.txt).. and indeed FROZEN_CACHE would achieve that.
However as you point out it doesn't solve the problem of actually wanting to be able to write to the cache. Your suggestion two different caches would indeed solve the problem but it does add a fair amount of complexity. This complexity reaches right down into the llvm/clang itself since the sysroot lives in the cache and we pass the sysroot to clang using
--sysroot.. clang doesn't support this notion of a secodary or overlay sysroot today.Creating a secondary, overlay sysroot IMHO would add a fair amount of complexity. I'm not ruling it out, but its a complexity and normal toolchains don't have.
I'd like to find a solution that matches normal toolchains perhaps. In your case you are trying to create read-only installation in /opt/emscripten that is read-only and shared between multiple users. Just like the system toolchain in /usr/bin and /usr/lib its not possible for normal users install new libraries here. They would need either a writable /usr/local/lib (which on some system is writable by all users), or they would need to install somewhere outside the sysroot and use explicit
-Land-Iflags.Of course if gcc and clang we designed today they might includes
$HOME/.local/liband$HOME/.local/includeby default in their include and lib paths, but that is generally not how they work.Again, I think the simplest solution would be this: If you want a read-only sysroot you better make sure its fully populated, or that user who want to populate it use
sudowhen they do so.Let me try to three options:
sudo ./embuidlerif needed.$HOME../embuilderwrites to the $HOME andsudo ./embuilderwrites to read-only sysroot.(1) is fully working today. (2) probably most works but might need a patch so that
sudooverrides FROZEN_CACHE. We could implyFROZEN_CACHEif the cache is read-only to avoid the need to the explicit setting?. (3) is adds a fair amount of complexity and is non-standard as far as I can tell.osen commentedon Sep 17, 2021
Ah, the
cpis run as root. I did prefix it with # rather than $ but admittedly it was very subtle!Yeah, going over it now I do understand that overlay system is complex and non-standard. Typically this is less of an issue with the system compiler because we build software normally and then
make installit into place. Whereas with Emscripten we use the compiler frontend itself to fetch the ports and build them. Half like a compiler, half like a package manager. Possibly this is where some weirdness comes from.OK, I can certainly live with it fully populated (minus the ports) via using
embuilder build SYSTEM. Then any ports I can install manually or via running embuilder via doas / sudo. (As an aside, if it is possible to automatically fetch and archive the source files for the ports without building, so I can create offline distfiles, then I will just specify FULL on embuilder. I will check the Emscripten scripts)So yes I think option 2) could be ideal. For now I can patch around it using the wrapper (or the config python file) by checking if the cache folder is owned by the executing user and setting FROZEN_CACHE based on that.
sbc100 commentedon Sep 17, 2021
Regarding the "distfile" thing for ports, it sounds like you want to be able to do
embuilder build ALLwithout a network connection, or something like that? We don't have any mechanism for that today. Is there some reason you can't use the network at the point when you doembuilder build ALL.osen commentedon Sep 17, 2021
Some packaging systems (in particular the FreeBSD and OpenBSD ports collections) do not allow for downloading files outside of the early
fetchstage. And unfortunately that stage is too early to be able to build the ports (no binaryen or llvm built yet). Ideally this would be done in thebuildstage.It isn't a deal breaker (for my uses). However it is annoying enough that I am happy to roll up my sleeves and perhaps add a
embuilder fetchfeature?Currently for the emscripten node_modules I create a temporary emscripten prefix do something like:
Bit naff but it does resolve the problem.