Skip to content
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

FoundationNetworking: Split networking into its own module #2289

Merged
merged 5 commits into from
May 24, 2019
Merged

FoundationNetworking: Split networking into its own module #2289

merged 5 commits into from
May 24, 2019

Conversation

millenomi
Copy link
Contributor

@millenomi millenomi commented May 22, 2019

This patch introduces a new module, FoundationNetworking, which will include all classes related to loading from remote URLs. Several types have been moved to that module, which downstream clients must import explicitly, e.g.:

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
  • Split the CFURLSessionInterface files out of Core Foundation and into their own library, libCFURLSessionInterface. This is produced by the CF CMakeLists.txt file on non-Darwin platforms. Just like CF, we strongly discourage linking to this library directly.

  • Split URLSession and related classes (except for URL and URLComponents) into a new FoundationNetworking module (SwiftFoundationNetworking on Darwin). Note:

  1. The features in Boxing.swift are needed to both modules. This file is compiled separately in both modules, and relies on a NS_BUILDING_FOUNDATION_NETWORKING compilation condition to compile correctly in FoundationNetworking

  2. SPI that was used in InputStream is now available through a public struct named _InputStreamSPIForFoundationNetworkingUseOnly for FoundationNetworking to use. This isn’t API, much like _ObjectiveCBridgeable.

Also:

  • Change some details to make tests fail without a fatal error for issues that aren’t programmer errors.

  • In some circumstances, CURL can set an immediate timeout reentrantly if the timeout callback invokes CURL hooks. Always dispatch the CURL hook call asynchronously from a request for an immediate timeout.

  • Types that have a contentsOf url: URL initializer can still accept non-file: URLs if FoundationNetworking is available in the address space of the current process (if it has been linked or dynamically loaded, for example). Loading non-file: URLs without this will result in a fatal error. Loading file: URLs will always succeed even in the absence of FoundationNetworking.

This patch introduces a new module, FoundationNetworking, which will include all classes related to loading from remote URLs. Several types have been moved to that module, which downstream clients must import explicitly, e.g.:

```
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
```

 - Split the CFURLSessionInterface files out of Core Foundation and into their own library, libCFURLSessionInterface. This is produced by the CF CMakeLists.txt file on non-Darwin platforms. Just like CF, we strongly discourage linking to this library directly.

 - Split URLSession and related classes (except for URL and URLComponents) into a new FoundationNetworking module (SwiftFoundationNetworking on Darwin). Note:

  1. The features in Boxing.swift are needed to both modules. This file is compiled separately in both modules, and relies on a NS_BUILDING_FOUNDATION_NETWORKING compilation condition to compile correctly in FoundationNetworking

  2. SPI that was used in InputStream is now available through a public struct named _InputStreamSPIForFoundationNetworkingUseOnly for FoundationNetworking to use. This isn’t API, much like _ObjectiveCBridgeable.


Also:

 - Change some details to make tests fail without a fatal error for issues that aren’t programmer errors.

 - In some circumstances, CURL can set an immediate timeout reentrantly if the timeout callback invokes CURL hooks. Always dispatch the CURL hook call asynchronously from a request for an immediate timeout. 

 - Types that have a `contentsOf url: URL` initializer can still accept non-file: URLs if FoundationNetworking is available in the address space of the current process (if it has been linked or dynamically loaded, for example). Loading non-file: URLs without this will result in a fatal error. Loading file: URLs will always succeed even in the absence of FoundationNetworking.
@millenomi
Copy link
Contributor Author

