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