In this mini-article I will show you how I develop applications for Linux in XCode 4. At the moment I’m using XCode 4.2.1, in OSX Lion, with Ubuntu 10.04 running on a virtual machine.
Although the article is related to XCode, after reading and trying out for yourself, you will notice that you can use whatever IDE you like. Here I’m just showing you how you can code in a OSX-only IDE and deploy in Linux.
Requirements
You will need some tools before we start:- VMWare or similar virtual machine application for Mac
- Used to run our Linux system, and that’s where we will compile and run the target application.
You can also use a physical computer instead. However, you will need network connectivity from mac to that computer because the communication between this linux System and our Mac is done by SSH. - OSXFuse
- Requirement for the next point
- SSHFS
- It will be used to mount a remote directory in our Mac, and access it as if it was an external hard disk drive
Once you have all these, we are ready to go, but take some seconds to make sure you can compile the application in your slave Linux system. This will ensure that any problems you may encounter are not related to your slave machine or the code you are trying to compile.
Accessing slave machine using SSH
Now you will need the slave machine’s IP address, typing ifconfig in its Terminal.As you can see in the image above, in my case I’ll be using 172.16.68.128 as the slave’s IP Address.
Then we make sure we can access it from your Mac typing the next command in Mac Terminal:
ssh username@ip
If we are asked for the password, then we got a valid SSH connection and we can move on…Adding an alias for the Linux Slave IP
Because IPs can change sometimes, even inside Local Area Network, I added an alias for this IP, so that I could use it in scripts instead of the IP address itself. When it changes, I only have to modify one single file. You can do the same, by editing the hosts file, so grab the Mac Terminal and type as you see:sudo vim /etc/hostsAdd a new line in the end, pressing I to enter insert mode:
172.16.68.128 vmcodeNow press ESC to exit “INSERT mode” and type :wq (yes, including the colon). If you have any problems using vim to edit this file, you can try nano instead – just replace “vim” with “nano” in the first command of this section. Now, you can use the virtual hostname “vmcode” instead of the real IP (which will be a lot easier to remember).
Setup script files
Now we need to setup two script files. I called them mountDir and sshbuild. The first one will mount the remote directory in our Mac and the latter is used to remotely compile the application.Firstly pick a directory for these scripts, where you need to place the sshfs-static-leopard file, because it will be needed for the mountDir script.
Mounting Directory Script
Using your favorite text editor, create a new file called “mountDir” (no extension), and paste the following code:#!/bin/bash echo password | ./sshfs-static-leopard username@vmcode:codedir ~/path/to/mount/dir -o password_stdinI’m explaining some of the variables in the command, that you should change to fit your needs:
- username and password are the credentials for the user in the slave Linux machine
- codedir is the directory in slave machine you want to mount in Mac
- ~/path/to/mount/dir is the path to the Mac’s directory where you want to mount the slave’s directory
If you are using a real box instead of a VM, I recommend you configure ssh public keys instead of storing your password in plain text. You SHOULD NOT use the same password as of your Mac User
This was pointed by reader Daid, and I explained him I used it in plain text because I’m connecting to this VM, used only for compiling purposes and not accessible from outside, so I’m not that worried about it.
Building remotely
The SSH command accepts a second argument, that is the command you want to execute right after the remote login. Save and create another file called “sshbuild” with the following content:#!/bin/bash ssh username@vmcode 'cd codedir; make'Note that username and codedir must match the ones you used before.
Setting up XCode
Before we move to XCode, you first need to mount the directory: open the Terminal, cd to your scripts dir, and run sh mountDir. Now you should see the contents of remote dir in your Mac’s ~/path/to/mount/dir.Any change you make to these files will be reproduced in the remote slave system (our VM), because SSHFS will be syncing in real-time.
Start XCode, then click File > New > New Workspace… and chose a location for the workspace file (I used the same as the scripts). You should see a window with three different spaces, they should have texts in the middle, from left ro right, saying “No Files”, “No Editor” and “No Selection”. Open a Finder window in the location ~/path/to/mount/ and then drag the subfolder that has a name starting with “OSXFUSE” to the “No Files” space in XCode.A popup will show up. Firstly, XCode will prompt you about your build tool. You should type the complete path to the sshbuild script, starting on root, on the “Build Tool” field.
After clicking “Next”, you will be presented something like the image below, you should select the second option, which is “Create folder references for any added folders”.
Basically you are adding a reference to that directory, so instead of working in a local copy, you will edit the remote files directly. Now that you have all the directories in the slave’s codedir in the left panel of XCode, you can now open, edit and save any of your files using XCode. Now hit Command+B to run the build tool, and if everything goes well, you should see a message box saying Build Successful.
Running the Application
As you are developing for Linux, you should preferably debug under Linux. However, I often use an Application called X11, taht can be found in your Applications/Utilities folder. Type this command under X11 console window and the application should run in your Mac as if it was on Linux, even if it needs to open windows:ssh +x username@vmcode cd codedir./application
No comments:
Post a Comment