#!/bin/sh

# extracts various bits of data form OpenCL heder files, which are expected to
# be in CL/

ALL_H="CL/cl.h CL/cl_ext.h CL/cl_gl.h CL/cl_gl_ext.h CL11/cl_d3d9.h CL/cl_d3d10.h CL12/cl_d3d11.h CL/cl_dx9_media_sharing.h"

# constants
(
   echo '// autogenerated by h_extract'
   cat $ALL_H |
      perl -ne 'print "const_iv($1)\n" if /^#define CL_(\S+)/'
   cat CL/cl_platform.h |
      perl -ne 'print "const_iv($1)\n" if /^#define CL_(\S+)\s+[^.]+\s*$/' |
      sort | uniq |
      perl -ne 'print unless /const_iv\((?:MAXFLOAT|INFINITY|NAN|HUGE_VALF?)\)/'
) >constiv.h

# fp constants
(
   echo '// autogenerated by h_extract'
   cat CL/cl_platform.h |
      perl -ne 'print "const_nv($1)\n" if /^#define\s+CL_(\S+)\s+.*\..*\s*$/'
   echo "const_nv(MAXFLOAT)"
   echo "const_nv(INFINITY)"
   echo "const_nv(HUGE_VAL)"
   echo "const_nv(HUGE_VALF)"
) >constnv.h

# constants that look like enum values
(
   echo '// autogenerated by h_extract'
   cat $ALL_H |
      perl -ne 'print "const_iv($1)\n" if /^#define CL_(\S+)\s+0x[0-9a-fA-F]{4}/'
) >enumstr.h

# error codes
(
   echo '// autogenerated by h_extract'
   echo "const_iv(SUCCESS)"
   cat $ALL_H |
      perl -ne 'print "const_iv($1)\n" if !/#define CL_BUILD_(?:NONE|ERROR|IN_PROGRESS)/ && /^#define CL_(\S+)\s+-\d+\s*$/'
) >errstr.h

# default constant values to -1
(
   echo '// autogenerated by h_extract'
   cat constiv.h |
      perl -ne '/const_iv\((.*)\)/ or next; print "#ifndef CL_$1\n#define CL_$1 -1\n#endif\n"'
) >default.h

# *glGet*Info
(
   <CL/cl.hpp perl -ne 'print "$1\n" if /^\s*F\((.*)\)\s*\\?\s*$/'
   # DEVICE_DOUBLE_FP_CONFIG
   # DEVICE_HALF_FP_CONFIG
   # PLATFORM_ICD_SUFFIX_KHR
   # PLATFORM_NOT_FOUND_KHR
   # DEVICE_COMPUTE_CAPABILITY_MAJOR_NV
   # DEVICE_COMPUTE_CAPABILITY_MINOR_NV
   # DEVICE_REGISTERS_PER_BLOCK_NV
   # DEVICE_WARP_SIZE_NV
   # DEVICE_GPU_OVERLAP_NV
   # DEVICE_KERNEL_EXEC_TIMEOUT_NV
   # DEVICE_INTEGRATED_MEMORY_NV
   # DEVICE_PROFILING_TIMER_OFFSET_AMD

   cat <<EOF
cl_program_build_info, CL_PROGRAM_BINARY_TYPE, cl_program_binary_type
cl_gl_texture_info, CL_GL_TEXTURE_TARGET, cl_GLenum
cl_gl_texture_info, CL_GL_MIPMAP_LEVEL, cl_GLint
cl_kernel_arg_info, CL_KERNEL_ARG_ADDRESS_QUALIFIER, cl_kernel_arg_address_qualifier
cl_kernel_arg_info, CL_KERNEL_ARG_ACCESS_QUALIFIER, cl_kernel_arg_access_qualifier
cl_kernel_arg_info, CL_KERNEL_ARG_TYPE_NAME, STRING_CLASS
cl_kernel_arg_info, CL_KERNEL_ARG_TYPE_QUALIFIER, cl_kernel_arg_type_qualifier
cl_kernel_arg_info, CL_KERNEL_ARG_NAME, STRING_CLASS
EOF
) >getinfo.txt

