#!/bin/sh
# Copyright 2015 Ghislain Antony Vaillant
#
# This file is part of the autopkgtest testsuite for clBLAS.

set -e

# Presence of $ADTTMP implies that someone will handle cleanup for us, so we
# can avoid duplicating the effort (signal handling, etc.) here.
if [ -z "$ADTTMP" ]
then
        echo "Required envvar \"$ADTTMP\"is not set" >&2
        exit 1
fi

# Copy example source code.
cp -a /usr/share/doc/libclblas-doc/examples/* "$ADTTMP"
cd "$ADTTMP"

# Create the CMake project.
cat <<EOF > CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(dummy)

find_package(OpenCL REQUIRED)
include_directories(\${OpenCL_INCLUDE_DIRS})
find_package(clBLAS REQUIRED)
include_directories(\${CLBLAS_INCLUDE_DIRS})
link_directories(\${CLBLAS_LIBRARY_DIRS})

file(GLOB EXAMPLE_FILES "*.c" ".cpp")
foreach(FILE \${EXAMPLE_FILES})
  get_filename_component(EXAMPLE_NAME \${FILE} NAME_WE)
  add_executable(\${EXAMPLE_NAME} \${FILE})
  target_link_libraries(\${EXAMPLE_NAME}
    \${CLBLAS_LIBRARIES}
    \${OpenCL_LIBRARIES}
    \${CMAKE_DL_LIBS}
  )
endforeach()

EOF

# Configure and build.
mkdir build && cd build
cmake ./..
echo "configure: OK"
make
echo "build: OK"
