# ########################################################################
# Copyright 2013 Advanced Micro Devices, Inc.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ########################################################################

# List the names of common files to compile across all platforms
set( clQMC.Source  clQMC.c
			private.c
			latticerule.c
			)

if( MSVC )
    # Use C++ with Microsoft compiler
    SET_SOURCE_FILES_PROPERTIES( ${clQMC.Source} PROPERTIES LANGUAGE CXX)
endif( )

# Windows only uses dllmain
#if( MSVC )
#  set( clQMC.Source ${clQMC.Source} dllmain.cpp )
#endif( )

set( clQMC.Headers
  private.h 
  ../include/clQMC/clQMC.h
  ../include/clQMC/latticerule.h
  )

set( clQMC.Files ${clQMC.Source} ${clQMC.Headers} )

# Include standard OpenCL headers
include_directories( ${OPENCL_INCLUDE_DIRS} ${PROJECT_BINARY_DIR}/include ../include )

#if(BUILD_SHARED_LIBRARY)
#  add_library( clQMC SHARED ${clQMC.Files} )
#else()
  add_library( clQMC STATIC ${clQMC.Files} )
#endif()
target_link_libraries( clQMC ${OPENCL_LIBRARIES} )

set_target_properties( clQMC PROPERTIES VERSION ${CLQMC_VERSION} )
set_target_properties( clQMC PROPERTIES SOVERSION ${CLQMC_SOVERSION} )
set_target_properties( clQMC PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/staging" )

if( CMAKE_COMPILER_IS_GNUCC )
  configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/clQMC.pc.in
    ${CMAKE_CURRENT_BINARY_DIR}/clQMC.pc @ONLY )

  install( FILES ${CMAKE_CURRENT_BINARY_DIR}/clQMC.pc
             DESTINATION lib${SUFFIX_LIB}/pkgconfig )
endif( )

# CPack configuration; include the executable into the package
install( TARGETS clQMC
        EXPORT clQMC-Targets
        RUNTIME DESTINATION bin${SUFFIX_BIN}
        LIBRARY DESTINATION lib${SUFFIX_LIB}
        ARCHIVE DESTINATION lib${SUFFIX_LIB}/import
        )
        
# CMake config files for clQMC
include( CMakePackageConfigHelpers )

set( LIB_INSTALL_DIR lib${SUFFIX_LIB} )
set( INCLUDE_INSTALL_DIR include )
set( ConfigPackageLocation ${LIB_INSTALL_DIR}/cmake/clQMC )

configure_package_config_file(
  clQMCConfig.cmake.in
  ${CMAKE_CURRENT_BINARY_DIR}/clQMCConfig.cmake
  INSTALL_DESTINATION ${ConfigPackageLocation}
  PATH_VARS INCLUDE_INSTALL_DIR LIB_INSTALL_DIR
)

write_basic_package_version_file(
  ${CMAKE_CURRENT_BINARY_DIR}/clQMCConfigVersion.cmake
  VERSION ${CLQMC_VERSION}
  COMPATIBILITY SameMajorVersion
)

# This generates the files that defines the import targets
install( EXPORT clQMC-Targets
  DESTINATION
    ${ConfigPackageLocation}
)

#Copy the config files generated by configure_package_config_file & write_basic_package_version_file
install(FILES
  ${CMAKE_CURRENT_BINARY_DIR}/clQMCConfig.cmake
  ${CMAKE_CURRENT_BINARY_DIR}/clQMCConfigVersion.cmake
  DESTINATION
    ${ConfigPackageLocation} )

# For debug builds, include the debug runtimes into the package for testing on non-developer machines
set( CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_SKIP true )
set( CMAKE_INSTALL_DEBUG_LIBRARIES true )
set( CMAKE_INSTALL_DEBUG_LIBRARIES_ONLY true )

if( WIN32 )
  set( CLQMC_RUNTIME_DESTINATION bin${SUFFIX_BIN} )
else( )
  set( CLQMC_RUNTIME_DESTINATION lib${SUFFIX_LIB} )
endif( )

include( InstallRequiredSystemLibraries )

# Install necessary runtime files for debug builds
install(    PROGRAMS ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS}
            CONFIGURATIONS Debug
	    DESTINATION ${CLQMC_RUNTIME_DESTINATION} )

# Install all *.pdb files for debug builds
install(    DIRECTORY ${PROJECT_BINARY_DIR}/staging/
  DESTINATION ${CLQMC_RUNTIME_DESTINATION}
            OPTIONAL
            CONFIGURATIONS Debug
            FILES_MATCHING PATTERN "*.pdb" )

# Install a snapshot of the source as it was for this build; useful for the .pdb's
install(    DIRECTORY ${PROJECT_SOURCE_DIR}
  DESTINATION ${CLQMC_RUNTIME_DESTINATION}
            OPTIONAL
            CONFIGURATIONS Debug )
