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

Add Linux support to SwiftDate #568

Closed
icyield opened this issue Aug 6, 2018 · 15 comments
Closed

Add Linux support to SwiftDate #568

icyield opened this issue Aug 6, 2018 · 15 comments
Assignees
Milestone

Comments

@icyield
Copy link

icyield commented Aug 6, 2018

5.0.5 does not compile under Linux. Multiple issues objective C runtimes, arc_random etc.

@mikecsh
Copy link

mikecsh commented Aug 25, 2018

Is there any specific version known to compile under Linux?

@malcommac malcommac changed the title Linux no longer supported. Restoring Linux support Sep 18, 2018
@malcommac
Copy link
Owner

Is there an easy way to test it? A container?

@malcommac malcommac self-assigned this Sep 18, 2018
@malcommac malcommac added the bug label Sep 18, 2018
@vzsg
Copy link

vzsg commented Sep 19, 2018

Sure there is. Install Docker for Mac CE, and run this from your project folder:

docker run --rm -it -w /app -v `pwd`:/app swift:4.2 swift build

After a cursory look (a friend is looking for working date arithmetics on Linux and found this project, I'm just helping out), the project doesn't build because there are multiple Darwin-only references in it, plus the unit tests are not set up correctly either.

(Travis can be potentially tamed to do Linux testing too, once the source and the tests are up to speed. As a side note, I've had more luck with CircleCI 2.0 for Swift + Linux.)

@malcommac
Copy link
Owner

Did you know if this error is referred to the git repository or...what?

danielembp15:SwiftDate daniele$ docker run --rm -it -w /app -v `pwd`:/app swift:4.2 swift build
docker: invalid reference format: repository name must be lowercase.
See 'docker run --help'.
danielembp15:SwiftDate daniele$ 

@vzsg
Copy link

vzsg commented Oct 27, 2018

Do you have any spaces in your current working directory path? My gut feeling says that it might cause trouble with the backticks.

@malcommac
Copy link
Owner

Yeah it was related to spaces in path. With iCloud it's a damn mess. Thanks.

@malcommac
Copy link
Owner

Related commit: #611 Removed arc4random_uniform to use new Swift 4.2 random APIs.

@malcommac
Copy link
Owner

This is the opening issues on Linux, strictly related to CFCalendar and CFCalendarUnit which should be available on CoreFoundation for Linux.
It's a bit strange.

danielembp15:SwiftDate daniele$ docker run --rm -it -w /app -v `pwd`:/app swift:4.2 swift build
Compile Swift Module 'SwiftDate' (158 sources)
/app/Sources/SwiftDate/Supports/Commons.swift:179:21: error: use of undeclared type 'CFCalendar'
        typealias CFType = CFCalendar
                           ^~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:226:25: error: use of undeclared type 'CFCalendarUnit'
        internal var _cfValue: CFCalendarUnit? {
                               ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:208:14: error: use of undeclared type 'CFAbsoluteTime'
                var start: CFAbsoluteTime = 0.0
                           ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:209:11: error: use of undeclared type 'CFTimeInterval'
                var ti: CFTimeInterval = 0.0
                        ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:212:12: error: use of unresolved identifier 'CFCalendarGetTimeRangeOfUnit'
                                return CFCalendarGetTimeRangeOfUnit(_cfObject, cfValue, date.timeIntervalSinceReferenceDate, startp, tip)
                                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:243:24: error: use of unresolved identifier 'CFCalendarUnit'
                case .day:                              return CFCalendarUnit.day.rawValue
                                                               ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:244:24: error: use of unresolved identifier 'CFCalendarUnit'
                case .era:                              return CFCalendarUnit.era.rawValue
                                                               ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:245:25: error: use of unresolved identifier 'CFCalendarUnit'
                case .year:                             return CFCalendarUnit.year.rawValue
                                                               ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:246:25: error: use of unresolved identifier 'CFCalendarUnit'
                case .month:                    return CFCalendarUnit.month.rawValue
                                                       ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:247:25: error: use of unresolved identifier 'CFCalendarUnit'
                case .hour:                             return CFCalendarUnit.hour.rawValue
                                                               ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:248:26: error: use of unresolved identifier 'CFCalendarUnit'
                case .minute:                   return CFCalendarUnit.minute.rawValue
                                                       ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:249:26: error: use of unresolved identifier 'CFCalendarUnit'
                case .second:                   return CFCalendarUnit.second.rawValue
                                                       ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:250:27: error: use of unresolved identifier 'CFCalendarUnit'
                case .weekday:                  return CFCalendarUnit.weekday.rawValue
                                                       ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:251:32: error: use of unresolved identifier 'CFCalendarUnit'
                case .weekdayOrdinal:   return CFCalendarUnit.weekdayOrdinal.rawValue
                                               ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:252:27: error: use of unresolved identifier 'CFCalendarUnit'
                case .quarter:                  return CFCalendarUnit.quarter.rawValue
                                                       ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:253:30: error: use of unresolved identifier 'CFCalendarUnit'
                case .weekOfMonth:              return CFCalendarUnit.weekOfMonth.rawValue
                                                       ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:254:29: error: use of unresolved identifier 'CFCalendarUnit'
                case .weekOfYear:               return CFCalendarUnit.weekOfYear.rawValue
                                                       ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:255:34: error: use of unresolved identifier 'CFCalendarUnit'
                case .yearForWeekOfYear:return CFCalendarUnit.yearForWeekOfYear.rawValue
                                               ^~~~~~~~~~~~~~
/app/Sources/SwiftDate/Date/Date.swift:19:1: error: type 'Date' does not conform to protocol 'DateRepresentable'
extension Date: DateRepresentable {
^
/app/Sources/SwiftDate/DateRepresentable.swift:171:6: note: protocol requires property 'customFormatter' with type 'DateFormatter?'; do you want to add a stub?
        var customFormatter: DateFormatter? { get set }
            ^
error: terminated(1): /usr/bin/swift-build-tool -f /app/.build/debug.yaml main output:

@malcommac
Copy link
Owner

malcommac commented Oct 27, 2018

Still remains only the definitions inside the CFCalendarUnit enum.

danielembp15:SwiftDate daniele$ docker run --rm -it -w /app -v `pwd`:/app swift:4.2 swift build
Compile Swift Module 'SwiftDate' (158 sources)
/app/Sources/SwiftDate/Supports/Commons.swift:246:24: error: type 'CFCalendarUnit' (aka 'UInt') has no member 'day'
                case .day:                              return CFCalendarUnit.day.rawValue
                                                               ^~~~~~~~~~~~~~ ~~~
/app/Sources/SwiftDate/Supports/Commons.swift:247:24: error: type 'CFCalendarUnit' (aka 'UInt') has no member 'era'
                case .era:                              return CFCalendarUnit.era.rawValue
                                                               ^~~~~~~~~~~~~~ ~~~
/app/Sources/SwiftDate/Supports/Commons.swift:248:25: error: type 'CFCalendarUnit' (aka 'UInt') has no member 'year'
                case .year:                             return CFCalendarUnit.year.rawValue
                                                               ^~~~~~~~~~~~~~ ~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:249:25: error: type 'CFCalendarUnit' (aka 'UInt') has no member 'month'
                case .month:                    return CFCalendarUnit.month.rawValue
                                                       ^~~~~~~~~~~~~~ ~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:250:25: error: type 'CFCalendarUnit' (aka 'UInt') has no member 'hour'
                case .hour:                             return CFCalendarUnit.hour.rawValue
                                                               ^~~~~~~~~~~~~~ ~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:251:26: error: type 'CFCalendarUnit' (aka 'UInt') has no member 'minute'
                case .minute:                   return CFCalendarUnit.minute.rawValue
                                                       ^~~~~~~~~~~~~~ ~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:252:26: error: type 'CFCalendarUnit' (aka 'UInt') has no member 'second'
                case .second:                   return CFCalendarUnit.second.rawValue
                                                       ^~~~~~~~~~~~~~ ~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:253:27: error: type 'CFCalendarUnit' (aka 'UInt') has no member 'weekday'
                case .weekday:                  return CFCalendarUnit.weekday.rawValue
                                                       ^~~~~~~~~~~~~~ ~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:254:32: error: type 'CFCalendarUnit' (aka 'UInt') has no member 'weekdayOrdinal'
                case .weekdayOrdinal:   return CFCalendarUnit.weekdayOrdinal.rawValue
                                               ^~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:255:27: error: type 'CFCalendarUnit' (aka 'UInt') has no member 'quarter'
                case .quarter:                  return CFCalendarUnit.quarter.rawValue
                                                       ^~~~~~~~~~~~~~ ~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:256:30: error: type 'CFCalendarUnit' (aka 'UInt') has no member 'weekOfMonth'
                case .weekOfMonth:              return CFCalendarUnit.weekOfMonth.rawValue
                                                       ^~~~~~~~~~~~~~ ~~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:257:29: error: type 'CFCalendarUnit' (aka 'UInt') has no member 'weekOfYear'
                case .weekOfYear:               return CFCalendarUnit.weekOfYear.rawValue
                                                       ^~~~~~~~~~~~~~ ~~~~~~~~~~
/app/Sources/SwiftDate/Supports/Commons.swift:258:34: error: type 'CFCalendarUnit' (aka 'UInt') has no member 'yearForWeekOfYear'
                case .yearForWeekOfYear:return CFCalendarUnit.yearForWeekOfYear.rawValue

@malcommac
Copy link
Owner

Okay it now compiles fine. I've switched from old internal implementation taken from CoreFoundation to rangeOfUnit() of NSCalendar (see https://github.com/apple/swift-corelibs-foundation/blob/fa8a1c2afa4c322493a83272758c2e816e3b1b8b/Foundation/NSCalendar.swift#L420).

@malcommac
Copy link
Owner

Is there a way to run unit tests via docker?

@malcommac malcommac changed the title Restoring Linux support Add Linux support to SwiftDate Oct 27, 2018
@malcommac malcommac added this to the 5.0.13 milestone Oct 27, 2018
malcommac added a commit that referenced this issue Oct 27, 2018
@malcommac
Copy link
Owner

commands is

docker run --rm -v "$(pwd):/pkg" -w "/pkg" swift:4.2 /bin/bash -c "swift test --build-path ./.build/linux"

@malcommac
Copy link
Owner

malcommac commented Oct 27, 2018

Finally it compiles successfully.
LinuxMain.swift file with test can be generated from XCTest of XCode via the script generateLinuxTests.sh (it needs of sourcery tools brew install sourcery):

sourcery --sources Tests --templates .sourcery/LinuxMain.stencil --output .sourcery --force-parse generated
mv .sourcery/LinuxMain.generated.swift Tests/LinuxMain.swift

However there are several failed tests.

@malcommac
Copy link
Owner

I had a strange issue with compare function with granularity inside the NSCalendar object.

let d2 = Date.init(timeIntervalSince1970: 1529431200.0) // 2018-06-19 18:00:00 +0000
let d3 = Date.init(timeIntervalSince1970: 1529604000.0) // 2018-06-21 18:00:00 +0000
print("Data2: \(d2), Date3: \(d3)")
let res = Calendar.current.compare(d2, to: d3, toGranularity: .month)

The following code return .orderedSame on macOS/iOS (and test succeded), while on Linux it just return .orderedAscending (failing the test).

I had filled the following radar on Swift forum:
https://bugs.swift.org/browse/SR-9101

@malcommac
Copy link
Owner

Follow #612

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

No branches or pull requests

4 participants