Commit 3ed0741718

Andrew Kelley <andrew@ziglang.org>
2022-01-14 20:39:06
glibc: clean up build logic
also use the common naming convention for glibc versions ("2.33" rather than "2-33"). I also verified that these files are exactly identical to the previous files from before zig updated to glibc 2.34.
1 parent 3f52132
Changed files (20)
lib
libc
glibc
src
lib/libc/glibc/csu/elf-init-2-33.c → lib/libc/glibc/csu/elf-init-2.33.c
File renamed without changes
lib/libc/glibc/sysdeps/aarch64/start-2-33.S → lib/libc/glibc/sysdeps/aarch64/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/alpha/start-2-33.S → lib/libc/glibc/sysdeps/alpha/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/arm/start-2-33.S → lib/libc/glibc/sysdeps/arm/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/hppa/start-2-33.S → lib/libc/glibc/sysdeps/hppa/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/i386/start-2-33.S → lib/libc/glibc/sysdeps/i386/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/ia64/start-2-33.S → lib/libc/glibc/sysdeps/ia64/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/m68k/start-2-33.S → lib/libc/glibc/sysdeps/m68k/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/microblaze/start-2-33.S → lib/libc/glibc/sysdeps/microblaze/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/mips/start-2-33.S → lib/libc/glibc/sysdeps/mips/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/powerpc/powerpc32/start-2-33.S → lib/libc/glibc/sysdeps/powerpc/powerpc32/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/powerpc/powerpc64/start-2-33.S → lib/libc/glibc/sysdeps/powerpc/powerpc64/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/riscv/start-2-33.S → lib/libc/glibc/sysdeps/riscv/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/s390/s390-32/start-2-33.S → lib/libc/glibc/sysdeps/s390/s390-32/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/s390/s390-64/start-2-33.S → lib/libc/glibc/sysdeps/s390/s390-64/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/sh/start-2-33.S → lib/libc/glibc/sysdeps/sh/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/sparc/sparc32/start-2-33.S → lib/libc/glibc/sysdeps/sparc/sparc32/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/sparc/sparc64/start-2-33.S → lib/libc/glibc/sysdeps/sparc/sparc64/start-2.33.S
File renamed without changes
lib/libc/glibc/sysdeps/x86_64/start-2-33.S → lib/libc/glibc/sysdeps/x86_64/start-2.33.S
File renamed without changes
src/glibc.zig
@@ -183,8 +183,8 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
     const arena = arena_allocator.allocator();
 
     const target = comp.getTarget();
-    const target_version = target.os.version_range.linux.glibc;
-    const start_needs_init_fini_functions = target_version.major < 2 or (target_version.major == 2 and target_version.minor <= 33);
+    const target_ver = target.os.version_range.linux.glibc;
+    const start_old_init_fini = target_ver.order(.{ .major = 2, .minor = 33 }) != .gt;
 
     switch (crt_file) {
         .crti_o => {
@@ -246,8 +246,9 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                     "-DASSEMBLER",
                     "-Wa,--noexecstack",
                 });
+                const src_path = if (start_old_init_fini) "start-2.33.S" else "start.S";
                 break :blk .{
-                    .src_path = try start_asm_path(comp, arena, if (start_needs_init_fini_functions) "start-2-33.S" else "start.S"),
+                    .src_path = try start_asm_path(comp, arena, src_path),
                     .extra_flags = args.items,
                 };
             };
@@ -280,6 +281,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
             const Dep = struct {
                 path: []const u8,
                 flavor: Flavor = .shared,
+                exclude: bool = false,
             };
             const deps = [_]Dep{
                 .{
@@ -299,6 +301,10 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                     .flavor = .nonshared,
                 },
                 .{ .path = lib_libc_glibc ++ "csu" ++ s ++ "errno.c" },
+                .{
+                    .path = lib_libc_glibc ++ "csu" ++ s ++ "elf-init-2.33.c",
+                    .exclude = !start_old_init_fini,
+                },
                 .{ .path = linux_prefix ++ "stat.c" },
                 .{ .path = linux_prefix ++ "fstat.c" },
                 .{ .path = linux_prefix ++ "lstat.c" },
@@ -312,46 +318,12 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                 .{ .path = linux_prefix ++ "stat_t64_cp.c" },
             };
 
-            var c_source_files: [deps.len + 1]Compilation.CSourceFile = undefined;
-
-            if (start_needs_init_fini_functions) {
-                c_source_files[0] = blk: {
-                    var args = std.ArrayList([]const u8).init(arena);
-                    try args.appendSlice(&[_][]const u8{
-                        "-std=gnu11",
-                        "-fgnu89-inline",
-                        "-fmerge-all-constants",
-                        "-fno-stack-protector",
-                        "-fmath-errno",
-                        "-I",
-                        try lib_path(comp, arena, lib_libc_glibc ++ "csu"),
-                    });
-                    try add_include_dirs(comp, arena, &args);
-                    try args.appendSlice(&[_][]const u8{
-                        "-DSTACK_PROTECTOR_LEVEL=0",
-                        "-fPIC",
-                        "-fno-stack-protector",
-                        "-fno-common",
-                        "-ftls-model=initial-exec",
-                        "-D_LIBC_REENTRANT",
-                        "-include",
-                        try lib_path(comp, arena, lib_libc_glibc ++ "include" ++ path.sep_str ++ "libc-modules.h"),
-                        "-DMODULE_NAME=libc",
-                        "-Wno-nonportable-include-path",
-                        "-include",
-                        try lib_path(comp, arena, lib_libc_glibc ++ "include" ++ path.sep_str ++ "libc-symbols.h"),
-                        "-DPIC",
-                        "-DLIBC_NONSHARED=1",
-                        "-DTOP_NAMESPACE=glibc",
-                    });
-                    break :blk .{
-                        .src_path = try lib_path(comp, arena, lib_libc_glibc ++ "csu" ++ path.sep_str ++ "elf-init-2-33.c"),
-                        .extra_flags = args.items,
-                    };
-                };
-            }
+            var files_buf: [deps.len]Compilation.CSourceFile = undefined;
+            var files_index: usize = 0;
+
+            for (deps) |dep| {
+                if (dep.exclude) continue;
 
-            for (deps) |dep, i| {
                 var args = std.ArrayList([]const u8).init(arena);
                 try args.appendSlice(&[_][]const u8{
                     "-std=gnu11",
@@ -393,12 +365,14 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                     shared_def,
                     "-DTOP_NAMESPACE=glibc",
                 });
-                c_source_files[i + @boolToInt(start_needs_init_fini_functions)] = .{
+                files_buf[files_index] = .{
                     .src_path = try lib_path(comp, arena, dep.path),
                     .extra_flags = args.items,
                 };
+                files_index += 1;
             }
-            return comp.build_crt_file("c_nonshared", .Lib, c_source_files[0 .. deps.len + @boolToInt(start_needs_init_fini_functions)]);
+            const files = files_buf[0..files_index];
+            return comp.build_crt_file("c_nonshared", .Lib, files);
         },
     }
 }