CCompilerDriver: How can I use different versions of clang or gcc on OS X?

I am using LibraryLink and CCompilerDriver on OS X. I would like to use a different compiler than the system’s default. For example, I can install different versions of clang and gcc using MacPorts. Using gcc is necessary if I want OpenMP.

CCompilerDriver comes with a clang driver and a gcc driver. Both work with the system defaults. But how can I coerce them to work with a compiler having a different name (in the case of MacPorts, gcc might be named gcc-mp-5) and installed in a different location (e.g. /opt/local/bin)? However, I am unable to use either (CCompilerDriver`ClangCompiler`ClangCompiler or CCompilerDriver`GCCCompiler`GCCCompiler) with a different installation of gcc or clang.

I can set up CCompilerDriver`GenericCCompiler`GenericCCompiler to sort of work, with something like this:

$CCompiler = {"Compiler" -> CCompilerDriver`GenericCCompiler`GenericCCompiler, 
    "SystemCompileOptions" -> "-m64 -fPIC -O2 -framework Foundation", "CompilerName" -> "g++-mp-5", 
    "CompilerInstallation" -> "/opt/local/bin"};

But this has its own problems because it’s geared towards Windows. For example "Libraries" -> {"foo"} will translate to the compiler option -lfoo.lib instead of -lfoo, and won’t work on OS X. Then I have to resort to some ugly hack such as passing -lfoo in "CompilerOptions", which then brings its own problems and inconveniences.

So, short of trying to write a compiler drivers (which looks very daunting and I won’t try), how can I use a non-default gcc or a non-default clang on OS X?

This seems to do something reasonable for me

 $CCompiler = {"Compiler" -> CCompilerDriver`GCCCompiler`GCCCompiler, 
   "CompilerInstallation" -> "/usr/local/bin/gcc-4.9"}

where gcc-4.9 is the actual compiler executable, not the directory containing it.

Thank you, this works!

But what is the "CompilerName" option for then? Is there an inconsistency between different drivers in how "CompilerName" and "CompilerInstallation" are used?

Yes, it is a bug: the code that checks for compiler existence takes into account only "CompilerInstallation" but not "CompilerName". Thank you for pointing this out.