Skip to content

Commit cec0ed0

Browse files
mi-acCommit bot
authored andcommitted
[icu] Support loading data file from default location
This allows using icu data, bundled in the icudtl.dat file, to be loaded automatically from a default location side-by-side with the executable. The v8 stand-alone default is still to use statically linked ICU data, but this will be switched in a separate follow-up CL. BUG=chromium:616033 LOG=y Review-Url: https://codereview.chromium.org/2042253002 Cr-Commit-Position: refs/heads/master@{#36823}
1 parent ea139c5 commit cec0ed0

18 files changed

Lines changed: 140 additions & 48 deletions

BUILD.gn

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -641,12 +641,18 @@ action("run_mksnapshot") {
641641

642642
source_set("v8_maybe_snapshot") {
643643
if (v8_use_snapshot && v8_use_external_startup_data) {
644-
public_deps = [ ":v8_external_snapshot" ]
644+
public_deps = [
645+
":v8_external_snapshot",
646+
]
645647
} else if (v8_use_snapshot) {
646-
public_deps = [ ":v8_snapshot" ]
648+
public_deps = [
649+
":v8_snapshot",
650+
]
647651
} else {
648652
assert(!v8_use_external_startup_data)
649-
public_deps = [ ":v8_nosnapshot" ]
653+
public_deps = [
654+
":v8_nosnapshot",
655+
]
650656
}
651657
}
652658

@@ -1886,6 +1892,8 @@ v8_source_set("v8_libbase") {
18861892
"src/base/cpu.h",
18871893
"src/base/division-by-constant.cc",
18881894
"src/base/division-by-constant.h",
1895+
"src/base/file-utils.cc",
1896+
"src/base/file-utils.h",
18891897
"src/base/flags.h",
18901898
"src/base/format-macros.h",
18911899
"src/base/functional.cc",
@@ -2065,11 +2073,11 @@ if (current_toolchain == v8_snapshot_toolchain) {
20652073
# Public targets
20662074
#
20672075

2068-
want_v8_shell = (
2076+
want_v8_shell =
20692077
(current_toolchain == host_toolchain && v8_toolset_for_shell == "host") ||
20702078
(current_toolchain == v8_snapshot_toolchain &&
20712079
v8_toolset_for_shell == "host") ||
2072-
(current_toolchain != host_toolchain && v8_toolset_for_shell == "target"))
2080+
(current_toolchain != host_toolchain && v8_toolset_for_shell == "target")
20732081

20742082
group("gn_all") {
20752083
testonly = true
@@ -2079,22 +2087,18 @@ group("gn_all") {
20792087
":v8_simple_json_fuzzer",
20802088
":v8_simple_parser_fuzzer",
20812089
":v8_simple_regexp_fuzzer",
2082-
":v8_simple_wasm_fuzzer",
20832090
":v8_simple_wasm_asmjs_fuzzer",
2091+
":v8_simple_wasm_fuzzer",
20842092
"test:gn_all",
20852093
"tools:gn_all",
20862094
]
20872095

20882096
if (want_v8_shell) {
2089-
deps += [
2090-
":v8_shell",
2091-
]
2097+
deps += [ ":v8_shell" ]
20922098
}
20932099

20942100
if (v8_test_isolation_mode != "noop") {
2095-
deps += [
2096-
":d8_run",
2097-
]
2101+
deps += [ ":d8_run" ]
20982102
}
20992103
}
21002104

@@ -2234,7 +2238,8 @@ v8_source_set("json_fuzzer") {
22342238
configs = [ ":internal_config" ]
22352239
}
22362240

2237-
v8_fuzzer("json_fuzzer") {}
2241+
v8_fuzzer("json_fuzzer") {
2242+
}
22382243

22392244
v8_source_set("parser_fuzzer") {
22402245
sources = [
@@ -2248,7 +2253,8 @@ v8_source_set("parser_fuzzer") {
22482253
configs = [ ":internal_config" ]
22492254
}
22502255

2251-
v8_fuzzer("parser_fuzzer") {}
2256+
v8_fuzzer("parser_fuzzer") {
2257+
}
22522258

22532259
v8_source_set("regexp_fuzzer") {
22542260
sources = [
@@ -2262,7 +2268,8 @@ v8_source_set("regexp_fuzzer") {
22622268
configs = [ ":internal_config" ]
22632269
}
22642270

2265-
v8_fuzzer("regexp_fuzzer") {}
2271+
v8_fuzzer("regexp_fuzzer") {
2272+
}
22662273

22672274
v8_source_set("wasm_fuzzer") {
22682275
sources = [
@@ -2276,7 +2283,8 @@ v8_source_set("wasm_fuzzer") {
22762283
configs = [ ":internal_config" ]
22772284
}
22782285

2279-
v8_fuzzer("wasm_fuzzer") {}
2286+
v8_fuzzer("wasm_fuzzer") {
2287+
}
22802288

22812289
v8_source_set("wasm_asmjs_fuzzer") {
22822290
sources = [
@@ -2290,4 +2298,5 @@ v8_source_set("wasm_asmjs_fuzzer") {
22902298
configs = [ ":internal_config" ]
22912299
}
22922300

2293-
v8_fuzzer("wasm_asmjs_fuzzer") {}
2301+
v8_fuzzer("wasm_asmjs_fuzzer") {
2302+
}

include/v8.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6707,7 +6707,24 @@ class V8_EXPORT V8 {
67076707
* If V8 was compiled with the ICU data in an external file, the location
67086708
* of the data file has to be provided.
67096709
*/
6710-
static bool InitializeICU(const char* icu_data_file = NULL);
6710+
V8_DEPRECATE_SOON(
6711+
"Use version with default location.",
6712+
static bool InitializeICU(const char* icu_data_file = nullptr));
6713+
6714+
/**
6715+
* Initialize the ICU library bundled with V8. The embedder should only
6716+
* invoke this method when using the bundled ICU. If V8 was compiled with
6717+
* the ICU data in an external file and when the default location of that
6718+
* file should be used, a path to the executable must be provided.
6719+
* Returns true on success.
6720+
*
6721+
* The default is a file called icudtl.dat side-by-side with the executable.
6722+
*
6723+
* Optionally, the location of the data file can be provided to override the
6724+
* default.
6725+
*/
6726+
static bool InitializeICUDefaultLocation(const char* exec_path,
6727+
const char* icu_data_file = nullptr);
67116728

67126729
/**
67136730
* Initialize the external startup data. The embedder only needs to

samples/hello-world.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
2424

2525
int main(int argc, char* argv[]) {
2626
// Initialize V8.
27-
V8::InitializeICU();
27+
V8::InitializeICUDefaultLocation(argv[0]);
2828
V8::InitializeExternalStartupData(argv[0]);
2929
Platform* platform = platform::CreateDefaultPlatform();
3030
V8::InitializePlatform(platform);

samples/process.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ void PrintMap(map<string, string>* m) {
687687

688688

689689
int main(int argc, char* argv[]) {
690-
v8::V8::InitializeICU();
690+
v8::V8::InitializeICUDefaultLocation(argv[0]);
691691
v8::V8::InitializeExternalStartupData(argv[0]);
692692
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
693693
v8::V8::InitializePlatform(platform);

samples/shell.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
7575

7676

7777
int main(int argc, char* argv[]) {
78-
v8::V8::InitializeICU();
78+
v8::V8::InitializeICUDefaultLocation(argv[0]);
7979
v8::V8::InitializeExternalStartupData(argv[0]);
8080
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
8181
v8::V8::InitializePlatform(platform);

src/api.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5513,6 +5513,10 @@ bool v8::V8::InitializeICU(const char* icu_data_file) {
55135513
return i::InitializeICU(icu_data_file);
55145514
}
55155515

5516+
bool v8::V8::InitializeICUDefaultLocation(const char* exec_path,
5517+
const char* icu_data_file) {
5518+
return i::InitializeICUDefaultLocation(exec_path, icu_data_file);
5519+
}
55165520

55175521
void v8::V8::InitializeExternalStartupData(const char* directory_path) {
55185522
i::InitializeExternalStartupData(directory_path);

src/base/file-utils.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2016 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "src/base/file-utils.h"
6+
7+
#include <string.h>
8+
9+
#include "src/base/platform/platform.h"
10+
11+
namespace v8 {
12+
namespace internal {
13+
14+
char* RelativePath(char** buffer, const char* exec_path, const char* name) {
15+
DCHECK(exec_path);
16+
int path_separator = static_cast<int>(strlen(exec_path)) - 1;
17+
while (path_separator >= 0 &&
18+
!base::OS::isDirectorySeparator(exec_path[path_separator])) {
19+
path_separator--;
20+
}
21+
if (path_separator >= 0) {
22+
int name_length = static_cast<int>(strlen(name));
23+
*buffer =
24+
reinterpret_cast<char*>(calloc(path_separator + name_length + 2, 1));
25+
*buffer[0] = '\0';
26+
strncat(*buffer, exec_path, path_separator + 1);
27+
strncat(*buffer, name, name_length);
28+
} else {
29+
*buffer = strdup(name);
30+
}
31+
return *buffer;
32+
}
33+
34+
} // namespace internal
35+
} // namespace v8

src/base/file-utils.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2016 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef V8_FILE_UTILS_H_
6+
#define V8_FILE_UTILS_H_
7+
8+
namespace v8 {
9+
namespace internal {
10+
11+
// Helper functions to manipulate file paths.
12+
13+
char* RelativePath(char** buffer, const char* exec_path, const char* name);
14+
15+
} // namespace internal
16+
} // namespace v8
17+
18+
#endif // V8_FILE_UTILS_H_

src/d8.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2422,7 +2422,7 @@ int Shell::Main(int argc, char* argv[]) {
24222422
#endif // defined(_MSC_VER)
24232423
#endif // defined(_WIN32) || defined(_WIN64)
24242424
if (!SetOptions(argc, argv)) return 1;
2425-
v8::V8::InitializeICU(options.icu_data_file);
2425+
v8::V8::InitializeICUDefaultLocation(argv[0], options.icu_data_file);
24262426
#ifndef V8_SHARED
24272427
g_platform = i::FLAG_verify_predictable
24282428
? new PredictablePlatform()

src/icu_util.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "unicode/putil.h"
1616
#include "unicode/udata.h"
1717

18+
#include "src/base/file-utils.h"
19+
1820
#define ICU_UTIL_DATA_FILE 0
1921
#define ICU_UTIL_DATA_SHARED 1
2022
#define ICU_UTIL_DATA_STATIC 2
@@ -38,6 +40,26 @@ void free_icu_data_ptr() {
3840
} // namespace
3941
#endif
4042

43+
bool InitializeICUDefaultLocation(const char* exec_path,
44+
const char* icu_data_file) {
45+
#if !defined(V8_I18N_SUPPORT)
46+
return true;
47+
#else
48+
#if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
49+
if (icu_data_file) {
50+
return InitializeICU(icu_data_file);
51+
}
52+
char* icu_data_file_default;
53+
RelativePath(&icu_data_file_default, exec_path, "icudtl.dat");
54+
bool result = InitializeICU(icu_data_file_default);
55+
free(icu_data_file_default);
56+
return result;
57+
#else
58+
return InitializeICU(NULL);
59+
#endif
60+
#endif
61+
}
62+
4163
bool InitializeICU(const char* icu_data_file) {
4264
#if !defined(V8_I18N_SUPPORT)
4365
return true;

0 commit comments

Comments
 (0)