11namespace fs = std::filesystem;
13#ifdef MAYAFLUX_PLATFORM_MACOS
16 std::string reported =
exec_command(
"brew --prefix 2>/dev/null");
19 return std::filesystem::path(reported);
22 const std::filesystem::path standard =
"/opt/homebrew";
29 .request =
"brew --prefix",
30 .tried = { standard } });
50#if defined(MAYAFLUX_PLATFORM_WINDOWS)
53 "C:\\Program Files (x86)\\Microsoft Visual Studio\\Installer\\vswhere.exe",
54 "C:\\Program Files\\Microsoft Visual Studio\\Installer\\vswhere.exe" },
55 .windows_kits_roots = {
56 "C:\\Program Files (x86)\\Windows Kits\\10",
57 "C:\\Program Files\\Windows Kits\\10" },
59#elif defined(MAYAFLUX_PLATFORM_MACOS)
61 .library_dirs = {
"/usr/local/lib",
"/usr/lib",
"/lib",
"/usr/local/lib64",
"/usr/lib64",
"/lib64" },
62 .include_dirs = {
"/usr/include",
"/usr/local/include" },
64 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk",
65 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk" },
69 .library_dirs = {
"/usr/local/lib",
"/usr/lib",
"/lib",
"/usr/local/lib64",
"/usr/lib64",
"/lib64" },
70 .include_dirs = {
"/usr/include",
"/usr/local/include" },
78 std::vector<std::string> paths;
79#ifdef MAYAFLUX_PLATFORM_MACOS
80 if (
const auto prefix = homebrew_prefix()) {
81 paths.push_back((*prefix /
"include" / leaf).
string());
85 paths.push_back((fs::path(dir) / leaf).
string());
101 static const std::vector<DependencyHint> hints = {
102 { .env_hint = Config::EIGEN_HINT,
104 .aliases = {
"eigen",
"Eigen" } },
105 { .env_hint = Config::FREETYPE_HINT,
107 .aliases = {
"freetype2",
"freetype" } },
108 { .vendored_root = (fs::path(Config::SOURCE_DIR) /
"third_party/magic_enum").
string(),
109 .env_hint = Config::MAGIC_ENUM_HINT,
110 .aliases = {
"magic_enum" },
111 .marker_file =
"magic_enum.hpp",
112 .system_include_suffixes = {
"",
"MayaFlux/thirdparty/magic_enum",
"magic_enum" } },
120 if (std::ranges::contains(hint.aliases, name)) {
129 template <
typename T,
typename Compute>
132 if (
auto view = cell.
read(); view && !view->empty()) {
135 T value = std::forward<Compute>(compute)();
136 if (!value.empty()) {
170 static std::string
find_dep_library(
const std::string& library_name,
const std::string& prefix =
"");
177#ifdef MAYAFLUX_PLATFORM_MACOS
178 static std::string get_macos_sdk_path();
188#ifdef MAYAFLUX_PLATFORM_WINDOWS
190 static std::string find_latest_vs_installation();
192 static std::string find_latest_msvc_version(
const fs::path& msvc_base);
194 static std::vector<std::string> get_msvc_includes();
196 static std::vector<std::string> get_msvc_libraries();
198 static std::vector<std::string> get_windows_sdk_includes();
200 static std::vector<std::string> get_windows_sdk_libraries();
202 static std::vector<std::string> probe_sdk_paths(
const std::string& subpath,
203 const std::vector<std::string>& subdirs,
204 const std::string& arch =
"");
208#ifdef MAYAFLUX_PLATFORM_MACOS
209 static std::string get_xcode_system_includes();
220 auto result =
exec_command(
"clang -print-resource-dir");
224 && (result.find(
"error:") == std::string::npos)
225 && (result.find(
"clang") != std::string::npos
226 || result.find(
"lib") != std::string::npos
227 || fs::exists(result))) {
230 return std::string {};
239 std::vector<std::string> includes;
241#ifdef MAYAFLUX_PLATFORM_WINDOWS
242 auto msvc_includes = get_msvc_includes();
243 includes.insert(includes.end(), msvc_includes.begin(), msvc_includes.end());
245 auto sdk_includes = get_windows_sdk_includes();
246 includes.insert(includes.end(), sdk_includes.begin(), sdk_includes.end());
250 includes.insert(includes.end(), clang_includes.begin(), clang_includes.end());
252#ifdef MAYAFLUX_PLATFORM_MACOS
253 std::string xcode_includes = SystemConfig::get_xcode_system_includes();
254 if (!xcode_includes.empty()) {
255 includes.push_back(xcode_includes);
258 if (
const auto prefix = homebrew_prefix(); prefix && fs::exists(*prefix /
"include")) {
259 includes.push_back((*prefix /
"include").
string());
272 std::vector<std::string> lib_paths;
274#ifdef MAYAFLUX_PLATFORM_WINDOWS
275 auto msvc_libs = get_msvc_libraries();
276 lib_paths.insert(lib_paths.end(), msvc_libs.begin(), msvc_libs.end());
278 auto sdk_libs = get_windows_sdk_libraries();
279 lib_paths.insert(lib_paths.end(), sdk_libs.begin(), sdk_libs.end());
282 lib_paths.insert(lib_paths.end(), system_libs.begin(), system_libs.end());
291 if (!prefix.empty()) {
293 return found->path.string();
297 std::vector<fs::path> directories;
299 directories.emplace_back(directory);
303 return found->path.string();
313 if (
const auto view = cache.
read()) {
314 if (
const auto hit = view->find(library_name); hit != view->end()) {
324 if (!result.empty()) {
335 const auto is_hit = [&hint](
const fs::path& dir) {
348 if (is_hit(fs::path(path))) {
355 const fs::path candidate = suffix.empty()
357 : fs::path(include) / suffix;
358 if (is_hit(candidate)) {
359 return candidate.string();
369#ifdef MAYAFLUX_PLATFORM_WINDOWS
370 const char* cmd =
"echo. | clang -v -E -x c++ - 2>&1";
372 const char* cmd =
"clang -v -E -x c++ - 2>&1 < /dev/null";
381 std::vector<std::string> paths;
382 bool in_search_paths =
false;
384 std::istringstream stream(output);
387 while (std::getline(stream, line)) {
388 if (line.find(
"#include <...> search starts here:") != std::string::npos) {
389 in_search_paths =
true;
392 if (line.find(
"End of search list.") != std::string::npos) {
395 if (in_search_paths) {
396 auto start = line.find_first_not_of(
" \t");
397 if (start != std::string::npos) {
398 auto end = line.find_last_not_of(
" \t");
399 std::string path = line.substr(start, end - start + 1);
401 paths.push_back(path);
411 if (!fs::exists(base)) {
415 std::string latest_version;
416 for (
const auto& entry : fs::directory_iterator(base)) {
417 if (entry.is_directory()) {
418 std::string name = entry.path().filename().string();
419 if (!name.empty() && std::isdigit(name[0])) {
420 if (latest_version.empty() || name > latest_version) {
421 latest_version = name;
426 return latest_version;
429#ifdef MAYAFLUX_PLATFORM_WINDOWS
431inline bool is_vs_installation(
const fs::path& candidate)
433 std::error_code code;
434 return !candidate.empty()
435 && fs::is_directory(candidate, code)
436 && fs::is_directory(candidate /
"VC" /
"Tools" /
"MSVC", code);
439inline std::string query_vswhere(
const std::string& vswhere_command)
441 const std::string command = vswhere_command
442 +
" -latest -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath 2>nul";
445 return is_vs_installation(fs::path(output)) ? output : std::string {};
448inline std::string SystemConfig::find_latest_vs_installation()
450 std::vector<std::string> commands {
"vswhere" };
452 for (
const char* name : {
"ProgramFiles(x86)",
"ProgramFiles",
"ProgramW6432" }) {
457 const fs::path exe = fs::path(root) /
"Microsoft Visual Studio" /
"Installer" /
"vswhere.exe";
458 std::error_code code;
459 if (fs::is_regular_file(exe, code)) {
460 commands.push_back(
"\"" + exe.string() +
"\"");
465 std::error_code code;
466 if (fs::is_regular_file(path, code)) {
467 commands.push_back(
"\"" + path +
"\"");
471 for (
const auto& command : commands) {
472 if (
auto found = query_vswhere(command); !found.empty()) {
477 const std::string developer_prompt =
safe_getenv(
"VSINSTALLDIR");
478 if (is_vs_installation(fs::path(developer_prompt))) {
479 return developer_prompt;
485inline std::string SystemConfig::find_latest_msvc_version(
const fs::path& msvc_base)
487 if (!fs::exists(msvc_base)) {
491 std::string latest_version;
492 for (
const auto& entry : fs::directory_iterator(msvc_base)) {
493 if (entry.is_directory()) {
494 std::string name = entry.path().filename().string();
495 if (!name.empty() && std::isdigit(name[0])) {
496 if (latest_version.empty() || name > latest_version) {
497 latest_version = name;
502 return latest_version;
505inline std::vector<std::string> SystemConfig::get_msvc_includes()
507 std::vector<std::string> includes;
509 std::string vs_path = find_latest_vs_installation();
510 if (!vs_path.empty()) {
511 fs::path msvc_base = fs::path(vs_path) /
"VC" /
"Tools" /
"MSVC";
512 std::string version = find_latest_msvc_version(msvc_base);
514 if (!version.empty()) {
515 fs::path include_path = msvc_base / version /
"include";
516 if (fs::exists(include_path)) {
517 includes.push_back(include_path.string());
522 if (includes.empty()) {
524 if (!vc_dir.empty() && fs::exists(vc_dir)) {
525 fs::path include_path = fs::path(vc_dir) /
"include";
526 if (fs::exists(include_path)) {
527 includes.push_back(include_path.string());
535inline std::vector<std::string> SystemConfig::get_msvc_libraries()
537 std::vector<std::string> lib_paths;
539 std::string vs_path = find_latest_vs_installation();
540 if (!vs_path.empty()) {
541 fs::path msvc_base = fs::path(vs_path) /
"VC" /
"Tools" /
"MSVC";
542 std::string version = find_latest_msvc_version(msvc_base);
544 if (!version.empty()) {
545 fs::path lib_path = msvc_base / version /
"lib" /
"x64";
546 if (fs::exists(lib_path)) {
547 lib_paths.push_back(lib_path.string());
552 if (lib_paths.empty()) {
554 if (!vc_dir.empty() && fs::exists(vc_dir)) {
555 fs::path lib_path = fs::path(vc_dir) /
"lib" /
"x64";
556 if (fs::exists(lib_path)) {
557 lib_paths.push_back(lib_path.string());
565inline std::vector<std::string> SystemConfig::get_windows_sdk_includes()
567 std::vector<std::string> includes;
569 std::string sdk_dir =
safe_getenv(
"WindowsSdkDir");
570 std::string sdk_ver =
safe_getenv(
"WindowsSDKVersion");
572 if (!sdk_dir.empty() && !sdk_ver.empty() && fs::exists(sdk_dir)) {
573 fs::path base = fs::path(sdk_dir);
574 std::string version = std::string(sdk_ver);
575 if (!version.empty() && version.back() ==
'\\') {
579 std::vector<std::string> subdirs = {
"ucrt",
"shared",
"um",
"winrt",
"cppwinrt" };
580 for (
const auto& subdir : subdirs) {
581 fs::path include_path = base /
"Include" / version / subdir;
582 if (fs::exists(include_path)) {
583 includes.push_back(include_path.string());
588 if (includes.empty()) {
589 includes = probe_sdk_paths(
"Include",
590 std::vector<std::string> {
"ucrt",
"shared",
"um",
"winrt",
"cppwinrt" });
596inline std::vector<std::string> SystemConfig::get_windows_sdk_libraries()
598 std::vector<std::string> lib_paths;
600 std::string sdk_dir =
safe_getenv(
"WindowsSdkDir");
601 std::string sdk_ver =
safe_getenv(
"WindowsSDKVersion");
603 if (!sdk_dir.empty() && !sdk_ver.empty() && fs::exists(sdk_dir)) {
604 fs::path base = fs::path(sdk_dir);
605 std::string version = std::string(sdk_ver);
606 if (!version.empty() && version.back() ==
'\\') {
610 std::vector<std::string> subdirs = {
"ucrt",
"um" };
611 for (
const auto& subdir : subdirs) {
612 fs::path lib_path = base /
"Lib" / version / subdir /
"x64";
613 if (fs::exists(lib_path)) {
614 lib_paths.push_back(lib_path.string());
619 if (lib_paths.empty()) {
620 lib_paths = probe_sdk_paths(
"Lib",
621 std::vector<std::string> {
"ucrt",
"um" },
"x64");
627inline std::vector<std::string> SystemConfig::probe_sdk_paths(
const std::string& subpath,
628 const std::vector<std::string>& subdirs,
629 const std::string& arch)
631 std::vector<std::string> paths;
633 if (fs::exists(base_path)) {
634 fs::path search_dir = fs::path(base_path) / subpath;
637 if (!version.empty()) {
638 for (
const auto& subdir : subdirs) {
639 fs::path final_path = fs::path(base_path) / subpath / version / subdir;
641 final_path = final_path / arch;
643 if (fs::exists(final_path)) {
644 paths.push_back(final_path.string());
659 std::vector<std::string> lib_paths;
661 std::string ld_library_path =
safe_getenv(
"LD_LIBRARY_PATH");
662 if (!ld_library_path.empty()) {
663 std::istringstream stream(ld_library_path);
665 while (std::getline(stream, path,
':')) {
666 if (!path.empty() && fs::exists(path)) {
667 lib_paths.push_back(path);
672#if defined(MAYAFLUX_PLATFORM_MACOS)
673 if (
const auto prefix = homebrew_prefix(); prefix && fs::exists(*prefix /
"lib")) {
674 lib_paths.push_back((*prefix /
"lib").
string());
679 if (fs::exists(path)) {
680 lib_paths.push_back(path);
687#ifdef MAYAFLUX_PLATFORM_MACOS
689inline std::string SystemConfig::get_macos_sdk_path()
691 const char* cmd =
"xcrun --show-sdk-path 2>/dev/null";
695 if (!sdk_path.empty() && fs::exists(sdk_path)) {
700 if (fs::exists(path)) {
708inline std::string SystemConfig::get_xcode_system_includes()
710 std::string sdk_path = get_macos_sdk_path();
711 if (sdk_path.empty()) {
715 std::string include_path = sdk_path +
"/usr/include";
716 if (fs::exists(include_path)) {
bool insert_or_assign(const K &key, V value)
Sets the value for key, adding the entry if it is absent.
Map kept as one sorted vector, cheap to copy.
Guard read() const noexcept
Pins the current snapshot.
void publish(T value)
Replaces the snapshot with value, whatever it was.
bool update(Edit edit)
Edits a copy of the current snapshot and publishes it.
Lock-free cell holding one immutable snapshot of a value, read without blocking.