4.8 KiB
Description
Build environment checking for node.js.
This allows for autoconf-like functionality for node addons/build scripts.
Note: Obsolete and/or exotic build environments or platforms not supported by node.js are not supported.
Requirements
- node.js -- v10.0.0 or newer
- Supported compilers:
- gcc
- clang
- MSVC 2013+ and Windows SDK 8.1+
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.checkHeader('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
The exported value is BuildEnvironment, the main class for dealing with a build environment.
BuildEnvironment
Methods
-
(constructor)([< object >config]) - Creates and returns a new BuildEnvironment instance.
configmay contain:-
compilerC - string - C compiler command to use. Note: this is ignored on Windows. Default:
process.env.CCor'cc' -
compilerCXX - string - C++ compiler command to use. Note: this is ignored on Windows. Default:
process.env.CXXor'c++' -
msvs_version - mixed - A string or number containing the year of the Visual Studio compiler to use. Note: this is for Windows only. Default: newest version installed
-
-
checkDeclared(< string >lang, < string >symbolName[, < object >options]) - boolean - Checks if a symbol
symbolNameis declared wherelangis either'c'or'c++'. Returnstrueif symbol exists,falseotherwise.optionsmay contain:-
headers - array - A list of headers to try when checking if the symbol is declared.
checkFunction()will always first try without a library. If not supplied, a default list of common (platform-specific) headers will be used. -
searchLibs - array - A list of library names (without the
'-l'prefix) to try when checking if the symbol is declared.checkDeclared()will always first try without a library.
-
-
checkFunction(< string >lang, < string >functionName[, < object >options]) - boolean - Checks if a function
functionNameexists and is linkable wherelangis either'c'or'c++'. Returnstrueif function exists,falseotherwise.optionsmay contain:- searchLibs - array - A list of library names (without the
'-l'prefix) to try when checking for this function.checkFunction()will always first try without a library.
- searchLibs - array - A list of library names (without the
-
checkFeature(< string >featureName) - mixed - Executes a special test for a "feature" and returns the result. Supported values for
featureName:-
'strerror_r'- Returns an object containing:-
declared- boolean - Whetherstrerror_r()is declared -
returnsCharPtr- boolean - Ifstrerror_r()is declared, whether it returnschar*(a GNU extension) or not.
-
-
-
checkHeader(< string >lang, < string >headerName) - boolean - Checks if the header
headerNameexists and is usable wherelangis either'c'or'c++'. Returnstrueif the header exists and is usable,falseotherwise. -
defines([< string >lang[, < boolean >rendered]]) - array - Returns a list of features, functions, headers, and symbols known to be defined by this build environment instance.
langis either'c'or'c++'Iflangis not set, defines for both'c'and'c++'will be returned. Ifrenderedistrue(defaults tofalse), autoconf-style defines (e.g. "HAVE_FOO=1") will be returned instead. Defines coming from features utilize base strings/names from autoconf for better compatibility. -
libs([< string >lang]) - array - Returns a list of (
'-l'-prefixed) libraries known to be required for features and functions defined by this build environment instance.langis either'c'or'c++'Iflangis not set, defines for both'c'and'c++'will be returned. -
tryCompile(< string >lang, < string >code[, < array >compilerParams]) - mixed - Attempts to compile
codewherelangis either'c'or'c++'.compilerParamsis an optional array of compiler/linker flags to include. Returnstrueon successful compilation, or an Error instance with anoutputproperty containing the compiler error output.