Advertising

Compiling Lua for iOS

To compile Lua for use on iPhone and iPad, I've grabbed the original source code for 5.1.4, and I have grabbed build_for_iphoneos.sh shell script by Christopher Stawarz. Both were slightly modified since Lua does not use autoconf, and build_for_iphoneos.sh is slightly dated. Read more to see what did I do.

You can read the full description with full copies of the files that need to be changed, or you can review diffs on the bottom of the post.

Full description

In "build_for_iphoneos.sh" I had to remove use of configure. I added a call to "make". I've exported "prefix" so I can use it in modified Lua's Makefile. Additionally, the script presumes older iOS SDKs will be shipped in newer Xcode releases, which isn't the case for quite a while now; instead one needs to build with the latest SDK and specify that an older iPhone is a target.

Here's the modified script:

#!/bin/bash

################################################################################
#
# Copyright (c) 2008-2009 Christopher J. Stawarz
# Modifications (c) 2011 Ivan Vucica
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
################################################################################



# Disallow undefined variables
set -u


default_gcc_version=4.2
default_iphoneos_version=3.1.2
default_macos_version=10.5

current_iphone_sdk=4.2

GCC_VERSION="${GCC_VERSION:-$default_gcc_version}"
export IPHONEOS_SDK="${IPHONEOS_DEPLOYMENT_TARGET:-$current_iphone_sdk}"
export IPHONEOS_DEPLOYMENT_TARGET="${IPHONEOS_DEPLOYMENT_TARGET:-$default_iphoneos_version}"
export MACOSX_DEPLOYMENT_TARGET="${MACOSX_DEPLOYMENT_TARGET:-$default_macos_version}"


usage ()
{
    cat >&2 << EOF
Usage: ${0##*/} [-ht] [-p prefix] target [configure_args]
	-h	Print help message
	-p	Installation prefix (default: \$HOME/Developer/Platforms/...)
	-t	Use 16-bit Thumb instruction set (instead of 32-bit ARM)

The target must be "device" or "simulator".  Any additional arguments
are passed to configure.

The following environment variables affect the build process:

	GCC_VERSION			(default: $default_gcc_version)
	IPHONEOS_DEPLOYMENT_TARGET	(default: $default_iphoneos_version)
	MACOSX_DEPLOYMENT_TARGET	(default: $default_macos_version)

EOF
}


while getopts ":hp:t" opt; do
    case $opt in
	h  ) usage ; exit 0 ;;
	p  ) prefix="$OPTARG" ;;
	t  ) thumb_opt=thumb ;;
	\? ) usage ; exit 2 ;;
    esac
done
shift $(( $OPTIND - 1 ))

if (( $# < 1 )); then
    usage
    exit 2
fi

target=$1
shift

case $target in

    device )
	arch=armv6
	platform=iPhoneOS
	extra_cflags="-m${thumb_opt:-no-thumb} -mthumb-interwork"
	;;

    simulator )
	arch=i386
	platform=iPhoneSimulator
	extra_cflags="-D__IPHONE_OS_VERSION_MIN_REQUIRED=${IPHONEOS_DEPLOYMENT_TARGET%%.*}0000"
	;;

    * )
	usage
	exit 2

esac


platform_dir="/Developer/Platforms/${platform}.platform/Developer"
platform_bin_dir="${platform_dir}/usr/bin"
platform_sdk_dir="${platform_dir}/SDKs/${platform}${IPHONEOS_SDK}.sdk"
prefix="${prefix:-${HOME}${platform_sdk_dir}}"

export CC="${platform_bin_dir}/gcc-${GCC_VERSION}"
export CFLAGS="-arch ${arch} -pipe -Os -gdwarf-2 -isysroot ${platform_sdk_dir} ${extra_cflags}"
export LDFLAGS="-arch ${arch} -isysroot ${platform_sdk_dir}"
export CXX="${platform_bin_dir}/g++-${GCC_VERSION}"
export CXXFLAGS="${CFLAGS}"
export CPP="/Developer/usr/bin/cpp-${GCC_VERSION}"
export CXXCPP="${CPP}"


#./configure \
#    --prefix="${prefix}" \
#    --host="${arch}-apple-darwin" \
#    --disable-shared \
#    --enable-static \
#    "$@" || exit
export prefix
make "$@" || exit
make install "$@" || exit

cat >&2 << EOF

Build succeeded!  Files were installed in

  $prefix

EOF

I've called this modified script "build_for_iphoneos_noconfigure.sh", and just like the original one, I've placed it in my path for easier access at a later time.

