npm install -g nw-gyp
mkdir node_modules
npm install NodObjC
PATHに
/Applications/Postgres.app/Contents/MacOS/bin/
を加えている場合、Postgres.appのxml2-config が使われると、ビルドに失敗するので、 /usr/bin/xml2-configが使われるようにする。
which xml2-config
xml2-configを/usb/binのものを使えば問題なくnode-webkit向けにビルド出来た。
cd node_modules/NodObjC/node_modules/libxmljs
nw-gyp clean
nw-gyp configure --target=0.8.5 --arch=ia32
nw-gyp build
libffiを静的にリンクするのだが、これがgyp管理外の為、nw-gyp configの際の--arch=ia32の指定が効かないので、 個別に対応する必要がある。
cd deps/libffi
make clean distclean >node_ffi_configure.out 2>&1
export CFLAGS="-arch i386"
sh configure --with-pic --enable-static --disable-shared --disable-builddir >>node_ffi_configure.out 2>&1
cd ../..
cd ../node-ffi
nw-gyp clean
nw-gyp configure --target=0.8.5 --arch=ia32
nw-gyp build
SOLINK_MODULE(target) Release/ffi_bindings.node
ld: warning: could not create compact unwind for .LFB3: non-standard register 5 being saved in prolog
SOLINK_MODULE(target) Release/ffi_bindings.node: Finished
32ビット用ののlibffi.aができていない場合、以下のように表示される。
SOLINK_MODULE(target) Release/ffi_bindings.node
ld: warning: ignoring file /Users/nanashi/work/tmp/node_modules/NodObjC/node_modules/node-ffi/deps/libffi/.libs/libffi.a, file was built for archive which is not the architecture being linked (i386): /Users/nanashi/work/tmp/node_modules/NodObjC/node_modules/node-ffi/deps/libffi/.libs/libffi.a
SOLINK_MODULE(target) Release/ffi_bindings.node: Finished
NSAutoreleasePool等は指定するとデバッガが起動できない。 node-webkit側でこの辺りを処理しているのだろうから余計なことはしないw。
cd ../../../../
<!DOCTYPE html>
<html>
<title>Hello,Cocoa</title>
<h1>Hello NodObjC</h1>
We are using node.js <script>document.write(process.version)</script>.
<div id="hello"></div>
<script>
var $ = require('NodObjC');
$.framework('Foundation');
$.framework('Cocoa');
// NSString from a JS String when an Objective-C class method requires one.
var string = $.NSString('stringWithUTF8String', 'Hello Objective-C World!');
// Print out the contents (calling [string description])
console.log(string.toString());
console.log(string);
// → Prints "Hello Objective-C World!"
document.getElementById("hello").innerHTML=string;
</script>
</html>
package.json
{
"name": "nw-demo",
"main": "index.html"
}