swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.27k stars 10.33k forks source link

[SR-6034] Foundation build fails on FreeBSD indicating that uint64_t (from stdint.h) must be included first by Glibc module #48591

Closed swift-ci closed 5 years ago

swift-ci commented 6 years ago
Previous ID SR-6034
Radar rdar://problem/34768586
Original Reporter Rogiel (JIRA User)
Type Bug
Status Resolved
Resolution Cannot Reproduce
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, ClangImporter, FreeBSD | |Assignee | None | |Priority | Medium | md5: db94691cb055495db7eb13025b3708ee

Issue Description:

While trying to build Swift Foundation master branch, I am hitting a issue when importing \<stdint.h> from CoreFoundation header files. The Swift compiler is complaining about having to import first from the SwiftGlibc.C.pty module and then crashes with a assertion error.

<module-includes>:1:10: note: in file included from <module-includes>:1:
#include "CoreFoundation.h"
         ^
CoreFoundation.h:60:10: note: in file included from CoreFoundation.h:60:
#include <CoreFoundation/CFBase.h>
         ^
CFBase.h:88:13: error: declaration of 'uint64_t' must be imported from module 'SwiftGlibc.C.pty' before it is required
    typedef uint64_t                UInt64;
            ^
/usr/include/sys/_stdint.h:69:21: note: previous declaration is here
typedef __uint64_t              uint64_t;
                                ^
