gencl.sh (1709B)
1 #!/bin/sh 2 # Run the C preprocessor on an OpenCL kernel to generate a C string array 3 # suitable for clCreateProgramWithSource. This allows us to create 4 # standalone OpenCL programs that do not depend on paths to the source 5 # tree (the programs will still run the OpenCL run-time compiler to 6 # compile the kernel, but the kernel is a string within the program, with 7 # no external include dependencies) 8 # Mark Moraes, D. E. Shaw Research 9 10 # indenting the cpp output makes errors from the OpenCL runtime compiler 11 # much more understandable. User can override with whatever they want. 12 # The classic BSD indent (yes, the one that lived in /usr/ucb/indent once) 13 # defaults to -br, but recent GNU indent versions do not. Both appear to 14 # accept -br, fortunately... (BSD indent does not accept -kr or -linux, alas) 15 16 PATH=$PATH:/usr/bin 17 export PATH 18 if type indent > /dev/null 2>&1; then 19 : ${GENCL_INDENT=indent} 20 else 21 : ${GENCL_INDENT=cat} 22 fi 23 24 # We rely on gsub in awk, which exists in everything except classic 25 # old V7 awk (Solaris!). If we can find gawk or nawk, we prefer those. 26 # http://www.shelldorado.com/articles/awkcompat.html 27 for f in gawk nawk awk; do 28 if type "$f" > /dev/null 2>&1; then 29 : ${GENCL_AWK="$f"} 30 break 31 fi 32 done 33 [ ${GENCL_AWK:+set} ] || { echo "$0: could not find awk!">&2; exit 1; } 34 35 usage="Usage: $0 inputoclfilename" 36 case $# in 37 1) ;; 38 *) echo "$usage" >&2; exit 1;; 39 esac 40 case "$1" in 41 ''|-*) echo "Invalid or empty inputoclfilename: $1 42 $usage" >&2; exit 1;; 43 esac 44 set -e 45 ${CC-cc} -xc -E -P -nostdinc -D__OPENCL_VERSION__=1 $CPPFLAGS "$1" | 46 ${GENCL_INDENT} | 47 ${GENCL_AWK} 'BEGIN {print "\"\\n\\"} 48 {gsub("\\", "\\\\", $0); gsub("\"", "\\\"", $0); print $0 "\\n\\"} 49 END {print "\""}'