ICU-375 on Win32, optionally write .obj directly from flat file

X-SVN-Rev: 1318
This commit is contained in:
Markus Scherer 2000-05-03 23:08:20 +00:00
parent 002534a549
commit 2c9d02db77
3 changed files with 201 additions and 40 deletions

View File

@ -19,6 +19,11 @@
* 12/09/1999 weiv Added multiple file handling
*/
#ifdef WIN32
#include <windows.h>
#include <time.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "unicode/utypes.h"
@ -36,7 +41,10 @@ static void
writeCCode(const char *filename, const char *destdir);
static void
getOutFilename(const char *inFilename, const char *destdir, char *outFilename, char *entryName);
writeObjectCode(const char *filename, const char *destdir);
static void
getOutFilename(const char *inFilename, const char *destdir, char *outFilename, char *entryName, const char *newSuffix);
static void
write8(FileStream *out, uint8_t byte);
@ -46,7 +54,8 @@ write8(FileStream *out, uint8_t byte);
static UOption options[]={
UOPTION_HELP_H,
UOPTION_HELP_QUESTION_MARK,
UOPTION_DESTDIR
UOPTION_DESTDIR,
UOPTION_DEF("object", 'o', UOPT_NO_ARG)
};
extern int
@ -67,13 +76,24 @@ main(int argc, const char *argv[]) {
"\tcreate a .c file with a byte array that contains the input file's data\n"
"\toptions:\n"
"\t\t-h or -? or --help this usage text\n"
"\t\t-d or --destdir destination directory, followed by the path\n",
"\t\t-d or --destdir destination directory, followed by the path\n"
"\t\t-o or --object write a .obj file instead of .c\n",
argv[0]);
} else {
while (--argc) {
fprintf(stdout, "Generating C code for %s\n", getLongPathname(argv[argc]));
const char *message, *filename;
void (*writeCode)(const char *filename, const char *destdir);
if(options[3].doesOccur) {
message="Generating object code for %s\n";
writeCode=&writeObjectCode;
} else {
message="Generating C code for %s\n";
writeCode=&writeCCode;
}
while(--argc) {
filename=getLongPathname(argv[argc]);
fprintf(stdout, message, filename);
column=0xffff;
writeCCode(getLongPathname(argv[argc]), options[2].value);
writeCode(filename, options[2].value);
}
}
@ -92,29 +112,29 @@ writeCCode(const char *filename, const char *destdir) {
exit(U_FILE_ACCESS_ERROR);
}
getOutFilename(filename, destdir, buffer, entry);
getOutFilename(filename, destdir, buffer, entry, ".c");
out=T_FileStream_open(buffer, "w");
if(out==NULL) {
fprintf(stderr, "genccode: unable to open output file %s\n", buffer);
exit(U_FILE_ACCESS_ERROR);
}
T_FileStream_writeLine(out, "#include \"unicode/utypes.h\"\nU_CAPI const struct U_EXPORT2 {\n double bogus;\n uint8_t bytes ");
T_FileStream_writeLine(out, "[");
sprintf(buffer, "%d", T_FileStream_size(in) );
T_FileStream_writeLine(out, buffer);
T_FileStream_writeLine(out, "]; \n} ");
for(i=0;i<strlen(entry);i++)
{
if(entry[i]=='-')
{
entry[i]='_';
/* turn dashes in the entry name into underscores */
length=uprv_strlen(entry);
for(i=0; i<length; ++i) {
if(entry[i]=='-') {
entry[i]='_';
}
}
}
T_FileStream_writeLine(out, entry);
T_FileStream_writeLine(out,"={ 0, {\n");
sprintf(buffer,
"#include \"unicode/utypes.h\"\n"
"U_CAPI const struct {\n"
" double bogus;\n"
" uint8_t bytes[%ld]; \n"
"} U_EXPORT2 %s={ 0, {\n",
T_FileStream_size(in), entry);
T_FileStream_writeLine(out, buffer);
for(;;) {
length=T_FileStream_read(in, buffer, sizeof(buffer));
@ -143,7 +163,126 @@ writeCCode(const char *filename, const char *destdir) {
}
static void
getOutFilename(const char *inFilename, const char *destdir, char *outFilename, char *entryName) {
writeObjectCode(const char *filename, const char *destdir) {
#ifdef WIN32
char buffer[4096], entry[40];
struct {
IMAGE_FILE_HEADER fileHeader;
IMAGE_SECTION_HEADER sections[2];
char linkerOptions[100];
} objHeader;
IMAGE_SYMBOL symbols[1];
struct {
DWORD sizeofLongNames;
char longNames[100];
} symbolNames;
FileStream *in, *out;
size_t i, entryLength, length, size;
in=T_FileStream_open(filename, "rb");
if(in==NULL) {
fprintf(stderr, "genccode: unable to open input file %s\n", filename);
exit(U_FILE_ACCESS_ERROR);
}
/* entry have a leading '_' */
entry[0]='_';
getOutFilename(filename, destdir, buffer, entry+1, ".obj");
/* turn dashes in the entry name into underscores */
entryLength=uprv_strlen(entry);
for(i=0; i<entryLength; ++i) {
if(entry[i]=='-') {
entry[i]='_';
}
}
/* open the output file */
out=T_FileStream_open(buffer, "wb");
if(out==NULL) {
fprintf(stderr, "genccode: unable to open output file %s\n", buffer);
exit(U_FILE_ACCESS_ERROR);
}
/* populate the .obj headers */
uprv_memset(&objHeader, 0, sizeof(objHeader));
uprv_memset(&symbols, 0, sizeof(symbols));
uprv_memset(&symbolNames, 0, sizeof(symbolNames));
size=T_FileStream_size(in);
/* write the linker export directive */
uprv_strcpy(objHeader.linkerOptions, "-export:");
length=8;
uprv_strcpy(objHeader.linkerOptions+length, entry);
length+=entryLength;
uprv_strcpy(objHeader.linkerOptions+length, ",data ");
length+=6;
/* set the file header */
objHeader.fileHeader.Machine=IMAGE_FILE_MACHINE_I386;
objHeader.fileHeader.NumberOfSections=2;
objHeader.fileHeader.TimeDateStamp=time(NULL);
objHeader.fileHeader.PointerToSymbolTable=IMAGE_SIZEOF_FILE_HEADER+2*IMAGE_SIZEOF_SECTION_HEADER+length+size; /* start of symbol table */
objHeader.fileHeader.NumberOfSymbols=1;
/* set the section for the linker options */
uprv_strncpy(objHeader.sections[0].Name, ".drectve", 8);
objHeader.sections[0].SizeOfRawData=length;
objHeader.sections[0].PointerToRawData=IMAGE_SIZEOF_FILE_HEADER+2*IMAGE_SIZEOF_SECTION_HEADER;
objHeader.sections[0].Characteristics=IMAGE_SCN_LNK_INFO|IMAGE_SCN_LNK_REMOVE|IMAGE_SCN_ALIGN_1BYTES;
/* set the data section */
uprv_strncpy(objHeader.sections[1].Name, ".rdata", 6);
objHeader.sections[1].SizeOfRawData=size;
objHeader.sections[1].PointerToRawData=IMAGE_SIZEOF_FILE_HEADER+2*IMAGE_SIZEOF_SECTION_HEADER+length;
objHeader.sections[1].Characteristics=IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_ALIGN_16BYTES|IMAGE_SCN_MEM_READ;
/* set the symbol table */
if(entryLength<=8) {
uprv_strncpy(symbols[0].N.ShortName, entry, entryLength);
symbolNames.sizeofLongNames=4;
} else {
symbols[0].N.Name.Short=0;
symbols[0].N.Name.Long=4;
symbolNames.sizeofLongNames=4+entryLength+1;
uprv_strcpy(symbolNames.longNames, entry);
}
symbols[0].SectionNumber=2;
symbols[0].StorageClass=IMAGE_SYM_CLASS_EXTERNAL;
/* write the file header and the linker options section */
T_FileStream_write(out, &objHeader, objHeader.sections[1].PointerToRawData);
/* copy the data file into section 2 */
for(;;) {
length=T_FileStream_read(in, buffer, sizeof(buffer));
if(length==0) {
break;
}
T_FileStream_write(out, buffer, length);
}
/* write the symbol table */
T_FileStream_write(out, symbols, IMAGE_SIZEOF_SYMBOL);
T_FileStream_write(out, &symbolNames, symbolNames.sizeofLongNames);
if(T_FileStream_error(in)) {
fprintf(stderr, "genccode: file read error while generating from file %s\n", filename);
exit(U_FILE_ACCESS_ERROR);
}
if(T_FileStream_error(out)) {
fprintf(stderr, "genccode: file write error while generating from file %s\n", filename);
exit(U_FILE_ACCESS_ERROR);
}
T_FileStream_close(out);
T_FileStream_close(in);
#endif
}
static void
getOutFilename(const char *inFilename, const char *destdir, char *outFilename, char *entryName, const char *newSuffix) {
const char *basename=findBasename(inFilename), *suffix=uprv_strrchr(basename, '.');
/* copy path */
@ -165,7 +304,7 @@ getOutFilename(const char *inFilename, const char *destdir, char *outFilename, c
/* the filename does not have a suffix */
uprv_strcpy(entryName, inFilename);
uprv_strcpy(outFilename, inFilename);
uprv_strcat(outFilename, ".c");
uprv_strcat(outFilename, newSuffix);
} else {
/* copy basename */
while(inFilename<suffix) {
@ -184,9 +323,7 @@ getOutFilename(const char *inFilename, const char *destdir, char *outFilename, c
*entryName=0;
/* add ".c" */
*outFilename++='.';
*outFilename++='c';
*outFilename=0;
uprv_strcpy(outFilename, newSuffix);
}
}

View File

@ -64,7 +64,7 @@ LINK32=link.exe
# PROP Intermediate_Dir "Debug"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /MDd /Za /W3 /Gm /GX /ZI /Od /I "..\..\common" /I "..\toolutil" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /MDd /Ze /W3 /Gm /GX /ZI /Od /I "..\..\common" /I "..\toolutil" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD BASE RSC /l 0x409 /d "_DEBUG"
# ADD RSC /l 0x409 /d "_DEBUG"
BSC32=bscmake.exe

View File

@ -30,7 +30,7 @@ ICUTOOLS=$(ICUP)\icu\source\tools
!ENDIF
LINK32 = link.exe
LINK32_FLAGS = /out:"$(ICUDATA)/icudata.dll" /DLL /NOENTRY /base:"0x4ad00000" /comment:" Copyright (C) 1999 International Business Machines Corporation and others. All Rights Reserved. "
LINK32_FLAGS = /out:"$(ICUDATA)/icudata.dll" /DLL /NOENTRY /base:"0x4ad00000" /comment:" Copyright (C) 1999-2000 International Business Machines Corporation and others. All Rights Reserved. "
CPP_FLAGS = /I$(ICUP)\icu\include /GD /c
#Here we test if configuration is given
@ -110,7 +110,7 @@ COL_FILES = $(GENCOL_SOURCE:.txt=.col)
# This target should build all the data files
ALL : GODATA $(RB_FILES) $(CNV_FILES) $(COL_FILES) icudata.dll test.dat base_test.dat test_dat.dll base_test_dat.dll base_dat.dll icudata.dat GOBACK
ALL : GODATA $(RB_FILES) $(CNV_FILES) $(COL_FILES) test.dat base_test.dat test_dat.dll base_test_dat.dll base_dat.dll icudata.dat icudata.dll GOBACK
@echo All targets are up to date
BRK_FILES = $(ICUDATA)\sent.brk $(ICUDATA)\char.brk $(ICUDATA)\line.brk $(ICUDATA)\word.brk $(ICUDATA)\line_th.brk $(ICUDATA)\word_th.brk
@ -120,12 +120,40 @@ CPP_SOURCES = $(C_CNV_FILES) uprops_dat.c unames_dat.c cnvalias_dat.c tz_dat.c $
LINK32_OBJS = $(CPP_SOURCES:.c=.obj)
# target for DLL
# switch this condition to "a"=="a" or "a"=="b" in order to change the way the dll is built
!IF "a"=="b"
# old way of building data DLLs via .c sources for the data pieces
icudata.dll : $(LINK32_OBJS) $(CNV_FILES)
@echo Creating DLL file
@echo Creating Data DLL file from intermediate .c files
@cd $(ICUDATA)
@$(LINK32) @<<
$(LINK32_FLAGS) $(LINK32_OBJS)
@$(ICUTOOLS)\gencmn\$(CFG)\gencmn -S -d $(ICUDATA) 0 <<
$(ICUDATA)\uprops.dat
$(ICUDATA)\unames.dat
$(ICUDATA)\cnvalias.dat
$(ICUDATA)\tz.dat
$(BRK_FILES:.brk =.brk
)
$(CNV_FILES:.cnv =.cnv
)
<<
@$(CPP) $(CPP_FLAGS) icudata_dat.c
@$(LINK32) @<<
$(LINK32_FLAGS) icudata_dat.obj $(LINK32_OBJS)
<<
!ELSE
# new way of building data DLLs directly from the common map file
icudata.dll: icudata.dat
@echo Creating Data DLL file from icudata.dat
@cd $(ICUDATA)
@$(ICUTOOLS)\genccode\$(CFG)\genccode -o $(ICUDATA)\$?
@$(LINK32) $(LINK32_FLAGS) icudata_dat.obj
!ENDIF
LINK32_TEST_FLAGS = /out:"$(ICUDATA)/test_dat.dll" /DLL /NOENTRY
LINK32_BASE_TEST_FLAGS = /out:"$(ICUDATA)/base_test_dat.dll" /DLL /NOENTRY
@ -199,20 +227,16 @@ $(ICUDATA)\word_th.brk : $(ICUDATA)\word_thLE.brk
copy $(ICUDATA)\word_thLE.brk $(ICUDATA)\word_th.brk
# target for memory mapped file
icudata.dat : $(CNV_FILES) uprops.dat unames.dat cnvalias.dat tz.dat
icudata.dat : $(CNV_FILES) $(BRK_FILES) uprops.dat unames.dat cnvalias.dat tz.dat
@echo Creating memory-mapped file
@cd $(ICUDATA)
@$(ICUTOOLS)\gencmn\$(CFG)\gencmn -c 1000000 <<
@$(ICUTOOLS)\gencmn\$(CFG)\gencmn -c -d $(ICUDATA) 1000000 <<
$(ICUDATA)\uprops.dat
$(ICUDATA)\unames.dat
$(ICUDATA)\cnvalias.dat
$(ICUDATA)\tz.dat
$(ICUDATA)\sent.brk
$(ICUDATA)\char.brk
$(ICUDATA)\line.brk
$(ICUDATA)\word.brk
$(ICUDATA)\line_th.brk
$(ICUDATA)\word_th.brk
$(BRK_FILES:.brk =.brk
)
$(CNV_FILES:.cnv =.cnv
)
<<