Skip to content

Instantly share code, notes, and snippets.

@kjunichi
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjunichi/9441613 to your computer and use it in GitHub Desktop.
Save kjunichi/9441613 to your computer and use it in GitHub Desktop.

NodObjCをnode-webkitで動かす

node-webkit-v0.8.5編

v0.9系はnode-ffiがビルドできないので難しい。

nw-gyp

npm install -g nw-gyp

NodObjCをnpmでフツーに入れる

mkdir node_modules
npm install NodObjC

Postgres.appを入れてる場合

PATHに

/Applications/Postgres.app/Contents/MacOS/bin/

を加えている場合、Postgres.appのxml2-config が使われると、ビルドに失敗するので、 /usr/bin/xml2-configが使われるようにする。

which xml2-config

nw-gypでconfigureとbuild

libxmljs

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

node-ffi

libffi-config.shを編集して32ビット用のバイナリを生成させる

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 ../../../../

index.html

<!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"
}

Link

関連

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment