configure_file(app_version.h.cmakein app_version.h ESCAPE_QUOTES)

if(NOT IS_ABSOLUTE "${IDE_ICON_PATH}")
  set(IDE_ICON_PATH "${CMAKE_CURRENT_SOURCE_DIR}/${IDE_ICON_PATH}")
endif()

if (NOT IS_ABSOLUTE ${IDE_LOGO_PATH})
  set(IDE_LOGO_PATH "${CMAKE_CURRENT_SOURCE_DIR}/${IDE_LOGO_PATH}")
endif()
configure_file(app_logo.qrc.cmakein app_logo_cmake.qrc)

# This needs to be explicit like this, to avoid issues with the path
# to the binary in generated SBOMs.
set(qtcreator_props "")
if(APPLE)
  list(APPEND qtcreator_props MACOSX_BUNDLE ON)
endif()
if(WIN32)
  list(APPEND qtcreator_props WIN32_EXECUTABLE ON)
endif()
add_qtc_executable(qtcreator
  DEFINES
    IDE_LIBRARY_BASENAME=\"${IDE_LIBRARY_BASE_PATH}\"
    IDE_APP_ID=\"${IDE_APP_ID}\"
  DEPENDS ExtensionSystem Qt::Core Qt::Widgets Utils shared_qtsingleapplication app_version
  SOURCES
    main.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/app_logo_cmake.qrc
    ../tools/qtcreatorcrashhandler/crashhandlersetup.cpp ../tools/qtcreatorcrashhandler/crashhandlersetup.h
  PROPERTIES
    ${qtcreator_props}
    OUTPUT_NAME "${IDE_APP_TARGET}"
  DESTINATION "${IDE_APP_PATH}"
  QTC_RUNNABLE
  QT_APP
)

extend_qtc_executable(qtcreator
  CONDITION APPLE
  DEPENDS ${FWFoundation}
  SOURCES
    main_mac.mm
)

extend_qtc_executable(qtcreator
  CONDITION SHOW_BUILD_DATE
  DEFINES QTC_SHOW_BUILD_DATE
)

if (NOT TARGET qtcreator)
  return()
endif()

# A HAP has nothing next to the application: every library is flattened into libs/<arch>
# and everything else lives among the module's resource files, while this build assumes
# ../lib/qtcreator/plugins and ../share/qtcreator next to the executable. Qt Creator can be
# told both, so the package carries its resources as a resource file directory and the run
# passes the two paths as arguments. The deploy steps of the HarmonyOS plugin read this.
if (OHOS)
  # An IDE on the device has to reach the user's own files, which is what this permission
  # is for; the provisioning profile has to allow it as well.
  qt_add_harmonyos_permission(qtcreator
    NAME ohos.permission.READ_WRITE_USER_FILE
    REASON "Open and save the files you work on"
    USED_SCENE_WHEN always
  )

  set(QTC_HARMONYOS_NATIVE_PACKAGE_FILES "" CACHE STRING
    "Files to put into the native package of Qt Creator's HarmonyOS package, \
for example an ssh client and its libraries")

  # Runs what a project built on the device, which needs a process the debugger may trace:
  # the device refuses to execute a file Qt Creator wrote and grants tracing per
  # application, so this goes into the native package, which Qt Creator may execute.
  set(_harmonyos_host_dir "${CMAKE_CURRENT_BINARY_DIR}/harmonyos")
  add_executable(qtchost
    "${PROJECT_SOURCE_DIR}/share/qtcreator/harmonyos/qtchost.cpp"
    "${PROJECT_SOURCE_DIR}/share/qtcreator/harmonyos/qtcload.cpp"
  )
  target_include_directories(qtchost
    PRIVATE "${PROJECT_SOURCE_DIR}/share/qtcreator/harmonyos")
  target_link_libraries(qtchost PRIVATE ${CMAKE_DL_LIBS})
  # In the application's context only what the application itself brought is on the
  # library path, so this depends on the C library alone.
  target_link_options(qtchost PRIVATE -static-libstdc++)
  set_target_properties(qtchost PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${_harmonyos_host_dir}")

  set(_harmonyos_native_files "\"${_harmonyos_host_dir}/qtchost\"")
  foreach(_file IN LISTS QTC_HARMONYOS_NATIVE_PACKAGE_FILES)
    if(_harmonyos_native_files)
      string(APPEND _harmonyos_native_files ",\n        ")
    endif()
    string(APPEND _harmonyos_native_files "\"${_file}\"")
  endforeach()

  set(_harmonyos_resources "/data/storage/el1/bundle/entry/resources/resfile")
  file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/qtcreator-harmonyos-extras.json"
"{
    \"resource-directories\": [
        \"${PROJECT_BINARY_DIR}/${IDE_DATA_PATH}\"
    ],
    \"native-package-files\": [
        ${_harmonyos_native_files}
    ],
    \"launch-arguments\": [
        \"-pluginpath\", \"/data/storage/el1/bundle/libs/arm64\",
        \"-resourcepath\", \"${_harmonyos_resources}/qtcreator\",
        \"-load\", \"HarmonyOS\"
    ]
}
")
endif()

