From e25338213b56e3daf96a3bcf215c25a1f66ee2a5 Mon Sep 17 00:00:00 2001
From: Sergey Fedorov <barracuda@macos-powerpc.org>
Date: Wed, 29 Jan 2025 08:22:38 +0800
Subject: [PATCH] CMakeLists: minor fixes for Apple

---
 CMakeLists.txt             | 36 +++++++++++++++++++-----------------
 cmake/macos.cmake          |  2 +-
 daemon/CMakeLists.txt      |  4 ++--
 llarp/CMakeLists.txt       |  4 +++-
 llarp/apple/CMakeLists.txt |  6 ++++--
 5 files changed, 29 insertions(+), 23 deletions(-)

diff --git CMakeLists.txt CMakeLists.txt
index b766111f2..a2779a69c 100644
--- CMakeLists.txt
+++ CMakeLists.txt
@@ -3,13 +3,15 @@ cmake_minimum_required(VERSION 3.13...3.24)  # 3.13 is buster's version
 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
 
 # Has to be set before `project()`, and ignored on non-macos:
-set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15 CACHE STRING "macOS deployment target (Apple clang only)")
+if(NOT MACOS_LEGACY)
+  set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15 CACHE STRING "macOS deployment target (Apple clang only)")
+endif()
 
 option(BUILD_DAEMON "build lokinet daemon and associated utils" ON)
 
 
 set(LANGS C CXX)
-if(APPLE AND BUILD_DAEMON)
+if(APPLE AND BUILD_DAEMON AND (NOT MACOS_LEGACY))
   set(LANGS ${LANGS} OBJC Swift)
 endif()
 
@@ -61,6 +63,8 @@ option(BUILD_PACKAGE "builds extra components for making an installer (with 'mak
 option(WITH_BOOTSTRAP "build lokinet-bootstrap tool" ${DEFAULT_WITH_BOOTSTRAP})
 option(WITH_PEERSTATS "build with experimental peerstats db support" OFF)
 option(STRIP_SYMBOLS "strip off all debug symbols into an external archive for all executables built" OFF)
+option(MACOS_DYNAMIC "allow non-static build on macOS, experimental" OFF)
+option(MACOS_LEGACY "compat build for legacy versions of macOS, experimental" OFF)
 
 set(BOOTSTRAP_FALLBACK_MAINNET "${PROJECT_SOURCE_DIR}/contrib/bootstrap/mainnet.signed" CACHE PATH "Fallback bootstrap path (mainnet)")
 set(BOOTSTRAP_FALLBACK_TESTNET "${PROJECT_SOURCE_DIR}/contrib/bootstrap/testnet.signed" CACHE PATH "Fallback bootstrap path (testnet)")
@@ -212,20 +216,18 @@ if (WOW64_CROSS_COMPILE OR WIN64_CROSS_COMPILE)
   include(cmake/cross_compile.cmake)
 endif()
 
-if(NOT APPLE)
-  if(NATIVE_BUILD)
-    if(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc*|power*")
-      add_compile_options(-mcpu=native -mtune=native)
-    else()
-      add_compile_options(-march=native -mtune=native)
-    endif()
-  elseif(NOT NON_PC_TARGET)
-    if (USE_AVX2)
-      add_compile_options(-march=haswell -mtune=haswell -mfpmath=sse)
-    else()
-      # Public binary releases
-      add_compile_options(-march=nocona -mtune=haswell -mfpmath=sse)
-    endif()
+if(NATIVE_BUILD)
+  if(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc*|power*")
+    add_compile_options(-mcpu=native -mtune=native)
+  else()
+    add_compile_options(-march=native -mtune=native)
+  endif()
+elseif(NOT NON_PC_TARGET)
+  if (USE_AVX2)
+    add_compile_options(-march=haswell -mtune=haswell -mfpmath=sse)
+  else()
+    # Public binary releases
+    add_compile_options(-march=nocona -mtune=haswell -mfpmath=sse)
   endif()
 endif()
 
@@ -316,7 +318,7 @@ add_subdirectory(docs)
 
 include(cmake/gui.cmake)
 
-if(APPLE)
+if(APPLE AND NOT MACOS_LEGACY)
   macos_target_setup()
 endif()
 
diff --git cmake/macos.cmake cmake/macos.cmake
index c72248359..1041f39ac 100644
--- cmake/macos.cmake
+++ cmake/macos.cmake
@@ -1,4 +1,4 @@
-if(NOT APPLE)
+if(NOT APPLE OR (APPLE AND MACOS_LEGACY))
   return()
 endif()
 
diff --git daemon/CMakeLists.txt daemon/CMakeLists.txt
index c9ff4aec6..5d248378f 100644
--- daemon/CMakeLists.txt
+++ daemon/CMakeLists.txt
@@ -1,6 +1,6 @@
 set(exetargets lokinet)
 
-if(APPLE)
+if(APPLE AND NOT MACOS_LEGACY)
   add_executable(lokinet lokinet.swift)
   target_compile_options(lokinet BEFORE PRIVATE -target x86_64-apple-macos${CMAKE_OSX_DEPLOYMENT_TARGET})
 else()
@@ -68,7 +68,7 @@ foreach(exe ${exetargets})
   endif()
   target_include_directories(${exe} PUBLIC "${PROJECT_SOURCE_DIR}")
   if(should_install)
-    if(APPLE)
+    if(APPLE AND NOT MACOS_LEGACY)
       install(TARGETS ${exe}
         BUNDLE DESTINATION "${PROJECT_BINARY_DIR}"
         RUNTIME DESTINATION "."
diff --git llarp/CMakeLists.txt llarp/CMakeLists.txt
index 6653a5f08..c9863996b 100644
--- llarp/CMakeLists.txt
+++ llarp/CMakeLists.txt
@@ -72,7 +72,9 @@ else()
 endif()
 
 if(APPLE)
-  add_subdirectory(apple)
+  if(NOT MACOS_LEGACY)
+    add_subdirectory(apple)
+  endif()
   target_sources(lokinet-platform PRIVATE util/nop_service_manager.cpp)
 endif()
 
diff --git llarp/apple/CMakeLists.txt llarp/apple/CMakeLists.txt
index 8dd561ef7..739f74c63 100644
--- llarp/apple/CMakeLists.txt
+++ llarp/apple/CMakeLists.txt
@@ -2,7 +2,7 @@
 # 3.13+ so that we can add link libraries to parent targets
 cmake_minimum_required(VERSION 3.13)
 
-if (BUILD_SHARED_LIBS OR NOT BUILD_STATIC_DEPS OR NOT STATIC_LINK)
+if ((BUILD_SHARED_LIBS OR NOT BUILD_STATIC_DEPS OR NOT STATIC_LINK) AND NOT MACOS_DYNAMIC)
   message(FATAL_ERROR "macOS builds require a full static build; perhaps use the contrib/mac.sh script to build?")
 endif()
 
@@ -24,7 +24,9 @@ enable_lto(lokinet-extension)
 
 # -fobjc-arc enables automatic reference counting for objective-C code
 # -e _NSExtensionMain because the appex has that instead of a `main` function entry point, of course.
-target_compile_options(lokinet-extension PRIVATE -fobjc-arc)
+if(CMAKE_C_COMPILER_ID MATCHES "Clang")
+  target_compile_options(lokinet-extension PRIVATE -fobjc-arc)
+endif()
 if(MACOS_SYSTEM_EXTENSION)
   target_compile_definitions(lokinet-extension PRIVATE MACOS_SYSTEM_EXTENSION)
   target_compile_definitions(lokinet-util PUBLIC MACOS_SYSTEM_EXTENSION)
