windows下 V8 JS引擎 编译 2016-06-30

最近需要编译V8,找了找网上的教程都是比较老的,所以自己折腾下编译。

由于google连不上的问题。。你需要一个稳定的VPN服务,这个是必须的,如果有代理,可以设置
git config –global http.proxy http://127.0.0.1:1080
git config –global https.proxy https://127.0.0.1:1080

1.使用vs2013编译,因为可能要支持C++ 11 新特性,所以需要提前装vs2013
2.谷歌现在V8源码放到了 https://github.com/v8/v8,但是还是推荐使用depot_tools gyp下载,直接下载的话,third_party目录下少cygwin,icu 两个文件夹,所以还是使用工具下载
3.安装git 客户端,下载depot_tools, 使用git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git,下载好了以后,例如我的目录
D:\v8\depottools\depot_tools,然后设置系统的环境变量path下添加D:\v8\depottools\depot_tools,期间可能会安装一些东西如python27版本.
这里写图片描述
4.下载v8源码,新建D:\v8\src目录,然后使用cmd命令进入当前目录,输入命令gclient config .gclient,会在当前目录下创建一个.gclient文件,然后把这个文件内容修改为
solutions = [
{
“managed”: False,
“name”: “src”,
“url”: “https://chromium.googlesource.com/v8/v8.git“,
“custom_deps”: {},
“deps_file”: “.DEPS.git”,
“safesync_url”: “”,
},
]
如图所示
这里写图片描述

cmd 进入D:\v8\src下,输入命令gclient sync命令更新。
这里写图片描述

一直等待更新吧,更新到一定程度会报错,比如文件找不到之类的如(v8/gypfiles/landmines.py),此时目录下应该有2个文件夹了,把缺少的文件从src目录拷贝到v8对应的目录,继续执行gclient sync
这里写图片描述
这里写图片描述

执行命令的过程中,可能自动会安装一些东西,如gyp,window8 sdk等等。
这里写图片描述
这里写图片描述
安装完gyp以后,同样需要在环境变量path下添加路径,如D:\v8\src\v8\tools\gyp

更新完成以后。把src目录下的gypfiles include test testing tools samples src等目录拷贝v8目录下,
gypfiles也拷贝一份到build目录下覆盖,如图
这里写图片描述

我可能因为网络问题,D:\v8\src\v8\third_party\icu 下还缺少一些文件,我查看了下,用的是icu4c-56.1版本,让后我在http://igor-zivkovic.from.hr/BLFS/general/icu.html网站,下载了http://download.icu-project.org/files/icu4c/56.1/icu4c-56_1-src.tgz对应的版本,拷贝到了对应的D:\v8\src\v8\third_party\icu目录下,在用原来的D:\v8\src\v8\third_party\icu备份的文件在覆盖一次

开始编译

cmd 进入D:\v8\src目录下
执行
set DEPOT_TOOLS_WIN_TOOLCHAIN=0
set GYP_GENERATORS=ninja,msvs-ninja
set GYP_MSVS_VERSION=2013
python build/gyp_v8 -Dtarget_arch=ia32
就会在build目录下生成all.sln工程文件。同事在out目录生成对应的编译文件,打开就可以编译,但是会报BytecodeRegisterOptimizer::kInvalidEquivalenceId 重定义在v8_base_0.lib重定义.
在D:\v8\src\v8\src\interpreter\bytecode-register-optimizer.cc中,
注释代码const uint32_t BytecodeRegisterOptimizer::kInvalidEquivalenceId; 就可以了,所以工程还是会报错,但是会在D:\v8\src\v8\out\Debug\obj\src 目录下生成
icui18n.lib v8_libplatform.lib v8_libbase.lib v8_base_0.lib v8_base_1.lib v8_base_2.lib v8_base_3.lib
v8_nosnapshot.lib icuuc.lib v8_libsampler.lib v8_external_snapshot.lib
等文件,就可以了。可以新建一个功能跑跑demo程序,头文件在D:\v8\src\src\include目录下

也可以使用ninja编译

set GYP_GENERATORS=ninja
python build/gyp_v8 -Dtarget_arch=ia32
ninja -C out/Debug all
这里写图片描述

测试demo

// 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 "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

#pragma comment(lib,"icui18n.lib")
#pragma comment(lib,"v8_libplatform.lib")
#pragma comment(lib,"v8_libbase.lib")
#pragma comment(lib,"v8_base_0.lib")
#pragma comment(lib,"v8_base_1.lib")
#pragma comment(lib,"v8_base_2.lib")
#pragma comment(lib,"v8_base_3.lib")
#pragma comment(lib,"v8_nosnapshot.lib")
#pragma comment(lib,"icuuc.lib")
#pragma comment(lib,"v8_libsampler.lib")
#pragma comment(lib,"v8_external_snapshot.lib")


using namespace v8;

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

    // Create a new Isolate and make it the current one.
    Isolate::CreateParams create_params;
    create_params.array_buffer_allocator =
        v8::ArrayBuffer::Allocator::NewDefaultAllocator();
    Isolate* isolate = Isolate::New(create_params);
    {
        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!'",
            NewStringType::kNormal).ToLocalChecked();

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

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

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

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

这里写图片描述

猜你喜欢

转载自blog.csdn.net/HeroRazor/article/details/51792793