# brief: Create source groups from specified directory and child directies for all child files.
# param: mainDir The directory where you want create source groups.
# remarks: This macro is recursive if child is directory too.
macro(GroupSources mainDir)
  file(GLOB children RELATIVE ${PROJECT_SOURCE_DIR}/${mainDir} ${PROJECT_SOURCE_DIR}/${mainDir}/*)
  foreach(child ${children})
    if(IS_DIRECTORY ${PROJECT_SOURCE_DIR}/${mainDir}/${child})
      GroupSources(${mainDir}/${child})
    else()
      string(REPLACE "/" "\\" groupName ${mainDir})
      string(REPLACE ".\\" "" groupName ${groupName})
      if (${groupName} STREQUAL ".")
        set(groupName "")
      endif()
      source_group(${groupName} FILES ${PROJECT_SOURCE_DIR}/${mainDir}/${child})
    endif()
  endforeach()
endmacro()

macro(MakeLibraryProject)
  file(GLOB_RECURSE SOURCE_FILES "*.hpp" "*.cpp")

  if (APPLE)
    file(GLOB_RECURSE COCOA_SOURCE_FILES "*.mm")
  endif ()

  GroupSources(.)

  if (BUILD_SWITCH_SHARED_LIBS)
    set(BUILD_SWITCH_TYPE_LIBRARY SHARED)
    if (UNIX)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
    endif() 
    add_definitions(-D${SWITCH_EXPORT})
  else()
    set(BUILD_SWITCH_TYPE_LIBRARY STATIC)
  endif()
  add_library(${PROJECT_NAME} ${BUILD_SWITCH_TYPE_LIBRARY} ${SOURCE_FILES} ${COCOA_SOURCE_FILES})
  target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>)
  target_link_libraries(${PROJECT_NAME} ${LINK_LIBRARIES})
  set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER src)

  install(TARGETS ${PROJECT_NAME} EXPORT Switch DESTINATION lib)

  if (EXISTS ${PROJECT_SOURCE_DIR}/include/Switch)
    install(DIRECTORY "include/Switch/." DESTINATION include/Switch)
  endif()

  if (MSVC)
    target_compile_options(${PROJECT_NAME} PRIVATE "$<$<CONFIG:Debug>:/Fd$<TARGET_FILE_DIR:${PROJECT_NAME}>/${PROJECT_NAME}${CMAKE_DEBUG_POSTFIX}.pdb>")
    target_compile_options(${PROJECT_NAME} PRIVATE "$<$<CONFIG:Release>:/Fd$<TARGET_FILE_DIR:${PROJECT_NAME}>/${PROJECT_NAME}.pdb>")
    install(FILES $<TARGET_FILE_DIR:${PROJECT_NAME}>/${PROJECT_NAME}${CMAKE_DEBUG_POSTFIX}.pdb DESTINATION lib CONFIGURATIONS Debug OPTIONAL)
    install(FILES $<TARGET_FILE_DIR:${PROJECT_NAME}>/${PROJECT_NAME}.pdb DESTINATION lib CONFIGURATIONS Release OPTIONAL)
  endif()
endmacro()
