###########################################################################
#
#  Library:   CTK
#
#  Copyright (c) Kitware 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.txt
#
#  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.
#
###########################################################################

#
# Depends on:
#  CTK/CMake/ctkMacroParseArguments.cmake
#

#! \brief Build a CTK library.
#!
#! \ingroup CMakeAPI
macro(ctkMacroBuildLib)
  ctkMacroParseArguments(MY
    "NAME;EXPORT_DIRECTIVE;SRCS;MOC_SRCS;GENERATE_MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;TARGET_LIBRARIES;RESOURCES;LIBRARY_TYPE;EXPORT_CUSTOM_CONTENT_FROM_VARIABLE;BYPASS_REGEX;GENERATE_EXPORT_HEADER;EXPORT_HEADER_NAME"
    "ENABLE_QTTESTING"
    ${ARGN}
    )

  # Keep parameter 'INCLUDE_DIRECTORIES' for backward compatiblity

  # Sanity checks
  if(NOT DEFINED MY_NAME)
    message(FATAL_ERROR "NAME is mandatory")
  endif()
  string(REGEX MATCH "^CTK.+" valid_library_name ${MY_NAME})
  if(NOT valid_library_name)
    message(FATAL_ERROR "CTK library name [${MY_NAME}] should start with 'CTK' uppercase !")
  endif()
  if(NOT DEFINED MY_EXPORT_DIRECTIVE)
    message(FATAL_ERROR "EXPORT_DIRECTIVE is mandatory")
  endif()
  if(NOT DEFINED MY_LIBRARY_TYPE)
    set(MY_LIBRARY_TYPE "SHARED")
  endif()

  # Define library name
  set(lib_name ${MY_NAME})

  # Library target names must not contain a '_' (reserved for plug-in target names)
  if(lib_name MATCHES _)
    message(FATAL_ERROR "The library name ${lib_name} must not contain a '_' character.")
  endif()

  # --------------------------------------------------------------------------
  # Include dirs
  set(my_includes
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_BINARY_DIR}
    # with CMake >2.9, use QT4_MAKE_OUTPUT_FILE instead ?
    ${CMAKE_CURRENT_BINARY_DIR}/Resources/UI
    )

  # Add the include directories from the library dependencies
  ctkFunctionGetIncludeDirs(my_includes ${lib_name})

  include_directories(
    ${my_includes}
    )

  if(CTK_QT_VERSION VERSION_LESS "5")
    # Add Qt include dirs and defines
    include(${QT_USE_FILE})
  endif()

  # Add the library directories from the external project
  ctkFunctionGetLibraryDirs(my_library_dirs ${lib_name})

  link_directories(
    ${my_library_dirs}
    )

  set(MY_LIBRARY_EXPORT_HEADER_NAME ${MY_EXPORT_HEADER_NAME})
  set(MY_LIBRARY_EXPORT_DIRECTIVE ${MY_EXPORT_DIRECTIVE})
  set(MY_EXPORT_HEADER_PREFIX ${MY_NAME})
  # Added a new way to bypass the renaming of header file.
  if(NOT DEFINED MY_BYPASS_REGEX AND NOT DEFINED MY_EXPORT_HEADER_NAME)
    string(REGEX REPLACE "^CTK" "ctk" MY_EXPORT_HEADER_PREFIX ${MY_EXPORT_HEADER_PREFIX})
  else()
    string(REGEX REPLACE "^CTK" "" MY_EXPORT_HEADER_PREFIX ${MY_EXPORT_HEADER_PREFIX})
  endif()

  set(MY_LIBNAME ${lib_name})

  set(CTK_EXPORT_CUSTOM_CONTENT "")
  if(MY_EXPORT_CUSTOM_CONTENT_FROM_VARIABLE)
    if(DEFINED "${MY_EXPORT_CUSTOM_CONTENT_FROM_VARIABLE}")
      set(CTK_EXPORT_CUSTOM_CONTENT "// ------- Expanded from \@CTK_EXPORT_CUSTOM_CONTENT\@ -------
${${MY_EXPORT_CUSTOM_CONTENT_FROM_VARIABLE}}
// ------------------------------------------------
")
    endif()
  endif()

  set(MY_EXPORT_HEADER_SUFFIX "Export")
  
  if(MY_EXPORT_HEADER_NAME)
    set(MY_EXPORT_HEADER_SUFFIX "")
    set(MY_EXPORT_HEADER_PREFIX ${MY_LIBRARY_EXPORT_HEADER_NAME})
  endif()

  configure_file(
    ${CTK_SOURCE_DIR}/Libs/ctkExport.h.in
    ${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}${MY_EXPORT_HEADER_SUFFIX}.h
    )
  set(dynamicHeaders
    "${dynamicHeaders};${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}${MY_EXPORT_HEADER_SUFFIX}.h")

  # Make sure variable are cleared
  set(MY_MOC_CPP)
  set(MY_UI_CPP)
  set(MY_QRC_SRCS)

  # Wrap
  if(MY_MOC_SRCS)
    # this is a workaround for Visual Studio. The relative include paths in the generated
    # moc files can get very long and can't be resolved by the MSVC compiler.
    if(CTK_QT_VERSION VERSION_GREATER "4")
      foreach(moc_src ${MY_MOC_SRCS})
        qt5_wrap_cpp(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src} OPTIONS -DHAVE_QT5)
      endforeach()
    else()
      foreach(moc_src ${MY_MOC_SRCS})
        QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
      endforeach()
    endif()
  endif()
  if(MY_GENERATE_MOC_SRCS)
    QT4_GENERATE_MOCS(${MY_GENERATE_MOC_SRCS})
  endif()
  if(CTK_QT_VERSION VERSION_GREATER "4")
    if(Qt5Widgets_FOUND)
      qt5_wrap_ui(MY_UI_CPP ${MY_UI_FORMS})
    elseif(MY_UI_FORMS)
      message(WARNING "Argument UI_FORMS ignored because Qt5Widgets module was not specified")
    endif()
  else()
    QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  endif()
  if(DEFINED MY_RESOURCES AND NOT MY_RESOURCES STREQUAL "")
    if(CTK_QT_VERSION VERSION_GREATER "4")
      qt5_add_resources(MY_QRC_SRCS ${MY_RESOURCES})
    else()
      QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
    endif()
  endif()

  source_group("Resources" FILES
    ${MY_RESOURCES}
    ${MY_UI_FORMS}
    )

  source_group("Generated" FILES
    ${MY_QRC_SRCS}
    ${MY_MOC_CPP}
    ${MY_UI_CPP}
    ${MOC_CPP_DECORATOR}
    )

  add_library(${lib_name} ${MY_LIBRARY_TYPE}
    ${MY_SRCS}
    ${MY_MOC_CPP}
    ${MY_UI_CPP}
    ${MY_QRC_SRCS}
    )

  # Set labels associated with the target.
  set_target_properties(${lib_name} PROPERTIES LABELS ${lib_name})

  # Apply user-defined properties to the library target.
  if(CTK_LIBRARY_PROPERTIES AND MY_LIBRARY_TYPE STREQUAL "SHARED")
    set_target_properties(${lib_name} PROPERTIES ${CTK_LIBRARY_PROPERTIES})
  endif()
  set_target_properties(${lib_name} PROPERTIES CTK_LIB_TARGET_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

  # Library properties specific to STATIC build
  if(MY_LIBRARY_TYPE STREQUAL "STATIC")
    if(CMAKE_SIZEOF_VOID_P EQUAL 8) # 64-bit
      set_target_properties(${lib_name} PROPERTIES COMPILE_FLAGS "-fPIC")
    endif()
  endif()

  # Install rules
  if(MY_LIBRARY_TYPE STREQUAL "SHARED")
    install(TARGETS ${lib_name} EXPORT CTKExports
      RUNTIME DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
      LIBRARY DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
      ARCHIVE DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Development)
  endif()

  set(my_libs
    ${MY_TARGET_LIBRARIES}
    )

  if(MINGW)
    list(APPEND my_libs ssp) # add stack smash protection lib
  endif()
  target_link_libraries(${lib_name} ${my_libs})

  # Update CTK_BASE_LIBRARIES
  set(CTK_BASE_LIBRARIES ${my_libs} ${lib_name} CACHE INTERNAL "CTK base libraries" FORCE)
  set(CTK_LIBRARIES ${CTK_LIBRARIES} ${lib_name} CACHE INTERNAL "CTK libraries" FORCE)

  # Install headers
  file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.tpp")
  install(FILES
    ${headers}
    ${dynamicHeaders}
    DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
    )

  # MBILOG
  # So that mbilog can work normally:
  include(GenerateExportHeader)

  # 1) the connection between MITK\CMake\mitkFunctionCreateModule.cmake#line#593 has to be incorporated in this macro.
  if (DEFINED MY_GENERATE_EXPORT_HEADER)
    generate_export_header(${lib_name} #${_export_macro_names}
      EXPORT_FILE_NAME ${MY_EXPORT_HEADER_PREFIX}Export.h
    )
  endif()

  # 2) the connection between MITK\CMake\mitkFunctionCreateModule.cmake#line#482 and #line#515 have to be incorporated in this macro.

  # Adding the US_MODULE_NAME connected with the CTK\Libs\mbilog\mbilogConfig.h.in template file
  target_compile_definitions(${lib_name} PRIVATE US_MODULE_NAME=${MY_NAME})

  set_property(TARGET ${lib_name} PROPERTY US_MODULE_NAME ${MY_NAME})

endmacro()


