Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
Created August 12, 2013 18:46
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pyrtsa/6213784 to your computer and use it in GitHub Desktop.
Save pyrtsa/6213784 to your computer and use it in GitHub Desktop.
When installing Haskell Platform fails on Mac OS X (with quick fix below)

When installing Haskell Platform fails on Mac OS X (with quick fix below)

I was fighting with Haskell last weekend. At first, I couldn't install some missing libraries with Cabal, and then, when trying to find out what's wrong, I ended up removing the whole Haskell installation — only to find out I could no longer install neither the Haskell Platform nor even just Cabal Install! The warnings I would see were more or less about the use of the single quote in source code:

Preprocessing library text-0.11.2.3...

Data/Text.hs:6:52:
     warning: missing terminating ' character [-Winvalid-pp-token]
-- Copyright   : (c) 2009, 2010, 2011, 2012 Bryan O'Sullivan,
                                                   ^

The errors that finally prevented the installation, were about having non-preprocessor-directive lines start with # in Haskell files that should be preprocessed with the C preprocessor (CPP):

Data/Text.hs:442:3:
     error: invalid preprocessing directive
     #-}

(I should mention here, by the way, that Clang is all correct in rejecting these source files: it's trying to protect poor coders from writing e.g. #elsif where #elif would be needed! It's just that Haskellers are used to a less strict version of the C preprocessor, one that's been supported by the GNU compiler since ages ago.)

I copied both the above warnings and the error from the Haskell text package bug report haskell/text#53, but similar problems have been previously cited in the GHC bug tracker as well as Homebrew bug tracker about cabal-install.

I asked about the problem in the #haskell IRC channel but I'm unsure where the source of this problem should be actually fixed:

  • a) in broken packages (i.e. by moving the closing pragma markers, #-}, to the end of the preceding line),
  • b) in the Homebrew formulas (in that case, I guess it would be best to fix the GHC formula), or
  • c) in GHC itself, to make it not use the current C compiler for preprocessing Haskell source files (there's a Haskell implementation of the CPP in http://hackage.haskell.org/package/cpphs-1.16 but some commenters in IRC were concerned about the LGPL-mode licensing.)

So before we'll find a satisfactory permanent solution, here's my quick interim fix for the problem.

QUICKFIX: Steps to install Haskell Platform on Mac OS X

…with Homebrew and a very recent version of Clang.

  1. Install Glasgow Haskell Compiler 7.6.3. (This would be installed by brew install haskell-platform anyway, but we need it before that.)

    brew install ghc
    
  2. Enable installation of GCC versions, and install e.g. gcc-4.7.

    brew tap homebrew/versions
    brew install gcc47
    
  3. GHC uses /usr/bin/gcc by default, which is actually Clang. And apparently, new Clang versions don't support the -traditional GCC option that makes the C preprocessor work in a friendly manner to Haskell files. So, instead of using Clang as the preprocessor, reconfigure the GHC compiler script to compile with /usr/local/bin/gcc-4.7 instead.

    nano /usr/local/Cellar/ghc/7.6.3/lib/ghc-7.6.3/settings
    

    (In the above file, replace the second line:

    ("C compiler command", "/usr/bin/gcc"),
    

    with:

    ("C compiler command", "/usr/local/bin/gcc-4.8"),
    
  4. Now you should be good to install Haskell Platform – or just cabal-install if that's what you like.

    brew install haskell-platform
    

This should now work out with reasonably few problems, warnings or errors.

(At least, I'm keeping my fingers crossed!)

— @pyrtsa, 2013-08-12.

@cartazio
Copy link

gcc shoud only be used for the CPP pass, its a bad idea to use it for code gen on OS X for a number of reasons.

once cabal and cabal-install 1.18 land in few weeks, a much nicer systematic solution will be possible

Copy link

ghost commented Aug 16, 2013

@cartazio is that really true, given this: http://ghc.haskell.org/trac/ghc/ticket/7602 ?

I've some patches to get things compiling again but unless that particular issue is resolved it sounds like GCC may be the better option for now.

@reem
Copy link

reem commented Sep 17, 2013

There's an easier solution to this: install XCode's Command Line Tools for Version 4.6.x (preferably 4.6.2 form April 2013) from the apple developer's website. This fix is much faster and is much much easier to implement. You may want to add something about this.

@mcandre
Copy link

mcandre commented Sep 20, 2013

@JonathanReem I bought a new Mac today and installed the Xcode command line tools on it.

brew install haskell-platform still fails.

Trace:

$ brew install haskell-platform
...
==> make install
    parse error on input `glDrawArraysInstanced'

Error:
Building the OpenGLRaw-1.3.0.0 package failed
make: *** [build.stamp] Error 2

System:

$ specs brew xcode os hardware
Specs:

specs 0.4
https://github.com/mcandre/specs#readme

brew --version
0.9.5

xcodebuild -version
Xcode 5.0
Build version 5A1413

system_profiler SPSoftwareDataType | grep 'System Version'
      System Version: OS X 10.8.4 (12E3200)

system_profiler | grep 'Model Identifier'
      Model Identifier: MacBookAir6,2

@mcandre
Copy link

mcandre commented Sep 21, 2013

@pyrtsa You wrote gcc-4.8 once by mistake during configuration of the settings file, though you correctly said gcc47 everywhere else.

Otherwise, this tutorial totally works for me! Thanks for posting!

@dmilith
Copy link

dmilith commented Oct 11, 2013

You don't need brew. There should be llvm-gcc available (If previously installed Xcode <4.1) in PATH. It would also work well and fix this issue.

@seantalts
Copy link

I faced the same problem using the haskell platform binary and I had a gcc-4.2 already installed, so I changed the settings file located at /Library/Frameworks/GHC.framework/Versions/7.6.3-x86_64/usr/lib/ghc-7.6.3/settings like so:

 ("C compiler command", "/usr/local/bin/gcc-4.2"),

@jkarni
Copy link

jkarni commented Feb 2, 2014

@cartazio Is there a way to configure ghc to use gcc-4.8 for the CPP pass but not everything else?

@soul916
Copy link

soul916 commented Feb 11, 2014

@pyrtsa Thanks for the gist, I resolve my "invalid preprocessing directive" issue follow your steps.

My env is slightly different with your, I use the official Haskell platform installed in OS X 10.8.5
For more people reference, the ghc settings file located in
/Library/Frameworks/GHC.framework/Versions/7.6.3-x86_64/usr/lib/ghc-7.6.3/settings

I just use brew install gcc48 then modify ghc settings file, the code can compiled successful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment