# As of February 2025 we require CMake 3.22.0
cmake_minimum_required(VERSION 3.22.0 FATAL_ERROR)


# As of cMake 3.27, find_package() will look for both case-sensitive and all-uppercase versions
# of the package. This seems to affect FLANN as included by Pixi
if(POLICY CMP0144)
    cmake_policy(SET CMP0144 NEW)
endif()

# FindPythonInterp and FindPythonLibs modules are deprecated, but are still in use by
# all versions of Shiboken2. This policy must be kept at OLD until Shiboken2 is no longer
# supported
if(POLICY CMP0148)
    cmake_policy(SET CMP0148 OLD)
endif()

# The exec_program command was deprecated in cMake 3.0, and policy CMP0153 was added in cMake
# 3.28 to control whether this gives a warning (the OLD behavior) or a fatal error (NEW)
if(POLICY CMP0153)
    cmake_policy(SET CMP0153 NEW)
endif()

# Suppress 'FindBoost module is removed' warning. Will use BoostConfig.cmake instead
if(POLICY CMP0167)
    cmake_policy(SET CMP0167 NEW)
endif()

# As of cMake 3.31 add_custom_command() rejects invalid arguments
if(POLICY CMP0175)
    cmake_policy(SET CMP0175 NEW)
endif()

# Added in cMake 3.31, enforce normalization of all DESTINATION paths given to install()
if(POLICY CMP0177)
    cmake_policy(SET CMP0177 NEW)
endif()


option(FREECAD_USE_CCACHE "Auto detect and use ccache during compilation" ON)

if(FREECAD_USE_CCACHE)
    find_program(CCACHE_PROGRAM ccache)  #This check should occur before project()
    if(CCACHE_PROGRAM)
        message(STATUS "Using ccache found at: " ${CCACHE_PROGRAM})
        set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
        set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
    endif()
endif()

project(FreeCAD)

