zoukankan      html  css  js  c++  java
  • windos vs2019 || clion cmake clang msvc 编译v8 引擎

     

    准备

    编译有问题就到V8群寻帮助 Q群 1414577

    必须要有代理

    配置临时环境变量

    管理员身份运行控制台(cmd)

    set HTTP_PROXY=http://127.0.0.1:10809
    set HTTPS_PROXY=http://127.0.0.1:10809
    set DEPOT_TOOLS_WIN_TOOLCHAIN=0
    

    代理端口查看设置里面的

    因为是临时环境变量,每次打开控制台都需要重新设置。

    下载 depot tools

    https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools_tutorial.html#_setting_up

     点击 budle 下载后解压

     将该目录加入到path中

     执行 gclient

     出现这个提示表示下载完成。 

    安装windows sdk10

    直接在vs中安装就行了

    下载源码

    fetch v8

    如果中途失败,继续执行 gclient sync

     出现这个提示表示下载完成。

    关于路径问题

    如果VS的安装目录和windows sdk的安装目录都是默认在C盘,可以跳过。

    编辑文件 build/vs_toolchain.py

    177行左右,直接将path改为vs的路径。

    204行左右,直接返回vs路径

    编译 

    使用 clang libc++ 编译动态库

    根据需要编译的平台选择相应的控制台,64位的选择x64,32位的选择x86

    因为是新开控制台,需要设置 

    set DEPOT_TOOLS_WIN_TOOLCHAIN=0

    设置 gn 参数

    gn args out.cl/x64.debug
    is_debug=true 
    target_cpu="x64"
    v8_enable_i18n_support=false 
    v8_use_external_startup_data=false 
    is_component_build=true

    可以生成 vs 解决方案

    gn gen --ide=vs out.cl\x64.debug

    可以直接打开vs编译,也可以继续命令行编译

     按照网上说法直接编译 gn_all

    或者命令行编译

    ninja -C out.cl/x64.debug v8

     

     打开解决方案

     查看v8_hello_world的代码

    // 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 <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include "include/libplatform/libplatform.h"
    #include "include/v8-context.h"
    #include "include/v8-initialization.h"
    #include "include/v8-isolate.h"
    #include "include/v8-local-handle.h"
    #include "include/v8-primitive.h"
    #include "include/v8-script.h"
    
    int main(int argc, char* argv[]) {
      // Initialize V8.
      v8::V8::InitializeICUDefaultLocation(argv[0]);
      v8::V8::InitializeExternalStartupData(argv[0]);
      std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
      v8::V8::InitializePlatform(platform.get());
      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<v8::Context> 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::Local<v8::String> source =
              v8::String::NewFromUtf8Literal(isolate, "'Hello' + ', World!'");
    
          // Compile the source code.
          v8::Local<v8::Script> script =
              v8::Script::Compile(context, source).ToLocalChecked();
    
          // Run the script to get the result.
          v8::Local<v8::Value> 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);
        }
    
        {
          // Use the JavaScript API to generate a WebAssembly module.
          //
          // |bytes| contains the binary format for the following module:
          //
          //     (func (export "add") (param i32 i32) (result i32)
          //       get_local 0
          //       get_local 1
          //       i32.add)
          //
          const char csource[] = R"(
            let bytes = new Uint8Array([
              0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01,
              0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07,
              0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01,
              0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b
            ]);
            let module = new WebAssembly.Module(bytes);
            let instance = new WebAssembly.Instance(module);
            instance.exports.add(3, 4);
          )";
    
          // Create a string containing the JavaScript source code.
          v8::Local<v8::String> source =
              v8::String::NewFromUtf8Literal(isolate, csource);
    
          // Compile the source code.
          v8::Local<v8::Script> script =
              v8::Script::Compile(context, source).ToLocalChecked();
    
          // Run the script to get the result.
          v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
    
          // Convert the result to a uint32 and print it.
          uint32_t number = result->Uint32Value(context).ToChecked();
          printf("3 + 4 = %u\n", number);
        }
      }
    
      // Dispose the isolate and tear down V8.
      isolate->Dispose();
      v8::V8::Dispose();
      v8::V8::ShutdownPlatform();
      delete create_params.array_buffer_allocator;
      return 0;
    }
    

     运行结果:

    如果用vs创建项目,会报链接错误:

    通过查看v8_libplatform.dll.lib的导出符号

    发现: 

    libc++编译的标准库会出现std::__1这种命名空间,而libstdc++则没有。

    参考这里的说法:

    VS2017链接V8引擎库遇到的一些问题_myctime的博客-CSDN博客

    是因为libc++的库在实现STL的时候使用了inline namespace __1,而libstdc++则没有。

    按照上面的解决方法,我没有成功。

    不使用 clang 编译静态库

    设置 gn 参数

    gn args out/x64.release
    v8_monolithic=true 
    v8_use_external_startup_data=false 
    use_custom_libcxx=false 
    is_component_build=false 
    treat_warnings_as_errors=false 
    v8_symbol_level=0 
    is_clang=false 
    is_debug=false

    顺便生成 vs 解决方案

    gn gen --ide=vs out\x64.release

    命令行编译

    ninja -C out/x64.release v8_monolith

    测试执行js文件:

    创建一个控制台应用,如下设置

     

     

     

     

    #include "libplatform/libplatform.h"
    #include "v8-context.h"
    #include "v8-initialization.h"
    #include "v8-isolate.h"
    #include "v8-local-handle.h"
    #include "v8-primitive.h"
    #include "v8-script.h"
    
    int main(int argc, char* argv[]) {
        // Initialize V8.
    
        v8::V8::InitializeICUDefaultLocation(argv[0]);
        v8::V8::InitializeExternalStartupData(argv[0]);
        std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
        v8::V8::InitializePlatform(platform.get());
        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<v8::Context> context = v8::Context::New(isolate);
    
            // Enter the context for compiling and running the hello world script.
            v8::Context::Scope context_scope(context);
    
            char* s = NULL;
            _get_pgmptr(&s);
    
            std::string path(s);
            size_t i = path.find_last_of("\\");
            path = path.substr(0, i);
            //i = path.find_last_of("\\");
            //path = path.substr(0, i);
            std::string js = path + "\\t.js";
    
            FILE* file;
            fopen_s(&file, js.c_str(), "rb");
            if (file != NULL) {
                fseek(file, 0, SEEK_END);
                size_t len = ftell(file);
                rewind(file);
                std::unique_ptr<char> buf(new char[len + 1]);
                buf.get()[len] = '\0';
                for (size_t i = 0; i < len;) {
                    i += fread(&buf.get()[i], 1, len - i, file);
                }
                fclose(file);
    
                // Create a string containing the JavaScript source code.
                v8::Local<v8::String> source =
                    v8::String::NewFromUtf8(isolate, buf.get(), v8::NewStringType::kNormal, static_cast<int>(len)).ToLocalChecked();
    
                // Compile the source code.
                v8::Local<v8::Script> script =
                    v8::Script::Compile(context, source).ToLocalChecked();
    
                // Run the script to get the result.
                v8::Local<v8::Value> 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 create_params.array_buffer_allocator;
        return 0;
    }
    

     t.js

    function hello() {
    	return "hello!";
    }
    hello();

    运行结果:

    博主教你手撸JVM 开源项目
    https://github.com/anons-org/nada
    https://gitee.com/grateful/nada
    博主长期对外收徒,欢迎咨询。
    《编程语言设计和实现》《MUD游戏开发》《软件破解和加密》《游戏辅助外挂》《JAVA开发》 以上课程非诚勿扰!



    =================================
    QQ:184377367
    GOLang Q1群:6848027
    GOLang Q2群:450509103
    C/C++/QT群 1414577
    单片机嵌入式/电子电路入门群群 306312845
    MUD/LIB/巫师交流群 391486684
    java/springboot/hadoop/JVM 群 4915800
    WEB前端开发交流群/nodejs/ 214737701
    操作系统研发群:15375777
    汇编/辅助/破解新手群:755783453
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    ReentrantLock和synchronized区别和联系?
    从上向下打印二叉树
    字符串的全排列
    二叉树中和为某一值得路径 java实现
    三大框架面试题
    SQL语句总结
    [terry笔记]Oracle10g/11g安装-redhat5.5
    [terry笔记]Oracle会话追踪(二):TKPROF
    [terry笔记]Oracle会话追踪(一):SQL_TRACE&EVENT 10046
    [terry笔记]IMPDP报错ORA-39083 Object type TYPE failed to create ORA-02304
  • 原文地址:https://www.cnblogs.com/cfas/p/15657434.html
Copyright © 2011-2022 走看看