Skip to content

Introduce Cmake support for SwiftCorelibsFoundation #4959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift open source project
##
## Copyright (c) 2024 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of Swift project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

cmake_minimum_required(VERSION 3.24)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)

if(POLICY CMP0156)
# Deduplicate linked libraries where appropriate
cmake_policy(SET CMP0156 NEW)
endif()

if(POLICY CMP0157)
# New Swift build model: improved incremental build performance and LSP support
cmake_policy(SET CMP0157 NEW)
endif()

if (NOT DEFINED CMAKE_C_COMPILER)
set(CMAKE_C_COMPILER clang)
endif()

project(Foundation
LANGUAGES C Swift)

if(NOT SWIFT_SYSTEM_NAME)
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(SWIFT_SYSTEM_NAME macosx)
else()
set(SWIFT_SYSTEM_NAME "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
endif()
endif()

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)

# Fetchable dependcies
include(FetchContent)
if (_SwiftFoundationICU_SourceDIR)
FetchContent_Declare(SwiftFoundationICU
SOURCE_DIR ${_SwiftFoundationICU_SourceDIR})
else()
FetchContent_Declare(SwiftFoundationICU
GIT_REPOSITORY https://github.com/apple/swift-foundation-icu.git
GIT_TAG 0.0.8)
endif()

if (_SwiftFoundation_SourceDIR)
FetchContent_Declare(SwiftFoundation
SOURCE_DIR ${_SwiftFoundation_SourceDIR})
else()
FetchContent_Declare(SwiftFoundation
GIT_REPOSITORY https://github.com/apple/swift-foundation.git
GIT_TAG main)
endif()
FetchContent_MakeAvailable(SwiftFoundationICU SwiftFoundation)

# Precompute module triple for installation
if(NOT SwiftFoundation_MODULE_TRIPLE)
set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info)
if(CMAKE_Swift_COMPILER_TARGET)
list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET})
endif()
execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json)
string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple")
set(SwiftFoundation_MODULE_TRIPLE "${module_triple}" CACHE STRING "swift module triple used for installed swiftmodule and swiftinterface files")
mark_as_advanced(SwiftFoundation_MODULE_TRIPLE)
endif()

# System dependencies (fail fast if dependencies are missing)
find_package(LibXml2 REQUIRED)
find_package(CURL REQUIRED)
find_package(dispatch CONFIG REQUIRED)

# Common build flags (_CFURLSessionInterface, _CFXMLInterface, CoreFoundation)
list(APPEND _Foundation_common_build_flags
"-DDEPLOYMENT_RUNTIME_SWIFT"
"-DCF_BUILDING_CF"
"-DDEPLOYMENT_ENABLE_LIBDISPATCH"
"-DHAVE_STRUCT_TIMESPEC"
"-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS"
"-Wno-shorten-64-to-32"
"-Wno-deprecated-declarations"
"-Wno-unreachable-code"
"-Wno-conditional-uninitialized"
"-Wno-unused-variable"
"-Wno-unused-function"
"-Wno-microsoft-enum-forward-reference"
"-fconstant-cfstrings"
"-fexceptions" # TODO: not on OpenBSD
"-fdollars-in-identifiers"
"-fno-common"
"-fcf-runtime-abi=swift"
"-fblocks")

if(CMAKE_BUILD_TYPE STREQUAL Debug)
list(APPEND _Foundation_common_build_flags
"-DDEBUG")
endif()

# Swift build flags (Foundation, FoundationNetworking, FoundationXML)
set(_Foundation_swift_build_flags)
list(APPEND _Foundation_swift_build_flags
"-DDEPLOYMENT_RUNTIME_SWIFT"
"-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS")

if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android")
list(APPEND _Foundation_common_build_flags
"-D_GNU_SOURCE"
"-I/usr/lib/swift") # dispatch
endif()

include(FoundationSwiftSupport)

