Controlling process architecture from Cocoa?

From Cocoa, how can I do the following two things:

  1. Determine the architectures available within a binary? The equivalent of 'file foo.dylib', but by calling an API rather than launching a task to run that command.

  2. Launch a process using a specific architecture in a universal binary, say an executable with both arm64 and x86_64, and I want to launch the x86_64 arch on an M1 Mac. The equivalent of 'arch -arch x86_64 foo', but perhaps by specifying an option to NSTask rather than launching an 'arch' task.

Here are straw man answers to my own questions. I'm still hoping someone can give me better answers.

  1. Just open the binary file and read the first few bytes of it. Traipse through the cafe babes and the feed faces to find the cputype, then check it against the defined values for arm64 or x86_64. The formats are defined in <mach-o/loader.h> and <mach-o/fat.h>. Another possibility would be to do a preflight dlopen(); if it works, you know the binary contains your current architecture. But that possibility won't work for me for other reasons.

  2. Don't use NSTask. Use posix_spawn, giving it a posix_spawn_attr that contains a posix_spawnattr_setbinpref_np, into which you can put the cputype you prefer. This is much more complex than using NSTask, and I can see why it hasn't been added to that class, but I sure wish it was. In my case, I will take the more pragmatic approach of just creating two non-fat binaries, one for each architecture. Then I can choose which one I want to launch, and do so using NSTask.

Controlling process architecture from Cocoa?
 
 
Q