症状

  • 2023/02 現在、GHCup で GHC をインストールして stack runstack build しようとすると、以下のようなエラーが発生する.

ld64.lld: error: unknown argument '--gc-sections'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
`gcc' failed in phase `Linker'. (Exit code: 1)
  • この原因は GHC をインストールする際に Homebrew でインストールした clang で GHC がビルドされてしまうことであるようだ.

  • この問題を回避するためには、Homebrew を PATH から外した状態で GHCup で GHC をインストールすればよい.

状況

  • 現在、 brew list を実行してみるとたしかに llvm パッケージがインストールされた環境となっている.

  • さらに、 llvm パッケージでインストールされる clang にパスを通すため、 .zshrc に以下のようにパスの指定が記述されている.

export PATH="/opt/homebrew/opt/llvm/bin:$PATH"

対応・対策

  • まず、 .zshrc を編集し、上に示した llvm パッケージのパスへの追加をコメントで無効化しておく.

  • 続いて新たにシェルを起動し、 llvm パッケージにパスが通っていないシェルであることを確認する.

    • Tmux 外で新たに立ち上げたシェルのほうが確実かもしれない.

$ which ghcup
/Users/annpin/.ghcup/bin/ghcup

$ which clang
/usr/bin/clang
  • この状態で GHC や stack の再インストールを行っていくが、実は初めて stack build などの stack コマンドを実行する際にも GHC のインストールは行われる.

    • よって、このときも Homebrew 由来の llvmclang がパスに含まれていないようにしないとならない.

    • 一旦 stack コマンド由来でインストールされた GHC は ~/.stack フォルダを削除することで再度インストールし直すことができる.

$ rm -rf ~/.stack
  • この状態で GHCup を使って Stack、HLS、cabal、GHC の一通りの再インストールを行う.

$ ghcup tui

確認

  • このようにセットアップした後ならば Homebrew 由来の llvm にパスが通っている状態でも問題なく Haskell アプリケーションのビルド・実行が行える.

$ stack new playground
Downloading template "new-template" to create project "playground" in playground/ ...

The following parameters were needed by the template but not provided: author-email, author-name, cate
gory, copyright, github-username
You can provide them in /Users/annpin/.stack/config.yaml, like this:
templates:
  params:
    author-email: value
    author-name: value
    category: value
    copyright: value
    github-username: value
Or you can pass each one as parameters like this:
stack new playground new-template -p "author-email:value" -p "author-name:value" -p "category:value" -p "copyright:value" -p "github-username:value"

Looking for .cabal or package.yaml files to use to init the project.
Using cabal packages:
- playground/

Selecting the best among 19 snapshots...

* Matches https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/11.yaml

Selected resolver: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/2
0/11.yaml
Initialising configuration using resolver: https://raw.githubusercontent.com/commercialhaskell/stackag
e-snapshots/master/lts/20/11.yaml
Total number of user packages considered: 1
Writing configuration to file: playground/stack.yaml
All done.
/Users/annpin/.stack/templates/new-template.hsfiles:    6.70 KiB downloaded...

$ cd playground

$ stack run
Building all executables for `playground' once. After a successful build of all of them, only specifie
d executables will be rebuilt.
playground> configure (lib + exe)
Configuring playground-0.1.0.0...
playground> build (lib + exe)
Preprocessing library for playground-0.1.0.0..
Building library for playground-0.1.0.0..
[1 of 2] Compiling Lib
[2 of 2] Compiling Paths_playground
Preprocessing executable 'playground-exe' for playground-0.1.0.0..
Building executable 'playground-exe' for playground-0.1.0.0..
[1 of 2] Compiling Main
[2 of 2] Compiling Paths_playground
Linking .stack-work/dist/aarch64-osx/Cabal-3.6.3.0/build/playground-exe/playground-exe ...
playground> copy/register
Installing library in /Users/annpin/src/bitbucket.org/AnnPin/ghcup-workaround/playground/.stack-work/i
nstall/aarch64-osx/78d2e95322a75175abe631b23dac6fcf548e4dc86afff37f5a79699074382968/9.2.5/lib/aarch64-
osx-ghc-9.2.5/playground-0.1.0.0-DxHwvVCGfs98iuqnmeeplV
Installing executable playground-exe in /Users/annpin/src/bitbucket.org/AnnPin/ghcup-workaround/playgr
ound/.stack-work/install/aarch64-osx/78d2e95322a75175abe631b23dac6fcf548e4dc86afff37f5a79699074382968/
9.2.5/bin
Registering library for playground-0.1.0.0..
someFunc

$ stack ghci
Using main module: 1. Package `playground' component playground:exe:playground-exe with main-is file:
/Users/annpin/src/bitbucket.org/AnnPin/ghcup-workaround/playground/app/Main.hs
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
Configuring GHCi with the following packages: playground

* * * * * * * *

Warning: Multiple files use the same module name:
         * Paths_playground found at the following paths
           * /Users/annpin/src/bitbucket.org/AnnPin/ghcup-workaround/playground/.stack-work/dist/aarch64-osx/Cabal-3.6.3.0/build/autogen/Paths_playground.hs (playground:lib)
           * /Users/annpin/src/bitbucket.org/AnnPin/ghcup-workaround/playground/.stack-work/dist/aarch64-osx/Cabal-3.6.3.0/build/playground-exe/autogen/Paths_playground.hs (playground:exe:playground-exe)
* * * * * * * *

GHCi, version 9.2.5: https://www.haskell.org/ghc/  :? for help
[1 of 3] Compiling Lib              ( /Users/annpin/src/bitbucket.org/AnnPin/ghcup-workaround/playgrou
nd/src/Lib.hs, interpreted )
[2 of 3] Compiling Main             ( /Users/annpin/src/bitbucket.org/AnnPin/ghcup-workaround/playgrou
nd/app/Main.hs, interpreted )
[3 of 3] Compiling Paths_playground ( /Users/annpin/src/bitbucket.org/AnnPin/ghcup-workaround/playgrou
nd/.stack-work/dist/aarch64-osx/Cabal-3.6.3.0/build/autogen/Paths_playground.hs, interpreted )
Ok, three modules loaded.
Loaded GHCi configuration from /private/var/folders/ql/3v2ny0zn6szg8p7h62zkrz_w0000gn/T/haskell-stack-
ghci/f9d68dc2/ghci-script
ghci> putStrLn "hello"
hello
ghci> :q
Leaving GHCi.