I have seen a number of similar questions here on WP_SE but I believe my situation is a little different because the common libraries I'm creating are frameworks that I'd certainly like to be able to update and maintain, but are not decorations - ie: I can't author a theme that depends on the library and then degrade the theme nicely if the library isn't found.
I have a set of utility functions and some classes that I have developed to really speed up certain aspects of WordPress customization. I have been using them extensively in custom themes, and they are then usually bundled in some subdirectory of the theme.
Now I'm writing a plugin and I want to use those libraries in the plugin. The logical place for them in the case of plugins would be inside some kind of lib/ or include/ folder inside my plugin's directory structure.
The problem of course is that if I'm using both the plugin and the theme, the functions and classes are all being declared twice and that throws an error. Require_once doesn't realize these are the same codebase because of the different paths.
One solution would be to bundle all these libs into a separate plugin, and have the theme and the plugin leverage this utility plugin. But because these libs are essential to the theme/plugin's operation, the plugin must be installed when the theme/plugin is in use. Thinking forward, I don't want someone to have to download and install my theme and then download and install a plugin so that the theme will work. So that solution seems out of the question for me.
Can a theme or plugin go and fetch another plugin and install it? Can the theme installation process write to a directory other than
wp-content/themes? same with the plugin activation process?