if (QTC_STATIC_BUILD)
  set(plugins_to_import "")
  set(source_file_content "#include <QtPlugin>\n")
  foreach(plugin IN LISTS __QTC_PLUGINS)
    if(TARGET ${plugin})
      list(APPEND plugins_to_import "${plugin}")
      # TODO ${plugin}Plugin is not correct for all plugins as the class name
      get_target_property(plugin_class ${plugin} QTC_PLUGIN_CLASS_NAME)
      string(APPEND source_file_content "Q_IMPORT_PLUGIN(${plugin_class})\n")
    endif()
  endforeach()

  string(APPEND source_file_content
    "struct ResourcesInitializer {\n"
    "  ResourcesInitializer() {\n")
  foreach(resource_file IN LISTS __QTC_RESOURCE_FILES)
     string(APPEND source_file_content "    Q_INIT_RESOURCE(${resource_file});\n")
  endforeach()
  string(APPEND source_file_content
    "  }\n"
    "} g_resources_initializer;\n")

  file(GENERATE
    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/plugin_imports.cpp"
    CONTENT "${source_file_content}"
  )
  extend_qtc_target(qtcreator
    DEPENDS ${plugins_to_import}
    SOURCES "${CMAKE_CURRENT_BINARY_DIR}/plugin_imports.cpp"
  )

  if (TARGET QmlDesigner)
    qt6_import_qml_plugins(qtcreator
      PATH_TO_SCAN "${CMAKE_CURRENT_SOURCE_DIR}/../../share/qtcreator/qmldesigner")
  endif()
endif()

if (WIN32)
  set(RC_APPLICATION_NAME "${IDE_DISPLAY_NAME}")
  set(RC_VERSION "${IDE_VERSION}.0")
  set(RC_VERSION_STRING "${IDE_VERSION_DISPLAY}")
  set(RC_COPYRIGHT "${IDE_COPYRIGHT}")
  set(RC_PUBLISHER "${IDE_PUBLISHER}")

  string(REPLACE " " "\\x20" RC_APPLICATION_NAME "${RC_APPLICATION_NAME}")
  string(REPLACE " " "\\x20" RC_PUBLISHER "${RC_PUBLISHER}")
  string(REPLACE " " "\\x20" RC_COPYRIGHT "${RC_COPYRIGHT}")
  string(REPLACE "." "," RC_VERSION "${RC_VERSION}")

  target_compile_definitions(qtcreator PRIVATE
    RC_PUBLISHER=${RC_PUBLISHER}
    RC_APPLICATION_NAME=${RC_APPLICATION_NAME}
    RC_VERSION=${RC_VERSION}
    RC_VERSION_STRING=${RC_VERSION_STRING}
    RC_COPYRIGHT=${RC_COPYRIGHT}
    RC_ICON_PATH=${IDE_ICON_PATH}
  )

  target_sources(qtcreator PRIVATE qtcreator.rc)
