buildcheck

Build environment checking (a la autoconf) for node.js

Usage no npm install needed!

<script type="module">
  import buildcheck from 'https://cdn.skypack.dev/buildcheck';
</script>

README

Description

Build environment checking for node.js.

This allows for autoconf-like functionality for node addons/build scripts.

Requirements

  • node.js -- v10.0.0 or newer
  • gcc or clang

Installation

npm install buildcheck

Examples

Check if a C function exists

'use strict';

const { BuildEnvironment } = require('buildcheck');

const buildEnv = new BuildEnvironment();

console.log(buildEnv.checkFunction('c', 'preadv2'));

Check if a C header is usable

'use strict';

const { BuildEnvironment } = require('buildcheck');

const buildEnv = new BuildEnvironment();

console.log(buildEnv.checkHeaders('c', ['linux/io_uring.h']));

Try to compile some C code

'use strict';

const { BuildEnvironment } = require('buildcheck');

const buildEnv = new BuildEnvironment();

// Should be a successful compile
console.log(buildEnv.tryCompile('c', 'int main() { return 0; }'));

// Should be a failed compile
console.log(buildEnv.tryCompile('c', 'int main() { return z; }'));

API

Exports

  • BuildEnvironment - The main class for dealing with a build environment.

BuildEnvironment

Methods

  • (constructor)([< object >config]) - Creates and returns a new BuildEnvironment instance. config may contain:

    • compilerC - string - C compiler command to use. Default: process.env.CC or 'cc'

    • compilerCPP - string - C++ compiler command to use. Default: process.env.CXX or 'c++'

  • checkFunction(< string >lang, < string >functionName[, < object >options]) - boolean - Checks if functionName exists and is linkable where lang is either 'c' or 'c++'. Returns true if function exists, false otherwise. options may contain:

    • headers - array - List of header names to include when testing for function availability. Surround header names with double quotes to get a result like #include "foo.h".

    • compilerParams - array - A list of compiler/linker flags to include when testing.

  • checkHeaders(< string >lang, < array >headerNames[, < array >compilerParams]) - boolean - Checks if the headers in headerNames exist and are usable where lang is either 'c' or 'c++'. compilerParams is an optional list of compiler/linker flags to include when testing. Returns true if the headers exist and are usable, false otherwise.

  • tryCompile(< string >lang, < string >code[, < array >compilerParams]) - mixed - Attempts to compile code where lang is either 'c' or 'c++'. compilerParams is an optional array of compiler/linker flags to include. Returns true on successful compilation, or an Error instance with an output property containing the compiler error output.