Debian Codegraph @

intl/icu/source/tools/pkgdata/pkgdata.cpp

raw
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /******************************************************************************
4 * Copyright (C) 2000-2016, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *******************************************************************************
7 * file name: pkgdata.cpp
8 * encoding: ANSI X3.4 (1968)
9 * tab size: 8 (not used)
10 * indentation:4
11 *
12 * created on: 2000may15
13 * created by: Steven \u24C7 Loomis
14 *
15 * This program packages the ICU data into different forms
16 * (DLL, common data, etc.)
17 */
18
19 // Defines _XOPEN_SOURCE for access to POSIX functions.
20 // Must be before any other #includes.
21 #include "uposixdefs.h"
22
23 #include "unicode/utypes.h"
24
25 #include "unicode/putil.h"
26 #include "putilimp.h"
27
28 #if U_HAVE_POPEN
29 #if (U_PF_MINGW <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(__STRICT_ANSI__)
30 /* popen/pclose aren't defined in strict ANSI on Cygwin and MinGW */
31 #undef __STRICT_ANSI__
32 #endif
33 #endif
34
35 #include "cmemory.h"
36 #include "cstring.h"
37 #include "filestrm.h"
38 #include "toolutil.h"
39 #include "unicode/uclean.h"
40 #include "unewdata.h"
41 #include "uoptions.h"
42 #include "package.h"
43 #include "pkg_icu.h"
44 #include "pkg_genc.h"
45 #include "pkg_gencmn.h"
46 #include "flagparser.h"
47 #include "filetools.h"
48 #include "charstr.h"
49 #include "uassert.h"
50
51 #if U_HAVE_POPEN
52 # include <unistd.h>
53 #endif
54
55 #include <stdio.h>
56 #include <stdlib.h>
57
58 U_CDECL_BEGIN
59 #include "pkgtypes.h"
60 U_CDECL_END
61
62 #if U_HAVE_POPEN
63 U_NAMESPACE_BEGIN
64 U_DEFINE_LOCAL_OPEN_POINTER(LocalPipeFilePointer, FILE, pclose);
65 U_NAMESPACE_END
66 #endif
67
68 using icu::LocalMemory;
69
70 static void loadLists(UPKGOptions *o, UErrorCode *status);
71
72 static int32_t pkg_executeOptions(UPKGOptions *o);
73
74 #ifdef WINDOWS_WITH_MSVC
75 static int32_t pkg_createWindowsDLL(const char mode, const char *gencFilePath, UPKGOptions *o);
76 #endif
77 static int32_t pkg_createSymLinks(const char *targetDir, UBool specialHandling=false);
78 static int32_t pkg_installLibrary(const char *installDir, const char *dir, UBool noVersion);
79 static int32_t pkg_installFileMode(const char *installDir, const char *srcDir, const char *fileListName);
80 static int32_t pkg_installCommonMode(const char *installDir, const char *fileName);
81
82 #ifdef BUILD_DATA_WITHOUT_ASSEMBLY
83 static int32_t pkg_createWithoutAssemblyCode(UPKGOptions *o, const char *targetDir, const char mode);
84 #endif
85
86 #ifdef CAN_WRITE_OBJ_CODE
87 static void pkg_createOptMatchArch(char *optMatchArch);
88 static void pkg_destroyOptMatchArch(char *optMatchArch);
89 #endif
90
91 static int32_t pkg_createWithAssemblyCode(const char *targetDir, const char mode, const char *gencFilePath);
92 static int32_t pkg_generateLibraryFile(const char *targetDir, const char mode, const char *objectFile, char *command = nullptr, UBool specialHandling=false);
93 static int32_t pkg_archiveLibrary(const char *targetDir, const char *version, UBool reverseExt);
94 static void createFileNames(UPKGOptions *o, const char mode, const char *version_major, const char *version, const char *libName, const UBool reverseExt, UBool noVersion);
95 static int32_t initializePkgDataFlags(UPKGOptions *o);
96
97 static int32_t pkg_getPkgDataPath(UBool verbose, UOption *option);
98 static int runCommand(const char* command, UBool specialHandling=false);
99
100 #define IN_COMMON_MODE(mode) (mode == 'a' || mode == 'c')
101 #define IN_DLL_MODE(mode) (mode == 'd' || mode == 'l')
102 #define IN_STATIC_MODE(mode) (mode == 's')
103 #define IN_FILES_MODE(mode) (mode == 'f')
104
105 enum {
106 NAME,
107 BLDOPT,
108 MODE,
109 HELP,
110 HELP_QUESTION_MARK,
111 VERBOSE,
112 COPYRIGHT,
113 COMMENT,
114 DESTDIR,
115 REBUILD,
116 TEMPDIR,
117 INSTALL,
118 SOURCEDIR,
119 ENTRYPOINT,
120 REVISION,
121 FORCE_PREFIX,
122 LIBNAME,
123 QUIET,
124 WITHOUT_ASSEMBLY,
125 PDS_BUILD,
126 WIN_UWP_BUILD,
127 WIN_DLL_ARCH,
128 WIN_DYNAMICBASE
129 };
130
131 /* This sets the modes that are available */
132 static struct {
133 const char *name, *alt_name;
134 const char *desc;
135 } modes[] = {
136 { "files", nullptr, "Uses raw data files (no effect). Installation copies all files to the target location." },
137 #if U_PLATFORM_HAS_WIN32_API
138 { "dll", "library", "Generates one common data file and one shared library, <package>.dll"},
139 { "common", "archive", "Generates just the common file, <package>.dat"},
140 { "static", "static", "Generates one statically linked library, " LIB_PREFIX "<package>" UDATA_LIB_SUFFIX }
141 #else
142 #ifdef UDATA_SO_SUFFIX
143 { "dll", "library", "Generates one shared library, <package>" UDATA_SO_SUFFIX },
144 #endif
145 { "common", "archive", "Generates one common data file, <package>.dat" },
146 { "static", "static", "Generates one statically linked library, " LIB_PREFIX "<package>" UDATA_LIB_SUFFIX }
147 #endif
148 };
149
150 static UOption options[]={
151 /*00*/ UOPTION_DEF( "name", 'p', UOPT_REQUIRES_ARG),
152 /*01*/ UOPTION_DEF( "bldopt", 'O', UOPT_REQUIRES_ARG), /* on Win32 it is release or debug */
153 /*02*/ UOPTION_DEF( "mode", 'm', UOPT_REQUIRES_ARG),
154 /*03*/ UOPTION_HELP_H, /* -h */
155 /*04*/ UOPTION_HELP_QUESTION_MARK, /* -? */
156 /*05*/ UOPTION_VERBOSE, /* -v */
157 /*06*/ UOPTION_COPYRIGHT, /* -c */
158 /*07*/ UOPTION_DEF( "comment", 'C', UOPT_REQUIRES_ARG),
159 /*08*/ UOPTION_DESTDIR, /* -d */
160 /*11*/ UOPTION_DEF( "rebuild", 'F', UOPT_NO_ARG),
161 /*12*/ UOPTION_DEF( "tempdir", 'T', UOPT_REQUIRES_ARG),
162 /*13*/ UOPTION_DEF( "install", 'I', UOPT_REQUIRES_ARG),
163 /*14*/ UOPTION_SOURCEDIR ,
164 /*15*/ UOPTION_DEF( "entrypoint", 'e', UOPT_REQUIRES_ARG),
165 /*16*/ UOPTION_DEF( "revision", 'r', UOPT_REQUIRES_ARG),
166 /*17*/ UOPTION_DEF( "force-prefix", 'f', UOPT_NO_ARG),
167 /*18*/ UOPTION_DEF( "libname", 'L', UOPT_REQUIRES_ARG),
168 /*19*/ UOPTION_DEF( "quiet", 'q', UOPT_NO_ARG),
169 /*20*/ UOPTION_DEF( "without-assembly", 'w', UOPT_NO_ARG),
170 /*21*/ UOPTION_DEF("zos-pds-build", 'z', UOPT_NO_ARG),
171 /*22*/ UOPTION_DEF("windows-uwp-build", 'u', UOPT_NO_ARG),
172 /*23*/ UOPTION_DEF("windows-DLL-arch", 'a', UOPT_REQUIRES_ARG),
173 /*24*/ UOPTION_DEF("windows-dynamicbase", 'b', UOPT_NO_ARG),
174 };
175
176 /* This enum and the following char array should be kept in sync. */
177 enum {
178 GENCCODE_ASSEMBLY_TYPE,
179 SO_EXT,
180 SOBJ_EXT,
181 A_EXT,
182 LIBPREFIX,
183 LIB_EXT_ORDER,
184 COMPILER,
185 LIBFLAGS,
186 GENLIB,
187 LDICUDTFLAGS,
188 LD_SONAME,
189 RPATH_FLAGS,
190 BIR_FLAGS,
191 AR,
192 ARFLAGS,
193 RANLIB,
194 INSTALL_CMD,
195 PKGDATA_FLAGS_SIZE
196 };
197 static const char* FLAG_NAMES[PKGDATA_FLAGS_SIZE] = {
198 "GENCCODE_ASSEMBLY_TYPE",
199 "SO",
200 "SOBJ",
201 "A",
202 "LIBPREFIX",
203 "LIB_EXT_ORDER",
204 "COMPILE",
205 "LIBFLAGS",
206 "GENLIB",
207 "LDICUDTFLAGS",
208 "LD_SONAME",
209 "RPATH_FLAGS",
210 "BIR_LDFLAGS",
211 "AR",
212 "ARFLAGS",
213 "RANLIB",
214 "INSTALL_CMD"
215 };
216 static char **pkgDataFlags = nullptr;
217
218 enum {
219 LIB_FILE,
220 LIB_FILE_VERSION_MAJOR,
221 LIB_FILE_VERSION,
222 LIB_FILE_VERSION_TMP,
223 #if U_PLATFORM == U_PF_CYGWIN
224 LIB_FILE_CYGWIN,
225 LIB_FILE_CYGWIN_VERSION,
226 #elif U_PLATFORM == U_PF_MINGW
227 LIB_FILE_MINGW,
228 #elif U_PLATFORM == U_PF_OS390
229 LIB_FILE_OS390BATCH_MAJOR,
230 LIB_FILE_OS390BATCH_VERSION,
231 #endif
232 LIB_FILENAMES_SIZE
233 };
234 static char libFileNames[LIB_FILENAMES_SIZE][256];
235
236 static UPKGOptions *pkg_checkFlag(UPKGOptions *o);
237
238 const char options_help[][320]={
239 "Set the data name",
240 #ifdef U_MAKE_IS_NMAKE
241 "The directory where the ICU is located (e.g. <ICUROOT> which contains the bin directory)",
242 #else
243 "Specify options for the builder.",
244 #endif
245 "Specify the mode of building (see below; default: common)",
246 "This usage text",
247 "This usage text",
248 "Make the output verbose",
249 "Use the standard ICU copyright",
250 "Use a custom comment (instead of the copyright)",
251 "Specify the destination directory for files",
252 "Force rebuilding of all data",
253 "Specify temporary dir (default: output dir)",
254 "Install the data (specify target)",
255 "Specify a custom source directory",
256 "Specify a custom entrypoint name (default: short name)",
257 "Specify a version when packaging in dll or static mode",
258 "Add package to all file names if not present",
259 "Library name to build (if different than package name)",
260 "Quiet mode. (e.g. Do not output a readme file for static libraries)",
261 "Build the data without assembly code",
262 "Build PDS dataset (zOS build only)",
263 "Build for Universal Windows Platform (Windows build only)",
264 "Specify the DLL machine architecture for LINK.exe (Windows build only)",
265 "Ignored. Enable DYNAMICBASE on the DLL. This is now the default. (Windows build only)",
266 };
267
268 const char *progname = "PKGDATA";
269
270 int
271 main(int argc, char* argv[]) {
272 int result = 0;
273 /* FileStream *out; */
274 UPKGOptions o;
275 CharList *tail;
276 UBool needsHelp = false;
277 UErrorCode status = U_ZERO_ERROR;
278 /* char tmp[1024]; */
279 uint32_t i;
280 int32_t n;
281
282 U_MAIN_INIT_ARGS(argc, argv);
283
284 progname = argv[0];
285
286 options[MODE].value = "common";
287
288 /* read command line options */
289 argc=u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options);
290
291 /* error handling, printing usage message */
292 /* I've decided to simply print an error and quit. This tool has too
293 many options to just display them all of the time. */
294
295 if(options[HELP].doesOccur || options[HELP_QUESTION_MARK].doesOccur) {
296 needsHelp = true;
297 }
298 else {
299 if(!needsHelp && argc<0) {
300 fprintf(stderr,
301 "%s: error in command line argument \"%s\"\n",
302 progname,
303 argv[-argc]);
304 fprintf(stderr, "Run '%s --help' for help.\n", progname);
305 return 1;
306 }
307
308
309 #if !defined(WINDOWS_WITH_MSVC) || defined(USING_CYGWIN)
310 if(!options[BLDOPT].doesOccur && uprv_strcmp(options[MODE].value, "common") != 0) {
311 if (pkg_getPkgDataPath(options[VERBOSE].doesOccur, &options[BLDOPT]) != 0) {
312 fprintf(stderr, " required parameter is missing: -O is required for static and shared builds.\n");
313 fprintf(stderr, "Run '%s --help' for help.\n", progname);
314 return 1;
315 }
316 }
317 #else
318 if(options[BLDOPT].doesOccur) {
319 fprintf(stdout, "Warning: You are using the -O option which is not needed for MSVC build on Windows.\n");
320 }
321 #endif
322
323 if(!options[NAME].doesOccur) /* -O we already have - don't report it. */
324 {
325 fprintf(stderr, " required parameter -p is missing \n");
326 fprintf(stderr, "Run '%s --help' for help.\n", progname);
327 return 1;
328 }
329
330 if(argc == 1) {
331 fprintf(stderr,
332 "No input files specified.\n"
333 "Run '%s --help' for help.\n", progname);
334 return 1;
335 }
336 } /* end !needsHelp */
337
338 if(argc<0 || needsHelp ) {
339 fprintf(stderr,
340 "usage: %s [-options] [-] [packageFile] \n"
341 "\tProduce packaged ICU data from the given list(s) of files.\n"
342 "\t'-' by itself means to read from stdin.\n"
343 "\tpackageFile is a text file containing the list of files to package.\n",
344 progname);
345
346 fprintf(stderr, "\n options:\n");
347 for(i=0;i<UPRV_LENGTHOF(options);i++) {
348 fprintf(stderr, "%-5s -%c %s%-10s %s\n",
349 (i<1?"[REQ]":""),
350 options[i].shortName,
351 options[i].longName ? "or --" : " ",
352 options[i].longName ? options[i].longName : "",
353 options_help[i]);
354 }
355
356 fprintf(stderr, "modes: (-m option)\n");
357 for(i=0;i<UPRV_LENGTHOF(modes);i++) {
358 fprintf(stderr, " %-9s ", modes[i].name);
359 if (modes[i].alt_name) {
360 fprintf(stderr, "/ %-9s", modes[i].alt_name);
361 } else {
362 fprintf(stderr, " ");
363 }
364 fprintf(stderr, " %s\n", modes[i].desc);
365 }
366 return 1;
367 }
368
369 /* OK, fill in the options struct */
370 uprv_memset(&o, 0, sizeof(o));
371
372 o.mode = options[MODE].value;
373 o.version = options[REVISION].doesOccur ? options[REVISION].value : nullptr;
374
375 o.shortName = options[NAME].value;
376 {
377 int32_t len = static_cast<int32_t>(uprv_strlen(o.shortName));
378 char *csname, *cp;
379 const char *sp;
380
381 cp = csname = static_cast<char*>(uprv_malloc((len + 1 + 1) * sizeof(*o.cShortName)));
382 if (*(sp = o.shortName)) {
383 *cp++ = isalpha(*sp) ? * sp : '_';
384 for (++sp; *sp; ++sp) {
385 *cp++ = isalnum(*sp) ? *sp : '_';
386 }
387 }
388 *cp = 0;
389
390 o.cShortName = csname;
391 }
392
393 if(options[LIBNAME].doesOccur) { /* get libname from shortname, or explicit -L parameter */
394 o.libName = options[LIBNAME].value;
395 } else {
396 o.libName = o.shortName;
397 }
398
399 if(options[QUIET].doesOccur) {
400 o.quiet = true;
401 } else {
402 o.quiet = false;
403 }
404
405 if(options[PDS_BUILD].doesOccur) {
406 #if U_PLATFORM == U_PF_OS390
407 o.pdsbuild = true;
408 #else
409 o.pdsbuild = false;
410 fprintf(stdout, "Warning: You are using the -z option which only works on z/OS.\n");
411
412 #endif
413 } else {
414 o.pdsbuild = false;
415 }
416
417 o.verbose = options[VERBOSE].doesOccur;
418
419
420 #if !defined(WINDOWS_WITH_MSVC) || defined(USING_CYGWIN) /* on UNIX, we'll just include the file... */
421 if (options[BLDOPT].doesOccur) {
422 o.options = options[BLDOPT].value;
423 } else {
424 o.options = nullptr;
425 }
426 #endif
427 if(options[COPYRIGHT].doesOccur) {
428 o.comment = U_COPYRIGHT_STRING;
429 } else if (options[COMMENT].doesOccur) {
430 o.comment = options[COMMENT].value;
431 }
432
433 if( options[DESTDIR].doesOccur ) {
434 o.targetDir = options[DESTDIR].value;
435 } else {
436 o.targetDir = "."; /* cwd */
437 }
438
439 o.rebuild = options[REBUILD].doesOccur;
440
441 if( options[TEMPDIR].doesOccur ) {
442 o.tmpDir = options[TEMPDIR].value;
443 } else {
444 o.tmpDir = o.targetDir;
445 }
446
447 if( options[INSTALL].doesOccur ) {
448 o.install = options[INSTALL].value;
449 } else {
450 o.install = nullptr;
451 }
452
453 if( options[SOURCEDIR].doesOccur ) {
454 o.srcDir = options[SOURCEDIR].value;
455 } else {
456 o.srcDir = ".";
457 }
458
459 if( options[ENTRYPOINT].doesOccur ) {
460 o.entryName = options[ENTRYPOINT].value;
461 } else {
462 o.entryName = o.cShortName;
463 }
464
465 o.withoutAssembly = false;
466 if (options[WITHOUT_ASSEMBLY].doesOccur) {
467 #ifndef BUILD_DATA_WITHOUT_ASSEMBLY
468 fprintf(stdout, "Warning: You are using the option to build without assembly code which is not supported on this platform.\n");
469 fprintf(stdout, "Warning: This option will be ignored.\n");
470 #else
471 o.withoutAssembly = true;
472 #endif
473 }
474
475 if (options[WIN_DYNAMICBASE].doesOccur) {
476 fprintf(stdout, "Note: Ignoring option -b (windows-dynamicbase).\n");
477 }
478
479 /* OK options are set up. Now the file lists. */
480 tail = nullptr;
481 for( n=1; n<argc; n++) {
482 o.fileListFiles = pkg_appendToList(o.fileListFiles, &tail, uprv_strdup(argv[n]));
483 }
484
485 /* load the files */
486 loadLists(&o, &status);
487 if( U_FAILURE(status) ) {
488 fprintf(stderr, "error loading input file lists: %s\n", u_errorName(status));
489 return 2;
490 }
491
492 result = pkg_executeOptions(&o);
493
494 if (pkgDataFlags != nullptr) {
495 for (n = 0; n < PKGDATA_FLAGS_SIZE; n++) {
496 if (pkgDataFlags[n] != nullptr) {
497 uprv_free(pkgDataFlags[n]);
498 }
499 }
500 uprv_free(pkgDataFlags);
501 }
502
503 if (o.cShortName != nullptr) {
504 uprv_free(const_cast<char*>(o.cShortName));
505 }
506 if (o.fileListFiles != nullptr) {
507 pkg_deleteList(o.fileListFiles);
508 }
509 if (o.filePaths != nullptr) {
510 pkg_deleteList(o.filePaths);
511 }
512 if (o.files != nullptr) {
513 pkg_deleteList(o.files);
514 }
515 return result;
516 }
517
518 static int runCommand(const char* command, UBool specialHandling) {
519 char *cmd = nullptr;
520 char cmdBuffer[SMALL_BUFFER_MAX_SIZE];
521 int32_t len = static_cast<int32_t>(strlen(command));
522
523 if (len == 0) {
524 return 0;
525 }
526
527 if (!specialHandling) {
528 #if defined(USING_CYGWIN) || U_PLATFORM == U_PF_MINGW || U_PLATFORM == U_PF_OS400
529 int32_t buff_len;
530 if ((len + BUFFER_PADDING_SIZE) >= SMALL_BUFFER_MAX_SIZE) {
531 cmd = (char *)uprv_malloc(len + BUFFER_PADDING_SIZE);
532 buff_len = len + BUFFER_PADDING_SIZE;
533 } else {
534 cmd = cmdBuffer;
535 buff_len = SMALL_BUFFER_MAX_SIZE;
536 }
537 #if defined(USING_CYGWIN) || U_PLATFORM == U_PF_MINGW
538 snprintf(cmd, buff_len, "bash -c \"%s\"", command);
539
540 #elif U_PLATFORM == U_PF_OS400
541 snprintf(cmd, buff_len "QSH CMD('%s')", command);
542 #endif
543 #else
544 goto normal_command_mode;
545 #endif
546 } else {
547 #if !(defined(USING_CYGWIN) || U_PLATFORM == U_PF_MINGW || U_PLATFORM == U_PF_OS400)
548 normal_command_mode:
549 #endif
550 cmd = const_cast<char*>(command);
551 }
552
553 printf("pkgdata: %s\n", cmd);
554 int result = system(cmd);
555 if (result != 0) {
556 fprintf(stderr, "-- return status = %d\n", result);
557 result = 1; // system() result code is platform specific.
558 }
559
560 if (cmd != cmdBuffer && cmd != command) {
561 uprv_free(cmd);
562 }
563
564 return result;
565 }
566
567 #define LN_CMD "ln -s"
568 #define RM_CMD "rm -f"
569
570 static int32_t pkg_executeOptions(UPKGOptions *o) {
571 int32_t result = 0;
572
573 const char mode = o->mode[0];
574 char targetDir[SMALL_BUFFER_MAX_SIZE] = "";
575 char tmpDir[SMALL_BUFFER_MAX_SIZE] = "";
576 char datFileName[SMALL_BUFFER_MAX_SIZE] = "";
577 char datFileNamePath[LARGE_BUFFER_MAX_SIZE] = "";
578 char checkLibFile[LARGE_BUFFER_MAX_SIZE] = "";
579
580 initializePkgDataFlags(o);
581
582 if (IN_FILES_MODE(mode)) {
583 /* Copy the raw data to the installation directory. */
584 if (o->install != nullptr) {
585 uprv_strcpy(targetDir, o->install);
586 if (o->shortName != nullptr) {
587 uprv_strcat(targetDir, PKGDATA_FILE_SEP_STRING);
588 uprv_strcat(targetDir, o->shortName);
589 }
590
591 if(o->verbose) {
592 fprintf(stdout, "# Install: Files mode, copying files to %s..\n", targetDir);
593 }
594 result = pkg_installFileMode(targetDir, o->srcDir, o->fileListFiles->str);
595 }
596 return result;
597 } else /* if (IN_COMMON_MODE(mode) || IN_DLL_MODE(mode) || IN_STATIC_MODE(mode)) */ {
598 UBool noVersion = false;
599
600 uprv_strcpy(targetDir, o->targetDir);
601 uprv_strcat(targetDir, PKGDATA_FILE_SEP_STRING);
602
603 uprv_strcpy(tmpDir, o->tmpDir);
604 uprv_strcat(tmpDir, PKGDATA_FILE_SEP_STRING);
605
606 uprv_strcpy(datFileNamePath, tmpDir);
607
608 uprv_strcpy(datFileName, o->shortName);
609 uprv_strcat(datFileName, UDATA_CMN_SUFFIX);
610
611 uprv_strcat(datFileNamePath, datFileName);
612
613 if(o->verbose) {
614 fprintf(stdout, "# Writing package file %s ..\n", datFileNamePath);
615 }
616 result = writePackageDatFile(datFileNamePath, o->comment, o->srcDir, o->fileListFiles->str, nullptr, U_CHARSET_FAMILY ? 'e' : U_IS_BIG_ENDIAN ? 'b' : 'l');
617 if (result != 0) {
618 fprintf(stderr,"Error writing package dat file.\n");
619 return result;
620 }
621
622 if (IN_COMMON_MODE(mode)) {
623 char targetFileNamePath[LARGE_BUFFER_MAX_SIZE] = "";
624
625 uprv_strcpy(targetFileNamePath, targetDir);
626 uprv_strcat(targetFileNamePath, datFileName);
627
628 /* Move the dat file created to the target directory. */
629 if (uprv_strcmp(datFileNamePath, targetFileNamePath) != 0) {
630 if (T_FileStream_file_exists(targetFileNamePath)) {
631 if ((result = remove(targetFileNamePath)) != 0) {
632 fprintf(stderr, "Unable to remove old dat file: %s\n",
633 targetFileNamePath);
634 return result;
635 }
636 }
637
638 result = rename(datFileNamePath, targetFileNamePath);
639
640 if (o->verbose) {
641 fprintf(stdout, "# Moving package file to %s ..\n",
642 targetFileNamePath);
643 }
644 if (result != 0) {
645 fprintf(
646 stderr,
647 "Unable to move dat file (%s) to target location (%s).\n",
648 datFileNamePath, targetFileNamePath);
649 return result;
650 }
651 }
652
653 if (o->install != nullptr) {
654 result = pkg_installCommonMode(o->install, targetFileNamePath);
655 }
656
657 return result;
658 } else /* if (IN_STATIC_MODE(mode) || IN_DLL_MODE(mode)) */ {
659 char gencFilePath[SMALL_BUFFER_MAX_SIZE] = "";
660 char version_major[10] = "";
661 UBool reverseExt = false;
662
663 #if !defined(WINDOWS_WITH_MSVC) || defined(USING_CYGWIN)
664 /* Get the version major number. */
665 if (o->version != nullptr) {
666 for (uint32_t i = 0;i < sizeof(version_major);i++) {
667 if (o->version[i] == '.') {
668 version_major[i] = 0;
669 break;
670 }
671 version_major[i] = o->version[i];
672 }
673 } else {
674 noVersion = true;
675 if (IN_DLL_MODE(mode)) {
676 fprintf(stdout, "Warning: Providing a revision number with the -r option is recommended when packaging data in the current mode.\n");
677 }
678 }
679
680 #if U_PLATFORM != U_PF_OS400
681 /* Certain platforms have different library extension ordering. (e.g. libicudata.##.so vs libicudata.so.##)
682 * reverseExt is false if the suffix should be the version number.
683 */
684 if (pkgDataFlags[LIB_EXT_ORDER][uprv_strlen(pkgDataFlags[LIB_EXT_ORDER])-1] == pkgDataFlags[SO_EXT][uprv_strlen(pkgDataFlags[SO_EXT])-1]) {
685 reverseExt = true;
686 }
687 #endif
688 /* Using the base libName and version number, generate the library file names. */
689 createFileNames(o, mode, version_major, o->version == nullptr ? "" : o->version, o->libName, reverseExt, noVersion);
690
691 if ((o->version!=nullptr || IN_STATIC_MODE(mode)) && o->rebuild == false && o->pdsbuild == false) {
692 /* Check to see if a previous built data library file exists and check if it is the latest. */
693 snprintf(checkLibFile, sizeof(checkLibFile), "%s%s", targetDir, libFileNames[LIB_FILE_VERSION]);
694 if (T_FileStream_file_exists(checkLibFile)) {
695 if (isFileModTimeLater(checkLibFile, o->srcDir, true) && isFileModTimeLater(checkLibFile, o->options)) {
696 if (o->install != nullptr) {
697 if(o->verbose) {
698 fprintf(stdout, "# Installing already-built library into %s\n", o->install);
699 }
700 result = pkg_installLibrary(o->install, targetDir, noVersion);
701 } else {
702 if(o->verbose) {
703 printf("# Not rebuilding %s - up to date.\n", checkLibFile);
704 }
705 }
706 return result;
707 } else if (o->verbose && (o->install!=nullptr)) {
708 fprintf(stdout, "# Not installing up-to-date library %s into %s\n", checkLibFile, o->install);
709 }
710 } else if(o->verbose && (o->install!=nullptr)) {
711 fprintf(stdout, "# Not installing missing %s into %s\n", checkLibFile, o->install);
712 }
713 }
714
715 if (pkg_checkFlag(o) == nullptr) {
716 /* Error occurred. */
717 return result;
718 }
719 #endif
720
721 if (!o->withoutAssembly && pkgDataFlags[GENCCODE_ASSEMBLY_TYPE][0] != 0) {
722 const char* genccodeAssembly = pkgDataFlags[GENCCODE_ASSEMBLY_TYPE];
723
724 if(o->verbose) {
725 fprintf(stdout, "# Generating assembly code %s of type %s ..\n", gencFilePath, genccodeAssembly);
726 }
727
728 /* Offset genccodeAssembly by 3 because "-a " */
729 if (genccodeAssembly &&
730 (uprv_strlen(genccodeAssembly)>3) &&
731 checkAssemblyHeaderName(genccodeAssembly+3)) {
732 writeAssemblyCode(
733 datFileNamePath,
734 o->tmpDir,
735 o->entryName,
736 nullptr,
737 gencFilePath,
738 sizeof(gencFilePath));
739
740 result = pkg_createWithAssemblyCode(targetDir, mode, gencFilePath);
741 if (result != 0) {
742 fprintf(stderr, "Error generating assembly code for data.\n");
743 return result;
744 } else if (IN_STATIC_MODE(mode)) {
745 if(o->install != nullptr) {
746 if(o->verbose) {
747 fprintf(stdout, "# Installing static library into %s\n", o->install);
748 }
749 result = pkg_installLibrary(o->install, targetDir, noVersion);
750 }
751 return result;
752 }
753 } else {
754 fprintf(stderr,"Assembly type \"%s\" is unknown.\n", genccodeAssembly);
755 return -1;
756 }
757 } else {
758 if(o->verbose) {
759 fprintf(stdout, "# Writing object code to %s ..\n", gencFilePath);
760 }
761 if (o->withoutAssembly) {
762 #ifdef BUILD_DATA_WITHOUT_ASSEMBLY
763 result = pkg_createWithoutAssemblyCode(o, targetDir, mode);
764 #else
765 /* This error should not occur. */
766 fprintf(stderr, "Error- BUILD_DATA_WITHOUT_ASSEMBLY is not defined. Internal error.\n");
767 #endif
768 } else {
769 #ifdef CAN_WRITE_OBJ_CODE
770 /* Try to detect the arch type, use nullptr if unsuccessful */
771 char optMatchArch[10] = { 0 };
772 pkg_createOptMatchArch(optMatchArch);
773 writeObjectCode(
774 datFileNamePath,
775 o->tmpDir,
776 o->entryName,
777 (optMatchArch[0] == 0 ? nullptr : optMatchArch),
778 nullptr,
779 nullptr,
780 gencFilePath,
781 sizeof(gencFilePath),
782 true);
783 pkg_destroyOptMatchArch(optMatchArch);
784 #if U_PLATFORM_IS_LINUX_BASED
785 result = pkg_generateLibraryFile(targetDir, mode, gencFilePath);
786 #elif defined(WINDOWS_WITH_MSVC)
787 result = pkg_createWindowsDLL(mode, gencFilePath, o);
788 #endif
789 #elif defined(BUILD_DATA_WITHOUT_ASSEMBLY)
790 result = pkg_createWithoutAssemblyCode(o, targetDir, mode);
791 #else
792 fprintf(stderr, "Error- neither CAN_WRITE_OBJ_CODE nor BUILD_DATA_WITHOUT_ASSEMBLY are defined. Internal error.\n");
793 return 1;
794 #endif
795 }
796
797 if (result != 0) {
798 fprintf(stderr, "Error generating package data.\n");
799 return result;
800 }
801 }
802 #if !U_PLATFORM_USES_ONLY_WIN32_API
803 if(!IN_STATIC_MODE(mode)) {
804 /* Certain platforms uses archive library. (e.g. AIX) */
805 if(o->verbose) {
806 fprintf(stdout, "# Creating data archive library file ..\n");
807 }
808 result = pkg_archiveLibrary(targetDir, o->version, reverseExt);
809 if (result != 0) {
810 fprintf(stderr, "Error creating data archive library file.\n");
811 return result;
812 }
813 #if U_PLATFORM != U_PF_OS400
814 if (!noVersion) {
815 /* Create symbolic links for the final library file. */
816 #if U_PLATFORM == U_PF_OS390
817 result = pkg_createSymLinks(targetDir, o->pdsbuild);
818 #else
819 result = pkg_createSymLinks(targetDir, noVersion);
820 #endif
821 if (result != 0) {
822 fprintf(stderr, "Error creating symbolic links of the data library file.\n");
823 return result;
824 }
825 }
826 #endif
827 } /* !IN_STATIC_MODE */
828 #endif
829
830 #if !U_PLATFORM_USES_ONLY_WIN32_API
831 /* Install the libraries if option was set. */
832 if (o->install != nullptr) {
833 if(o->verbose) {
834 fprintf(stdout, "# Installing library file to %s ..\n", o->install);
835 }
836 result = pkg_installLibrary(o->install, targetDir, noVersion);
837 if (result != 0) {
838 fprintf(stderr, "Error installing the data library.\n");
839 return result;
840 }
841 }
842 #endif
843 }
844 }
845 return result;
846 }
847
848 /* Initialize the pkgDataFlags with the option file given. */
849 static int32_t initializePkgDataFlags(UPKGOptions *o) {
850 UErrorCode status = U_ZERO_ERROR;
851 int32_t result = 0;
852 int32_t currentBufferSize = SMALL_BUFFER_MAX_SIZE;
853 int32_t tmpResult = 0;
854
855 /* Initialize pkgdataFlags */
856 pkgDataFlags = static_cast<char**>(uprv_malloc(sizeof(char*) * PKGDATA_FLAGS_SIZE));
857
858 /* If we run out of space, allocate more */
859 #if !defined(WINDOWS_WITH_MSVC) || defined(USING_CYGWIN)
860 do {
861 #endif
862 if (pkgDataFlags != nullptr) {
863 for (int32_t i = 0; i < PKGDATA_FLAGS_SIZE; i++) {
864 pkgDataFlags[i] = static_cast<char*>(uprv_malloc(sizeof(char) * currentBufferSize));
865 if (pkgDataFlags[i] != nullptr) {
866 pkgDataFlags[i][0] = 0;
867 } else {
868 fprintf(stderr,"Error allocating memory for pkgDataFlags.\n");
869 /* If an error occurs, ensure that the rest of the array is nullptr */
870 for (int32_t n = i + 1; n < PKGDATA_FLAGS_SIZE; n++) {
871 pkgDataFlags[n] = nullptr;
872 }
873 return -1;
874 }
875 }
876 } else {
877 fprintf(stderr,"Error allocating memory for pkgDataFlags.\n");
878 return -1;
879 }
880
881 if (o->options == nullptr) {
882 return result;
883 }
884
885 #if !defined(WINDOWS_WITH_MSVC) || defined(USING_CYGWIN)
886 /* Read in options file. */
887 if(o->verbose) {
888 fprintf(stdout, "# Reading options file %s\n", o->options);
889 }
890 status = U_ZERO_ERROR;
891 tmpResult = parseFlagsFile(o->options, pkgDataFlags, currentBufferSize, FLAG_NAMES, static_cast<int32_t>(PKGDATA_FLAGS_SIZE), &status);
892 if (status == U_BUFFER_OVERFLOW_ERROR) {
893 for (int32_t i = 0; i < PKGDATA_FLAGS_SIZE; i++) {
894 if (pkgDataFlags[i]) {
895 uprv_free(pkgDataFlags[i]);
896 pkgDataFlags[i] = nullptr;
897 }
898 }
899 currentBufferSize = tmpResult;
900 } else if (U_FAILURE(status)) {
901 fprintf(stderr,"Unable to open or read \"%s\" option file. status = %s\n", o->options, u_errorName(status));
902 return -1;
903 }
904 #endif
905 if(o->verbose) {
906 fprintf(stdout, "# pkgDataFlags=\n");
907 for(int32_t i=0;i<PKGDATA_FLAGS_SIZE;i++) {
908 fprintf(stdout, " [%d] %s: %s\n", i, FLAG_NAMES[i], pkgDataFlags[i]);
909 }
910 fprintf(stdout, "\n");
911 }
912 #if !defined(WINDOWS_WITH_MSVC) || defined(USING_CYGWIN)
913 } while (status == U_BUFFER_OVERFLOW_ERROR);
914 #endif
915
916 return result;
917 }
918
919
920 /*
921 * Given the base libName and version numbers, generate the library file names and store it in libFileNames.
922 * Depending on the configuration, the library name may either end with version number or shared object suffix.
923 */
924 static void createFileNames(UPKGOptions *o, const char mode, const char *version_major, const char *version, const char *libName, UBool reverseExt, UBool noVersion) {
925 const char* FILE_EXTENSION_SEP = uprv_strlen(pkgDataFlags[SO_EXT]) == 0 ? "" : ".";
926 const char* FILE_SUFFIX = pkgDataFlags[LIB_EXT_ORDER][0] == '.' ? "." : "";
927 #if defined(__GNUC__) && !defined(__clang__)
928 _Pragma("GCC diagnostic push")
929 _Pragma("GCC diagnostic ignored \"-Wformat-truncation\"")
930 #endif
931
932 #if U_PLATFORM == U_PF_MINGW
933 /* MinGW does not need the library prefix when building in dll mode. */
934 if (IN_DLL_MODE(mode)) {
935 snprintf(libFileNames[LIB_FILE], sizeof(libFileNames[LIB_FILE]), "%s", libName);
936 } else {
937 snprintf(libFileNames[LIB_FILE], sizeof(libFileNames[LIB_FILE]), "%s%s%s",
938 (strstr(libName, "icudt") ? "lib" : ""),
939 pkgDataFlags[LIBPREFIX],
940 libName);
941 }
942 #else
943 snprintf(libFileNames[LIB_FILE], sizeof(libFileNames[LIB_FILE]), "%s%s",
944 pkgDataFlags[LIBPREFIX],
945 libName);
946 #endif
947
948 if(o->verbose) {
949 fprintf(stdout, "# libFileName[LIB_FILE] = %s\n", libFileNames[LIB_FILE]);
950 }
951
952 #if U_PLATFORM == U_PF_MINGW
953 // Name the import library lib*.dll.a
954 snprintf(libFileNames[LIB_FILE_MINGW], sizeof(libFileNames[LIB_FILE_MINGW]), "lib%s.dll.a", libName);
955 #elif U_PLATFORM == U_PF_CYGWIN
956 snprintf(libFileNames[LIB_FILE_CYGWIN], sizeof(libFileNames[LIB_FILE_CYGWIN]), "cyg%s%s%s",
957 libName,
958 FILE_EXTENSION_SEP,
959 pkgDataFlags[SO_EXT]);
960 snprintf(libFileNames[LIB_FILE_CYGWIN_VERSION], sizeof(libFileNames[LIB_FILE_CYGWIN_VERSION]), "cyg%s%s%s%s",
961 libName,
962 version_major,
963 FILE_EXTENSION_SEP,
964 pkgDataFlags[SO_EXT]);
965
966 uprv_strcat(pkgDataFlags[SO_EXT], ".");
967 uprv_strcat(pkgDataFlags[SO_EXT], pkgDataFlags[A_EXT]);
968 #elif U_PLATFORM == U_PF_OS400 || defined(_AIX)
969 snprintf(libFileNames[LIB_FILE_VERSION_TMP], sizeof(libFileNames[LIB_FILE_VERSION_TMP]), "%s%s%s",
970 libFileNames[LIB_FILE],
971 FILE_EXTENSION_SEP,
972 pkgDataFlags[SOBJ_EXT]);
973 #elif U_PLATFORM == U_PF_OS390
974 snprintf(libFileNames[LIB_FILE_VERSION_TMP], sizeof(libFileNames[LIB_FILE_VERSION_TMP]), "%s%s%s%s%s",
975 libFileNames[LIB_FILE],
976 pkgDataFlags[LIB_EXT_ORDER][0] == '.' ? "." : "",
977 reverseExt ? version : pkgDataFlags[SOBJ_EXT],
978 FILE_EXTENSION_SEP,
979 reverseExt ? pkgDataFlags[SOBJ_EXT] : version);
980
981 snprintf(libFileNames[LIB_FILE_OS390BATCH_VERSION], sizeof(libFileNames[LIB_FILE_OS390BATCH_VERSION]), "%s%s.x",
982 libFileNames[LIB_FILE],
983 version);
984 snprintf(libFileNames[LIB_FILE_OS390BATCH_MAJOR], sizeof(libFileNames[LIB_FILE_OS390BATCH_MAJOR]), "%s%s.x",
985 libFileNames[LIB_FILE],
986 version_major);
987 #else
988 if (noVersion && !reverseExt) {
989 snprintf(libFileNames[LIB_FILE_VERSION_TMP], sizeof(libFileNames[LIB_FILE_VERSION_TMP]), "%s%s%s",
990 libFileNames[LIB_FILE],
991 FILE_SUFFIX,
992 pkgDataFlags[SOBJ_EXT]);
993 } else {
994 snprintf(libFileNames[LIB_FILE_VERSION_TMP], sizeof(libFileNames[LIB_FILE_VERSION_TMP]), "%s%s%s%s%s",
995 libFileNames[LIB_FILE],
996 FILE_SUFFIX,
997 reverseExt ? version : pkgDataFlags[SOBJ_EXT],
998 FILE_EXTENSION_SEP,
999 reverseExt ? pkgDataFlags[SOBJ_EXT] : version);
1000 }
1001 #endif
1002 if (noVersion && !reverseExt) {
1003 snprintf(libFileNames[LIB_FILE_VERSION_MAJOR], sizeof(libFileNames[LIB_FILE_VERSION_TMP]), "%s%s%s",
1004 libFileNames[LIB_FILE],
1005 FILE_SUFFIX,
1006 pkgDataFlags[SO_EXT]);
1007
1008 snprintf(libFileNames[LIB_FILE_VERSION], sizeof(libFileNames[LIB_FILE_VERSION]), "%s%s%s",
1009 libFileNames[LIB_FILE],
1010 FILE_SUFFIX,
1011 pkgDataFlags[SO_EXT]);
1012 } else {
1013 snprintf(libFileNames[LIB_FILE_VERSION_MAJOR], sizeof(libFileNames[LIB_FILE_VERSION_MAJOR]), "%s%s%s%s%s",
1014 libFileNames[LIB_FILE],
1015 FILE_SUFFIX,
1016 reverseExt ? version_major : pkgDataFlags[SO_EXT],
1017 FILE_EXTENSION_SEP,
1018 reverseExt ? pkgDataFlags[SO_EXT] : version_major);
1019
1020 snprintf(libFileNames[LIB_FILE_VERSION], sizeof(libFileNames[LIB_FILE_VERSION]), "%s%s%s%s%s",
1021 libFileNames[LIB_FILE],
1022 FILE_SUFFIX,
1023 reverseExt ? version : pkgDataFlags[SO_EXT],
1024 FILE_EXTENSION_SEP,
1025 reverseExt ? pkgDataFlags[SO_EXT] : version);
1026 }
1027
1028 if(o->verbose) {
1029 fprintf(stdout, "# libFileName[LIB_FILE_VERSION] = %s\n", libFileNames[LIB_FILE_VERSION]);
1030 }
1031
1032 #if U_PF_MINGW <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
1033 /* Cygwin and MinGW only deals with the version major number. */
1034 uprv_strcpy(libFileNames[LIB_FILE_VERSION_TMP], libFileNames[LIB_FILE_VERSION_MAJOR]);
1035 #endif
1036
1037 if(IN_STATIC_MODE(mode)) {
1038 snprintf(libFileNames[LIB_FILE_VERSION], sizeof(libFileNames[LIB_FILE_VERSION]), "%s.%s", libFileNames[LIB_FILE], pkgDataFlags[A_EXT]);
1039 libFileNames[LIB_FILE_VERSION_MAJOR][0]=0;
1040 if(o->verbose) {
1041 fprintf(stdout, "# libFileName[LIB_FILE_VERSION] = %s (static)\n", libFileNames[LIB_FILE_VERSION]);
1042 }
1043 }
1044 #if defined(__GNUC__) && !defined(__clang__)
1045 _Pragma("GCC diagnostic pop")
1046 #endif
1047
1048 }
1049
1050 /* Create the symbolic links for the final library file. */
1051 static int32_t pkg_createSymLinks(const char *targetDir, UBool specialHandling) {
1052 int32_t result = 0;
1053 char cmd[LARGE_BUFFER_MAX_SIZE];
1054 char name1[SMALL_BUFFER_MAX_SIZE]; /* symlink file name */
1055 char name2[SMALL_BUFFER_MAX_SIZE]; /* file name to symlink */
1056 const char* FILE_EXTENSION_SEP = uprv_strlen(pkgDataFlags[SO_EXT]) == 0 ? "" : ".";
1057
1058 #if U_PLATFORM != U_PF_CYGWIN
1059 /* No symbolic link to make. */
1060 if (uprv_strlen(libFileNames[LIB_FILE_VERSION]) == 0 || uprv_strlen(libFileNames[LIB_FILE_VERSION_MAJOR]) == 0 ||
1061 uprv_strcmp(libFileNames[LIB_FILE_VERSION], libFileNames[LIB_FILE_VERSION_MAJOR]) == 0) {
1062 return result;
1063 }
1064
1065 snprintf(cmd, sizeof(cmd), "cd %s && %s %s && %s %s %s",
1066 targetDir,
1067 RM_CMD,
1068 libFileNames[LIB_FILE_VERSION_MAJOR],
1069 LN_CMD,
1070 libFileNames[LIB_FILE_VERSION],
1071 libFileNames[LIB_FILE_VERSION_MAJOR]);
1072 result = runCommand(cmd);
1073 if (result != 0) {
1074 fprintf(stderr, "Error creating symbolic links. Failed command: %s\n", cmd);
1075 return result;
1076 }
1077 #endif
1078
1079 if (specialHandling) {
1080 #if U_PLATFORM == U_PF_CYGWIN
1081 snprintf(name1, sizeof(name1), "%s", libFileNames[LIB_FILE_CYGWIN]);
1082 snprintf(name2, sizeof(name2), "%s", libFileNames[LIB_FILE_CYGWIN_VERSION]);
1083 #elif U_PLATFORM == U_PF_OS390
1084 /* Create the symbolic links for the import data */
1085 /* Use the cmd buffer to store path to import data file to check its existence */
1086 snprintf(cmd, sizeof(cmd), "%s/%s", targetDir, libFileNames[LIB_FILE_OS390BATCH_VERSION]);
1087 if (T_FileStream_file_exists(cmd)) {
1088 snprintf(cmd, sizeof(cmd), "cd %s && %s %s && %s %s %s",
1089 targetDir,
1090 RM_CMD,
1091 libFileNames[LIB_FILE_OS390BATCH_MAJOR],
1092 LN_CMD,
1093 libFileNames[LIB_FILE_OS390BATCH_VERSION],
1094 libFileNames[LIB_FILE_OS390BATCH_MAJOR]);
1095 result = runCommand(cmd);
1096 if (result != 0) {
1097 fprintf(stderr, "Error creating symbolic links. Failed command: %s\n", cmd);
1098 return result;
1099 }
1100
1101 snprintf(cmd, sizeof(cmd), "cd %s && %s %s.x && %s %s %s.x",
1102 targetDir,
1103 RM_CMD,
1104 libFileNames[LIB_FILE],
1105 LN_CMD,
1106 libFileNames[LIB_FILE_OS390BATCH_VERSION],
1107 libFileNames[LIB_FILE]);
1108 result = runCommand(cmd);
1109 if (result != 0) {
1110 fprintf(stderr, "Error creating symbolic links. Failed command: %s\n", cmd);
1111 return result;
1112 }
1113 }
1114
1115 /* Needs to be set here because special handling skips it */
1116 snprintf(name1, sizeof(name1), "%s%s%s", libFileNames[LIB_FILE], FILE_EXTENSION_SEP, pkgDataFlags[SO_EXT]);
1117 snprintf(name2, sizeof(name2), "%s", libFileNames[LIB_FILE_VERSION]);
1118 #else
1119 goto normal_symlink_mode;
1120 #endif
1121 } else {
1122 #if U_PLATFORM != U_PF_CYGWIN
1123 normal_symlink_mode:
1124 #endif
1125 snprintf(name1, sizeof(name1), "%s%s%s", libFileNames[LIB_FILE], FILE_EXTENSION_SEP, pkgDataFlags[SO_EXT]);
1126 snprintf(name2, sizeof(name2), "%s", libFileNames[LIB_FILE_VERSION]);
1127 }
1128
1129 snprintf(cmd, sizeof(cmd), "cd %s && %s %s && %s %s %s",
1130 targetDir,
1131 RM_CMD,
1132 name1,
1133 LN_CMD,
1134 name2,
1135 name1);
1136
1137 result = runCommand(cmd);
1138
1139 return result;
1140 }
1141
1142 static int32_t pkg_installLibrary(const char *installDir, const char *targetDir, UBool noVersion) {
1143 int32_t result = 0;
1144 char cmd[LARGE_BUFFER_MAX_SIZE];
1145
1146 auto ret = snprintf(cmd,
1147 sizeof(cmd),
1148 "cd %s && %s %s %s%s%s",
1149 targetDir,
1150 pkgDataFlags[INSTALL_CMD],
1151 libFileNames[LIB_FILE_VERSION],
1152 installDir, PKGDATA_FILE_SEP_STRING, libFileNames[LIB_FILE_VERSION]);
1153 (void)ret;
1154 U_ASSERT(0 <= ret && ret < SMALL_BUFFER_MAX_SIZE);
1155
1156 result = runCommand(cmd);
1157
1158 if (result != 0) {
1159 fprintf(stderr, "Error installing library. Failed command: %s\n", cmd);
1160 return result;
1161 }
1162
1163 #ifdef CYGWINMSVC
1164 snprintf(cmd, sizeof(cmd), "cd %s && %s %s.lib %s",
1165 targetDir,
1166 pkgDataFlags[INSTALL_CMD],
1167 libFileNames[LIB_FILE],
1168 installDir
1169 );
1170 result = runCommand(cmd);
1171
1172 if (result != 0) {
1173 fprintf(stderr, "Error installing library. Failed command: %s\n", cmd);
1174 return result;
1175 }
1176 #elif U_PLATFORM == U_PF_CYGWIN
1177 snprintf(cmd, sizeof(cmd), "cd %s && %s %s %s",
1178 targetDir,
1179 pkgDataFlags[INSTALL_CMD],
1180 libFileNames[LIB_FILE_CYGWIN_VERSION],
1181 installDir
1182 );
1183 result = runCommand(cmd);
1184
1185 if (result != 0) {
1186 fprintf(stderr, "Error installing library. Failed command: %s\n", cmd);
1187 return result;
1188 }
1189
1190 #elif U_PLATFORM == U_PF_OS390
1191 if (T_FileStream_file_exists(libFileNames[LIB_FILE_OS390BATCH_VERSION])) {
1192 snprintf(cmd, sizeof(cmd), "%s %s %s",
1193 pkgDataFlags[INSTALL_CMD],
1194 libFileNames[LIB_FILE_OS390BATCH_VERSION],
1195 installDir
1196 );
1197 result = runCommand(cmd);
1198
1199 if (result != 0) {
1200 fprintf(stderr, "Error installing library. Failed command: %s\n", cmd);
1201 return result;
1202 }
1203 }
1204 #endif
1205
1206 if (noVersion) {
1207 return result;
1208 } else {
1209 return pkg_createSymLinks(installDir, true);
1210 }
1211 }
1212
1213 static int32_t pkg_installCommonMode(const char *installDir, const char *fileName) {
1214 int32_t result = 0;
1215 char cmd[LARGE_BUFFER_MAX_SIZE] = "";
1216
1217 if (!T_FileStream_file_exists(installDir)) {
1218 UErrorCode status = U_ZERO_ERROR;
1219
1220 uprv_mkdir(installDir, &status);
1221 if (U_FAILURE(status)) {
1222 fprintf(stderr, "Error creating installation directory: %s\n", installDir);
1223 return -1;
1224 }
1225 }
1226 #ifndef U_WINDOWS_WITH_MSVC
1227 snprintf(cmd, sizeof(cmd), "%s %s %s", pkgDataFlags[INSTALL_CMD], fileName, installDir);
1228 #else
1229 snprintf(cmd, sizeof(cmd), "%s %s %s %s", WIN_INSTALL_CMD, fileName, installDir, WIN_INSTALL_CMD_FLAGS);
1230 #endif
1231
1232 result = runCommand(cmd);
1233 if (result != 0) {
1234 fprintf(stderr, "Failed to install data file with command: %s\n", cmd);
1235 }
1236
1237 return result;
1238 }
1239
1240 #ifdef U_WINDOWS_MSVC
1241 /* Copy commands for installing the raw data files on Windows. */
1242 #define WIN_INSTALL_CMD "xcopy"
1243 #define WIN_INSTALL_CMD_FLAGS "/E /Y /K"
1244 #endif
1245 static int32_t pkg_installFileMode(const char *installDir, const char *srcDir, const char *fileListName) {
1246 int32_t result = 0;
1247 char cmd[LARGE_BUFFER_MAX_SIZE] = "";
1248
1249 if (!T_FileStream_file_exists(installDir)) {
1250 UErrorCode status = U_ZERO_ERROR;
1251
1252 uprv_mkdir(installDir, &status);
1253 if (U_FAILURE(status)) {
1254 fprintf(stderr, "Error creating installation directory: %s\n", installDir);
1255 return -1;
1256 }
1257 }
1258 #ifndef U_WINDOWS_WITH_MSVC
1259 char buffer[SMALL_BUFFER_MAX_SIZE] = "";
1260 int32_t bufferLength = 0;
1261
1262 FileStream *f = T_FileStream_open(fileListName, "r");
1263 if (f != nullptr) {
1264 for(;;) {
1265 if (T_FileStream_readLine(f, buffer, SMALL_BUFFER_MAX_SIZE) != nullptr) {
1266 bufferLength = static_cast<int32_t>(uprv_strlen(buffer));
1267 /* Remove new line character. */
1268 if (bufferLength > 0) {
1269 buffer[bufferLength-1] = 0;
1270 }
1271
1272 auto ret = snprintf(cmd,
1273 sizeof(cmd),
1274 "%s %s%s%s %s%s%s",
1275 pkgDataFlags[INSTALL_CMD],
1276 srcDir, PKGDATA_FILE_SEP_STRING, buffer,
1277 installDir, PKGDATA_FILE_SEP_STRING, buffer);
1278 (void)ret;
1279 U_ASSERT(0 <= ret && ret < SMALL_BUFFER_MAX_SIZE);
1280
1281 result = runCommand(cmd);
1282 if (result != 0) {
1283 fprintf(stderr, "Failed to install data file with command: %s\n", cmd);
1284 break;
1285 }
1286 } else {
1287 if (!T_FileStream_eof(f)) {
1288 fprintf(stderr, "Failed to read line from file: %s\n", fileListName);
1289 result = -1;
1290 }
1291 break;
1292 }
1293 }
1294 T_FileStream_close(f);
1295 } else {
1296 result = -1;
1297 fprintf(stderr, "Unable to open list file: %s\n", fileListName);
1298 }
1299 #else
1300 snprintf(cmd, sizeof(cmd), "%s %s %s %s", WIN_INSTALL_CMD, srcDir, installDir, WIN_INSTALL_CMD_FLAGS);
1301 result = runCommand(cmd);
1302 if (result != 0) {
1303 fprintf(stderr, "Failed to install data file with command: %s\n", cmd);
1304 }
1305 #endif
1306
1307 return result;
1308 }
1309
1310 /* Archiving of the library file may be needed depending on the platform and options given.
1311 * If archiving is not needed, copy over the library file name.
1312 */
1313 static int32_t pkg_archiveLibrary(const char *targetDir, const char *version, UBool reverseExt) {
1314 int32_t result = 0;
1315 char cmd[LARGE_BUFFER_MAX_SIZE];
1316
1317 /* If the shared object suffix and the final object suffix is different and the final object suffix and the
1318 * archive file suffix is the same, then the final library needs to be archived.
1319 */
1320 if (uprv_strcmp(pkgDataFlags[SOBJ_EXT], pkgDataFlags[SO_EXT]) != 0 && uprv_strcmp(pkgDataFlags[A_EXT], pkgDataFlags[SO_EXT]) == 0) {
1321 #if defined(__GNUC__) && !defined(__clang__)
1322 _Pragma("GCC diagnostic push")
1323 _Pragma("GCC diagnostic ignored \"-Wformat-truncation\"")
1324 #endif
1325
1326 snprintf(libFileNames[LIB_FILE_VERSION], sizeof(libFileNames[LIB_FILE_VERSION]), "%s%s%s.%s",
1327 libFileNames[LIB_FILE],
1328 pkgDataFlags[LIB_EXT_ORDER][0] == '.' ? "." : "",
1329 reverseExt ? version : pkgDataFlags[SO_EXT],
1330 reverseExt ? pkgDataFlags[SO_EXT] : version);
1331 #if defined(__GNUC__) && !defined(__clang__)
1332 _Pragma("GCC diagnostic pop")
1333 #endif
1334
1335 snprintf(cmd, sizeof(cmd), "%s %s %s%s %s%s",
1336 pkgDataFlags[AR],
1337 pkgDataFlags[ARFLAGS],
1338 targetDir,
1339 libFileNames[LIB_FILE_VERSION],
1340 targetDir,
1341 libFileNames[LIB_FILE_VERSION_TMP]);
1342
1343 result = runCommand(cmd);
1344 if (result != 0) {
1345 fprintf(stderr, "Error creating archive library. Failed command: %s\n", cmd);
1346 return result;
1347 }
1348
1349 snprintf(cmd, sizeof(cmd), "%s %s%s",
1350 pkgDataFlags[RANLIB],
1351 targetDir,
1352 libFileNames[LIB_FILE_VERSION]);
1353
1354 result = runCommand(cmd);
1355 if (result != 0) {
1356 fprintf(stderr, "Error creating archive library. Failed command: %s\n", cmd);
1357 return result;
1358 }
1359
1360 /* Remove unneeded library file. */
1361 snprintf(cmd, sizeof(cmd), "%s %s%s",
1362 RM_CMD,
1363 targetDir,
1364 libFileNames[LIB_FILE_VERSION_TMP]);
1365
1366 result = runCommand(cmd);
1367 if (result != 0) {
1368 fprintf(stderr, "Error creating archive library. Failed command: %s\n", cmd);
1369 return result;
1370 }
1371
1372 } else {
1373 uprv_strcpy(libFileNames[LIB_FILE_VERSION], libFileNames[LIB_FILE_VERSION_TMP]);
1374 }
1375
1376 return result;
1377 }
1378
1379 /*
1380 * Using the compiler information from the configuration file set by -O option, generate the library file.
1381 * command may be given to allow for a larger buffer for cmd.
1382 */
1383 static int32_t pkg_generateLibraryFile(const char *targetDir, const char mode, const char *objectFile, char *command, UBool specialHandling) {
1384 int32_t result = 0;
1385 char *cmd = nullptr;
1386 UBool freeCmd = false;
1387 int32_t length = 0;
1388
1389 (void)specialHandling; // Suppress unused variable compiler warnings on platforms where all usage
1390 // of this parameter is #ifdefed out.
1391
1392 /* This is necessary because if packaging is done without assembly code, objectFile might be extremely large
1393 * containing many object files and so the calling function should supply a command buffer that is large
1394 * enough to handle this. Otherwise, use the default size.
1395 */
1396 if (command != nullptr) {
1397 cmd = command;
1398 }
1399
1400 if (IN_STATIC_MODE(mode)) {
1401 if (cmd == nullptr) {
1402 length = static_cast<int32_t>(uprv_strlen(pkgDataFlags[AR]) + uprv_strlen(pkgDataFlags[ARFLAGS]) + uprv_strlen(targetDir) +
1403 uprv_strlen(libFileNames[LIB_FILE_VERSION]) + uprv_strlen(objectFile) + uprv_strlen(pkgDataFlags[RANLIB]) + BUFFER_PADDING_SIZE);
1404 if ((cmd = static_cast<char*>(uprv_malloc(sizeof(char) * length))) == nullptr) {
1405 fprintf(stderr, "Unable to allocate memory for command.\n");
1406 return -1;
1407 }
1408 freeCmd = true;
1409 }
1410 sprintf(cmd, "%s %s %s%s %s",
1411 pkgDataFlags[AR],
1412 pkgDataFlags[ARFLAGS],
1413 targetDir,
1414 libFileNames[LIB_FILE_VERSION],
1415 objectFile);
1416
1417 result = runCommand(cmd);
1418 if (result == 0) {
1419 sprintf(cmd, "%s %s%s",
1420 pkgDataFlags[RANLIB],
1421 targetDir,
1422 libFileNames[LIB_FILE_VERSION]);
1423
1424 result = runCommand(cmd);
1425 }
1426 } else /* if (IN_DLL_MODE(mode)) */ {
1427 if (cmd == nullptr) {
1428 length = static_cast<int32_t>(uprv_strlen(pkgDataFlags[GENLIB]) + uprv_strlen(pkgDataFlags[LDICUDTFLAGS]) +
1429 ((uprv_strlen(targetDir) + uprv_strlen(libFileNames[LIB_FILE_VERSION_TMP])) * 2) +
1430 uprv_strlen(objectFile) + uprv_strlen(pkgDataFlags[LD_SONAME]) +
1431 uprv_strlen(pkgDataFlags[LD_SONAME][0] == 0 ? "" : libFileNames[LIB_FILE_VERSION_MAJOR]) +
1432 uprv_strlen(pkgDataFlags[RPATH_FLAGS]) + uprv_strlen(pkgDataFlags[BIR_FLAGS]) + BUFFER_PADDING_SIZE);
1433 #if U_PLATFORM == U_PF_CYGWIN
1434 length += static_cast<int32_t>(uprv_strlen(targetDir) + uprv_strlen(libFileNames[LIB_FILE_CYGWIN_VERSION]));
1435 #elif U_PLATFORM == U_PF_MINGW
1436 length += static_cast<int32_t>(uprv_strlen(targetDir) + uprv_strlen(libFileNames[LIB_FILE_MINGW]));
1437 #endif
1438 if ((cmd = static_cast<char*>(uprv_malloc(sizeof(char) * length))) == nullptr) {
1439 fprintf(stderr, "Unable to allocate memory for command.\n");
1440 return -1;
1441 }
1442 freeCmd = true;
1443 }
1444 #if U_PLATFORM == U_PF_MINGW
1445 sprintf(cmd, "%s%s%s %s -o %s%s %s %s%s %s %s",
1446 pkgDataFlags[GENLIB],
1447 targetDir,
1448 libFileNames[LIB_FILE_MINGW],
1449 pkgDataFlags[LDICUDTFLAGS],
1450 targetDir,
1451 libFileNames[LIB_FILE_VERSION_TMP],
1452 #elif U_PLATFORM == U_PF_CYGWIN
1453 sprintf(cmd, "%s%s%s %s -o %s%s %s %s%s %s %s",
1454 pkgDataFlags[GENLIB],
1455 targetDir,
1456 libFileNames[LIB_FILE_VERSION_TMP],
1457 pkgDataFlags[LDICUDTFLAGS],
1458 targetDir,
1459 libFileNames[LIB_FILE_CYGWIN_VERSION],
1460 #elif U_PLATFORM == U_PF_AIX
1461 sprintf(cmd, "%s %s%s;%s %s -o %s%s %s %s%s %s %s",
1462 RM_CMD,
1463 targetDir,
1464 libFileNames[LIB_FILE_VERSION_TMP],
1465 pkgDataFlags[GENLIB],
1466 pkgDataFlags[LDICUDTFLAGS],
1467 targetDir,
1468 libFileNames[LIB_FILE_VERSION_TMP],
1469 #else
1470 sprintf(cmd, "%s %s -o %s%s %s %s%s %s %s",
1471 pkgDataFlags[GENLIB],
1472 pkgDataFlags[LDICUDTFLAGS],
1473 targetDir,
1474 libFileNames[LIB_FILE_VERSION_TMP],
1475 #endif
1476 objectFile,
1477 pkgDataFlags[LD_SONAME],
1478 pkgDataFlags[LD_SONAME][0] == 0 ? "" : libFileNames[LIB_FILE_VERSION_MAJOR],
1479 pkgDataFlags[RPATH_FLAGS],
1480 pkgDataFlags[BIR_FLAGS]);
1481
1482 /* Generate the library file. */
1483 result = runCommand(cmd);
1484
1485 #if U_PLATFORM == U_PF_OS390
1486 sprintf(cmd, "%s %s -o %s%s %s %s%s %s %s", pkgDataFlags[GENLIB], pkgDataFlags[LDICUDTFLAGS],
1487 targetDir, BATCH_STUB_TARGET, objectFile, pkgDataFlags[LD_SONAME],
1488 pkgDataFlags[LD_SONAME][0] == 0 ? "" : libFileNames[LIB_FILE_VERSION_MAJOR],
1489 pkgDataFlags[RPATH_FLAGS], pkgDataFlags[BIR_FLAGS]);
1490
1491 result = runCommand(cmd);
1492 #endif
1493 }
1494
1495 if (result != 0) {
1496 fprintf(stderr, "Error generating library file. Failed command: %s\n", cmd);
1497 }
1498
1499 if (freeCmd) {
1500 uprv_free(cmd);
1501 }
1502
1503 return result;
1504 }
1505
1506 static int32_t pkg_createWithAssemblyCode(const char *targetDir, const char mode, const char *gencFilePath) {
1507 char tempObjectFile[SMALL_BUFFER_MAX_SIZE] = "";
1508 int32_t result = 0;
1509 int32_t length = 0;
1510
1511 /* Remove the ending .s and replace it with .o for the new object file. */
1512 uprv_strcpy(tempObjectFile, gencFilePath);
1513 tempObjectFile[uprv_strlen(tempObjectFile)-1] = 'o';
1514
1515 length = static_cast<int32_t>(uprv_strlen(pkgDataFlags[COMPILER]) + uprv_strlen(pkgDataFlags[LIBFLAGS])
1516 + uprv_strlen(tempObjectFile) + uprv_strlen(gencFilePath) + BUFFER_PADDING_SIZE);
1517
1518 LocalMemory<char> cmd(static_cast<char*>(uprv_malloc(sizeof(char) * length)));
1519 if (cmd.isNull()) {
1520 return -1;
1521 }
1522
1523 /* Generate the object file. */
1524 snprintf(cmd.getAlias(), length, "%s %s -o %s %s",
1525 pkgDataFlags[COMPILER],
1526 pkgDataFlags[LIBFLAGS],
1527 tempObjectFile,
1528 gencFilePath);
1529
1530 result = runCommand(cmd.getAlias());
1531
1532 if (result != 0) {
1533 fprintf(stderr, "Error creating with assembly code. Failed command: %s\n", cmd.getAlias());
1534 return result;
1535 }
1536
1537 return pkg_generateLibraryFile(targetDir, mode, tempObjectFile);
1538 }
1539
1540 #ifdef BUILD_DATA_WITHOUT_ASSEMBLY
1541 /*
1542 * Generation of the data library without assembly code needs to compile each data file
1543 * individually and then link it all together.
1544 * Note: Any update to the directory structure of the data needs to be reflected here.
1545 */
1546 enum {
1547 DATA_PREFIX_BRKITR,
1548 DATA_PREFIX_COLL,
1549 DATA_PREFIX_CURR,
1550 DATA_PREFIX_LANG,
1551 DATA_PREFIX_RBNF,
1552 DATA_PREFIX_REGION,
1553 DATA_PREFIX_TRANSLIT,
1554 DATA_PREFIX_ZONE,
1555 DATA_PREFIX_UNIT,
1556 DATA_PREFIX_LENGTH
1557 };
1558
1559 const static char DATA_PREFIX[DATA_PREFIX_LENGTH][10] = {
1560 "brkitr",
1561 "coll",
1562 "curr",
1563 "lang",
1564 "rbnf",
1565 "region",
1566 "translit",
1567 "zone",
1568 "unit"
1569 };
1570
1571 static int32_t pkg_createWithoutAssemblyCode(UPKGOptions *o, const char *targetDir, const char mode) {
1572 int32_t result = 0;
1573 CharList *list = o->filePaths;
1574 CharList *listNames = o->files;
1575 int32_t listSize = pkg_countCharList(list);
1576 char *buffer;
1577 char *cmd;
1578 char gencmnFile[SMALL_BUFFER_MAX_SIZE] = "";
1579 char tempObjectFile[SMALL_BUFFER_MAX_SIZE] = "";
1580 #ifdef USE_SINGLE_CCODE_FILE
1581 char icudtAll[SMALL_BUFFER_MAX_SIZE] = "";
1582 FileStream *icudtAllFile = nullptr;
1583
1584 snprintf(icudtAll, sizeof(icudtAll), "%s%s%sall.c",
1585 o->tmpDir,
1586 PKGDATA_FILE_SEP_STRING,
1587 libFileNames[LIB_FILE]);
1588 /* Remove previous icudtall.c file. */
1589 if (T_FileStream_file_exists(icudtAll) && (result = remove(icudtAll)) != 0) {
1590 fprintf(stderr, "Unable to remove old icudtall file: %s\n", icudtAll);
1591 return result;
1592 }
1593
1594 if((icudtAllFile = T_FileStream_open(icudtAll, "w"))==nullptr) {
1595 fprintf(stderr, "Unable to write to icudtall file: %s\n", icudtAll);
1596 return result;
1597 }
1598 #endif
1599
1600 if (list == nullptr || listNames == nullptr) {
1601 /* list and listNames should never be nullptr since we are looping through the CharList with
1602 * the given size.
1603 */
1604 return -1;
1605 }
1606
1607 if ((cmd = static_cast<char*>(uprv_malloc((listSize + 2) * SMALL_BUFFER_MAX_SIZE))) == nullptr) {
1608 fprintf(stderr, "Unable to allocate memory for cmd.\n");
1609 return -1;
1610 } else if ((buffer = static_cast<char*>(uprv_malloc((listSize + 1) * SMALL_BUFFER_MAX_SIZE))) == nullptr) {
1611 fprintf(stderr, "Unable to allocate memory for buffer.\n");
1612 uprv_free(cmd);
1613 return -1;
1614 }
1615
1616 for (int32_t i = 0; i < (listSize + 1); i++) {
1617 const char *file ;
1618 const char *name;
1619
1620 if (i == 0) {
1621 /* The first iteration calls the gencmn function and initializes the buffer. */
1622 createCommonDataFile(o->tmpDir, o->shortName, o->entryName, nullptr, o->srcDir, o->comment, o->fileListFiles->str, 0, true, o->verbose, gencmnFile);
1623 buffer[0] = 0;
1624 #ifdef USE_SINGLE_CCODE_FILE
1625 uprv_strcpy(tempObjectFile, gencmnFile);
1626 tempObjectFile[uprv_strlen(tempObjectFile) - 1] = 'o';
1627
1628 sprintf(cmd, "%s %s -o %s %s",
1629 pkgDataFlags[COMPILER],
1630 pkgDataFlags[LIBFLAGS],
1631 tempObjectFile,
1632 gencmnFile);
1633
1634 result = runCommand(cmd);
1635 if (result != 0) {
1636 break;
1637 }
1638
1639 sprintf(buffer, "%s",tempObjectFile);
1640 #endif
1641 } else {
1642 char newName[SMALL_BUFFER_MAX_SIZE];
1643 char dataName[SMALL_BUFFER_MAX_SIZE];
1644 char dataDirName[SMALL_BUFFER_MAX_SIZE];
1645 const char *pSubstring;
1646 file = list->str;
1647 name = listNames->str;
1648
1649 newName[0] = dataName[0] = 0;
1650 for (int32_t n = 0; n < DATA_PREFIX_LENGTH; n++) {
1651 dataDirName[0] = 0;
1652 sprintf(dataDirName, "%s%s", DATA_PREFIX[n], PKGDATA_FILE_SEP_STRING);
1653 /* If the name contains a prefix (indicating directory), alter the new name accordingly. */
1654 pSubstring = uprv_strstr(name, dataDirName);
1655 if (pSubstring != nullptr) {
1656 char newNameTmp[SMALL_BUFFER_MAX_SIZE] = "";
1657 const char *p = name + uprv_strlen(dataDirName);
1658 for (int32_t i = 0;;i++) {
1659 if (p[i] == '.') {
1660 newNameTmp[i] = '_';
1661 continue;
1662 }
1663 newNameTmp[i] = p[i];
1664 if (p[i] == 0) {
1665 break;
1666 }
1667 }
1668 auto ret = snprintf(newName,
1669 sizeof(newName),
1670 "%s_%s",
1671 DATA_PREFIX[n],
1672 newNameTmp);
1673 (void)ret;
1674 U_ASSERT(0 <= ret && ret < SMALL_BUFFER_MAX_SIZE);
1675 ret = snprintf(dataName,
1676 sizeof(dataName),
1677 "%s_%s",
1678 o->shortName,
1679 DATA_PREFIX[n]);
1680 (void)ret;
1681 U_ASSERT(0 <= ret && ret < SMALL_BUFFER_MAX_SIZE);
1682 }
1683 if (newName[0] != 0) {
1684 break;
1685 }
1686 }
1687
1688 if(o->verbose) {
1689 printf("# Generating %s \n", gencmnFile);
1690 }
1691
1692 writeCCode(
1693 file,
1694 o->tmpDir,
1695 nullptr,
1696 dataName[0] != 0 ? dataName : o->shortName,
1697 newName[0] != 0 ? newName : nullptr,
1698 gencmnFile,
1699 sizeof(gencmnFile));
1700
1701 #ifdef USE_SINGLE_CCODE_FILE
1702 sprintf(cmd, "#include \"%s\"\n", gencmnFile);
1703 T_FileStream_writeLine(icudtAllFile, cmd);
1704 /* don't delete the file */
1705 #endif
1706 }
1707
1708 #ifndef USE_SINGLE_CCODE_FILE
1709 uprv_strcpy(tempObjectFile, gencmnFile);
1710 tempObjectFile[uprv_strlen(tempObjectFile) - 1] = 'o';
1711
1712 sprintf(cmd, "%s %s -o %s %s",
1713 pkgDataFlags[COMPILER],
1714 pkgDataFlags[LIBFLAGS],
1715 tempObjectFile,
1716 gencmnFile);
1717 result = runCommand(cmd);
1718 if (result != 0) {
1719 fprintf(stderr, "Error creating library without assembly code. Failed command: %s\n", cmd);
1720 break;
1721 }
1722
1723 uprv_strcat(buffer, " ");
1724 uprv_strcat(buffer, tempObjectFile);
1725
1726 #endif
1727
1728 if (i > 0) {
1729 list = list->next;
1730 listNames = listNames->next;
1731 }
1732 }
1733
1734 #ifdef USE_SINGLE_CCODE_FILE
1735 T_FileStream_close(icudtAllFile);
1736 uprv_strcpy(tempObjectFile, icudtAll);
1737 tempObjectFile[uprv_strlen(tempObjectFile) - 1] = 'o';
1738
1739 sprintf(cmd, "%s %s -I. -o %s %s",
1740 pkgDataFlags[COMPILER],
1741 pkgDataFlags[LIBFLAGS],
1742 tempObjectFile,
1743 icudtAll);
1744
1745 result = runCommand(cmd);
1746 if (result == 0) {
1747 uprv_strcat(buffer, " ");
1748 uprv_strcat(buffer, tempObjectFile);
1749 } else {
1750 fprintf(stderr, "Error creating library without assembly code. Failed command: %s\n", cmd);
1751 }
1752 #endif
1753
1754 if (result == 0) {
1755 /* Generate the library file. */
1756 #if U_PLATFORM == U_PF_OS390
1757 result = pkg_generateLibraryFile(targetDir, mode, buffer, cmd, (o->pdsbuild && IN_DLL_MODE(mode)));
1758 #else
1759 result = pkg_generateLibraryFile(targetDir,mode, buffer, cmd);
1760 #endif
1761 }
1762
1763 uprv_free(buffer);
1764 uprv_free(cmd);
1765
1766 return result;
1767 }
1768 #endif
1769
1770 #ifdef WINDOWS_WITH_MSVC
1771 #define LINK_CMD "link.exe /nologo /release /out:"
1772 #define LINK_FLAGS "/NXCOMPAT /DYNAMICBASE /DLL /NOENTRY /MANIFEST:NO /implib:"
1773
1774 #define LINK_EXTRA_UWP_FLAGS "/APPCONTAINER "
1775 #define LINK_EXTRA_UWP_FLAGS_X86_ONLY "/SAFESEH "
1776
1777 #define LINK_EXTRA_FLAGS_MACHINE "/MACHINE:"
1778 #define LIB_CMD "LIB.exe /nologo /out:"
1779 #define LIB_FILE "icudt.lib"
1780 #define LIB_EXT UDATA_LIB_SUFFIX
1781 #define DLL_EXT UDATA_SO_SUFFIX
1782
1783 static int32_t pkg_createWindowsDLL(const char mode, const char *gencFilePath, UPKGOptions *o) {
1784 int32_t result = 0;
1785 char cmd[LARGE_BUFFER_MAX_SIZE];
1786 if (IN_STATIC_MODE(mode)) {
1787 char staticLibFilePath[SMALL_BUFFER_MAX_SIZE] = "";
1788
1789 #ifdef CYGWINMSVC
1790 snprintf(staticLibFilePath, sizeof(staticLibFilePath), "%s%s%s%s%s",
1791 o->targetDir,
1792 PKGDATA_FILE_SEP_STRING,
1793 pkgDataFlags[LIBPREFIX],
1794 o->libName,
1795 LIB_EXT);
1796 #else
1797 snprintf(staticLibFilePath, sizeof(staticLibFilePath), "%s%s%s%s%s",
1798 o->targetDir,
1799 PKGDATA_FILE_SEP_STRING,
1800 (strstr(o->libName, "icudt") ? "s" : ""),
1801 o->libName,
1802 LIB_EXT);
1803 #endif
1804
1805 snprintf(cmd, sizeof(cmd), "%s\"%s\" \"%s\"",
1806 LIB_CMD,
1807 staticLibFilePath,
1808 gencFilePath);
1809 } else if (IN_DLL_MODE(mode)) {
1810 char dllFilePath[SMALL_BUFFER_MAX_SIZE] = "";
1811 char libFilePath[SMALL_BUFFER_MAX_SIZE] = "";
1812 char resFilePath[SMALL_BUFFER_MAX_SIZE] = "";
1813 char tmpResFilePath[SMALL_BUFFER_MAX_SIZE] = "";
1814
1815 #ifdef CYGWINMSVC
1816 uprv_strcpy(dllFilePath, o->targetDir);
1817 #else
1818 uprv_strcpy(dllFilePath, o->srcDir);
1819 #endif
1820 uprv_strcat(dllFilePath, PKGDATA_FILE_SEP_STRING);
1821 uprv_strcpy(libFilePath, dllFilePath);
1822
1823 #ifdef CYGWINMSVC
1824 uprv_strcat(libFilePath, o->libName);
1825 uprv_strcat(libFilePath, ".lib");
1826
1827 uprv_strcat(dllFilePath, o->libName);
1828 uprv_strcat(dllFilePath, o->version);
1829 #else
1830 if (strstr(o->libName, "icudt")) {
1831 uprv_strcat(libFilePath, LIB_FILE);
1832 } else {
1833 uprv_strcat(libFilePath, o->libName);
1834 uprv_strcat(libFilePath, ".lib");
1835 }
1836 uprv_strcat(dllFilePath, o->entryName);
1837 #endif
1838 uprv_strcat(dllFilePath, DLL_EXT);
1839
1840 uprv_strcpy(tmpResFilePath, o->tmpDir);
1841 uprv_strcat(tmpResFilePath, PKGDATA_FILE_SEP_STRING);
1842 uprv_strcat(tmpResFilePath, ICUDATA_RES_FILE);
1843
1844 if (T_FileStream_file_exists(tmpResFilePath)) {
1845 snprintf(resFilePath, sizeof(resFilePath), "\"%s\"", tmpResFilePath);
1846 }
1847
1848 /* Check if dll file and lib file exists and that it is not newer than genc file. */
1849 if (!o->rebuild && (T_FileStream_file_exists(dllFilePath) && isFileModTimeLater(dllFilePath, gencFilePath)) &&
1850 (T_FileStream_file_exists(libFilePath) && isFileModTimeLater(libFilePath, gencFilePath))) {
1851 if(o->verbose) {
1852 printf("# Not rebuilding %s - up to date.\n", gencFilePath);
1853 }
1854 return 0;
1855 }
1856
1857 char extraFlags[SMALL_BUFFER_MAX_SIZE] = "";
1858 #ifdef WINDOWS_WITH_MSVC
1859 if (options[WIN_UWP_BUILD].doesOccur) {
1860 uprv_strcat(extraFlags, LINK_EXTRA_UWP_FLAGS);
1861
1862 if (options[WIN_DLL_ARCH].doesOccur) {
1863 if (uprv_strcmp(options[WIN_DLL_ARCH].value, "X86") == 0) {
1864 uprv_strcat(extraFlags, LINK_EXTRA_UWP_FLAGS_X86_ONLY);
1865 }
1866 }
1867 }
1868
1869 if (options[WIN_DLL_ARCH].doesOccur) {
1870 uprv_strcat(extraFlags, LINK_EXTRA_FLAGS_MACHINE);
1871 uprv_strcat(extraFlags, options[WIN_DLL_ARCH].value);
1872 }
1873
1874 #endif
1875 snprintf(cmd, sizeof(cmd), "%s\"%s\" %s %s\"%s\" \"%s\" %s",
1876 LINK_CMD,
1877 dllFilePath,
1878 extraFlags,
1879 LINK_FLAGS,
1880 libFilePath,
1881 gencFilePath,
1882 resFilePath
1883 );
1884 }
1885
1886 result = runCommand(cmd, true);
1887 if (result != 0) {
1888 fprintf(stderr, "Error creating Windows DLL library. Failed command: %s\n", cmd);
1889 }
1890
1891 return result;
1892 }
1893 #endif
1894
1895 static UPKGOptions *pkg_checkFlag(UPKGOptions *o) {
1896 #if U_PLATFORM == U_PF_AIX
1897 /* AIX needs a map file. */
1898 char *flag = nullptr;
1899 int32_t length = 0;
1900 char tmpbuffer[SMALL_BUFFER_MAX_SIZE];
1901 const char MAP_FILE_EXT[] = ".map";
1902 FileStream *f = nullptr;
1903 char mapFile[SMALL_BUFFER_MAX_SIZE] = "";
1904 int32_t start = -1;
1905 uint32_t count = 0;
1906 const char rm_cmd[] = "rm -f all ;";
1907
1908 flag = pkgDataFlags[GENLIB];
1909
1910 /* This portion of the code removes 'rm -f all' in the GENLIB.
1911 * Only occurs in AIX.
1912 */
1913 if (uprv_strstr(flag, rm_cmd) != nullptr) {
1914 char *tmpGenlibFlagBuffer = nullptr;
1915 int32_t i, offset;
1916
1917 length = static_cast<int32_t>(uprv_strlen(flag) + 1);
1918 tmpGenlibFlagBuffer = (char *)uprv_malloc(length);
1919 if (tmpGenlibFlagBuffer == nullptr) {
1920 /* Memory allocation error */
1921 fprintf(stderr,"Unable to allocate buffer of size: %d.\n", length);
1922 return nullptr;
1923 }
1924
1925 uprv_strcpy(tmpGenlibFlagBuffer, flag);
1926
1927 offset = static_cast<int32_t>(uprv_strlen(rm_cmd));
1928
1929 for (i = 0; i < (length - offset); i++) {
1930 flag[i] = tmpGenlibFlagBuffer[offset + i];
1931 }
1932
1933 /* Zero terminate the string */
1934 flag[i] = 0;
1935
1936 uprv_free(tmpGenlibFlagBuffer);
1937 }
1938
1939 flag = pkgDataFlags[BIR_FLAGS];
1940 length = static_cast<int32_t>(uprv_strlen(pkgDataFlags[BIR_FLAGS]));
1941
1942 for (int32_t i = 0; i < length; i++) {
1943 if (flag[i] == MAP_FILE_EXT[count]) {
1944 if (count == 0) {
1945 start = i;
1946 }
1947 count++;
1948 } else {
1949 count = 0;
1950 }
1951
1952 if (count == uprv_strlen(MAP_FILE_EXT)) {
1953 break;
1954 }
1955 }
1956
1957 if (start >= 0) {
1958 int32_t index = 0;
1959 for (int32_t i = 0;;i++) {
1960 if (i == start) {
1961 for (int32_t n = 0;;n++) {
1962 if (o->shortName[n] == 0) {
1963 break;
1964 }
1965 tmpbuffer[index++] = o->shortName[n];
1966 }
1967 }
1968
1969 tmpbuffer[index++] = flag[i];
1970
1971 if (flag[i] == 0) {
1972 break;
1973 }
1974 }
1975
1976 uprv_memset(flag, 0, length);
1977 uprv_strcpy(flag, tmpbuffer);
1978
1979 uprv_strcpy(mapFile, o->shortName);
1980 uprv_strcat(mapFile, MAP_FILE_EXT);
1981
1982 f = T_FileStream_open(mapFile, "w");
1983 if (f == nullptr) {
1984 fprintf(stderr,"Unable to create map file: %s.\n", mapFile);
1985 return nullptr;
1986 } else {
1987 snprintf(tmpbuffer, sizeof(tmpbuffer), "%s%s ", o->entryName, UDATA_CMN_INTERMEDIATE_SUFFIX);
1988
1989 T_FileStream_writeLine(f, tmpbuffer);
1990
1991 T_FileStream_close(f);
1992 }
1993 }
1994 #elif U_PLATFORM == U_PF_CYGWIN || U_PLATFORM == U_PF_MINGW
1995 /* Cygwin needs to change flag options. */
1996 char *flag = nullptr;
1997 int32_t length = 0;
1998
1999 flag = pkgDataFlags[GENLIB];
2000 length = static_cast<int32_t>(uprv_strlen(pkgDataFlags[GENLIB]));
2001
2002 int32_t position = length - 1;
2003
2004 for(;position >= 0;position--) {
2005 if (flag[position] == '=') {
2006 position++;
2007 break;
2008 }
2009 }
2010
2011 uprv_memset(flag + position, 0, length - position);
2012 #elif U_PLATFORM == U_PF_OS400
2013 /* OS/400 needs to fix the ld options (swap single quote with double quote) */
2014 char *flag = nullptr;
2015 int32_t length = 0;
2016
2017 flag = pkgDataFlags[GENLIB];
2018 length = static_cast<int32_t>(uprv_strlen(pkgDataFlags[GENLIB]));
2019
2020 int32_t position = length - 1;
2021
2022 for(int32_t i = 0; i < length; i++) {
2023 if (flag[i] == '\'') {
2024 flag[i] = '\"';
2025 }
2026 }
2027 #endif
2028 // Don't really need a return value, just need to stop compiler warnings about
2029 // the unused parameter 'o' on platforms where it is not otherwise used.
2030 return o;
2031 }
2032
2033 static void loadLists(UPKGOptions *o, UErrorCode *status)
2034 {
2035 CharList *l, *tail = nullptr, *tail2 = nullptr;
2036 FileStream *in;
2037 char line[16384];
2038 char *linePtr, *lineNext;
2039 const uint32_t lineMax = 16300;
2040 char *tmp;
2041 int32_t tmpLength = 0;
2042 char *s;
2043 int32_t ln=0; /* line number */
2044
2045 for(l = o->fileListFiles; l; l = l->next) {
2046 if(o->verbose) {
2047 fprintf(stdout, "# pkgdata: Reading %s..\n", l->str);
2048 }
2049 /* TODO: stdin */
2050 in = T_FileStream_open(l->str, "r"); /* open files list */
2051
2052 if(!in) {
2053 fprintf(stderr, "Error opening <%s>.\n", l->str);
2054 *status = U_FILE_ACCESS_ERROR;
2055 return;
2056 }
2057
2058 while(T_FileStream_readLine(in, line, sizeof(line))!=nullptr) { /* for each line */
2059 ln++;
2060 if(uprv_strlen(line)>lineMax) {
2061 fprintf(stderr, "%s:%d - line too long (over %d chars)\n", l->str, static_cast<int>(ln), static_cast<int>(lineMax));
2062 exit(1);
2063 }
2064 /* remove spaces at the beginning */
2065 linePtr = line;
2066 /* On z/OS, disable call to isspace (#9996). Investigate using uprv_isspace instead (#9999) */
2067 #if U_PLATFORM != U_PF_OS390
2068 while(isspace(*linePtr)) {
2069 linePtr++;
2070 }
2071 #endif
2072 s=linePtr;
2073 /* remove trailing newline characters */
2074 while(*s!=0) {
2075 if(*s=='\r' || *s=='\n') {
2076 *s=0;
2077 break;
2078 }
2079 ++s;
2080 }
2081 if((*linePtr == 0) || (*linePtr == '#')) {
2082 continue; /* comment or empty line */
2083 }
2084
2085 /* Now, process the line */
2086 lineNext = nullptr;
2087
2088 while(linePtr && *linePtr) { /* process space-separated items */
2089 while(*linePtr == ' ') {
2090 linePtr++;
2091 }
2092 /* Find the next quote */
2093 if(linePtr[0] == '"')
2094 {
2095 lineNext = uprv_strchr(linePtr+1, '"');
2096 if(lineNext == nullptr) {
2097 fprintf(stderr, "%s:%d - missing trailing double quote (\")\n",
2098 l->str, static_cast<int>(ln));
2099 exit(1);
2100 } else {
2101 lineNext++;
2102 if(*lineNext) {
2103 if(*lineNext != ' ') {
2104 fprintf(stderr, "%s:%d - malformed quoted line at position %d, expected ' ' got '%c'\n",
2105 l->str, static_cast<int>(ln), static_cast<int>(lineNext - line), *lineNext ? *lineNext : '0');
2106 exit(1);
2107 }
2108 *lineNext = 0;
2109 lineNext++;
2110 }
2111 }
2112 } else {
2113 lineNext = uprv_strchr(linePtr, ' ');
2114 if(lineNext) {
2115 *lineNext = 0; /* terminate at space */
2116 lineNext++;
2117 }
2118 }
2119
2120 /* add the file */
2121 s = const_cast<char*>(getLongPathname(linePtr));
2122
2123 /* normal mode.. o->files is just the bare list without package names */
2124 o->files = pkg_appendToList(o->files, &tail, uprv_strdup(linePtr));
2125 if(uprv_pathIsAbsolute(s) || s[0] == '.') {
2126 fprintf(stderr, "pkgdata: Error: absolute path encountered. Old style paths are not supported. Use relative paths such as 'fur.res' or 'translit%cfur.res'.\n\tBad path: '%s'\n", U_FILE_SEP_CHAR, s);
2127 exit(U_ILLEGAL_ARGUMENT_ERROR);
2128 }
2129 /* The +5 is to add a little extra space for, among other things, PKGDATA_FILE_SEP_STRING */
2130 tmpLength = static_cast<int32_t>(uprv_strlen(o->srcDir) + uprv_strlen(s) + 5);
2131 if ((tmp = static_cast<char*>(uprv_malloc(tmpLength))) == nullptr) {
2132 fprintf(stderr, "pkgdata: Error: Unable to allocate tmp buffer size: %d\n", tmpLength);
2133 exit(U_MEMORY_ALLOCATION_ERROR);
2134 }
2135 uprv_strcpy(tmp, o->srcDir);
2136 uprv_strcat(tmp, o->srcDir[uprv_strlen(o->srcDir)-1] == U_FILE_SEP_CHAR ? "" : PKGDATA_FILE_SEP_STRING);
2137 uprv_strcat(tmp, s);
2138 o->filePaths = pkg_appendToList(o->filePaths, &tail2, tmp);
2139 linePtr = lineNext;
2140 } /* for each entry on line */
2141 } /* for each line */
2142 T_FileStream_close(in);
2143 } /* for each file list file */
2144 }
2145
2146 /* Helper for pkg_getPkgDataPath() */
2147 #if U_HAVE_POPEN
2148 static UBool getPkgDataPath(const char *cmd, UBool verbose, char *buf, size_t items) {
2149 icu::CharString cmdBuf;
2150 UErrorCode status = U_ZERO_ERROR;
2151 icu::LocalPipeFilePointer p;
2152 size_t n;
2153
2154 cmdBuf.append(cmd, status);
2155 if (verbose) {
2156 fprintf(stdout, "# Calling: %s\n", cmdBuf.data());
2157 }
2158 p.adoptInstead( popen(cmdBuf.data(), "r") );
2159
2160 if (p.isNull() || (n = fread(buf, 1, items-1, p.getAlias())) <= 0) {
2161 fprintf(stderr, "%s: Error calling '%s'\n", progname, cmd);
2162 *buf = 0;
2163 return false;
2164 }
2165
2166 return true;
2167 }
2168 #endif
2169
2170 /* Get path to pkgdata.inc. Try pkg-config first, falling back to icu-config. */
2171 static int32_t pkg_getPkgDataPath(UBool verbose, UOption *option) {
2172 #if U_HAVE_POPEN
2173 static char buf[512] = "";
2174 UBool pkgconfigIsValid = true;
2175 const char *pkgconfigCmd = "pkg-config --variable=pkglibdir icu-uc";
2176 const char *icuconfigCmd = "icu-config --incpkgdatafile";
2177 const char *pkgdata = "pkgdata.inc";
2178
2179 if (!getPkgDataPath(pkgconfigCmd, verbose, buf, UPRV_LENGTHOF(buf))) {
2180 if (!getPkgDataPath(icuconfigCmd, verbose, buf, UPRV_LENGTHOF(buf))) {
2181 fprintf(stderr, "%s: icu-config not found. Fix PATH or specify -O option\n", progname);
2182 return -1;
2183 }
2184
2185 pkgconfigIsValid = false;
2186 }
2187
2188 for (int32_t length = strlen(buf) - 1; length >= 0; length--) {
2189 if (buf[length] == '\n' || buf[length] == ' ') {
2190 buf[length] = 0;
2191 } else {
2192 break;
2193 }
2194 }
2195
2196 if (!*buf) {
2197 fprintf(stderr, "%s: Unable to locate pkgdata.inc. Unable to parse the results of '%s'. Check paths or use the -O option to specify the path to pkgdata.inc.\n", progname, pkgconfigIsValid ? pkgconfigCmd : icuconfigCmd);
2198 return -1;
2199 }
2200
2201 if (pkgconfigIsValid) {
2202 uprv_strcat(buf, U_FILE_SEP_STRING);
2203 uprv_strcat(buf, pkgdata);
2204 }
2205
2206 buf[strlen(buf)] = 0;
2207
2208 option->value = buf;
2209 option->doesOccur = true;
2210
2211 return 0;
2212 #else
2213 return -1;
2214 #endif
2215 }
2216
2217 #ifdef CAN_WRITE_OBJ_CODE
2218 /* Create optMatchArch for genccode architecture detection */
2219 static void pkg_createOptMatchArch(char *optMatchArch) {
2220 #if !defined(WINDOWS_WITH_MSVC) || defined(USING_CYGWIN)
2221 const char* code = "void oma(){}";
2222 const char* source = "oma.c";
2223 const char* obj = "oma.obj";
2224 FileStream* stream = nullptr;
2225
2226 stream = T_FileStream_open(source,"w");
2227 if (stream != nullptr) {
2228 T_FileStream_writeLine(stream, code);
2229 T_FileStream_close(stream);
2230
2231 char cmd[LARGE_BUFFER_MAX_SIZE];
2232 snprintf(cmd, sizeof(cmd), "%s %s -o %s",
2233 pkgDataFlags[COMPILER],
2234 source,
2235 obj);
2236
2237 if (runCommand(cmd) == 0){
2238 sprintf(optMatchArch, "%s", obj);
2239 }
2240 else {
2241 fprintf(stderr, "Failed to compile %s\n", source);
2242 }
2243 if(!T_FileStream_remove(source)){
2244 fprintf(stderr, "T_FileStream_remove failed to delete %s\n", source);
2245 }
2246 }
2247 else {
2248 fprintf(stderr, "T_FileStream_open failed to open %s for writing\n", source);
2249 }
2250 #endif
2251 }
2252 static void pkg_destroyOptMatchArch(char *optMatchArch) {
2253 if(T_FileStream_file_exists(optMatchArch) && !T_FileStream_remove(optMatchArch)){
2254 fprintf(stderr, "T_FileStream_remove failed to delete %s\n", optMatchArch);
2255 }
2256 }
2257 #endif