gn_to_cmakelists.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. #!/usr/bin/env -S python3 -B
  2. #
  3. # Copyright (c) 2021-2021 Project CHIP Authors.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. ##
  18. # @file
  19. # Generates CMakeLists based on gn json output.
  20. # Converts json build instructions (see https://gn.googlesource.com/gn/+/HEAD/docs/reference.md#ide-options)
  21. # into CMakeLists.txt compatible build instructions
  22. #
  23. # Usage: gn_to_cmakelists.py <json_file_name>
  24. # gn gen out/config --ide=json --json-ide-script=//scripts/examples/gn_to_cmakelists.py
  25. # or
  26. # gn gen out/config --ide=json
  27. # python gn/gn_to_cmakelists.py out/project.json
  28. #
  29. import functools
  30. import itertools
  31. import json
  32. import os
  33. import posixpath
  34. import string
  35. import sys
  36. def CMakeStringEscape(a):
  37. """Escapes the string 'a' for use inside a CMake string.
  38. This means escaping
  39. '\' otherwise it may be seen as modifying the next character
  40. '"' otherwise it will end the string
  41. ';' otherwise the string becomes a list
  42. The following do not need to be escaped
  43. '#' when the lexer is in string state, this does not start a comment
  44. """
  45. return a.replace('\\', '\\\\').replace(';', '\\;').replace('"', '\\"')
  46. def CMakeDoubleStringEscape(a):
  47. """Escapes the string 'a' for use inside a CMake {{response_file_contents}} string.
  48. """
  49. return a.replace('"', '\\\\\\"')
  50. def CMakeTargetEscape(a):
  51. """Escapes the string 'a' for use as a CMake target name.
  52. CMP0037 in CMake 3.0 restricts target names to "^[A-Za-z0-9_.:+-]+$"
  53. The ':' is only allowed for imported targets.
  54. """
  55. def Escape(c):
  56. if c in string.ascii_letters or c in string.digits or c in '_.+-':
  57. return c
  58. else:
  59. return '__'
  60. return ''.join(map(Escape, a))
  61. def RemoveByPrefix(list, prefixs):
  62. ret = list
  63. for pre in prefixs:
  64. ret = [x for x in ret if not x.startswith(pre)]
  65. return ret
  66. def SetVariable(out, variable_name, value):
  67. """Sets a CMake variable."""
  68. out.write('set("')
  69. out.write(CMakeStringEscape(variable_name))
  70. out.write('" "')
  71. out.write(CMakeStringEscape(value))
  72. out.write('")\n')
  73. def SetVariableList(out, variable_name, values):
  74. """Sets a CMake variable to a list."""
  75. if not values:
  76. SetVariable(out, variable_name, "")
  77. return
  78. if len(values) == 1:
  79. SetVariable(out, variable_name, values[0])
  80. return
  81. out.write('list(APPEND "')
  82. out.write(CMakeStringEscape(variable_name))
  83. out.write('"\n "')
  84. out.write('"\n "'.join([CMakeStringEscape(value) for value in values]))
  85. out.write('")\n')
  86. def SetFilesProperty(output, variable, property_name, values, sep):
  87. """Given a set of source files, sets the given property on them."""
  88. output.write('set_source_files_properties(')
  89. WriteVariable(output, variable)
  90. output.write(' PROPERTIES ')
  91. output.write(property_name)
  92. output.write(' "')
  93. for value in values:
  94. output.write(CMakeStringEscape(value))
  95. output.write(sep)
  96. output.write('")\n')
  97. def SetCurrentTargetProperty(out, property_name, values, sep=''):
  98. """Given a target, sets the given property."""
  99. out.write('set_target_properties("${target}" PROPERTIES ')
  100. out.write(property_name)
  101. out.write(' "')
  102. for value in values:
  103. out.write(CMakeStringEscape(value))
  104. out.write(sep)
  105. out.write('")\n')
  106. def WriteVariable(output, variable_name, prepend=None):
  107. if prepend:
  108. output.write(prepend)
  109. output.write('${')
  110. output.write(variable_name)
  111. output.write('}')
  112. # See GetSourceFileType in gn
  113. source_file_types = {
  114. '.cc': 'cxx',
  115. '.cpp': 'cxx',
  116. '.cxx': 'cxx',
  117. '.m': 'objc',
  118. '.mm': 'objcc',
  119. '.c': 'c',
  120. '.s': 'asm',
  121. '.S': 'asm',
  122. '.asm': 'asm',
  123. '.o': 'obj',
  124. '.obj': 'obj',
  125. }
  126. class CMakeTargetType(object):
  127. def __init__(self, command, modifier, property_modifier, is_linkable):
  128. self.command = command
  129. self.modifier = modifier
  130. self.property_modifier = property_modifier
  131. self.is_linkable = is_linkable
  132. CMakeTargetType.custom = CMakeTargetType('add_custom_target', 'SOURCES',
  133. None, False)
  134. # See GetStringForOutputType in gn
  135. cmake_target_types = {
  136. 'unknown': CMakeTargetType.custom,
  137. 'group': CMakeTargetType.custom,
  138. 'executable': CMakeTargetType('add_executable', None, 'RUNTIME', True),
  139. 'loadable_module': CMakeTargetType('add_library', 'MODULE', 'LIBRARY', True),
  140. 'shared_library': CMakeTargetType('add_library', 'SHARED', 'LIBRARY', True),
  141. 'static_library': CMakeTargetType('add_library', 'STATIC', 'ARCHIVE', True),
  142. 'source_set': CMakeTargetType('add_library', 'OBJECT', None, False),
  143. 'copy': CMakeTargetType.custom,
  144. 'action': CMakeTargetType.custom,
  145. 'action_foreach': CMakeTargetType.custom,
  146. 'bundle_data': CMakeTargetType.custom,
  147. 'create_bundle': CMakeTargetType.custom,
  148. 'generated_file': CMakeTargetType.custom,
  149. }
  150. def FindFirstOf(s, a):
  151. return min(s.find(i) for i in a if i in s)
  152. class Project(object):
  153. def __init__(self, project_json):
  154. self.targets = project_json['targets']
  155. build_settings = project_json['build_settings']
  156. self.root_path = build_settings['root_path']
  157. self.build_path = self.GetAbsolutePath(build_settings['build_dir'])
  158. def GetAbsolutePath(self, path):
  159. if path.startswith('//'):
  160. return posixpath.join(self.root_path, path[2:])
  161. else:
  162. return path
  163. def GetObjectSourceDependencies(self, gn_target_name, object_dependencies):
  164. """All OBJECT libraries whose sources have not been absorbed."""
  165. dependencies = self.targets[gn_target_name].get('deps', [])
  166. for dependency in dependencies:
  167. dependency_type = self.targets[dependency].get('type', None)
  168. if dependency_type == 'source_set':
  169. object_dependencies.add(dependency)
  170. if dependency_type not in gn_target_types_that_absorb_objects:
  171. self.GetObjectSourceDependencies(
  172. dependency, object_dependencies)
  173. def GetObjectLibraryDependencies(self, gn_target_name, object_dependencies):
  174. """All OBJECT libraries whose libraries have not been absorbed."""
  175. dependencies = self.targets[gn_target_name].get('deps', [])
  176. for dependency in dependencies:
  177. dependency_type = self.targets[dependency].get('type', None)
  178. if dependency_type == 'source_set':
  179. object_dependencies.add(dependency)
  180. self.GetObjectLibraryDependencies(
  181. dependency, object_dependencies)
  182. def GetCMakeTargetName(self, gn_target_name):
  183. # See <chromium>/src/tools/gn/label.cc#Resolve
  184. # //base/test:test_support(//build/toolchain/win:msvc)
  185. path_separator = FindFirstOf(gn_target_name, (':', '('))
  186. location = None
  187. name = None
  188. toolchain = None
  189. if not path_separator:
  190. location = gn_target_name[2:]
  191. else:
  192. location = gn_target_name[2:path_separator]
  193. toolchain_separator = gn_target_name.find('(', path_separator)
  194. if toolchain_separator == -1:
  195. name = gn_target_name[path_separator + 1:]
  196. else:
  197. if toolchain_separator > path_separator:
  198. name = gn_target_name[path_separator +
  199. 1:toolchain_separator]
  200. assert gn_target_name.endswith(')')
  201. toolchain = gn_target_name[toolchain_separator + 1:-1]
  202. assert location or name
  203. cmake_target_name = None
  204. if location.endswith('/' + name):
  205. cmake_target_name = location
  206. elif location:
  207. cmake_target_name = location + '_' + name
  208. else:
  209. cmake_target_name = name
  210. if toolchain:
  211. cmake_target_name += '--' + toolchain
  212. return CMakeTargetEscape(cmake_target_name)
  213. class Target(object):
  214. def __init__(self, gn_target_name, project):
  215. self.gn_name = gn_target_name
  216. self.properties = project.targets[self.gn_name]
  217. self.cmake_name = project.GetCMakeTargetName(self.gn_name)
  218. self.gn_type = self.properties.get('type', None)
  219. self.cmake_type = cmake_target_types.get(self.gn_type, None)
  220. def WriteAction(out, target, project, sources, synthetic_dependencies):
  221. outputs = []
  222. output_directories = set()
  223. for output in target.properties.get('outputs', []):
  224. output_abs_path = project.GetAbsolutePath(output)
  225. outputs.append(output_abs_path)
  226. output_directory = posixpath.dirname(output_abs_path)
  227. if output_directory:
  228. output_directories.add(output_directory)
  229. outputs_name = '${target}__output'
  230. SetVariableList(out, outputs_name, outputs)
  231. response_file_contents = target.properties.get(
  232. 'response_file_contents', [])
  233. if len(response_file_contents) > 0:
  234. out.write('file(WRITE ${PROJECT_BINARY_DIR}/${target}_tmp "')
  235. out.write(' '.join(map(CMakeDoubleStringEscape, response_file_contents)))
  236. out.write('")\n')
  237. out.write('add_custom_command(OUTPUT ')
  238. WriteVariable(out, outputs_name)
  239. out.write('\n')
  240. if output_directories:
  241. out.write(' COMMAND ${CMAKE_COMMAND} -E make_directory "')
  242. out.write('" "'.join(map(CMakeStringEscape, output_directories)))
  243. out.write('"\n')
  244. script = target.properties['script']
  245. arguments = target.properties['args']
  246. arguments = [
  247. x if x != "{{response_file_name}}" else "${PROJECT_BINARY_DIR}/${target}_tmp" for x in arguments]
  248. out.write(' COMMAND python3 "')
  249. out.write(CMakeStringEscape(project.GetAbsolutePath(script)))
  250. out.write('"')
  251. if arguments:
  252. out.write('\n "')
  253. out.write('"\n "'.join(map(CMakeStringEscape, arguments)))
  254. out.write('"')
  255. out.write('\n')
  256. out.write(' DEPENDS ')
  257. for sources_type_name in sources.values():
  258. WriteVariable(out, sources_type_name, ' ')
  259. out.write('\n')
  260. # TODO: CMake 3.7 is introducing DEPFILE
  261. out.write(' WORKING_DIRECTORY "')
  262. out.write(CMakeStringEscape(project.build_path))
  263. out.write('"\n')
  264. out.write(' COMMENT "Action: ${target}"\n')
  265. out.write(' VERBATIM)\n')
  266. synthetic_dependencies.add(outputs_name)
  267. def ExpandPlaceholders(source, a):
  268. source_dir, source_file_part = posixpath.split(source)
  269. source_name_part, _ = posixpath.splitext(source_file_part)
  270. # TODO: {{source_gen_dir}}, {{source_out_dir}}, {{response_file_name}}
  271. return a.replace('{{source}}', source) \
  272. .replace('{{source_file_part}}', source_file_part) \
  273. .replace('{{source_name_part}}', source_name_part) \
  274. .replace('{{source_dir}}', source_dir) \
  275. .replace('{{source_root_relative_dir}}', source_dir)
  276. def WriteActionForEach(out, target, project, sources, synthetic_dependencies):
  277. all_outputs = target.properties.get('outputs', [])
  278. inputs = target.properties.get('sources', [])
  279. # TODO: consider expanding 'output_patterns' instead.
  280. outputs_per_input = len(all_outputs) / len(inputs)
  281. for count, source in enumerate(inputs):
  282. source_abs_path = project.GetAbsolutePath(source)
  283. outputs = []
  284. output_directories = set()
  285. for output in all_outputs[int(outputs_per_input * count):
  286. int(outputs_per_input * (count+1))]:
  287. output_abs_path = project.GetAbsolutePath(output)
  288. outputs.append(output_abs_path)
  289. output_directory = posixpath.dirname(output_abs_path)
  290. if output_directory:
  291. output_directories.add(output_directory)
  292. outputs_name = '${target}__output_' + str(count)
  293. SetVariableList(out, outputs_name, outputs)
  294. out.write('add_custom_command(OUTPUT ')
  295. WriteVariable(out, outputs_name)
  296. out.write('\n')
  297. if output_directories:
  298. out.write(' COMMAND ${CMAKE_COMMAND} -E make_directory "')
  299. out.write('" "'.join(map(CMakeStringEscape, output_directories)))
  300. out.write('"\n')
  301. script = target.properties['script']
  302. # TODO: need to expand {{xxx}} in arguments
  303. arguments = target.properties['args']
  304. out.write(' COMMAND python3 "')
  305. out.write(CMakeStringEscape(project.GetAbsolutePath(script)))
  306. out.write('"')
  307. if arguments:
  308. out.write('\n "')
  309. expand = functools.partial(ExpandPlaceholders, source_abs_path)
  310. out.write('"\n "'.join(
  311. map(CMakeStringEscape, map(expand, arguments))))
  312. out.write('"')
  313. out.write('\n')
  314. out.write(' DEPENDS')
  315. if 'input' in sources:
  316. WriteVariable(out, sources['input'], ' ')
  317. out.write(' "')
  318. out.write(CMakeStringEscape(source_abs_path))
  319. out.write('"\n')
  320. # TODO: CMake 3.7 is introducing DEPFILE
  321. out.write(' WORKING_DIRECTORY "')
  322. out.write(CMakeStringEscape(project.build_path))
  323. out.write('"\n')
  324. out.write(' COMMENT "Action ${target} on ')
  325. out.write(CMakeStringEscape(source_abs_path))
  326. out.write('"\n')
  327. out.write(' VERBATIM)\n')
  328. synthetic_dependencies.add(outputs_name)
  329. def WriteCopy(out, target, project, sources, synthetic_dependencies):
  330. inputs = target.properties.get('sources', [])
  331. raw_outputs = target.properties.get('outputs', [])
  332. # TODO: consider expanding 'output_patterns' instead.
  333. outputs = []
  334. for output in raw_outputs:
  335. output_abs_path = project.GetAbsolutePath(output)
  336. outputs.append(output_abs_path)
  337. outputs_name = '${target}__output'
  338. SetVariableList(out, outputs_name, outputs)
  339. out.write('add_custom_command(OUTPUT ')
  340. WriteVariable(out, outputs_name)
  341. out.write('\n')
  342. for src, dst in zip(inputs, outputs):
  343. abs_src_path = CMakeStringEscape(project.GetAbsolutePath(src))
  344. # CMake distinguishes between copying files and copying directories but
  345. # gn does not. We assume if the src has a period in its name then it is
  346. # a file and otherwise a directory.
  347. if "." in os.path.basename(abs_src_path):
  348. out.write(' COMMAND ${CMAKE_COMMAND} -E copy "')
  349. else:
  350. out.write(' COMMAND ${CMAKE_COMMAND} -E copy_directory "')
  351. out.write(abs_src_path)
  352. out.write('" "')
  353. out.write(CMakeStringEscape(dst))
  354. out.write('"\n')
  355. out.write(' DEPENDS ')
  356. for sources_type_name in sources.values():
  357. WriteVariable(out, sources_type_name, ' ')
  358. out.write('\n')
  359. out.write(' WORKING_DIRECTORY "')
  360. out.write(CMakeStringEscape(project.build_path))
  361. out.write('"\n')
  362. out.write(' COMMENT "Copy ${target}"\n')
  363. out.write(' VERBATIM)\n')
  364. synthetic_dependencies.add(outputs_name)
  365. def WriteCompilerFlags(out, target, project, sources):
  366. # Hack, set linker language to c if no c or cxx files present.
  367. if 'c' not in sources and 'cxx' not in sources:
  368. SetCurrentTargetProperty(out, 'LINKER_LANGUAGE', ['C'])
  369. # Mark uncompiled sources as uncompiled.
  370. if 'input' in sources:
  371. SetFilesProperty(out, sources['input'],
  372. 'HEADER_FILE_ONLY', ('True',), '')
  373. if 'other' in sources:
  374. SetFilesProperty(out, sources['other'],
  375. 'HEADER_FILE_ONLY', ('True',), '')
  376. # Mark object sources as linkable.
  377. if 'obj' in sources:
  378. SetFilesProperty(out, sources['obj'], 'EXTERNAL_OBJECT', ('True',), '')
  379. # TODO: 'output_name', 'output_dir', 'output_extension'
  380. # This includes using 'source_outputs' to direct compiler output.
  381. # should not use output_dir, or library will not in android studio folders
  382. output_name = target.properties.get('output_name', [])
  383. if output_name:
  384. out.write(
  385. 'set_property(TARGET "${target}" PROPERTY OUTPUT_NAME "')
  386. out.write(output_name)
  387. out.write('")\n')
  388. out.write('set_property(TARGET "${target}" PROPERTY PREFIX "")\n')
  389. # Includes
  390. includes = target.properties.get('include_dirs', [])
  391. if includes:
  392. out.write('set_property(TARGET "${target}" ')
  393. out.write('APPEND PROPERTY INCLUDE_DIRECTORIES')
  394. for include_dir in includes:
  395. out.write('\n "')
  396. out.write(project.GetAbsolutePath(include_dir))
  397. out.write('"')
  398. out.write(')\n')
  399. # Defines
  400. defines = target.properties.get('defines', [])
  401. if defines:
  402. SetCurrentTargetProperty(out, 'COMPILE_DEFINITIONS', defines, ';')
  403. # Compile flags
  404. # "arflags", "asmflags", "cflags",
  405. # "cflags_c", "clfags_cc", "cflags_objc", "clfags_objcc"
  406. # CMake does not have per target lang compile flags.
  407. # TODO: $<$<COMPILE_LANGUAGE:CXX>:cflags_cc style generator expression.
  408. # http://public.kitware.com/Bug/view.php?id=14857
  409. # -march flag is gnerated by android studio, remove it here
  410. blockPrefixFlags = ["-march="]
  411. flags = []
  412. flags.extend(target.properties.get('cflags', []))
  413. flags = RemoveByPrefix(flags, blockPrefixFlags)
  414. cflags_asm = target.properties.get('asmflags', [])
  415. cflags_asm = RemoveByPrefix(cflags_asm, blockPrefixFlags)
  416. cflags_c = target.properties.get('cflags_c', [])
  417. cflags_c = RemoveByPrefix(cflags_c, blockPrefixFlags)
  418. cflags_cxx = target.properties.get('cflags_cc', [])
  419. cflags_cxx = RemoveByPrefix(cflags_cxx, blockPrefixFlags)
  420. cflags_objc = cflags_c[:]
  421. cflags_objc.extend(target.properties.get('cflags_objc', []))
  422. cflags_objc = RemoveByPrefix(cflags_objc, blockPrefixFlags)
  423. cflags_objcc = cflags_cxx[:]
  424. cflags_objcc.extend(target.properties.get('cflags_objcc', []))
  425. cflags_objcc = RemoveByPrefix(cflags_objcc, blockPrefixFlags)
  426. if 'c' in sources and not any(k in sources for k in ('asm', 'cxx', 'objc', 'objcc')):
  427. flags.extend(cflags_c)
  428. elif 'cxx' in sources and not any(k in sources for k in ('asm', 'c', 'objc', 'objcc')):
  429. flags.extend(cflags_cxx)
  430. elif 'objc' in sources and not any(k in sources for k in ('asm', 'c', 'cxx', 'objcc')):
  431. flags.extend(cflags_objc)
  432. elif 'objcc' in sources and not any(k in sources for k in ('asm', 'c', 'cxx', 'objc')):
  433. flags.extend(cflags_objcc)
  434. else:
  435. # TODO: This is broken, one cannot generally set properties on files,
  436. # as other targets may require different properties on the same files.
  437. if 'asm' in sources and cflags_asm:
  438. SetFilesProperty(out, sources['asm'],
  439. 'COMPILE_FLAGS', cflags_asm, ' ')
  440. if 'c' in sources and cflags_c:
  441. SetFilesProperty(out, sources['c'], 'COMPILE_FLAGS', cflags_c, ' ')
  442. if 'cxx' in sources and cflags_cxx:
  443. SetFilesProperty(out, sources['cxx'],
  444. 'COMPILE_FLAGS', cflags_cxx, ' ')
  445. if 'objc' in sources and cflags_objc:
  446. SetFilesProperty(out, sources['objc'],
  447. 'COMPILE_FLAGS', cflags_objc, ' ')
  448. if 'objcc' in sources and cflags_objcc:
  449. SetFilesProperty(out, sources['objcc'],
  450. 'COMPILE_FLAGS', cflags_objcc, ' ')
  451. if flags:
  452. SetCurrentTargetProperty(out, 'COMPILE_FLAGS', flags, ' ')
  453. # Linker flags
  454. ldflags = target.properties.get('ldflags', [])
  455. ldflags = RemoveByPrefix(ldflags, blockPrefixFlags)
  456. if ldflags:
  457. SetCurrentTargetProperty(out, 'LINK_FLAGS', ldflags, ' ')
  458. gn_target_types_that_absorb_objects = (
  459. 'executable',
  460. 'loadable_module',
  461. 'shared_library',
  462. 'static_library'
  463. )
  464. def WriteSourceVariables(out, target, project):
  465. # gn separates the sheep from the goats based on file extensions.
  466. # A full separation is done here because of flag handing (see Compile flags).
  467. source_types = {'cxx': [], 'c': [], 'asm': [], 'objc': [], 'objcc': [],
  468. 'obj': [], 'obj_target': [], 'input': [], 'other': []}
  469. all_sources = target.properties.get('sources', [])
  470. # As of cmake 3.11 add_library must have sources. If there are
  471. # no sources, add empty.cpp as the file to compile.
  472. if len(all_sources) == 0:
  473. all_sources.append(posixpath.join(project.build_path, 'empty.cpp'))
  474. # TODO .def files on Windows
  475. for source in all_sources:
  476. _, ext = posixpath.splitext(source)
  477. source_abs_path = project.GetAbsolutePath(source)
  478. source_types[source_file_types.get(
  479. ext, 'other')].append(source_abs_path)
  480. for input_path in target.properties.get('inputs', []):
  481. input_abs_path = project.GetAbsolutePath(input_path)
  482. source_types['input'].append(input_abs_path)
  483. # OBJECT library dependencies need to be listed as sources.
  484. # Only executables and non-OBJECT libraries may reference an OBJECT library.
  485. # https://gitlab.kitware.com/cmake/cmake/issues/14778
  486. if target.gn_type in gn_target_types_that_absorb_objects:
  487. object_dependencies = set()
  488. project.GetObjectSourceDependencies(
  489. target.gn_name, object_dependencies)
  490. for dependency in object_dependencies:
  491. cmake_dependency_name = project.GetCMakeTargetName(dependency)
  492. obj_target_sources = '$<TARGET_OBJECTS:' + cmake_dependency_name + '>'
  493. source_types['obj_target'].append(obj_target_sources)
  494. sources = {}
  495. for source_type, sources_of_type in source_types.items():
  496. if sources_of_type:
  497. sources[source_type] = '${target}__' + source_type + '_srcs'
  498. SetVariableList(out, sources[source_type], sources_of_type)
  499. return sources
  500. def WriteTarget(out, target, project):
  501. out.write('\n#')
  502. out.write(target.gn_name)
  503. out.write('\n')
  504. if target.cmake_type is None:
  505. print('Target %s has unknown target type %s, skipping.' %
  506. (target.gn_name, target.gn_type))
  507. return
  508. SetVariable(out, 'target', target.cmake_name)
  509. sources = WriteSourceVariables(out, target, project)
  510. synthetic_dependencies = set()
  511. if target.gn_type == 'action':
  512. WriteAction(out, target, project, sources, synthetic_dependencies)
  513. if target.gn_type == 'action_foreach':
  514. WriteActionForEach(out, target, project, sources,
  515. synthetic_dependencies)
  516. if target.gn_type == 'copy':
  517. WriteCopy(out, target, project, sources, synthetic_dependencies)
  518. out.write(target.cmake_type.command)
  519. out.write('("${target}"')
  520. if target.cmake_type.modifier is not None:
  521. out.write(' ')
  522. out.write(target.cmake_type.modifier)
  523. for sources_type_name in sources.values():
  524. WriteVariable(out, sources_type_name, ' ')
  525. if synthetic_dependencies:
  526. out.write(' DEPENDS')
  527. for synthetic_dependencie in synthetic_dependencies:
  528. WriteVariable(out, synthetic_dependencie, ' ')
  529. out.write(')\n')
  530. if target.cmake_type.command != 'add_custom_target':
  531. WriteCompilerFlags(out, target, project, sources)
  532. libraries = set()
  533. nonlibraries = set()
  534. dependencies = set(target.properties.get('deps', []))
  535. # Transitive OBJECT libraries are in sources.
  536. # Those sources are dependent on the OBJECT library dependencies.
  537. # Those sources cannot bring in library dependencies.
  538. object_dependencies = set()
  539. if target.gn_type != 'source_set':
  540. project.GetObjectLibraryDependencies(
  541. target.gn_name, object_dependencies)
  542. for object_dependency in object_dependencies:
  543. dependencies.update(project.targets.get(
  544. object_dependency).get('deps', []))
  545. for dependency in dependencies:
  546. gn_dependency_type = project.targets.get(
  547. dependency, {}).get('type', None)
  548. cmake_dependency_type = cmake_target_types.get(
  549. gn_dependency_type, None)
  550. cmake_dependency_name = project.GetCMakeTargetName(dependency)
  551. if cmake_dependency_type.command != 'add_library':
  552. nonlibraries.add(cmake_dependency_name)
  553. elif cmake_dependency_type.modifier != 'OBJECT':
  554. if target.cmake_type.is_linkable:
  555. libraries.add(cmake_dependency_name)
  556. else:
  557. nonlibraries.add(cmake_dependency_name)
  558. # Non-library dependencies.
  559. if nonlibraries:
  560. nonlibrarieslist = list(nonlibraries)
  561. nonlibrarieslist.sort()
  562. out.write('add_dependencies("${target}"')
  563. for nonlibrary in nonlibrarieslist:
  564. out.write('\n "')
  565. out.write(nonlibrary)
  566. out.write('"')
  567. out.write(')\n')
  568. # Non-OBJECT library dependencies.
  569. combined_library_lists = [target.properties.get(
  570. key, []) for key in ['libs', 'frameworks']]
  571. external_libraries = list(itertools.chain(*combined_library_lists))
  572. if target.cmake_type.is_linkable and (external_libraries or libraries):
  573. library_dirs = target.properties.get('lib_dirs', [])
  574. if library_dirs:
  575. SetVariableList(
  576. out, '${target}__library_directories', library_dirs)
  577. system_libraries = []
  578. for external_library in external_libraries:
  579. if '/' in external_library:
  580. libraries.add(project.GetAbsolutePath(external_library))
  581. else:
  582. if external_library.endswith('.framework'):
  583. external_library = external_library[:-len('.framework')]
  584. system_library = 'library__' + external_library
  585. if library_dirs:
  586. system_library = system_library + '__for_${target}'
  587. out.write('find_library("')
  588. out.write(CMakeStringEscape(system_library))
  589. out.write('" "')
  590. out.write(CMakeStringEscape(external_library))
  591. out.write('"')
  592. if library_dirs:
  593. out.write(' PATHS "')
  594. WriteVariable(out, '${target}__library_directories')
  595. out.write('"')
  596. out.write(')\n')
  597. system_libraries.append(system_library)
  598. if (target.cmake_type.command == "add_library" and target.cmake_type.modifier == "SHARED") \
  599. or (target.cmake_type.command == "add_executable"):
  600. out.write('target_link_libraries("${target}" -Wl,--start-group')
  601. else:
  602. out.write('target_link_libraries("${target}"')
  603. librarieslist = list(libraries)
  604. librarieslist.sort()
  605. for library in librarieslist:
  606. out.write('\n "')
  607. out.write(CMakeStringEscape(library))
  608. out.write('"')
  609. for system_library in system_libraries:
  610. WriteVariable(out, system_library, '\n "')
  611. out.write('"')
  612. if (target.cmake_type.command == "add_library" and target.cmake_type.modifier == "SHARED") \
  613. or (target.cmake_type.command == "add_executable"):
  614. out.write('\n -Wl,--end-group')
  615. out.write(')\n')
  616. def WriteProject(project):
  617. out = open(posixpath.join(project.build_path, 'CMakeLists.txt'), 'w+')
  618. out.write('# Generated by gn_to_cmake.py.\n')
  619. out.write('cmake_minimum_required(VERSION 3.7 FATAL_ERROR)\n')
  620. out.write('cmake_policy(VERSION 3.7)\n')
  621. out.write('project(MatterAndroid)\n\n')
  622. out.write('file(WRITE "')
  623. out.write(CMakeStringEscape(
  624. posixpath.join(project.build_path, "empty.cpp")))
  625. out.write('")\n')
  626. for target_name in project.targets.keys():
  627. out.write('\n')
  628. WriteTarget(out, Target(target_name, project), project)
  629. out.close()
  630. def main():
  631. if len(sys.argv) != 2:
  632. print('Usage: ' + sys.argv[0] + ' <json_file_name>')
  633. exit(1)
  634. json_path = sys.argv[1]
  635. project = None
  636. with open(json_path, 'r') as json_file:
  637. project = json.loads(json_file.read())
  638. WriteProject(Project(project))
  639. if __name__ == "__main__":
  640. main()