@swift-ci please test

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)
set_target_properties(CFURLSessionInterface
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't have CFCharacterSet mappings.

CMakeLists.txt Outdated
${CMAKE_C_COMPILER_TARGET}
CFLAGS
${MSVCRT_C_FLAGS}
${deployment_target}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should no longer be needed, deployment_target has been removed in favour of TARGET_OS_* from TargetConditionals.h

CMakeLists.txt Outdated
CFLAGS
${MSVCRT_C_FLAGS}
${deployment_target}
${deployment_enable_libdispatch}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that I have cleaned this up as well. This was previously needed as there was a mode for CoreFoundation to be built without libdispatch which is no longer supported.

CMakeLists.txt Outdated
${libdispatch_ldflags}
-lFoundation
${Foundation_INTERFACE_LIBRARIES}
${CFURLSessionInterface_LIBRARIES}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation seems odd

CMakeLists.txt Outdated
LINK_FLAGS
${MSVCRT_LINK_FLAGS}
-L${CMAKE_CURRENT_BINARY_DIR}
${ICU_UC_LIBRARY} ${ICU_I18N_LIBRARY}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this part actually use ICU?

CMakeLists.txt Outdated
${Foundation_RPATH}
${WORKAROUND_SR9138}
${WORKAROUND_SR9995}
$<$<PLATFORM_ID:Windows>:$<TARGET_OBJECTS:CoreFoundationResources>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need the CoreFoundationResources? That is for the TimeZone information

CMakeLists.txt Outdated
$<$<PLATFORM_ID:Windows>:$<TARGET_OBJECTS:CoreFoundationResources>>
SWIFT_FLAGS
-DDEPLOYMENT_RUNTIME_SWIFT
-DNS_BUILDING_FOUNDATION_NETWORKING
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation seems odd

CMakeLists.txt Outdated
-DDEPLOYMENT_RUNTIME_SWIFT
-DNS_BUILDING_FOUNDATION_NETWORKING
${deployment_enable_libdispatch}
-I;${ICU_INCLUDE_DIR}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need ICU here?

endif()

if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
target_compile_options(CoreFoundation
PRIVATE
-fblocks
/EHsc)
target_compile_options(CFURLSessionInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does CFURLSessionInterface use blocks? Or exceptions /EHsc enables exceptions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like these two should be a baseline for all CF code, and will keep them as-is.

endif()
if(CMAKE_SYSTEM_NAME STREQUAL Android)
target_link_libraries(CoreFoundation
PRIVATE
log)
target_link_libraries(CFURLSessionInterface
PRIVATE
log)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does CFURLSessionInterface use the android log apis?

endif()
target_link_libraries(CoreFoundation
PRIVATE
Threads::Threads
${CMAKE_DL_LIBS})
target_link_libraries(CFURLSessionInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does CFURLSessionInterface use threads and dl* functions?

if(CMAKE_SYSTEM_NAME STREQUAL Windows)
target_link_libraries(CoreFoundation
PRIVATE
AdvAPI32
Secur32
User32
mincore)
target_link_libraries(CFURLSessionInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that you probably don't need to link against most of these libraries. WS2_32 and mincore are probably all that are needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't test Windows builds at the moment; is it okay if I leave them in and you can remove them later?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds fine to me - unlike Unix, the semantics on Windows are that they will be dropped if unused anyways.

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)
set_target_properties(CFURLSessionInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This re-embeds the character set data, why?

@millenomi
Copy link
Contributor Author

Collectively responding: I replicated the existing setup for CF line-for-line. Certain things are flat-out incorrect, like the character data; certain are not useful, like icu. I'll do a pass for all the objections.

@millenomi
Copy link
Contributor Author

You are correct. Yay, sleep deprivation. Fixing.

@millenomi
Copy link
Contributor Author

@compnerd I addressed everything that I could locally check would compile.

@millenomi
Copy link
Contributor Author

@swift-ci please test

2 similar comments
@millenomi
Copy link
Contributor Author

@swift-ci please test

@millenomi
Copy link
Contributor Author

@swift-ci please test

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this seems good now.

CMakeLists.txt Outdated
uuid
CoreFoundation
$<$<PLATFORM_ID:Windows>:CoreFoundationResources>
Foundation)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is off

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed it; I'll fix it before merge.


@available(*, unavailable, message: "This type has moved to the FoundationNetworking module. Import that module to use it.",
renamed: "FoundationNetworking.CachedURLResponse")
public typealias CachedURLResponse = AnyObject
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: these should not use renamed:, because we want people to continue using the unqualified spelling to refer to them once they've imported FoundationNetworking. (That should still work without ambiguity because unavailable declarations should only be used if there's no valid available one.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're relying on the latter here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I cannot mandate a spelling be qualified or not; it is my job to make both work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(but I agree we shouldn't be recommending to use the qualified one if possible.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I'm specifically talking about renamed: because that changes the error message and creates a fix-it.

@millenomi
Copy link
Contributor Author

@swift-ci please test and merge

1 similar comment
@millenomi
Copy link
Contributor Author

@swift-ci please test and merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants