Description
In the context of #276 (comment), I've been thinking that there are scenarios where one might want to obfuscate only some packages: when publishing or packaging an obfuscated library, to be consumed as regular Go packages via source code.
We do sort of support this already, via something like:
GOPRIVATE=my.corp/some/library garble -debugdir=out build my.corp/some/library/...
Then, the obfuscated source code will be under out/my.corp/some/library
.
I think this use case is valid, given that it's not possible (or at least not easy) to distribute Go libraries without distributing source code too. One such example is unidoc, where its libraries like unipdf are obfuscated then pushed to GitHub.
We already have much of the machinery here, and garble is pretty advanced as a Go obfuscator, so I think it makes sense to take it one step further and make this easier to do. For example, via:
garble export out my.corp/some/library/...
Some key differences compared to how we currently obfuscate code:
- Import paths should remain untouched, for the sake of keeping imports working.
- Exported names should also remain untouched, such as types and functions.
- Godoc comments of exported names should be left in place.
- License comments should be left in place.
Internal packages such as my.corp/some/library/internal/foo
would still be fully obfuscated, including changing their API names and import paths. That could be a way to tell what packages must remain importable and usable.
Otherwise, I think obfuscation could stay the same: stripping unexported names, position information, most comments, etc.
We might have to tweak our obfuscator to collapse newlines, too - right now, it does that via /*line
comments for the compiler, but newlines in the printed source code remain, since those don't affect binaries. They affect published source code, though - and it's also likely that those compiler directives shouldn't be present in this new mode.