eliminate bin2c and use objcopy instead to translate binary data to an object file

This commit is contained in:
Joel Dice 2007-10-26 11:38:36 -06:00
parent d0089c4d88
commit ccdc7fdda3
3 changed files with 40 additions and 84 deletions

21
classpath/classpath.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "stdint.h"
#ifdef __MINGW32__
# define EXPORT __declspec(dllexport)
#else
# define EXPORT __attribute__ ((visibility("default")))
#endif
extern "C" {
extern const uint8_t _binary_classpath_jar_start[];
extern const uint8_t _binary_classpath_jar_size[];
EXPORT const uint8_t*
vmClasspath(unsigned* size)
{
*size = reinterpret_cast<uintptr_t>(_binary_classpath_jar_size);
return _binary_classpath_jar_start;
}
} // extern "C"

View File

@ -42,11 +42,12 @@ cxx = $(build-cxx)
cc = $(build-cc)
ar = ar
ranlib = ranlib
objcopy = objcopy
vg = nice valgrind --suppressions=valgrind.supp --undef-value-errors=no \
--num-callers=32 --db-attach=yes --freelist-vol=100000000
db = gdb --args
javac = javac
zip = zip
jar = jar
strip = :
show-size = :
@ -92,6 +93,7 @@ ifeq ($(platform),windows)
dlltool = i586-mingw32msvc-dlltool
ar = i586-mingw32msvc-ar
ranlib = i586-mingw32msvc-ranlib
objcopy = i586-mingw32msvc-objcopy
rdynamic = -Wl,--export-dynamic
lflags = -L$(lib) -lm -lz -lws2_32 -Wl,--kill-at -mwindows -mconsole
@ -114,11 +116,19 @@ ifeq ($(mode),fast)
endif
ifeq ($(arch),i386)
object-arch = i386
object-format = elf32-i386
pointer-size = 4
else
object-arch = i386:x86-64
object-format = elf64-x86-64
pointer-size = 8
endif
ifeq ($(platform),windows)
object-format = pe-i386
endif
cpp-objects = $(foreach x,$(1),$(patsubst $(2)/%.cpp,$(3)/%.o,$(x)))
asm-objects = $(foreach x,$(1),$(patsubst $(2)/%.S,$(3)/%-asm.o,$(x)))
java-classes = $(foreach x,$(1),$(patsubst $(2)/%.java,$(3)/%.class,$(x)))
@ -182,17 +192,13 @@ generator-objects = \
$(call cpp-objects,$(generator-sources),$(src),$(native-build))
generator = $(native-build)/generator
bin2c-sources = $(src)/bin2c.cpp
bin2c-objects = $(call cpp-objects,$(bin2c-sources),$(src),$(native-build))
bin2c = $(native-build)/bin2c
archive = $(native-build)/libvm.a
interpreter = $(native-build)/vm
classpath-sources = $(shell find $(classpath) -name '*.java')
classpath-classes = \
$(call java-classes,$(classpath-sources),$(classpath),$(classpath-build))
classpath-object = $(native-build)/classpath.o
classpath-object = $(native-build)/classpath-jar.o
test-sources = $(shell find $(test) -name '*.java')
test-classes = $(call java-classes,$(test-sources),$(test),$(test-build))
@ -282,22 +288,16 @@ $(interpreter-asm-objects): $(native-build)/%-asm.o: $(src)/%.S
$(driver-object): $(native-build)/%.o: $(src)/%.cpp
$(compile-object)
$(bin2c-objects): $(native-build)/%.o: $(src)/%.cpp
@echo "compiling $(@)"
@mkdir -p -m 1777 $(dir $(@))
$(build-cxx) $(build-cflags) -c $(<) -o $(@)
$(build)/classpath.zip: $(classpath-classes)
echo $(classpath-classes)
$(build)/classpath.jar: $(classpath-classes)
(wd=$$(pwd); \
cd $(classpath-build); \
$(zip) -q -0 $${wd}/$(@) $$(find -name '*.class'))
$(jar) c0f $${wd}/$(@) $$(find -name '*.class'))
$(build)/classpath.c: $(build)/classpath.zip $(bin2c)
$(bin2c) $(<) vmClasspath >$(@)
$(classpath-object): $(build)/classpath.c
$(cxx) $(cflags) -c $(<) -o $(@)
$(classpath-object): $(build)/classpath.jar
(wd=$$(pwd); \
cd $(build); \
$(objcopy) -I binary classpath.jar \
-O $(object-format) -B $(object-arch) $${wd}/$(@))
$(generator-objects): $(native-build)/%.o: $(src)/%.cpp
@echo "compiling $(@)"
@ -333,6 +333,3 @@ $(generator): $(generator-objects)
@echo "linking $(@)"
$(build-cc) $(^) -o $(@)
$(bin2c): $(bin2c-objects)
@echo "linking $(@)"
$(build-cc) $(^) -o $(@)

View File

@ -1,62 +0,0 @@
#include "stdio.h"
#include "stdint.h"
#include "stdlib.h"
namespace {
void
writeCode(FILE* in, FILE* out, const char* procedure)
{
fprintf(out, "#ifdef __MINGW32__\n");
fprintf(out, "# define EXPORT __declspec(dllexport)\n");
fprintf(out, "#else\n");
fprintf(out, "# define EXPORT __attribute__"
"((visibility(\"default\")))\n");
fprintf(out, "#endif\n\n");
fprintf(out, "namespace { const unsigned char data[] = {\n");
const unsigned size = 4096;
uint8_t buffer[size];
while (not feof(in)) {
unsigned c = fread(buffer, 1, size, in);
for (unsigned i = 0; i < c; ++i) {
fprintf(out, "0x%x,", buffer[i]);
}
}
fprintf(out, "}; }\n\n");
fprintf(out, "extern \"C\" EXPORT const unsigned char*\n");
fprintf(out, "%s(unsigned* size)\n", procedure);
fprintf(out, "{\n");
fprintf(out, " *size = sizeof(data);\n");
fprintf(out, " return data;\n");
fprintf(out, "}\n");
}
void
usageAndExit(const char* name)
{
fprintf(stderr, "usage: %s <input file> <procedure name>\n", name);
exit(-1);
}
} // namespace
int
main(int ac, const char** av)
{
if (ac != 3) {
usageAndExit(av[0]);
}
FILE* in = fopen(av[1], "rb");
if (in) {
writeCode(in, stdout, av[2]);
fclose(in);
} else {
fprintf(stderr, "trouble opening %s\n", av[1]);
exit(-1);
}
}