Repository navigation
Refactor system_libs.py for easier combinations - #8780
Conversation
kripken
left a comment
There was a problem hiding this comment.
Very nice work! Skipping to the end, I like how the definitions of the libraries look - much better than before. I didn't read this all yet, but here are some initial comments.
I have mixed feelings about the name changes caused by this, like embuilder now expects libdlmalloc instead of dlmalloc. On the one hand this might break some users. On the other, the new names are all lib* which is more consistent? Also we've changed names in the past with no bug reports. So I think this is fine, but curious to hear what others think.
At minimum we should add a clear note in the changelog, that some internal library names may change, including in a visible way on embuilder. Also the recursive dependencies are not done any more.
A large comment on top of the new code would help, I think, to explain the overall design. In particular, I started to read and noticed I didn't realize why cls is used so much - why are classes the base of everything (and not instances), what inheritance means, etc.
| print('Skipping building of native-optimizer; EMSCRIPTEN_NATIVE_OPTIMIZER set in .emscripten config.') | ||
| else: | ||
| tasks += ['native_optimizer'] | ||
| tasks += ['struct_info'] |
There was a problem hiding this comment.
generated_struct_info.json is no longer automatically built due to the build system change, so we make this task run explicitly when building all libraries.
There was a problem hiding this comment.
please add a comment explaining that, as otherwise it's very surprising to see it singled out for special treatment. something like "is necessary for building anything else". but that's not the full explanation, since isn't libc in a similar situation?
There was a problem hiding this comment.
It might make more sense for this to be listed in system targets.
| - Add a new system for managing system libraries. (#8780) | ||
| This may require minor changes when performing certain operations: | ||
| - When using `embuilder.py` to build a specific library, the name may have | ||
| changed: all library names are prefixed with lib now. |
There was a problem hiding this comment.
please add for consistency (to explain why the names changed)
| `libdlmalloc` or `libpthreads_stub`. These names will link in the correct | ||
| version of the library: if the build is configured to use `emmalloc`, `libmalloc` | ||
| will mean `libemmalloc`, and if thread support is disabled, `libpthreads` will | ||
| mean `libpthreads_stub`. |
There was a problem hiding this comment.
please explain the benefit here too.
| def is_flag(arg): | ||
| return arg.startswith('--') | ||
|
|
||
| shared.check_sanity() |
There was a problem hiding this comment.
i vaguely remember this was here for a reason - that we must check here so the cache is invalidated up front, rather than later? please add a comment explaining this. also, I think this should run even without main - how about putting it at the top level at the bottom of the file before checking if we are in a main program?
| print('Skipping building of native-optimizer; EMSCRIPTEN_NATIVE_OPTIMIZER set in .emscripten config.') | ||
| else: | ||
| tasks += ['native_optimizer'] | ||
| tasks += ['struct_info'] |
There was a problem hiding this comment.
please add a comment explaining that, as otherwise it's very surprising to see it singled out for special treatment. something like "is necessary for building anything else". but that's not the full explanation, since isn't libc in a similar situation?
| # emmalloc doesn't actually use errno, but it's easier to build it again | ||
| extra += '_noerrno' | ||
| if self.is_mt: | ||
| extra += '_threadsafe' |
There was a problem hiding this comment.
doesn't inheriting from MTLibrary handle adding the -mt suffix for us?
if this is overriding just to keep the same names as before, i think it's ok to change the name if it simplifies the code (which it would if just one place adds -mt or such to the name).
There was a problem hiding this comment.
Somewhat unfortunately, due to the way inheritance work, we cannot actually call super if we want use the prefix libdlmalloc or libemmalloc, which is the reason why we are handling it here manually.
There was a problem hiding this comment.
i probably don't understand yet. why doesn't this problem happen with all the other uses of MTLibrary?
There was a problem hiding this comment.
All other uses of MTLibrary are fine because they end up calling Library's get_base_name, returning the name class attribute. However, the name attribute on libmalloc is libmalloc, so using inheritance, we'd be forced to have libmalloc-mt-dlmalloc..., which looks terrible.
There was a problem hiding this comment.
Thinking on this some more, I'm not sure malloc is that special, and this issue may happen more over time. If inheritance is clumsy here, perhaps an option is to avoid it? That is, instead of
class SomeLibrary(MTLibrary, OtherLibrary):
..
there could be a single class for all library types, and "Modifiers" (VariantGenerators? Variators?) that are added to a list,
class SomeLibrary(Library):
modifiers = [MTLibrary, OtherLibrary]
..
then the "Modifiers" would be called in the proper places?
There was a problem hiding this comment.
Actually now that I think about it, we can still do this with inheritance. For this use case, we just need a method that returns self.name that can be overridden. I suppose we might as well do this for libmalloc as an example for the future.
There was a problem hiding this comment.
IMO using multiple inheritance makes the implementation harder to follow but makes the resulting client code more elegant. The code is documented well enough that I don't think the complexity of the implementation will be a maintenance issue going forward, so I'm happy with the current design.
@kripken I agree that your proposed solution would have a simpler implementation, so if you feel strongly about this then I would be happy going in that direction as well.
|
Very nice work! Overall I think this is good enough to land after the last changes, and we can iterate on improvements from there, so lgtm from me. However, since this is a big change and the others are away, let's wait til next week and see if we can get feedback from some of them. |
| will mean `libemmalloc`, and if thread support is disabled, `libpthreads` will | ||
| mean `libpthreads_stub`. This allows you to say `libmalloc` or `libpthreads` without | ||
| worrying about which implementation is supposed to be used, and avoid duplicate | ||
| symbols if you used the wrong implementation. |
There was a problem hiding this comment.
The above comment only pertains to EMCC_FORCE_STDLIBS and not to embuilder.py command line usage, right?
One core use case of embuilder.py is to drive a prebuild of specific system libraries at compiler toolchain package authoring time, so that these system libraries will not be attempted to be built on demand: this helps give fast first time experience to users, e.g. with Unity when they export their first HTML5 project, on a weak laptop building the system libraries would add several minutes to first build - leading users to perceive that web exports are really slow. Also another reason is that installation is done in 'Program Files' folder, where everything is read-only from the get-go. Building content on demand inside 'Program Files' would not work.
To achieve the above, we use embuilder.py to prebuild libemmalloc and libdlmalloc for example, so that still works? (Briefly looking it appears to me that it does, but just verifying)
There was a problem hiding this comment.
Yes, embuilder.py is meant to be able to build any version of the library you want. EMCC_FORCE_STDLIBS now uses the right variant for the current build.
|
Hmm, there's some conflict upcoming with this PR I'm hoping can land soon #7672 . I hope that's easy to factor in to the structure that this PR brings? |
| # included at the same time as compiler-rt. | ||
| # Note that this also includes things that may be depended on by those | ||
| # functions - fmin uses signbit, for example, so signbit must be here (so if | ||
| # fmin is added by codegen, it will have all it needs). |
There was a problem hiding this comment.
How did you verify that you got all the dependencies?
There was a problem hiding this comment.
This wasn't my code. I just moved this function outside of calculate.
| def vary_on(cls): | ||
| """ | ||
| Returns a list of boolean constructor arguments that defines the variations | ||
| of this library. |
There was a problem hiding this comment.
It would be good to clarify that these are strings. When I initially read this I thought "boolean contructor arguments" meant bools.
| def create_libc(libname): | ||
| logger.debug(' building libc for cache') | ||
| class CXXLibrary(Library): | ||
| emcc = shared.EMXX |
| if shared.Settings.DEBUG_LEVEL >= 3: | ||
| self.is_debug = kwargs.pop('is_debug') | ||
| self.use_errno = kwargs.pop('use_errno') | ||
| self.is_tracing = kwargs.pop('is_tracing') |
There was a problem hiding this comment.
What would happen if some abstract library wanted to use the same kwargs? Should these have a malloc_ prefix or something like that?
There was a problem hiding this comment.
That would only be a problem if libmalloc inherits from that abstract library. If that were the case, we probably wouldn't need this line.
If we really want to, we could use the __ name mangling, but I don't feel it's necessary.
| src_glob = '*.cc' | ||
|
|
||
|
|
||
| def calculate(temp_files, in_temp, stdout_, stderr_, forced=[]): |
There was a problem hiding this comment.
This may not be appropriate for this PR, but it would be great if this function had a more descriptive name.
Co-Authored-By: Thomas Lively <[email protected]>
Changes:
* Made `sanitizer_common` and `ubsan` runtimes work:
* Add `SANITIZER_EMSCRIPTEN` platform
* Add `sanitizer_emscripten.cc` for Emscripten-specific implementations of
helper and abstraction functions
* Stub out functions that do not work on Emscripten. Using these will be
abort the program. These will be implemented later.
* Add `emscripten_get_module_name` to get the WASM module name
* Changed `embuilder` to build `ubsan` and `sanitizer_common` runtimes
* Add `ubsan`-related unit tests
* Remove old JS-based `ubsan` hack and its unit tests
Known limitations:
* Stack traces do not work.
|
I'm a little concerned that we may have lost the ability to control the order of libraries on the command line. Were they in declaration order before and now alphabetical? I am a little surprised that alphabetical order is sufficient but the tests pass so I guess its OK so far. I suppose really we want to build a dependency DAG such that any dependent libraries always come after on the command line. But I feel like the complexity here is already getting out of control. |
|
@sbc100 The actual order of libraries we pass to The thing that changed is that we now try to resolve the undefined symbols in our object files by going through our libraries in alphabetical order, instead of the declaration order from before. Even before this change, the libraries were not linked in topological order based on dependencies. Compiling a C++ hello world program before this PR passes the libraries on the command line in this order: We note clearly that If the libraries were supposed to be passed in topological order, we can add a topological sort. However, |
|
Checking the man page for |
|
The order matters when one two libraries both define the same symbol. The first one on the command line will generally win. I'm not sure we take advantage of that anywhere. The order in which As you say not a huge deal probably, especially for I guess the only time it will really matter is if we want to have a library that overrides symbols from another library. Until that time maybe we can ignore this issue. In that case it would be "depends" so much as "comes_before" maybe? |
|
If we do find ourselves needing the libraries to appear in certain orders, we could actually support having the libraries appear in the order they appear in |
No description provided.