There is a CMake module for checking if a file can be included from C code: https://cmake.org/cmake/help/latest/module/CheckIncludeFile.html
include(CheckIncludeFile)
check_include_file(netinet/in.h SUPPORTS_IN_H)
if(SUPPORTS_IN_H)
# ...
else()
# ...
endif(SUPPORTS_IN_H)
For C++, you can use CheckIncludeFileCXX