いろいろやってみるにっき

てきとーに生きている奴の日記

古いエントリのサムネイル画像がリンク切れになってたりするけど、チマチマ修正中


SwiftをWSLで動かす その1

はじめに

こないだIonic FrameworkをMac上で動かした。これは弊社のCTOがJavaScript系の開発環境を好んでいるため、先行して調査したものである。

shigeo-t.hatenablog.com

shigeo-t.hatenablog.com

shigeo-t.hatenablog.com

 

しかし、ミーティングで動きを見せたところ「iOSアプリだけなので素直にSwiftを書く」と言われてしまった。まあ、素直に考えればそれが正しい。Android,iOSマルチプラットフォームじゃないのにマルチプラットフォーム用の環境を使うのもおかしいわけで。それにIonicでTensorFlow Liteとの接続で苦労するのもバカらしいという話だった。

www.tensorflow.org

 

目次

 

Swiftの開発環境下調べ

MacならすぐにSwiftが使えるのを知った上でWindowsで環境を作ってみようと考えた。”Swift Windows 開発”でDuckDuckGo検索してみると、こんな記事がヒットした。

japan.zdnet.com

 

読んでみると、

Appleプログラミング言語「Swift」と、Googleユーザーインターフェース(UI)フレームワーク「Flutter」が「Windows 10」上で利用可能になった。

Flutterは関係ないのでSwiftの話を追ってみる。

Windows上でのSwiftを試すには、ツールチェーンインストーラーと統合開発環境IDE)「Visual Studio 2019」とともに、「Windows 10 SDK」や、C++のコードをビルドするためのツールセット、「Windows Universal C Runtime」といったコンポーネントが必要となる。

なんか話が大げさになってきた。Visual Studioの環境は無くはないが、なんかめんどくさい。

 

Appleの説明を読んでみる。

www.apple.com

Swiftはどこででも。
しかも、誰でも使えるようになりました。
無料でオープンソースのSwiftは、Apache 2.0オープンソースライセンスのもと、多くのデベロッパ教育機関、学生の方々に幅広く利用していただけます。Appleでは、iOSmacOS、watchOS、tvOS、Linux向けのコードをコンパイルできる、macOSLinuxのためのバイナリを提供しています。さらに、Swiftを一段とパワフルな言語に進化させるため、ユーザーのみなさんがSwiftのソースコードの改良に直接参加できるコミュニティも作りました。

Windowsについて言及されてない。

 

なお、上記記事からSwift.orgのダウンロードページに飛ぶとWindows版はある。

swift.org

 

よくわからないが、”Swift Windows 開発”でDuckDuckGo検索した中にこんなエントリがあった。

qiita.com

 

ということで、どうせCTOはUbuntuで開発するし、オレはWSLが使いやすいのでこれを参考にすることにした。

 

おまけでWindows版Swiftをインストール

本題のWSL環境に取り掛かる前にWindows版をインストールしてみる。ほんとにVisual Studio必要なのか見ておきたい。Visual Studio無しのPCで試す。Swift5.3.3のWindows版をダウンロード。

f:id:shigeo-t:20210419093419p:plain

 

実行する。普通のWindowsインストーラだ。殺風景だけど。

f:id:shigeo-t:20210419093452p:plain

[Install]をクリックすると動き出す。

f:id:shigeo-t:20210419093544p:plain

ほどなく終わる。

f:id:shigeo-t:20210419093615p:plain

[Close]をクリックするとインストーラは終わる。さてどうなっているのか?適当にswiftって叩くと何も出ずに終わるので、--helpを付けてみた。コンパイラのヘルプが出た。

PS C:\Users\shigeo-t> swift --help
OVERVIEW: Swift compiler

USAGE: swift

OPTIONS:
  -assert-config <value>  Specify the assert_configuration replacement. Possible values are Debug, Release, Unchecked, DisableReplacement.
  -color-diagnostics      Print diagnostics in color
  -continue-building-after-errors
                          Continue building, even after errors are encountered
  -coverage-prefix-map <value>
                          Remap source paths in coverage info
  -debug-info-format=<value>
                          Specify the debug info format type to either 'dwarf' or 'codeview'
  -debug-info-store-invocation
                          Emit the compiler invocation in the debug info.
  -debug-prefix-map <value>
                          Remap source paths in debug info
  -disable-astscope-lookup
                          Disable ASTScope-based unqualified name lookup
  -disable-autolinking-runtime-compatibility-dynamic-replacements
                          Do not use autolinking for the dynamic replacement runtime compatibility library
  -disable-autolinking-runtime-compatibility
                          Do not use autolinking for runtime compatibility libraries
  -disable-fuzzy-forward-scan-trailing-closure-matching
                          Disable fuzzy forward-scan trailing closure matching
  -disable-only-one-dependency-file
                          Disables incremental build optimization that only produces one dependencies file
  -disable-parser-lookup  Disable parser lookup & use ast scope lookup only (experimental)
  -disable-request-based-incremental-dependencies
                          Disable request-based name tracking
  -driver-compare-incremental-schemes-path <path>
                          Path to use for machine-readable comparision
  -driver-compare-incremental-schemes
                          Print a simple message comparing dependencies with source ranges (w/ fallback)
  -D <value>              Marks a conditional compilation flag as true
  -embed-tbd-for-module <value>
                          Embed symbols from the module in the emitted tbd file
  -enable-astscope-lookup Enable ASTScope-based unqualified name lookup
  -enable-experimental-additive-arithmetic-derivation
                          Enable experimental 'AdditiveArithmetic' derived conformances
  -enable-experimental-concise-pound-file
                          Enable experimental concise '#file' identifier
  -enable-experimental-forward-mode-differentiation
                          Enable experimental forward mode differentiation
  -enable-fuzzy-forward-scan-trailing-closure-matching
                          Enable fuzzy forward-scan trailing closure matching
  -enable-library-evolution
                          Build the module to allow binary-compatible library evolution
  -enable-only-one-dependency-file
                          Enables incremental build optimization that only produces one dependencies file
  -enable-request-based-incremental-dependencies
                          Enable request-based name tracking
  -enable-source-range-dependencies
                          Try using source range information
  -enforce-exclusivity=<enforcement>
                          Enforce law of exclusivity
  -framework <value>      Specifies a framework which should be linked against
  -Fsystem <value>        Add directory to system framework search path
  -F <value>              Add directory to framework search path
  -gdwarf-types           Emit full DWARF type info.
  -gline-tables-only      Emit minimal debug info for backtraces only
  -gnone                  Don't emit debug info
  -g                      Emit debug info. This is the preferred setting for debugging with LLDB.
  -help                   Display available options
  -index-store-path <path>
                          Store indexing data to <path>
  -I <value>              Add directory to the import search path
  -j <n>                  Number of commands to execute in parallel
  -libc <value>           libc runtime library to use
  -L <value>              Add directory to library link search path
  -l<value>               Specifies a library which should be linked against
  -module-cache-path <value>
                          Specifies the Clang module cache path
  -module-link-name <value>
                          Library to link against when using this module
  -module-name <value>    Name of the module to build
  -no-color-diagnostics   Do not print diagnostics in color
  -no-warnings-as-errors  Don't treat warnings as errors
  -nostdimport            Don't search the standard library import path for modules
  -num-threads <n>        Enable multi-threading and specify number of threads
  -Onone                  Compile without any optimization
  -Osize                  Compile with optimizations and target small code size
  -Ounchecked             Compile with optimizations and remove runtime safety checks
  -O                      Compile with optimizations
  -print-educational-notes
                          Include educational notes in printed diagnostic output, if available
  -print-target-info      Print target information for the given target <triple>, such as x86_64-apple-macos10.9
  -Rcross-import          Emit a remark if a cross-import of a module is triggered.
  -remove-runtime-asserts Remove runtime safety checks.
  -Rpass-missed=<value>   Report missed transformations by optimization passes whose name matches the given POSIX regular expression
  -Rpass=<value>          Report performed transformations by optimization passes whose name matches the given POSIX regular expression
  -runtime-compatibility-version <value>
                          Link compatibility library for Swift runtime version, or 'none'
  -save-optimization-record-passes <regex>
                          Only include passes which match a specified regular expression inthe generated optimization record (by default, include all passes)
  -save-optimization-record-path <value>
                          Specify the file name of any generated optimization record
  -save-optimization-record=<format>
                          Generate an optimization record file in a specific format (default: YAML)
  -save-optimization-record
                          Generate a YAML optimization record file
  -sdk <sdk>              Compile against <sdk>
  -serialize-diagnostics-path <path>
                          Emit a serialized diagnostics file to <path>
  -static-executable      Statically link the executable
  -static-stdlib          Statically link the Swift standard library
  -suppress-warnings      Suppress all warnings
  -swift-version <vers>   Interpret input according to a specific Swift language version number
  -target-cpu <value>     Generate code for a particular CPU variant
  -target-variant <value> Generate 'zippered' code for macCatalyst that can run on the specified variant target triple in addition to the main -target triple
  -target <triple>        Generate code for the given target <triple>, such as x86_64-apple-macos10.9
  -use-ld=<value>         Specifies the linker to be used
  -version                Print version information and exit
  -vfsoverlay <value>     Add directory to VFS overlay file
  -v                      Show commands to run and use verbose output
  -warn-implicit-overrides
                          Warn about implicit overrides of protocol members
  -warn-swift3-objc-inference-complete
                          Warn about deprecated @objc inference in Swift 3 for every declaration that will no longer be inferred as @objc in Swift 4
  -warn-swift3-objc-inference-minimal
                          Warn about deprecated @objc inference in Swift 3 based on direct uses of the Objective-C entrypoint
  -warnings-as-errors     Treat warnings as errors
  -working-directory <path>
                          Resolve file paths relative to the specified directory
  -Xcc <arg>              Pass <arg> to the C/C++/Objective-C compiler
  -Xlinker <value>        Specifies an option which should be passed to the linker

SEE ALSO - PACKAGE MANAGER COMMANDS:
        "swift build" Build sources into binary products
        "swift package" Perform operations on Swift packages
        "swift run" Build and run an executable product
        "swift test" Build and run tests

 

Windows上でSwiftの環境構築をする (WSL + VS Code) - Qiitaを見ると、

  1. Windows上で直接構築する
  2. WSL上で構築する
  3. Dockerを利用する

1ができればいいのですが、SwiftPMが対応していないなどまだ未熟な印象です。
1の方法でやりたい場合swift-buildに方法が書いてあります。

 

swift-buildを開いてみる。

github.com

Requirements

Required Visual Studio Components

Component ID
MSVC v142 - VS 2019 C++ x64/x86 build tools (v14.25)1 Microsoft.VisualStudio.Component.VC.Tools.x86.x64
Windows Universal C Runtime Microsoft.VisualStudio.Component.Windows10SDK
Windows 10 SDK (10.0.17763.0)2 Microsoft.VisualStudio.Component.Windows10SDK.17763

   1 This is needed for the Visual C++ headers (you can use -use-ld=lld to use lld instead of link.exe)
   2 You may install a newer SDK if you desire. 17763 is listed here to match the minimum Windows release supported.

ああ、確かにVC++とか使うならVisual Studio必要だわ。

 

アプリ動作先のOSはWindowsじゃないし、そこで頑張るのは間違っている気がする。iOSアプリを作れればいいので、当初予定通りWSLの環境を作ることにする。

 

ということでその2で環境構築する。

お時間あったら、他のエントリもクリックして頂ければ幸いです。