Next, we need to make slight adaptations to the Makefile in "src/" subdirectory of lua-5.1.4. Primarily, we need to introduce use of LDFLAGS in addition to MYLDFLAGS in a few spots, since they're set by "build_for_iphoneos_noconfigure.sh". We need to remove setting of CC and CFLAGS, since they will also be set by the shell script.

# makefile for building Lua
# see ../INSTALL for installation instructions
# see ../Makefile and luaconf.h for further customization

# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================

# Your platform. See PLATS for possible values.
PLAT= none

#CC= gcc
#CFLAGS= -O2 -Wall $(MYCFLAGS)
AR= ar rcu
RANLIB= ranlib
RM= rm -f
LIBS= -lm $(MYLIBS)

MYCFLAGS=
MYLDFLAGS=
MYLIBS=

# == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE =========

PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris

LUA_A=	liblua.a
CORE_O=	lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \
	lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o  \
	lundump.o lvm.o lzio.o
LIB_O=	lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
	lstrlib.o loadlib.o linit.o

LUA_T=	lua
LUA_O=	lua.o

LUAC_T=	luac
LUAC_O=	luac.o print.o

ALL_O= $(CORE_O) $(LIB_O) $(LUA_O) $(LUAC_O)
ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T)
ALL_A= $(LUA_A)

default: $(PLAT)

all:	$(ALL_T)

o:	$(ALL_O)

a:	$(ALL_A)

$(LUA_A): $(CORE_O) $(LIB_O)
	$(AR) $@ $?
	$(RANLIB) $@

$(LUA_T): $(LUA_O) $(LUA_A)
	$(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) $(LDFLAGS)

$(LUAC_T): $(LUAC_O) $(LUA_A)
	$(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) $(LDFLAGS)

clean:
	$(RM) $(ALL_T) $(ALL_O)

depend:
	@$(CC) $(CFLAGS) -MM l*.c print.c

echo:
	@echo "PLAT = $(PLAT)"
	@echo "CC = $(CC)"
	@echo "CFLAGS = $(CFLAGS)"
	@echo "AR = $(AR)"
	@echo "RANLIB = $(RANLIB)"
	@echo "RM = $(RM)"
	@echo "MYCFLAGS = $(MYCFLAGS)"
	@echo "MYLDFLAGS = $(MYLDFLAGS)"
	@echo "MYLIBS = $(MYLIBS)"

# convenience targets for popular platforms

none:
	@echo "Please choose a platform:"
	@echo "   $(PLATS)"

aix:
	$(MAKE) all CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-ldl" MYLDFLAGS="-brtl -bexpall"

ansi:
	$(MAKE) all MYCFLAGS=-DLUA_ANSI

bsd:
	$(MAKE) all MYCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-Wl,-E"

freebsd:
	$(MAKE) all MYCFLAGS="-DLUA_USE_LINUX" MYLIBS="-Wl,-E -lreadline"

generic:
	$(MAKE) all MYCFLAGS=

