From 930ba56d0318927d47c8b0229274a7edf73e3145 Mon Sep 17 00:00:00 2001 From: levk Date: Tue, 26 Dec 2017 11:14:19 +0200 Subject: [PATCH] build: support cmake Refs: https://github.com/nodejs/node/issues/12425 --- .gitignore | 4 ++++ BUILDING.md | 11 +++++++++-- Makefile | 9 +++++++++ tools/fix-cmake-lists.py | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100755 tools/fix-cmake-lists.py diff --git a/.gitignore b/.gitignore index 72ccaddaceb4f5..aa0f7369d0e729 100644 --- a/.gitignore +++ b/.gitignore @@ -115,3 +115,7 @@ icu_config.gypi deps/uv/.github/ deps/uv/docs/code/ deps/uv/docs/src/guide/ + +# CMake +CMakeLists.txt +cmake-build-* \ No newline at end of file diff --git a/BUILDING.md b/BUILDING.md index 6443e52b2277d5..9d990cfd919f3e 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -111,10 +111,17 @@ $ ./configure $ make -j4 ``` -Running `make` with the `-j4` flag will cause it to run 4 compilation jobs +Alternatively, to build Node.js via [CMake](https://cmake.org/): +```console +$ ./configure +$ make configure-cmake-[debug/release] +$ cmake --build cmake-build-[debug/release] --target node -- -j 4 +``` + +Running `make` or `cmake` with the `-j4` flag will cause it to run 4 compilation jobs concurrently which may significantly reduce build time. The number after `-j` can be changed to best suit the number of processor cores on your machine. If -you run into problems running `make` with concurrency, try running it without +you run into problems running `make` or `cmake` with concurrency, try running it without the `-j4` flag. See the [GNU Make Documentation](https://www.gnu.org/software/make/manual/html_node/Parallel.html) for more information. diff --git a/Makefile b/Makefile index 68714926cf5deb..f9e3de3d342d07 100644 --- a/Makefile +++ b/Makefile @@ -86,6 +86,15 @@ out/Makefile: common.gypi deps/uv/uv.gyp deps/http_parser/http_parser.gyp \ config.gypi $(PYTHON) tools/gyp_node.py -f make +generate-cmake-files: + $(PYTHON) tools/gyp_node.py -f cmake && $(PYTHON) tools/fix-cmake-lists.py + +configure-cmake-debug: generate-cmake-files + cp -f out/Debug/CMakeLists.txt . + +configure-cmake-release: generate-cmake-files + cp -f out/Release/CMakeLists.txt . + config.gypi: configure $(error Missing or stale $@, please run ./$<) diff --git a/tools/fix-cmake-lists.py b/tools/fix-cmake-lists.py new file mode 100755 index 00000000000000..3adb9b49ea5432 --- /dev/null +++ b/tools/fix-cmake-lists.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +import os + +script_dir = os.path.dirname(__file__) +node_root = os.path.normpath(os.path.join(script_dir, os.pardir)) +output_dir = os.path.join(os.path.abspath(node_root), 'out') + +cmake_debug_path = os.path.join(output_dir, 'Debug', 'CMakeLists.txt') +cmake_release_path = os.path.join(output_dir, 'Release', 'CMakeLists.txt') + +def fix_cmake_list(path): + with open(path, 'r') as f: + fixed_lines = [] + for line in f.readlines(): + line = line.replace('"../../', '"') + line = line.replace('${CMAKE_CURRENT_LIST_DIR}/../..', '${CMAKE_CURRENT_LIST_DIR}') + line = line.replace(' "src/tracing/trace_event.hsrc/util.h"\n', ' "src/tracing/trace_event.h"\n "src/util.h"\n') + line = line.replace('"deps/include/v8-inspector.h"', '"deps/v8/include/v8-inspector.h"') + line = line.replace('"deps/include/v8-inspector-protocol.h"', '"deps/v8/include/v8-inspector-protocol.h"') + line = line.replace('"${builddir}/obj.target/node/gen', '"${builddir}/CMakeFiles/node.dir/obj/gen') + line = line.replace('"${builddir}/obj.target/node', '"${builddir}/CMakeFiles/node.dir') + fixed_lines.append(line) + + with open(path, 'w') as f: + f.writelines(fixed_lines) + +def fix_cmake_lists(): + fix_cmake_list(cmake_debug_path) + fix_cmake_list(cmake_release_path) + +if __name__ == '__main__': + fix_cmake_lists()