Skip to content

Commit

Permalink
Upgrade to v8 10.0.139.9 with tools support (#29)
Browse files Browse the repository at this point in the history
- upgrade to v8 10.0.139.9
- add mkcodecache tools
- archive mksnapshot tools
  • Loading branch information
Kudo authored May 22, 2022
1 parent 992e860 commit 3db0198
Show file tree
Hide file tree
Showing 33 changed files with 854 additions and 238 deletions.
21 changes: 11 additions & 10 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
# sudo sh -c "chmod 777 $GITHUB_WORKSPACE/../../_temp"
sudo sh -c "chmod 777 /home"
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Setup Ubuntu environment
run: |
Expand All @@ -41,7 +41,7 @@ jobs:
- name: Export settings from npm package
run: npm run export_npm_env

# - uses: actions/cache@v2
# - uses: actions/cache@v3
# with:
# path: v8
# key: android-v8-${{ env.V8_VERSION }}-${{ env.CACHE_KEY_SUFFIX }}
Expand Down Expand Up @@ -73,11 +73,12 @@ jobs:
- name: Archive
run: |
scripts/archive.sh android
tar -cvf dist.tar dist
- uses: actions/upload-artifact@master
- uses: actions/upload-artifact@v3
with:
name: dist-${{ matrix.variant }}
path: dist
path: dist.tar


###############################################################
Expand Down Expand Up @@ -108,7 +109,7 @@ jobs:
# sudo sh -c "chmod 777 $GITHUB_WORKSPACE/../../_temp"
# sudo sh -c "chmod 777 /home"
#
# - uses: actions/checkout@v2
# - uses: actions/checkout@v3
#
# - name: Setup Ubuntu environment
# run: |
Expand All @@ -120,7 +121,7 @@ jobs:
# - name: Export settings from npm package
# run: npm run export_npm_env
#
# # - uses: actions/cache@v1
# # - uses: actions/cache@v3
# # id: v8-source-cache
# # with:
# # path: v8
Expand All @@ -140,7 +141,7 @@ jobs:
# run: |
# scripts/build.sh android arm
#
# - uses: actions/upload-artifact@master
# - uses: actions/upload-artifact@v3
# with:
# name: build-intl-arm
# path: build
Expand Down Expand Up @@ -174,7 +175,7 @@ jobs:
# sudo sh -c "chmod 777 $GITHUB_WORKSPACE/../../_temp"
# sudo sh -c "chmod 777 /home"
#
# - uses: actions/checkout@v2
# - uses: actions/checkout@v3
#
# - name: Setup Ubuntu environment
# run: |
Expand All @@ -186,7 +187,7 @@ jobs:
# - name: Export settings from npm package
# run: npm run export_npm_env
#
# # - uses: actions/cache@v1
# # - uses: actions/cache@v3
# # id: v8-source-cache
# # with:
# # path: v8
Expand All @@ -206,7 +207,7 @@ jobs:
# run: |
# scripts/build.sh android ${{ env.arch }}
#
# - uses: actions/upload-artifact@master
# - uses: actions/upload-artifact@v3
# with:
# name: build-${{ matrix.variant }}-${{ matrix.arch }}
# path: build
7 changes: 4 additions & 3 deletions .github/workflows/ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
NO_JIT: true

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3

- name: Export settings from npm package
run: |
Expand All @@ -29,8 +29,9 @@ jobs:
run: |
scripts/build.sh ios
scripts/archive.sh ios
tar -cvf dist.tar dist
- uses: actions/upload-artifact@master
- uses: actions/upload-artifact@v3
with:
name: dist
path: dist
path: dist.tar
59 changes: 59 additions & 0 deletions .github/workflows/tools_macos_android.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: v8-android tools builder for macos host

on: [push]

env:
CACHE_KEY_SUFFIX: v2

jobs:
macos:
runs-on: macOS-latest

strategy:
matrix:
variant: [intl, nointl, jit-intl, jit-nointl]

env:
NO_INTL: ${{ contains(matrix.variant, 'nointl') }}
NO_JIT: ${{ !contains(matrix.variant, 'jit') }}
TOOLS_ONLY: true

steps:
- uses: actions/checkout@v3

- name: Export settings from npm package
run: npm run export_npm_env

- name: Setup V8 build environment
run: |
brew install coreutils
scripts/setup.sh -r ${{ env.V8_VERSION }} macos_android
- name: Patch V8
run: scripts/patch.sh macos_android

# - name: Build arm
# run: |
# scripts/build.sh macos_android arm
#
# - name: Build x86
# run: |
# scripts/build.sh macos_android x86

- name: Build arm64
run: |
scripts/build.sh macos_android arm64
- name: Build x64
run: |
scripts/build.sh macos_android x64
- name: Archive
run: |
scripts/archive.sh macos_android
tar -cvf dist.tar dist
- uses: actions/upload-artifact@v3
with:
name: macos-tools-${{ matrix.variant }}
path: dist.tar
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,9 @@ gradle-app.setting
.cipd/*
.gclient
.gclient_entries

# publish archives
mkcodecache
mksnapshot
*.bin
*.zip
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.formatting.provider": "black"
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ To integrate with React Native, please check [react-native-v8](https://github.co
1. Single libv8android.so (or libv8.so on iOS) file.
2. Support i18n and JavaScript [Intl](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl).
3. V8 Lite mode (JIT-less mode) https://v8.dev/blog/v8-lite
4. Build by Android official NDK r19c which prevent potential ABI incompatible issue to integrate with React Native.
4. Build by Android official NDK r21e which prevent potential ABI incompatible issue to integrate with React Native.

## Build Guides

Expand Down
130 changes: 130 additions & 0 deletions mkcodecache/mkcodecache.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright 2006-2008 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/libplatform/libplatform.h"
#include "include/v8-initialization.h"
#include "include/v8-message.h"
#include "include/v8-script.h"
#include "src/base/platform/platform.h"

#include <chrono>

namespace v8 {

namespace {

// Reads a file into a v8 string.
Local<String> ReadFile(Isolate* isolate, const char* name,
bool should_throw) {
std::unique_ptr<base::OS::MemoryMappedFile> file(
base::OS::MemoryMappedFile::open(
name, base::OS::MemoryMappedFile::FileMode::kReadOnly));
if (!file) {
if (should_throw) {
std::ostringstream oss;
oss << "Error loading file: \"" << name << '"';
isolate->ThrowError(
v8::String::NewFromUtf8(isolate, oss.str().c_str()).ToLocalChecked());
}
return Local<String>();
}

int size = static_cast<int>(file->size());
char* chars = static_cast<char*>(file->memory());
Local<String> result = String::NewFromUtf8(isolate, chars, NewStringType::kNormal, size)
.ToLocalChecked();
return result;
}

} // namespace

} // namespace v8

int main(int argc, char** argv) {
if (argc < 2) {
::printf("Usage: %s script_file\n", argv[0]);
exit(1);
}

v8::base::EnsureConsoleOutput();
v8::V8::InitializeICUDefaultLocation(argv[0]);
std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::SetFlagsFromString("--nolazy");
v8::V8::Initialize();
v8::V8::InitializeExternalStartupData(argv[0]);

v8::Isolate::CreateParams createParams;
auto arrayBufferAllocator =
std::unique_ptr<v8::ArrayBuffer::Allocator>(v8::ArrayBuffer::Allocator::NewDefaultAllocator());
createParams.array_buffer_allocator = arrayBufferAllocator.get();
v8::Isolate* isolate = v8::Isolate::New(createParams);
v8::HandleScope handle_scope(isolate);


v8::Local<v8::String> source = v8::ReadFile(isolate, argv[1], false);

if (argc == 2) {
v8::ScriptOrigin origin = v8::ScriptOrigin(isolate, v8::String::NewFromUtf8Literal(isolate, "(mkcodecache)"));

v8::ScriptCompiler::Source scriptSource(source, origin);
// v8::Local<v8::UnboundScript> unboundScript = v8::ScriptCompiler::CompileUnboundScript(isolate, &scriptSource, v8::ScriptCompiler::kEagerCompile).ToLocalChecked();
v8::Local<v8::UnboundScript> unboundScript = v8::ScriptCompiler::CompileUnboundScript(isolate, &scriptSource, v8::ScriptCompiler::kNoCompileOptions).ToLocalChecked();
v8::ScriptCompiler::CachedData *cachedData = v8::ScriptCompiler::CreateCodeCache(unboundScript);
::printf("cache data size %d\n", cachedData->length);

FILE* file = v8::base::Fopen("v8codecache.bin", "wb");
if (file) {
fwrite(cachedData->data, 1, cachedData->length, file);
v8::base::Fclose(file);
}
} else {
v8::ScriptOrigin origin = v8::ScriptOrigin(isolate, v8::String::NewFromUtf8Literal(isolate, "(mkcodecache)"));
std::unique_ptr<v8::ScriptCompiler::CachedData> cachedData;
FILE* file = v8::base::Fopen("v8codecache.bin", "rb");
if (file) {
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
uint8_t *buffer = new uint8_t[size];
rewind(file);

if (fread(buffer, 1, size, file) != size) {
::printf("ooxx fread error\n");
}
v8::base::Fclose(file);

cachedData = std::make_unique<v8::ScriptCompiler::CachedData>(
buffer,
static_cast<int>(size),
v8::ScriptCompiler::CachedData::BufferPolicy::BufferOwned);
}

auto begin = std::chrono::high_resolution_clock::now();

v8::Local<v8::Context> context = v8::Context::New(isolate);
v8::Context::Scope context_scope(context);

v8::ScriptCompiler::CachedData *cachedDataPtr = cachedData.release();
v8::ScriptCompiler::Source scriptSource(source, origin, cachedDataPtr);
// v8::ScriptCompiler::Source scriptSource(source, origin, nullptr);
v8::Local<v8::Script> compiledScript;
if (!v8::ScriptCompiler::Compile(context, &scriptSource, v8::ScriptCompiler::kConsumeCodeCache).ToLocal(&compiledScript)) {
// if (!v8::ScriptCompiler::Compile(context, &scriptSource, v8::ScriptCompiler::kNoCompileOptions).ToLocal(&compiledScript)) {
::printf("ooxx error\n");
}

if (cachedDataPtr->rejected) {
::printf("ooxx rejected\n");
}

auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - begin);
::printf("ooxx compile time: %lld\n", static_cast<long long int>(duration.count()));
}

v8::V8::Dispose();
v8::V8::DisposePlatform();

return 0;
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"private": true,
"version": "9.93.0",
"version": "10.100.0",
"workspaces": [
"packages/*"
],
"scripts": {
"clean": "rm -rf dist; rm -rf build; rm -rf v8/out.v8.*",
"export_npm_env": "./scripts/export_npm_env.sh",
"setup": "bash -c '. ./scripts/export_npm_env.sh && ./scripts/setup.sh android'",
"start": "bash -c '. ./scripts/export_npm_env.sh && ./scripts/start.sh android'",
"setup_mksnapshot": "bash -c '. ./scripts/export_npm_env.sh && MKSNAPSHOT_ONLY=1 ./scripts/setup.sh android'",
"start_mksnapshot": "bash -c '. ./scripts/export_npm_env.sh && MKSNAPSHOT_ONLY=1 ./scripts/start.sh android'",
"setup_ios": "bash -c '. ./scripts/export_npm_env.sh && ./scripts/setup.sh ios'",
"start_ios": "bash -c '. ./scripts/export_npm_env.sh && ./scripts/start.sh ios'"
"setup_macos_android": "bash -c '. ./scripts/export_npm_env.sh && ./scripts/setup.sh macos_android'",
"start": "bash -c '. ./scripts/export_npm_env.sh && ./scripts/start.sh android'",
"start_ios": "bash -c '. ./scripts/export_npm_env.sh && ./scripts/start.sh ios'",
"start_tools_macos_android": "bash -c '. ./scripts/export_npm_env.sh && TOOLS_ONLY=true ./scripts/start.sh macos_android'"
},
"config": {
"V8": "9.3.345.16"
"V8": "10.0.139.9"
}
}
2 changes: 1 addition & 1 deletion packages/v8-android-jit-nointl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "v8-android-jit-nointl",
"version": "9.93.0",
"version": "10.100.0",
"description": "Pre-build version of V8 to be used by React Native apps",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/v8-android-jit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "v8-android-jit",
"version": "9.93.0",
"version": "10.100.0",
"description": "Pre-build version of V8 to be used by React Native apps",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/v8-android-nointl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "v8-android-nointl",
"version": "9.93.0",
"version": "10.100.0",
"description": "Pre-build version of V8 to be used by React Native apps (Non Intl supported version)",
"repository": {
"type": "git",
Expand Down
24 changes: 24 additions & 0 deletions packages/v8-android-tools-linux/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "v8-android-tools-linux",
"version": "10.100.0",
"description": "Tools for v8-android",
"repository": {
"type": "git",
"url": "git+https://github.com/Kudo/v8-android-buildscripts.git",
"directory": "packages/v8-android-tools-linux"
},
"keywords": [
"react-native",
"android",
"v8",
"tools"
],
"license": "BSD-2-Clause",
"bugs": {
"url": "https://github.com/Kudo/v8-android-buildscripts/issues"
},
"homepage": "https://github.com/Kudo/v8-android-buildscripts#readme",
"files": [
"v8-android*"
]
}
Loading

0 comments on commit 3db0198

Please sign in to comment.