| 1 | #!/usr/bin/env bash
|
| 2 | #
|
| 3 | # Build actions used in the Makefile.
|
| 4 | #
|
| 5 | # Usage:
|
| 6 | # build/ovm-actions.sh <function name>
|
| 7 |
|
| 8 | set -o nounset
|
| 9 | set -o pipefail
|
| 10 | set -o errexit
|
| 11 | shopt -s strict:all 2>/dev/null || true # dogfood for OSH
|
| 12 |
|
| 13 | REPO_ROOT=$(cd $(dirname $0)/..; pwd)
|
| 14 | readonly REPO_ROOT
|
| 15 |
|
| 16 | source build/common.sh
|
| 17 |
|
| 18 | main-name() {
|
| 19 | local python_main=${1:-hello}
|
| 20 | local ovm_bundle_prefix=${2:-hello.ovm}
|
| 21 |
|
| 22 | cat <<EOF
|
| 23 | char* MAIN_NAME = "$python_main";
|
| 24 | #if OVM_DEBUG
|
| 25 | char* OVM_BUNDLE_FILENAME = "${ovm_bundle_prefix}-dbg";
|
| 26 | #else
|
| 27 | char* OVM_BUNDLE_FILENAME = "$ovm_bundle_prefix";
|
| 28 | #endif
|
| 29 | EOF
|
| 30 | }
|
| 31 |
|
| 32 | c-module-toc() {
|
| 33 | cd $PY27
|
| 34 | ../build/c_module_toc.py
|
| 35 | }
|
| 36 |
|
| 37 | # Modules needed to 'import runpy'.
|
| 38 | runpy-deps() {
|
| 39 | $PREPARE_DIR/python -S build/runpy_deps.py both "$@"
|
| 40 | }
|
| 41 |
|
| 42 | runpy-py-to-compile() {
|
| 43 | $PREPARE_DIR/python -S build/runpy_deps.py py
|
| 44 | }
|
| 45 |
|
| 46 | # This version gets the paths out of the repo. But it requires that we
|
| 47 | # build all of Python!
|
| 48 | #
|
| 49 | # OK yeah so there are a few steps to building minimal app bundles.
|
| 50 | # 1. Build all of Python normally. Normal -D options.
|
| 51 | # ./run.sh build-clang-default
|
| 52 | # 2. Then run a special build that is based on that.
|
| 53 | #
|
| 54 | # Only need a debug build.
|
| 55 |
|
| 56 | # Run grep -F .so for the native dependencies. Have to add those
|
| 57 | # somewhere.
|
| 58 | app-deps() {
|
| 59 | local app_name=${1:-hello}
|
| 60 | local pythonpath=${2:-build/testdata}
|
| 61 | local main_module=${3:-hello}
|
| 62 |
|
| 63 | local prefix=_build/$app_name/app-deps
|
| 64 |
|
| 65 | PYTHONPATH=$pythonpath \
|
| 66 | $PREPARE_DIR/python -S build/dynamic_deps.py both $main_module $prefix
|
| 67 | }
|
| 68 |
|
| 69 | # .py files to compile
|
| 70 | py-to-compile() {
|
| 71 | local pythonpath=${1:-build/testdata}
|
| 72 | local main_module=${2:-hello}
|
| 73 |
|
| 74 | PYTHONPATH=$pythonpath \
|
| 75 | $PREPARE_DIR/python -S build/dynamic_deps.py py $main_module
|
| 76 | }
|
| 77 |
|
| 78 | # For embedding in oil/bytecode.zip.
|
| 79 | help-manifest() {
|
| 80 | local dir=$1
|
| 81 | for path in $dir/*; do
|
| 82 | echo "$path $path" # relative path is the same
|
| 83 | done
|
| 84 | }
|
| 85 |
|
| 86 | ysh-stdlib-manifest() {
|
| 87 | for path in stdlib/*.ysh; do
|
| 88 | echo "$path $path" # relative path is the same
|
| 89 | done
|
| 90 | }
|
| 91 |
|
| 92 | pyc-version-manifest() {
|
| 93 | local manifest_path=${1:-_build/oil/bytecode-opy-manifest.txt} # For example
|
| 94 |
|
| 95 | # Just show a string like "bytecode-opy.zip" for now. There is no OPy
|
| 96 | # version yet.
|
| 97 | local filename=$(basename $manifest_path)
|
| 98 | local user_str=${filename%-manifest.txt}.zip
|
| 99 | local dir=$(dirname $manifest_path)
|
| 100 |
|
| 101 | echo $user_str > $dir/pyc-version.txt
|
| 102 |
|
| 103 | # Put it at the root, like release-date and oil-version.txt.
|
| 104 | echo $dir/pyc-version.txt pyc-version.txt
|
| 105 | }
|
| 106 |
|
| 107 | # Make .d file
|
| 108 | make-dotd() {
|
| 109 | local app_name=${1:-hello}
|
| 110 | local app_deps_to_compile=${2:-_tmp/hello/app-deps-to-compile.txt}
|
| 111 |
|
| 112 | # TODO: For each module, look it up in the manifest.
|
| 113 | # I guess make a Python file.
|
| 114 |
|
| 115 | echo "# TODO $app_deps_to_compile"
|
| 116 |
|
| 117 | # The dependencies we want.
|
| 118 | # X to prevent screwing things up.
|
| 119 | echo "X_build/$app_name/ovm:"
|
| 120 | echo "X_build/$app_name/ovm-dbg:"
|
| 121 | echo "X_build/$app_name/ovm-cov:"
|
| 122 | }
|
| 123 |
|
| 124 | #
|
| 125 | # C Code generation. The three functions below are adapted from
|
| 126 | # Modules/makesetup.
|
| 127 | #
|
| 128 |
|
| 129 | extdecls() {
|
| 130 | for mod in "$@"; do
|
| 131 | test "$mod" = line_input && echo "#ifdef HAVE_READLINE"
|
| 132 | echo "extern void init$mod(void);"
|
| 133 | test "$mod" = line_input && echo "#endif"
|
| 134 | done
|
| 135 | return 0 # because test can fail
|
| 136 | }
|
| 137 |
|
| 138 | initbits() {
|
| 139 | for mod in "$@"; do
|
| 140 | test "$mod" = line_input && echo "#ifdef HAVE_READLINE"
|
| 141 | echo " {\"$mod\", init$mod},"
|
| 142 | test "$mod" = line_input && echo "#endif"
|
| 143 | done
|
| 144 | return 0 # because test can fail
|
| 145 | }
|
| 146 |
|
| 147 | # Ported from sed to awk. Awk is MUCH nicer (no $NL ugliness, -v flag, etc.)
|
| 148 | gen-module-init() {
|
| 149 | local extdecls
|
| 150 | extdecls=$(extdecls "$@")
|
| 151 | local initbits
|
| 152 | initbits=$(initbits "$@")
|
| 153 |
|
| 154 | local template=$PY27/Modules/config.c.in
|
| 155 |
|
| 156 | awk -v template=$template -v extdecls="$extdecls" -v initbits="$initbits" '
|
| 157 | BEGIN {
|
| 158 | print "/* Generated automatically from " template " */"
|
| 159 | }
|
| 160 | /MARKER 1/ {
|
| 161 | print extdecls
|
| 162 | next
|
| 163 | }
|
| 164 | /MARKER 2/ {
|
| 165 | print initbits
|
| 166 | next
|
| 167 | }
|
| 168 | {
|
| 169 | print $0
|
| 170 | }
|
| 171 | ' $template
|
| 172 | }
|
| 173 |
|
| 174 | #
|
| 175 | # C Modules
|
| 176 | #
|
| 177 |
|
| 178 | join-modules() {
|
| 179 | local static=${1:-static-c-modules.txt}
|
| 180 | local discovered=${2:-_build/oil/all-deps-c.txt}
|
| 181 |
|
| 182 | # Filter out comments, print the first line.
|
| 183 | #
|
| 184 | # TODO: I don't want to depend on egrep and GNU flags on the target systems?
|
| 185 | # Ship this file I guess.
|
| 186 | egrep --no-filename --only-matching '^[a-zA-Z0-9_\.]+' $static $discovered \
|
| 187 | | sort | uniq
|
| 188 | }
|
| 189 |
|
| 190 | "$@"
|