inconsistent header includes when using clang with -fmodules and CPATH is set

On MacPorts, we install headers and libraries in a non-standard subdirectory, usually /opt/local.

We set CPATH to accomplish this and it usually works well, except not with -fmodules set.

Things look like they should be OK:

$ CPATH=/opt/local/include cpp -v -fmodules
Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/
“/Applications/” -cc1 -triple x86_64-apple-macosx10.13.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -disable-llvm-verifier -discard-value-names -main-file-name - -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -target-linker-version 409.12 -v -resource-dir /Applications/ -I /usr/include -I/opt/local/include -fdebug-compilation-dir /Users/cunningh -ferror-limit 19 -fmessage-length 245 -stack-protector 1 -fblocks -fencode-extended-block-signature -fmodules -fimplicit-module-maps -fmodules-cache-path=/var/folders/r3/82p666c95kjgzc4z9fj3pdwr0000gn/C/org.llvm.clang.cunningh/ModuleCache -fmodules-validate-system-headers -fobjc-runtime=macosx-10.13.0 -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -traditional-cpp -o - -x c -
clang -cc1 version 10.0.0 (clang-1000.11.45.5) default target x86_64-apple-darwin17.7.0
ignoring duplicate directory “/usr/include”
as it is a non-system directory that duplicates a system directory
#include “…” search starts here:
#include <…> search starts here:
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)

But in practice, we see errors like this:

In file included from /Applications/
/opt/local/include/unctrl.h:60:63: error: unknown type name 'SCREEN'

What happens is clang looks into the SDK first, finds ncurses.h in there, but that then calls into the unctrl.h in the CPATH path, and the build fails as the ncurses in the SDK is version 5, but MacPorts is version 6.

I suspect this is happening more often than just this example, but this is the one that delivers our errors.

Should clang not use the fmodules-cache if CPATH is set?

Any ideas or workarounds for us would be much appreciated.

Ken Cunningham