#!/bin/sh # Write a C source holding each runtime header's text, so zi2c # can copy the headers a program includes next to its generated code, # and which other embedded headers each text includes (bit j for header j). set -eu test $# -le 32 || { echo "embed_headers.sh: at most 32 headers" >&2; exit 1; } echo '/* Generated by scripts/embed_headers.sh. Do not edit. */' echo '#include "compiler_runtime_emit.h"' echo '#include ' echo 'static const RuntimeHeader runtime_headers[] = {' for header in "$@"; do printf ' {"%s",\n' "$(basename "$header")" sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/^/ "/' -e 's/$/\\n"/' "$header" includes=0 bit=0 for other in "$@"; do if grep -qF "#include \"$(basename "$other")\"" "$header"; then includes="$includes | 1u << $bit" fi bit=$((bit + 1)) done printf ' , %s},\n' "$includes" done echo ' {NULL, NULL, 0}' echo '};' echo 'int64_t CompilerRuntimeHeaderCount(void) {' echo ' return (int64_t)(sizeof(runtime_headers) / sizeof(runtime_headers[0]) - 1);' echo '}' echo 'const RuntimeHeader *CompilerRuntimeHeaderAt(int64_t index) {' echo ' if (index < 0 || index >= CompilerRuntimeHeaderCount()) return NULL;' echo ' return &runtime_headers[index];' echo '}'