Porting UNIX application to MAC OS X - macos

I am student working on an academic project - 'Porting KVM to MAC OS X'. I gathered a lot of literature regarding KVM, MAC OS X etc., But, I am still unclear as how to proceed. I checked Apple's Developer website which lists a hundred things to do for the porting process.I dont understand why Mac, having UNIX at its heart, needs a lot of changes to the source code, just to make it run.
Also, I heard that Fink (and also macports) is a tool, using which I can port any Unix application to Mac OS X. Is that true? I checked Fink's website, where in I don't find any details which suggests that I can use Fink as a tool for porting. All I see is Fink (and also macports) is a package management system which has several linux applications and will run only those applications on Mac. KVM is not there on the list. So, again I am confused. Please suggest me, how to go about it? Just one step. Is the way Apple community suggesting, the only way? Please help.

I believe that you are looking at the wrong direction.
KVM is not an application. QEMU, the standard user interface for KVM, is mostly a front-end. The main part of KVM lies within the Linux kernel. You would have to provide an equivalent of that kernel code for OS X.
This is a completely different beast than application porting. There is no standard like POSIX within the kernel of an operating system - there is not even the guarantee of internal interface compatibility between different versions of the same kernel. The Linux and OS X kernels are completely different even in their basic design, since OS X uses a Mach-based kernel.
You will have to understand how both kernels work and find out what changes you need to make. Depending on how different the two kernels are, your task might even amount to a full re-implementation of KVM. You will also need a concrete understanding of how virtualization is implemented on modern CPUs and quite probably a more than passing knowledge of assembly and various low-level computer specifics.

if you find something with the functionality you are looking for, it wont be a port of KVM.
the level that you would be abstracting doesn't even exist on the mac, the bottom layers are all different...
(Approximation)
lintel ->
BIOS : BOOTLOADER : LINUX_KERNEL : INIT
Macintel->
UEFI : MACH_MICROKERNEL : BSD_STUFF : LAUNCHD
both being POSIX you might expect more underpinnings to be the same, but really they are all different...

Related

What does M1 mac optimization process for an application mean?

