if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "4.0")
    set(CMAKE_POLICY_VERSION_MINIMUM 3.13 CACHE STRING "" FORCE)
endif()

cmake_minimum_required(VERSION 3.13)

if(POLICY CMP0177)
    cmake_policy(SET CMP0177 NEW)
endif()


# The following line used to be in tests/CMakeLists.txt
# Having it there causes rebuilds of all targets on any CMakeLists.txt change under tests/
# It has no effect on how code is compiled or linked.
# It just lets you later do `set_property(TARGET foo PROPERTY FOLDER "bar")`
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

if (WIN32)
    # Detect known CI environments
    set(IS_CI FALSE)
    if(DEFINED ENV{CI})
        set(IS_CI TRUE)
    elseif(DEFINED ENV{GITHUB_ACTIONS})
        set(IS_CI TRUE)
    elseif(DEFINED ENV{GITLAB_CI})
        set(IS_CI TRUE)
    elseif(DEFINED ENV{TF_BUILD})
        set(IS_CI TRUE)
    elseif(DEFINED ENV{BUILD_NUMBER}) # Jenkins
        set(IS_CI TRUE)
    endif()

    # Detect common misconfiguration (Strawberry Perl in PATH before CMake)
    # We use ENV to check the PATH order
    string(REPLACE "\\" "/" ENV_PATH "$ENV{PATH}")
    string(FIND "${ENV_PATH}" "Strawberry/c/bin" STRAWBERRY_POS)
    string(FIND "${ENV_PATH}" "Program Files/CMake/bin" CMAKE_POS)

    if (STRAWBERRY_POS GREATER -1 AND CMAKE_POS GREATER -1 AND STRAWBERRY_POS LESS CMAKE_POS)
        set(_warning_text "
#############################################################
Detected Strawberry Perl's 'c/bin' appearing before CMake in PATH.
This is known to cause CMake to misbehave (e.g., missing modules).
Please adjust your PATH so that:
C:\\Program Files\\CMake\\bin
appears before:
C:\\Strawberry\\c\\bin
You can do this in Environment Variables settings.
#############################################################
        ")

        if(NOT IS_CI)
            message(FATAL_ERROR "${_warning_text}")
        endif()
    endif()
endif ()

if (APPLE)
    # if CMAKE_OSX_DEPLOYMENT_TARGET is not set, set it to 12.0 (the lowest Xcode 27 accepts)
    if (NOT CMAKE_OSX_DEPLOYMENT_TARGET)
        set(CMAKE_OSX_DEPLOYMENT_TARGET "12.0" CACHE STRING "Minimum OS X deployment version" FORCE)
    endif ()
    message(STATUS "CMAKE_OSX_DEPLOYMENT_TARGET: ${CMAKE_OSX_DEPLOYMENT_TARGET}")
endif ()

# Keep MSVC's default /W3 out of CMAKE_<LANG>_FLAGS so it can be applied to our own
# targets only. Silencing a bundled target would otherwise override a warning level,
# which cl reports as D9025 for every file it compiles.
if (POLICY CMP0092)
    cmake_policy(SET CMP0092 NEW)
endif ()

# project() reads this, so set it first.
set(CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_CURRENT_LIST_DIR}/cmake/modules/ClangClShowIncludes.cmake")

project(OrcaSlicer)

# Backward compatibility for old CMake versions
if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_VERSION VERSION_LESS "3.25")
    set(LINUX ON CACHE BOOL "" FORCE)
endif ()

include("version.inc")
include(GNUInstallDirs)
include(CMakeDependentOption)

