Skip to main content

cabal.project Per GHC Version (Part 2)

In the cabal.project Per GHC Version blog entry, I mentioned that I changed the PhatSort Makefile so that a cabal.project file can be specified. Unlike Stack configuration, however, it was easy to use the wrong configuration and get a misleading error message. I updated the Makefile to resolve this issue.

The Makefile uses a CONFIG argument to select a stack.yaml configuration file when using Stack. In this case, the configuration file determines which version of GHC is used, so there cannot be a mismatch. I implemented a PROJECT_FILE argument to select a cabal.project file in a similar way, but the version of GHC used is determined by the PATH, not the configuration file. If I forget to specify the PROJECT_FILE when in an environment using GHC 9.2.1, for example, the default cabal.project file is used, resulting in failure.

I changed the Makefile to make it easier to use. If a PROJECT_FILE argument is passed, it is used. Otherwise, a cabal.project file for the version of GHC in the PATH is used if it exists.

ifneq ($(origin CABAL), undefined)
  MODE := cabal
  CABAL_ARGS :=
  ifneq ($(origin PROJECT_FILE), undefined)
    CABAL_ARGS += "--project-file=$(PROJECT_FILE)"
  else
    PROJECT_FILE := cabal-$(shell ghc --version | sed 's/.* //').project
    ifneq (,$(wildcard $(PROJECT_FILE)))
      CABAL_ARGS += "--project-file=$(PROJECT_FILE)"
    endif
  endif
endif

As usual, writing Makefile code was much more painful and time consuming than it should be. I had to refactor and test many times before finally arriving on the above code, which works but does not seem much different from other code that did not.

Author

Travis Cardwell

Published

Tags
Related Blog Entries