You know the ARM-based M1 chips that are used in modern mac computers. On those macs, some number of software are ran through the layer called Rosetta (Discord, Steam), some natively, directly through M1 (Slack, IntelliJ) and some actually doesn't work in either way (Virtual Box). Huge list holding the status can be found here.
Apps that can be ran only with Rosetta are not yet M1 optimized, their developers have to optimize it, it takes some time to do so. But what does it mean to optimize it? What the process looks like? I'm quite sure that they don't rewrite the whole application code to another language (like Swift), because Jetbrains was able to M1 optimize their apps quite quickly. On the other hand, Discord is not yet optimized, same for Unity game engine (it's in beta though).
At bottom, it just means that the compiler's backend was configured to emit ARM64 instructions for the program instead of (or in-addition to) x86-64 instructions.
This means that certain x86-64 specific functionality instruction can no longer be used, unless equivalent ARM instructions are used instead.
This usually isn't much of a problem though, because most macOS software is typically written at a higher level of abstraction, using system-provided frameworks.
For example, using CoreImage to manipulate images abstracts you from the details of the CPU and GPU. In such cases, Apple does the heavy lifting of porting over their frameworks. All you have to do as an application developer is to check a box that says "target ARM64".

Getting started with OSX kernel programming

For someone familiar with Linux kernel programming, what are some resources for getting started with OSX kernel programming? I've read some of the Apple Developer resources, but they seem fairly generic (e.g. basic concurrency control primitives). Specifically I am interested in file system development.
Amit Singh's book "Mac OS X Internals" contains a chapter describing the Implementation of HFS+, which might be helpful. If you find resources describing BSD's VFS layer, that might help too, as that's where OS X's VFS layer originates (though with its own page cache, called the Unified Buffer Cache or UBC). Moreover, you could try poking around in the source code of MacFuse and its descendants. Looking at the source of some of the simpler file systems (HFS+ is a bit big for this) will probably also help.

How closely are Mac OS X and BSD related?

I read that Mac OS X and bsd are related. How closely are they related. Can Mac OS X software be tweaked and installed on BSD?
Back in the days of OS X 10.4 I spent some time failing to write a VFS for OS X. In those days, of the major subsystems of the kernel, only the network stack and the VFS were still truly BSD. At that time, even the VFS had been partly rewritten to make it more modular (all the BSD VFS data structures became opaque pointers and the API was through what were called KPI functions). I believe the network stack was going the same way. There was also a thin layer at the interface with userland that made the OS look like BSD to userland programs.
Everything else had been pretty much rewritten or replaced: memory management, process management etc came from the Mach microkernel; the device driver subsystem was written from the ground up by Apple.
In terms of userland programming, OS X is very similar to BSD and programs written for BSD should be easily portable. However, OS X has a lot of APIs that aren't available in BSD. These include almost everything to do with the user interface - graphics, sound etc. There are also other interfaces that don't exist in BSD such as the launch API which is the OS X preferred way of launching background processes.
The Wikipedia BSD article is good (and accords with my own understanding, for what that's worth). It says that Darwin, the system on which Apple's Mac OS X is built, is a derivative of 4.4BSD-Lite2 and FreeBSD, and notes that 4.4BSD is the last release that Berkeley was involved with.
So, Darwin is as BSD as you can get (just like all the other BSDs!). OS X refers to those parts of the distribution which aren't open-source, principally the GUI, but including a variety of frameworks, and anything which relies on these won't be portable.
OS X as a whole is a UNIX 03 system. That's equivalent to being a truly POSIX-compliant system (as opposed to being POSIX-like).
As other answers have noted, the userland parts of the OS are unsurprising to anyone with much unix experience, and I've rarely had any difficulty building portable-unix software on OS X.
In contrast, the non-userland parts of the OS are pretty different. Apple seems to be willing to innovate in those areas fairly cheerfully. I think (but I'm not positive) that these changes are formally part of Darwin. One of the most obvious differences is that launchd has replaced cron, at, inetd, and much of the startup infrastructure.
If the Mac software uses Cocoa, Apple's proprietary display library (which it does, if it runs on the Mac with a GUI and does not require starting an X server), then you may have some issues running the code on a normal BSD system.
If your code only uses POSIX-specified functions, it will cleanly port to Linux, BSD, and even Windows.
It is true that Mac OS X and BSD are related. Although they have different kernels, they share a common ancestor and significant userland code. Obviously, I can't quantify "how close" - that's subjective.
Mac OS X is one flavor of BSD Unix. As Borelaid already pointed out, that does not necessarily mean that porting Mac apps to other flavors of BSD would be easy or even manageable, much less so than between other common BSD flavors. Every one of them brings their own specifics, and OS X more than most.
Porting programs from other BSD flavors to OS X also involves work and does not always work (smoothly or at all), but is usually much more straightforward.
It depends on what kinds of apps you are trying to port. If you write POSIX-compatible C/C++ console programs, they will compile and work just fine under any POSIX-compatible system (mostly Linux and BSD flavors), but note that OS X often doesn't implement the newest POSIX functions (e.g. utimensat) which are available in Linux. On the other hand, graphical applications use Cocoa or the older Carbon, which would require GNUstep. Porting graphical applications is quite uncommon because every graphical environment has different design standards and conventions, so graphical applications usually have to be written from scratch for each graphical environment.
Installed - no.
Ported from source, maybe - see Gnustep, Cocotron, EtoileOS - all of which offer varying degrees of compatibility with the Cocoa development stack (but not with the older Carbon).
There is a limited amount of x-platform objective-C software developed that way.

Help writing a DVB driver for OS X

I'm looking at options to access DVB data on OS X. Initially I want to support the EyeTV DTT USB device, but in the long-run I'd like to support a number of popular devices. The problem I have is that there is no standard way of controlling such devices.
All the applications I know of that use them either hide the driver code within the application (for example EyeTV itself, all it's drivers are implemented totally in userspace and are not accessible to external apps), or they use the seemingly defunkt MMInputFamily driver (no source code availible any more, author gone walkabouts).
I've done some research and found that a number of the devices I want to support are supported within the Linux DVB project. Further research indicates that some years ago there was an attempt to abstract the linux implementation so that it could potentially be recompiled on other platforms. The idea being that efforts to support devices should be pooled and the best way to do that would be to make the current open source implementation work on multiple platforms: it seems in the end to have amounted to little however.
The idea of compiling linux drivers against other *nix type platforms has also been taken up elsewhere with some success. The approach the author took is detailed on the page I linked, it seems potentially viable on OS X as well.
At any rate, there seem to be a number of options, but no clear winner:
Find the source code for the MMInputFamily driver, try to get it working on OS X 10.6 and add support for the devices I require, referrencing the linux source code for pointers. Problem: the source code is nowhere to be found, nor is the author. Additionally it seems the author might perhaps have gone down another route had he fully appreciated the previous efforts to port the linux drivers to OS X.
Attempt to port the linux drivers to OS X in a manner similar to the FreeBSD project I linked. Problem: this is very low-level work and work in this layer is not recommended by Apple if it can be avoided.
Write a driver with OS X's IOKit: this is the preferred method for implementing drivers but I would have to do everything from scratch, clearly not a small job.
If I could I would really like to use the Linux source code, but I'm unsure if such a thing is really viable. Does anyone have any advice or ideas on the best way to proceed with this task?

How can a Windows programmer be sufficiently productive on Mac OS X?

I've been using MacBook Pro for a few months at home, and I was wondering if there's a good book or guide that can help me be a better programmer on Mac. Maybe Mac-equivalent of Beginning Linux Programming. Note I am not looking for resource on how to program Mac application, instead I am looking for more general guide of using Mac for general development environment.
As a background, I am a Windows programmer by day. I've also done some Linux and BSD over the years, esp in school, like socket programming, graphics, make install type stuff. At home, I'll be doing Java, Scala, PHP, etc. on Mac.
So far, I've been using Eclipse, QuickSilver, and TextMate. VMWare Fusion, XCode and NetBeans are set up, but I don't use them. A DVI KVM switch is hooked up to real keyboard, trackball, and monitor. Recently stayed up till late fighting with MacPorts, and figured out I needed x86_64. The most struggle I had was configuring PHP. I don't know why they don't ship with MySQL and GD library. I eventually figured it out Googling around, and built the extensions from source. I have a feeling that I didn't get the memo and didn't read some basic guide on how to become a programmer on Mac, like how the whole architecture works. How can a Windows programmer be sufficiently productive on Mac OS X?
Related: Setting up a Mac for programmers
Edit: The specific type of application I want to develop doesn't really matter in my opinion. It could be Java, Scala, PHP as I mentioned or Cocoa, C++, or whatever.
What I am looking for is specific book, resource, advice on how to be more effective programmer on Mac, preferably something beyond "install XYZ".
Having converted from Windows to Mac OS X about five years ago, I often find myself thinking the same thing. I just cannot be productive on Windows (as much, I can be productive) as I can on Mac OS X.
To be honest, there are lots of small differences between Mac OS X and Windows. I find the biggest reason for people thinking like this (at it normally only applies to gamers and developers) is that they are trying to use the Mac like a Windows machine. You need to learn to accept that you have to use the command key, not the control key, etc.
It sounds like you are using a Mac because you have to as opposed to because you want to. It really is a much better platform than Windows once you get used to it.
I think a lot of Windows programmers come to Mac and don't try to learn it properly because they are complacent thinking they know it all because they have "used Windows all their life". I guess once you discover Spotlight, Expose, Mac OS X Keyboard shortcuts, etc. You will find your self being MUCH more productive that you ever were on Windows.... and its actually a fun OS to use.
Checkout some of the best Mac applications you can get here and here. You can also do a search for "top 100 mac apps".
Also, I noticed you were trying to setup some kind of web server directly into Mac OS X. It does ship with one, but if you are going to add MySQL and some other extensions I wouldn't go the MacPorts route. Get VMWare Fusion or VirtualBox (open source) and run the server in a VM. Much cleaner. I have a subversion/trac FreeBSD VM that handles my local version control.
I would like to add that if you don't presently use Xcode, you should definitely learn it and use it asap. It's a much nicer IDE to use than Visual Studio and it will make your life much easier.
Don't forget you have probably spent years on Windows help sites, you're going to a small degree need to do that with the Mac. Whenever you have a problem about using the Mac, ask a question on ServerFault. We are all more than eager to help you out.
Good luck.
You seem to want an overview of how Mac OS X works at a system level, more than recomenations about tools and so forth. If that's the case, I'd start with the (very basic) Mac OS X System Architecture Guide from Apple, then move on to Getting Started with Mac OS X, which should give you enough of an overview to get started.
It's not clear from your question what you intend to actually make with your programming time, but if you decide to persue Cocoa/OS X development, I recommend Cocoa Programming for Mac OS X by Aaron Hillegass.
I have a similar situation like yours. I use Windows for development and about a year back purchased a MBP for home (as I shifted to an office). I find it really difficult to get any real work done on my MBP. Somehow am used to the Windows environment with dual screens. But let that not stop you. A couple of software which I suggest you should get are:
Transmit - Good ftp client
MAMP - Runs a webserver nearly out-of-the-box. Good for basic development
Quicksilver - Helps in quick finding of applications
Spaces along with gestures (Configure your gestures to move from one screen to another, I use three fingers glide. its amazing)
Entourage - for email
Terminal - for ssh (putty alternative) (included)
Dreamweaver/BBedit/Textmate (all pretty decent. but i love editplus on windows. not a fan of IDE)
I assume your question is not about learning COCOA and more about being more effective using a MAC. Well, the above tools might help you.
Unfortunately, your question isn't very clear as to what you really want.
If you're looking to write anything cross-platform, it can be very helpful to have a virtual machine for testing. When in Linux, I've always used VirtualBox, and it works on OS X as well.
Also, as for choice of IDE, a lot of it comes down to your preference. Eclipse is nice because there's a plugin for almost everything for it. My experience with TextMate is limited, but my local Ruby Users Group swears by it.
Finally, a suggestion for not just Mac, but any platform really. Learn your hotkeys, set up new ones for things you commonly do, and use them frequently. Not having to take your hands off the keyboard to click a mouse through a few menus can really improve productivity. It may take a little time for them to grow on you, but once they become second nature, you'll wonder how you lived without them.
Basically, you can apply all your Linux/UNIX knowledge that you already have to the Mac. If you use the Terminal (/Applications/Utilities/Terminal.app) you can run all your favorite UNIX commands. Mac has a special command called "open" which is equivalent to the Windows "start" command (used to launch programs and files). You can also use "open -a" to open an application by name (e.g. "open -a Finder").
You might want to reconsider Xcode. Xcode opens more quickly than Eclipse and provides very good syntax highlighting, brace matching, block indenting, and more. Xcode doesn't have to be used as an IDE, you can also use it as a code editor, just like you are currently using TextMate.
For code editing (and everything else), try Aquamacs (http://www.aquamacs.org). It's a Cocoa-native build of Emacs, and it's brilliant for any programming task.

Resources