set(SLIC3R_RESOURCES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/resources")
file(TO_NATIVE_PATH "${SLIC3R_RESOURCES_DIR}" SLIC3R_RESOURCES_DIR_WIN)

if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message(STATUS "No build type selected, default to Release")
  set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type (default Release)" FORCE)
endif()

if (DEFINED BBL_RELEASE_TO_PUBLIC)
    add_compile_definitions("BBL_RELEASE_TO_PUBLIC=${BBL_RELEASE_TO_PUBLIC}")
    if (BBL_RELEASE_TO_PUBLIC)
        add_compile_definitions(WXINSPECTOR_DISABLE)
    endif ()
else ()
    add_compile_definitions("BBL_RELEASE_TO_PUBLIC=$<CONFIG:Release>")
    add_compile_definitions("$<$<CONFIG:Release>:WXINSPECTOR_DISABLE>")
endif ()

if(DEFINED ENV{SLIC3R_STATIC})
    set(SLIC3R_STATIC_INITIAL $ENV{SLIC3R_STATIC})
else()
    set(SLIC3R_STATIC_INITIAL 1)
endif()

option(SLIC3R_STATIC            "Compile OrcaSlicer with static libraries (Boost, TBB)" ${SLIC3R_STATIC_INITIAL})
option(SLIC3R_GUI               "Compile OrcaSlicer with GUI components (OpenGL, wxWidgets)" 1)
option(SLIC3R_OPENVDB           "Compile OrcaSlicer with OpenVDB (used only by SLA hollowing)" 0)
option(SLIC3R_CAD               "Compile OrcaSlicer with the parametric Design/CAD tab (needs OCCT's TKFillet, TKOffset and TKBool)" 1)
option(SLIC3R_FHS               "Assume OrcaSlicer is to be installed in a FHS directory structure" 0)
option(SLIC3R_PROFILE           "Compile OrcaSlicer with an invasive Shiny profiler" 0)
option(SLIC3R_PCH               "Use precompiled headers" 1)
option(SLIC3R_WARNINGS          "Emit compiler warnings for OrcaSlicer sources" 1)
option(SLIC3R_BUNDLED_WARNINGS  "Emit compiler warnings for bundled third-party sources" 0)
option(SLIC3R_MSVC_COMPILE_PARALLEL "Compile on Visual Studio in parallel" 1)
option(SLIC3R_MSVC_PDB          "Generate PDB files on MSVC in Release mode" 1)
option(SLIC3R_RELATIVE_DEBUG_PATHS "Record a relative compilation directory in debug info (clang-cl)" 0)
option(SLIC3R_ASAN              "Enable ASan on Clang and GCC" 0)
option(SLIC3R_GLIBCXX_ASSERTIONS "Enable libstdc++'s lightweight checks (_GLIBCXX_ASSERTIONS)" 0)

# Python stubgen module
option(ORCA_BUILD_PYTHON_STUBGEN_MODULE "Build importable Python module for pybind11-stubgen" ON)
set(ORCA_BUNDLED_UV_EXECUTABLE "" CACHE FILEPATH "Path to a uv executable to bundle for Python package installation. Leave empty to auto-download.")

# Auto-download uv if not provided by the user.
# uv is pinned: the executable ships inside the signed app, so the build must
# be reproducible and the download verified. To upgrade, bump ORCA_UV_VERSION
# and refresh every hash below from the release's .sha256 assets, e.g.:
#   curl -sL https://github.com/astral-sh/uv/releases/download/<ver>/uv-<triple>.tar.gz.sha256
# The Flatpak copy in scripts/flatpak/com.orcaslicer.OrcaSlicer.yml pins the
# same version and hashes and must be bumped with them.
set(ORCA_UV_VERSION "0.13.7")
set(ORCA_UV_SHA256_aarch64-apple-darwin      "81b811b57e4624df7ed3aac148e0359100d9962f3d9dce00aec533dc2e086731")
set(ORCA_UV_SHA256_x86_64-apple-darwin       "20c47e4c1f630b8abaff8c291fa9b9aa300fef92c4fbff95adf198b55287cda7")
set(ORCA_UV_SHA256_aarch64-unknown-linux-gnu "71279f8d3c67241e807b9d8db872d1bf541579fb9f08b7d1eb1682196d1e57dc")
set(ORCA_UV_SHA256_x86_64-unknown-linux-gnu  "c5221ff41b7892bfc223543883600125f6a47fe845093e0f5bdff4f359196d83")
set(ORCA_UV_SHA256_aarch64-pc-windows-msvc   "505d9e65e7692bac3ddf98c66754aa26c5050c409c184af30a881a0a72cc85e3")
set(ORCA_UV_SHA256_x86_64-pc-windows-msvc    "0cdc5b38fe5a8168ed6a25814f5ba43c21d313dd3187314fccb37b7cec46e021")

# Version-scoped cache dir so a version bump invalidates the cached binary.
set(ORCA_UV_DOWNLOAD_DIR "${CMAKE_BINARY_DIR}/.uv/${ORCA_UV_VERSION}")
set(ORCA_UV_BINARY "${ORCA_UV_DOWNLOAD_DIR}/uv")
if(WIN32)
    set(ORCA_UV_BINARY "${ORCA_UV_DOWNLOAD_DIR}/uv.exe")
endif()

# Older configures FORCE-cached the auto-downloaded path; any cache entry
# under .uv/ is that legacy value -- clear it so auto-download re-resolves
# (user-provided paths never live under .uv/).
if(ORCA_BUNDLED_UV_EXECUTABLE MATCHES "/\\.uv/")
    set(ORCA_BUNDLED_UV_EXECUTABLE "" CACHE FILEPATH "Path to the auto-downloaded uv executable" FORCE)
endif()

# A Flatpak build has no network and cannot download below: the orca_deps module
# in scripts/flatpak/com.orcaslicer.OrcaSlicer.yml fetches the same pinned uv and
# installs it into the app resources, where the runtime looks for it.
set(ORCA_UV_FLATPAK_BINARY "${CMAKE_INSTALL_FULL_DATAROOTDIR}/OrcaSlicer/tools/uv/uv")
if(FLATPAK AND NOT ORCA_BUNDLED_UV_EXECUTABLE AND EXISTS "${ORCA_UV_FLATPAK_BINARY}")
    message(STATUS "Using Flatpak-provided uv: ${ORCA_UV_FLATPAK_BINARY}")
    set(ORCA_BUNDLED_UV_EXECUTABLE "${ORCA_UV_FLATPAK_BINARY}")
endif()

if(NOT ORCA_BUNDLED_UV_EXECUTABLE)
    if(NOT EXISTS "${ORCA_UV_BINARY}")
        # Select uv by TARGET arch. On macOS universal builds each leg sets a single
        # CMAKE_OSX_ARCHITECTURES (and the x86_64 leg cross-builds on an arm64 runner),
        # so prefer it over the host CMAKE_SYSTEM_PROCESSOR -- otherwise both legs fetch
        # the same arch and the later universal lipo merge of tools/uv/uv fails.
        set(_orca_uv_proc "${CMAKE_SYSTEM_PROCESSOR}")
        if(APPLE AND CMAKE_OSX_ARCHITECTURES)
            set(_orca_uv_proc "${CMAKE_OSX_ARCHITECTURES}")
        endif()
        # All release archives are tar.gz except the Windows zip.
        set(ORCA_UV_EXT "tar.gz")
        if(_orca_uv_proc MATCHES "x86_64|AMD64|amd64")
            if(WIN32)
                set(ORCA_UV_ARCH "x86_64-pc-windows-msvc")
                set(ORCA_UV_EXT "zip")
            elseif(APPLE)
                set(ORCA_UV_ARCH "x86_64-apple-darwin")
            else()
                set(ORCA_UV_ARCH "x86_64-unknown-linux-gnu")
            endif()
        elseif(_orca_uv_proc MATCHES "aarch64|arm64|ARM64")
            if(WIN32)
                set(ORCA_UV_ARCH "aarch64-pc-windows-msvc")
                set(ORCA_UV_EXT "zip")
            elseif(APPLE)
                set(ORCA_UV_ARCH "aarch64-apple-darwin")
            else()
                set(ORCA_UV_ARCH "aarch64-unknown-linux-gnu")
            endif()
        else()
            message(WARNING "Unsupported architecture for auto-downloading uv: ${_orca_uv_proc}")
        endif()

        if(ORCA_UV_ARCH)
            set(ORCA_UV_URL "https://github.com/astral-sh/uv/releases/download/${ORCA_UV_VERSION}/uv-${ORCA_UV_ARCH}.${ORCA_UV_EXT}")
            set(ORCA_UV_ARCHIVE "${ORCA_UV_DOWNLOAD_DIR}/uv-archive.${ORCA_UV_EXT}")
            file(MAKE_DIRECTORY "${ORCA_UV_DOWNLOAD_DIR}")
            message(STATUS "Downloading uv ${ORCA_UV_VERSION} from ${ORCA_UV_URL} ...")
            file(DOWNLOAD "${ORCA_UV_URL}" "${ORCA_UV_ARCHIVE}" STATUS ORCA_UV_DL_STATUS TLS_VERIFY ON)
            list(GET ORCA_UV_DL_STATUS 0 ORCA_UV_DL_CODE)
            list(GET ORCA_UV_DL_STATUS 1 ORCA_UV_DL_MSG)
            if(NOT ORCA_UV_DL_CODE EQUAL 0)
                file(REMOVE "${ORCA_UV_ARCHIVE}")
                message(FATAL_ERROR "Failed to download uv: ${ORCA_UV_DL_MSG}")
            else()
                file(SHA256 "${ORCA_UV_ARCHIVE}" _orca_uv_actual_sha256)
                if(NOT _orca_uv_actual_sha256 STREQUAL "${ORCA_UV_SHA256_${ORCA_UV_ARCH}}")
                    file(REMOVE "${ORCA_UV_ARCHIVE}")
                    message(FATAL_ERROR
                        "uv archive checksum mismatch for ${ORCA_UV_ARCH} ${ORCA_UV_VERSION}:\n"
                        "  expected ${ORCA_UV_SHA256_${ORCA_UV_ARCH}}\n"
                        "  actual   ${_orca_uv_actual_sha256}\n"
                        "Possible tampering or a stale pin; refusing to bundle.")
                endif()
                message(STATUS "Extracting uv ...")
                file(ARCHIVE_EXTRACT INPUT "${ORCA_UV_ARCHIVE}" DESTINATION "${ORCA_UV_DOWNLOAD_DIR}")
                file(REMOVE "${ORCA_UV_ARCHIVE}")
                # Pinned archives have a fixed layout: the tarballs hold
                # uv-<triple>/uv, the Windows zip holds uv.exe at the root
                # (already at ORCA_UV_BINARY). A layout change implies a new
                # archive, hence a hash bump -- and RENAME fails loudly.
                if(NOT WIN32)
                    file(RENAME "${ORCA_UV_DOWNLOAD_DIR}/uv-${ORCA_UV_ARCH}/uv" "${ORCA_UV_BINARY}")
                    file(REMOVE_RECURSE "${ORCA_UV_DOWNLOAD_DIR}/uv-${ORCA_UV_ARCH}")
                endif()
            endif()
        endif()
    endif()

    if(EXISTS "${ORCA_UV_BINARY}")
        # Plain set (shadows the empty cache entry for this configure run):
        # the version-scoped path re-derives every configure, so a version
        # bump takes effect in warm build dirs without cache surgery.
        set(ORCA_BUNDLED_UV_EXECUTABLE "${ORCA_UV_BINARY}")
    endif()
endif()

# Resolve ORCA_BUNDLED_UV_EXECUTABLE -> ORCA_BUNDLED_UV_EXECUTABLE_CONFIG / ORCA_BUNDLED_UV_FILENAME
# Must run before add_subdirectory(src) so target_compile_definitions sees it.
if(ORCA_BUNDLED_UV_EXECUTABLE)
    if(EXISTS "${ORCA_BUNDLED_UV_EXECUTABLE}")
        file(TO_CMAKE_PATH "${ORCA_BUNDLED_UV_EXECUTABLE}" ORCA_BUNDLED_UV_EXECUTABLE_CONFIG)
        get_filename_component(ORCA_BUNDLED_UV_FILENAME "${ORCA_BUNDLED_UV_EXECUTABLE}" NAME)
    else()
        message(WARNING "ORCA_BUNDLED_UV_EXECUTABLE does not exist: ${ORCA_BUNDLED_UV_EXECUTABLE}")
        set(ORCA_BUNDLED_UV_EXECUTABLE "")
        set(ORCA_BUNDLED_UV_EXECUTABLE_CONFIG "")
        set(ORCA_BUNDLED_UV_FILENAME "")
    endif()
else()
    set(ORCA_BUNDLED_UV_EXECUTABLE_CONFIG "")
    set(ORCA_BUNDLED_UV_FILENAME "")
endif()

# If SLIC3R_FHS is 1 -> SLIC3R_DESKTOP_INTEGRATION is always 0, othrewise variable.
CMAKE_DEPENDENT_OPTION(SLIC3R_DESKTOP_INTEGRATION "Allow performing desktop integration during runtime" 1 "NOT SLIC3R_FHS" 0)

set(OPENVDB_FIND_MODULE_PATH "" CACHE PATH "Path to OpenVDB installation's find modules.")

set(SLIC3R_GTK "3" CACHE STRING "GTK version to use with wxWidgets on Linux")

set(IS_CROSS_COMPILE FALSE)

option (COLORED_OUTPUT "Always produce ANSI-colored output (GNU/Clang only)." OFF)
if (${COLORED_OUTPUT})
    if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
        add_compile_options (-fdiagnostics-color=always)
    elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
        add_compile_options (-fcolor-diagnostics)
    endif ()
endif ()

if (APPLE)
    list(LENGTH CMAKE_OSX_ARCHITECTURES _arch_len)
    if (_arch_len GREATER 1)
        message(FATAL_ERROR "OrcaSlicer only supports building for one architecture at a time. Please make sure only one architecture is specified in CMAKE_OSX_ARCHITECTURES")
    endif ()
    set(CMAKE_FIND_FRAMEWORK LAST)
    set(CMAKE_FIND_APPBUNDLE LAST)
    list(FIND CMAKE_OSX_ARCHITECTURES ${CMAKE_SYSTEM_PROCESSOR} _arch_idx)
    if (CMAKE_OSX_ARCHITECTURES AND _arch_idx LESS 0)
        set(IS_CROSS_COMPILE TRUE)
    endif ()
    if (CMAKE_MACOSX_BUNDLE)
        set(CMAKE_INSTALL_RPATH @executable_path/../Frameworks)
    endif()
    SET(CMAKE_XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "com.orcaslicer.OrcaSlicer")

    # The macOS CI jobs build with Ninja (build_release_macos.sh -x), so the Xcode generator
    # is not covered. Xcode adds -Wshorten-64-to-32 by default ("Implicit Conversion to 32 Bit
    # Type"); Ninja/-Wall does not, and under -Werror it fails Xcode builds on code CI accepts.
    set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_64_TO_32_BIT_CONVERSION "NO")

    # The Xcode generator ignores CMAKE_<LANG>_COMPILER_LAUNCHER, so hand the launcher to
    # Xcode's own Compiler Launcher setting, which wraps every clang compile (C, C++,
    # Objective-C++ and the precompiled header) with one program: the C++ launcher wins when
    # both are set. The setting takes a single program path, not a CMake launcher list.
    # Xcode versions without it ignore it and build uncached.
    if (CMAKE_GENERATOR STREQUAL "Xcode")
        if (CMAKE_CXX_COMPILER_LAUNCHER)
            set(CMAKE_XCODE_ATTRIBUTE_C_COMPILER_LAUNCHER "${CMAKE_CXX_COMPILER_LAUNCHER}")
        elseif (CMAKE_C_COMPILER_LAUNCHER)
            set(CMAKE_XCODE_ATTRIBUTE_C_COMPILER_LAUNCHER "${CMAKE_C_COMPILER_LAUNCHER}")
        endif ()
    endif ()

    message(STATUS "Orca: IS_CROSS_COMPILE: ${IS_CROSS_COMPILE}")
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
    set(CMAKE_INSTALL_RPATH "$ORIGIN")
