From 11d8823791644a307a7c521b08d1e213e7836885 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 15 Mar 2012 17:15:18 -0700 Subject: [PATCH] process: add `process.config` This is the JS representation of the `config.gypi` file that was used when compiling node. With this information, you can tell whether the current node binary has shared or static dependencies, or any other configuration options that may have been used. --- doc/api/process.markdown | 27 ++++++++++++++++++ src/node.js | 16 +++++++++++ test/simple/test-process-config.js | 44 ++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 test/simple/test-process-config.js diff --git a/doc/api/process.markdown b/doc/api/process.markdown index 831c2c55062..b7ab3b7c4ad 100644 --- a/doc/api/process.markdown +++ b/doc/api/process.markdown @@ -256,6 +256,33 @@ Will output: ev: '4.4', openssl: '1.0.0e-fips' } +## process.config + +An Object containing the JavaScript representation of the configure options +that were used to compile the current node executable. This is the same as +the "config.gypi" file that was produced when running the `./configure` script. + +An example of the possible output looks like: + + { target_defaults: + { cflags: [], + default_configuration: 'Release', + defines: [], + include_dirs: [], + libraries: [] }, + variables: + { host_arch: 'x64', + node_install_npm: 'true', + node_install_waf: 'true', + node_prefix: '', + node_shared_v8: 'false', + node_shared_zlib: 'false', + node_use_dtrace: 'false', + node_use_openssl: 'true', + node_use_system_openssl: 'false', + strict_aliasing: 'true', + target_arch: 'x64', + v8_use_snapshot: 'true' } } ## process.installPrefix diff --git a/src/node.js b/src/node.js index 9dd9fd41950..52467a69b20 100644 --- a/src/node.js +++ b/src/node.js @@ -37,6 +37,7 @@ startup.globalConsole(); startup.processAssert(); + startup.processConfig(); startup.processNextTick(); startup.processStdio(); startup.processKillAndExit(); @@ -177,6 +178,21 @@ }; }; + startup.processConfig = function() { + // used for `process.config`, but not a real module + var config = NativeModule._source.config; + delete NativeModule._source.config; + + // strip the gyp comment line at the beginning + config = config.split('\n').slice(1).join('\n').replace(/'/g, '"'); + + process.config = JSON.parse(config, function(key, value) { + if (value === 'true') return true; + if (value === 'false') return false; + return value; + }); + } + startup.processNextTick = function() { var nextTickQueue = []; diff --git a/test/simple/test-process-config.js b/test/simple/test-process-config.js new file mode 100644 index 00000000000..b3236cd5ec3 --- /dev/null +++ b/test/simple/test-process-config.js @@ -0,0 +1,44 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +var common = require('../common'); +var assert = require('assert'); +var fs = require('fs'); +var path = require('path'); + +// check for existence +assert(process.hasOwnProperty('config')); + +// ensure that `process.config` is an Object +assert(Object(process.config) === process.config); + +var configPath = path.resolve(__dirname, '..', '..', 'config.gypi'); +var config = fs.readFileSync(configPath, 'utf8'); + +// clean up comment at the first line +config = config.split('\n').slice(1).join('\n').replace(/'/g, '"'); +config = JSON.parse(config, function(key, value) { + if (value === 'true') return true; + if (value === 'false') return false; + return value; +}); + +assert.deepEqual(config, process.config);