[Haskell-cafe] Better integration with Xcode

haskell cafe haskellcafe at gmail.com
Wed Nov 6 23:47:44 UTC 2013


On Tue, Nov 5, 2013 at 5:05 PM, haskell cafe <haskellcafe at gmail.com> wrote:

> I'm pretty new to Haskell, but have been having fun learning it. Being a
> Mac and Windows programmer most of my life, I'm used to using GUIs and IDEs
> to program and don't really care for using command-line tools. As such,
> I've been working on getting Xcode to build Haskell files directly and I
> have something working that I thought others might be interested in.
>
> Looking through the "Building/Cross-Compiling/iOS" instructions, it looks
> like the standard way of building Haskell files is to build them on the
> command line into a library and then link your C/C++/Objective-C sources
> against the library. But you don't need to do this. You can directly edit
> Haskell in Xcode (with very basic syntax highlighting), and build it from
> Xcode all without ever going to the command-line.
>
> Would people be interested in working this way? If so, I'll be happy to
> post steps to do it.
>

A couple people indicated off-list that they would be interested in this,
so here are the details. I'll send information about getting Xcode to do
syntax coloring in a separate email.

You can get Xcode to compile source files from any language and link them
into your C/C++/Objective-C/Objective-C++ program by using Build Rules.
Note that this is different from adding a Run Script Build Phase. This is a
Build Rule, not a Build Phase.

In Xcode 5:

1) Click on your Project in the Project Navigator

2) Click on the Target in the Project editor

3) Click on the "Build Rules" tab (Note: *Not* "Build Settings" or "Build
Phases").

4) From the "Editor" menu, choose "Add Build Rule"

This will create a new build rule in your project. It should look like this:
[image: Inline image 1]
5) From the "Process" popup menu (in the new build rule), select "Source
Files With Names Matching:" and enter "*.hs" into the text field next to it
(leaving out the quotation marks). (Being new to Haskell, I'm unclear on
the difference between ".hs" and ".lhs" files. But if ".lhs" files are
compiled by ghc, you can do these steps a second time and replace "*.hs"
with "*.lhs".)

6) From the "Using" popup menu, choose "Custom Script:"

7) Enter the following for the script:

#!/bin/sh
ghc -c -odir ${DERIVED_FILE_DIR} ${SCRIPT_INPUT_FILE}

The above runs a bourne shell script that simply invokes ghc, tells it to
compile the next Haskell source file, and put the .o into Xcode's derived
sources folder with all the other .o files that your Xcode project
produces. Feel free to add whatever other ghc options you need to compile
your files.

8) In the "Output Files" section, click the "+" button. You'll be prompted
to enter a filename. Enter this:

$(DERIVED_FILE_DIR)/$(INPUT_FILE_BASE).o

That says that the output files from the build rule will be in the Derived
Files directory, and they will have a name that's the same as the input
file, but instead of ending in ".hs" it will end in ".o" as object files
should. This is what allows Xcode to understand dependencies in Haskell.
(It's not perfect - it won't recognize when you update a module used by
Haskell source file, but it's good enough to recognize that you changed the
source file which produces a given object file and will rebuild that object
file as appropriate.)

9) Add your Haskell source files to your Xcode project.

10) Be sure to call "hs_init()" and "hs_exit()" in your
C/C++/Objective-C/Objective-C++ source so that you can safely call and use
Haskell code in your project.

11) Add any libraries that are required to link against the Haskell source.
In my simple example, I had to link against libiconv.2.dylib,
libHSinteger-gmp-0.5.0.0.a, libHSghc-prim-0.3.0.0.a, libHSbase-4.6.0.1.a,
and libHSrts.a. You may need a different set of libraries for your project
depending on what your Haskell source does. The description at <
http://www.haskell.org/haskellwiki/Using_Haskell_in_an_Xcode_Cocoa_project#Add_Haskell_libraries.2C_compile_and_run>
has a pretty good description of how to figure out which ones you need.

That's it! Let me know if you need any clarification on any parts of this.

-DC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20131106/59a8b53a/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: HaskellInXcode.png
Type: image/png
Size: 239795 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20131106/59a8b53a/attachment-0001.png>


More information about the Haskell-Cafe mailing list