MCPcopy Create free account
hub / github.com/nodejs/node / GetCompilerPredefines

Function GetCompilerPredefines

tools/gyp/pylib/gyp/common.py:422–482  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

420
421
422def GetCompilerPredefines(): # -> dict
423 cmd = []
424 defines = {}
425
426 # shlex.split() will eat '\' in posix mode, but
427 # setting posix=False will preserve extra '"' cause CreateProcess fail on Windows
428 # this makes '\' in %CC_target% and %CFLAGS% work
429 def replace_sep(s):
430 return s.replace(os.sep, "/") if os.sep != "/" else s
431
432 if CC := os.environ.get("CC_target") or os.environ.get("CC"):
433 cmd += shlex.split(replace_sep(CC))
434 if CFLAGS := os.environ.get("CFLAGS"):
435 cmd += shlex.split(replace_sep(CFLAGS))
436 elif CXX := os.environ.get("CXX_target") or os.environ.get("CXX"):
437 cmd += shlex.split(replace_sep(CXX))
438 if CXXFLAGS := os.environ.get("CXXFLAGS"):
439 cmd += shlex.split(replace_sep(CXXFLAGS))
440 else:
441 return defines
442
443 if sys.platform == "win32":
444 fd, input = tempfile.mkstemp(suffix=".c")
445 real_cmd = [*cmd, "-dM", "-E", "-x", "c", input]
446 try:
447 os.close(fd)
448 stdout = subprocess.run(
449 real_cmd, shell=True, capture_output=True, check=True
450 ).stdout
451 except subprocess.CalledProcessError as e:
452 print(
453 "Warning: failed to get compiler predefines\n"
454 "cmd: %s\n"
455 "status: %d" % (e.cmd, e.returncode),
456 file=sys.stderr,
457 )
458 return defines
459 finally:
460 os.unlink(input)
461 else:
462 input = "/dev/null"
463 real_cmd = [*cmd, "-dM", "-E", "-x", "c", input]
464 try:
465 stdout = subprocess.run(
466 real_cmd, shell=False, capture_output=True, check=True
467 ).stdout
468 except subprocess.CalledProcessError as e:
469 print(
470 "Warning: failed to get compiler predefines\n"
471 "cmd: %s\n"
472 "status: %d" % (e.cmd, e.returncode),
473 file=sys.stderr,
474 )
475 return defines
476
477 lines = stdout.decode("utf-8").replace("\r\n", "\n").split("\n")
478 for line in lines:
479 if (line or "").startswith("#define "):

Callers 1

GetFlavorFunction · 0.85

Calls 9

replace_sepFunction · 0.70
getMethod · 0.65
closeMethod · 0.65
decodeMethod · 0.65
printFunction · 0.50
splitMethod · 0.45
runMethod · 0.45
unlinkMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected