Compiling swift fails because 'unicode/ucnv.h' is not found

I have some problems to compile swift on macOS. I followed the instructions on swift/README.md at main · apple/swift · GitHub. The tools (cmake, ninja) are installed from MacPorts.

$ ./swift/utils/build-script --release-debuginfo --debug-swift-stdlib

ultimately fails with

+ /opt/local/bin/cmake --build /Users/martin/swift-source/build/Ninja-RelWithDebInfoAssert+stdlib-DebugAssert/llvm-macosx-x86_64 -- -j4 all
[124/1944][  6%][26.234s] Building CXX...nifest.dir/WindowsManifestMerger.cpp.o
FAILED: lib/WindowsManifest/CMakeFiles/LLVMWindowsManifest.dir/WindowsManifestMerger.cpp.o 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++  -DGTEST_HAS_RTTI=0 -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Ilib/WindowsManifest -I/Users/martin/swift-source/llvm/lib/WindowsManifest -I/opt/local/include/libxml2 -Iinclude -I/Users/martin/swift-source/llvm/include -Wno-unknown-warning-option -Werror=unguarded-availability-new -fno-stack-protector -g -fPIC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -std=c++11 -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wcovered-switch-default -Wno-class-memaccess -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wstring-conversion -fdiagnostics-color -O2  -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -mmacosx-version-min=10.9   -UNDEBUG  -fno-exceptions -fno-rtti -MD -MT lib/WindowsManifest/CMakeFiles/LLVMWindowsManifest.dir/WindowsManifestMerger.cpp.o -MF lib/WindowsManifest/CMakeFiles/LLVMWindowsManifest.dir/WindowsManifestMerger.cpp.o.d -o lib/WindowsManifest/CMakeFiles/LLVMWindowsManifest.dir/WindowsManifestMerger.cpp.o -c /Users/martin/swift-source/llvm/lib/WindowsManifest/WindowsManifestMerger.cpp
In file included from /Users/martin/swift-source/llvm/lib/WindowsManifest/WindowsManifestMerger.cpp:21:
In file included from /opt/local/include/libxml2/libxml/xmlreader.h:14:
In file included from /opt/local/include/libxml2/libxml/tree.h:1307:
In file included from /opt/local/include/libxml2/libxml/xmlmemory.h:218:
In file included from /opt/local/include/libxml2/libxml/threads.h:35:
In file included from /opt/local/include/libxml2/libxml/globals.h:18:
In file included from /opt/local/include/libxml2/libxml/parser.h:810:
/opt/local/include/libxml2/libxml/encoding.h:31:10: fatal error: 'unicode/ucnv.h' file not found
#include <unicode/ucnv.h>
		 ^~~~~~~~~~~~~~~~

(I can provide the full transcript, if necessary.) The same compiler error was reported at #57990 (libxml2 @2.9.7_2: ICU include directory not in pkg-config output) – MacPorts, so this seems to be a MacPorts problem.

The solution (if I understand it correctly from that thread) is to add the include path -I/opt/local/include to the compiler options. Unfortunately, I am not familiar with cmake (or the Swift build tools), so any advice how that can be done would be appreciated.

Regards, Martin.

PS: I wasn't sure under which category this should be posted, please move it to a different one if appropriate.

I had this issue in a non-Swift project (the llvm-bolt tool), and was able to fix it by adding this line

include_directories(/opt/local/include)

To the very beginning of my CMakeLists.txt file.
I think Mac ships with a bad version of libxml2, and CMake uses it instead of the proper version installed by Homebrew/MacPorts/etc.

Thank you for your response. After updating the ports to the latest version I could not reproduce the problem anymore, so apparently that has been fixed by MacPorts in the meantime.