linux:
	$(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-Wl,-E -ldl -lreadline -lhistory -lncurses"

macosx:
	$(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-lreadline"
# use this on Mac OS X 10.3-
#	$(MAKE) all MYCFLAGS=-DLUA_USE_MACOSX

mingw:
	$(MAKE) "LUA_A=lua51.dll" "LUA_T=lua.exe" \
	"AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \
	"MYCFLAGS=-DLUA_BUILD_AS_DLL" "MYLIBS=" "MYLDFLAGS=-s" lua.exe
	$(MAKE) "LUAC_T=luac.exe" luac.exe

posix:
	$(MAKE) all MYCFLAGS=-DLUA_USE_POSIX

solaris:
	$(MAKE) all MYCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-ldl"

# list targets that do not create files (but not all makes understand .PHONY)
.PHONY: all $(PLATS) default o a clean depend echo none

# DO NOT DELETE

lapi.o: lapi.c lua.h luaconf.h lapi.h lobject.h llimits.h ldebug.h \
  lstate.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h \
  lundump.h lvm.h
lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h
lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h
lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \
  lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \
  ltable.h
ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h
ldebug.o: ldebug.c lua.h luaconf.h lapi.h lobject.h llimits.h lcode.h \
  llex.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h \
  lfunc.h lstring.h lgc.h ltable.h lvm.h
ldo.o: ldo.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
  lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lparser.h lstring.h \
  ltable.h lundump.h lvm.h
ldump.o: ldump.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h \
  lzio.h lmem.h lundump.h
lfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h lmem.h \
  lstate.h ltm.h lzio.h
lgc.o: lgc.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
  lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h
linit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h
liolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h
llex.o: llex.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h ltm.h \
  lzio.h lmem.h llex.h lparser.h lstring.h lgc.h ltable.h
lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h
lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
  ltm.h lzio.h lmem.h ldo.h
loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h
lobject.o: lobject.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h \
  ltm.h lzio.h lmem.h lstring.h lgc.h lvm.h
lopcodes.o: lopcodes.c lopcodes.h llimits.h lua.h luaconf.h
loslib.o: loslib.c lua.h luaconf.h lauxlib.h lualib.h
lparser.o: lparser.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \
  lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h \
  lfunc.h lstring.h lgc.h ltable.h
lstate.o: lstate.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
  ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h llex.h lstring.h ltable.h
lstring.o: lstring.c lua.h luaconf.h lmem.h llimits.h lobject.h lstate.h \
  ltm.h lzio.h lstring.h lgc.h
lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h
ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
  ltm.h lzio.h lmem.h ldo.h lgc.h ltable.h
ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h
ltm.o: ltm.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h lzio.h \
  lmem.h lstring.h lgc.h ltable.h
lua.o: lua.c lua.h luaconf.h lauxlib.h lualib.h
luac.o: luac.c lua.h luaconf.h lauxlib.h ldo.h lobject.h llimits.h \
  lstate.h ltm.h lzio.h lmem.h lfunc.h lopcodes.h lstring.h lgc.h \
  lundump.h
lundump.o: lundump.c lua.h luaconf.h ldebug.h lstate.h lobject.h \
  llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h lundump.h
lvm.o: lvm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
  lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h
lzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \
  lzio.h
print.o: print.c ldebug.h lstate.h lua.h luaconf.h lobject.h llimits.h \
  ltm.h lzio.h lmem.h lopcodes.h lundump.h

# (end of Makefile)

Finally, you need to introduce a slight change to the Makefile in root of Lua's distribution. INSTALL_TOP needs to be a replica of "prefix" environment variable since it's set by the shell script.

Here's just the excerpt of the top of the Makefile. I think you'll easily notice what's

# makefile for installing Lua
# see INSTALL for installation instructions
# see src/Makefile and src/luaconf.h for further customization

# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================

# Your platform. See PLATS for possible values.
PLAT= none

# Where to install. The installation starts in the src and doc directories,
# so take care if INSTALL_TOP is not an absolute path.
INSTALL_TOP= ${prefix}
INSTALL_BIN= $(INSTALL_TOP)/bin
INSTALL_INC= $(INSTALL_TOP)/include
INSTALL_LIB= $(INSTALL_TOP)/lib
INSTALL_MAN= $(INSTALL_TOP)/man/man1

###### -- continues below --
 

That's it. Now you can compile Lua for platform "generic" (trying to compile for "macosx" pulls in readline which isn't available on iPhone):

build_for_iphoneos_noconfigure.sh device generic

Output should end with:

Build succeeded!  Files were installed in

  /Users/ivucica/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk

Excerpt from documentation for "build_for_iphoneos.sh", which explains how to set up your Xcode project:

By default, the script will install files in a subdirectory of $HOME/Developer/Platforms that mirrors the layout of /Developer/Platforms. For example, the installation prefix for a device build defaults to $HOME/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.1.sdk. The advantage of this is that you can make Xcode aware of the installed files for both the simulator and the device by adding the following under "All Configurations" in your project settings:

Header Search Paths: $(HOME)/$(SDK_DIR)/include Library Search Paths: $(HOME)/$(SDK_DIR)/lib

Of course, you can move these things into a better place, or you can attempt to design an SDK, similar to what I've done when adding iPhone support to libapril and friends.

Just the diffs

Using Mercurial I quickly made the following diff for lua-5.1.4, if you're interested only in diff on Makefiles:

diff -r 130652fce1b4 Makefile
--- a/Makefile  Fri Mar 04 14:13:36 2011 +0100
+++ b/Makefile  Fri Mar 04 14:13:56 2011 +0100
@@ -9,7 +9,7 @@
 
 # Where to install. The installation starts in the src and doc directories,
 # so take care if INSTALL_TOP is not an absolute path.
-INSTALL_TOP= /usr/local
+INSTALL_TOP= ${prefix}
 INSTALL_BIN= $(INSTALL_TOP)/bin
 INSTALL_INC= $(INSTALL_TOP)/include
 INSTALL_LIB= $(INSTALL_TOP)/lib
diff -r 130652fce1b4 src/Makefile
--- a/src/Makefile      Fri Mar 04 14:13:36 2011 +0100
+++ b/src/Makefile      Fri Mar 04 14:13:56 2011 +0100
@@ -7,8 +7,8 @@
 # Your platform. See PLATS for possible values.
 PLAT= none
 
-CC= gcc
-CFLAGS= -O2 -Wall $(MYCFLAGS)
+#CC= gcc
+#CFLAGS= -O2 -Wall $(MYCFLAGS)
 AR= ar rcu
 RANLIB= ranlib
 RM= rm -f
@@ -52,10 +52,10 @@
        $(RANLIB) $@
 
 $(LUA_T): $(LUA_O) $(LUA_A)
-       $(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(LUA_A) $(LIBS)
+       $(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) $(LDFLAGS)
 
 $(LUAC_T): $(LUAC_O) $(LUA_A)
-       $(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS)
+       $(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) $(LDFLAGS)
 
 clean:
        $(RM) $(ALL_T) $(ALL_O)

And diff for the build_for_iphoneos.sh:

--- build_for_iphoneos.sh       2011-03-04 12:38:06.000000000 +0100
+++ build_for_iphoneos_noconfigure.sh   2011-03-04 13:48:05.000000000 +0100
@@ -36,7 +36,10 @@
 default_iphoneos_version=3.1.2
 default_macos_version=10.5
 
+current_iphone_sdk=4.2
+
 GCC_VERSION="${GCC_VERSION:-$default_gcc_version}"
+export IPHONEOS_SDK="${IPHONEOS_DEPLOYMENT_TARGET:-$current_iphone_sdk}"
 export IPHONEOS_DEPLOYMENT_TARGET="${IPHONEOS_DEPLOYMENT_TARGET:-$default_iphoneos_version}"
 export MACOSX_DEPLOYMENT_TARGET="${MACOSX_DEPLOYMENT_TARGET:-$default_macos_version}"
 
@@ -103,7 +106,7 @@
 
 platform_dir="/Developer/Platforms/${platform}.platform/Developer"
 platform_bin_dir="${platform_dir}/usr/bin"
-platform_sdk_dir="${platform_dir}/SDKs/${platform}${IPHONEOS_DEPLOYMENT_TARGET}.sdk"
+platform_sdk_dir="${platform_dir}/SDKs/${platform}${IPHONEOS_SDK}.sdk"
 prefix="${prefix:-${HOME}${platform_sdk_dir}}"
 
 export CC="${platform_bin_dir}/gcc-${GCC_VERSION}"
@@ -115,14 +118,15 @@
 export CXXCPP="${CPP}"
 
 
-./configure \
-    --prefix="${prefix}" \
-    --host="${arch}-apple-darwin" \
-    --disable-shared \
-    --enable-static \
-    "$@" || exit
-
-make install || exit
+#./configure \
+#    --prefix="${prefix}" \
+#    --host="${arch}-apple-darwin" \
+#    --disable-shared \
+#    --enable-static \
+#    "$@" || exit
+export prefix
+make "$@" || exit
+make install "$@" || exit
 
 cat >&2 << EOF
 


rest of the post
About me

Correct check for Game Center availability

Apple's example code for Game Center does something strange -- in fact, we can simply go out and call those things bugs. Keep reading to see what I'm talking about. Here's the code.

        // check for presence of GKLocalPlayer API
        Class gcClass = (NSClassFromString(@"GKLocalPlayer"));
        
        // check if the device is running iOS 4.1 or later
        NSString *reqSysVer = @"4.1";
        NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
        BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);

        return (gcClass && osVersionSupported);

Well, that's incorrect and doesn't count on the fact that Game Center is unavailable on iPhone 3G despite iOS 4.1 and 4.2 existing there. If your goal wasn't to avoid crashes caused by Game Center classes not existing, but to check if Game Center really is available, you need to check if you are running on iPhone 3G. Update April 6th 2011: I have missed a few lines in the documentation that say you will receive GKErrorNotSupported when authenticating local player, if Game Center is not available. However, a library I'm working on needs this sort of check to return availability prior to authenticating player, to determine best achievement engine. You may need this as well, so I'm keeping this up. An excellent way to do this was posted on iOS Developer Tips and uses sysctlbyname() C function found in BSD operating systems such as OS X or iOS to query the kernel for value of hw.machine string.

The above method proposes adding a category to UIDevice that adds -(NSString*)machine. While this would be a most excellent way to do this, it is not an appropriate way to handle stuff in a C++ library; it should not add categories or anything like that unless it is absolutely necessary, so I just patched the function that checks availability of Game Center directly.

Compared to the iOS Developer Tips' solution, I also added a check whether or not sysctlbyname() fails. This should prevent any problems in case Apple decides to remove support for hw.machine (although I see no reason for them doing that).

We don't need to check for iPhone, iPod Touch (no iOS4) or iPod Touch 2G (Game Center available).

Let's take a look at the code:

    bool GameCenterAchievementsService::isGameCenterAvailable()
    {
        // check for presence of GKLocalPlayer API
        Class gcClass = (NSClassFromString(@"GKLocalPlayer"));
        
        // check if the device is running iOS 4.1 or later
        NSString *reqSysVer = @"4.1";
        NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
        BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
        
        // device must not be an iphone 3g
        bool validDevice = true;
        {
            size_t size;
            if(sysctlbyname("hw.machine", NULL, &size, NULL, 0)!=-1)
            {
                char*name = (char*)malloc(size);
                if(sysctlbyname("hw.machine", name, &size, NULL, 0)!=-1)
                {
                    if (!strcmp(name, "iPhone1,1") || !strcmp(name, "iPhone1,2") || !strcmp(name, "iPod1,1")) 
                    {
                        validDevice = false;
                    }
                }
                free(name);
            }
        }
        
        return (gcClass && osVersionSupported && validDevice);
    }

Note that this is Objective-C++, but is trivial to patch for pure Objective-C. Report bugs in comments section below!


rest of the post

License for your next free software/open source project

You're a budding developer. You're writing a program. FSF has its lengthy explanations about many licenses, as does OSI. Yet, you want a short and concise answer.

Here's an overview of some of the licenses I consider for my projects, their major differences, and why I pick each of them. I'll also describe how to apply them.

Note: I Am Not A Lawyer, and applying these is at your own risk. Don't apply them blindly, and talk to your employer; you might not be able to decide licensing for your own work. Also, don't use these notes as info on what you may or may not do with code you know to be GPL'd, LGPL'd or BSD-licensed.  Read the licenses themselves; these are notes indended only to help in quick decision, not to explain the licenses in great detail.

This is also largely done from memory, without doing proper research right now; it should be pretty correct, though.

Corrections and questions? Put them in the comments section, please :-) GNU General Public License v2 - GPLv2

  • Allows licensees (your users) to modify the source code and redistribute it
  • Copyleft license - Requires distributors to ship modified source code along with the executable
  • If someone links with your GPL'd library, they must license their executable or library under GPL as well; this applies for both static and dynamic linking (making non-GPL'd Linux kernel modules illegal, according to many)
