1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 1999-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: gennames.c
9 * encoding: UTF-8
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 1999nov01
14 * created by: Markus W. Scherer
15 *
16 * This program reads a binary file and creates a C source code file
17 * with a byte array that contains the data of the binary file.
18 *
19 * 12/09/1999 weiv Added multiple file handling
20 */
21
22 #include "unicode/utypes.h"
23
24 #if U_PLATFORM_HAS_WIN32_API
25 # define VC_EXTRALEAN
26 # define WIN32_LEAN_AND_MEAN
27 # define NOUSER
28 # define NOSERVICE
29 # define NOIME
30 # define NOMCX
31 #include <windows.h>
32 #include <time.h>
33 #endif
34
35 #if U_PLATFORM_IS_LINUX_BASED && U_HAVE_ELF_H
36 # define U_ELF
37 #endif
38
39 #ifdef U_ELF
40 # include <elf.h>
41 # if defined(ELFCLASS64)
42 # define U_ELF64
43 # endif
44 /* Old elf.h headers may not have EM_X86_64, or have EM_X8664 instead. */
45 # ifndef EM_X86_64
46 # define EM_X86_64 62
47 # endif
48 # define ICU_ENTRY_OFFSET 0
49 #endif
50
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include "unicode/putil.h"
54 #include "cmemory.h"
55 #include "cstring.h"
56 #include "filestrm.h"
57 #include "toolutil.h"
58 #include "unicode/uclean.h"
59 #include "uoptions.h"
60 #include "pkg_genc.h"
61
62 enum {
63 kOptHelpH = 0,
64 kOptHelpQuestionMark,
65 kOptDestDir,
66 kOptQuiet,
67 kOptName,
68 kOptEntryPoint,
69 #ifdef CAN_GENERATE_OBJECTS
70 kOptObject,
71 kOptMatchArch,
72 kOptSkipDllExport,
73 #endif
74 kOptFilename,
75 kOptAssembly
76 };
77
78 static UOption options[]={
79 /*0*/UOPTION_HELP_H,
80 UOPTION_HELP_QUESTION_MARK,
81 UOPTION_DESTDIR,
82 UOPTION_QUIET,
83 UOPTION_DEF("name", 'n', UOPT_REQUIRES_ARG),
84 UOPTION_DEF("entrypoint", 'e', UOPT_REQUIRES_ARG),
85 #ifdef CAN_GENERATE_OBJECTS
86 /*6*/UOPTION_DEF("object", 'o', UOPT_NO_ARG),
87 UOPTION_DEF("match-arch", 'm', UOPT_REQUIRES_ARG),
88 UOPTION_DEF("skip-dll-export", '\0', UOPT_NO_ARG),
89 #endif
90 UOPTION_DEF("filename", 'f', UOPT_REQUIRES_ARG),
91 UOPTION_DEF("assembly", 'a', UOPT_REQUIRES_ARG)
92 };
93
94 #define CALL_WRITECCODE 'c'
95 #define CALL_WRITEASSEMBLY 'a'
96 #define CALL_WRITEOBJECT 'o'
97 extern int
main(int argc,char * argv[])98 main(int argc, char* argv[]) {
99 UBool verbose = TRUE;
100 char writeCode;
101
102 U_MAIN_INIT_ARGS(argc, argv);
103
104 options[kOptDestDir].value = ".";
105
106 /* read command line options */
107 argc=u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options);
108
109 /* error handling, printing usage message */
110 if(argc<0) {
111 fprintf(stderr,
112 "error in command line argument \"%s\"\n",
113 argv[-argc]);
114 }
115 if(argc<0 || options[kOptHelpH].doesOccur || options[kOptHelpQuestionMark].doesOccur) {
116 fprintf(stderr,
117 "usage: %s [-options] filename1 filename2 ...\n"
118 "\tread each binary input file and \n"
119 "\tcreate a .c file with a byte array that contains the input file's data\n"
120 "options:\n"
121 "\t-h or -? or --help this usage text\n"
122 "\t-d or --destdir destination directory, followed by the path\n"
123 "\t-q or --quiet do not display warnings and progress\n"
124 "\t-n or --name symbol prefix, followed by the prefix\n"
125 "\t-e or --entrypoint entry point name, followed by the name (_dat will be appended)\n"
126 "\t-r or --revision Specify a version\n"
127 , argv[0]);
128 #ifdef CAN_GENERATE_OBJECTS
129 fprintf(stderr,
130 "\t-o or --object write a .obj file instead of .c\n"
131 "\t-m or --match-arch file.o match the architecture (CPU, 32/64 bits) of the specified .o\n"
132 "\t ELF format defaults to i386. Windows defaults to the native platform.\n"
133 "\t--skip-dll-export Don't export the ICU data entry point symbol (for use when statically linking)\n");
134 #endif
135 fprintf(stderr,
136 "\t-f or --filename Specify an alternate base filename. (default: symbolname_typ)\n"
137 "\t-a or --assembly Create assembly file. (possible values are: ");
138
139 printAssemblyHeadersToStdErr();
140 } else {
141 const char *message, *filename;
142 /* TODO: remove void (*writeCode)(const char *, const char *); */
143
144 if(options[kOptAssembly].doesOccur) {
145 message="generating assembly code for %s\n";
146 writeCode = CALL_WRITEASSEMBLY;
147 /* TODO: remove writeCode=&writeAssemblyCode; */
148
149 if (!checkAssemblyHeaderName(options[kOptAssembly].value)) {
150 fprintf(stderr,
151 "Assembly type \"%s\" is unknown.\n", options[kOptAssembly].value);
152 return -1;
153 }
154 }
155 #ifdef CAN_GENERATE_OBJECTS
156 else if(options[kOptObject].doesOccur) {
157 message="generating object code for %s\n";
158 writeCode = CALL_WRITEOBJECT;
159 /* TODO: remove writeCode=&writeObjectCode; */
160 }
161 #endif
162 else
163 {
164 message="generating C code for %s\n";
165 writeCode = CALL_WRITECCODE;
166 /* TODO: remove writeCode=&writeCCode; */
167 }
168 if (options[kOptQuiet].doesOccur) {
169 verbose = FALSE;
170 }
171 while(--argc) {
172 filename=getLongPathname(argv[argc]);
173 if (verbose) {
174 fprintf(stdout, message, filename);
175 }
176
177 switch (writeCode) {
178 case CALL_WRITECCODE:
179 writeCCode(filename, options[kOptDestDir].value,
180 options[kOptName].doesOccur ? options[kOptName].value : NULL,
181 options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL,
182 NULL,
183 0);
184 break;
185 case CALL_WRITEASSEMBLY:
186 writeAssemblyCode(filename, options[kOptDestDir].value,
187 options[kOptEntryPoint].doesOccur ? options[kOptEntryPoint].value : NULL,
188 options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL,
189 NULL,
190 0);
191 break;
192 #ifdef CAN_GENERATE_OBJECTS
193 case CALL_WRITEOBJECT:
194 writeObjectCode(filename, options[kOptDestDir].value,
195 options[kOptEntryPoint].doesOccur ? options[kOptEntryPoint].value : NULL,
196 options[kOptMatchArch].doesOccur ? options[kOptMatchArch].value : NULL,
197 options[kOptFilename].doesOccur ? options[kOptFilename].value : NULL,
198 NULL,
199 0,
200 !options[kOptSkipDllExport].doesOccur);
201 break;
202 #endif
203 default:
204 /* Should never occur. */
205 break;
206 }
207 /* TODO: remove writeCode(filename, options[kOptDestDir].value); */
208 }
209 }
210
211 return 0;
212 }
213