Xcode Swig Build Rule

28 November 2011

I've been playing with Swig in Xcode to build interfaces in Lua for my game engine. Here is the build rule that I added to Xcode so it knows how to build the .i (interface) files

Set source files with names matching to *.i

Set Using Custom Script and fill in the script with:

IFS=' ' read -ra header_paths_arr <<< "$HEADER_SEARCH_PATHS"
header_path_string=""
for arg in ${header_paths_arr[@]}; do
    header_path_string+="-I$arg "
done

/usr/local/bin/swig ${header_path_string} -lua -c++ ${INPUT_FILE_PATH}

The output files is set to

${INPUT_FILE_DIR}/${INPUT_FILE_BASE}_wrap.cxx

The only real 'clever' bit here is the parsing of the environment variable HEADER_SEARCH_PATHS in to a string of the form

-Isearchpath1 -Isearchpath2 -I ...

so that swig can find any header that the interface file references.

The downside of this approach is that if you change any header file that the interface (.i) file depends on Xcode doesn't know that it should rebuild the interface. There is no way of adding this information to Xcode ( in Xcode 4.x anyway). You could work around this by invoking a shell script or a Makefile but I didn't get that far.

In the end I am not going to use Swig because there is no easy way to instantiate an object in c++ from lua and override it's virtual function in lua. This is possible in tolua++ with a few modifications so I am switching to that. I'll detail that in another blog post.

comments powered by Disqus