I typically pick GPLv2 for my bigger projects. It makes me feel "paid" for my work by forcing anyone who takes my work to actually contribute to the betterment of the world; thus, they contribute to the betterment of me.

Note that due to its "infectious" nature, it can only be linked with liberally-licensed code, with other GPLv2 code, and due to a special exception, with LGPLv2 code.

If you license as "GPLv2 or any later version", user can consider the code to be GPLv3 as well. In that case, the user can link with GPLv3 libraries, in which case the code becomes GPLv3 licensed.

Short info on how to apply (for more info, read the last portion of the license itself):

  • when text program starts, or when about dialog is opened, print out something similar to:

Gnomovision version 69, Copyright (C) year  name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.

  • on top of each source code file, add this:


Copyright (C)   
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  U

  • include "COPYING" file with the full license text
  • to avoid licensing under "later version", simply delete relevant text from the source files; do not delete it from text of the license (text of the license itself is copyrighted by FSF)
  • if you want to add exceptions, add them on top of COPYING file; don't add restrictions, but add permissions; make sure you don't modify the license text itself since it's copyrighted by FSF!

GNU General Public License v3 - GPLv3

  • Almost the same as GPLv2
  • Copyleft license
  • Adds protection againts "tivoization": source code being available due to GPL provisions, but hardware actually locking the user out from deploying modified code
  • Adds some patent protection for users
  • Cannot be linked with GPLv2 code unless it's "GPLv2 or later"-licensed