add_subdirectory(Sources)
add_subdirectory(cmake/modules)
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ let package = Package(
dependencies: [
.package(
url: "https://github.com/apple/swift-foundation-icu",
from: "0.0.5"
from: "0.0.7"
),
.package(
url: "https://github.com/apple/swift-foundation",
Expand Down Expand Up @@ -121,7 +121,7 @@ let package = Package(
.target(
name: "_CoreFoundation",
dependencies: [
.product(name: "FoundationICU", package: "swift-foundation-icu"),
.product(name: "_FoundationICU", package: "swift-foundation-icu"),
],
path: "Sources/CoreFoundation",
cSettings: coreFoundationBuildSettings
Expand Down
20 changes: 20 additions & 0 deletions Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift open source project
##
## Copyright (c) 2024 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of Swift project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

add_subdirectory(CoreFoundation)
add_subdirectory(_CFXMLInterface)
add_subdirectory(_CFURLSessionInterface)
add_subdirectory(Foundation)
add_subdirectory(FoundationXML)
add_subdirectory(FoundationNetworking)
4 changes: 2 additions & 2 deletions Sources/CoreFoundation/CFBundle_Locale.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
#include "CFPreferences.h"

#if __HAS_APPLE_ICU__
#include <unicode/ualoc.h>
#include <_foundation_unicode/ualoc.h>
#endif
#include <unicode/uloc.h>
#include <_foundation_unicode/uloc.h>
#include <ctype.h>

static CFStringRef _CFBundleCopyLanguageFoundInLocalizations(CFArrayRef localizations, CFStringRef language);
Expand Down
4 changes: 2 additions & 2 deletions Sources/CoreFoundation/CFCharacterSet.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#include <stdlib.h>
#include <string.h>
#include "CFPriv.h"
#include <unicode/uchar.h>
#include <unicode/uset.h>
#include <_foundation_unicode/uchar.h>
#include <_foundation_unicode/uset.h>

#define BITSPERBYTE 8 /* (CHAR_BIT * sizeof(unsigned char)) */
#define LOG_BPB 3
Expand Down
2 changes: 1 addition & 1 deletion Sources/CoreFoundation/CFDateIntervalFormatter.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "CFLocale.h"
#include "CFTimeZone.h"

#include <unicode/udateintervalformat.h>
#include <_foundation_unicode/udateintervalformat.h>

#if TARGET_OS_WASI
#define LOCK() do {} while (0)
Expand Down
4 changes: 2 additions & 2 deletions Sources/CoreFoundation/CFICUConverters.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "CFICUConverters.h"
#include "CFStringEncodingExt.h"
#include "CFUniChar.h"
#include <unicode/ucnv.h>
#include <unicode/uversion.h>
#include <_foundation_unicode/ucnv.h>
#include <_foundation_unicode/uversion.h>
#include "CFInternal.h"
#include <stdio.h>

Expand Down
26 changes: 13 additions & 13 deletions Sources/CoreFoundation/CFLocale.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
#include "CFLocaleInternal.h"
#include <stdatomic.h>
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
#include <unicode/uloc.h> // ICU locales
#include <unicode/ulocdata.h> // ICU locale data
#include <unicode/ucal.h>
#include <unicode/ucurr.h> // ICU currency functions
#include <unicode/uset.h> // ICU Unicode sets
#include <unicode/putil.h> // ICU low-level utilities
#include <unicode/umsg.h> // ICU message formatting
#include <unicode/ucol.h>
#include <unicode/unumsys.h> // ICU numbering systems
#include <unicode/uvernum.h>
#if U_ICU_VERSION_MAJOR_NUM > 53 && __has_include(<unicode/uameasureformat.h>)
#include <unicode/uameasureformat.h>
#include <_foundation_unicode/uloc.h> // ICU locales
#include <_foundation_unicode/ulocdata.h> // ICU locale data
#include <_foundation_unicode/ucal.h>
#include <_foundation_unicode/ucurr.h> // ICU currency functions
#include <_foundation_unicode/uset.h> // ICU Unicode sets
#include <_foundation_unicode/putil.h> // ICU low-level utilities
#include <_foundation_unicode/umsg.h> // ICU message formatting
#include <_foundation_unicode/ucol.h>
#include <_foundation_unicode/unumsys.h> // ICU numbering systems
#include <_foundation_unicode/uvernum.h>
#if U_ICU_VERSION_MAJOR_NUM > 53 && __has_include(<_foundation_unicode/uameasureformat.h>)
#include <_foundation_unicode/uameasureformat.h>

extern int32_t
uameasfmt_getUnitsForUsage( const char* locale,
Expand Down Expand Up @@ -1745,7 +1745,7 @@ static bool __CFLocaleCopyTemperatureUnit(CFLocaleRef locale, bool user, CFTypeR
if (!done) {
char localeID[ULOC_FULLNAME_CAPACITY+ULOC_KEYWORD_AND_VALUES_CAPACITY];
if (CFStringGetCString(locale->_identifier, localeID, sizeof(localeID)/sizeof(char), kCFStringEncodingASCII)) {
#if U_ICU_VERSION_MAJOR_NUM > 53 && __has_include(<unicode/uameasureformat.h>)
#if U_ICU_VERSION_MAJOR_NUM > 53 && __has_include(<_foundation_unicode/uameasureformat.h>)
UErrorCode icuStatus = U_ZERO_ERROR;
UAMeasureUnit unit;
int32_t unitCount = uameasfmt_getUnitsForUsage(localeID, "temperature", "weather", &unit, 1, &icuStatus);
Expand Down
2 changes: 1 addition & 1 deletion Sources/CoreFoundation/CFLocaleIdentifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#include <stdlib.h>
#include <stdio.h>
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
#include <unicode/uloc.h>
#include <_foundation_unicode/uloc.h>
#else
#define ULOC_KEYWORD_SEPARATOR '@'
#define ULOC_FULLNAME_CAPACITY 56
Expand Down
2 changes: 1 addition & 1 deletion Sources/CoreFoundation/CFRegularExpression.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "CFInternal.h"
#define U_SHOW_DRAFT_API 1
#define U_SHOW_INTERNAL_API 1
#include <unicode/uregex.h>
#include <_foundation_unicode/uregex.h>

#define STACK_BUFFER_SIZE 256

Expand Down
2 changes: 1 addition & 1 deletion Sources/CoreFoundation/CFString.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "CFString_Internal.h"
#include "CFRuntime_Internal.h"
#include <assert.h>
#include <unicode/uchar.h>
#include <_foundation_unicode/uchar.h>
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD
#include "CFConstantKeys.h"
#include "CFStringLocalizedFormattingInternal.h"
Expand Down
2 changes: 1 addition & 1 deletion Sources/CoreFoundation/CFStringTransform.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "CFUniChar.h"
#include "CFPriv.h"
#include "CFInternal.h"
#include <unicode/utrans.h>
#include <_foundation_unicode/utrans.h>

static const char *__CFStringTransformGetICUIdentifier(CFStringRef identifier);

Expand Down
4 changes: 2 additions & 2 deletions Sources/CoreFoundation/CFStringUtilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include <limits.h>
#include <stdlib.h>
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_WASI
#include <unicode/ucol.h>
#include <unicode/ucoleitr.h>
#include <_foundation_unicode/ucol.h>
#include <_foundation_unicode/ucoleitr.h>
#endif
#include <string.h>

Expand Down
6 changes: 3 additions & 3 deletions Sources/CoreFoundation/CFTimeZone.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unicode/ucal.h>
#include <unicode/udat.h>
#include <unicode/ustring.h>
#include <_foundation_unicode/ucal.h>
#include <_foundation_unicode/udat.h>
#include <_foundation_unicode/ustring.h>
#include "CFDateFormatter.h"
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
#include <dirent.h>
Expand Down
Loading