Skip to content

Commit

Permalink
Consistently use Darwin and other cosmetics
Browse files Browse the repository at this point in the history
pkg uses the OS, which under MacOS is Darwin. Rename PKG_OS_MACOS to PKG_OS_DARWIN to be consistent in the source as well.

Cater for a corner case in having a basename in Mach-O dynamic library.

Adjust comments and simplify parsing code in pkg_abi_macho after the major abi refactoring.
  • Loading branch information
Keve authored and bapt committed Dec 4, 2024
1 parent 034d21d commit c11e5ab
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 43 deletions.
8 changes: 4 additions & 4 deletions libpkg/pkg_abi.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static struct {
{ PKG_OS_NETBSD, "NetBSD" },
{ PKG_OS_DRAGONFLY, "dragonfly" },
{ PKG_OS_LINUX, "Linux" },
{ PKG_OS_MACOS, "Darwin" },
{ PKG_OS_DARWIN, "Darwin" },
{ -1, NULL },
};

Expand Down Expand Up @@ -109,7 +109,7 @@ pkg_os_uses_amd64_name(enum pkg_os os)
{
switch (os) {
case PKG_OS_FREEBSD:
case PKG_OS_MACOS:
case PKG_OS_DARWIN:
return (true);
case PKG_OS_NETBSD:
case PKG_OS_LINUX:
Expand Down Expand Up @@ -175,7 +175,7 @@ pkg_abi_string_only_major_version(enum pkg_os os)
switch (os) {
case PKG_OS_FREEBSD:
case PKG_OS_NETBSD:
case PKG_OS_MACOS:
case PKG_OS_DARWIN:
return (true);
case PKG_OS_DRAGONFLY:
case PKG_OS_LINUX:
Expand Down Expand Up @@ -371,7 +371,7 @@ pkg_abi_from_file(struct pkg_abi *abi)

enum pkg_arch arch_hint = PKG_ARCH_UNKNOWN;
if (work_arch_hint[0]) {
arch_hint = pkg_arch_from_string(PKG_OS_MACOS, work_arch_hint);
arch_hint = pkg_arch_from_string(PKG_OS_DARWIN, work_arch_hint);
if (arch_hint == PKG_ARCH_UNKNOWN) {
pkg_emit_error("Invalid ABI_FILE architecture hint %s",
work_arch_hint);
Expand Down
74 changes: 36 additions & 38 deletions libpkg/pkg_abi_macho.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@

/**
* Routines to support pkg_abi.c functions when dealing with Mach-O files.
* Supports getting ABI and ALTABI from the binary's load commands. Cave: picks
* first binary in FAT collection. Supports getting shared libary information.
* Picks right binary in FAT collection based on ABI. Supports FreeBSD naming of
* architectures.
* Supports getting struct pkg_abi from the binary's load commands.
* Supports getting shared libary information (needed, provided & loader).
* Picks right binary in Universal binary based on ABI.
*/

static enum pkg_arch
Expand Down Expand Up @@ -129,7 +128,7 @@ match_entry(macho_file_t *mf, enum pkg_arch arch_hint)
const fat_arch_t *p_end = p + mf->narch;
while (p < p_end) {
// do not match cpu_hint.type == CPU_TYPE_ANY which is used if the
// archname hint was not recognized
// arch_hint was not recognized
if (p->cpu.type == cpu_hint.type &&
p->cpu.type_is64 == cpu_hint.type_is64) {
switch (cpu_hint.type) {
Expand All @@ -140,7 +139,7 @@ match_entry(macho_file_t *mf, enum pkg_arch arch_hint)
CPU_SUBTYPE_ARM_ALL ||
p->cpu.subtype_arm ==
cpu_hint.subtype_arm) {
goto matched;
return p;
}
break;
case CPU_TYPE_POWERPC:
Expand All @@ -150,7 +149,7 @@ match_entry(macho_file_t *mf, enum pkg_arch arch_hint)
CPU_SUBTYPE_POWERPC_ALL ||
p->cpu.subtype_ppc ==
cpu_hint.subtype_ppc) {
goto matched;
return p;
}
break;
case CPU_TYPE_X86:
Expand All @@ -160,27 +159,26 @@ match_entry(macho_file_t *mf, enum pkg_arch arch_hint)
CPU_SUBTYPE_X86_ALL ||
p->cpu.subtype_x86 ==
cpu_hint.subtype_x86) {
goto matched;
return p;
}
break;
default:
break;
}
}
pkg_debug(1, "Looking for %s, did not match %s",
pkg_arch_to_string(PKG_OS_MACOS, arch_hint),
pkg_arch_to_string(PKG_OS_MACOS, cputype_to_pkg_arch(p->cpu)));
pkg_arch_to_string(PKG_OS_DARWIN, arch_hint),
pkg_arch_to_string(PKG_OS_DARWIN, cputype_to_pkg_arch(p->cpu)));
p++;
}
pkg_emit_notice("Scanned %d entr%s, found none matching selector %s",
mf->narch, mf->narch > 1 ? "ies" : "y",
pkg_arch_to_string(PKG_OS_MACOS, arch_hint));
pkg_arch_to_string(PKG_OS_DARWIN, arch_hint));
return 0;
} else if (mf->narch > 1 ) {
pkg_debug(1,"Found %"PRIu32" entries in universal binary, picking first",
mf->narch);
}
matched:
return p;
}

