Skip to content

Commit abecf42

Browse files
committed
Pass sysroot explicitly to libclang on macOS
1 parent 8f18542 commit abecf42

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

include/cppast/compile_config.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ class compile_config
168168
return do_use_c();
169169
}
170170

171+
/// \effects Appends a raw compiler flag (e.g. `-isysroot <path>`).
172+
/// Use this to forward driver-level options that have no dedicated setter.
173+
void add_flag(std::string flag)
174+
{
175+
flags_.push_back(std::move(flag));
176+
}
177+
171178
protected:
172179
compile_config(std::vector<std::string> def_flags) : flags_(std::move(def_flags)) {}
173180

@@ -176,11 +183,6 @@ class compile_config
176183

177184
~compile_config() noexcept = default;
178185

179-
void add_flag(std::string flag)
180-
{
181-
flags_.push_back(std::move(flag));
182-
}
183-
184186
const std::vector<std::string>& get_flags() const noexcept
185187
{
186188
return flags_;

test/integration.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,27 @@
55

66
#include <cppast/cpp_preprocessor.hpp>
77

8+
#include <cstdlib>
9+
810
using namespace cppast;
911

12+
#ifdef __APPLE__
13+
namespace
14+
{
15+
// libclang's embedded driver doesn't auto-detect the Apple SDK from SDKROOT
16+
// for clang_parseTranslationUnit (only the driver binary does). Forward
17+
// -isysroot explicitly so libc++ headers can find <stdlib.h>, <wchar.h>, etc.
18+
void apply_macos_sysroot(libclang_compile_config& config)
19+
{
20+
if (auto sdkroot = std::getenv("SDKROOT"))
21+
{
22+
config.add_flag("-isysroot");
23+
config.add_flag(sdkroot);
24+
}
25+
}
26+
} // namespace
27+
#endif
28+
1029
TEST_CASE("stdlib", "[.][integration]")
1130
{
1231
auto code = R"(
@@ -105,6 +124,9 @@ TEST_CASE("stdlib", "[.][integration]")
105124

106125
libclang_compile_config config;
107126
config.set_flags(cpp_standard::cpp_latest);
127+
#ifdef __APPLE__
128+
apply_macos_sysroot(config);
129+
#endif
108130
auto file = parser.parse("stdlib.cpp", config);
109131
REQUIRE(!parser.error());
110132
REQUIRE(file);
@@ -126,6 +148,9 @@ TEST_CASE("cppast", "[integration]")
126148
libclang_compile_config config(database, CPPAST_INTEGRATION_FILE);
127149
config.set_flags(cpp_standard::cpp_latest);
128150
config.fast_preprocessing(true);
151+
#ifdef __APPLE__
152+
apply_macos_sysroot(config);
153+
#endif
129154
parse_files(parser, files, config);
130155

131156
REQUIRE(!parser.error());

0 commit comments

Comments
 (0)