Where developers come to connect, share, build and be inspired.

0

Scripting Objective-C code with Clang

1914 views


Make the script executable. Leave off the extension.

It works by using AWK to get the objc portion and then creating a new tmp file that is compiled and executed. When the program is executed we immediately delete the two temporary files.

For me this works faster than Xcode or a Makefile. Clang is that fast.

  • Cons: compile error line numbers are off by the SHELL portion.
  • Pros: it's cool.
#!/bin/bash

D=/Applications/Xcode.app/Contents/Developer/Library/Frameworks
OBJC=`awk '/^__OBJC__/ {print NR + 1; exit 0; }' $0`
tail -n+$OBJC $0 > /tmp/YOLOtests.m

clang -ObjC -F$D -I. -fmodules -fobjc-arc \
    -framework SenTestingKit \
    /tmp/YOLOtests.m *.m -o /tmp/YOLOtests

install_name_tool -change \
    @rpath/SenTestingKit.framework/Versions/A/SenTestingKit \
    $D/SenTestingKit.framework/SenTestingKit \
    /tmp/YOLOtests

exec /tmp/YOLOtests


__OBJC__

#import <SenTestingKit/SenTestingKit.h>
#import "YOLO.h"


int main() {
    unlink("/tmp/YOLOtests.m");  //before or… doesn't happen! FFS
    unlink("/tmp/YOLOtests");

    @autoreleasepool {
        SenSelfTestMain();
    }
}

Real project example: https://github.com/mxcl/YOLOKit/blob/master/tests

Add a comment