Expand Down Expand Up @@ -277,7 +275,7 @@ pkg_macho_abi_from_fd(int fd, struct pkg_abi *abi, enum pkg_arch arch_hint)
macho_version_t darwin;
map_platform_to_darwin(&darwin, bv->platform, bv->minos);

abi->os = PKG_OS_MACOS;
abi->os = PKG_OS_DARWIN;

abi->major = darwin.major;
abi->minor = darwin.minor;
Expand Down Expand Up @@ -391,33 +389,33 @@ analyse_macho(int fd, struct pkg *pkg, const bool baselibs)
dylib->compatibility_version.minor,
dylib->compatibility_version.patch);
} else {
// while under Darwin full path references are recommended and ubiquitous,
// we align with pkg native environment and use only the basename
// this also strips off any @executable_path, @loader_path, @rpath components
const char * basename = strrchr(dylib->path, '/');
if (basename) {
pkg_debug(3,
"Adding dynamic library path: %s ts %"PRIu32" current(%"PRIuFAST16", %"PRIuFAST16", %"PRIuFAST16") compat(%"PRIuFAST16", %"PRIuFAST16", %"PRIuFAST16")\n",
dylib->path, dylib->timestamp,
dylib->current_version.major,
dylib->current_version.minor,
dylib->current_version.patch,
dylib->compatibility_version.major,
dylib->compatibility_version.minor,
dylib->compatibility_version.patch);

basename++;

char *lib_with_version;
if (dylib->current_version.patch) {
xasprintf(&lib_with_version, "%s-%"PRIuFAST16".%"PRIuFAST16".%"PRIuFAST16, basename, dylib->current_version.major, dylib->current_version.minor, dylib->current_version.patch);
} else {
xasprintf(&lib_with_version, "%s-%"PRIuFAST16".%"PRIuFAST16, basename, dylib->current_version.major, dylib->current_version.minor);
}
if (LC_ID_DYLIB == loadcmd) {
pkg_addshlib_provided(pkg, lib_with_version);
} else {
pkg_addshlib_required(pkg, lib_with_version);
}
free(lib_with_version);
basename = basename ? basename + 1 : dylib->path;
pkg_debug(3,
"Adding dynamic library path: %s ts %"PRIu32" current(%"PRIuFAST16", %"PRIuFAST16", %"PRIuFAST16") compat(%"PRIuFAST16", %"PRIuFAST16", %"PRIuFAST16")\n",
dylib->path, dylib->timestamp,
dylib->current_version.major,
dylib->current_version.minor,
dylib->current_version.patch,
dylib->compatibility_version.major,
dylib->compatibility_version.minor,
dylib->compatibility_version.patch);

char *lib_with_version;
if (dylib->current_version.patch) {
xasprintf(&lib_with_version, "%s-%"PRIuFAST16".%"PRIuFAST16".%"PRIuFAST16, basename, dylib->current_version.major, dylib->current_version.minor, dylib->current_version.patch);
} else {
xasprintf(&lib_with_version, "%s-%"PRIuFAST16".%"PRIuFAST16, basename, dylib->current_version.major, dylib->current_version.minor);
}
if (LC_ID_DYLIB == loadcmd) {
pkg_addshlib_provided(pkg, lib_with_version);
} else {
pkg_addshlib_required(pkg, lib_with_version);
}
free(lib_with_version);
}
free(dylib);
break;
Expand Down
2 changes: 1 addition & 1 deletion libpkg/private/pkg_abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ enum pkg_os {
PKG_OS_NETBSD,
PKG_OS_DRAGONFLY,
PKG_OS_LINUX,
PKG_OS_MACOS,
PKG_OS_DARWIN,
};

/*
Expand Down

0 comments on commit c11e5ab

Please sign in to comment.