Skip to content

Commit 00cbf5b

Browse files
sgallagherMylesBorins
authored andcommitted
build: auto-load ICU data from --with-icu-default-data-dir
When compiled with `--with-intl=small` and `--with-icu-default-data-dir=PATH`, Node.js will use PATH as a fallback location for the ICU data. We will first perform an access check using fopen(PATH, 'r') to ensure that the file is readable. If it is, we'll set the icu_data_directory and proceed. There's a slight overhead for the fopen() check, but it should be barely measurable. This will be useful for Linux distribution packagers who want to be able to ship a minimal node binary in a container image but also be able to add on the full i18n support where needed. With this patch, it becomes possible to ship the interpreter as /usr/bin/node in one package for the distribution and to ship the data files in another package (without a strict dependency between the two). This means that users of the distribution will not need to explicitly direct Node.js to locate the ICU data. It also means that in environments where full internationalization is not required, they do not need to carry the extra content (with the associated storage costs). Refs: #3460 Signed-off-by: Stephen Gallagher <sgallagh@redhat.com> PR-URL: #30825 Reviewed-By: Steven R Loomis <srloomis@us.ibm.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 30d3249 commit 00cbf5b

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

configure.py

+9
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,14 @@
450450
'the icu4c source archive. '
451451
'v%d.x or later recommended.' % icu_versions['minimum_icu'])
452452

453+
intl_optgroup.add_option('--with-icu-default-data-dir',
454+
action='store',
455+
dest='with_icu_default_data_dir',
456+
help='Path to the icuXXdt{lb}.dat file. If unspecified, ICU data will '
457+
'only be read if the NODE_ICU_DATA environment variable or the '
458+
'--icu-data-dir runtime argument is used. This option has effect '
459+
'only when Node.js is built with --with-intl=small-icu.')
460+
453461
parser.add_option('--with-ltcg',
454462
action='store_true',
455463
dest='with_ltcg',
@@ -1394,6 +1402,7 @@ def write_config(data, name):
13941402
locs.add('root') # must have root
13951403
o['variables']['icu_locales'] = ','.join(str(loc) for loc in locs)
13961404
# We will check a bit later if we can use the canned deps/icu-small
1405+
o['variables']['icu_default_data'] = options.with_icu_default_data_dir or ''
13971406
elif with_intl == 'full-icu':
13981407
# full ICU
13991408
o['variables']['v8_enable_i18n_support'] = 1

node.gypi

+7
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@
103103
'conditions': [
104104
[ 'icu_small=="true"', {
105105
'defines': [ 'NODE_HAVE_SMALL_ICU=1' ],
106+
'conditions': [
107+
[ 'icu_default_data!=""', {
108+
'defines': [
109+
'NODE_ICU_DEFAULT_DATA_DIR="<(icu_default_data)"',
110+
],
111+
}],
112+
],
106113
}]],
107114
}],
108115
[ 'node_no_browser_globals=="true"', {

src/node.cc

+20
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090

9191
#if defined(NODE_HAVE_I18N_SUPPORT)
9292
#include <unicode/uvernum.h>
93+
#include <unicode/utypes.h>
9394
#endif
9495

9596

@@ -882,6 +883,25 @@ int InitializeNodeWithArgs(std::vector<std::string>* argv,
882883
if (per_process::cli_options->icu_data_dir.empty())
883884
credentials::SafeGetenv("NODE_ICU_DATA",
884885
&per_process::cli_options->icu_data_dir);
886+
887+
#ifdef NODE_ICU_DEFAULT_DATA_DIR
888+
// If neither the CLI option nor the environment variable was specified,
889+
// fall back to the configured default
890+
if (per_process::cli_options->icu_data_dir.empty()) {
891+
// Check whether the NODE_ICU_DEFAULT_DATA_DIR contains the right data
892+
// file and can be read.
893+
static const char full_path[] =
894+
NODE_ICU_DEFAULT_DATA_DIR "/" U_ICUDATA_NAME ".dat";
895+
896+
FILE* f = fopen(full_path, "rb");
897+
898+
if (f != nullptr) {
899+
fclose(f);
900+
per_process::cli_options->icu_data_dir = NODE_ICU_DEFAULT_DATA_DIR;
901+
}
902+
}
903+
#endif // NODE_ICU_DEFAULT_DATA_DIR
904+
885905
// Initialize ICU.
886906
// If icu_data_dir is empty here, it will load the 'minimal' data.
887907
if (!i18n::InitializeICUDirectory(per_process::cli_options->icu_data_dir)) {

0 commit comments

Comments
 (0)