endif ()

# Proposal for C++ unit tests and sandboxes
option(SLIC3R_BUILD_SANDBOXES   "Build development sandboxes" OFF)
option(BUILD_TESTS       "Build unit tests" OFF)
option(ORCA_TOOLS       "Build Orca tools" OFF)

if (FLATPAK)
    set(SLIC3R_FHS ON CACHE BOOL "" FORCE)
    set(SLIC3R_DESKTOP_INTEGRATION OFF CACHE BOOL "" FORCE)
endif ()

if (IS_CROSS_COMPILE)
    message("Detected cross compilation setup. Tests and encoding checks will be forcedly disabled!")
    set(BUILD_TESTS OFF CACHE BOOL "" FORCE)
endif ()

# With a precompiled header, clang-cl can omit an exception type's copy constructor from its throw info,
# so copies of the exception share and double-free their data: https://github.com/llvm/llvm-project/issues/53486
if (MSVC AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    message(STATUS "clang-cl: building without the precompiled header (llvm/llvm-project#53486)")
    set_property(CACHE SLIC3R_PCH PROPERTY HELPSTRING "Use precompiled headers (ignored with clang-cl, llvm/llvm-project#53486)")
    set(SLIC3R_PCH OFF)
endif ()

# Print out the SLIC3R_* cache options
get_cmake_property(_cache_vars CACHE_VARIABLES)
list (SORT _cache_vars)
foreach (_cache_var ${_cache_vars})
    if("${_cache_var}" MATCHES "^SLIC3R_")
        message(STATUS "${_cache_var}: ${${_cache_var}}")
    endif ()
endforeach()

if (SLIC3R_GUI)
    add_definitions(-DSLIC3R_GUI)
endif ()

if (SLIC3R_CAD)
    add_definitions(-DSLIC3R_CAD)
endif ()

if(SLIC3R_DESKTOP_INTEGRATION)
    add_definitions(-DSLIC3R_DESKTOP_INTEGRATION)
endif ()

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    message(STATUS "Automatically setting CMAKE_INSTALL_PREFIX")
    set_property(CACHE CMAKE_INSTALL_PREFIX PROPERTY VALUE "${CMAKE_BINARY_DIR}/OrcaSlicer")
endif()

message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")

if (MSVC AND CMAKE_CXX_COMPILER_ID STREQUAL Clang)
    set(IS_CLANG_CL TRUE)

    # clang-cl can interpret SYSTEM header paths if -imsvc is used
    set(CMAKE_INCLUDE_SYSTEM_FLAG_CXX "-imsvc")

    # Thin archives reference the objects instead of copying them; only lld-link reads them.
    if (NOT CMAKE_GENERATOR MATCHES "Visual Studio" AND CMAKE_AR MATCHES "llvm-lib" AND CMAKE_LINKER MATCHES "lld-link")
        string(APPEND CMAKE_STATIC_LINKER_FLAGS " /llvmlibthin")
    endif ()
else ()
    set(IS_CLANG_CL FALSE)
endif ()

if (MSVC)
    # CMP0092 only applies when the cache is created; an existing tree keeps its /W3,
    # which a silenced bundled target would then override (D9025, once per file).
    string(REGEX REPLACE "/W[0-4]" "" CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}")
    string(REGEX REPLACE "/W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")

    # /MP only matters for the VS generators, where CMake turns it into the
    # MultiProcessorCompilation property. Ninja parallelises on its own, and
    # clang-cl warns "argument unused" if the flag reaches it.
    if (SLIC3R_MSVC_COMPILE_PARALLEL AND CMAKE_GENERATOR MATCHES "Visual Studio")
           add_compile_options(/MP)
    endif ()

    # Parse lambdas the way the standard says, as clang and GCC already do. Without it
    # MSVC keeps its legacy lambda processor under /std:c++17 and rejects reading a
    # constexpr constant inside a lambda that does not capture it (C3493), which no
    # other compiler requires. Implied by /std:c++20 and /permissive-, so it is only
    # needed while we are on C++17. clang-cl is conforming already and does not take
    # the flag. Requires VS2019 16.8 or newer.
    if (NOT IS_CLANG_CL)
        # cl.exe only warns (D9002) about an unknown /Zc: option, so without this the
        # flag would be dropped and the first lambda reading a constexpr constant would
        # fail with C3493 far from the cause.
        if (MSVC_VERSION LESS 1928)
            message(FATAL_ERROR "Visual Studio 2019 16.8 (MSVC 19.28) or newer is required; detected MSVC ${MSVC_VERSION}.")
        endif ()
        add_compile_options(/Zc:lambda)
    endif ()
    # /bigobj (Increase Number of Sections in .Obj file)
    add_compile_options(-bigobj)
    # error C3859: virtual memory range for PCH exceeded; please recompile with a command line option of '-Zm90' or greater
    # Generate symbols at every build target, even for the release.
	# -Zm520 fixes error C3859 but forces the compiler to pre-allocate that memory for every translation unit regardless
	# combining /Zi with /FS frees up a significant amount of memory pressure across all parallel compile jobs and makes /MP faster overall.
    if (SLIC3R_MSVC_PDB)
        add_compile_options(/Zi /FS)
    endif ()
    # Disable STL4007: Many result_type typedefs and all argument_type, first_argument_type, and second_argument_type typedefs are deprecated in C++17.
    #FIXME Remove this line after eigen library adapts to the new C++17 adaptor rules.
    add_compile_options(-D_SILENCE_CXX17_ADAPTOR_TYPEDEFS_DEPRECATION_WARNING)
    # Disable warnings on conversion from unsigned to signed (possible loss of data)
    # C4244: 'conversion' conversion from 'type1' to 'type2', possible loss of data. An integer type is converted to a smaller integer type.
    # C4267: The compiler detected a conversion from size_t to a smaller type.
    add_compile_options(/wd4244 /wd4267)
    # Disable warnings on comparison of unsigned and signed
    # C4018: signed/unsigned mismatch
    add_compile_options(/wd4018)
    # Prevent linker restart when TBB (or other deps) built with /GL are linked
	# Make your full (clean) build slower because it enables link-time code generation across all translation units.
	# helps incremental builds by preventing the double-link restart from TBB
    set(CMAKE_EXE_LINKER_FLAGS    "${CMAKE_EXE_LINKER_FLAGS} /LTCG")
    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LTCG")
endif ()

# Without this every object names its build directory and two worktrees never
# share cache entries. The linker still writes absolute paths into the PDB.
if (SLIC3R_RELATIVE_DEBUG_PATHS)
    if (IS_CLANG_CL)
        add_compile_options(-ffile-compilation-dir=.)
    else ()
        message(WARNING "SLIC3R_RELATIVE_DEBUG_PATHS is only implemented for clang-cl")
    endif ()
endif ()

if (${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang" AND ${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER 15)
    add_compile_definitions(BOOST_NO_CXX98_FUNCTION_BASE _HAS_AUTO_PTR_ETC=0)
endif()

if (MINGW)
    add_compile_options(-Wa,-mbig-obj)
endif ()

if (NOT MSVC)
    # ARMs (Raspberry PI) use an unsigned char by default. Let's make it consistent for OrcaSlicer on all platforms.
    add_compile_options(-fsigned-char)
endif ()

if ("${DEP_BUILD_DIR}" STREQUAL "")
    get_filename_component(BIN_DIR_NAME ${CMAKE_BINARY_DIR} NAME)
    if (APPLE AND BIN_DIR_NAME STREQUAL "${CMAKE_OSX_ARCHITECTURES}")
        file(RELATIVE_PATH BIN_DIR_NAME ${CMAKE_BINARY_DIR}/../.. ${CMAKE_BINARY_DIR})
    endif ()
    set(DEP_BUILD_DIR "${CMAKE_SOURCE_DIR}/deps/${BIN_DIR_NAME}" CACHE PATH "Path to dependencies build directory" FORCE)
    message(STATUS "DEP_BUILD_DIR: ${DEP_BUILD_DIR} (generated automatically and saved to cache)")
    set(AUTOGENERATED_DEP_BUILD_DIR ${DEP_BUILD_DIR} CACHE PATH "Provides the last autogenerated DEP_BUILD_DIR" FORCE)
else ()
    message(STATUS "DEP_BUILD_DIR: ${DEP_BUILD_DIR} (from cache or command line)")
endif ()

if ("${CMAKE_PREFIX_PATH}" STREQUAL "" OR "${CMAKE_PREFIX_PATH}" STREQUAL "${AUTOGENERATED_PREFIX_PATH}")
    if (DEFINED AUTOGENERATED_DEP_BUILD_DIR AND NOT "${DEP_BUILD_DIR}" STREQUAL "${AUTOGENERATED_DEP_BUILD_DIR}")
        message(STATUS "CMAKE_PREFIX_PATH is being re-generated due to DEP_BUILD_DIR being manually updated")
        set(REGEN_DESTDIR TRUE)
        unset(AUTOGENERATED_DEP_BUILD_DIR CACHE)
    endif ()
else ()
    unset(AUTOGENERATED_PREFIX_PATH CACHE)
endif ()

# Display and check CMAKE_PREFIX_PATH
if ("${CMAKE_PREFIX_PATH}" STREQUAL "" OR REGEN_DESTDIR)
    set(CMAKE_PREFIX_PATH "${DEP_BUILD_DIR}/OrcaSlicer_dep/usr/local" CACHE PATH "Path to dependencies install directory" FORCE)
    message(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH} (generated automatically and saved to cache)")
    set(AUTOGENERATED_PREFIX_PATH ${CMAKE_PREFIX_PATH} CACHE STRING "Provides the last autogenerated CMAKE_PREFIX_PATH" FORCE)
    unset(REGEN_DESTDIR CACHE)
else ()
    message(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH} (from cache or command line)")
endif ()
set(PREFIX_PATH_CHECK ${CMAKE_PREFIX_PATH})

# the CMAKE_PREFIX_PATH environment variable is separate from the CMAKE_PREFIX_PATH cache variable and provides additional paths to search for libraries.
if (NOT "$ENV{CMAKE_PREFIX_PATH}" STREQUAL "")
    message(STATUS "CMAKE_PREFIX_PATH: $ENV{CMAKE_PREFIX_PATH} (from environment)")
    list(APPEND PREFIX_PATH_CHECK $ENV{CMAKE_PREFIX_PATH})
endif ()

# Check all directories in CMAKE_PREFIX_PATH variables
foreach (DIR ${PREFIX_PATH_CHECK})
    if (NOT EXISTS "${DIR}")
        message(WARNING "CMAKE_PREFIX_PATH element doesn't exist: ${DIR}")
    endif ()
endforeach ()

if (APPLE)
    set(CMAKE_MACOSX_RPATH ON CACHE BOOL "")
    set(CMAKE_MACOSX_BUNDLE ON CACHE BOOL "")
endif ()

if (APPLE AND CMAKE_MACOSX_RPATH AND "${CMAKE_INSTALL_RPATH}" STREQUAL "")
    set(CMAKE_INSTALL_RPATH ${CMAKE_PREFIX_PATH})
endif ()

# Add our own cmake module path.
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules/)
message(STATUS "PROJECT_SOURCE_DIR: ${PROJECT_SOURCE_DIR}")
message(STATUS "CMAKE_MODULE_PATH: ${CMAKE_MODULE_PATH}")

enable_testing ()

# Enable C++17 language standard.
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT WIN32)
    # Add DEBUG flags to debug builds.
    add_compile_definitions("$<IF:$<CONFIG:Debug>,DEBUG,NDEBUG>")
endif()

# To be able to link libslic3r with the Perl XS module.
# Once we get rid of Perl and libslic3r is linked statically, we can get rid of -fPIC
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

if (APPLE)
    message("OS X SDK Path: ${CMAKE_OSX_SYSROOT}")
    if (CMAKE_OSX_DEPLOYMENT_TARGET)
        message("OS X Deployment Target: ${CMAKE_OSX_DEPLOYMENT_TARGET}")
    else ()
        message("OS X Deployment Target: (default)")
    endif ()
endif ()

if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
    find_package(PkgConfig REQUIRED)

    # Boost on Raspberry-Pi does not link to pthreads.
    set(THREADS_PREFER_PTHREAD_FLAG ON)
    find_package(Threads REQUIRED)

    find_package(DBus REQUIRED)
    include_directories(SYSTEM ${DBUS_INCLUDE_DIRS})
endif()

if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUXX)
    # Adding -fext-numeric-literals to enable GCC extensions on definitions of quad float literals, which are required by Boost.
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fext-numeric-literals" )
endif()

