From e47e0092b45b036ea2988b69af9be4e1c61f1ccf Mon Sep 17 00:00:00 2001 From: "Node.js GitHub Bot" Date: Sun, 5 May 2024 00:26:13 +0000 Subject: [PATCH] tools: update gyp-next to 0.17.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/52835 Reviewed-By: Luigi Pinca Reviewed-By: Christian Clauss Reviewed-By: Michaƫl Zasso Reviewed-By: Chengzhong Wu --- tools/gyp/CHANGELOG.md | 15 +++ tools/gyp/data/ninja/build.ninja | 4 + tools/gyp/pylib/gyp/common.py | 63 ++++++++++- tools/gyp/pylib/gyp/common_test.py | 103 +++++++++++++++++- tools/gyp/pylib/gyp/generator/android.py | 6 +- .../gyp/generator/compile_commands_json.py | 10 +- tools/gyp/pylib/gyp/generator/gypsh.py | 7 +- tools/gyp/pylib/gyp/generator/make.py | 80 ++++++++++---- tools/gyp/pylib/gyp/generator/msvs.py | 21 ++-- tools/gyp/pylib/gyp/generator/ninja.py | 31 ++++++ tools/gyp/pylib/gyp/generator/ninja_test.py | 12 ++ tools/gyp/pylib/gyp/input.py | 12 +- tools/gyp/pylib/gyp/msvs_emulation.py | 10 +- tools/gyp/pyproject.toml | 16 +-- tools/gyp/release-please-config.json | 11 ++ 15 files changed, 328 insertions(+), 73 deletions(-) create mode 100644 tools/gyp/data/ninja/build.ninja create mode 100644 tools/gyp/release-please-config.json diff --git a/tools/gyp/CHANGELOG.md b/tools/gyp/CHANGELOG.md index 3ef8abf2327ab8..0909e275fee84f 100644 --- a/tools/gyp/CHANGELOG.md +++ b/tools/gyp/CHANGELOG.md @@ -1,5 +1,20 @@ # Changelog +## [0.17.0](https://github.com/nodejs/gyp-next/compare/v0.16.2...v0.17.0) (2024-04-29) + + +### Features + +* generate compile_commands.json with ninja ([#228](https://github.com/nodejs/gyp-next/issues/228)) ([7b20b46](https://github.com/nodejs/gyp-next/commit/7b20b4673d8cf46ff61898eb19569007d55c854a)) + + +### Bug Fixes + +* failed to detect flavor if compiler path include white spaces ([#240](https://github.com/nodejs/gyp-next/issues/240)) ([f3b9753](https://github.com/nodejs/gyp-next/commit/f3b9753e7526377020e7d40e66b624db771cf84a)) +* support cross compiling for wasm with make generator ([#222](https://github.com/nodejs/gyp-next/issues/222)) ([de0e1c9](https://github.com/nodejs/gyp-next/commit/de0e1c9a5791d1bf4bc3103f878ab74814864ab4)) +* support empty dictionary keys in input ([#245](https://github.com/nodejs/gyp-next/issues/245)) ([178459f](https://github.com/nodejs/gyp-next/commit/178459ff343a2771d5f30f04467d2f032d6b3565)) +* update Ruff to 0.3.1 ([876ccaf](https://github.com/nodejs/gyp-next/commit/876ccaf5629e1b95e13aaa2b0eb6cbd08fa80593)) + ## [0.16.2](https://github.com/nodejs/gyp-next/compare/v0.16.1...v0.16.2) (2024-03-07) diff --git a/tools/gyp/data/ninja/build.ninja b/tools/gyp/data/ninja/build.ninja new file mode 100644 index 00000000000000..2400dbb1f0daba --- /dev/null +++ b/tools/gyp/data/ninja/build.ninja @@ -0,0 +1,4 @@ +rule cc + command = cc $in $out + +build my.out: cc my.in diff --git a/tools/gyp/pylib/gyp/common.py b/tools/gyp/pylib/gyp/common.py index b73a0c55b1e349..762ae021090cac 100644 --- a/tools/gyp/pylib/gyp/common.py +++ b/tools/gyp/pylib/gyp/common.py @@ -9,6 +9,7 @@ import tempfile import sys import subprocess +import shlex from collections.abc import MutableSet @@ -422,8 +423,54 @@ def EnsureDirExists(path): except OSError: pass +def GetCrossCompilerPredefines(): # -> dict + cmd = [] + + # shlex.split() will eat '\' in posix mode, but + # setting posix=False will preserve extra '"' cause CreateProcess fail on Windows + # this makes '\' in %CC_target% and %CFLAGS% work + def replace_sep(s): + return s.replace(os.sep, "/") if os.sep != "/" else s + + if CC := os.environ.get("CC_target") or os.environ.get("CC"): + cmd += shlex.split(replace_sep(CC)) + if CFLAGS := os.environ.get("CFLAGS"): + cmd += shlex.split(replace_sep(CFLAGS)) + elif CXX := os.environ.get("CXX_target") or os.environ.get("CXX"): + cmd += shlex.split(replace_sep(CXX)) + if CXXFLAGS := os.environ.get("CXXFLAGS"): + cmd += shlex.split(replace_sep(CXXFLAGS)) + else: + return {} -def GetFlavor(params): + if sys.platform == "win32": + fd, input = tempfile.mkstemp(suffix=".c") + real_cmd = [*cmd, "-dM", "-E", "-x", "c", input] + try: + os.close(fd) + stdout = subprocess.run( + real_cmd, shell=True, + capture_output=True, check=True + ).stdout + finally: + os.unlink(input) + else: + input = "/dev/null" + real_cmd = [*cmd, "-dM", "-E", "-x", "c", input] + stdout = subprocess.run( + real_cmd, shell=False, + capture_output=True, check=True + ).stdout + + defines = {} + lines = stdout.decode("utf-8").replace("\r\n", "\n").split("\n") + for line in lines: + if (line or "").startswith("#define "): + _, key, *value = line.split(" ") + defines[key] = " ".join(value) + return defines + +def GetFlavorByPlatform(): """Returns |params.flavor| if it's set, the system's default flavor else.""" flavors = { "cygwin": "win", @@ -431,8 +478,6 @@ def GetFlavor(params): "darwin": "mac", } - if "flavor" in params: - return params["flavor"] if sys.platform in flavors: return flavors[sys.platform] if sys.platform.startswith("sunos"): @@ -452,6 +497,18 @@ def GetFlavor(params): return "linux" +def GetFlavor(params): + if "flavor" in params: + return params["flavor"] + + defines = GetCrossCompilerPredefines() + if "__EMSCRIPTEN__" in defines: + return "emscripten" + if "__wasm__" in defines: + return "wasi" if "__wasi__" in defines else "wasm" + + return GetFlavorByPlatform() + def CopyTool(flavor, out_path, generator_flags={}): """Finds (flock|mac|win)_tool.gyp in the gyp directory and copies it diff --git a/tools/gyp/pylib/gyp/common_test.py b/tools/gyp/pylib/gyp/common_test.py index 05344085ad3b11..b6c4cccc1ac5ca 100755 --- a/tools/gyp/pylib/gyp/common_test.py +++ b/tools/gyp/pylib/gyp/common_test.py @@ -9,7 +9,8 @@ import gyp.common import unittest import sys - +import os +from unittest.mock import patch, MagicMock class TestTopologicallySorted(unittest.TestCase): def test_Valid(self): @@ -24,9 +25,8 @@ def test_Valid(self): def GetEdge(node): return tuple(graph[node]) - self.assertEqual( - gyp.common.TopologicallySorted(graph.keys(), GetEdge), ["a", "c", "d", "b"] - ) + assert gyp.common.TopologicallySorted( + graph.keys(), GetEdge) == ["a", "c", "d", "b"] def test_Cycle(self): """Test that an exception is thrown on a cyclic graph.""" @@ -58,7 +58,7 @@ def tearDown(self): def assertFlavor(self, expected, argument, param): sys.platform = argument - self.assertEqual(expected, gyp.common.GetFlavor(param)) + assert expected == gyp.common.GetFlavor(param) def test_platform_default(self): self.assertFlavor("freebsd", "freebsd9", {}) @@ -73,6 +73,99 @@ def test_platform_default(self): def test_param(self): self.assertFlavor("foobar", "linux2", {"flavor": "foobar"}) + class MockCommunicate: + def __init__(self, stdout): + self.stdout = stdout + + def decode(self, encoding): + return self.stdout + + @patch("os.close") + @patch("os.unlink") + @patch("tempfile.mkstemp") + def test_GetCrossCompilerPredefines(self, mock_mkstemp, mock_unlink, mock_close): + mock_close.return_value = None + mock_unlink.return_value = None + mock_mkstemp.return_value = (0, "temp.c") + + def mock_run(env, defines_stdout, expected_cmd): + with patch("subprocess.run") as mock_run: + mock_process = MagicMock() + mock_process.returncode = 0 + mock_process.stdout = TestGetFlavor.MockCommunicate(defines_stdout) + mock_run.return_value = mock_process + expected_input = "temp.c" if sys.platform == "win32" else "/dev/null" + with patch.dict(os.environ, env): + defines = gyp.common.GetCrossCompilerPredefines() + flavor = gyp.common.GetFlavor({}) + if env.get("CC_target"): + mock_run.assert_called_with( + [ + *expected_cmd, + "-dM", "-E", "-x", "c", expected_input + ], + shell=sys.platform == "win32", + capture_output=True, check=True) + return [defines, flavor] + + [defines1, _] = mock_run({}, "", []) + assert {} == defines1 + + [defines2, flavor2] = mock_run( + { "CC_target": "/opt/wasi-sdk/bin/clang" }, + "#define __wasm__ 1\n#define __wasi__ 1\n", + ["/opt/wasi-sdk/bin/clang"] + ) + assert { "__wasm__": "1", "__wasi__": "1" } == defines2 + assert flavor2 == "wasi" + + [defines3, flavor3] = mock_run( + { "CC_target": "/opt/wasi-sdk/bin/clang --target=wasm32" }, + "#define __wasm__ 1\n", + ["/opt/wasi-sdk/bin/clang", "--target=wasm32"] + ) + assert { "__wasm__": "1" } == defines3 + assert flavor3 == "wasm" + + [defines4, flavor4] = mock_run( + { "CC_target": "/emsdk/upstream/emscripten/emcc" }, + "#define __EMSCRIPTEN__ 1\n", + ["/emsdk/upstream/emscripten/emcc"] + ) + assert { "__EMSCRIPTEN__": "1" } == defines4 + assert flavor4 == "emscripten" + + # Test path which include white space + [defines5, flavor5] = mock_run( + { + "CC_target": "\"/Users/Toyo Li/wasi-sdk/bin/clang\" -O3", + "CFLAGS": "--target=wasm32-wasi-threads -pthread" + }, + "#define __wasm__ 1\n#define __wasi__ 1\n#define _REENTRANT 1\n", + [ + "/Users/Toyo Li/wasi-sdk/bin/clang", + "-O3", + "--target=wasm32-wasi-threads", + "-pthread" + ] + ) + assert { + "__wasm__": "1", + "__wasi__": "1", + "_REENTRANT": "1" + } == defines5 + assert flavor5 == "wasi" + + original_sep = os.sep + os.sep = "\\" + [defines6, flavor6] = mock_run( + { "CC_target": "\"C:\\Program Files\\wasi-sdk\\clang.exe\"" }, + "#define __wasm__ 1\n#define __wasi__ 1\n", + ["C:/Program Files/wasi-sdk/clang.exe"] + ) + os.sep = original_sep + assert { "__wasm__": "1", "__wasi__": "1" } == defines6 + assert flavor6 == "wasi" if __name__ == "__main__": unittest.main() diff --git a/tools/gyp/pylib/gyp/generator/android.py b/tools/gyp/pylib/gyp/generator/android.py index d3c97c666db077..2a63f412dbc836 100644 --- a/tools/gyp/pylib/gyp/generator/android.py +++ b/tools/gyp/pylib/gyp/generator/android.py @@ -739,9 +739,9 @@ def ComputeOutput(self, spec): % (self.android_class, self.android_module) ) else: - path = "$(call intermediates-dir-for,{},{},,,$(GYP_VAR_PREFIX))".format( - self.android_class, - self.android_module, + path = ( + f"$(call intermediates-dir-for,{self.android_class}," + f"{self.android_module},,,$(GYP_VAR_PREFIX))" ) assert spec.get("product_dir") is None # TODO: not supported? diff --git a/tools/gyp/pylib/gyp/generator/compile_commands_json.py b/tools/gyp/pylib/gyp/generator/compile_commands_json.py index 0ffa3bb5980fe9..5d7f14da9699da 100644 --- a/tools/gyp/pylib/gyp/generator/compile_commands_json.py +++ b/tools/gyp/pylib/gyp/generator/compile_commands_json.py @@ -108,10 +108,14 @@ def GenerateOutput(target_list, target_dicts, data, params): cwd = os.path.dirname(build_file) AddCommandsForTarget(cwd, target, params, per_config_commands) + output_dir = None try: - output_dir = params["options"].generator_output - except (AttributeError, KeyError): - output_dir = params["generator_flags"].get("output_dir", "out") + # generator_output can be `None` on Windows machines, or even not + # defined in other cases + output_dir = params.get("options").generator_output + except AttributeError: + pass + output_dir = output_dir or params["generator_flags"].get("output_dir", "out") for configuration_name, commands in per_config_commands.items(): filename = os.path.join(output_dir, configuration_name, "compile_commands.json") gyp.common.EnsureDirExists(filename) diff --git a/tools/gyp/pylib/gyp/generator/gypsh.py b/tools/gyp/pylib/gyp/generator/gypsh.py index 82a07ddc6577be..8dfb1f1645f77c 100644 --- a/tools/gyp/pylib/gyp/generator/gypsh.py +++ b/tools/gyp/pylib/gyp/generator/gypsh.py @@ -49,10 +49,9 @@ def GenerateOutput(target_list, target_dicts, data, params): # Use a banner that looks like the stock Python one and like what # code.interact uses by default, but tack on something to indicate what # locals are available, and identify gypsh. - banner = "Python {} on {}\nlocals.keys() = {}\ngypsh".format( - sys.version, - sys.platform, - repr(sorted(locals.keys())), + banner = ( + f"Python {sys.version} on {sys.platform}\nlocals.keys() = " + f"{sorted(locals.keys())!r}\ngypsh" ) code.interact(banner, local=locals) diff --git a/tools/gyp/pylib/gyp/generator/make.py b/tools/gyp/pylib/gyp/generator/make.py index 1b9974948e4de5..392d900914dea9 100644 --- a/tools/gyp/pylib/gyp/generator/make.py +++ b/tools/gyp/pylib/gyp/generator/make.py @@ -25,6 +25,7 @@ import os import re import subprocess +import sys import gyp import gyp.common import gyp.xcode_emulation @@ -378,7 +379,7 @@ def CalculateGeneratorInputInfo(params): CXXFLAGS.target ?= $(CPPFLAGS) $(CXXFLAGS) LINK.target ?= %(LINK.target)s LDFLAGS.target ?= $(LDFLAGS) -AR.target ?= $(AR) +AR.target ?= %(AR.target)s PLI.target ?= %(PLI.target)s # C++ apps need to be linked with g++. @@ -442,13 +443,21 @@ def CalculateGeneratorInputInfo(params): define fixup_dep # The depfile may not exist if the input file didn't have any #includes. touch $(depfile).raw -# Fixup path as in (1). -sed -e "s|^$(notdir $@)|$@|" $(depfile).raw >> $(depfile) +# Fixup path as in (1).""" + + (r""" +sed -e "s|^$(notdir $@)|$@|" -re 's/\\\\([^$$])/\/\1/g' $(depfile).raw >> $(depfile)""" + if sys.platform == 'win32' else r""" +sed -e "s|^$(notdir $@)|$@|" $(depfile).raw >> $(depfile)""") + + r""" # Add extra rules as in (2). # We remove slashes and replace spaces with new lines; # remove blank lines; -# delete the first line and append a colon to the remaining lines. -sed -e 's|\\||' -e 'y| |\n|' $(depfile).raw |\ +# delete the first line and append a colon to the remaining lines.""" + + (""" +sed -e 's/\\\\\\\\$$//' -e 's/\\\\\\\\/\\//g' -e 'y| |\\n|' $(depfile).raw |\\""" + if sys.platform == 'win32' else """ +sed -e 's|\\\\||' -e 'y| |\\n|' $(depfile).raw |\\""") + + r""" grep -v '^$$' |\ sed -e 1d -e 's|$$|:|' \ >> $(depfile) @@ -724,6 +733,10 @@ def QuoteIfNecessary(string): string = '"' + string.replace('"', '\\"') + '"' return string +def replace_sep(string): + if sys.platform == 'win32': + string = string.replace('\\\\', '/').replace('\\', '/') + return string def StringToMakefileVariable(string): """Convert a string to a value that is acceptable as a make variable name.""" @@ -859,7 +872,7 @@ def Write( self.output = self.ComputeMacBundleOutput(spec) self.output_binary = self.ComputeMacBundleBinaryOutput(spec) else: - self.output = self.output_binary = self.ComputeOutput(spec) + self.output = self.output_binary = replace_sep(self.ComputeOutput(spec)) self.is_standalone_static_library = bool( spec.get("standalone_static_library", 0) @@ -985,7 +998,7 @@ def WriteSubMake(self, output_filename, makefile_path, targets, build_dir): # sub-project dir (see test/subdirectory/gyptest-subdir-all.py). self.WriteLn( "export builddir_name ?= %s" - % os.path.join(os.path.dirname(output_filename), build_dir) + % replace_sep(os.path.join(os.path.dirname(output_filename), build_dir)) ) self.WriteLn(".PHONY: all") self.WriteLn("all:") @@ -2063,7 +2076,7 @@ def WriteList(self, value_list, variable=None, prefix="", quoter=QuoteIfNecessar """ values = "" if value_list: - value_list = [quoter(prefix + value) for value in value_list] + value_list = [replace_sep(quoter(prefix + value)) for value in value_list] values = " \\\n\t" + " \\\n\t".join(value_list) self.fp.write(f"{variable} :={values}\n\n") @@ -2369,10 +2382,12 @@ def WriteAutoRegenerationRule(params, root_makefile, makefile_name, build_files) "\t$(call do_cmd,regen_makefile)\n\n" % { "makefile_name": makefile_name, - "deps": " ".join(SourceifyAndQuoteSpaces(bf) for bf in build_files), - "cmd": gyp.common.EncodePOSIXShellList( - [gyp_binary, "-fmake"] + gyp.RegenerateFlags(options) + build_files_args + "deps": replace_sep( + " ".join(SourceifyAndQuoteSpaces(bf) for bf in build_files) ), + "cmd": replace_sep(gyp.common.EncodePOSIXShellList( + [gyp_binary, "-fmake"] + gyp.RegenerateFlags(options) + build_files_args + )), } ) @@ -2435,33 +2450,52 @@ def CalculateMakefilePath(build_file, base_name): makefile_path = os.path.join( options.toplevel_dir, options.generator_output, makefile_name ) - srcdir = gyp.common.RelativePath(srcdir, options.generator_output) + srcdir = replace_sep(gyp.common.RelativePath(srcdir, options.generator_output)) srcdir_prefix = "$(srcdir)/" flock_command = "flock" copy_archive_arguments = "-af" makedep_arguments = "-MMD" + + # wasm-ld doesn't support --start-group/--end-group + link_commands = LINK_COMMANDS_LINUX + if flavor in ["wasi", "wasm"]: + link_commands = link_commands.replace(' -Wl,--start-group', '').replace( + ' -Wl,--end-group', '' + ) + + CC_target = replace_sep(GetEnvironFallback(("CC_target", "CC"), "$(CC)")) + AR_target = replace_sep(GetEnvironFallback(("AR_target", "AR"), "$(AR)")) + CXX_target = replace_sep(GetEnvironFallback(("CXX_target", "CXX"), "$(CXX)")) + LINK_target = replace_sep(GetEnvironFallback(("LINK_target", "LINK"), "$(LINK)")) + PLI_target = replace_sep(GetEnvironFallback(("PLI_target", "PLI"), "pli")) + CC_host = replace_sep(GetEnvironFallback(("CC_host", "CC"), "gcc")) + AR_host = replace_sep(GetEnvironFallback(("AR_host", "AR"), "ar")) + CXX_host = replace_sep(GetEnvironFallback(("CXX_host", "CXX"), "g++")) + LINK_host = replace_sep(GetEnvironFallback(("LINK_host", "LINK"), "$(CXX.host)")) + PLI_host = replace_sep(GetEnvironFallback(("PLI_host", "PLI"), "pli")) + header_params = { "default_target": default_target, "builddir": builddir_name, "default_configuration": default_configuration, "flock": flock_command, "flock_index": 1, - "link_commands": LINK_COMMANDS_LINUX, + "link_commands": link_commands, "extra_commands": "", "srcdir": srcdir, "copy_archive_args": copy_archive_arguments, "makedep_args": makedep_arguments, - "CC.target": GetEnvironFallback(("CC_target", "CC"), "$(CC)"), - "AR.target": GetEnvironFallback(("AR_target", "AR"), "$(AR)"), - "CXX.target": GetEnvironFallback(("CXX_target", "CXX"), "$(CXX)"), - "LINK.target": GetEnvironFallback(("LINK_target", "LINK"), "$(LINK)"), - "PLI.target": GetEnvironFallback(("PLI_target", "PLI"), "pli"), - "CC.host": GetEnvironFallback(("CC_host", "CC"), "gcc"), - "AR.host": GetEnvironFallback(("AR_host", "AR"), "ar"), - "CXX.host": GetEnvironFallback(("CXX_host", "CXX"), "g++"), - "LINK.host": GetEnvironFallback(("LINK_host", "LINK"), "$(CXX.host)"), - "PLI.host": GetEnvironFallback(("PLI_host", "PLI"), "pli"), + "CC.target": CC_target, + "AR.target": AR_target, + "CXX.target": CXX_target, + "LINK.target": LINK_target, + "PLI.target": PLI_target, + "CC.host": CC_host, + "AR.host": AR_host, + "CXX.host": CXX_host, + "LINK.host": LINK_host, + "PLI.host": PLI_host, } if flavor == "mac": flock_command = "./gyp-mac-tool flock" diff --git a/tools/gyp/pylib/gyp/generator/msvs.py b/tools/gyp/pylib/gyp/generator/msvs.py index 13b0794b4dccc3..6b5b24acc00019 100644 --- a/tools/gyp/pylib/gyp/generator/msvs.py +++ b/tools/gyp/pylib/gyp/generator/msvs.py @@ -1778,11 +1778,9 @@ def _GetCopies(spec): outer_dir = posixpath.split(src_bare)[1] fixed_dst = _FixPath(dst) full_dst = f'"{fixed_dst}\\{outer_dir}\\"' - cmd = 'mkdir {} 2>nul & cd "{}" && xcopy /e /f /y "{}" {}'.format( - full_dst, - _FixPath(base_dir), - outer_dir, - full_dst, + cmd = ( + f'mkdir {full_dst} 2>nul & cd "{_FixPath(base_dir)}" ' + f'&& xcopy /e /f /y "{outer_dir}" {full_dst}' ) copies.append( ( @@ -1794,10 +1792,9 @@ def _GetCopies(spec): ) else: fix_dst = _FixPath(cpy["destination"]) - cmd = 'mkdir "{}" 2>nul & set ERRORLEVEL=0 & copy /Y "{}" "{}"'.format( - fix_dst, - _FixPath(src), - _FixPath(dst), + cmd = ( + f'mkdir "{fix_dst}" 2>nul & set ERRORLEVEL=0 & ' + f'copy /Y "{_FixPath(src)}" "{_FixPath(dst)}"' ) copies.append(([src], [dst], cmd, f"Copying {src} to {fix_dst}")) return copies @@ -1899,10 +1896,8 @@ def _GetPlatformOverridesOfProject(spec): for config_name, c in spec["configurations"].items(): config_fullname = _ConfigFullName(config_name, c) platform = c.get("msvs_target_platform", _ConfigPlatform(c)) - fixed_config_fullname = "{}|{}".format( - _ConfigBaseName(config_name, _ConfigPlatform(c)), - platform, - ) + base_name = _ConfigBaseName(config_name, _ConfigPlatform(c)) + fixed_config_fullname = f"{base_name}|{platform}" if spec["toolset"] == "host" and generator_supports_multiple_toolsets: fixed_config_fullname = f"{config_name}|x64" config_platform_overrides[config_fullname] = fixed_config_fullname diff --git a/tools/gyp/pylib/gyp/generator/ninja.py b/tools/gyp/pylib/gyp/generator/ninja.py index 8ba341e96d3f0d..0146c4996260a6 100644 --- a/tools/gyp/pylib/gyp/generator/ninja.py +++ b/tools/gyp/pylib/gyp/generator/ninja.py @@ -11,6 +11,7 @@ import os.path import re import signal +import shutil import subprocess import sys import gyp @@ -2210,6 +2211,7 @@ def GenerateOutputForConfig(target_list, target_dicts, data, params, config_name options = params["options"] flavor = gyp.common.GetFlavor(params) generator_flags = params.get("generator_flags", {}) + generate_compile_commands = generator_flags.get("compile_commands", False) # build_dir: relative path from source root to our output files. # e.g. "out/Debug" @@ -2878,6 +2880,35 @@ def GenerateOutputForConfig(target_list, target_dicts, data, params, config_name master_ninja_file.close() + if generate_compile_commands: + compile_db = GenerateCompileDBWithNinja(toplevel_build) + compile_db_file = OpenOutput( + os.path.join(toplevel_build, "compile_commands.json") + ) + compile_db_file.write(json.dumps(compile_db, indent=2)) + compile_db_file.close() + + +def GenerateCompileDBWithNinja(path, targets=["all"]): + """Generates a compile database using ninja. + + Args: + path: The build directory to generate a compile database for. + targets: Additional targets to pass to ninja. + + Returns: + List of the contents of the compile database. + """ + ninja_path = shutil.which("ninja") + if ninja_path is None: + raise Exception("ninja not found in PATH") + json_compile_db = subprocess.check_output( + [ninja_path, "-C", path] + + targets + + ["-t", "compdb", "cc", "cxx", "objc", "objcxx"] + ) + return json.loads(json_compile_db) + def PerformBuild(data, configurations, params): options = params["options"] diff --git a/tools/gyp/pylib/gyp/generator/ninja_test.py b/tools/gyp/pylib/gyp/generator/ninja_test.py index 7d180685b21cf9..15cddfdf2443bf 100644 --- a/tools/gyp/pylib/gyp/generator/ninja_test.py +++ b/tools/gyp/pylib/gyp/generator/ninja_test.py @@ -6,6 +6,7 @@ """ Unit tests for the ninja.py file. """ +from pathlib import Path import sys import unittest @@ -50,6 +51,17 @@ def test_BinaryNamesLinux(self): writer.ComputeOutputFileName(spec, "static_library").endswith(".a") ) + def test_GenerateCompileDBWithNinja(self): + build_dir = ( + Path(__file__).resolve().parent.parent.parent.parent / "data" / "ninja" + ) + compile_db = ninja.GenerateCompileDBWithNinja(build_dir) + assert len(compile_db) == 1 + assert compile_db[0]["directory"] == str(build_dir) + assert compile_db[0]["command"] == "cc my.in my.out" + assert compile_db[0]["file"] == "my.in" + assert compile_db[0]["output"] == "my.out" + if __name__ == "__main__": unittest.main() diff --git a/tools/gyp/pylib/gyp/input.py b/tools/gyp/pylib/gyp/input.py index 8f39519dee51fb..7150269cda585e 100644 --- a/tools/gyp/pylib/gyp/input.py +++ b/tools/gyp/pylib/gyp/input.py @@ -1135,18 +1135,16 @@ def EvalCondition(condition, conditions_key, phase, variables, build_file): true_dict = condition[i + 1] if type(true_dict) is not dict: raise GypError( - "{} {} must be followed by a dictionary, not {}".format( - conditions_key, cond_expr, type(true_dict) - ) + f"{conditions_key} {cond_expr} must be followed by a dictionary, " + f"not {type(true_dict)}" ) if len(condition) > i + 2 and type(condition[i + 2]) is dict: false_dict = condition[i + 2] i = i + 3 if i != len(condition): raise GypError( - "{} {} has {} unexpected trailing items".format( - conditions_key, cond_expr, len(condition) - i - ) + f"{conditions_key} {cond_expr} has " + f"{len(condition) - i} unexpected trailing items" ) else: false_dict = None @@ -2538,6 +2536,8 @@ def ProcessListFiltersInDict(name, the_dict): lists = [] del_lists = [] for key, value in the_dict.items(): + if not key: + continue operation = key[-1] if operation not in {"!", "/"}: continue diff --git a/tools/gyp/pylib/gyp/msvs_emulation.py b/tools/gyp/pylib/gyp/msvs_emulation.py index 38fa21dd666697..adda5a0273f8a6 100644 --- a/tools/gyp/pylib/gyp/msvs_emulation.py +++ b/tools/gyp/pylib/gyp/msvs_emulation.py @@ -830,17 +830,15 @@ def _GetLdManifestFlags( ("VCLinkerTool", "UACUIAccess"), config, default="false" ) - inner = """ + level = execution_level_map[execution_level] + inner = f""" - + -""".format( - execution_level_map[execution_level], - ui_access, - ) +""" else: inner = "" diff --git a/tools/gyp/pyproject.toml b/tools/gyp/pyproject.toml index fb87c8218ed911..d8680bdc0d32ee 100644 --- a/tools/gyp/pyproject.toml +++ b/tools/gyp/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "gyp-next" -version = "0.16.2" +version = "0.17.0" authors = [ { name="Node.js contributors", email="ryzokuken@disroot.org" }, ] @@ -29,7 +29,7 @@ classifiers = [ ] [project.optional-dependencies] -dev = ["flake8", "ruff == 0.3.0", "pytest"] +dev = ["flake8", "ruff == 0.4.2", "pytest"] [project.scripts] gyp = "gyp:script_main" @@ -38,6 +38,11 @@ gyp = "gyp:script_main" "Homepage" = "https://github.com/nodejs/gyp-next" [tool.ruff] +extend-exclude = ["pylib/packaging"] +line-length = 88 +target-version = "py37" + +[tool.ruff.lint] select = [ "C4", # flake8-comprehensions "C90", # McCabe cyclomatic complexity @@ -101,14 +106,11 @@ ignore = [ "RUF012", "UP031", ] -extend-exclude = ["pylib/packaging"] -line-length = 88 -target-version = "py37" -[tool.ruff.mccabe] +[tool.ruff.lint.mccabe] max-complexity = 101 -[tool.ruff.pylint] +[tool.ruff.lint.pylint] max-args = 11 max-branches = 108 max-returns = 10 diff --git a/tools/gyp/release-please-config.json b/tools/gyp/release-please-config.json new file mode 100644 index 00000000000000..b6cad32a2dd0e0 --- /dev/null +++ b/tools/gyp/release-please-config.json @@ -0,0 +1,11 @@ +{ + "last-release-sha": "78756421b0d7bb335992a9c7d26ba3cc8b619708", + "packages": { + ".": { + "release-type": "python", + "package-name": "gyp-next", + "bump-minor-pre-major": true, + "include-component-in-tag": false + } + } +}