Swift

作成日 : 2022-11-10
最終更新日 :

Swift の本

Swift のインストール

下記は Windows 11 のネイティブの環境にインストールしたときのメモである。その後、この Windows の Swift の挙動がおかしいので、 しかたなくアンインストールし、かわりに WSL2 の Ubuntu 22.04 で入れている。

次の環境でインストールした。

OS: Windows 11 Home Version 10.0.22621
Tool Chain: Swift 5.7.1
IDE: Visual Studio 2022 Community

  1. Windows で「設定>プライバシーとセキュリティ>開発者向け」画面で、「開発者モード」の設定をオンにする。
  2. Git for Windows をインストールする。下記から [Download] をクリックする。
    https://gitforwindows.org/
    Git のインストールではいろいろ選択が必要なダイアログが出てくるが、エディタを除きデフォルトを選択した。エディタのデフォルトは Vim であるが、 私は Visual Studio Code にした。
  3. Visual Studio 2022 Community をインストールする。
    https://visualstudio.microsoft.com/ja/vs/whatsnew/
    Visual Studio Installer の[変更(M)]をクリックする。 C++ によるデスクトップ開発(Desktop development with C++)をチェックする。 個別のコンポーネント(Indivisual Components)をクリックし、検索窓に「ユニバーサル」と入れ表示される項目で、 Windows ユニバーサル CRT(Windows Universal C Runtime)をチェックして、[インストール]をクリックする。
  4. Swift を下記の「Windows 10」の「x86_64」からインストールする。
    https://www.swift.org/download/
  5. スタートから「x64 Native Tools Command Prompt for VS 2022」を探し、右クリックで「管理者として実行」を選ぶ。
  6. ウィンドウが開いたら次のコマンドを順次実行する。
    1. copy %SDKROOT%\usr\share\ucrt.modulemap "%UniversalCRTSdkDir%\Include\%UCRTVersion%\ucrt\module.modulemap"
    2. copy %SDKROOT%\usr\share\visualc.modulemap "%VCToolsInstallDir%\include\module.modulemap"
    3. copy %SDKROOT%\usr\share\visualc.apinotes "%VCToolsInstallDir%\include\visualc.apinotes"
    4. copy %SDKROOT%\usr\share\winsdk.modulemap "%UniversalCRTSdkDir%\Include\%UCRTVersion%\um\module.modulemap"
    途中で、
    C:\Program Files (x86)\Windows Kits\10\\Include\10.0.22000.0\ucrt\module.modulemap を上書きしますか? (Yes/No/All):
    などと尋ねられたら Yes を入力する。
  7. exit でコマンドプロンプトを終了させる

参考:

https://zenn.dev/kok_i/articles/df8f9fb978780b

Swift の実行

  1. 適当なエディタで saluton.swift ファイルを作る。内容は次の通り。
    print("Saluton, mondo")
  2. コンパイルする
    swiftc .\saluton.swift
    次のとおり表示される:
    ライブラリ saluton.lib とオブジェクト saluton.exp を作成中
  3. 次の通り実行する
    .\saluton.exe
    次の通り表示される:
    Saluton, mondo

私はひねくれものだから、Macintosh でもなく Linux でもなく Windows ネイティブ、 つまり Windows Subsystem for Linux も Docker も使わずに Swift を使ってみることにした。 Swift のバージョンは 5.7.1 である。

p.8 では REPL (Read-Eval-Print Loop) は単に swift コマンドだけで実行できるように書かれているが、 私の環境では単に swift と入力すると次の表示が出て終了する。

Welcome to Swift!

Subcommands:

  swift build      Build Swift packages
  swift package    Create and work on packages
  swift run        Run a program from a package
  swift test       Run package tests
  swift repl       Experiment with Swift code interactively

  Use `swift --help` for descriptions of available options and flags.

  Use `swift help >subcommand>` for more information about a subcommand.

そこで swift repl としてみると、すぐにプロンプトが返ってきてしまう。

python310.dll が見つからないため、コードの実行を続行できません。

私が入れている Python の環境は Python 3.9.15 である。ううむ、3.10 にバージョンアップしなければならないのか。

C:> conda install python=3.10
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed

CondaError: KeyboardInterrupt

バッチ ジョブを終了しますか (Y/N)? Y

しかたがない。新しい環境にした

C:> conda create --name py10 python=3.10
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: C:\Users\username\anaconda3\envs\py10

  added / updated specs:
    - python=3.10


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    certifi-2022.12.7          |  py310haa95532_0         149 KB
    libffi-3.4.2               |       hd77b12b_6         109 KB
    pip-22.3.1                 |  py310haa95532_0         2.8 MB
    python-3.10.8              |       h966fe2a_1        15.8 MB
    setuptools-65.5.0          |  py310haa95532_0         1.2 MB
    wincertstore-0.2           |  py310haa95532_2          15 KB
    ------------------------------------------------------------
                                           Total:        20.0 MB

The following NEW packages will be INSTALLED:
done
#
# To activate this environment, use
#
#     $ conda activate py10
#
# To deactivate an active environment, use
#
#     $ conda deactivate
C:> conda activate py10

(py10) C:> python --version
Python 3.10.8

(py10) C:> swift repl
Python path configuration:
  PYTHONHOME = (not set)
  PYTHONPATH = (not set)
  program name = 'python'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = 'C:\\Library\\Developer\\Toolchains\\unknown-Asserts-development.xctoolchain\\usr\\bin\\lldb.exe'
  sys.base_prefix = 'C:\\Users\\username\\anaconda3\\envs\\py10'
  sys.base_exec_prefix = 'C:\\Users\\username\\anaconda3\\envs\\py10'
  sys.platlibdir = 'lib'
  sys.executable = 'C:\\Library\\Developer\\Toolchains\\unknown-Asserts-development.xctoolchain\\usr\\bin\\lldb.exe'
  sys.prefix = 'C:\\Users\\username\\anaconda3\\envs\\py10'
  sys.exec_prefix = 'C:\\Users\\username\\anaconda3\\envs\\py10'
  sys.path = [
    'C:\\Users\\username\\anaconda3\\envs\\py10\\python310.zip',
    '.\\DLLs',
    '.\\lib',
    'C:\\Library\\Developer\\Toolchains\\unknown-Asserts-development.xctoolchain\\usr\\bin',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x000051d4 (most recent call first):
  <no Python frame>

まりんきょ学問所コンピュータの部屋コンピュータの本 > Swift


MARUYAMA Satosi