Skip to content

Commit

Permalink
[build] An awk script to extract PyMethodDef constants from C source.
Browse files Browse the repository at this point in the history
Still needs some work to be useful.

- Show the total number of symbols.
- build/compile.sh: an example of how to remove dead code with GCC.  Not
  enabled yet, but I tested it and it makes a difference.
  • Loading branch information
Andy Chu committed Oct 13, 2018
1 parent d8edecd commit ffe3e73
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
3 changes: 3 additions & 0 deletions benchmarks/native-code.R
Expand Up @@ -24,6 +24,9 @@ options(stringsAsFactors = F,
Basic = function(ctx) {
Banner('BASIC METRICS')

ctx$symbols %>% arrange(desc(filesize)) %>% head(20) -> f1
ShowValue('Number of Symbols: %d', nrow(ctx$symbols))

ctx$symbols %>% arrange(desc(filesize)) %>% head(20) -> f1
ShowFrame('By Size On Disk:', f1)

Expand Down
63 changes: 60 additions & 3 deletions benchmarks/native-code.sh
Expand Up @@ -90,10 +90,67 @@ run-for-release() {
report
}

py-method-defs() {
# TODO:
# Write to a separate file like _build/pydefs/intobject.include
# #ifdef OVM_MAIN
# #include "intobject.include"
# #else
# ...
# #end
#
# Should those files be checked in an edited by hand? Or join them somehow
# with oil-symbols.txt?
# I think this is hard because of METHODS.
# Maybe you should have a config file that controls it. It takes a .include
# file and then whitelist/blacklist, and then generates a new one.
# could put it in build/pydefs-config.txt
#
# And then reprint the PyMethoDef without docstrings? It shouldn't be that
# hard to parse. You can almost do it with a regex, since commas don't appear
# in the string.

extract-defs() {
# NOTE: PyMemberDef is also interesting, but we don't need it for the build.
gawk '
/static PyMethodDef/ {
if (printing != 0) {
printf("%s:%d Expected not to be printing\n", FILENAME, FNR);
exit 1;
}
printing = 1;
printf("\n");
printf("/* Extracted from %s */\n", FILENAME);
}
{
if (printing) {
print
}
}
/^[:space:]*\}/ {
printing = 0;
}
' "$@"
}

source build/common.sh # $PY27

# TODO: Use PREPROC_FLAGS from build/compile.sh.
preprocess() {
# What about stuff in pyconfig.h?
gcc -I $PY27 -E -D OVM_MAIN -
}

extract-all-defs() {
echo '#include "pyconfig.h"'
# 52 different instances. Sometimes multiple ones per file.
find _tmp/oil-tar-test -name '*.[ch]' \
| xargs grep -n 'static PyMethodDef'
find _tmp/oil-tar-test -name '*.[ch]' | xargs -- $0 extract-defs
}

py-method-defs() {
extract-all-defs #| preprocess
}


"$@"
12 changes: 11 additions & 1 deletion build/compile.sh
Expand Up @@ -149,7 +149,16 @@ readonly CC=${CC:-cc} # cc should be on POSIX systems
# isn't necessary. Python's configure.ac sometimes does it by compiling a test
# file; at other times it does it by grepping $CC --help.

readonly BASE_CFLAGS='-fno-strict-aliasing -fwrapv -Wall -Wstrict-prototypes'
BASE_CFLAGS='-fno-strict-aliasing -fwrapv -Wall -Wstrict-prototypes'

# TODO: Enable this once you test that Clang also accepts the flags? Or should
# that be detected by a config file?
# https://stackoverflow.com/questions/6687630/how-to-remove-unused-c-c-symbols-with-gcc-and-ld
#BASE_CFLAGS="$BASE_CFLAGS -fdata-sections -ffunction-sections"
readonly BASE_CFLAGS

#BASE_LDFLAGS='-Wl,--gc-sections'
BASE_LDFLAGS=''

# The user should be able to customize CFLAGS, but it shouldn't disable what's
# in BASE_CFLAGS.
Expand Down Expand Up @@ -211,6 +220,7 @@ build() {
$c_module_src_list \
Modules/ovm.c \
-l m \
${BASE_LDFLAGS} \
$readline_flags \
"$@" \
|| true
Expand Down

0 comments on commit ffe3e73

Please sign in to comment.