The cmpcodesize script is a little unwieldy--the same file contains
command-line argument parsing, otool invocation, and result
output formatting.
To split the script into several files, we'll need to turn it into a
Python package. In order to do so, we first move it into its own
directory.
Note that to invoke the script, you'll now need to run
`utils/cmpcodesize/cmpcodesize`. That additional path component is
inconvenient for now, but things will get better soon.
`$SWIFT_OLD_BUILDDIR` and `$SWIFT_NEW_BUILDDIR` are only necessary when
not using `--` to delineate two sets of files.
1. Only grab them from the environment when `--` is *not* used.
2. Assert if they're not present in that codepath. Print what *is*
specified in the environment, in order to help the user correct
their mistake.
We can detect improper use of --list very early in the script's
execution. Favor doing so to warn the user earlier. Also, add
documentation for proper use of --list.
The values are already stored on the parsed arguments, so remove the
variables and clear up the variable namespace a little.
Also, use snake_case, which is more Pythonic. This commit doesn't
change all variable names in the interest of keeping changes minimal.
We can detect improper combinations of --list and
--additional-sections/--category very soon when executing the script.
Assert in those cases. This used to be a warning.
Because of the way Python uses references for lists, the logic around
`oldFileArgs`, `newFileArgs`, and `curFiles` is difficult to follow.
Use a less efficient algorithm to find and split the elements based on
the '--' separator.
Although its performance is negligibly worse O(2n) as opposed to O(n), it's
easier to understand and uses one less imperative loop. Also, it's not as if
we'll ever encounter input that makes the performance difference
matter, so :shipit:!
Rather than manually parse the arguments passed to cmpcodesize, use the
Python stdlib. By doing so, we can eliminate a lot of redundant code.
One problem: argparse can't handle positional arguments of the string
'--', since it misinterprets them as optional arguments. We replace
that stirng with a special token.
In order to prevent the Python interpreter from running code that is
meant to be executed directly, the convention is to check the context
in which the code is being interpreted.
Add a check for the context stored in the `__name__` variable, and only
execute the cmpcodesize script if it is being run as a command-line
script (that is, `__main__`).
This script can:
*) Compare sizes of text sections (or other sections)
*) Compare sizes of function categories (Swift functions, Protocol Witnesses, Specializations, etc.)
*) Provide a detailed list or comparison of all functions
Use cmpcodesize -h to get info on the usage.
I copied the internals of Nadav's analyzeDylibSize script (Thanks!).
As the function of the analyzeDylibSize is now covered by this new script I deleted the old script.
Sorry that I implemented in ruby and not in python (I know ruby much better than python).
Swift SVN r25501