diff --git a/bootstrap/Compilation.zig b/bootstrap/Compilation.zig index c67db18..2587826 100644 --- a/bootstrap/Compilation.zig +++ b/bootstrap/Compilation.zig @@ -166,7 +166,12 @@ const MuslContext = struct { } }; -pub fn compileCSourceFile(context: *const Context, arguments: [][*:0]u8) !void { +const CSourceKind = enum{ + c, + cpp, +}; + +pub fn compileCSourceFile(context: *const Context, arguments: [][*:0]u8, kind: CSourceKind) !void { const musl = try MuslContext.init(context); var exists = true; var dir = std.fs.cwd().openDir(musl.global_cache_dir, .{}) catch b: { @@ -258,24 +263,42 @@ pub fn compileCSourceFile(context: *const Context, arguments: [][*:0]u8) !void { var clang_args = UnpinnedArray([]const u8){}; try clang_args.append(context.my_allocator, context.executable_absolute_path); try clang_args.append(context.my_allocator, "clang"); - try clang_args.append(context.my_allocator, "-nostdinc"); - // TODO: fix - switch (@import("builtin").os.tag) { - .linux => { - try clang_args.append_slice(context.my_allocator, &.{ "-isystem", "/home/david/dev/zig/lib/include", "-isystem", "/home/david/dev/zig/lib/libc/include/x86_64-linux-gnu", "-isystem", "/home/david/dev/zig/lib/libc/include/generic-glibc", "-isystem", "/home/david/dev/zig/lib/libc/include/x86-linux-any", "-isystem", "/home/david/dev/zig/lib/libc/include/any-linux-any" }); - try clang_args.append(context.my_allocator, "-isystem"); - try clang_args.append(context.my_allocator, "/usr/include"); - try clang_args.append(context.my_allocator, "-isystem"); - try clang_args.append(context.my_allocator, "/usr/include/linux"); - }, - .macos => { - try clang_args.append_slice(context.my_allocator, &.{ - "-iframework", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks", - "-isystem", try context.pathFromCompiler("lib/include"), - "-isystem", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include", - }); - }, - else => @compileError("Foo"), + + if (kind == .c or kind == .cpp) { + try clang_args.append(context.my_allocator, "-nostdinc"); + + switch (@import("builtin").os.tag) { + .linux => { + try clang_args.append_slice(context.my_allocator, &.{ "-isystem", "/home/david/dev/zig/lib/include", "-isystem", "/home/david/dev/zig/lib/libc/include/x86_64-linux-gnu", "-isystem", "/home/david/dev/zig/lib/libc/include/generic-glibc", "-isystem", "/home/david/dev/zig/lib/libc/include/x86-linux-any", "-isystem", "/home/david/dev/zig/lib/libc/include/any-linux-any" }); + try clang_args.append(context.my_allocator, "-isystem"); + try clang_args.append(context.my_allocator, "/usr/include"); + try clang_args.append(context.my_allocator, "-isystem"); + try clang_args.append(context.my_allocator, "/usr/include/linux"); + }, + .macos => { + try clang_args.append_slice(context.my_allocator, &.{ + "-iframework", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks", + "-isystem", try context.pathFromCompiler("lib/include"), + "-isystem", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include", + }); + }, + else => @compileError("Operating system not supported"), + } + } + + if (kind == .cpp) { + try clang_args.append(context.my_allocator, "-nostdinc++"); + switch (@import("builtin").os.tag) { + .linux => { + }, + .macos => { + try clang_args.append_slice(context.my_allocator, &.{ + "-isystem", try context.pathFromCompiler("lib/libcxx/include"), + "-isystem", try context.pathFromCompiler("lib/libcxxabi/include"), + }); + }, + else => @compileError("Operating system not supported"), + } } for (arguments) |arg| { @@ -15076,7 +15099,7 @@ pub const Unit = struct { var stack_protector = "-fno-stack-protector".*; var arguments = [_][*:0]u8{ &c_flag, c_source_file, &o_flag, basename_z, &g_flag, &stack_protector }; - try compileCSourceFile(context, &arguments); + try compileCSourceFile(context, &arguments, .c); unit.object_files.append_with_capacity(basename_z); } diff --git a/bootstrap/main.zig b/bootstrap/main.zig index 38bce38..394fc29 100644 --- a/bootstrap/main.zig +++ b/bootstrap/main.zig @@ -69,11 +69,9 @@ pub fn entry_point(arguments: [][*:0]u8) !void { // const exit_code = try clangMain(allocator, arguments); // std.process.exit(exit_code); } else if (byte_equal(command, "cc")) { - // TODO: transform our arguments to Clang and invoke it - try Compilation.compileCSourceFile(context, command_arguments); + try Compilation.compileCSourceFile(context, command_arguments, .c); } else if (byte_equal(command, "c++")) { - // TODO: transform our arguments to Clang and invoke it - todo(); + try Compilation.compileCSourceFile(context, command_arguments, .cpp); } else if (byte_equal(command, "exe")) { try Compilation.buildExecutable(context, command_arguments, .{ .is_test = false, diff --git a/test/cc/cpp_first/.gitignore b/test/cc/cpp_first/.gitignore new file mode 100644 index 0000000..c63175d --- /dev/null +++ b/test/cc/cpp_first/.gitignore @@ -0,0 +1,3 @@ +main +*.o +build diff --git a/test/cc/cpp_first/CMakeLists.txt b/test/cc/cpp_first/CMakeLists.txt new file mode 100644 index 0000000..8e749af --- /dev/null +++ b/test/cc/cpp_first/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.15) +project(cpp_first CXX) +add_executable(cpp_first main.cpp) diff --git a/test/cc/cpp_first/main.cpp b/test/cc/cpp_first/main.cpp new file mode 100644 index 0000000..1ef8461 --- /dev/null +++ b/test/cc/cpp_first/main.cpp @@ -0,0 +1,7 @@ +#include + +int main() +{ + std::cout << "Hello world\n"; + return 0; +}