Skip to content

Commit

Permalink
release: switch between modes using define
Browse files Browse the repository at this point in the history
Use `LLHTTP_STRICT_MODE` C define to switch between modes instead of
separate compilation targets.

Remove `bitcode` output as it is slower than C version anyway.

See: envoyproxy/envoy#5155 (comment)
PR-URL: #58
  • Loading branch information
indutny committed Jul 13, 2020
1 parent 5ae11b7 commit afe7356
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ build/libllhttp.a: build/c/llhttp.o build/native/api.o \
build/native/http.o
$(AR) rcs $@ build/c/llhttp.o build/native/api.o build/native/http.o

build/bitcode/llhttp.o: build/bitcode/llhttp.bc
$(CLANG) $(CFLAGS) -c $< -o $@

build/c/llhttp.o: build/c/llhttp.c
$(CLANG) $(CFLAGS) $(INCLUDES) -c $< -o $@

Expand All @@ -30,7 +27,6 @@ build/native/%.o: src/native/%.c build/llhttp.h src/native/api.h \
$(CLANG) $(CFLAGS) $(INCLUDES) -c $< -o $@

build/llhttp.h: generate
build/bitcode/llhttp.bc: generate
build/c/llhttp.c: generate

build/native:
Expand Down
61 changes: 45 additions & 16 deletions bin/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,58 @@ const pkgFile = path.join(__dirname, '..', 'package.json');
const pkg = JSON.parse(fs.readFileSync(pkgFile).toString());

const BUILD_DIR = path.join(__dirname, '..', 'build');
const BITCODE_DIR = path.join(BUILD_DIR, 'bitcode');
const C_DIR = path.join(BUILD_DIR, 'c');
const SRC_DIR = path.join(__dirname, '..', 'src');

const BITCODE_FILE = path.join(BITCODE_DIR, 'llhttp.bc');
const C_FILE = path.join(C_DIR, 'llhttp.c');
const HEADER_FILE = path.join(BUILD_DIR, 'llhttp.h');

for (const dir of [ BUILD_DIR, BITCODE_DIR, C_DIR ]) {
for (const dir of [ BUILD_DIR, C_DIR ]) {
try {
fs.mkdirSync(dir);
} catch (e) {
// no-op
}
}

const mode = process.argv[2] === 'strict' ? 'strict' : 'loose';
function build(mode: 'strict' | 'loose') {
const llparse = new LLParse('llhttp__internal');
const instance = new llhttp.HTTP(llparse, mode);

return llparse.build(instance.build().entry, {
c: {
header: 'llhttp',
},
debug: process.env.LLPARSE_DEBUG ? 'llhttp__debug' : undefined,
generateBitcode: false,
headerGuard: 'INCLUDE_LLHTTP_ITSELF_H_',
});
}

function guard(strict: string, loose: string): string {
let out = '';

const llparse = new LLParse('llhttp__internal');
const instance = new llhttp.HTTP(llparse, mode);
if (strict === loose) {
return strict;
}

const artifacts = llparse.build(instance.build().entry, {
c: {
header: 'llhttp',
},
debug: process.env.LLPARSE_DEBUG ? 'llhttp__debug' : undefined,
headerGuard: 'INCLUDE_LLHTTP_ITSELF_H_',
});
out += '#ifdef LLHTTP_STRICT_MODE\n';
out += '\n';
out += strict + '\n';
out += '\n';
out += '#else /* !LLHTTP_STRICT_MODE */\n';
out += '\n';
out += loose + '\n';
out += '\n';
out += '#endif /* LLHTTP_STRICT_MODE */\n';

return out;
}

const artifacts = {
loose: build('loose'),
strict: build('strict'),
};

let headers = '';

Expand All @@ -53,9 +77,14 @@ headers += `#define LLHTTP_VERSION_MINOR ${version.minor}\n`;
headers += `#define LLHTTP_VERSION_PATCH ${version.patch}\n`;
headers += '\n';

headers += '#ifndef LLHTTP_STRICT_MODE\n';
headers += '# define LLHTTP_STRICT_MODE 1\n';
headers += '#endif\n';
headers += '\n';

const cHeaders = new llhttp.CHeaders();

headers += artifacts.header;
headers += guard(artifacts.strict.header, artifacts.loose.header);

headers += '\n';

Expand All @@ -68,6 +97,6 @@ headers += fs.readFileSync(path.join(SRC_DIR, 'native', 'api.h'));
headers += '\n';
headers += '#endif /* INCLUDE_LLHTTP_H_ */\n';

fs.writeFileSync(BITCODE_FILE, artifacts.bitcode);
fs.writeFileSync(C_FILE, artifacts.c);
fs.writeFileSync(C_FILE,
guard(artifacts.strict.c || '', artifacts.loose.c || ''));
fs.writeFileSync(HEADER_FILE, headers);

0 comments on commit afe7356

Please sign in to comment.