@@ -552,32 +552,56 @@ fn addCmakeCfgOptionsToExe(
552552 if (use_zig_libcxx ) {
553553 exe .linkLibCpp ();
554554 } else {
555- const need_cpp_includes = true ;
556- const lib_suffix = switch (cfg .llvm_linkage ) {
557- .static = > exe .target .staticLibSuffix ()[1.. ],
558- .dynamic = > exe .target .dynamicLibSuffix ()[1.. ],
559- };
560-
561555 // System -lc++ must be used because in this code path we are attempting to link
562- // against system-provided LLVM, Clang, LLD.
563- if (exe .target .getOsTag () == .linux ) {
564- // First we try to link against gcc libstdc++. If that doesn't work, we fall
565- // back to -lc++ and cross our fingers.
566- addCxxKnownPath (b , cfg , exe , b .fmt ("libstdc++.{s}" , .{lib_suffix }), "" , need_cpp_includes ) catch | err | switch (err ) {
567- error .RequiredLibraryNotFound = > {
556+ // against system-provided or system-dependent LLVM, Clang, LLD.
557+ try exe .bundle .append (.{ .name = "libcxx" , .allow = false });
558+ if (cfg .zig_static_cxx ) {
559+ const lib_suffix = exe .target .staticLibSuffix ()[1.. ];
560+ const need_cpp_includes = true ;
561+ switch (exe .target .getOsTag ()) {
562+ .linux = > {
563+ // First we try to link against gcc libstdc++. If that doesn't work, we fall
564+ // back to -lc++ and cross our fingers.
565+ addCxxKnownPath (b , cfg , exe , b .fmt ("libstdc++.{s}" , .{lib_suffix }), "" , need_cpp_includes ) catch | err | switch (err ) {
566+ error .RequiredLibraryNotFound = > {
567+ exe .linkSystemLibrary ("c++" );
568+ },
569+ else = > | e | return e ,
570+ };
571+ exe .linkSystemLibrary ("unwind" );
572+ },
573+ .ios , .macos , .watchos , .tvos = > {
574+ exe .linkSystemLibrary ("c++" );
575+ },
576+ .freebsd = > {
577+ try addCxxKnownPath (b , cfg , exe , b .fmt ("libc++.{s}" , .{lib_suffix }), null , need_cpp_includes );
578+ try addCxxKnownPath (b , cfg , exe , b .fmt ("libgcc_eh.{s}" , .{lib_suffix }), null , need_cpp_includes );
579+ @panic ("WIP: mike" );
580+ },
581+ .openbsd = > {
582+ try addCxxKnownPath (b , cfg , exe , b .fmt ("libc++.{s}" , .{lib_suffix }), null , need_cpp_includes );
583+ try addCxxKnownPath (b , cfg , exe , b .fmt ("libc++abi.{s}" , .{lib_suffix }), null , need_cpp_includes );
584+ @panic ("WIP: mike" );
585+ },
586+ .netbsd = > {
587+ @panic ("WIP: mike" );
588+ },
589+ .dragonfly = > {
590+ @panic ("WIP: mike" );
591+ },
592+ else = > {
593+ @panic ("WIP: mike" );
594+ },
595+ }
596+ } else {
597+ switch (exe .target .getOsTag ()) {
598+ .ios , .macos , .watchos , .tvos , .freebsd , .openbsd = > {
568599 exe .linkSystemLibrary ("c++" );
569600 },
570- else = > | e | return e ,
571- };
572- exe .linkSystemLibrary ("unwind" );
573- } else if (exe .target .isFreeBSD ()) {
574- try addCxxKnownPath (b , cfg , exe , b .fmt ("libc++.{s}" , .{lib_suffix }), null , need_cpp_includes );
575- exe .linkSystemLibrary ("pthread" );
576- } else if (exe .target .getOsTag () == .openbsd ) {
577- try addCxxKnownPath (b , cfg , exe , b .fmt ("libc++.{s}" , .{lib_suffix }), null , need_cpp_includes );
578- try addCxxKnownPath (b , cfg , exe , b .fmt ("libc++abi.{s}" , .{lib_suffix }), null , need_cpp_includes );
579- } else if (exe .target .isDarwin ()) {
580- exe .linkSystemLibrary ("c++" );
601+ else = > {
602+ exe .linkSystemLibrary ("stdc++" );
603+ },
604+ }
581605 }
582606 }
583607
@@ -675,6 +699,7 @@ const CMakeConfig = struct {
675699 llvm_include_dir : []const u8 ,
676700 llvm_libraries : []const u8 ,
677701 dia_guids_lib : []const u8 ,
702+ zig_static_cxx : bool ,
678703};
679704
680705const max_config_h_bytes = 1 * 1024 * 1024 ;
@@ -740,6 +765,7 @@ fn parseConfigH(b: *Builder, config_h_text: []const u8) ?CMakeConfig {
740765 .llvm_include_dir = undefined ,
741766 .llvm_libraries = undefined ,
742767 .dia_guids_lib = undefined ,
768+ .zig_static_cxx = false ,
743769 };
744770
745771 const mappings = [_ ]struct { prefix : []const u8 , field : []const u8 }{
@@ -792,6 +818,7 @@ fn parseConfigH(b: *Builder, config_h_text: []const u8) ?CMakeConfig {
792818 .field = "llvm_lib_dir" ,
793819 },
794820 // .prefix = ZIG_LLVM_LINK_MODE parsed manually below
821+ // .prefix = ZIG_STATIC_CXX parsed manually below
795822 };
796823
797824 var lines_it = mem .tokenize (u8 , config_h_text , "\r \n " );
@@ -809,6 +836,8 @@ fn parseConfigH(b: *Builder, config_h_text: []const u8) ?CMakeConfig {
809836 _ = it .next ().? ; // skip the stuff before the quote
810837 const quoted = it .next ().? ; // the stuff inside the quote
811838 ctx .llvm_linkage = if (mem .eql (u8 , quoted , "shared" )) .dynamic else .static ;
839+ } else if (mem .startsWith (u8 , line , "#define ZIG_STATIC_CXX " )) {
840+ ctx .zig_static_cxx = true ;
812841 }
813842 }
814843 return ctx ;
0 commit comments