| ... | ... | @@ -1,6 +1,13 @@ |
| 1 | | //! TODO build libcxx and libcxxabi from source |
| 1 | const std = @import("std"); |
| 2 | const path = std.fs.path; |
| 3 | const assert = std.debug.assert; |
| 2 | 4 | |
| 3 | | pub const libcxxabi_files = [_][]const u8{ |
| 5 | const target_util = @import("target.zig"); |
| 6 | const Compilation = @import("Compilation.zig"); |
| 7 | const build_options = @import("build_options"); |
| 8 | const trace = @import("tracy.zig").trace; |
| 9 | |
| 10 | const libcxxabi_files = [_][]const u8{ |
| 4 | 11 | "src/abort_message.cpp", |
| 5 | 12 | "src/cxa_aux_runtime.cpp", |
| 6 | 13 | "src/cxa_default_handlers.cpp", |
| ... | ... | @@ -22,7 +29,7 @@ pub const libcxxabi_files = [_][]const u8{ |
| 22 | 29 | "src/stdlib_typeinfo.cpp", |
| 23 | 30 | }; |
| 24 | 31 | |
| 25 | | pub const libcxx_files = [_][]const u8{ |
| 32 | const libcxx_files = [_][]const u8{ |
| 26 | 33 | "src/algorithm.cpp", |
| 27 | 34 | "src/any.cpp", |
| 28 | 35 | "src/bind.cpp", |
| ... | ... | @@ -64,3 +71,229 @@ pub const libcxx_files = [_][]const u8{ |
| 64 | 71 | "src/variant.cpp", |
| 65 | 72 | "src/vector.cpp", |
| 66 | 73 | }; |
| 74 | |
| 75 | pub fn buildLibCXX(comp: *Compilation) !void { |
| 76 | if (!build_options.have_llvm) { |
| 77 | return error.ZigCompilerNotBuiltWithLLVMExtensions; |
| 78 | } |
| 79 | |
| 80 | const tracy = trace(@src()); |
| 81 | defer tracy.end(); |
| 82 | |
| 83 | var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa); |
| 84 | defer arena_allocator.deinit(); |
| 85 | const arena = &arena_allocator.allocator; |
| 86 | |
| 87 | const root_name = "c++"; |
| 88 | const output_mode = .Lib; |
| 89 | const link_mode = .Static; |
| 90 | const target = comp.getTarget(); |
| 91 | const basename = try std.zig.binNameAlloc(arena, root_name, target, output_mode, link_mode, null); |
| 92 | |
| 93 | const emit_bin = Compilation.EmitLoc{ |
| 94 | .directory = null, // Put it in the cache directory. |
| 95 | .basename = basename, |
| 96 | }; |
| 97 | |
| 98 | const cxxabi_include_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxxabi", "include" }); |
| 99 | const cxx_include_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxx", "include" }); |
| 100 | var c_source_files = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 101 | try c_source_files.ensureCapacity(libcxx_files.len); |
| 102 | |
| 103 | for (libcxx_files) |cxx_src| { |
| 104 | var cflags = std.ArrayList([]const u8).init(arena); |
| 105 | |
| 106 | if (target.os.tag == .windows) { |
| 107 | // Filesystem stuff isn't supported on Windows. |
| 108 | if (std.mem.startsWith(u8, cxx_src, "src/filesystem/")) |
| 109 | continue; |
| 110 | } else { |
| 111 | if (std.mem.startsWith(u8, cxx_src, "src/support/win32/")) |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | try cflags.append("-DNDEBUG"); |
| 116 | try cflags.append("-D_LIBCPP_BUILDING_LIBRARY"); |
| 117 | try cflags.append("-D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER"); |
| 118 | try cflags.append("-DLIBCXX_BUILDING_LIBCXXABI"); |
| 119 | try cflags.append("-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS"); |
| 120 | try cflags.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS"); |
| 121 | |
| 122 | if (target.abi.isMusl()) { |
| 123 | try cflags.append("-D_LIBCPP_HAS_MUSL_LIBC"); |
| 124 | } |
| 125 | |
| 126 | try cflags.append("-I"); |
| 127 | try cflags.append(cxx_include_path); |
| 128 | |
| 129 | try cflags.append("-I"); |
| 130 | try cflags.append(cxxabi_include_path); |
| 131 | |
| 132 | try cflags.append("-O3"); |
| 133 | try cflags.append("-DNDEBUG"); |
| 134 | if (target_util.supports_fpic(target)) { |
| 135 | try cflags.append("-fPIC"); |
| 136 | } |
| 137 | try cflags.append("-nostdinc++"); |
| 138 | try cflags.append("-fvisibility-inlines-hidden"); |
| 139 | try cflags.append("-std=c++14"); |
| 140 | try cflags.append("-Wno-user-defined-literals"); |
| 141 | |
| 142 | c_source_files.appendAssumeCapacity(.{ |
| 143 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxx", cxx_src }), |
| 144 | .extra_flags = cflags.items, |
| 145 | }); |
| 146 | } |
| 147 | |
| 148 | const sub_compilation = try Compilation.create(comp.gpa, .{ |
| 149 | // TODO use the global cache directory here |
| 150 | .zig_cache_directory = comp.zig_cache_directory, |
| 151 | .zig_lib_directory = comp.zig_lib_directory, |
| 152 | .target = target, |
| 153 | .root_name = root_name, |
| 154 | .root_pkg = null, |
| 155 | .output_mode = output_mode, |
| 156 | .rand = comp.rand, |
| 157 | .libc_installation = comp.bin_file.options.libc_installation, |
| 158 | .emit_bin = emit_bin, |
| 159 | .optimize_mode = comp.bin_file.options.optimize_mode, |
| 160 | .link_mode = link_mode, |
| 161 | .want_sanitize_c = false, |
| 162 | .want_stack_check = false, |
| 163 | .want_valgrind = false, |
| 164 | .want_pic = comp.bin_file.options.pic, |
| 165 | .emit_h = null, |
| 166 | .strip = comp.bin_file.options.strip, |
| 167 | .is_native_os = comp.bin_file.options.is_native_os, |
| 168 | .self_exe_path = comp.self_exe_path, |
| 169 | .c_source_files = c_source_files.items, |
| 170 | .verbose_cc = comp.verbose_cc, |
| 171 | .verbose_link = comp.bin_file.options.verbose_link, |
| 172 | .verbose_tokenize = comp.verbose_tokenize, |
| 173 | .verbose_ast = comp.verbose_ast, |
| 174 | .verbose_ir = comp.verbose_ir, |
| 175 | .verbose_llvm_ir = comp.verbose_llvm_ir, |
| 176 | .verbose_cimport = comp.verbose_cimport, |
| 177 | .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features, |
| 178 | .clang_passthrough_mode = comp.clang_passthrough_mode, |
| 179 | .link_libc = true, |
| 180 | }); |
| 181 | defer sub_compilation.destroy(); |
| 182 | |
| 183 | try sub_compilation.updateSubCompilation(); |
| 184 | |
| 185 | assert(comp.libcxx_static_lib == null); |
| 186 | comp.libcxx_static_lib = Compilation.CRTFile{ |
| 187 | .full_object_path = try sub_compilation.bin_file.options.directory.join(comp.gpa, &[_][]const u8{basename}), |
| 188 | .lock = sub_compilation.bin_file.toOwnedLock(), |
| 189 | }; |
| 190 | } |
| 191 | |
| 192 | pub fn buildLibCXXABI(comp: *Compilation) !void { |
| 193 | if (!build_options.have_llvm) { |
| 194 | return error.ZigCompilerNotBuiltWithLLVMExtensions; |
| 195 | } |
| 196 | |
| 197 | const tracy = trace(@src()); |
| 198 | defer tracy.end(); |
| 199 | |
| 200 | var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa); |
| 201 | defer arena_allocator.deinit(); |
| 202 | const arena = &arena_allocator.allocator; |
| 203 | |
| 204 | const root_name = "c++abi"; |
| 205 | const output_mode = .Lib; |
| 206 | const link_mode = .Static; |
| 207 | const target = comp.getTarget(); |
| 208 | const basename = try std.zig.binNameAlloc(arena, root_name, target, output_mode, link_mode, null); |
| 209 | |
| 210 | const emit_bin = Compilation.EmitLoc{ |
| 211 | .directory = null, // Put it in the cache directory. |
| 212 | .basename = basename, |
| 213 | }; |
| 214 | |
| 215 | const cxxabi_include_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxxabi", "include" }); |
| 216 | const cxx_include_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxx", "include" }); |
| 217 | |
| 218 | var c_source_files: [libcxxabi_files.len]Compilation.CSourceFile = undefined; |
| 219 | for (libcxxabi_files) |cxxabi_src, i| { |
| 220 | var cflags = std.ArrayList([]const u8).init(arena); |
| 221 | |
| 222 | try cflags.append("-DHAVE___CXA_THREAD_ATEXIT_IMPL"); |
| 223 | try cflags.append("-D_LIBCPP_DISABLE_EXTERN_TEMPLATE"); |
| 224 | try cflags.append("-D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS"); |
| 225 | try cflags.append("-D_LIBCXXABI_BUILDING_LIBRARY"); |
| 226 | try cflags.append("-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS"); |
| 227 | try cflags.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS"); |
| 228 | |
| 229 | if (target.abi.isMusl()) { |
| 230 | try cflags.append("-D_LIBCPP_HAS_MUSL_LIBC"); |
| 231 | } |
| 232 | |
| 233 | try cflags.append("-I"); |
| 234 | try cflags.append(cxxabi_include_path); |
| 235 | |
| 236 | try cflags.append("-I"); |
| 237 | try cflags.append(cxx_include_path); |
| 238 | |
| 239 | try cflags.append("-O3"); |
| 240 | try cflags.append("-DNDEBUG"); |
| 241 | if (target_util.supports_fpic(target)) { |
| 242 | try cflags.append("-fPIC"); |
| 243 | } |
| 244 | try cflags.append("-nostdinc++"); |
| 245 | try cflags.append("-fstrict-aliasing"); |
| 246 | try cflags.append("-funwind-tables"); |
| 247 | try cflags.append("-D_DEBUG"); |
| 248 | try cflags.append("-UNDEBUG"); |
| 249 | try cflags.append("-std=c++11"); |
| 250 | |
| 251 | c_source_files[i] = .{ |
| 252 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxxabi", cxxabi_src }), |
| 253 | .extra_flags = cflags.items, |
| 254 | }; |
| 255 | } |
| 256 | |
| 257 | const sub_compilation = try Compilation.create(comp.gpa, .{ |
| 258 | // TODO use the global cache directory here |
| 259 | .zig_cache_directory = comp.zig_cache_directory, |
| 260 | .zig_lib_directory = comp.zig_lib_directory, |
| 261 | .target = target, |
| 262 | .root_name = root_name, |
| 263 | .root_pkg = null, |
| 264 | .output_mode = output_mode, |
| 265 | .rand = comp.rand, |
| 266 | .libc_installation = comp.bin_file.options.libc_installation, |
| 267 | .emit_bin = emit_bin, |
| 268 | .optimize_mode = comp.bin_file.options.optimize_mode, |
| 269 | .link_mode = link_mode, |
| 270 | .want_sanitize_c = false, |
| 271 | .want_stack_check = false, |
| 272 | .want_valgrind = false, |
| 273 | .want_pic = comp.bin_file.options.pic, |
| 274 | .emit_h = null, |
| 275 | .strip = comp.bin_file.options.strip, |
| 276 | .is_native_os = comp.bin_file.options.is_native_os, |
| 277 | .self_exe_path = comp.self_exe_path, |
| 278 | .c_source_files = &c_source_files, |
| 279 | .verbose_cc = comp.verbose_cc, |
| 280 | .verbose_link = comp.bin_file.options.verbose_link, |
| 281 | .verbose_tokenize = comp.verbose_tokenize, |
| 282 | .verbose_ast = comp.verbose_ast, |
| 283 | .verbose_ir = comp.verbose_ir, |
| 284 | .verbose_llvm_ir = comp.verbose_llvm_ir, |
| 285 | .verbose_cimport = comp.verbose_cimport, |
| 286 | .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features, |
| 287 | .clang_passthrough_mode = comp.clang_passthrough_mode, |
| 288 | .link_libc = true, |
| 289 | }); |
| 290 | defer sub_compilation.destroy(); |
| 291 | |
| 292 | try sub_compilation.updateSubCompilation(); |
| 293 | |
| 294 | assert(comp.libcxxabi_static_lib == null); |
| 295 | comp.libcxxabi_static_lib = Compilation.CRTFile{ |
| 296 | .full_object_path = try sub_compilation.bin_file.options.directory.join(comp.gpa, &[_][]const u8{basename}), |
| 297 | .lock = sub_compilation.bin_file.toOwnedLock(), |
| 298 | }; |
| 299 | } |