if ((NOT MSVC OR IS_CLANG_CL) AND ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
    if (IS_CLANG_CL)
        # clang-cl reads -Wall as MSVC /Wall, which clang maps to -Weverything. /W4 is
        # its -Wall -Wextra and, unlike /clang:-Wall, is ordered with the -Wno-* below
        # instead of after them. The -Wextra-only warnings are dropped again so the set
        # matches what -Wall gives the GNU/Clang builds.
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4" )
        add_compile_options(-Wno-unused-parameter -Wno-ignored-qualifiers -Wno-missing-field-initializers)
    elseif (NOT MINGW)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall" )
    endif ()

    # Every warning is an error unless it appears in one of the two lists below.
    #   disabled - never wanted. Off everywhere, so it never warns or errors.
    #   demoted  - wanted, not cleared yet. Still warns, does not error.

    # Disabled.
    set(warnings_disabled
        reorder                  # members initialised in an order we chose
        sign-compare             # signed/unsigned comparisons throughout
        misleading-indentation   # false positives on mixed tabs and spaces
        switch                   # unhandled enum value in a switch
        unused-function          # commented-out or conditionally compiled code
        unused-variable          # commented-out or conditionally compiled code
        unused-but-set-variable  # commented-out or conditionally compiled code
        unused-label             # commented-out or conditionally compiled code
        unused-local-typedefs    # commented-out or conditionally compiled code
    )
    if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        list(APPEND warnings_disabled deprecated-declarations)  # legacy OpenGL calls
    endif ()
    if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 6.0)
        list(APPEND warnings_disabled ignored-attributes)       # from Eigen headers marked SYSTEM
    endif ()
    if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
        list(APPEND warnings_disabled unknown-pragmas)          # igl pragmas, GCC bug 66943
    endif ()
    foreach (w IN LISTS warnings_disabled)
        add_compile_options(-Wno-${w})
    endforeach ()

    # GCC is not built in CI, so don't throw errors CI won't catch.
    if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
        add_compile_options(-Werror=return-type)
    else ()
        # Turn everything else into an error. Dependency headers are exempt because the
        # SYSTEM include flag (-imsvc on clang-cl, -isystem elsewhere) keeps their
        # diagnostics out.
        add_compile_options(-Werror)
    endif ()

    # Demoted. Remove a name once its category is cleared on every compiler.
    set(warnings_demoted)
    if (APPLE)
        list(APPEND warnings_demoted
            # MacDarkMode.mm makes two calls to AppKit's private titlebarViewController
            # and one to a wxWidgets category on NSTableColumn whose header is not
            # imported. Clearing it means declaring the private selectors ourselves, which
            # needs a macOS build to verify.
            objc-method-access
        )
    endif ()
    if (WIN32 AND CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64")
        list(APPEND warnings_demoted
            # About two dozen GetProcAddress casts, most in the vendored dark_mode.hpp,
            # retype FARPROC to a real signature. The __stdcall typedefs are identical to
            # FARPROC on x64, so only arm64 reports them. Clearing them is a separate
            # sweep.
            cast-function-type-mismatch
        )
    endif ()
    if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        list(APPEND warnings_demoted
            # enum-constexpr-conversion is a Clang warning that defaults to an error,
            # present through clang 20 and gone in clang 21.
            enum-constexpr-conversion
        )
    endif ()

    # The list mixes names not every compiler has, so add each exception only where the
    # compiler knows the warning. Probe with the positive -W<name>, which an unknown
    # warning fails on both compilers (GCC errors, Clang reports unknown-warning-option).
    # An option that takes a =N argument rejects the bare -W<name>, so fall back to
    # -W<name>=1 and demote with the trailing =.
    include(CheckCXXCompilerFlag)
    foreach (category IN LISTS warnings_demoted)
        string(MAKE_C_IDENTIFIER "ORCA_HAS_W_${category}" _orca_has_w)
        check_cxx_compiler_flag("-W${category}" ${_orca_has_w})
        if (${_orca_has_w})
            add_compile_options(-Wno-error=${category})
        else ()
            check_cxx_compiler_flag("-W${category}=1" ${_orca_has_w}_arg)
            if (${${_orca_has_w}_arg})
                add_compile_options(-Wno-error=${category}=)
            endif ()
        endif ()
    endforeach ()

    # Compress the debug info with zstd to save space in Flatpak CI builds
    if(FLATPAK)
        if(("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 13.0) OR
           ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 15.0))
            add_compile_options(-gz=zstd)
        endif()
    endif()

endif()

if (SLIC3R_GLIBCXX_ASSERTIONS)
    # libstdc++'s lightweight checks: operator[] past the end, front() or
    # back() on an empty container and the like abort with a message instead
    # of reading out of range. No class changes layout, so prebuilt deps link
    # as they are. A no-op with libc++ and the MSVC STL.
    add_compile_definitions(_GLIBCXX_ASSERTIONS)
endif ()

if (SLIC3R_ASAN)
    # ASAN should be available on MSVC starting with Visual Studio 2019 16.9
    # https://devblogs.microsoft.com/cppblog/address-sanitizer-for-msvc-now-generally-available/
    add_compile_options(-fsanitize=address)

    if (NOT MSVC)
        add_compile_options(-fno-omit-frame-pointer)
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
        set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
        set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -fsanitize=address")
    else()
        add_compile_definitions(_DISABLE_STRING_ANNOTATION=1 _DISABLE_VECTOR_ANNOTATION=1)
    endif ()

    if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan")
    endif ()
endif ()

if (APPLE)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=partial-availability -Werror=unguarded-availability -Werror=unguarded-availability-new")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=partial-availability -Werror=unguarded-availability -Werror=unguarded-availability-new")
endif ()

if(MSVC)
# Ignore truncating casts in initializers & constructors
# https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4305
add_compile_options(/wd4305)
endif()