I commonly don't pick GPLv3, since it adds protections I don't really need, and tends to make you look radical. That would not be a bad thing if it didn't also have the potential to turn away potential contributors, and if it didn't have the potential to turn away corporate contributors who don't want to be struck by the patent protection clauses. (Apple stopped contributing to GCC after 4.2, which was the last version licensed under GPLv2.)

I do like licensing stuff under "GPLv2 or later", though.

To see how to apply this, Google it up or see the license text itself.

GNU Lesser General Public License v2.1 - LGPLv2.1

  • Almost the same as GPLv2
  • Copyleft license - Requires distributors to ship source code to modifications of the library (modified DLL)
  • Specifically allows dynamic linking (.so, .dll) with non-LGPL licensed code, but does not allow static linking (otherwise that code must be LGPL or GPL licensed as well)
While FSF advocates you don't use LGPL, I still find it a nice license. Dynamic linking is permitted with closed source code, but the source code to libraries themselves must be published if they are modified and distributed.

It can also be applied to programs if you feel it could be componentized, and want to allow components to be linked with closed source programs.

Short info on how to apply (for more info, read the last portion of the license itself):

  • add this on top of each source file:


Copyright (C)   
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  U

  • include "COPYING" file with full license text

BSD-style license

  • Allows licensees (your users) to modify the source code and redistribute it
  • Liberal license - does not require distributors to ship the code, and does not require any particular license for the remaining code
  • A very basic license, that only serves to point out you don't give any warranty; pretty much common sense, but a good idea to point out