endif()

if (APPLE)
  if (CMAKE_OSX_DEPLOYMENT_TARGET)
    set(MACOSX_DEPLOYMENT_TARGET ${CMAKE_OSX_DEPLOYMENT_TARGET})
  endif()
  set_target_properties(qtcreator PROPERTIES
      MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/app-Info.plist"
  )
  # The profiler's call-stack sampler attaches with task_for_pid(), which the
  # kernel only grants to a binary carrying the debugger entitlement. Packaging
  # signs a release with the file below (see scripts/common.py); a build from the
  # source tree is signed ad-hoc without entitlements, so native sampling does not
  # work there. Sign it with the same file that packaging uses - the whole list,
  # not the debugger entitlement alone - so a source build behaves like a released
  # one.
  qtc_sign_with_entitlements(qtcreator
    "${QtCreator_SOURCE_DIR}/dist/installer/mac/Qt Creator.entitlements")
  # icon
  ## Roughly check for Xcode 26 tools for the default icon generation method
  if(${CMAKE_C_COMPILER_ID} STREQUAL "AppleClang"
      AND ${CMAKE_C_COMPILER_VERSION} VERSION_GREATER_EQUAL 17.0.0.17000319)
    set(QTC_USE_XCODE_26_FOR_APPICON_DEFAULT ON)
  else()
    set(QTC_USE_XCODE_26_FOR_APPICON_DEFAULT OFF)
  endif()
  option(QTC_USE_XCODE_26_FOR_APPICON
    "Use Xcode 26 tools to generate the app icon from Icon Composer"
    ${QTC_USE_XCODE_26_FOR_APPICON_DEFAULT}
  ) # switch to ON to disable legacy mode
  if(QTC_USE_XCODE_26_FOR_APPICON)
    set(QTCREATOR_APP_ICON "${CMAKE_CURRENT_BINARY_DIR}/qtcreator.icns")
    set(QTCREATOR_ACTOOL_APP_ICON_OUTPUT "${QTCREATOR_APP_ICON}")
    set(QTCREATOR_APP_ICON_ASSET "${IDE_ICON_PATH}/macos/qtcreator.icon")
    set(QTCREATOR_APP_ICON_FILES
      macos/qtcreator.icon/icon.json
      macos/qtcreator.icon/Assets/icon.png
    )
  else()
    set(QTCREATOR_APP_ICON "${IDE_ICON_PATH}/macos/legacy/qtcreator.icns")
    set(QTCREATOR_ACTOOL_APP_ICON_OUTPUT)
    set(QTCREATOR_APP_ICON_ASSET "${CMAKE_CURRENT_BINARY_DIR}/qtcreator-appicon.xcassets")
    set(QTCREATOR_APP_ICON_FILES
      ${QTCREATOR_APP_ICON_ASSET}/qtcreator.iconset/icon_16x16.png
      ${QTCREATOR_APP_ICON_ASSET}/qtcreator.iconset/icon_16x16@2x.png
      ${QTCREATOR_APP_ICON_ASSET}/qtcreator.iconset/icon_128x128.png
      ${QTCREATOR_APP_ICON_ASSET}/qtcreator.iconset/icon_128x128@2x.png
    )
    add_custom_command(
      OUTPUT ${QTCREATOR_APP_ICON_FILES}
      COMMAND
        iconutil -c iconset
        "${QTCREATOR_APP_ICON}"
        -o "${QTCREATOR_APP_ICON_ASSET}/qtcreator.iconset"
      DEPENDS
        "${QTCREATOR_APP_ICON}"
      VERBATIM
    )
  endif()
  set(QTCREATOR_PROJECT_ICONSET_FILES
    macos/qtcreator.xcassets/qtcreator-project.iconset/icon_128x128.png
    macos/qtcreator.xcassets/qtcreator-project.iconset/icon_128x128@2x.png
    macos/qtcreator.xcassets/qtcreator-project.iconset/icon_16x16.png
    macos/qtcreator.xcassets/qtcreator-project.iconset/icon_16x16@2x.png
    macos/qtcreator.xcassets/qtcreator-project.iconset/icon_256x256.png
    macos/qtcreator.xcassets/qtcreator-project.iconset/icon_256x256@2x.png
    macos/qtcreator.xcassets/qtcreator-project.iconset/icon_32x32.png
    macos/qtcreator.xcassets/qtcreator-project.iconset/icon_32x32@2x.png
    macos/qtcreator.xcassets/qtcreator-project.iconset/icon_512x512.png
    macos/qtcreator.xcassets/qtcreator-project.iconset/icon_512x512@2x.png
  )
  add_custom_command(
    OUTPUT Assets.car ${QTCREATOR_ACTOOL_APP_ICON_OUTPUT}
    COMMAND
      xcrun actool --app-icon qtcreator --output-partial-info-plist "${CMAKE_CURRENT_BINARY_DIR}/qtcreator.Info.plist"
      --platform macosx --target-device mac --minimum-deployment-target 11.0 --compile "${CMAKE_CURRENT_BINARY_DIR}"
      --bundle-identifier "${IDE_BUNDLE_IDENTIFIER}"
      "${IDE_ICON_PATH}/macos/qtcreator.xcassets" "${QTCREATOR_APP_ICON_ASSET}"
      > /dev/null
    BYPRODUCTS qtcreator.Info.plist
    DEPENDS
      ${QTCREATOR_PROJECT_ICONSET_FILES}
      ${QTCREATOR_APP_ICON_FILES}
    VERBATIM
  )
  add_custom_command(
    OUTPUT qtcreator-project.icns
    COMMAND
      iconutil -c icns
      "${IDE_ICON_PATH}/macos/qtcreator.xcassets/qtcreator-project.iconset"
      -o "${CMAKE_CURRENT_BINARY_DIR}/qtcreator-project.icns"
    DEPENDS
      ${QTCREATOR_PROJECT_ICONSET_FILES}
    VERBATIM
  )
  target_sources(qtcreator
    PRIVATE
      "${CMAKE_CURRENT_BINARY_DIR}/Assets.car"
      ${QTCREATOR_APP_ICON}
      "${CMAKE_CURRENT_BINARY_DIR}/qtcreator-project.icns"
  )
  qtc_copy_to_builddir(copy_icns
    DESTINATION ${IDE_DATA_PATH}
    FILES
      ${CMAKE_CURRENT_BINARY_DIR}/Assets.car
      "${QTCREATOR_APP_ICON}"
      ${IDE_ICON_PATH}/macos/qtcreator.icon/Assets/icon.png
      ${CMAKE_CURRENT_BINARY_DIR}/qtcreator-project.icns
  )
  install(
    FILES
      ${CMAKE_CURRENT_BINARY_DIR}/Assets.car
      "${QTCREATOR_APP_ICON}"
      ${IDE_ICON_PATH}/macos/qtcreator.icon/Assets/icon.png
      ${CMAKE_CURRENT_BINARY_DIR}/qtcreator-project.icns
    DESTINATION ${IDE_DATA_PATH}
  )
endif()

option(QTC_FORCE_XCB "Enables that Qt Creator forces XCB on Linux if QT_QPA_PLATFORM is not set." OFF)
extend_qtc_executable(qtcreator
  CONDITION QTC_FORCE_XCB
  DEFINES
    QTC_FORCE_XCB
)

if ((NOT WIN32) AND (NOT APPLE))
  # install logo
  foreach(size 16 24 32 48 64 128 256 512)
    install(
      FILES ${IDE_LOGO_PATH}/images/logo/${size}/QtProject-${IDE_ID}.png
      DESTINATION share/icons/hicolor/${size}x${size}/apps
    )
  endforeach()
endif()
