LoginSignup
11
11

More than 5 years have passed since last update.

NodObjCをnode-webkitで動かす

Last updated at Posted at 2014-03-09

node-webkit-v0.8.5編

v0.9系はnode-ffiがビルドがフツーのnode.jsのv0.11系でNGなので、ハードル高いそう。

nw-gyp

npm install -g nw-gyp

NodObjCをnpmでフツーに入れる

NodeObjCはカレントにnode_modulesディレクトリが無いと、
変な場所にインストールされることがあったので、予めnode_modulesディレクトリを
作っておく。

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をnode-webkit向けにビルド

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をnode-webkit向けにビルド

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

内部でlibffi.aを静的にリンクするのだが、これがgyp管理外の為、nw-gyp configの際の--arch=ia32の指定が効かないので、そのままだと64ビット用のバイナリが作られてしまう。
その為、個別に対応する必要がある。

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

i386用の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 ../../../../
~/Downloads/node-webkit-v0.8.5-osx-ia32/node-webkit.app/Contents/MacOS/node-webkit .

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"
}
11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11