There are a few variants of BSD-style license: 2-clause, 3-clause and 4-clause. Google them up, all variants are quite short, and not very hard to understand. License text itself is not copyrighted, but you will want to keep modifications as simple as possible (just your name).

To apply the license to your code, add text similar to this on top of each of your source code lines and to a file named COPYING -- but please first Google up other variants of the license:

* Copyright (C)  .  All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1.  Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
* 2.  Redistributions in binary form must reproduce the above copyright
*     notice, this list of conditions and the following disclaimer in the
*     documentation and/or other materials provided with the distribution.
* 3.  Neither the name of  nor the names of
*     its contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY  AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL  OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

(This is an example of a 3-clause license.)

This is similar to an MIT-style license.

Your own license with custom provisions

Don't do this. It won't be OSI-approved, and people will have to study them in detail before deciding if they are free, and if they can be combined with other software such as GPL'd.

Public domain

This is not a license, this constitutes giving up any copyright (while above you retain it). It's a bad idea since some jurisdictions don't allow this anyway, so people over there don't actually get any rights. In other jurisdictions worldwide, the way to release rights varies wildly. I have no idea what the variations are, and I don't care to study it.

If you worry enough to want to learn at least a bit about licenses, then you probably don't want to go public domain.


rest of the post

A simple validator for Mac App Store submissions

I figured out I had to have an automated check prior to Mac App Store submission because I managed to miss some stuff a few times.

So here's a beginning of a simple unofficial test suite for Mac App Store submissions: CheckRelease on BitBucket

It currently checks only if you have some external dependencies. I'll try to write a validation for checking if you have added all the frameworks you depend on into the app, and I hope to extend it with some additional tests.

It's under BSD license, and contributions are very welcome!

Also, hopefully, Apple will extend their own validator built into Application Loader and Xcode. Currently we have to wait a few days for rejections from the App Store team that are as trivial as "forgot to put in a framework" and "forgot to get rid of an external dependency" (or, at least, a reference to an external dependency that you actually don't depend on, and that you never actually use).


rest of the post

Dropbox under Debian

To get Dropbox with Nautilus extension to run under Debian:

1. Download .tar.bz2 from Dropbox's site. 2. sudo apt-get install libnautilus-extension-dev python-docutils 3. tar xvvfj nautilus-dropbox-0.6.7.tar.bz2 4. cd nautilus-dropbox-0.6.7 5. ./configure 6. make 7. sudo make install


rest of the post

Mac App Store rejection due to incorrect framework paths

Apple's very strict: you can reference frameworks in /System/Library/Frameworks and in your own app. That's all fine, but there's another unexpected obstacle. App can be self-contained (the library executable paths patched using install_name_tool) but the install path still references /Library/Frameworks. See "otool -L"; the first reference is a self-reference, and it looks like you can't easily update it after the library is built. (Or I didn't look hard enough). And yup, leaving that an absolute path referencing into /Library/Frameworks, that's enough for rejection.

Untested theory is that updating "Dynamic Library Install Name" (LC_ID_DYLIB) is enough to fix this. I've set it to:

@executable_path/../Frameworks/$(EXECUTABLE_PATH)

in place of

$(DYLIB_INSTALL_NAME_BASE:standardizepath)/$(EXECUTABLE_PATH)


rest of the post