Assertion failed: (DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!"), function getDeclID, file clang/lib/Serialization/ASTWriter.cpp, line 5413.

NOTE: I have redacted the log a little bit for readability (see the full log below).

It seems that if I replace \<stdint.h> with these 4 headers, the compiler stops complaining about the UInt needing the module, but the assertion still triggers and crashes the compiler.

#include <sys/cdefs.h>
#include <sys/_types.h>
#include <machine/_stdint.h>
#include <sys/_stdint.h>

Simple hello world programs are working fine.

Error output

[6/234] CompileSwift: Foundation/ByteCountFormatter.swift
FAILED: ../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64/Foundation/Foundation/ByteCountFormatter.swift.o 
mkdir -p `dirname ../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64/Foundation/Foundation/ByteCountFormatter.swift.o`; mkdir -p ../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64; /usr/home/rogiel/Swift/build/Ninja-ReleaseAssert/swift-freebsd-x86_64/bin/swift -frontend -c Foundation/NSObject.swift Foundation/AffineTransform.swift Foundation/NSArray.swift Foundation/NSAttributedString.swift Foundation/Bundle.swift -primary-file Foundation/ByteCountFormatter.swift Foundation/NSCache.swift Foundation/NSCalendar.swift Foundation/NSCFArray.swift Foundation/NSCFBoolean.swift Foundation/NSCFDictionary.swift Foundation/NSCFSet.swift Foundation/NSCFString.swift Foundation/NSCharacterSet.swift Foundation/NSCFCharacterSet.swift Foundation/NSCoder.swift Foundation/NSComparisonPredicate.swift Foundation/NSCompoundPredicate.swift Foundation/NSConcreteValue.swift Foundation/NSData.swift Foundation/NSDate.swift Foundation/DateComponentsFormatter.swift Foundation/DateFormatter.swift Foundation/DateIntervalFormatter.swift Foundation/Decimal.swift Foundation/NSDecimalNumber.swift Foundation/NSDictionary.swift Foundation/EnergyFormatter.swift Foundation/NSEnumerator.swift Foundation/NSError.swift Foundation/NSExpression.swift Foundation/FileHandle.swift Foundation/FileManager.swift Foundation/Formatter.swift Foundation/NSGeometry.swift Foundation/Host.swift Foundation/HTTPCookie.swift Foundation/HTTPCookieStorage.swift Foundation/NSIndexPath.swift Foundation/NSIndexSet.swift Foundation/ISO8601DateFormatter.swift Foundation/JSONSerialization.swift Foundation/NSKeyedCoderOldStyleArray.swift Foundation/NSKeyedArchiver.swift Foundation/NSKeyedArchiverHelpers.swift Foundation/NSKeyedUnarchiver.swift Foundation/LengthFormatter.swift Foundation/NSLocale.swift Foundation/NSLock.swift Foundation/NSLog.swift Foundation/MassFormatter.swift Foundation/NSNotification.swift Foundation/NotificationQueue.swift Foundation/NSNull.swift Foundation/NSNumber.swift Foundation/NumberFormatter.swift Foundation/NSObjCRuntime.swift Foundation/Operation.swift Foundation/NSOrderedSet.swift Foundation/NSPathUtilities.swift Foundation/NSPersonNameComponents.swift Foundation/PersonNameComponentsFormatter.swift Foundation/NSPlatform.swift Foundation/Port.swift Foundation/PortMessage.swift Foundation/NSPredicate.swift Foundation/ProcessInfo.swift Foundation/Progress.swift Foundation/ProgressFraction.swift Foundation/PropertyListSerialization.swift Foundation/NSRange.swift Foundation/NSRegularExpression.swift Foundation/RunLoop.swift Foundation/Scanner.swift Foundation/NSSet.swift Foundation/NSSortDescriptor.swift Foundation/NSSpecialValue.swift Foundation/Stream.swift Foundation/NSString.swift Foundation/NSStringAPI.swift Foundation/NSSwiftRuntime.swift Foundation/Process.swift Foundation/NSTextCheckingResult.swift Foundation/Thread.swift Foundation/Timer.swift Foundation/NSTimeZone.swift Foundation/NSURL.swift Foundation/URLAuthenticationChallenge.swift Foundation/URLCache.swift Foundation/URLCredential.swift Foundation/URLCredentialStorage.swift Foundation/NSURLError.swift Foundation/URLProtectionSpace.swift Foundation/URLProtocol.swift Foundation/NSURLRequest.swift Foundation/URLResponse.swift Foundation/URLSession/Configuration.swift Foundation/URLSession/http/EasyHandle.swift Foundation/URLSession/http/HTTPBodySource.swift Foundation/URLSession/http/HTTPMessage.swift Foundation/URLSession/http/MultiHandle.swift Foundation/URLSession/URLSession.swift Foundation/URLSession/URLSessionConfiguration.swift Foundation/URLSession/URLSessionDelegate.swift Foundation/URLSession/URLSessionTask.swift Foundation/URLSession/TaskRegistry.swift Foundation/URLSession/http/TransferState.swift Foundation/URLSession/http/libcurlHelpers.swift Foundation/URLSession/http/HTTPURLProtocol.swift Foundation/UserDefaults.swift Foundation/NSUUID.swift Foundation/NSValue.swift Foundation/XMLDocument.swift Foundation/XMLDTD.swift Foundation/XMLDTDNode.swift Foundation/XMLElement.swift Foundation/XMLNode.swift Foundation/XMLParser.swift Foundation/FoundationErrors.swift Foundation/URL.swift Foundation/UUID.swift Foundation/Boxing.swift Foundation/ReferenceConvertible.swift Foundation/Date.swift Foundation/Data.swift Foundation/CharacterSet.swift Foundation/URLRequest.swift Foundation/PersonNameComponents.swift Foundation/Notification.swift Foundation/URLComponents.swift Foundation/DateComponents.swift Foundation/DateInterval.swift Foundation/IndexPath.swift Foundation/IndexSet.swift Foundation/StringEncodings.swift Foundation/ExtraStringAPIs.swift Foundation/Measurement.swift Foundation/NSMeasurement.swift Foundation/MeasurementFormatter.swift Foundation/Unit.swift Foundation/TimeZone.swift Foundation/Calendar.swift Foundation/Locale.swift Foundation/String.swift Foundation/Set.swift Foundation/Dictionary.swift Foundation/Array.swift Foundation/Bridging.swift Foundation/CGFloat.swift Foundation/Codable.swift Foundation/JSONEncoder.swift -I/usr/home/rogiel/Swift/build/Ninja-ReleaseAssert/swift-freebsd-x86_64/lib/swift/freebsd -Xcc -fblocks -resource-dir /usr/home/rogiel/Swift/build/Ninja-ReleaseAssert/swift-freebsd-x86_64/lib/swift -target x86_64-v11.0-freebsd11.0   -Ibootstrap/common/usr/include -Ibootstrap/common/usr/local/include   -Ibootstrap/x86_64-freebsd/usr/include -Ibootstrap/x86_64-freebsd/usr/local/include -O  -I../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64/Foundation -I../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64/Foundation/usr//lib/swift -I../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64 -DDEPLOYMENT_RUNTIME_SWIFT -I/usr/include -I../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64/Foundation/usr/lib/swift -I/usr/home/rogiel/Swift/build/Ninja-ReleaseAssert/xctest-freebsd-x86_64 -L/usr/home/rogiel/Swift/build/Ninja-ReleaseAssert/xctest-freebsd-x86_64 -I/usr/include/curl  -I/usr/include/libxml2  -DDEPLOYMENT_ENABLE_LIBDISPATCH -I/usr/home/rogiel/Swift/swift-corelibs-libdispatch -I/usr/home/rogiel/Swift/build/Ninja-ReleaseAssert/libdispatch-freebsd-x86_64/src/swift -Xcc -fblocks -swift-version 4 -module-name Foundation -module-link-name Foundation -o ../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64/Foundation/Foundation/ByteCountFormatter.swift.o -emit-module-path ../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64/Foundation/Foundation/ByteCountFormatter.swift.o.~partial.swiftmodule -emit-module-doc-path ../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64/Foundation/Foundation/ByteCountFormatter.swift.o.~partial.swiftdoc -emit-dependencies-path ../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64/Foundation/Foundation/ByteCountFormatter.swift.o.d -emit-reference-dependencies-path ../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64/Foundation/Foundation/ByteCountFormatter.swift.o.swiftdeps -module-cache-path ../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64
<module-includes>:1:10: note: in file included from <module-includes>:1:
#include "CoreFoundation.h"
         ^
/usr/home/rogiel/Swift/swift-corelibs-foundation/../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64/Foundation/usr//lib/swift/CoreFoundation/CoreFoundation.h:60:10: note: in file included from /usr/home/rogiel/Swift/swift-corelibs-foundation/../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64/Foundation/usr//lib/swift/CoreFoundation/CoreFoundation.h:60:
#include <CoreFoundation/CFBase.h>
         ^
/usr/home/rogiel/Swift/swift-corelibs-foundation/../build/Ninja-ReleaseAssert/foundation-freebsd-x86_64/Foundation/usr//lib/swift/CoreFoundation/CFBase.h:88:13: error: declaration of 'uint64_t' must be imported from module 'SwiftGlibc.C.pty' before it is required
    typedef uint64_t                UInt64;
            ^
/usr/include/sys/_stdint.h:69:21: note: previous declaration is here
typedef __uint64_t              uint64_t;
                                ^
Assertion failed: (DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!"), function getDeclID, file /usr/home/rogiel/Swift/llvm/tools/clang/lib/Serialization/ASTWriter.cpp, line 5413.
0  swift       0x0000000003d98098
1  swift       0x0000000003d987f6
2  libthr.so.3 0x0000000804f5278f pthread_sigmask + 1279
3  libthr.so.3 0x0000000804f51d6f pthread_getspecific + 3743
4  libthr.so.3 0x00007ffffffff193 pthread_getspecific + 4211794627
Stack dump:
0.  <eof> parser at end of file
ninja: build stopped: subcommand failed.

Some context of the changes I have currently to make the compiler, stdlib and Foundation to (not) build:

On swift/lib/Runtime/Config.h I have enabled SWIFT_USE_SWIFTCALL for FreeBSD systems (it looks like __has_attribute(swiftcall) is evaluating to false even though the intrinsic is available).

#ifndef SWIFT_USE_SWIFTCALL
// Clang doesn't support mangling functions with the swiftcall attribute
// on Windows and crashes during compilation: http://bugs.llvm.org/show_bug.cgi?id=32000
#if (__has_attribute(swiftcall) || defined(__linux__) || defined(__FreeBSD__)) &&  defined(_WIN32) && !defined(__FreeBSD__)
#define SWIFT_USE_SWIFTCALL 1
#else
#define SWIFT_USE_SWIFTCALL 0
#endif
#endif

On swift/stdlib/CMakeLists.txt (for SR-5779)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")

On swift/stdlib/public/SwiftShims/SwiftStdint.h & swift/stdlib/public/SwiftShims/SwiftStddef.h:

I have added a extra !defined(FreeBSD). This did not seem to make any difference on the error.

// stdint.h is provided by Clang, but it dispatches to libc's stdint.h.  As a
// result, using stdint.h here would pull in Darwin module (which includes
// libc). This creates a dependency cycle, so we can't use stdint.h in
// SwiftShims.
// On Linux, the story is different. We get the error message
// "/usr/include/x86_64-linux-gnu/sys/types.h:146:10: error: 'stddef.h' file not
// found"
// This is a known Clang/Ubuntu bug.

// Clang has been defining __INTxx_TYPE__ macros for a long time.
// __UINTxx_TYPE__ are defined only since Clang 3.5.
#if !defined(__APPLE__) && !defined(__linux__) && !defined(__FreeBSD__)
#include <stdint.h>
...
#else
...
#endif 

swift-corelibs-foundation/lib/target.py made the changes as per the patch included in the FreeBSD ports repository. The other patches no longer seem necessary.

On all swift-corelibs-foundation/Foundation/.swift* I have added a #if os(FreeBSD) where appropriate.

belkadan commented 6 years ago

This is probably a Clang issue more than a Swift issue. The fastest way to work around it might just be to include the header that it's complaining about (whichever header corresponds to SwiftGlibc.C.pty).

belkadan commented 6 years ago

@swift-ci create

swift-ci commented 6 years ago

Comment by Rogiel Sulzbach (JIRA)

Thanks @belkadan, will try this tonight.

I am now facing a another, but maybe related, issue when compiling libdispatch. In this case, I get the same assertion claiming "Declaration not emitted!" but no include error. I will try to investigate further and create another issue about it.

belkadan commented 5 years ago

A year later: our Clang folks think this is fixed in the swift-5.0-branch. Mind trying again with a development snapshot from https://swift.org/download ?

bc7072e8-7d37-4a38-8fee-cab463d443b7 commented 5 years ago

Rogiel (JIRA User), Could you verify if the problem is fixed and if so move the JIRA to "Closed"?

Thank you!