Mac OS下V8引擎编译方法

V8 的Github地址 :https://github.com/v8/v8

因为需要访问到google的服务器,所以下面做的一切都需要翻到墙外面。方法自行解决吧。

1.安装depot_tools

v8的版本控制是有一个叫depot_tools的工具维护的,这个东西相当于的将git包了一层。
[具体安装方法](http://www.chromium.org/developers/how-tos/install-depot-tools)

下载depot_tools库

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

将下载后的路径添加到环境变量中

Mac 下编辑 ~/.bash_profile文件,在文件最开头添加下列代码
其中/Users/XXX/Documents/Git/depot_tools是clone库后的路径

export DEPOT_TOOLS=/Users/XXX/Documents/Git/depot_tools
export PATH=$DEPOT_TOOLS:$PATH

保存环境变量

source ~/.bash_profile

2.编译V8

执行脚本,下载所有需要的依赖

gclient sync

使用gn生成编译文件

tools/dev/v8gen.py x64.release

使用ninjia编译

ninja -C out.gn/x64.release

(选项)使用py脚本测试编译后的文件是否有问题

tools/run-tests.py --gn

3.在XCode上编译运行

执行完上一步后,会在 out.gn/x64.release 文件夹生成V8的静态库等结果。
这一步是将需要的文件链接到一起

3.1 使用xcode生成一个命令行工程,

File->New->Project-> 选择Mac OS 下的Command Line Tools工程

3.2 将v8/include 文件夹拷贝到xcode xx.xcodeproj同级目录下。

Mac OS下V8引擎编译方法_第1张图片

3.3 设置文件索引路径

targer -> Build Settings -> Search Paths 设置 Header Search Paths 添加一项 $(SRCROOT)

targer -> Build Settings -> Search Paths 设置 User Header Search Paths 添加一项 $(SRCROOT)/include

3.2 拷贝静态库

在xcode工程,xx.xcodeproj同级目录下新建一个lib文件夹,再在其次级新建icu和inspector文件夹

x64.release/obj 拷贝 libv8_base.a、libv8_external_snapshot.a、libv8_libbase.a、libv8_libplatform.a、libv8_libsampler.a拷贝至刚新建的lib目录

x64.release/obj/third_party/icu拷贝libicui18n.a、libicuuc.a至 lib/icu文件夹

x64.release/obj/src/inspector拷贝libinspector.a至lib/inspector

Mac OS下V8引擎编译方法_第2张图片

3.3 拷贝

跳转到xcode输出执行文件的目录下

x64.release目录下的
natives_blob.bin、snapshot_blob.bin、icudtl.dat
拷贝到xcode输出执行文件的目录下
这里写图片描述

4.编译执行

用官方提供的helloworld来测试。

// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include 
#include 
#include 

#include "include/libplatform/libplatform.h"
#include "include/v8.h"

int main(int argc, char* argv[]) {
  // Initialize V8.
  v8::V8::InitializeICUDefaultLocation(argv[0]);
  v8::V8::InitializeExternalStartupData(argv[0]);
  v8::Platform* platform = v8::platform::CreateDefaultPlatform();
  v8::V8::InitializePlatform(platform);
  v8::V8::Initialize();

  // Create a new Isolate and make it the current one.
  v8::Isolate::CreateParams create_params;
  create_params.array_buffer_allocator =
      v8::ArrayBuffer::Allocator::NewDefaultAllocator();
  v8::Isolate* isolate = v8::Isolate::New(create_params);
  {
    v8::Isolate::Scope isolate_scope(isolate);

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

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

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

    // Create a string containing the JavaScript source code.
    v8::LocalString> source =
        v8::String::NewFromUtf8(isolate, "'Hello' + ', World!'",
                                v8::NewStringType::kNormal)
            .ToLocalChecked();

    // Compile the source code.
    v8::LocalScript> script =
        v8::Script::Compile(context, source).ToLocalChecked();

    // Run the script to get the result.
    v8::LocalValue> result = script->Run(context).ToLocalChecked();

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

  // Dispose the isolate and tear down V8.
  isolate->Dispose();
  v8::V8::Dispose();
  v8::V8::ShutdownPlatform();
  delete platform;
  delete create_params.array_buffer_allocator;
  return 0;
}

编译执行。ok

你可能感兴趣的:(Mac,OS,X,mac)