Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #3 from indutny/feature-can2c

make: use can2c
  • Loading branch information...
commit b34ee3a8eb93629ae22986d191279bdf70b12f94 2 parents 9dae2b2 + 3d72da5
@creationix creationix authored
View
2  .gitignore
@@ -1 +1,3 @@
build
+tools/*
+!tools/*.cc
View
12 Makefile
@@ -28,7 +28,7 @@ LIBS=build/main.o \
build/luv_stream.o \
build/luv_tcp.o \
build/luv_timer.o \
- build/net.o
+ build/net.cc
all: build/canio
@@ -41,11 +41,17 @@ ${UVDIR}/uv.a: ${UVDIR}/Makefile
${HTTPDIR}/http_parser.o: ${HTTPDIR}/Makefile
$(MAKE) -C ${HTTPDIR} http_parser.o
+tools/can2c: tools/can2c.cc
+ g++ -g -Wall -Werror $< -o $@
+
build:
mkdir -p build
-build/%.o: lib/%.can build
- objcopy --input binary --output elf64-x86-64 --binary-architecture i386 $< $@
+build/%.cc: lib/%.can build tools/can2c
+ tools/can2c $< $@
+
+build/%.o: build/%.cc
+ g++ -g -Wall -Werror -c $< -o $@
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
2  deps/candor
@@ -1 +1 @@
-Subproject commit b89b9cb68aafb399191b581e171bb0ec3f8c8628
+Subproject commit aedc93fce7cf01ea736d45bfd81a1f925ea2a19b
2  deps/libuv
@@ -1 +1 @@
-Subproject commit c9c9d805dae321a7bdf0077547c2da2dbe70f2a2
+Subproject commit e729a82e7de13f048fdb515d638daadae53ad291
View
9 src/cio_libs.cc
@@ -9,17 +9,14 @@ using namespace candor;
#define XX(name) \
-extern char _binary_lib_##name##_can_start; \
-extern char _binary_lib_##name##_can_end; \
+extern const char __binding_lib_##name[]; \
static Handle<Object> module_##name; \
Object* cio_##name##_module() { \
if (!module_##name.IsEmpty()) { \
return *module_##name; \
} \
- char* code = &_binary_lib_##name##_can_start; \
- int len = &_binary_lib_##name##_can_end - \
- &_binary_lib_##name##_can_start; \
- Function* fn = Function::New(#name, code, len);\
+ Function* fn = Function::New( \
+ #name, __binding_lib_##name); \
fn->SetContext(cio_global_context()); \
module_##name.Wrap(fn->Call(0, NULL)); \
return *module_##name; \
View
67 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.
Something went wrong with that request. Please try again.