951f08f0a6fe1f10d66e7497f202ba51579ecc4de1d709e834109138c2c4ef18de849472bce74da89e71c2b3e537599b892e9ea9c9854aedba7e3add8f8d4a 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright (c) 2016 Ben Noordhuis <info@bnoordhuis.nl>. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. import gyp.common
  5. import gyp.xcode_emulation
  6. import json
  7. import os
  8. generator_additional_non_configuration_keys = []
  9. generator_additional_path_sections = []
  10. generator_extra_sources_for_rules = []
  11. generator_filelist_paths = None
  12. generator_supports_multiple_toolsets = True
  13. generator_wants_sorted_dependencies = False
  14. # Lifted from make.py. The actual values don't matter much.
  15. generator_default_variables = {
  16. "CONFIGURATION_NAME": "$(BUILDTYPE)",
  17. "EXECUTABLE_PREFIX": "",
  18. "EXECUTABLE_SUFFIX": "",
  19. "INTERMEDIATE_DIR": "$(obj).$(TOOLSET)/$(TARGET)/geni",
  20. "PRODUCT_DIR": "$(builddir)",
  21. "RULE_INPUT_DIRNAME": "%(INPUT_DIRNAME)s",
  22. "RULE_INPUT_EXT": "$(suffix $<)",
  23. "RULE_INPUT_NAME": "$(notdir $<)",
  24. "RULE_INPUT_PATH": "$(abspath $<)",
  25. "RULE_INPUT_ROOT": "%(INPUT_ROOT)s",
  26. "SHARED_INTERMEDIATE_DIR": "$(obj)/gen",
  27. "SHARED_LIB_PREFIX": "lib",
  28. "STATIC_LIB_PREFIX": "lib",
  29. "STATIC_LIB_SUFFIX": ".a",
  30. }
  31. def IsMac(params):
  32. return gyp.common.GetFlavor(params) == "mac"
  33. def CalculateVariables(default_variables, params):
  34. default_variables.setdefault("OS", gyp.common.GetFlavor(params))
  35. def AddCommandsForTarget(cwd, target, params, per_config_commands):
  36. output_dir = params["generator_flags"].get("output_dir", "out")
  37. for configuration_name, configuration in target["configurations"].items():
  38. if IsMac(params):
  39. xcode_settings = gyp.xcode_emulation.XcodeSettings(target)
  40. cflags = xcode_settings.GetCflags(configuration_name)
  41. cflags_c = xcode_settings.GetCflagsC(configuration_name)
  42. cflags_cc = xcode_settings.GetCflagsCC(configuration_name)
  43. else:
  44. cflags = configuration.get("cflags", [])
  45. cflags_c = configuration.get("cflags_c", [])
  46. cflags_cc = configuration.get("cflags_cc", [])
  47. cflags_c = cflags + cflags_c
  48. cflags_cc = cflags + cflags_cc
  49. defines = configuration.get("defines", [])
  50. defines = ["-D" + s for s in defines]
  51. # TODO(bnoordhuis) Handle generated source files.
  52. extensions = (".c", ".cc", ".cpp", ".cxx")
  53. sources = [s for s in target.get("sources", []) if s.endswith(extensions)]
  54. def resolve(filename):
  55. return os.path.abspath(os.path.join(cwd, filename))
  56. # TODO(bnoordhuis) Handle generated header files.
  57. include_dirs = configuration.get("include_dirs", [])
  58. include_dirs = [s for s in include_dirs if not s.startswith("$(obj)")]
  59. includes = ["-I" + resolve(s) for s in include_dirs]
  60. defines = gyp.common.EncodePOSIXShellList(defines)
  61. includes = gyp.common.EncodePOSIXShellList(includes)
  62. cflags_c = gyp.common.EncodePOSIXShellList(cflags_c)
  63. cflags_cc = gyp.common.EncodePOSIXShellList(cflags_cc)
  64. commands = per_config_commands.setdefault(configuration_name, [])
  65. for source in sources:
  66. file = resolve(source)
  67. isc = source.endswith(".c")
  68. cc = "cc" if isc else "c++"
  69. cflags = cflags_c if isc else cflags_cc
  70. command = " ".join(
  71. (
  72. cc,
  73. defines,
  74. includes,
  75. cflags,
  76. "-c",
  77. gyp.common.EncodePOSIXShellArgument(file),
  78. )
  79. )
  80. commands.append({"command": command, "directory": output_dir, "file": file})
  81. def GenerateOutput(target_list, target_dicts, data, params):
  82. per_config_commands = {}
  83. for qualified_target, target in target_dicts.items():
  84. build_file, target_name, toolset = gyp.common.ParseQualifiedTarget(
  85. qualified_target
  86. )
  87. if IsMac(params):
  88. settings = data[build_file]
  89. gyp.xcode_emulation.MergeGlobalXcodeSettingsToSpec(settings, target)
  90. cwd = os.path.dirname(build_file)
  91. AddCommandsForTarget(cwd, target, params, per_config_commands)
  92. output_dir = None
  93. try:
  94. # generator_output can be `None` on Windows machines, or even not
  95. # defined in other cases
  96. output_dir = params.get("options").generator_output
  97. except AttributeError:
  98. pass
  99. output_dir = output_dir or params["generator_flags"].get("output_dir", "out")
  100. for configuration_name, commands in per_config_commands.items():
  101. filename = os.path.join(output_dir, configuration_name, "compile_commands.json")
  102. gyp.common.EnsureDirExists(filename)
  103. fp = open(filename, "w")
  104. json.dump(commands, fp=fp, indent=0, check_circular=False)
  105. def PerformBuild(data, configurations, params):
  106. pass