From aeb53a823f21caf9bbe4c30773e343ab95ff8d81 Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Thu, 29 Jun 2023 20:33:10 -0400 Subject: [PATCH] swift-corelibs: switch build system to cmake Switching the build system to cmake makes it easier to make changes to the build (particularly which dependencies to link). It also removes a lot of manual build steps and fixes the issue identified by @emilazy in NixOS#238791. Fixes NixOS#238791. --- .../0004-Fix-Darwin-cmake-build.patch | 66 +++++++++++++++ ...0005-Fix-framework-installation-path.patch | 23 +++++ ...06-System-CF-framework-compatibility.patch | 84 +++++++++++++++++++ .../darwin/swift-corelibs/corefoundation.nix | 82 ++++++------------ 4 files changed, 197 insertions(+), 58 deletions(-) create mode 100644 pkgs/os-specific/darwin/swift-corelibs/0004-Fix-Darwin-cmake-build.patch create mode 100644 pkgs/os-specific/darwin/swift-corelibs/0005-Fix-framework-installation-path.patch create mode 100644 pkgs/os-specific/darwin/swift-corelibs/0006-System-CF-framework-compatibility.patch diff --git a/pkgs/os-specific/darwin/swift-corelibs/0004-Fix-Darwin-cmake-build.patch b/pkgs/os-specific/darwin/swift-corelibs/0004-Fix-Darwin-cmake-build.patch new file mode 100644 index 000000000000..afffa1abc8e0 --- /dev/null +++ b/pkgs/os-specific/darwin/swift-corelibs/0004-Fix-Darwin-cmake-build.patch @@ -0,0 +1,66 @@ +--- a/CoreFoundation/CMakeLists.txt 1969-12-31 19:00:01.000000000 -0500 ++++ b/CoreFoundation/CMakeLists.txt 2023-06-29 18:52:49.096019700 -0400 +@@ -129,7 +129,7 @@ + Base.subproj/CFByteOrder.h + Base.subproj/CFUUID.h + Base.subproj/CFUtilities.h +- Base.subproj/SwiftRuntime/CoreFoundation.h ++ Base.subproj/CoreFoundation.h # The SwiftRuntime version of this file causes linker errors and is not correct for standalone CF. + Base.subproj/SwiftRuntime/TargetConditionals.h + # Collections + Collections.subproj/CFArray.h +@@ -245,6 +245,8 @@ + # RunLoop + RunLoop.subproj/CFRunLoop.c + RunLoop.subproj/CFSocket.c ++ RunLoop.subproj/CFMachPort.c # These files are missing from the upstream `CMakeLists.txt` but required to build on Darwin. ++ RunLoop.subproj/CFMessagePort.c + # Stream + Stream.subproj/CFConcreteStreams.c + Stream.subproj/CFSocketStream.c +@@ -336,6 +338,11 @@ + target_include_directories(CoreFoundation + PRIVATE + ${CURL_INCLUDE_DIRS}) ++elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin) ++ find_package(CURL REQUIRED) ++ target_include_directories(CoreFoundation PRIVATE ${CURL_INCLUDE_DIRS}) ++ find_package(LibXml2 REQUIRED) ++ target_include_directories(CoreFoundation PRIVATE ${LIBXML2_INCLUDE_DIR}) + else() + target_include_directories(CoreFoundation + PRIVATE +@@ -365,6 +372,10 @@ + PRIVATE + ${CURL_LIBRARIES} + ${LIBXML2_LIBRARIES}) ++elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin) ++ target_link_libraries(CoreFoundation PRIVATE ++ ${CURL_LIBRARIES} ++ ${LIBXML2_LIBRARIES}) + else() + target_link_libraries(CoreFoundation + PRIVATE +@@ -398,9 +400,19 @@ + target_link_libraries(CoreFoundation + PRIVATE + icucore) +- set_target_properties(CoreFoundation +- PROPERTIES LINK_FLAGS +- -Xlinker;-alias_list;-Xlinker;Base.subproj/DarwinSymbolAliases;-twolevel_namespace;-sectcreate;__UNICODE;__csbitmaps;CharacterSets/CFCharacterSetBitmaps.bitmap;-sectcreate;__UNICODE;__properties;CharacterSets/CFUniCharPropertyDatabase.data;-sectcreate;__UNICODE;__data;CharacterSets/CFUnicodeData-L.mapping;-segprot;__UNICODE;r;r) ++ target_link_options(CoreFoundation ++ PUBLIC ++ "LINKER:-alias_list,../Base.subproj/DarwinSymbolAliases" ++ "LINKER:-twolevel_namespace" ++ "LINKER:-sectcreate,__UNICODE,__csbitmaps,../CharacterSets/CFCharacterSetBitmaps.bitmap" ++ "LINKER:-sectcreate,__UNICODE,__properties,../CharacterSets/CFUniCharPropertyDatabase.data" ++ "LINKER:-sectcreate,__UNICODE,__data,../CharacterSets/CFUnicodeData-L.mapping" ++ "LINKER:-segprot,__UNICODE,r,r" ++ "LINKER:-current_version,1454.90.0" ++ "LINKER:-compatibility_version,150.0.0" ++ "LINKER:-init,___CFInitialize") ++ set(CMAKE_SHARED_LIBRARY_PREFIX "") ++ set(CMAKE_SHARED_LIBRARY_SUFFIX "") + endif() + + install(TARGETS diff --git a/pkgs/os-specific/darwin/swift-corelibs/0005-Fix-framework-installation-path.patch b/pkgs/os-specific/darwin/swift-corelibs/0005-Fix-framework-installation-path.patch new file mode 100644 index 000000000000..e771ab3c66f2 --- /dev/null +++ b/pkgs/os-specific/darwin/swift-corelibs/0005-Fix-framework-installation-path.patch @@ -0,0 +1,23 @@ +diff -u aa/CoreFoundation/CMakeLists.txt b/CoreFoundation/CMakeLists.txt +--- a/CoreFoundation/CMakeLists.txt 1969-12-31 19:00:01.000000000 -0500 ++++ b/CoreFoundation/CMakeLists.txt 2023-06-29 18:59:19.492601179 -0400 + +@@ -424,16 +424,11 @@ + set(CMAKE_SHARED_LIBRARY_SUFFIX "") + endif() + +-install(TARGETS +- CoreFoundation +- DESTINATION +- "${CMAKE_INSTALL_FULL_LIBDIR}") + install(DIRECTORY + ${CoreFoundation_FRAMEWORK_DIRECTORY} + DESTINATION +- ${CMAKE_INSTALL_PREFIX}/System/Library/Frameworks +- USE_SOURCE_PERMISSIONS +- PATTERN PrivateHeaders EXCLUDE) ++ ${CMAKE_INSTALL_PREFIX}/Library/Frameworks ++ USE_SOURCE_PERMISSIONS) + + + # TODO(compnerd) formalize this diff --git a/pkgs/os-specific/darwin/swift-corelibs/0006-System-CF-framework-compatibility.patch b/pkgs/os-specific/darwin/swift-corelibs/0006-System-CF-framework-compatibility.patch new file mode 100644 index 000000000000..248cb5f60037 --- /dev/null +++ b/pkgs/os-specific/darwin/swift-corelibs/0006-System-CF-framework-compatibility.patch @@ -0,0 +1,84 @@ +diff -u a/CoreFoundation/CMakeLists.txt b/CoreFoundation/CMakeLists.txt +--- a/CoreFoundation/CMakeLists.txt 1969-12-31 19:00:01.000000000 -0500 ++++ b/CoreFoundation/CMakeLists.txt 2023-06-29 18:59:08.659632504 -0400 +@@ -1,5 +1,5 @@ + +-cmake_minimum_required(VERSION 3.4.3) ++cmake_minimum_required(VERSION 3.14) + list(APPEND CMAKE_MODULE_PATH + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") + +@@ -45,6 +45,8 @@ + ${FRAMEWORK_LIBRARY_TYPE} + FRAMEWORK_DIRECTORY + CoreFoundation_FRAMEWORK_DIRECTORY ++ VERSION ++ A + MODULE_MAP + Base.subproj/module.modulemap + PRIVATE_HEADERS +diff -u a/CoreFoundation/cmake/modules/CoreFoundationAddFramework.cmake b/CoreFoundation/cmake/modules/CoreFoundationAddFramework.cmake +--- a/CoreFoundation/cmake/modules/CoreFoundationAddFramework.cmake 1969-12-31 19:00:01.000000000 -0500 ++++ b/CoreFoundation/cmake/modules/CoreFoundationAddFramework.cmake 2023-06-29 18:57:55.792860996 -0400 +@@ -3,7 +3,7 @@ + + function(add_framework NAME) + set(options STATIC SHARED) +- set(single_value_args MODULE_MAP FRAMEWORK_DIRECTORY) ++ set(single_value_args MODULE_MAP FRAMEWORK_DIRECTORY VERSION) + set(multiple_value_args PRIVATE_HEADERS PUBLIC_HEADERS SOURCES) + cmake_parse_arguments(AF "${options}" "${single_value_args}" "${multiple_value_args}" ${ARGN}) + +@@ -14,26 +14,32 @@ + set(AF_TYPE SHARED) + endif() + ++ file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/${NAME}.framework/Versions/${AF_VERSION}) ++ file(CREATE_LINK ${AF_VERSION} ${CMAKE_BINARY_DIR}/${NAME}.framework/Versions/Current SYMBOLIC) ++ + if(AF_MODULE_MAP) + file(COPY + ${AF_MODULE_MAP} + DESTINATION +- ${CMAKE_BINARY_DIR}/${NAME}.framework/Modules ++ ${CMAKE_BINARY_DIR}/${NAME}.framework/Versions/Current/Modules + NO_SOURCE_PERMISSIONS) ++ file(CREATE_LINK Versions/Current/Modules ${CMAKE_BINARY_DIR}/${NAME}.framework/Modules SYMBOLIC) + endif() + if(AF_PUBLIC_HEADERS) + file(COPY + ${AF_PUBLIC_HEADERS} + DESTINATION +- ${CMAKE_BINARY_DIR}/${NAME}.framework/Headers ++ ${CMAKE_BINARY_DIR}/${NAME}.framework/Versions/Current/Headers + NO_SOURCE_PERMISSIONS) ++ file(CREATE_LINK Versions/Current/Headers ${CMAKE_BINARY_DIR}/${NAME}.framework/Headers SYMBOLIC) + endif() + if(AF_PRIVATE_HEADERS) + file(COPY + ${AF_PRIVATE_HEADERS} + DESTINATION +- ${CMAKE_BINARY_DIR}/${NAME}.framework/PrivateHeaders ++ ${CMAKE_BINARY_DIR}/${NAME}.framework/Versions/Current/PrivateHeaders + NO_SOURCE_PERMISSIONS) ++ file(CREATE_LINK Versions/Current/PrivateHeaders ${CMAKE_BINARY_DIR}/${NAME}.framework/PrivateHeaders SYMBOLIC) + endif() + add_custom_target(${NAME}_POPULATE_HEADERS + DEPENDS +@@ -51,13 +57,15 @@ + set_target_properties(${NAME} + PROPERTIES + LIBRARY_OUTPUT_DIRECTORY +- ${CMAKE_BINARY_DIR}/${NAME}.framework) ++ ${CMAKE_BINARY_DIR}/${NAME}.framework/Versions/Current) + target_compile_options(${NAME} + PRIVATE + -F;${CMAKE_BINARY_DIR} + -I;${CMAKE_BINARY_DIR}/${NAME}.framework/PrivateHeaders) + add_dependencies(${NAME} ${NAME}_POPULATE_HEADERS) + ++ file(CREATE_LINK Versions/Current/${NAME} ${CMAKE_BINARY_DIR}/${NAME}.framework/${NAME} SYMBOLIC) ++ + if(AF_FRAMEWORK_DIRECTORY) + set(${AF_FRAMEWORK_DIRECTORY} ${CMAKE_BINARY_DIR}/${NAME}.framework PARENT_SCOPE) + endif() diff --git a/pkgs/os-specific/darwin/swift-corelibs/corefoundation.nix b/pkgs/os-specific/darwin/swift-corelibs/corefoundation.nix index 3db9073ad554..4b99e75aad70 100644 --- a/pkgs/os-specific/darwin/swift-corelibs/corefoundation.nix +++ b/pkgs/os-specific/darwin/swift-corelibs/corefoundation.nix @@ -1,4 +1,4 @@ -{ lib, stdenv, fetchFromGitHub, fetchurl, makeSetupHook, ninja, launchd, libdispatch, python3, curl, libxml2, objc4, ICU }: +{ lib, stdenv, fetchFromGitHub, fetchurl, makeSetupHook, cmake, ninja, pkg-config, launchd, libdispatch, python3, libxml2, objc4, icu }: let # 10.12 adds a new sysdir.h that our version of CF in the main derivation depends on, but @@ -21,8 +21,8 @@ stdenv.mkDerivation { sha256 = "17kpql0f27xxz4jjw84vpas5f5sn4vdqwv10g151rc3rswbwln1z"; }; - nativeBuildInputs = [ ninja python3 ]; - buildInputs = [ (lib.getDev launchd) libdispatch libxml2 objc4 ICU ]; + nativeBuildInputs = [ cmake ninja pkg-config python3 ]; + buildInputs = [ (lib.getDev launchd) libdispatch libxml2 objc4 icu ]; patches = [ ./0001-Add-missing-TARGET_OS_-defines.patch @@ -30,6 +30,14 @@ stdenv.mkDerivation { ./0002-Add-missing-launchd-header.patch # CFURLComponents fails to build with clang 16 due to an invalid pointer conversion. This is fixed upstream. ./0003-Fix-incompatible-pointer-conversion.patch + # Fix `CMakeLists.txt` to allow it to be used instead of `build.py` to build on Darwin. + ./0004-Fix-Darwin-cmake-build.patch + # Install CF framework in `$out/Library/Frameworks` instead of `$out/System/Frameworks`. + ./0005-Fix-framework-installation-path.patch + # Build a framework that matches the contents of the system CoreFoundation. This patch adds + # versioning and drops the prefix and suffix, so the dynamic library is named `CoreFoundation` + # instead of `libCoreFoundation.dylib`. + ./0006-System-CF-framework-compatibility.patch ]; postPatch = '' @@ -37,19 +45,6 @@ stdenv.mkDerivation { cp ${sysdir-free-system-directories} Base.subproj/CFSystemDirectories.c - # In order, since I can't comment individual lines: - # 1. Disable dispatch support for now - # 2. For the linker too - # 3. Use the legit CoreFoundation.h, not the one telling you not to use it because of Swift - substituteInPlace build.py \ - --replace "cf.CFLAGS += '-DDEPLOYMENT" '#' \ - --replace "cf.LDFLAGS += '-ldispatch" '#' - - # Fix sandbox impurities. - substituteInPlace ../lib/script.py \ - --replace '/bin/cp' cp - patchShebangs --build ../configure - # Includes xpc for some initialization routine that they don't define anyway, so no harm here substituteInPlace PlugIn.subproj/CFBundlePriv.h \ --replace '#if (TARGET_OS_MAC' '#if (0' @@ -61,54 +56,25 @@ stdenv.mkDerivation { # The MIN macro doesn't seem to be defined sensibly for us. Not sure if our stdenv or their bug substituteInPlace Base.subproj/CoreFoundation_Prefix.h \ --replace '#if DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX' '#if 1' - - # Somehow our ICU doesn't have this, probably because it's too old (we'll update it soon when we update the rest of the SDK) - substituteInPlace Locale.subproj/CFLocale.c \ - --replace '#if U_ICU_VERSION_MAJOR_NUM' '#if 0 //' ''; - BUILD_DIR = "./Build"; - CFLAGS = "-DINCLUDE_OBJC -I${libxml2.dev}/include/libxml2"; # They seem to assume we include objc in some places and not in others, make a PR; also not sure why but libxml2 include path isn't getting picked up from buildInputs + env.NIX_CFLAGS_COMPILE = toString [ + # Silence warnings regarding other targets + "-Wno-error=undef-prefix" + # Avoid redefinitions when including objc headers + "-DINCLUDE_OBJC=1" + ]; - # I'm guessing at the version here. https://github.com/apple/swift-corelibs-foundation/commit/df3ec55fe6c162d590a7653d89ad669c2b9716b1 imported "high sierra" - # and this version is a version from there. No idea how accurate it is. - LDFLAGS = "-current_version 1454.90.0 -compatibility_version 150.0.0 -init ___CFInitialize"; - - configurePhase = '' - ../configure release --sysroot UNUSED - ''; + cmakeFlags = [ + "-DBUILD_SHARED_LIBS=ON" + "-DCF_ENABLE_LIBDISPATCH=OFF" + ]; enableParallelBuilding = true; - buildPhase = '' - runHook preBuild - - ninja -j $NIX_BUILD_CORES - - runHook postBuild - ''; - - # TODO: their build system sorta kinda can do this, but it doesn't seem to work right now - # Also, this includes a bunch of private headers in the framework, which is not what we want - installPhase = '' - base="$out/Library/Frameworks/CoreFoundation.framework" - mkdir -p $base/Versions/A/{Headers,PrivateHeaders,Modules} - - cp ./Build/CoreFoundation/libCoreFoundation.dylib $base/Versions/A/CoreFoundation - - # Note that this could easily live in the ldflags above as `-install_name @rpath/...` but - # https://github.com/NixOS/nixpkgs/issues/46434 thwarts that, so for now I'm hacking it up - # after the fact. - install_name_tool -id '@rpath/CoreFoundation.framework/Versions/A/CoreFoundation' $base/Versions/A/CoreFoundation - - cp ./Build/CoreFoundation/usr/include/CoreFoundation/*.h $base/Versions/A/Headers - cp ./Build/CoreFoundation/usr/include/CoreFoundation/module.modulemap $base/Versions/A/Modules - - ln -s A $base/Versions/Current - - for i in CoreFoundation Headers Modules; do - ln -s Versions/Current/$i $base/$i - done + postInstall = '' + install_name_tool -id '@rpath/CoreFoundation.framework/Versions/A/CoreFoundation' \ + "$out/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation" ''; darwinEnvHook = makeSetupHook { name = "darwin-env-hook"; } ./pure-corefoundation-hook.sh;