# Where all the bundled libraries reside?
set(LIBDIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(LIBDIR_BIN ${CMAKE_CURRENT_BINARY_DIR}/src)
message(STATUS "LIBDIR: ${LIBDIR}")
message(STATUS "LIBDIR_BIN: ${LIBDIR_BIN}")

# For the bundled boost libraries (boost::nowide)
include_directories(SYSTEM ${LIBDIR})
# For generated header files
include_directories(SYSTEM ${LIBDIR_BIN}/dev-utils/platform)
# For ligigl
include_directories(SYSTEM ${LIBDIR}/libigl)

if(WIN32)
    add_definitions(-D_USE_MATH_DEFINES -D_WIN32 -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS)
    if(MSVC)
        # BOOST_ALL_NO_LIB: Avoid the automatic linking of Boost libraries on Windows. Rather rely on explicit linking.
        add_definitions(-DBOOST_ALL_NO_LIB -DBOOST_USE_WINAPI_VERSION=0x602 -DBOOST_SYSTEM_USE_UTF8 )
        # Force the source code encoding to UTF-8. See OrcaSlicer GH pull request #5583
        add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
        add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
    endif(MSVC)
endif(WIN32)

add_definitions(-DwxUSE_UNICODE -D_UNICODE -DUNICODE -DWXINTL_NO_GETTEXT_MACRO)

# Disable unsafe implicit wxString to const char* / std::string and vice versa. This implicit conversion breaks the UTF-8 encoding quite often.
add_definitions(-DwxNO_UNSAFE_WXSTRING_CONV)

if (SLIC3R_PROFILE)
    message("OrcaSlicer will be built with a Shiny invasive profiler")
    add_definitions(-DSLIC3R_PROFILE)
endif ()

# Disable optimization for RelWithDebInfo
if(CMAKE_C_FLAGS_RELWITHDEBINFO MATCHES "/O2")
    string(REGEX REPLACE "/O2" "/Od" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
endif()

if(CMAKE_CXX_FLAGS_RELWITHDEBINFO MATCHES "/O2")
    string(REGEX REPLACE "/O2" "/Od" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
endif()

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
    if(CMAKE_C_FLAGS_RELWITHDEBINFO MATCHES "-O2")
        string(REGEX REPLACE "-O2" "-O0" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
    endif()

    if(CMAKE_CXX_FLAGS_RELWITHDEBINFO MATCHES "-O2")
        string(REGEX REPLACE "-O2" "-O0" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
    endif()
endif()

if(MSVC)
    if(CMAKE_C_FLAGS_RELWITHDEBINFO MATCHES "/Ob1")
        string(REGEX REPLACE "/Ob1" "/Ob0" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
    endif()
    if(CMAKE_CXX_FLAGS_RELWITHDEBINFO MATCHES "/Ob1")
        string(REGEX REPLACE "/Ob1" "/Ob0" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
    endif()

    set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /RTC1")
    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /RTC1")
endif()

list(REMOVE_DUPLICATES CMAKE_C_FLAGS_RELWITHDEBINFO)
list(REMOVE_DUPLICATES CMAKE_CXX_FLAGS_RELWITHDEBINFO)

# Find and configure boost
if(SLIC3R_STATIC)
    # Use static boost libraries.
    set(Boost_USE_STATIC_LIBS ON)
    # Use boost libraries linked statically to the C++ runtime.
    # set(Boost_USE_STATIC_RUNTIME ON)
else()
    add_definitions(-DBOOST_LOG_DYN_LINK)
endif()
#set(Boost_DEBUG ON)
# set(Boost_COMPILER "-mgw81")
# boost::process was introduced first in version 1.64.0,
# boost::beast::detail::base64 was introduced first in version 1.66.0
if(POLICY CMP0167)
  cmake_policy(SET CMP0167 NEW)
endif()
set(Boost_NO_SYSTEM_PATHS TRUE)
find_package(Boost 1.83.0 REQUIRED COMPONENTS system filesystem thread log log_setup locale regex chrono atomic date_time iostreams program_options nowide)

# Find and configure Eigen3
find_package(Eigen3 5.0.1 REQUIRED)

add_library(boost_libs INTERFACE)
add_library(boost_headeronly INTERFACE)

if (APPLE)
    # BOOST_ASIO_DISABLE_KQUEUE : prevents a Boost ASIO bug on OS X: https://svn.boost.org/trac/boost/ticket/5339
    target_compile_definitions(boost_headeronly INTERFACE BOOST_ASIO_DISABLE_KQUEUE)
endif()

if(NOT SLIC3R_STATIC)
    target_compile_definitions(boost_headeronly INTERFACE BOOST_LOG_DYN_LINK)
endif()

function(slic3r_remap_configs targets from_Cfg to_Cfg)
    if(MSVC)
        string(TOUPPER ${from_Cfg} from_CFG)

        foreach(tgt ${targets})
            if(TARGET ${tgt})
                set_target_properties(${tgt} PROPERTIES MAP_IMPORTED_CONFIG_${from_CFG} ${to_Cfg})
            endif()
        endforeach()
    endif()
endfunction()

target_include_directories(boost_headeronly SYSTEM INTERFACE ${Boost_INCLUDE_DIRS})
target_link_libraries(boost_libs INTERFACE boost_headeronly ${Boost_LIBRARIES})

# Find and configure intel-tbb
if(SLIC3R_STATIC)
    set(TBB_STATIC 1)
endif()
set(TBB_DEBUG 1)
if ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo" OR MSVC)
    set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO RelWithDebInfo Release "")
endif()
find_package(TBB REQUIRED)
# include_directories(SYSTEM ${TBB_INCLUDE_DIRS})
# add_definitions(${TBB_DEFINITIONS})
# if(MSVC)
#     # Suppress implicit linking of the TBB libraries by the Visual Studio compiler.
#     add_definitions(-D__TBB_NO_IMPLICIT_LINKAGE)
# endif()
# The Intel TBB library will use the std::exception_ptr feature of C++11.
# add_definitions(-DTBB_USE_CAPTURED_EXCEPTION=0)

find_package(OpenSSL REQUIRED)
find_package(CURL REQUIRED)

if (SLIC3R_GUI)
    # LibDataChannel's installed export references its bundled dependencies,
    # but does not install their CMake targets. Recreate those targets from
    # the same dependency prefix before loading the LibDataChannel config.
    if (NOT TARGET Usrsctp::usrsctp)
        find_library(_ORCA_USRSCTP_LIBRARY NAMES usrsctp
            PATHS "${CMAKE_PREFIX_PATH}/lib" NO_DEFAULT_PATH)
        if (_ORCA_USRSCTP_LIBRARY)
            add_library(Usrsctp::usrsctp UNKNOWN IMPORTED GLOBAL)
            set_target_properties(Usrsctp::usrsctp PROPERTIES
                IMPORTED_LOCATION "${_ORCA_USRSCTP_LIBRARY}"
                IMPORTED_LINK_INTERFACE_LANGUAGES C
                INTERFACE_LINK_LIBRARIES "Threads::Threads")
        endif()
    endif()

    if (NOT TARGET LibJuice::LibJuice)
        find_library(_ORCA_LIBJUICE_LIBRARY NAMES juice
            PATHS "${CMAKE_PREFIX_PATH}/lib" NO_DEFAULT_PATH)
        if (_ORCA_LIBJUICE_LIBRARY)
            add_library(LibJuice::LibJuice UNKNOWN IMPORTED GLOBAL)
            set_target_properties(LibJuice::LibJuice PROPERTIES
                IMPORTED_LOCATION "${_ORCA_LIBJUICE_LIBRARY}"
                IMPORTED_LINK_INTERFACE_LANGUAGES C
                INTERFACE_LINK_LIBRARIES "Threads::Threads")
        endif()
    endif()

    find_package(LibDataChannel CONFIG REQUIRED)
endif()


add_library(libcurl INTERFACE)
target_link_libraries(libcurl INTERFACE CURL::libcurl)

find_package(ZLIB REQUIRED)
target_link_libraries(libcurl INTERFACE ZLIB::ZLIB)

# Fixing curl's cmake config script bugs
if (NOT WIN32)
    # Required by libcurl
    #find_package(ZLIB REQUIRED)
    #target_link_libraries(libcurl INTERFACE ZLIB::ZLIB)
    #find_package(Libssh2 REQUIRED)
    #target_link_libraries(libcurl INTERFACE Libssh2::libssh2)
else()
    target_link_libraries(libcurl INTERFACE crypt32)
endif()
if (APPLE)
    # libcurl's macOS start-up calls SCDynamicStoreCopyProxies. curl's own
    # CMake package links the framework, but our FindCURL module builds
    # CURL::libcurl from the static archive alone.
    target_link_libraries(libcurl INTERFACE "-framework SystemConfiguration")
endif()

if (SLIC3R_STATIC AND NOT SLIC3R_STATIC_EXCLUDE_CURL)
    if (NOT APPLE)
        # libcurl is always linked dynamically to the system libcurl on OSX.
        # On other systems, libcurl is linked statically if SLIC3R_STATIC is set.
        target_compile_definitions(libcurl INTERFACE CURL_STATICLIB)
    endif()
    if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
        # As of now, our build system produces a statically linked libcurl,
        # which links the OpenSSL library dynamically.
        find_package(OpenSSL REQUIRED)
        message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
        message("OpenSSL libraries: ${OPENSSL_LIBRARIES}")
        target_include_directories(libcurl SYSTEM INTERFACE ${OPENSSL_INCLUDE_DIR})
        target_link_libraries(libcurl INTERFACE ${OPENSSL_LIBRARIES})
    endif()
endif()

## OPTIONAL packages

# Linux links the system libexpat when it is the pinned version; every other case
# builds that version in the deps project, so all platforms link the same parser.
# Requiring 2.10.0 rather than the 2.4.0 security floor keeps distro packages that
# are missing later fixes from slipping in.
find_package(EXPAT 2.10.0 REQUIRED)

find_package(PNG REQUIRED)

set(OpenGL_GL_PREFERENCE "LEGACY")
find_package(OpenGL REQUIRED)

if(APPLE AND CMAKE_VERSION VERSION_GREATER_EQUAL "4.0")
    set(OPENGL_LIBRARIES "-framework OpenGL" CACHE STRING "OpenGL framework" FORCE)
endif()

find_package(glfw3 REQUIRED)

# Find the Cereal serialization library
find_package(cereal REQUIRED)
if (NOT TARGET cereal::cereal)
    set_target_properties(cereal PROPERTIES IMPORTED_GLOBAL TRUE)
    add_library(cereal::cereal ALIAS cereal)
else ()
    set_target_properties(cereal::cereal PROPERTIES IMPORTED_GLOBAL TRUE)
endif ()

# l10n
set(L10N_DIR "${SLIC3R_RESOURCES_DIR}/i18n")
set(BBL_L18N_DIR "${CMAKE_CURRENT_SOURCE_DIR}/localization/i18n")
add_custom_target(gettext_make_pot
    COMMAND xgettext --keyword=L --keyword=_L --keyword=_u8L --keyword=L_CONTEXT:1,2c --keyword=_L_CONTEXT:1,2c --keyword=_u8L_CONTEXT:1,2c --keyword=_L_PLURAL:1,2 --add-comments=TRN --from-code=UTF-8 --no-location --debug --boost
        -f "${BBL_L18N_DIR}/list.txt"
        -o "${BBL_L18N_DIR}/OrcaSlicer.pot"
    COMMAND hintsToPot ${SLIC3R_RESOURCES_DIR} ${BBL_L18N_DIR}
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
    COMMENT "Generate pot file from strings in the source tree"
)
add_custom_target(gettext_merge_po_with_pot
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
    COMMENT "Merge localization po with new generated pot file"
)
file(GLOB BBL_L10N_PO_FILES "${BBL_L18N_DIR}/*/OrcaSlicer*.po")
foreach(po_file ${BBL_L10N_PO_FILES})
    GET_FILENAME_COMPONENT(po_dir "${po_file}" DIRECTORY)
    SET(po_new_file "${po_dir}/OrcaSlicer_.po")
    add_custom_command(
        TARGET gettext_merge_po_with_pot PRE_BUILD
        COMMAND msgmerge -N -o ${po_file} ${po_file} "${BBL_L18N_DIR}/OrcaSlicer.pot"
    )
endforeach()
add_custom_target(gettext_po_to_mo
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
    COMMENT "Generate localization po files (binary) from mo files (texts)"
)
file(GLOB L10N_PO_FILES "${BBL_L18N_DIR}/*/OrcaSlicer*.po")
foreach(po_file ${BBL_L10N_PO_FILES})
    GET_FILENAME_COMPONENT(SECOND_FOLDER_ABSOLUTE ${po_file} DIRECTORY)
    string(REGEX REPLACE ".*/(.*)" "\\1" po_dir "${SECOND_FOLDER_ABSOLUTE}" )
    SET(mo_file "${L10N_DIR}/${po_dir}/OrcaSlicer.mo")
    add_custom_command(
        TARGET gettext_po_to_mo PRE_BUILD
        COMMAND msgfmt ARGS --check-format -o ${mo_file} ${po_file}
        #COMMAND msgfmt ARGS --check-compatibility -o ${mo_file} ${po_file}
    )
endforeach()

find_package(NLopt 1.4 REQUIRED)

# Use bundled Python from deps instead of system Python
set(_bundled_python_version "3.12.13")
set(_bundled_python_abi "312")
string(REGEX REPLACE "^([0-9]+\\.[0-9]+)\\..*$" "\\1" _bundled_python_version_short "${_bundled_python_version}")
set(_bundled_python_root "${CMAKE_PREFIX_PATH}/libpython")
set(Python3_ROOT_DIR "${_bundled_python_root}" CACHE PATH "Root directory for bundled Python" FORCE)
set(Python3_USE_STATIC_LIBS OFF)
set(Python3_FIND_STRATEGY LOCATION)
set(Python3_FIND_IMPLEMENTATIONS CPython)

if(WIN32)
    set(_bundled_python_executable "${_bundled_python_root}/python.exe")
    set(_bundled_python_library "${_bundled_python_root}/libs/python${_bundled_python_abi}.lib")
    if(EXISTS "${_bundled_python_root}/python_d.exe")
        set(_bundled_python_executable "${_bundled_python_root}/python_d.exe")
    endif()
    if(EXISTS "${_bundled_python_root}/libs/python${_bundled_python_abi}_d.lib")
        set(_bundled_python_library "${_bundled_python_root}/libs/python${_bundled_python_abi}_d.lib")
    endif()

    set(Python3_FIND_REGISTRY NEVER)
    set(Python3_EXECUTABLE "${_bundled_python_executable}" CACHE FILEPATH "Bundled Python executable" FORCE)
    set(Python3_INCLUDE_DIR "${_bundled_python_root}/include" CACHE PATH "Bundled Python include directory" FORCE)
    set(Python3_LIBRARY "${_bundled_python_library}" CACHE FILEPATH "Bundled Python embed import library" FORCE)
elseif(APPLE)
    set(Python3_FIND_FRAMEWORK NEVER)

    find_program(_bundled_python_executable
        NAMES python${_bundled_python_version_short} python3
        PATHS "${_bundled_python_root}/bin"
        NO_DEFAULT_PATH
    )
    find_path(_bundled_python_include_dir
        NAMES Python.h
        PATHS
            "${_bundled_python_root}/include/python${_bundled_python_version_short}"
            "${_bundled_python_root}/include"
        NO_DEFAULT_PATH
    )
    find_library(_bundled_python_library
        NAMES python${_bundled_python_version_short} libpython${_bundled_python_version_short}
        PATHS "${_bundled_python_root}/lib"
        NO_DEFAULT_PATH
    )

    if(NOT _bundled_python_executable)
        message(FATAL_ERROR "Bundled Python executable not found under ${_bundled_python_root}/bin")
    endif()
    if(NOT _bundled_python_include_dir)
        message(FATAL_ERROR "Bundled Python headers not found under ${_bundled_python_root}/include")
    endif()
    if(NOT _bundled_python_library)
        message(FATAL_ERROR "Bundled Python library not found under ${_bundled_python_root}/lib")
    endif()

    set(Python3_EXECUTABLE "${_bundled_python_executable}" CACHE FILEPATH "Bundled Python executable" FORCE)
    set(Python3_INCLUDE_DIR "${_bundled_python_include_dir}" CACHE PATH "Bundled Python include directory" FORCE)
    set(Python3_LIBRARY "${_bundled_python_library}" CACHE FILEPATH "Bundled Python embed library" FORCE)
endif()

find_package(Python3 ${_bundled_python_version} EXACT REQUIRED
    COMPONENTS Interpreter Development.Embed
)

# Provide a minimal pybind11::embed target sourced from deps_src/pybind11 headers.
set(PYBIND11_SOURCE_DIR "${CMAKE_SOURCE_DIR}/deps_src/pybind11")
if(NOT EXISTS "${PYBIND11_SOURCE_DIR}/include/pybind11/pybind11.h")
    message(FATAL_ERROR "pybind11 headers not found in ${PYBIND11_SOURCE_DIR}. Did you initialize submodules?")
endif()

add_library(pybind11_headers INTERFACE)
target_include_directories(pybind11_headers INTERFACE "${PYBIND11_SOURCE_DIR}/include")
add_library(pybind11::headers ALIAS pybind11_headers)
add_library(pybind11::pybind11 ALIAS pybind11_headers)

add_library(pybind11_embed INTERFACE)
target_link_libraries(pybind11_embed INTERFACE pybind11_headers Python3::Python)
target_compile_definitions(pybind11_embed INTERFACE PYBIND11_SIMPLE_GIL_MANAGEMENT)
add_library(pybind11::embed ALIAS pybind11_embed)


if (SLIC3R_OPENVDB)
    if(SLIC3R_STATIC)
        set(OPENVDB_USE_STATIC_LIBS ON)
        set(USE_BLOSC TRUE)
    endif ()

    find_package(OpenVDB 5.0 COMPONENTS openvdb)
    if(OpenVDB_FOUND)
        slic3r_remap_configs(IlmBase::Half RelWithDebInfo Release)
        slic3r_remap_configs(Blosc::blosc RelWithDebInfo Release)
    else ()
        message(FATAL_ERROR "OpenVDB could not be found with the bundled find module. "
                       "You can try to specify the find module location of your "
                       "OpenVDB installation with the OPENVDB_FIND_MODULE_PATH cache variable, "
                       "or configure with -DSLIC3R_OPENVDB=OFF to build without SLA hollowing.")
    endif ()
else ()
    message(STATUS "SLIC3R_OPENVDB is OFF: building without OpenVDB (SLA hollowing disabled).")
endif ()

find_path(SPNAV_INCLUDE_DIR spnav.h)
if (SPNAV_INCLUDE_DIR)
    find_library(SPNAV_LIB NAMES libspnav.a spnav)
    if (SPNAV_LIB)
        add_definitions(-DHAVE_SPNAV)
        message(STATUS "SPNAV library found: ${SPNAV_LIB}")
    else()
        message(STATUS "SPNAV library NOT found, Spacenavd not supported")
    endif()
else()
    message(STATUS "SPNAV library NOT found, Spacenavd not supported")
endif()

set(TOP_LEVEL_PROJECT_DIR ${PROJECT_SOURCE_DIR})
function(orcaslicer_copy_dlls target config postfix output_dlls)
    if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64")
        set(_arch "x64")
    elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "X86")
        set(_arch "x86")
    elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ARM64")
        set(_arch "arm64")
    else ()
        message(FATAL_ERROR "Unable to detect architecture: ${CMAKE_SYSTEM_PROCESSOR}")
    endif ()

    get_property(_is_multi GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
    get_target_property(_alt_out_dir ${target} RUNTIME_OUTPUT_DIRECTORY)

    if (_alt_out_dir)
        set (_out_dir "${_alt_out_dir}")
        message ("set out_dir to _alt_out_dir: ${_out_dir}")
    elseif (_is_multi)
        set(_out_dir "${CMAKE_CURRENT_BINARY_DIR}/${config}")
        message ("set out_dir to CMAKE_CURRENT_BINARY_DIR/config: ${_out_dir}")
    else ()
        set(_out_dir "${CMAKE_CURRENT_BINARY_DIR}")
        message ("set out_dir to CMAKE_CURRENT_BINARY_DIR: ${_out_dir}")
    endif ()

    file(COPY ${TOP_LEVEL_PROJECT_DIR}/deps/GMP/gmp/lib/win-${_arch}/libgmp-10.dll
            ${TOP_LEVEL_PROJECT_DIR}/deps/MPFR/mpfr/lib/win-${_arch}/libmpfr-4.dll
            ${TOP_LEVEL_PROJECT_DIR}/deps/WebView2/lib/win-${_arch}/WebView2Loader.dll
        DESTINATION ${_out_dir})

    # Stage the OCCT toolkits libslic3r links (published as OCCT_LIBS), not whatever the
    # deps prefix happens to hold, and fail the configure if one of them is missing.
    if (NOT OCCT_LIBS)
        message(FATAL_ERROR "OCCT_LIBS is not set; libslic3r must be configured first.")
    endif ()
    string(TOUPPER "${config}" _config_upper)
    set(_occt_bin "${OCCT_BIN_DIR_${_config_upper}}")
    set(_occt_dlls "")
    set(_occt_staged "")
    set(_missing_occt "")
    foreach (_tk IN LISTS OCCT_LIBS)
        if (EXISTS "${_occt_bin}/${_tk}.dll")
            list(APPEND _occt_dlls "${_occt_bin}/${_tk}.dll")
            list(APPEND _occt_staged "${_out_dir}/${_tk}.dll")
        else ()
            list(APPEND _missing_occt "${_tk}.dll")
        endif ()
    endforeach ()
    if (_missing_occt)
        message(FATAL_ERROR
            "OCCT DLLs missing from ${_occt_bin}/: ${_missing_occt}\n"
            "Rebuild the dependencies (build_release_vs2022.bat deps) with the same "
            "SLIC3R_CAD setting as this project.")
    endif ()
    file(COPY ${_occt_dlls}
            ${CMAKE_PREFIX_PATH}/bin/avformat-61.dll
            ${CMAKE_PREFIX_PATH}/bin/avcodec-61.dll
            ${CMAKE_PREFIX_PATH}/bin/swresample-5.dll
            ${CMAKE_PREFIX_PATH}/bin/swscale-8.dll
            ${CMAKE_PREFIX_PATH}/bin/avutil-59.dll
         DESTINATION ${_out_dir})

    set(_dll_list
        ${_out_dir}/libgmp-10.dll
        ${_out_dir}/libmpfr-4.dll
        ${_out_dir}/WebView2Loader.dll

        ${_out_dir}/avformat-61.dll
        ${_out_dir}/avcodec-61.dll
        ${_out_dir}/swresample-5.dll
        ${_out_dir}/swscale-8.dll
        ${_out_dir}/avutil-59.dll
    )
    list(APPEND _dll_list ${_occt_staged})
    set(${output_dlls} ${_dll_list} PARENT_SCOPE)

endfunction()

function(orcaslicer_copy_sos target config postfix output_sos)

    get_property(_is_multi GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
    get_target_property(_alt_out_dir ${target} RUNTIME_OUTPUT_DIRECTORY)

    if (_alt_out_dir)
        set(_out_dir "${_alt_out_dir}")
    elseif (_is_multi)
        set(_out_dir "${CMAKE_CURRENT_BINARY_DIR}/${config}")
    else ()
        set(_out_dir "${CMAKE_CURRENT_BINARY_DIR}")
    endif ()

    file(COPY ${CMAKE_PREFIX_PATH}/lib/libavformat.so
            ${CMAKE_PREFIX_PATH}/lib/libavformat.so.61
            ${CMAKE_PREFIX_PATH}/lib/libavformat.so.61.1.100
            ${CMAKE_PREFIX_PATH}/lib/libavcodec.so
            ${CMAKE_PREFIX_PATH}/lib/libavcodec.so.61
            ${CMAKE_PREFIX_PATH}/lib/libavcodec.so.61.3.100
            ${CMAKE_PREFIX_PATH}/lib/libavutil.so
            ${CMAKE_PREFIX_PATH}/lib/libavutil.so.59
            ${CMAKE_PREFIX_PATH}/lib/libavutil.so.59.8.100
            ${CMAKE_PREFIX_PATH}/lib/libswscale.so
            ${CMAKE_PREFIX_PATH}/lib/libswscale.so.8
            ${CMAKE_PREFIX_PATH}/lib/libswscale.so.8.1.100
            ${CMAKE_PREFIX_PATH}/lib/libswresample.so
            ${CMAKE_PREFIX_PATH}/lib/libswresample.so.5
            ${CMAKE_PREFIX_PATH}/lib/libswresample.so.5.1.100
         DESTINATION ${_out_dir})

    set(${output_sos}
        ${_out_dir}/libavformat.so
        ${_out_dir}/libavformat.so.61
        ${_out_dir}/libavformat.so.61.1.100
        ${_out_dir}/libavcodec.so
        ${_out_dir}/libavcodec.so.61
        ${_out_dir}/libavcodec.so.61.3.100
        ${_out_dir}/libavutil.so
        ${_out_dir}/libavutil.so.59
        ${_out_dir}/libavutil.so.59.8.100
        ${_out_dir}/libswscale.so
        ${_out_dir}/libswscale.so.8
        ${_out_dir}/libswscale.so.8.1.100
        ${_out_dir}/libswresample.so
        ${_out_dir}/libswresample.so.5
        ${_out_dir}/libswresample.so.5.1.100
        PARENT_SCOPE
    )
endfunction()

# Bundled sources set their own warning flags, and a plain -Wall there means /Wall
# (= -Weverything) under clang-cl. Target options are applied after the ones a target
# set on itself, so these win. Targets are discovered rather than listed so a newly
# bundled library needs no maintenance here.
function(orcaslicer_silence_third_party_warnings _dir)
    get_property(_subdirs DIRECTORY "${_dir}" PROPERTY SUBDIRECTORIES)
    foreach (_subdir IN LISTS _subdirs)
        orcaslicer_silence_third_party_warnings("${_subdir}")
    endforeach ()
    get_property(_targets DIRECTORY "${_dir}" PROPERTY BUILDSYSTEM_TARGETS)
    foreach (_target IN LISTS _targets)
        get_target_property(_type ${_target} TYPE)
        if (NOT _type STREQUAL "INTERFACE_LIBRARY" AND NOT _type STREQUAL "UTILITY")
            if (MSVC AND NOT IS_CLANG_CL)
                # Drop any level the target set for itself, or -w overrides it and cl
                # reports D9025 once per file.
                get_target_property(_opts ${_target} COMPILE_OPTIONS)
                if (_opts)
                    string(REGEX REPLACE "/W[0-4]|/Wall" "" _opts "${_opts}")
                    string(REGEX REPLACE ";;+" ";" _opts "${_opts}")
                    set_target_properties(${_target} PROPERTIES COMPILE_OPTIONS "${_opts}")
                endif ()
                # CMake maps a level into the VS generator's WarningLevel element, while a
                # bare -w stays on the command line and trips D9025 there, once per file.
                target_compile_options(${_target} PRIVATE /W0)
            else ()
                target_compile_options(${_target} PRIVATE -w)
            endif ()
        endif ()
    endforeach ()
endfunction()


# libslic3r, OrcaSlicer GUI and the OrcaSlicer executable.
add_subdirectory(deps_src)

if (NOT SLIC3R_BUNDLED_WARNINGS)
    orcaslicer_silence_third_party_warnings("${CMAKE_CURRENT_SOURCE_DIR}/deps_src")
endif ()

# Warning level for the targets added below: our sources, plus glad and libvgcode,
# which are vendored but live under src/. The deps_src libraries were configured just
# above. CMP0092 left MSVC without a default level, so it is set here.
if (NOT SLIC3R_WARNINGS)
    add_compile_options(-w)
elseif (MSVC AND NOT IS_CLANG_CL)
    # /we4715 is C4715, no return from a non-void function, an error on the GNU/Clang
    # builds under -Werror. MSVC is not in that model, so this stays a single promoted
    # warning.
    add_compile_options(/W3 /we4715)
endif ()

add_subdirectory(src)
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT OrcaSlicer_app_gui)

add_dependencies(gettext_make_pot hintsToPot)

if(SLIC3R_BUILD_SANDBOXES)
    add_subdirectory(sandboxes)
endif()

if(BUILD_TESTS)
    add_subdirectory(tests)
    if (NOT SLIC3R_BUNDLED_WARNINGS)
        # Catch2 is vendored under tests/ and sets its own warning flags too.
        orcaslicer_silence_third_party_warnings("${CMAKE_CURRENT_SOURCE_DIR}/tests/catch2")
    endif ()
endif()

if (NOT WIN32 AND NOT APPLE)
    set(SLIC3R_APP_CMD "orca-slicer")
    configure_file(${LIBDIR}/dev-utils/platform/unix/build_appimage.sh.in ${CMAKE_CURRENT_BINARY_DIR}/build_appimage.sh USE_SOURCE_PERMISSIONS @ONLY)
endif()

# Resources install target, configure fhs.hpp on UNIX
if (WIN32)
    install(DIRECTORY "${SLIC3R_RESOURCES_DIR}/" DESTINATION "./resources")
    if(ORCA_BUNDLED_UV_EXECUTABLE)
        install(PROGRAMS "${ORCA_BUNDLED_UV_EXECUTABLE}" DESTINATION "./resources/tools/uv" RENAME "${ORCA_BUNDLED_UV_FILENAME}")
    endif()
    set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_SKIP TRUE)
    include(InstallRequiredSystemLibraries)
    # A missing MSVC runtime is an error because an installer without it cannot start on a clean machine.
    set(_orca_runtime_names ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS})
    list(TRANSFORM _orca_runtime_names REPLACE "^.*/" "")
    if (MSVC AND (NOT "msvcp140.dll" IN_LIST _orca_runtime_names OR NOT "vcruntime140.dll" IN_LIST _orca_runtime_names))
        set(_orca_runtime_error "CMake ${CMAKE_VERSION} did not find msvcp140.dll and vcruntime140.dll for MSVC ${MSVC_VERSION}. Update CMake to a release that supports this Visual Studio.")
        message(WARNING "${_orca_runtime_error}")
        install(CODE "message(FATAL_ERROR \"${_orca_runtime_error}\")")
    endif ()
    install (PROGRAMS ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS} DESTINATION ".")
elseif (SLIC3R_FHS)
    # CMAKE_INSTALL_FULL_DATAROOTDIR: read-only architecture-independent data root (share)
    set(SLIC3R_FHS_RESOURCES "${CMAKE_INSTALL_FULL_DATAROOTDIR}/OrcaSlicer")
    install(DIRECTORY ${SLIC3R_RESOURCES_DIR}/ DESTINATION ${SLIC3R_FHS_RESOURCES}
        PATTERN "*/udev" EXCLUDE
    )
    if(ORCA_BUNDLED_UV_EXECUTABLE)
        # A Flatpak build finds uv at this exact path, installed by the orca_deps
        # module before this one configures (see ORCA_UV_FLATPAK_BINARY above).
        # Installing it there again would copy the file onto itself.
        set(_orca_uv_fhs_binary "${SLIC3R_FHS_RESOURCES}/tools/uv/${ORCA_BUNDLED_UV_FILENAME}")
        if(NOT ORCA_BUNDLED_UV_EXECUTABLE_CONFIG STREQUAL _orca_uv_fhs_binary)
            install(PROGRAMS "${ORCA_BUNDLED_UV_EXECUTABLE}" DESTINATION "${SLIC3R_FHS_RESOURCES}/tools/uv" RENAME "${ORCA_BUNDLED_UV_FILENAME}")
        endif()
    endif()
    install(FILES src/dev-utils/platform/unix/com.orcaslicer.OrcaSlicer.desktop DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/applications)
    foreach(SIZE 32 128 192)
        install(FILES ${SLIC3R_RESOURCES_DIR}/images/OrcaSlicer_${SIZE}px.png
            DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/icons/hicolor/${SIZE}x${SIZE}/apps RENAME OrcaSlicer.png
        )
    endforeach()
elseif (CMAKE_MACOSX_BUNDLE)
    # install(DIRECTORY "${SLIC3R_RESOURCES_DIR}/" DESTINATION "${CMAKE_INSTALL_PREFIX}/OrcaSlicer.app/Contents/resources")
    if(ORCA_BUNDLED_UV_EXECUTABLE)
        install(PROGRAMS "${ORCA_BUNDLED_UV_EXECUTABLE}" DESTINATION "${CMAKE_INSTALL_PREFIX}/OrcaSlicer.app/Contents/Resources/tools/uv" RENAME "${ORCA_BUNDLED_UV_FILENAME}")
    endif()
else ()
    install(FILES src/dev-utils/platform/unix/com.orcaslicer.OrcaSlicer.desktop DESTINATION ${CMAKE_INSTALL_PREFIX}/resources/applications)
    install(DIRECTORY "${SLIC3R_RESOURCES_DIR}/" DESTINATION "${CMAKE_INSTALL_PREFIX}/resources")
    if(ORCA_BUNDLED_UV_EXECUTABLE)
        install(PROGRAMS "${ORCA_BUNDLED_UV_EXECUTABLE}" DESTINATION "${CMAKE_INSTALL_PREFIX}/resources/tools/uv" RENAME "${ORCA_BUNDLED_UV_FILENAME}")
    endif()
endif ()

if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
    set(LIBRARY_FILES
    ${LIBDIR_BIN}/libavformat.so.61
    ${LIBDIR_BIN}/libavformat.so.61.1.100
    ${LIBDIR_BIN}/libavcodec.so.61
    ${LIBDIR_BIN}/libavcodec.so.61.3.100
    ${LIBDIR_BIN}/libavutil.so.59
    ${LIBDIR_BIN}/libavutil.so.59.8.100
    ${LIBDIR_BIN}/libswresample.so.5
    ${LIBDIR_BIN}/libswresample.so.5.1.100
    ${LIBDIR_BIN}/libswscale.so.8
    ${LIBDIR_BIN}/libswscale.so.8.1.100
    )
    install(FILES ${LIBRARY_FILES} DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
endif ()

install(FILES ${CMAKE_SOURCE_DIR}/LICENSE.txt DESTINATION ".")
configure_file(${LIBDIR}/dev-utils/platform/unix/fhs.hpp.in ${LIBDIR_BIN}/dev-utils/platform/unix/fhs.hpp)

set (CPACK_PACKAGE_NAME "OrcaSlicer")
set (CPACK_PACKAGE_VENDOR "SoftFever")
set (CPACK_PACKAGE_VERSION_MAJOR "${ORCA_VERSION_MAJOR}")
set (CPACK_PACKAGE_VERSION_MINOR "${ORCA_VERSION_MINOR}")
set (CPACK_PACKAGE_VERSION_PATCH "${ORCA_VERSION_PATCH}")
set (CPACK_PACKAGE_FILE_NAME "OrcaSlicer_Windows_Installer_V${SoftFever_VERSION}")
# Suffix the Windows installer with its target arch so the x64 and arm64 builds
# produce distinct filenames (matches ARCH_SUFFIX in build_orca.yml). Same
# CMAKE_SYSTEM_PROCESSOR mapping used by orcaslicer_copy_dlls() above.
if (WIN32)
    if (CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64")
        string (APPEND CPACK_PACKAGE_FILE_NAME "_arm64")
    else ()
        string (APPEND CPACK_PACKAGE_FILE_NAME "_x64")
    endif ()
endif ()
set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "Orca Slicer is an open source slicer for FDM printers")
set (CPACK_PACKAGE_HOMEPAGE_URL "https://github.com/OrcaSlicer/OrcaSlicer")
set (CPACK_PACKAGE_INSTALL_DIRECTORY ${CPACK_PACKAGE_NAME})
set (CPACK_PACKAGE_ICON "${CMAKE_SOURCE_DIR}/resources/images\\\\OrcaSlicer.ico")
set (CPACK_NSIS_MUI_ICON "${CPACK_PACKAGE_ICON}")
set (CPACK_NSIS_MUI_UNIICON "${CPACK_PACKAGE_ICON}")
# Relative to the install dir: CPack's NSIS template prefixes $INSTDIR itself.
set (CPACK_NSIS_INSTALLED_ICON_NAME "orca-slicer.exe")
set (CPACK_PACKAGE_CHECKSUM SHA256)
set (CPACK_PACKAGE_INSTALL_REGISTRY_KEY "OrcaSlicer")
set (CPACK_NSIS_ENABLE_UNINSTALL_BEFORE_INSTALL ON)
set (CPACK_NSIS_EXECUTABLES_DIRECTORY ".")
# set (CPACK_NSIS_MODIFY_PATH "ON")
set(CPACK_PACKAGE_EXECUTABLES "orca-slicer;OrcaSlicer")
set(CPACK_CREATE_DESKTOP_LINKS "orca-slicer")
set (CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/LICENSE.txt) # must also include in install command

# CI release builds Authenticode-sign the installer as makensis builds it: the payload before it is
# compressed, the uninstaller the installer writes, and the installer itself. Only a configure run
# with ORCA_SIGN_KMS_KEY in its environment (build_orca.yml) wires this in; scripts/sign_windows.ps1
# lists what signing needs. The commands go into an included file because CPack writes
# CPACK_NSIS_* values into CPackConfig.cmake unescaped. !include needs a native path, with its
# backslashes doubled for CPackConfig.cmake. PowerShell 7 is preferred: Windows PowerShell started
# under a PowerShell 7 session (CI runs cpack from one) inherits its module path and cannot load
# Get-AuthenticodeSignature.
if (WIN32 AND DEFINED ENV{ORCA_SIGN_KMS_KEY})
    find_program(ORCA_SIGN_PWSH pwsh)
    if (ORCA_SIGN_PWSH)
        set(_orca_sign_shell pwsh)
    else ()
        set(_orca_sign_shell powershell)
    endif ()
    set(ORCA_SIGN_COMMAND "${_orca_sign_shell} -NoProfile -ExecutionPolicy Bypass -File \"${CMAKE_SOURCE_DIR}/scripts/sign_windows.ps1\" -Path")
    string(CONFIGURE [=[
!system '@ORCA_SIGN_COMMAND@ "${INST_DIR}"' = 0
!uninstfinalize '@ORCA_SIGN_COMMAND@ "%1"' = 0
!finalize '@ORCA_SIGN_COMMAND@ "%1"' = 0
]=] _orca_sign_nsh @ONLY)
    file(WRITE "${CMAKE_BINARY_DIR}/sign_windows.nsh" "${_orca_sign_nsh}")
    file(TO_NATIVE_PATH "${CMAKE_BINARY_DIR}/sign_windows.nsh" _orca_sign_nsh_path)
    string(REPLACE "\\" "\\\\" _orca_sign_nsh_path "${_orca_sign_nsh_path}")
    set(CPACK_NSIS_EXTRA_PREINSTALL_COMMANDS "!include \\\"${_orca_sign_nsh_path}\\\"")
endif ()

set(CPACK_WIX_UPGRADE_GUID "058245e8-20e0-4a95-9ab7-1acfe17ad511")
set(CPACK_GENERATOR NSIS)
include(CPack)
