Misc advancements towards compiling C++

This commit is contained in:
David Gonzalez Martin 2024-03-24 14:50:18 -06:00
parent c18e41372e
commit 809de4411d
5 changed files with 58 additions and 24 deletions

View File

@ -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); const musl = try MuslContext.init(context);
var exists = true; var exists = true;
var dir = std.fs.cwd().openDir(musl.global_cache_dir, .{}) catch b: { var dir = std.fs.cwd().openDir(musl.global_cache_dir, .{}) catch b: {
@ -258,8 +263,10 @@ pub fn compileCSourceFile(context: *const Context, arguments: [][*:0]u8) !void {
var clang_args = UnpinnedArray([]const u8){}; var clang_args = UnpinnedArray([]const u8){};
try clang_args.append(context.my_allocator, context.executable_absolute_path); 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, "clang");
if (kind == .c or kind == .cpp) {
try clang_args.append(context.my_allocator, "-nostdinc"); try clang_args.append(context.my_allocator, "-nostdinc");
// TODO: fix
switch (@import("builtin").os.tag) { switch (@import("builtin").os.tag) {
.linux => { .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_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" });
@ -275,7 +282,23 @@ pub fn compileCSourceFile(context: *const Context, arguments: [][*:0]u8) !void {
"-isystem", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include", "-isystem", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include",
}); });
}, },
else => @compileError("Foo"), 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| { for (arguments) |arg| {
@ -15076,7 +15099,7 @@ pub const Unit = struct {
var stack_protector = "-fno-stack-protector".*; var stack_protector = "-fno-stack-protector".*;
var arguments = [_][*:0]u8{ &c_flag, c_source_file, &o_flag, basename_z, &g_flag, &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); unit.object_files.append_with_capacity(basename_z);
} }

View File

@ -69,11 +69,9 @@ pub fn entry_point(arguments: [][*:0]u8) !void {
// const exit_code = try clangMain(allocator, arguments); // const exit_code = try clangMain(allocator, arguments);
// std.process.exit(exit_code); // std.process.exit(exit_code);
} else if (byte_equal(command, "cc")) { } else if (byte_equal(command, "cc")) {
// TODO: transform our arguments to Clang and invoke it try Compilation.compileCSourceFile(context, command_arguments, .c);
try Compilation.compileCSourceFile(context, command_arguments);
} else if (byte_equal(command, "c++")) { } else if (byte_equal(command, "c++")) {
// TODO: transform our arguments to Clang and invoke it try Compilation.compileCSourceFile(context, command_arguments, .cpp);
todo();
} else if (byte_equal(command, "exe")) { } else if (byte_equal(command, "exe")) {
try Compilation.buildExecutable(context, command_arguments, .{ try Compilation.buildExecutable(context, command_arguments, .{
.is_test = false, .is_test = false,

3
test/cc/cpp_first/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
main
*.o
build

View File

@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.15)
project(cpp_first CXX)
add_executable(cpp_first main.cpp)

View File

@ -0,0 +1,7 @@
#include <iostream>
int main()
{
std::cout << "Hello world\n";
return 0;
}