Skip to content

Commit

Permalink
Merge pull request creationix#3 from indutny/feature-can2c
Browse files Browse the repository at this point in the history
make: use can2c
  • Loading branch information
creationix committed Mar 26, 2012
2 parents 9dae2b2 + 3d72da5 commit b34ee3a
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1 +1,3 @@
build build
tools/*
!tools/*.cc
12 changes: 9 additions & 3 deletions Makefile
Expand Up @@ -28,7 +28,7 @@ LIBS=build/main.o \
build/luv_stream.o \ build/luv_stream.o \
build/luv_tcp.o \ build/luv_tcp.o \
build/luv_timer.o \ build/luv_timer.o \
build/net.o build/net.cc


all: build/canio all: build/canio


Expand All @@ -41,11 +41,17 @@ ${UVDIR}/uv.a: ${UVDIR}/Makefile
${HTTPDIR}/http_parser.o: ${HTTPDIR}/Makefile ${HTTPDIR}/http_parser.o: ${HTTPDIR}/Makefile
$(MAKE) -C ${HTTPDIR} http_parser.o $(MAKE) -C ${HTTPDIR} http_parser.o


tools/can2c: tools/can2c.cc
g++ -g -Wall -Werror $< -o $@

build: build:
mkdir -p build mkdir -p build


build/%.o: lib/%.can build build/%.cc: lib/%.can build tools/can2c
objcopy --input binary --output elf64-x86-64 --binary-architecture i386 $< $@ tools/can2c $< $@

build/%.o: build/%.cc
g++ -g -Wall -Werror -c $< -o $@


build/%.o: src/%.cc build src/%.h ${DEPS} build/%.o: src/%.cc build src/%.h ${DEPS}
g++ -g -Wall -Werror -c $< -o $@ -I${HTTPDIR} -I${UVDIR}/include -I${CANDIR}/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 g++ -g -Wall -Werror -c $< -o $@ -I${HTTPDIR} -I${UVDIR}/include -I${CANDIR}/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
Expand Down
2 changes: 1 addition & 1 deletion deps/candor
2 changes: 1 addition & 1 deletion deps/libuv
Submodule libuv updated 1 files
+0 −1 uv.gyp
9 changes: 3 additions & 6 deletions src/cio_libs.cc
Expand Up @@ -9,17 +9,14 @@ using namespace candor;




#define XX(name) \ #define XX(name) \
extern char _binary_lib_##name##_can_start; \ extern const char __binding_lib_##name[]; \
extern char _binary_lib_##name##_can_end; \
static Handle<Object> module_##name; \ static Handle<Object> module_##name; \
Object* cio_##name##_module() { \ Object* cio_##name##_module() { \
if (!module_##name.IsEmpty()) { \ if (!module_##name.IsEmpty()) { \
return *module_##name; \ return *module_##name; \
} \ } \
char* code = &_binary_lib_##name##_can_start; \ Function* fn = Function::New( \
int len = &_binary_lib_##name##_can_end - \ #name, __binding_lib_##name); \
&_binary_lib_##name##_can_start; \
Function* fn = Function::New(#name, code, len);\
fn->SetContext(cio_global_context()); \ fn->SetContext(cio_global_context()); \
module_##name.Wrap(fn->Call(0, NULL)); \ module_##name.Wrap(fn->Call(0, NULL)); \
return *module_##name; \ return *module_##name; \
Expand Down
67 changes: 67 additions & 0 deletions tools/can2c.cc
@@ -0,0 +1,67 @@
#include <stdio.h> // fprintf
#include <stdlib.h> // exit

int main(int argc, char** argv) {
if (argc < 2) {
fprintf(stderr, "Usage: can2c .h lib/script.can [build/script.h]\n");
exit(1);
}

// Open both files
FILE* in = fopen(argv[1], "r");
if (in == NULL) {
fprintf(stderr, "Can\'t open input file: %s\n", argv[1]);
exit(1);
}

FILE* out;
if (argc >= 3) {
out = fopen(argv[2], "w");
if (out == NULL) {
fclose(in);
fprintf(stderr, "Can\'t open output file: %s\n", argv[2]);
exit(1);
}
} else {
out = stdout;
}

// Replace non-chars with _ in input filename
// and '.' with '\0'
for (int i = 0; argv[1][i] != 0; i++) {
unsigned char c = argv[1][i];
if (c == '.') {
argv[1][i] = 0;
break;
} else if (c < 'a' || c > 'z') {
argv[1][i] = '_';
}
}

// Put generated content into the output file
fprintf(out, "extern const char __binding_%s[] = {\n", argv[1]);

int offset = 0;
while (!feof(in)) {
// Read file
unsigned char buffer[1024];
size_t read = fread(buffer, 1, sizeof(buffer), in);
if (read == 0) {
fclose(in);
fclose(out);
fprintf(stderr, "Failed to read input file\n");
exit(1);
}

// Translate it to byte sequence
for (size_t i = 0; i < read; i++) {
fprintf(out, "0x%.2x, ", buffer[i]);
if (++offset % 30 == 0) fprintf(out, "\n");
}
}

fprintf(out, "0x0\n};");

fclose(in);
fclose(out);
}

0 comments on commit b34ee3a

Please sign in to comment.