f51e8d7a369dba378fe8207bcfe25eca46384ca0c5fcf15959a54202479748241fba0fec9d37aada48cf5e9e418b096bd29e3373c47e79038eb0ff132733de 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. # Copyright (c) 2012 Google Inc. 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. """GYP backend that generates Eclipse CDT settings files.
  5. This backend DOES NOT generate Eclipse CDT projects. Instead, it generates XML
  6. files that can be imported into an Eclipse CDT project. The XML file contains a
  7. list of include paths and symbols (i.e. defines).
  8. Because a full .cproject definition is not created by this generator, it's not
  9. possible to properly define the include dirs and symbols for each file
  10. individually. Instead, one set of includes/symbols is generated for the entire
  11. project. This works fairly well (and is a vast improvement in general), but may
  12. still result in a few indexer issues here and there.
  13. This generator has no automated tests, so expect it to be broken.
  14. """
  15. from xml.sax.saxutils import escape
  16. import os.path
  17. import subprocess
  18. import gyp
  19. import gyp.common
  20. import gyp.msvs_emulation
  21. import shlex
  22. import xml.etree.ElementTree as ET
  23. generator_wants_static_library_dependencies_adjusted = False
  24. generator_default_variables = {}
  25. for dirname in ["INTERMEDIATE_DIR", "PRODUCT_DIR", "LIB_DIR", "SHARED_LIB_DIR"]:
  26. # Some gyp steps fail if these are empty(!), so we convert them to variables
  27. generator_default_variables[dirname] = "$" + dirname
  28. for unused in [
  29. "RULE_INPUT_PATH",
  30. "RULE_INPUT_ROOT",
  31. "RULE_INPUT_NAME",
  32. "RULE_INPUT_DIRNAME",
  33. "RULE_INPUT_EXT",
  34. "EXECUTABLE_PREFIX",
  35. "EXECUTABLE_SUFFIX",
  36. "STATIC_LIB_PREFIX",
  37. "STATIC_LIB_SUFFIX",
  38. "SHARED_LIB_PREFIX",
  39. "SHARED_LIB_SUFFIX",
  40. "CONFIGURATION_NAME",
  41. ]:
  42. generator_default_variables[unused] = ""
  43. # Include dirs will occasionally use the SHARED_INTERMEDIATE_DIR variable as
  44. # part of the path when dealing with generated headers. This value will be
  45. # replaced dynamically for each configuration.
  46. generator_default_variables["SHARED_INTERMEDIATE_DIR"] = "$SHARED_INTERMEDIATE_DIR"
  47. def CalculateVariables(default_variables, params):
  48. generator_flags = params.get("generator_flags", {})
  49. for key, val in generator_flags.items():
  50. default_variables.setdefault(key, val)
  51. flavor = gyp.common.GetFlavor(params)
  52. default_variables.setdefault("OS", flavor)
  53. if flavor == "win":
  54. gyp.msvs_emulation.CalculateCommonVariables(default_variables, params)
  55. def CalculateGeneratorInputInfo(params):
  56. """Calculate the generator specific info that gets fed to input (called by
  57. gyp)."""
  58. generator_flags = params.get("generator_flags", {})
  59. if generator_flags.get("adjust_static_libraries", False):
  60. global generator_wants_static_library_dependencies_adjusted
  61. generator_wants_static_library_dependencies_adjusted = True
  62. def GetAllIncludeDirectories(
  63. target_list,
  64. target_dicts,
  65. shared_intermediate_dirs,
  66. config_name,
  67. params,
  68. compiler_path,
  69. ):
  70. """Calculate the set of include directories to be used.
  71. Returns:
  72. A list including all the include_dir's specified for every target followed
  73. by any include directories that were added as cflag compiler options.
  74. """
  75. gyp_includes_set = set()
  76. compiler_includes_list = []
  77. # Find compiler's default include dirs.
  78. if compiler_path:
  79. command = shlex.split(compiler_path)
  80. command.extend(["-E", "-xc++", "-v", "-"])
  81. proc = subprocess.Popen(
  82. args=command,
  83. stdin=subprocess.PIPE,
  84. stdout=subprocess.PIPE,
  85. stderr=subprocess.PIPE,
  86. )
  87. output = proc.communicate()[1].decode("utf-8")
  88. # Extract the list of include dirs from the output, which has this format:
  89. # ...
  90. # #include "..." search starts here:
  91. # #include <...> search starts here:
  92. # /usr/include/c++/4.6
  93. # /usr/local/include
  94. # End of search list.
  95. # ...
  96. in_include_list = False
  97. for line in output.splitlines():
  98. if line.startswith("#include"):
  99. in_include_list = True
  100. continue
  101. if line.startswith("End of search list."):
  102. break
  103. if in_include_list:
  104. include_dir = line.strip()
  105. if include_dir not in compiler_includes_list:
  106. compiler_includes_list.append(include_dir)
  107. flavor = gyp.common.GetFlavor(params)
  108. if flavor == "win":
  109. generator_flags = params.get("generator_flags", {})
  110. for target_name in target_list:
  111. target = target_dicts[target_name]
  112. if config_name in target["configurations"]:
  113. config = target["configurations"][config_name]
  114. # Look for any include dirs that were explicitly added via cflags. This
  115. # may be done in gyp files to force certain includes to come at the end.
  116. # TODO(jgreenwald): Change the gyp files to not abuse cflags for this, and
  117. # remove this.
  118. if flavor == "win":
  119. msvs_settings = gyp.msvs_emulation.MsvsSettings(target, generator_flags)
  120. cflags = msvs_settings.GetCflags(config_name)
  121. else:
  122. cflags = config["cflags"]
  123. for cflag in cflags:
  124. if cflag.startswith("-I"):
  125. include_dir = cflag[2:]
  126. if include_dir not in compiler_includes_list:
  127. compiler_includes_list.append(include_dir)
  128. # Find standard gyp include dirs.
  129. if "include_dirs" in config:
  130. include_dirs = config["include_dirs"]
  131. for shared_intermediate_dir in shared_intermediate_dirs:
  132. for include_dir in include_dirs:
  133. include_dir = include_dir.replace(
  134. "$SHARED_INTERMEDIATE_DIR", shared_intermediate_dir
  135. )
  136. if not os.path.isabs(include_dir):
  137. base_dir = os.path.dirname(target_name)
  138. include_dir = base_dir + "/" + include_dir
  139. include_dir = os.path.abspath(include_dir)
  140. gyp_includes_set.add(include_dir)
  141. # Generate a list that has all the include dirs.
  142. all_includes_list = list(gyp_includes_set)
  143. all_includes_list.sort()
  144. for compiler_include in compiler_includes_list:
  145. if compiler_include not in gyp_includes_set:
  146. all_includes_list.append(compiler_include)
  147. # All done.
  148. return all_includes_list
  149. def GetCompilerPath(target_list, data, options):
  150. """Determine a command that can be used to invoke the compiler.
  151. Returns:
  152. If this is a gyp project that has explicit make settings, try to determine
  153. the compiler from that. Otherwise, see if a compiler was specified via the
  154. CC_target environment variable.
  155. """
  156. # First, see if the compiler is configured in make's settings.
  157. build_file, _, _ = gyp.common.ParseQualifiedTarget(target_list[0])
  158. make_global_settings_dict = data[build_file].get("make_global_settings", {})
  159. for key, value in make_global_settings_dict:
  160. if key in ["CC", "CXX"]:
  161. return os.path.join(options.toplevel_dir, value)
  162. # Check to see if the compiler was specified as an environment variable.
  163. for key in ["CC_target", "CC", "CXX"]:
  164. compiler = os.environ.get(key)
  165. if compiler:
  166. return compiler
  167. return "gcc"
  168. def GetAllDefines(target_list, target_dicts, data, config_name, params, compiler_path):
  169. """Calculate the defines for a project.
  170. Returns:
  171. A dict that includes explicit defines declared in gyp files along with all
  172. of the default defines that the compiler uses.
  173. """
  174. # Get defines declared in the gyp files.
  175. all_defines = {}
  176. flavor = gyp.common.GetFlavor(params)
  177. if flavor == "win":
  178. generator_flags = params.get("generator_flags", {})
  179. for target_name in target_list:
  180. target = target_dicts[target_name]
  181. if flavor == "win":
  182. msvs_settings = gyp.msvs_emulation.MsvsSettings(target, generator_flags)
  183. extra_defines = msvs_settings.GetComputedDefines(config_name)
  184. else:
  185. extra_defines = []
  186. if config_name in target["configurations"]:
  187. config = target["configurations"][config_name]
  188. target_defines = config["defines"]
  189. else:
  190. target_defines = []
  191. for define in target_defines + extra_defines:
  192. split_define = define.split("=", 1)
  193. if len(split_define) == 1:
  194. split_define.append("1")
  195. if split_define[0].strip() in all_defines:
  196. # Already defined
  197. continue
  198. all_defines[split_define[0].strip()] = split_define[1].strip()
  199. # Get default compiler defines (if possible).
  200. if flavor == "win":
  201. return all_defines # Default defines already processed in the loop above.
  202. if compiler_path:
  203. command = shlex.split(compiler_path)
  204. command.extend(["-E", "-dM", "-"])
  205. cpp_proc = subprocess.Popen(
  206. args=command, cwd=".", stdin=subprocess.PIPE, stdout=subprocess.PIPE
  207. )
  208. cpp_output = cpp_proc.communicate()[0].decode("utf-8")
  209. cpp_lines = cpp_output.split("\n")
  210. for cpp_line in cpp_lines:
  211. if not cpp_line.strip():
  212. continue
  213. cpp_line_parts = cpp_line.split(" ", 2)
  214. key = cpp_line_parts[1]
  215. val = cpp_line_parts[2] if len(cpp_line_parts) >= 3 else "1"
  216. all_defines[key] = val
  217. return all_defines
  218. def WriteIncludePaths(out, eclipse_langs, include_dirs):
  219. """Write the includes section of a CDT settings export file."""
  220. out.write(
  221. ' <section name="org.eclipse.cdt.internal.ui.wizards.'
  222. 'settingswizards.IncludePaths">\n'
  223. )
  224. out.write(' <language name="holder for library settings"></language>\n')
  225. for lang in eclipse_langs:
  226. out.write(' <language name="%s">\n' % lang)
  227. for include_dir in include_dirs:
  228. out.write(
  229. ' <includepath workspace_path="false">%s</includepath>\n'
  230. % include_dir
  231. )
  232. out.write(" </language>\n")
  233. out.write(" </section>\n")
  234. def WriteMacros(out, eclipse_langs, defines):
  235. """Write the macros section of a CDT settings export file."""
  236. out.write(
  237. ' <section name="org.eclipse.cdt.internal.ui.wizards.'
  238. 'settingswizards.Macros">\n'
  239. )
  240. out.write(' <language name="holder for library settings"></language>\n')
  241. for lang in eclipse_langs:
  242. out.write(' <language name="%s">\n' % lang)
  243. for key in sorted(defines):
  244. out.write(
  245. " <macro><name>%s</name><value>%s</value></macro>\n"
  246. % (escape(key), escape(defines[key]))
  247. )
  248. out.write(" </language>\n")
  249. out.write(" </section>\n")
  250. def GenerateOutputForConfig(target_list, target_dicts, data, params, config_name):
  251. options = params["options"]
  252. generator_flags = params.get("generator_flags", {})
  253. # build_dir: relative path from source root to our output files.
  254. # e.g. "out/Debug"
  255. build_dir = os.path.join(generator_flags.get("output_dir", "out"), config_name)
  256. toplevel_build = os.path.join(options.toplevel_dir, build_dir)
  257. # Ninja uses out/Debug/gen while make uses out/Debug/obj/gen as the
  258. # SHARED_INTERMEDIATE_DIR. Include both possible locations.
  259. shared_intermediate_dirs = [
  260. os.path.join(toplevel_build, "obj", "gen"),
  261. os.path.join(toplevel_build, "gen"),
  262. ]
  263. GenerateCdtSettingsFile(
  264. target_list,
  265. target_dicts,
  266. data,
  267. params,
  268. config_name,
  269. os.path.join(toplevel_build, "eclipse-cdt-settings.xml"),
  270. options,
  271. shared_intermediate_dirs,
  272. )
  273. GenerateClasspathFile(
  274. target_list,
  275. target_dicts,
  276. options.toplevel_dir,
  277. toplevel_build,
  278. os.path.join(toplevel_build, "eclipse-classpath.xml"),
  279. )
  280. def GenerateCdtSettingsFile(
  281. target_list,
  282. target_dicts,
  283. data,
  284. params,
  285. config_name,
  286. out_name,
  287. options,
  288. shared_intermediate_dirs,
  289. ):
  290. gyp.common.EnsureDirExists(out_name)
  291. with open(out_name, "w") as out:
  292. out.write('<?xml version="1.0" encoding="UTF-8"?>\n')
  293. out.write("<cdtprojectproperties>\n")
  294. eclipse_langs = [
  295. "C++ Source File",
  296. "C Source File",
  297. "Assembly Source File",
  298. "GNU C++",
  299. "GNU C",
  300. "Assembly",
  301. ]
  302. compiler_path = GetCompilerPath(target_list, data, options)
  303. include_dirs = GetAllIncludeDirectories(
  304. target_list,
  305. target_dicts,
  306. shared_intermediate_dirs,
  307. config_name,
  308. params,
  309. compiler_path,
  310. )
  311. WriteIncludePaths(out, eclipse_langs, include_dirs)
  312. defines = GetAllDefines(
  313. target_list, target_dicts, data, config_name, params, compiler_path
  314. )
  315. WriteMacros(out, eclipse_langs, defines)
  316. out.write("</cdtprojectproperties>\n")
  317. def GenerateClasspathFile(
  318. target_list, target_dicts, toplevel_dir, toplevel_build, out_name
  319. ):
  320. """Generates a classpath file suitable for symbol navigation and code
  321. completion of Java code (such as in Android projects) by finding all
  322. .java and .jar files used as action inputs."""
  323. gyp.common.EnsureDirExists(out_name)
  324. result = ET.Element("classpath")
  325. def AddElements(kind, paths):
  326. # First, we need to normalize the paths so they are all relative to the
  327. # toplevel dir.
  328. rel_paths = set()
  329. for path in paths:
  330. if os.path.isabs(path):
  331. rel_paths.add(os.path.relpath(path, toplevel_dir))
  332. else:
  333. rel_paths.add(path)
  334. for path in sorted(rel_paths):
  335. entry_element = ET.SubElement(result, "classpathentry")
  336. entry_element.set("kind", kind)
  337. entry_element.set("path", path)
  338. AddElements("lib", GetJavaJars(target_list, target_dicts, toplevel_dir))
  339. AddElements("src", GetJavaSourceDirs(target_list, target_dicts, toplevel_dir))
  340. # Include the standard JRE container and a dummy out folder
  341. AddElements("con", ["org.eclipse.jdt.launching.JRE_CONTAINER"])
  342. # Include a dummy out folder so that Eclipse doesn't use the default /bin
  343. # folder in the root of the project.
  344. AddElements("output", [os.path.join(toplevel_build, ".eclipse-java-build")])
  345. ET.ElementTree(result).write(out_name)
  346. def GetJavaJars(target_list, target_dicts, toplevel_dir):
  347. """Generates a sequence of all .jars used as inputs."""
  348. for target_name in target_list:
  349. target = target_dicts[target_name]
  350. for action in target.get("actions", []):
  351. for input_ in action["inputs"]:
  352. if os.path.splitext(input_)[1] == ".jar" and not input_.startswith("$"):
  353. if os.path.isabs(input_):
  354. yield input_
  355. else:
  356. yield os.path.join(os.path.dirname(target_name), input_)
  357. def GetJavaSourceDirs(target_list, target_dicts, toplevel_dir):
  358. """Generates a sequence of all likely java package root directories."""
  359. for target_name in target_list:
  360. target = target_dicts[target_name]
  361. for action in target.get("actions", []):
  362. for input_ in action["inputs"]:
  363. if os.path.splitext(input_)[1] == ".java" and not input_.startswith(
  364. "$"
  365. ):
  366. dir_ = os.path.dirname(
  367. os.path.join(os.path.dirname(target_name), input_)
  368. )
  369. # If there is a parent 'src' or 'java' folder, navigate up to it -
  370. # these are canonical package root names in Chromium. This will
  371. # break if 'src' or 'java' exists in the package structure. This
  372. # could be further improved by inspecting the java file for the
  373. # package name if this proves to be too fragile in practice.
  374. parent_search = dir_
  375. while os.path.basename(parent_search) not in ["src", "java"]:
  376. parent_search, _ = os.path.split(parent_search)
  377. if not parent_search or parent_search == toplevel_dir:
  378. # Didn't find a known root, just return the original path
  379. yield dir_
  380. break
  381. else:
  382. yield parent_search
  383. def GenerateOutput(target_list, target_dicts, data, params):
  384. """Generate an XML settings file that can be imported into a CDT project."""
  385. if params["options"].generator_output:
  386. raise NotImplementedError("--generator_output not implemented for eclipse")
  387. user_config = params.get("generator_flags", {}).get("config", None)
  388. if user_config:
  389. GenerateOutputForConfig(target_list, target_dicts, data, params, user_config)
  390. else:
  391. config_names = target_dicts[target_list[0]]["configurations"]
  392. for config_name in config_names:
  393. GenerateOutputForConfig(
  394. target_list, target_dicts, data, params, config_name
  395. )