# the stub exe. in a folder of its own so the runtime switch below stays off
# everything else in the build.
#
# two things can be set before this is added:
#   GARAGE_STUB_ICON     an .ico for it. shortcuts point at the stub, so this
#                        is the icon people see
#   GARAGE_STUB_OUT_DIR  where the exe goes, for a build that has to know
cmake_minimum_required(VERSION 3.14)

# the stub sits on its own in <root>. the vc runtime dlls, if the app ships
# them at all, are a folder down where it cant see them. so its linked static.
# flutters project() runs before its cmake_policy(), which leaves /MD sat in
# the flags and MSVC_RUNTIME_LIBRARY ignored, so both ways are covered
foreach(_flags
    CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_PROFILE CMAKE_CXX_FLAGS_RELEASE
    CMAKE_CXX_FLAGS_RELWITHDEBINFO CMAKE_CXX_FLAGS_MINSIZEREL)
  if(DEFINED ${_flags})
    string(REPLACE "/MDd" "/MTd" ${_flags} "${${_flags}}")
    string(REPLACE "/MD" "/MT" ${_flags} "${${_flags}}")
  endif()
endforeach()

set(_stub_sources "garage_stub.cpp" "garage_common.h")
if(MSVC)
  # as a source so the linker merges it. through the .rc it would collide with
  # the manifest the linker makes itself
  list(APPEND _stub_sources "garage_stub.exe.manifest")
endif()


set(GARAGE_STUB_ICON_LINE "")
if(GARAGE_STUB_ICON AND EXISTS "${GARAGE_STUB_ICON}")
  # forward slashes. a backslash inside an .rc string is an escape
  file(TO_CMAKE_PATH "${GARAGE_STUB_ICON}" _stub_icon)
  set(GARAGE_STUB_ICON_LINE "1 ICON \"${_stub_icon}\"")
else()
  message(STATUS "garage_updates: no icon for the stub, it gets the plain windows one")
endif()

# with msvc and no icon there would be nothing in the .rc at all, so it stays out
if(GARAGE_STUB_ICON_LINE OR NOT MSVC)
  set(GARAGE_STUB_MANIFEST_FILE "${CMAKE_CURRENT_SOURCE_DIR}/garage_stub.exe.manifest")
  configure_file("garage_stub.rc.in" "${CMAKE_CURRENT_BINARY_DIR}/garage_stub.rc" @ONLY)
  list(APPEND _stub_sources "${CMAKE_CURRENT_BINARY_DIR}/garage_stub.rc")
  if(NOT MSVC)
    set_property(SOURCE "${CMAKE_CURRENT_BINARY_DIR}/garage_stub.rc" APPEND PROPERTY
      COMPILE_DEFINITIONS GARAGE_RC_MANIFEST)
  endif()
endif()

add_executable(garage_stub WIN32 ${_stub_sources})
target_compile_features(garage_stub PRIVATE cxx_std_17)
target_compile_definitions(garage_stub PRIVATE UNICODE _UNICODE)
set_target_properties(garage_stub PROPERTIES
  OUTPUT_NAME "garage_stub"
  MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
if(MSVC)
  # no APPLY_STANDARD_SETTINGS. thats /WX, and this has never been through msvc
  target_compile_options(garage_stub PRIVATE /W4 /EHsc /utf-8)
endif()
if(MINGW)
  # mingw only looks for wWinMain when its told to
  target_link_options(garage_stub PRIVATE -municode "-Wl,--subsystem,windows")
endif()
target_link_libraries(garage_stub PRIVATE user32 shell32 advapi32)

if(GARAGE_STUB_OUT_DIR)
  # the empty genex stops visual studio adding a Release\ on the end
  set_target_properties(garage_stub PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${GARAGE_STUB_OUT_DIR}$<0:>")
endif()
