Mac の PlatformIO IDE で #include エラーが出る問題を解決する

MacVSCode で PlatformIO を使って M5Stack の開発をしていると、#include <M5EPD.h> の行に

#include エラーが検出されました。includePath を更新してください。...
ソース ファイルを開けません "AvailabilityMacros.h" (dependency of "M5EPD.h")

というエラーが出現する。英語版では

#include errors detected. Please update your includePath. ...
cannot open source file "AvailabilityMacros.h"

であろう。

この状態でもプロジェクトのコンパイルは可能だが、VSCode が常にエラーの存在を訴えてきて annoying である。

このエラーが発生する原因は、最近の macOS には /usr/include が存在しないのにもかかわらず VSCode (もしくは PlatformIO もしくは C/C++ extension)が /usr/include を読みに行っているからだと思われる。

% xcrun --show-sdk-path
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk

で正しい path を確認してやり、platformio.inibuild_flags 内に次の行を追加すればエラーは消える。

build_flags = 
    ; ...
    -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/**