From b0fa5fcf7f4c308727fafae872245a02b98efc64 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 28 Nov 2026 12:56:16 +0000 Subject: [PATCH] wip: toolchain for Mac OS X 10.4 --- cmake/macosx-legacy.cmake | 99 +++++++++++++++++++++++++++++++++++ cmake/macosx-legacy/net/if.h | 16 ++++++ cmake/macosx-legacy/signal.h | 24 +++++++++ cmake/macosx-legacy/time.h | 33 ++++++++++++ ssh/cmake/BundledLibssh.cmake | 7 +++ winport/CMakeLists.txt | 33 +++++++++--- 6 files changed, 206 insertions(+), 6 deletions(-) create mode 100644 cmake/macosx-legacy.cmake create mode 100644 cmake/macosx-legacy/net/if.h create mode 100644 cmake/macosx-legacy/signal.h create mode 100644 cmake/macosx-legacy/time.h diff --git a/cmake/macosx-legacy.cmake b/cmake/macosx-legacy.cmake new file mode 100644 index 0000000..ab54095 --- /dev/null +++ b/cmake/macosx-legacy.cmake @@ -0,0 +1,99 @@ +# CMake toolchain file for Mac OS X 10.4 (Tiger) and later, on 32-bit +# PowerPC or 32-bit Intel. It builds the Windows app (winport/) over the +# Mac OS X 10.0-10.5 port of clang-retro MFC, on Linux. +# +# cmake -S . -B build/ppc -DCMAKE_TOOLCHAIN_FILE=cmake/macosx-legacy.cmake \ +# -DMACOSX_LEGACY_ARCH=powerpc -DMACOSX_LEGACY_LLVM=/opt/llvm \ +# -DMACOSX_LEGACY_SYSROOT=/opt/clang-dos/macosx10.4 \ +# -DMACOSX_LEGACY_CCTOOLS=/opt/cctools-ppc \ +# -DCLANG_RETRO_MFC=/path/to/mfc -DDMODEM_GUI_WINPORT=ON +# +# You need these: +# MACOSX_LEGACY_ARCH powerpc or i386 +# MACOSX_LEGACY_LLVM clang of clang-retro/llvm (the llvm-prebuilt +# release), with emulated TLS for Mac OS X 10.4 +# MACOSX_LEGACY_SYSROOT the macosx10.4 sysroot of clang-retro/dos-sdk +# (packaging/get-toolchain.sh -s macosx10.4): libc++, +# and MacOSX10.4u.sdk in it +# MACOSX_LEGACY_CCTOOLS Apple's ld64 and ar for PowerPC +# (dos-sdk's scripts/build-cctools-ppc.sh) +# The environment variables of the same names also set them. +set(CMAKE_SYSTEM_NAME Darwin) +set(CMAKE_SYSTEM_VERSION 8.0) + +foreach(_v ARCH LLVM SYSROOT CCTOOLS) + if(NOT MACOSX_LEGACY_${_v} AND DEFINED ENV{MACOSX_LEGACY_${_v}}) + set(MACOSX_LEGACY_${_v} "$ENV{MACOSX_LEGACY_${_v}}") + endif() + if(NOT MACOSX_LEGACY_${_v}) + message(FATAL_ERROR "Set MACOSX_LEGACY_${_v} (cmake/macosx-legacy.cmake)") + endif() + set(MACOSX_LEGACY_${_v} "${MACOSX_LEGACY_${_v}}" CACHE STRING "" FORCE) + # (the builds that D-Modem configures with this file see them too) + set(ENV{MACOSX_LEGACY_${_v}} "${MACOSX_LEGACY_${_v}}") +endforeach() +# (try_compile projects see these) +list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES + MACOSX_LEGACY_ARCH MACOSX_LEGACY_LLVM MACOSX_LEGACY_SYSROOT MACOSX_LEGACY_CCTOOLS) + +set(CMAKE_SYSTEM_PROCESSOR ${MACOSX_LEGACY_ARCH}) +set(MACOSX_LEGACY_TARGET "${MACOSX_LEGACY_ARCH}-apple-macosx10.4") +set(MACOSX_LEGACY_SDK "${MACOSX_LEGACY_SYSROOT}/MacOSX10.4u.sdk") +set(_cct "${MACOSX_LEGACY_CCTOOLS}/bin/powerpc-apple-darwin8-") + +set(CMAKE_OSX_SYSROOT "${MACOSX_LEGACY_SDK}") +set(CMAKE_OSX_DEPLOYMENT_TARGET 10.4) +set(CMAKE_OSX_ARCHITECTURES "") +# ld64 cannot read the symbol table that llvm-ar writes for PowerPC +set(CMAKE_AR "${_cct}ar") +set(CMAKE_RANLIB "${_cct}ranlib") +set(CMAKE_LIPO "${_cct}lipo") +set(CMAKE_INSTALL_NAME_TOOL "${_cct}install_name_tool") + +# The C library functions that Mac OS X 10.4 does not have, such as +# clock_gettime: port/compat/macosx of clang-retro MFC (CLANG_RETRO_MFC), +# and cmake/macosx-legacy (the fixes of headers of the 10.4 SDK). +# libwinport.a has their code. _DARWIN_C_SOURCE: gmtime_r and the other +# POSIX functions also with -std=c99. And libc++ of the sysroot, with +# threads. +if(NOT CLANG_RETRO_MFC AND DEFINED ENV{CLANG_RETRO_MFC}) + set(CLANG_RETRO_MFC "$ENV{CLANG_RETRO_MFC}") +endif() +if(NOT CLANG_RETRO_MFC) + message(FATAL_ERROR "Set CLANG_RETRO_MFC (cmake/macosx-legacy.cmake)") +endif() +set(ENV{CLANG_RETRO_MFC} "${CLANG_RETRO_MFC}") +list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES CLANG_RETRO_MFC) +# These are mandatory arguments of the compilers, with the target, not +# flags: a -DCMAKE_C_FLAGS of a project or an ExternalProject keeps them. +set(_compat -isystem ${CMAKE_CURRENT_LIST_DIR}/macosx-legacy + -isystem ${CLANG_RETRO_MFC}/port/compat/macosx + -DWINPORT_MACOSX_COMPAT -D_DARWIN_C_SOURCE) +set(_cxx -nostdinc++ -isystem ${MACOSX_LEGACY_SYSROOT}/include/c++/v1 + -D__STDC_FORMAT_MACROS) +set(CMAKE_C_COMPILER "${MACOSX_LEGACY_LLVM}/bin/clang" + "--target=${MACOSX_LEGACY_TARGET}" ${_compat}) +set(CMAKE_CXX_COMPILER "${MACOSX_LEGACY_LLVM}/bin/clang++" + "--target=${MACOSX_LEGACY_TARGET}" ${_cxx} ${_compat}) +set(CMAKE_OBJCXX_COMPILER ${CMAKE_CXX_COMPILER}) +# The builtins of the sysroot have emulated TLS and the atomic library +# functions (__atomic_fetch_add_8 and others), which libc++ and libre need +if(MACOSX_LEGACY_ARCH STREQUAL "powerpc") + set(_rt "${MACOSX_LEGACY_SYSROOT}/lib/darwin/libclang_rt.ppc_osx.a") +else() + set(_rt "${MACOSX_LEGACY_SYSROOT}/lib/darwin/libclang_rt.osx.a") +endif() +set(_ld "-fuse-ld=${_cct}ld -nostdlib++ -L${MACOSX_LEGACY_SYSROOT}/lib -lc++ -lc++abi -lunwind ${_rt}") +# (a program exports no symbols of libc++: on i386, ld64 stops at a +# pointer difference to a global weak symbol, such as std::to_string's +# to __itoa::__pow10_32 in the libc++ of the sysroot, when another object +# also has that symbol) +set(CMAKE_EXE_LINKER_FLAGS_INIT "${_ld} -Wl,-unexported_symbol,__ZNSt3__1*") +set(CMAKE_SHARED_LINKER_FLAGS_INIT "${_ld}") +set(CMAKE_MODULE_LINKER_FLAGS_INIT "${_ld}") + +list(APPEND CMAKE_FIND_ROOT_PATH "${MACOSX_LEGACY_SDK}" "${MACOSX_LEGACY_SYSROOT}") +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) diff --git a/cmake/macosx-legacy/net/if.h b/cmake/macosx-legacy/net/if.h new file mode 100644 index 0000000..2fca827 --- /dev/null +++ b/cmake/macosx-legacy/net/if.h @@ -0,0 +1,16 @@ +/* + * net/if.h for Mac OS X 10.4 (cmake/macosx-legacy.cmake): sys/socket.h, + * then the net/if.h of the SDK. + * + * The net/if.h of the 10.4 SDK uses struct sockaddr and does not include + * sys/socket.h. POSIX lets a program include net/if.h first, and libssh + * does. + */ +#ifndef DMODEM_MACOSX_LEGACY_NET_IF_H +#define DMODEM_MACOSX_LEGACY_NET_IF_H + +#include +#include +#include_next + +#endif diff --git a/cmake/macosx-legacy/signal.h b/cmake/macosx-legacy/signal.h new file mode 100644 index 0000000..b8b20c3 --- /dev/null +++ b/cmake/macosx-legacy/signal.h @@ -0,0 +1,24 @@ +/* + * signal.h for Mac OS X 10.4 (cmake/macosx-legacy.cmake): the signal.h + * of the SDK, with SIG_DFL, SIG_IGN, SIG_HOLD and SIG_ERR as pointers to + * a function with a prototype. + * + * Without _POSIX_C_SOURCE, the 10.4 SDK gives them as pointers to a + * function with no prototype. libssh compiles with + * -Werror=strict-prototypes, which stops at these. + */ +#ifndef DMODEM_MACOSX_LEGACY_SIGNAL_H +#define DMODEM_MACOSX_LEGACY_SIGNAL_H + +#include_next + +#undef SIG_DFL +#undef SIG_IGN +#undef SIG_HOLD +#undef SIG_ERR +#define SIG_DFL ((void (*)(int))0) +#define SIG_IGN ((void (*)(int))1) +#define SIG_HOLD ((void (*)(int))5) +#define SIG_ERR ((void (*)(int))-1) + +#endif diff --git a/cmake/macosx-legacy/time.h b/cmake/macosx-legacy/time.h new file mode 100644 index 0000000..41fbb2a --- /dev/null +++ b/cmake/macosx-legacy/time.h @@ -0,0 +1,33 @@ +/* + * time.h for Mac OS X 10.4 (cmake/macosx-legacy.cmake): the time.h of + * clang-retro MFC (port/compat/macosx) and of the SDK, and the + * reentrant functions of POSIX. + * + * The time.h of the 10.4 SDK does not declare gmtime_r, localtime_r, + * asctime_r and ctime_r when _POSIX_C_SOURCE is set. These functions are + * POSIX functions, and libSystem of 10.4 has them. Mbed TLS sets + * _POSIX_C_SOURCE and calls gmtime_r. + */ +#ifndef DMODEM_MACOSX_LEGACY_TIME_H +#define DMODEM_MACOSX_LEGACY_TIME_H + +#include_next + +#if defined(_POSIX_C_SOURCE) && !defined(_ANSI_SOURCE) +#ifdef __cplusplus +extern "C" { +#endif +/* Converts a time to UTC in the given structure. */ +struct tm *gmtime_r(const time_t *, struct tm *); +/* Converts a time to local time in the given structure. */ +struct tm *localtime_r(const time_t *, struct tm *); +/* Writes a time structure as text into the given buffer. */ +char *asctime_r(const struct tm *, char *); +/* Writes a time as text into the given buffer. */ +char *ctime_r(const time_t *, char *); +#ifdef __cplusplus +} +#endif +#endif + +#endif diff --git a/ssh/cmake/BundledLibssh.cmake b/ssh/cmake/BundledLibssh.cmake index 144991e..3f1e97d 100644 --- a/ssh/cmake/BundledLibssh.cmake +++ b/ssh/cmake/BundledLibssh.cmake @@ -28,6 +28,13 @@ endif() if(CLANG_DOS) list(APPEND _args -DCLANG_DOS=${CLANG_DOS} -DCLANG_RETRO_CONFIG=${CLANG_RETRO_CONFIG}) endif() +# (cmake/macosx-legacy.cmake) +if(MACOSX_LEGACY_TARGET) + foreach(_v MACOSX_LEGACY_ARCH MACOSX_LEGACY_LLVM MACOSX_LEGACY_SYSROOT + MACOSX_LEGACY_CCTOOLS CLANG_RETRO_MFC) + list(APPEND _args -D${_v}=${${_v}}) + endforeach() +endif() if(CMAKE_SYSTEM_NAME STREQUAL "iOS") list(APPEND _args -DCMAKE_SYSTEM_NAME=iOS) endif() diff --git a/winport/CMakeLists.txt b/winport/CMakeLists.txt index b9aa7c3..06cd40f 100644 --- a/winport/CMakeLists.txt +++ b/winport/CMakeLists.txt @@ -14,7 +14,10 @@ endif() find_package(Python3 REQUIRED COMPONENTS Interpreter) # (Homebrew's LLVM has llvm-rc; Xcode does not) find_program(DMODEM_LLVM_RC NAMES llvm-rc llvm-rc-20 llvm-rc-19 llvm-rc-18 - HINTS /opt/homebrew/opt/llvm/bin /usr/local/opt/llvm/bin REQUIRED) + HINTS ${MACOSX_LEGACY_LLVM}/bin /opt/homebrew/opt/llvm/bin /usr/local/opt/llvm/bin REQUIRED) +# The compiler alone, with no arguments (cmake/macosx-legacy.cmake gives +# the target as an argument of the compiler) +list(GET CMAKE_CXX_COMPILER 0 _winport_cxx) # The target triple of the layer's Makefile. On macOS, the # architecture is CMAKE_OSX_ARCHITECTURES when it is set, else the @@ -37,7 +40,10 @@ else() set(_arch_linux x86_64) set(_arch_apple x86_64) endif() -if(APPLE) +if(MACOSX_LEGACY_TARGET) + # Mac OS X 10.4 and later (cmake/macosx-legacy.cmake) + set(WINPORT_TARGET "${MACOSX_LEGACY_TARGET}") +elseif(APPLE) set(WINPORT_TARGET "${_arch_apple}-apple-macos11") else() set(WINPORT_TARGET "${_arch_linux}-linux-gnu") @@ -53,10 +59,21 @@ ProcessorCount(_ncpu) if(_ncpu EQUAL 0) set(_ncpu 1) endif() +if(MACOSX_LEGACY_TARGET) + # (the Makefile sets the flags of the 10.4 SDK and libc++ itself. + # winhelp_file.cpp uses std::abs with no : the libc++ of the + # sysroot does not give it through .) + set(_winport_make_args BIN=${MACOSX_LEGACY_LLVM}/bin + SDK=${MACOSX_LEGACY_SDK} LIBCXX=${MACOSX_LEGACY_SYSROOT} + CCTOOLS=${MACOSX_LEGACY_CCTOOLS}/bin/powerpc-apple-darwin8- + "CXXFLAGS=-O2 -include cstdlib") +else() + set(_winport_make_args STDLIB= CXX=${_winport_cxx} AR=${CMAKE_AR}) +endif() add_custom_command(OUTPUT "${WINPORT_LIB}" COMMAND make -C "${CLANG_RETRO_MFC}" -j${_ncpu} - TARGET=${WINPORT_TARGET} STDLIB= OUT=${WINPORT_OUT} - CXX=${CMAKE_CXX_COMPILER} AR=${CMAKE_AR} "${WINPORT_LIB}" + TARGET=${WINPORT_TARGET} OUT=${WINPORT_OUT} + ${_winport_make_args} "${WINPORT_LIB}" DEPENDS ${_winport_deps} COMMENT "Building libwinport.a (${WINPORT_TARGET})" VERBATIM) @@ -69,7 +86,7 @@ add_dependencies(winport winport_lib) set(_win "${PROJECT_SOURCE_DIR}/windows") set(APP_RES_CPP "${CMAKE_CURRENT_BINARY_DIR}/app-res.cpp") add_custom_command(OUTPUT "${APP_RES_CPP}" - COMMAND ${CMAKE_COMMAND} -E env RC=${DMODEM_LLVM_RC} CXX=${CMAKE_CXX_COMPILER} + COMMAND ${CMAKE_COMMAND} -E env RC=${DMODEM_LLVM_RC} CXX=${_winport_cxx} ${Python3_EXECUTABLE} "${CLANG_RETRO_MFC}/port/tools/res2cpp.py" -I "${_win}/src" -I "${_win}/res" "${_win}/res/app.rc" -o "${APP_RES_CPP}" DEPENDS "${_win}/res/app.rc" "${_win}/res/app.manifest" "${_win}/res/dmodem.ico" @@ -97,7 +114,11 @@ if(DMODEM_GUI_EMBED AND TARGET dmodem_embed) target_link_libraries(dmodem-win32 PRIVATE dmodem_embed) target_compile_definitions(dmodem-win32 PRIVATE DMODEM_GUI_EMBED) endif() -if(APPLE) +if(MACOSX_LEGACY_TARGET) + # (cmake/macosx-legacy.cmake adds port/compat/macosx, the C library + # functions that 10.4 does not have) + target_link_libraries(dmodem-win32 PRIVATE "-framework Cocoa" "-framework OpenGL" pthread) +elseif(APPLE) target_link_libraries(dmodem-win32 PRIVATE "-framework Cocoa" pthread) else() target_link_libraries(dmodem-win32 PRIVATE dl pthread)