-- Patch the root CMakeLists.txt to avoid network fetches during build.
-- Use the pre-fetched mimalloc and cadical sources from GH_TUPLE instead of
-- downloading them from GitHub. Also stop searching for a system cadical
-- binary so the bundled cadical is built and installed.
-- https://github.com/leanprover/lean4/issues/5603 (bundled cadical avoids
-- installing a duplicate system binary).

--- CMakeLists.txt.orig	2026-11-16 02:28:42 UTC
+++ CMakeLists.txt
@@ -44,10 +44,9 @@ if(NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten")
 
 # Don't do anything with cadical/leantar on wasm
 if(NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten")
-  find_program(CADICAL cadical)
   # `libcadical.a` is linked into the Lean libraries, so it has to be built with matching flags
   # (position-independent, hidden visibility). Packagers can point these at their own build to
-  # avoid the source fetch below, but then have to provide all three.
+  # avoid the bundled source below, but then have to provide all three.
   set(CADICAL_LIB "" CACHE FILEPATH "Path to a static libcadical to link into the Lean libraries")
   set(CADICAL_INCLUDE_DIR "" CACHE PATH "Directory containing cadical.hpp")
   if(NOT (CADICAL AND CADICAL_LIB AND CADICAL_INCLUDE_DIR))
@@ -82,11 +81,11 @@ if(NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten")
     ExternalProject_Add(
       cadical
       PREFIX cadical
-      GIT_REPOSITORY https://github.com/arminbiere/cadical
-      GIT_TAG rel-2.1.2
+      SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cadical"
       CONFIGURE_COMMAND ""
       BUILD_COMMAND
         $(MAKE) -f ${CMAKE_SOURCE_DIR}/src/cadical.mk CMAKE_EXECUTABLE_SUFFIX=${CMAKE_EXECUTABLE_SUFFIX}
+        CADICAL_OUT=${CMAKE_BINARY_DIR}/cadical
         CXX=${CADICAL_CXX} CXXFLAGS=${CADICAL_CXXFLAGS} LDFLAGS=${CADICAL_LDFLAGS} AR=${CMAKE_AR}
       BUILD_IN_SOURCE ON
       INSTALL_COMMAND ""
@@ -100,7 +99,7 @@ if(NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten")
       set(CADICAL_LIB ${CMAKE_BINARY_DIR}/cadical/libcadical.a)
     endif()
     if(NOT CADICAL_INCLUDE_DIR)
-      set(CADICAL_INCLUDE_DIR ${CMAKE_BINARY_DIR}/cadical/src/cadical/src)
+      set(CADICAL_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cadical/src)
     endif()
     list(APPEND EXTRA_DEPENDS cadical)
   endif()
@@ -159,14 +158,8 @@ if(USE_MIMALLOC)
 if(USE_MIMALLOC)
   FetchContent_Declare(
     mimalloc
-    GIT_REPOSITORY https://github.com/microsoft/mimalloc
-    GIT_BRANCH dev3
-    GIT_TAG v3.4.5
-    # Unnecessarily deep directory structure, but it saves us from a complicated
-    # stage0 update for now. If we ever update the other dependencies like
-    # cadical, it might be worth reorganizing the directory structure.
-    SOURCE_DIR
-    "${CMAKE_BINARY_DIR}/mimalloc/src/mimalloc"
+    # Point directly to the directory where the FreeBSD port extracted the files
+    SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cmake/mimalloc/src/mimalloc"
     EXCLUDE_FROM_ALL
   )
   FetchContent_MakeAvailable(mimalloc)
