Skip to content

Instantly share code, notes, and snippets.

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 rwaldron/c7c013024a0ab3cffa02 to your computer and use it in GitHub Desktop.
Save rwaldron/c7c013024a0ab3cffa02 to your computer and use it in GitHub Desktop.

Compiling v8 and Hello, World Example on Mac OSX Mavericks (10.9.4)

by Keith Rosenberg (netpoetica)

Note: do this in some sort of project/ directory that makes sense. depot_tools are going to need to be in your path, so you may want to install them somewhere you are comfortable with.

1) Get the v8 source

git clone https://github.com/v8/v8.git

2) Install depot tools

Note: you can read about depot_tools here

git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git

additionally, the path to depot tools must be available in your PATH, so add it to your ~/.zshrc or ~/.bashrc file

cd depot_tools
pwd
// should print ~/Desktop/git/depot_tools or the like
vim ~/.zshrc
// or .bashrc

// Add it to the path or add this line at the bottom of your file
export PATH=~/Desktop/git/depot_tools:"$PATH"

3) Add gyp and c/c++/linking configuration to your environment

First make sure clang and clang++ are in in your PATH by running

which clang && which clang++

If for some reason you do not have clang available, make sure you have the most recent Xcode command line tools installed.

vim ~/.zshrc

and add the following:

export CXX="`which clang++`"
export CC="`which clang`"
export CPP="`which clang` -E"
export LINK="`which clang++`"
export CXX_host="`which clang++`"
export CC_host="`which clang`"
export CPP_host="`which clang` -E"
export LINK_host="`which clang++`"
export GYP_DEFINES="clang=1"

4) Install node gyp

In the command line, in the v8 folder, run

make builddeps

it's worth noting that some documentation says to run

make dependencies

from what I can tell, they both install gyp, but make dependencies installs some third party tools you may eventually need. So either one should work, but go ahead and run both if you want (I did) - it won't hurt anything.

5) Finally, run make to create static libs for linking

Now the juicy part. In the v8 folder, you'll have to run make. v8's make lets you specify your architecture and number of cores.

However, your best bet is to let make figure out which architecture to build for your machine. If you know how many cores you have for your CPU, there is a command you can use to specify that.

If you open Activity Monitor, and then navigate to Window -> CPU Usage, the visual will pop up that looks like a bar graph. However many bars there are, that is how many cores you have.

On my machine, I have four, so I would run

make native -j 4

However, if you are unsure, just run

make native

This may take a while (on a kind-of-crap MacBook Pro, takes about 10 minutes).

6) Now, copy the hello world example below into a new file in the v8 folder (direct child of v8 folder) and save it as hello_world.cpp

#include <v8.h>

using namespace v8;

int main(int argc, char* argv[]) {
  // Create a new Isolate and make it the current one.
  Isolate* isolate = Isolate::New();
  Isolate::Scope isolate_scope(isolate);

  // Create a stack-allocated handle scope.
  HandleScope handle_scope(isolate);

  // Create a new context.
  Local<Context> context = Context::New(isolate);

  // Enter the context for compiling and running the hello world script.
  Context::Scope context_scope(context);

  // Create a string containing the JavaScript source code.
  Local<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");

  // Compile the source code.
  Local<Script> script = Script::Compile(source);

  // Run the script to get the result.
  Local<Value> result = script->Run();

  // Convert the result to an UTF8 string and print it.
  String::Utf8Value utf8(result);
  printf("%s\n", *utf8);
  return 0;
}

7) Finally, you're ready to compile and see hello_world.cpp's output!

In terminal,in the v8/ folder, run

clang++ -Iinclude out/native/libv8_base.a out/native/libv8_libbase.a out/native/libv8_snapshot.a out/native/libicudata.a out/native/libicuuc.a out/native/libicui18n.a hello_world.cpp -o hello_world 

viola! You're now ready to start messing with v8!

Resources

Google's Getting Started Guide

Installing depot_tools

About depot_tools - in depth

Using Git to Keep v8 Up to Date

The Difference Between make builddeps and make dependencies

// From https://developers.google.com/v8/get_started
#include <v8.h>
using namespace v8;
int main(int argc, char* argv[]) {
// Create a new Isolate and make it the current one.
Isolate* isolate = Isolate::New();
Isolate::Scope isolate_scope(isolate);
// Create a stack-allocated handle scope.
HandleScope handle_scope(isolate);
// Create a new context.
Local<Context> context = Context::New(isolate);
// Enter the context for compiling and running the hello world script.
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
Local<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
// Compile the source code.
Local<Script> script = Script::Compile(source);
// Run the script to get the result.
Local<Value> result = script->Run();
// Convert the result to an UTF8 string and print it.
String::Utf8Value utf8(result);
printf("%s\n", *utf8);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment