compnerd/swift-win32

How do you actually use this when you compile with swiftc?

PseudonymPatel opened this issue · 4 comments

I feel like I should know this, but how do you actually use this? I have built it using cmake as instructed in the readme, but I get errors when using ninja.

Once it is built, how would I actually include it as a dependency when building using swiftc? With SPM it's simple enough, but what about with swiftc, and more specifically, swiftc on windows. This might be a really basic question, but I would appreciate any point in the right direction, blog posts, etc.

I'm not sure how to answer the first question - the library itself is used as any other library. Could you share the errors that you get when building with CMake + Ninja? That shouldn't happen - there is CI setup which seems to be able to build the examples and the library.

As to how to use it as a dependency - you can use it with SPM or with CMake. For the latter, I can point you to a couple of places to look. The first is my set of swift-cmake-examples: https://github.com/compnerd/swift-cmake-examples. Those are generic examples for how to use CMake with Swift. However if you are looking for something specifically with Swift/Win32: https://github.com/tensorflow/swift-models/blob/master/Examples/GPT2-Inference/CMakeLists.txt and https://github.com/tensorflow/swift-models/blob/master/CMakeLists.txt actually give a working (albeit dated) example of use of the library.

Thanks for the quick reply! The following is the build errors. I am using the x64 Native Tools command prompt running without admin. I am using the release build of swift, not the nightly builds.

C:\Users\Sheen\Swift\swift-win32>cmake -B build -D BUILD_SHARED_LIBS=YES -D CMAKE_BUILD_TYPE=Release -D CMAKE_Swift_FLAGS="%SWIFTFLAGS%" -G Ninja -S .
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Sheen/Swift/swift-win32/build

C:\Users\Sheen\Swift\swift-win32>ninja -C build SwiftWin32 UICatalog
ninja: Entering directory `build'
[1/2] Linking Swift shared library bin\SwiftWin32.dll
FAILED: bin/SwiftWin32.dll Sources/CMakeFiles/SwiftWin32.dir/Application/_TriviallyConstructible.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Application/Application.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Application/ApplicationDelegate.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Application/ApplicationMain.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Application/LaunchKeyOptions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/AlertAction.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/AlertController.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/AnimationCurve.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Button.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Color.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ContentContainer.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Control.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/DatePicker.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Device.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/EdgeInsets.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Font.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Label.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ProgressView.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Responder.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Scene.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/SceneConfiguration.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/SceneDelegate.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/SceneSession.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/SceneSizeRestrictions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Screen.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Slider.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Stepper.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Switch.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TextField.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TextInputTraits.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TextView.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TraitCollection.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TraitEnvironment.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/View.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ViewController.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ViewControllerTransitionCoordinator.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ViewControllerTransitionCoordinatorContext.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Window.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/WindowScene.swift.obj Sources/CMakeFiles/SwiftWin32.dir/CG/AffineTransform.swift.obj Sources/CMakeFiles/SwiftWin32.dir/CG/Point.swift.obj Sources/CMakeFiles/SwiftWin32.dir/CG/Rect.swift.obj Sources/CMakeFiles/SwiftWin32.dir/CG/Size.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/BatteryMonitor.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/Error.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/Logging.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/Rect+UIExtensions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/PropertyWrappers.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/String+UIExtensions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/WindowClass.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/WindowsHandle.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/WinSDK+Extensions.swift.obj swift/SwiftWin32.swiftmodule lib/SwiftWin32.lib
cmd.exe /C "cd . && C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\swiftc.exe -output-file-map Sources\CMakeFiles\SwiftWin32.dir\output-file-map.json -incremental -j 12 -emit-library -o bin\SwiftWin32.dll -module-name SwiftWin32 -module-link-name SwiftWin32 -emit-module -emit-module-path swift\SwiftWin32.swiftmodule -emit-dependencies -DSwiftWin32_EXPORTS -sdk C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk -I C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift -L C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift/windows -O -libc MD   -Xcc -DNONAMELESSUNION -Xcc -DCOBJMACROS  @CMakeFiles\SwiftWin32.rsp    -Xlinker -implib:lib\SwiftWin32.lib  && cd ."
..\Sources\UI\TextField.swift:85:21: error: cannot find type 'PARAFORMAT' in scope
      var pfFormat: PARAFORMAT = PARAFORMAT()
                    ^~~~~~~~~~
..\Sources\UI\TextField.swift:86:43: error: cannot find type 'PARAFORMAT' in scope
      pfFormat.cbSize = UINT(MemoryLayout<PARAFORMAT>.size)
                                          ^~~~~~~~~~
..\Sources\UI\TextField.swift:87:31: error: cannot find 'PFM_ALIGNMENT' in scope
      pfFormat.dwMask = DWORD(PFM_ALIGNMENT)
                              ^~~~~~~~~~~~~
..\Sources\UI\TextField.swift:90:38: error: cannot find 'EM_GETPARAFORMAT' in scope
        SendMessageW(self.hWnd, UINT(EM_GETPARAFORMAT), 0,
                                     ^~~~~~~~~~~~~~~~
..\Sources\UI\TextField.swift:104:21: error: cannot find type 'PARAFORMAT' in scope
      var pfFormat: PARAFORMAT = PARAFORMAT()
                    ^~~~~~~~~~
..\Sources\UI\TextField.swift:105:43: error: cannot find type 'PARAFORMAT' in scope
      pfFormat.cbSize = UINT(MemoryLayout<PARAFORMAT>.size)
                                          ^~~~~~~~~~
..\Sources\UI\TextField.swift:106:31: error: cannot find 'PFM_ALIGNMENT' in scope
      pfFormat.dwMask = DWORD(PFM_ALIGNMENT)
                              ^~~~~~~~~~~~~
..\Sources\UI\TextField.swift:111:52: error: cannot find 'PFA_LEFT' in scope
      case .left:       pfFormat.wAlignment = WORD(PFA_LEFT)
                                                   ^~~~~~~~
..\Sources\UI\TextField.swift:112:52: error: cannot find 'PFA_RIGHT' in scope
      case .right:      pfFormat.wAlignment = WORD(PFA_RIGHT)
                                                   ^~~~~~~~~
..\Sources\UI\TextField.swift:113:52: error: cannot find 'PFA_CENTER' in scope
      case .center:     pfFormat.wAlignment = WORD(PFA_CENTER)
                                                   ^~~~~~~~~~
..\Sources\UI\TextField.swift:114:52: error: cannot find 'PFA_JUSTIFY' in scope
      case .justified:  pfFormat.wAlignment = WORD(PFA_JUSTIFY)
                                                   ^~~~~~~~~~~
..\Sources\UI\TextField.swift:118:38: error: cannot find 'EM_SETPARAFORMAT' in scope
        SendMessageW(self.hWnd, UINT(EM_SETPARAFORMAT), 0,
                                     ^~~~~~~~~~~~~~~~
..\Sources\UI\TextField.swift:134:34: error: cannot find 'EM_SETTYPOGRAPHYOPTIONS' in scope
    SendMessageW(self.hWnd, UINT(EM_SETTYPOGRAPHYOPTIONS),
                                 ^~~~~~~~~~~~~~~~~~~~~~~
..\Sources\UI\TextField.swift:135:25: error: cannot find 'TO_ADVANCEDTYPOGRAPHY' in scope
                 WPARAM(TO_ADVANCEDTYPOGRAPHY), LPARAM(TO_ADVANCEDTYPOGRAPHY))
                        ^~~~~~~~~~~~~~~~~~~~~
..\Sources\UI\TextField.swift:135:56: error: cannot find 'TO_ADVANCEDTYPOGRAPHY' in scope
                 WPARAM(TO_ADVANCEDTYPOGRAPHY), LPARAM(TO_ADVANCEDTYPOGRAPHY))
                                                       ^~~~~~~~~~~~~~~~~~~~~
..\Sources\UI\View.swift:132:15: warning: immutable value 'hPreviousParent' was never used; consider replacing with '_' or removing it
    guard let hPreviousParent: HWND = SetParent(view.hWnd, self.hWnd) else {
              ^~~~~~~~~~~~~~~
              _
ninja: build stopped: subcommand failed.

Apart from that, is there a way to compile with dependencies using swiftc, or must I use cmake (I'll use cmake either way, just wondering)?

Ah, that's due to the newer features that are now in use. You could roll back to an earlier state (I think that sometime in June/July) or use a newer snapshot build of the toolchain.

Ultimately, any build system is invoking swiftc, so it is possible to build with swiftc but it is far from convenient. I highly recommend CMake for the time being. Although it is possible to build the library with swift package manager, building the applications still requires some additional work Pat build.

Thank you, using the snapshot build fixed things