# WebAssembly is an explicit, opt-in configuration.  Desktop builds retain all
# existing defaults; the Web preset supplies the Emscripten toolchain and this
# block narrows the module graph to the first native-kernel probe.
option(FREECAD_WEB "Build the FreeCAD WebAssembly core configuration" OFF)
option(FREECAD_WEB_ENABLE_ASSEMBLY "Enable the Assembly Web probe and its dependency closure" OFF)
option(FREECAD_WEB_ENABLE_CAM "Enable the CAM Path App Web probe and its dependency closure" OFF)
option(FREECAD_WEB_ENABLE_CLOUD "Enable the Cloud App Web probe and its dependency closure" OFF)
option(FREECAD_WEB_ENABLE_INSPECTION "Enable the Inspection App Web probe and its dependency closure" OFF)
option(FREECAD_WEB_ENABLE_FEM "Enable the GUI-free FEM App Web probe and its dependency closure" OFF)
option(FREECAD_WEB_ENABLE_MESHPART "Enable the MeshPart App Web probe and its dependency closure" OFF)
option(FREECAD_WEB_ENABLE_OPENSCAD "Enable the OpenSCAD App Web probe and its dependency closure" OFF)
option(FREECAD_WEB_ENABLE_SANDBOX "Enable the Sandbox App Web probe and its dependency closure" OFF)
option(FREECAD_WEB_ENABLE_REVERSEENGINEERING "Enable the GUI-free ReverseEngineering App Web probe" OFF)
option(FREECAD_WEB_ENABLE_ROBOT "Enable the GUI-free Robot App Web probe" OFF)
option(FREECAD_WEB_ENABLE_SHOW "Enable the GUI-free Show Python Web probe and source closure" OFF)
option(FREECAD_WEB_ENABLE_SPREADSHEET "Enable the GUI-free Spreadsheet App Web probe and source closure" OFF)
option(FREECAD_WEB_ENABLE_START "Enable the GUI-free Start App Web probe and source closure" OFF)
option(FREECAD_WEB_ENABLE_SURFACE "Enable the GUI-free Surface App Web probe and source closure" OFF)
option(FREECAD_WEB_ENABLE_WEB "Enable the GUI-free Web App probe and source closure" OFF)
option(FREECAD_WEB_ENABLE_TEST "Enable the Python-only Test source probe" OFF)
option(FREECAD_WEB_ENABLE_TUX "Enable the GUI-only Tux boundary probe" OFF)
option(FREECAD_WEB_ENABLE_TECHDRAW_NATIVE "Enable native TechDraw App with QtGui/QtWidgets/QtConcurrent in Web build" OFF)
if(FREECAD_WEB)
    if(NOT EMSCRIPTEN OR NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
        message(FATAL_ERROR
            "FREECAD_WEB requires the Emscripten CMake toolchain. "
            "Use the freecad-web-core preset or pass -DCMAKE_TOOLCHAIN_FILE explicitly.")
    endif()

    set(BUILD_GUI OFF CACHE BOOL "Web core does not build the desktop GUI" FORCE)
    set(BUILD_FORCE_DIRECTORY ON CACHE BOOL "Web builds must be out of source" FORCE)
    set(BUILD_DYNAMIC_LINK_PYTHON OFF CACHE BOOL "Web core does not dynamically link Python" FORCE)
    set(BUILD_TRACY_FRAME_PROFILER OFF CACHE BOOL "Web core does not build Tracy" FORCE)
    set(ENABLE_DEVELOPER_TESTS OFF CACHE BOOL "Web core does not build desktop tests" FORCE)
    set(FREECAD_USE_PCH OFF CACHE BOOL "Web core does not use desktop PCH defaults" FORCE)
    set(FREECAD_USE_FREETYPE OFF CACHE BOOL "Web core leaves optional FreeType disabled" FORCE)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -include${CMAKE_SOURCE_DIR}/src/FCWebRangesCompat.h")
    add_compile_definitions(FREECAD_WEB_CORE)
    if(FREECAD_WEB_ENABLE_TECHDRAW_NATIVE)
        # Qt Concurrent/future is built with the Emscripten pthread ABI; all
        # dependent FreeCAD static objects must use matching atomics metadata.
        add_compile_options("-pthread" "-sUSE_PTHREADS=1" "-sPTHREAD_POOL_SIZE=4")
    endif()

    set(_freecad_web_disabled_options
            BUILD_ADDONMGR
            BUILD_ASSEMBLY
            BUILD_BIM
            BUILD_CAM
            BUILD_CLOUD
            BUILD_DRAWING
            BUILD_DRAFT
            BUILD_FEM
            BUILD_FLAT_MESH
            BUILD_HELP
            BUILD_INSPECTION
            BUILD_MESH
            BUILD_MESH_PART
            BUILD_OPENSCAD
            BUILD_PLOT
            BUILD_POINTS
            BUILD_REVERSEENGINEERING
            BUILD_ROBOT
            BUILD_SANDBOX
            BUILD_SHOW
            BUILD_SPREADSHEET
            BUILD_START
            BUILD_SURFACE
            BUILD_TECHDRAW
            BUILD_TEST
            BUILD_TUX
            BUILD_VR
            BUILD_WEB)
    if(FREECAD_WEB_ENABLE_WEB)
        list(REMOVE_ITEM _freecad_web_disabled_options BUILD_WEB)
    endif()
    if(FREECAD_WEB_ENABLE_ASSEMBLY)
        # Assembly's App target requires Spreadsheet; the configured dependency
        # graph also requires Draft and TechDraw. Keep this opt-in so the
        # default viewer/kernel build remains unchanged.
        list(REMOVE_ITEM _freecad_web_disabled_options
            BUILD_ASSEMBLY BUILD_SPREADSHEET BUILD_DRAFT BUILD_TECHDRAW)
    endif()
    if(FREECAD_WEB_ENABLE_CAM)
        # CAM's Path App target is GUI-free, but its build graph requires Mesh
        # in addition to the existing Part and Import Web modules.
        list(REMOVE_ITEM _freecad_web_disabled_options BUILD_CAM BUILD_MESH)
    endif()
    if(FREECAD_WEB_ENABLE_CLOUD)
        list(REMOVE_ITEM _freecad_web_disabled_options BUILD_CLOUD)
    endif()
    if(FREECAD_WEB_ENABLE_INSPECTION)
        list(REMOVE_ITEM _freecad_web_disabled_options BUILD_INSPECTION BUILD_MESH BUILD_POINTS)
    endif()
    if(FREECAD_WEB_ENABLE_FEM)
        list(REMOVE_ITEM _freecad_web_disabled_options BUILD_FEM)
    endif()
    if(FREECAD_WEB_ENABLE_MESHPART)
        list(REMOVE_ITEM _freecad_web_disabled_options BUILD_MESH_PART BUILD_MESH)
    endif()
    if(FREECAD_WEB_ENABLE_OPENSCAD)
        # OpenSCAD is a Python workbench whose source target depends on the
        # Draft/TechDraw/Spreadsheet script closure, even when BUILD_GUI is OFF.
        list(REMOVE_ITEM _freecad_web_disabled_options
            BUILD_OPENSCAD BUILD_DRAFT BUILD_TECHDRAW BUILD_SPREADSHEET)
    endif()
    if(FREECAD_WEB_ENABLE_SANDBOX)
        # Sandbox App is GUI-free only when its QtCore/PyCXX document protector
        # is linked without SandboxGui. Mesh is its direct App dependency.
        list(REMOVE_ITEM _freecad_web_disabled_options BUILD_SANDBOX BUILD_MESH)
    endif()
    if(FREECAD_WEB_ENABLE_REVERSEENGINEERING)
        list(REMOVE_ITEM _freecad_web_disabled_options BUILD_REVERSEENGINEERING)
    endif()
    if(FREECAD_WEB_ENABLE_ROBOT)
        list(REMOVE_ITEM _freecad_web_disabled_options BUILD_ROBOT)
    endif()
    if(FREECAD_WEB_ENABLE_SHOW)
        # Show's GUI-free Python core is embedded into a native Web probe.
        # Keep the official source target enabled for the same source closure;
        # GUI-only SceneDetails are not linked into the probe.
        list(REMOVE_ITEM _freecad_web_disabled_options BUILD_SHOW)
    endif()
    if(FREECAD_WEB_ENABLE_SPREADSHEET)
        list(REMOVE_ITEM _freecad_web_disabled_options BUILD_SPREADSHEET)
    endif()
    if(FREECAD_WEB_ENABLE_START)
        list(REMOVE_ITEM _freecad_web_disabled_options BUILD_START)
    endif()
    if(FREECAD_WEB_ENABLE_SURFACE)
        list(REMOVE_ITEM _freecad_web_disabled_options BUILD_SURFACE)
    endif()
    foreach(_freecad_web_disabled_option IN LISTS _freecad_web_disabled_options)
        set(${_freecad_web_disabled_option} OFF CACHE BOOL "Disabled by FREECAD_WEB core configuration" FORCE)
    endforeach()
    unset(_freecad_web_disabled_options)

    foreach(_freecad_web_enabled_option BUILD_IMPORT BUILD_MATERIAL BUILD_MEASURE BUILD_PART BUILD_PART_DESIGN BUILD_SKETCHER)
        set(${_freecad_web_enabled_option} ON CACHE BOOL "Enabled by FREECAD_WEB core configuration" FORCE)
    endforeach()
    if(FREECAD_WEB_ENABLE_FEM)
        set(BUILD_FEM ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_FEM" FORCE)
    endif()
    if(FREECAD_WEB_ENABLE_ASSEMBLY)
        foreach(_freecad_web_assembly_option BUILD_ASSEMBLY BUILD_SPREADSHEET BUILD_DRAFT BUILD_TECHDRAW)
            set(${_freecad_web_assembly_option} ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_ASSEMBLY" FORCE)
        endforeach()
    endif()
    if(FREECAD_WEB_ENABLE_CAM)
        foreach(_freecad_web_cam_option BUILD_CAM BUILD_MESH)
            set(${_freecad_web_cam_option} ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_CAM" FORCE)
        endforeach()
    endif()
    if(FREECAD_WEB_ENABLE_CLOUD)
        set(BUILD_CLOUD ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_CLOUD" FORCE)
    endif()
    if(FREECAD_WEB_ENABLE_INSPECTION)
        foreach(_freecad_web_inspection_option BUILD_INSPECTION BUILD_MESH BUILD_POINTS)
            set(${_freecad_web_inspection_option} ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_INSPECTION" FORCE)
        endforeach()
    endif()
    if(FREECAD_WEB_ENABLE_MESHPART)
        foreach(_freecad_web_meshpart_option BUILD_MESH_PART BUILD_MESH)
            set(${_freecad_web_meshpart_option} ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_MESHPART" FORCE)
        endforeach()
    endif()
    if(FREECAD_WEB_ENABLE_OPENSCAD)
        foreach(_freecad_web_openscad_option BUILD_OPENSCAD BUILD_DRAFT BUILD_TECHDRAW BUILD_SPREADSHEET)
            set(${_freecad_web_openscad_option} ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_OPENSCAD" FORCE)
        endforeach()
    endif()
    if(FREECAD_WEB_ENABLE_SANDBOX)
        foreach(_freecad_web_sandbox_option BUILD_SANDBOX BUILD_MESH)
            set(${_freecad_web_sandbox_option} ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_SANDBOX" FORCE)
        endforeach()
    endif()
    if(FREECAD_WEB_ENABLE_REVERSEENGINEERING)
        set(BUILD_REVERSEENGINEERING ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_REVERSEENGINEERING" FORCE)
    endif()
    if(FREECAD_WEB_ENABLE_ROBOT)
        set(BUILD_ROBOT ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_ROBOT" FORCE)
    endif()
    if(FREECAD_WEB_ENABLE_SHOW)
        set(BUILD_SHOW ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_SHOW" FORCE)
    endif()
    if(FREECAD_WEB_ENABLE_SPREADSHEET)
        set(BUILD_SPREADSHEET ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_SPREADSHEET" FORCE)
    endif()
    if(FREECAD_WEB_ENABLE_START)
        set(BUILD_START ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_START" FORCE)
    endif()
    if(FREECAD_WEB_ENABLE_SURFACE)
        set(BUILD_SURFACE ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_SURFACE" FORCE)
    endif()
    if(FREECAD_WEB_ENABLE_WEB)
        set(BUILD_WEB ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_WEB" FORCE)
    endif()
    if(FREECAD_WEB_ENABLE_TECHDRAW_NATIVE)
        set(BUILD_TECHDRAW ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_TECHDRAW_NATIVE" FORCE)
        set(BUILD_SPREADSHEET ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_TECHDRAW_NATIVE" FORCE)
        set(BUILD_DRAFT ON CACHE BOOL "Enabled by FREECAD_WEB_ENABLE_TECHDRAW_NATIVE" FORCE)
    endif()

    include(cMake/FreeCADWebDependencies.cmake)
endif()

# Sanitizer support
option(FREECAD_USE_SANITIZER_ASAN "Enable AddressSanitizer ie. (ASan)" OFF) 
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    option(FREECAD_USE_SANITIZER_LSAN "Enable LeakSanitizer (LSan)" OFF)
    option(FREECAD_USE_SANITIZER_TSAN "Enable ThreadSanitizer (TSan)" OFF)
    option(FREECAD_USE_SANITIZER_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF)
    option(FREECAD_USE_SANITIZER_MSAN "Enable MemorySanitizer (MSan)" OFF)
endif()

mark_as_advanced(
    FREECAD_USE_SANITIZER_ASAN
    FREECAD_USE_SANITIZER_LSAN
    FREECAD_USE_SANITIZER_TSAN
    FREECAD_USE_SANITIZER_UBSAN
    FREECAD_USE_SANITIZER_MSAN
)

set(PACKAGE_VERSION_MAJOR "1")
set(PACKAGE_VERSION_MINOR "2")
set(PACKAGE_VERSION_PATCH "0") # number of patch release (e.g. "4" for the 0.18.4 release)
set(PACKAGE_VERSION_SUFFIX "dev") # either "dev" for development snapshot or "" (empty string)
set(PACKAGE_BUILD_VERSION "0") # used when the same FreeCAD version will be re-released (for example using an updated LibPack)
string(TIMESTAMP PACKAGE_COPYRIGHT_YEAR "%Y")

set(PACKAGE_VERSION "${PACKAGE_VERSION_MAJOR}.${PACKAGE_VERSION_MINOR}.${PACKAGE_VERSION_PATCH}")
set(PACKAGE_STRING "${PROJECT_NAME} ${PACKAGE_VERSION}")

# include local modules
include(CheckCXXCompilerFlag)
include(AddFileDependencies)
include(cMake/FreeCadMacros.cmake)
# include helper functions/macros
add_subdirectory(cMake/FreeCAD_Helpers)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cMake")

CompilerChecksAndSetups()
ConfigureCMakeVariables()
InitializeFreeCADBuildOptions()
CheckInterModuleDependencies()
FreeCADLibpackChecks()
SetupDoxygen()
SetupLibFmt()
SetupSanitizers()
SetupYamlCpp()
SetupZipIos()
find_package(ICU REQUIRED COMPONENTS uc i18n)
if(FREECAD_WEB)
    # Emscripten's ICU port keeps the data archive separate from uc/i18n.
    # Keep it in the Web link interface without changing desktop discovery.
    list(APPEND ICU_LIBRARIES "${FREECAD_WEB_ICU_DATA_LIBRARY}")
endif()
if(NOT FREECAD_LIBPACK_USE OR FREECAD_LIBPACK_CHECKFILE_CLBUNDLER OR FREECAD_LIBPACK_CHECKFILE_VERSION)
    SetupPython()
    SetupPCL()
    SetupPybind11()
    SetupXercesC()
    find_package(ZLIB REQUIRED)
    find_package(PyCXX REQUIRED)
    SetupOpenCasCade()
    if(BUILD_GUI)
        # Do this before the check for SMESH because it depends on vtk
        # that may have its own OpenGL check but possibly fails and leaves
        # OPENGL_gl_LIBRARY empty that results into linker errors
        SetupOpenGL()
    endif(BUILD_GUI)
    SetupSalomeSMESH()
    if (BUILD_FEM_NETGEN)
        set(NETGEN_DEFINITIONS -DNO_PARALLEL_THREADS -DOCCGEOMETRY)
        if (NOT FREECAD_USE_EXTERNAL_SMESH)
            find_package(NETGEN REQUIRED)
        endif()
    endif()
    # not needed at the moment
    #find_package(OpenCV REQUIRED)
    SetupSwig()
    SetupEigen()
    # This file is included directly due to some cMake macros that are defined within.
    # If these macro definitions are embedded within another macro, it causes problems.
    include(cMake/FreeCAD_Helpers/SetupQt.cmake)
    SetupFreetype()

    if(BUILD_GUI)
        SetupCoin3D()
        SetupPivy()
        SetupSpaceball()
        SetupShibokenAndPyside()
        SetupMatplotlib()
    endif(BUILD_GUI)

    # SetupCoin3D can overwrite find_package(Boost) output so keep this after.
    SetupBoost()

    if(BUILD_BIM)
        SetupLark()
    endif()
endif()

if(BUILD_VR)
    find_package(Rift)
endif(BUILD_VR)

SetLibraryVersions()
SetGlobalCompilerAndLinkerSettings()

add_subdirectory(src)
add_subdirectory(data)

BuildAndInstallDesignerPlugin()

CreatePackagingTargets()

if(MSVC AND FREECAD_LIBPACK_USE AND LIBPACK_FOUND)
    CopyLibpackDirectories()
endif()

if (ENABLE_DEVELOPER_TESTS)
    include(CTest)
    enable_testing()
    add_subdirectory(tests)
endif()

PrintFinalReport()

message("\n=================================================\n"
        "Now run 'cmake --build ${CMAKE_BINARY_DIR}' to build ${PROJECT_NAME}\n"
        "=================================================\n")
