# avoid some cmake warnings
IF(POLICY CMP0026)
 CMAKE_POLICY(SET CMP0026 OLD)
ENDIF()

INCLUDE_DIRECTORIES(${LUA_INCDIR})
IF(LUALIB)
  LINK_DIRECTORIES(${LUA_LIBDIR}) # note: must be done before defining target
ENDIF()

ADD_LIBRARY(luaT SHARED luaT.h luaT.c)
if(BUILD_STATIC)
  ADD_LIBRARY(luaT_static STATIC luaT.h luaT.c)
endif()

IF(APPLE)
  SET_TARGET_PROPERTIES(luaT PROPERTIES
    LINK_FLAGS "-undefined dynamic_lookup")
ENDIF()

IF(LUALIB)
  TARGET_LINK_LIBRARIES(luaT ${LUALIB}) # must be done after ;)
ENDIF()

INSTALL(TARGETS luaT
  EXPORT torch-exports
  RUNTIME DESTINATION "${Torch_INSTALL_BIN_SUBDIR}"
  LIBRARY DESTINATION "${Torch_INSTALL_LIB_SUBDIR}"
  ARCHIVE DESTINATION "${Torch_INSTALL_LIB_SUBDIR}")

INSTALL(FILES luaT.h
          DESTINATION "${Torch_INSTALL_INCLUDE_SUBDIR}")

# Create luaT.cmake
GET_TARGET_PROPERTY(LUAT_OUTPUT_NAME luaT LOCATION)
GET_FILENAME_COMPONENT(LUAT_OUTPUT_NAME ${LUAT_OUTPUT_NAME} NAME)
SET(LUAT_LIBRARIES "${Torch_INSTALL_LIB}/${LUAT_OUTPUT_NAME}")
SET(LUAT_INCLUDE_DIR "${Torch_INSTALL_INCLUDE}")
CONFIGURE_FILE(luaTConfig.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/cmake-exports/luaTConfig.cmake")
INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake-exports/luaTConfig.cmake" 
  DESTINATION "${Torch_INSTALL_CMAKE_SUBDIR}")
