| 1 | #!/usr/bin/env bash | 
| 2 | # | 
| 3 | # Demonstrate compiler features | 
| 4 | # | 
| 5 | # Usage: | 
| 6 | #   demo/c-compiler.sh <function name> | 
| 7 |  | 
| 8 | set -o nounset | 
| 9 | set -o pipefail | 
| 10 | set -o errexit | 
| 11 |  | 
| 12 | REPO_ROOT=$(cd $(dirname $0)/..; pwd) | 
| 13 |  | 
| 14 | source $REPO_ROOT/build/common.sh | 
| 15 |  | 
| 16 |  | 
| 17 | # chrome://tracing | 
| 18 | # https://aras-p.info/blog/2019/01/16/time-trace-timeline-flame-chart-profiler-for-Clang/ | 
| 19 | ftime-trace() { | 
| 20 | local dir=_tmp/ftime-trace | 
| 21 | mkdir -p $dir | 
| 22 | rm -f -v $dir/* | 
| 23 |  | 
| 24 | echo ' int foo() { return 32; } ' > $dir/lib.cc | 
| 25 | echo ' int main() { return 42; } ' > $dir/main.cc | 
| 26 |  | 
| 27 | #$CLANGXX --version | 
| 28 |  | 
| 29 | # Compiler annoyances: | 
| 30 | # - -ftime-trace is IGNORED without -c, which means compile without linking | 
| 31 | # - Can't specify -o with multiple source files | 
| 32 |  | 
| 33 | set -x | 
| 34 | $CLANGXX -ftime-trace -o $dir/main.o -c $dir/main.cc | 
| 35 | $CLANGXX -ftime-trace -o $dir/lib.o -c $dir/lib.cc | 
| 36 | set +x | 
| 37 | echo | 
| 38 |  | 
| 39 | ls -l $dir | 
| 40 | echo | 
| 41 |  | 
| 42 | # .o file is 'relocatable', otherwise it's 'executable' | 
| 43 | file $dir/* | 
| 44 | echo | 
| 45 | } | 
| 46 |  | 
| 47 | preprocessor() { | 
| 48 | local dir=_tmp/preprocess | 
| 49 | mkdir -p $dir | 
| 50 | rm -f -v $dir/* | 
| 51 |  | 
| 52 | echo ' | 
| 53 | #include <stdio.h> | 
| 54 | int foo() { return 32; } | 
| 55 | ' > $dir/lib.cc | 
| 56 |  | 
| 57 | # Create a file that gets included twice | 
| 58 | { | 
| 59 | echo '#ifndef LIB2_H' | 
| 60 | echo '#define LIB2_H' | 
| 61 |  | 
| 62 | # ~13K lines | 
| 63 | echo '#include <vector>' | 
| 64 |  | 
| 65 | # Almost 32K lines! | 
| 66 | #echo '#include <unordered_map>' | 
| 67 |  | 
| 68 | # This doesn't make a difference!  The preprocessor strips comments | 
| 69 | for i in $(seq 1000); do | 
| 70 | echo '// comment' | 
| 71 | done | 
| 72 |  | 
| 73 | for i in $(seq 1000); do | 
| 74 | echo "int foo$i() { return $i; }" | 
| 75 | done | 
| 76 |  | 
| 77 | echo '#endif  // LIB2_H' | 
| 78 | } > $dir/lib2.h | 
| 79 |  | 
| 80 | echo ' | 
| 81 | #include <vector> | 
| 82 | #include "lib2.h"  // transitive include | 
| 83 |  | 
| 84 | inline int bar() { return 1; } | 
| 85 | ' > $dir/lib.h | 
| 86 |  | 
| 87 | # wow 12K files for <vector> | 
| 88 | echo ' | 
| 89 | #include <vector> | 
| 90 | #include "lib.h" | 
| 91 | #include "lib2.h"  // duplicate include | 
| 92 |  | 
| 93 | int main() { return 42; } | 
| 94 | ' > $dir/main.cc | 
| 95 |  | 
| 96 | $CXX -E $dir/lib.cc > $dir/lib.post.cc | 
| 97 |  | 
| 98 | $CXX -E $dir/main.cc > $dir/main.post.cc | 
| 99 |  | 
| 100 | wc -l $dir/*.post.cc | 
| 101 |  | 
| 102 | # make sure the file compiles | 
| 103 | $CXX -o $dir/main $dir/main.cc | 
| 104 | } | 
| 105 |  | 
| 106 | duplicate-symbols() { | 
| 107 | local dir=_tmp/duplicate-symbols | 
| 108 | rm -f -v $dir/* | 
| 109 | mkdir -p $dir | 
| 110 |  | 
| 111 | echo ' | 
| 112 | #include "mycpp/runtime.h" | 
| 113 |  | 
| 114 | GLOBAL_STR(str0, "hi"); | 
| 115 |  | 
| 116 | int* g1 = new int[100]; | 
| 117 |  | 
| 118 | ' > $dir/lib.cc | 
| 119 |  | 
| 120 | # Why is it OK to link asdl/runtime.cc and _build/cpp/oils-for-unix.cc together? | 
| 121 | # | 
| 122 | # Oh they are NOT linked together.  asdl/runtime.cc is only for tests! | 
| 123 |  | 
| 124 | echo ' | 
| 125 | #include "mycpp/runtime.h" | 
| 126 |  | 
| 127 | GLOBAL_STR(str0, "hi"); | 
| 128 |  | 
| 129 | // int* g1 = new int[100]; | 
| 130 | int* g2 = new int[100]; | 
| 131 |  | 
| 132 | int main() { | 
| 133 | printf("hi\n"); | 
| 134 | } | 
| 135 | ' > $dir/main.cc | 
| 136 |  | 
| 137 | local flags='-D GC' | 
| 138 | $CXX -I . -o $dir/main $flags $dir/lib.cc $dir/main.cc | 
| 139 |  | 
| 140 | $dir/main | 
| 141 | } | 
| 142 |  | 
| 143 | "$@" |