bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2016 Google Inc. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license that can be |
| 6 | # found in the LICENSE file. |
| 7 | |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 8 | |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 9 | """ |
| 10 | Usage: gn_to_cmake.py <json_file_name> |
| 11 | |
| 12 | gn gen out/config --ide=json --json-ide-script=../../gn/gn_to_cmake.py |
| 13 | |
| 14 | or |
| 15 | |
| 16 | gn gen out/config --ide=json |
| 17 | python gn/gn_to_cmake.py out/config/project.json |
Ben Wagner | 388faa0 | 2016-10-05 17:18:38 -0400 | [diff] [blame] | 18 | |
| 19 | The first is recommended, as it will auto-update. |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 20 | """ |
| 21 | |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 22 | |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 23 | import itertools |
| 24 | import functools |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 25 | import json |
| 26 | import posixpath |
| 27 | import os |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 28 | import string |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 29 | import sys |
| 30 | |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 31 | |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 32 | def CMakeStringEscape(a): |
| 33 | """Escapes the string 'a' for use inside a CMake string. |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 34 | |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 35 | This means escaping |
| 36 | '\' otherwise it may be seen as modifying the next character |
| 37 | '"' otherwise it will end the string |
| 38 | ';' otherwise the string becomes a list |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 39 | |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 40 | The following do not need to be escaped |
| 41 | '#' when the lexer is in string state, this does not start a comment |
| 42 | """ |
| 43 | return a.replace('\\', '\\\\').replace(';', '\\;').replace('"', '\\"') |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 44 | |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 45 | |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 46 | def CMakeTargetEscape(a): |
| 47 | """Escapes the string 'a' for use as a CMake target name. |
| 48 | |
| 49 | CMP0037 in CMake 3.0 restricts target names to "^[A-Za-z0-9_.:+-]+$" |
| 50 | The ':' is only allowed for imported targets. |
| 51 | """ |
| 52 | def Escape(c): |
| 53 | if c in string.ascii_letters or c in string.digits or c in '_.+-': |
| 54 | return c |
| 55 | else: |
| 56 | return '__' |
| 57 | return ''.join(map(Escape, a)) |
| 58 | |
| 59 | |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 60 | def SetVariable(out, variable_name, value): |
| 61 | """Sets a CMake variable.""" |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 62 | out.write('set("') |
| 63 | out.write(CMakeStringEscape(variable_name)) |
| 64 | out.write('" "') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 65 | out.write(CMakeStringEscape(value)) |
| 66 | out.write('")\n') |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 67 | |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 68 | |
| 69 | def SetVariableList(out, variable_name, values): |
| 70 | """Sets a CMake variable to a list.""" |
| 71 | if not values: |
| 72 | return SetVariable(out, variable_name, "") |
| 73 | if len(values) == 1: |
| 74 | return SetVariable(out, variable_name, values[0]) |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 75 | out.write('list(APPEND "') |
| 76 | out.write(CMakeStringEscape(variable_name)) |
| 77 | out.write('"\n "') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 78 | out.write('"\n "'.join([CMakeStringEscape(value) for value in values])) |
| 79 | out.write('")\n') |
| 80 | |
| 81 | |
| 82 | def SetFilesProperty(output, variable, property_name, values, sep): |
| 83 | """Given a set of source files, sets the given property on them.""" |
| 84 | output.write('set_source_files_properties(') |
| 85 | WriteVariable(output, variable) |
| 86 | output.write(' PROPERTIES ') |
| 87 | output.write(property_name) |
| 88 | output.write(' "') |
| 89 | for value in values: |
| 90 | output.write(CMakeStringEscape(value)) |
| 91 | output.write(sep) |
| 92 | output.write('")\n') |
| 93 | |
| 94 | |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 95 | def SetCurrentTargetProperty(out, property_name, values, sep=''): |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 96 | """Given a target, sets the given property.""" |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 97 | out.write('set_target_properties("${target}" PROPERTIES ') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 98 | out.write(property_name) |
| 99 | out.write(' "') |
| 100 | for value in values: |
| 101 | out.write(CMakeStringEscape(value)) |
| 102 | out.write(sep) |
| 103 | out.write('")\n') |
| 104 | |
| 105 | |
| 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 | |
| 113 | |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 114 | # See GetSourceFileType in gn |
| 115 | source_file_types = { |
| 116 | '.cc': 'cxx', |
| 117 | '.cpp': 'cxx', |
| 118 | '.cxx': 'cxx', |
| 119 | '.c': 'c', |
| 120 | '.s': 'asm', |
| 121 | '.S': 'asm', |
| 122 | '.asm': 'asm', |
| 123 | '.o': 'obj', |
| 124 | '.obj': 'obj', |
| 125 | } |
| 126 | |
| 127 | |
| 128 | class CMakeTargetType(object): |
| 129 | def __init__(self, command, modifier, property_modifier, is_linkable): |
| 130 | self.command = command |
| 131 | self.modifier = modifier |
| 132 | self.property_modifier = property_modifier |
| 133 | self.is_linkable = is_linkable |
| 134 | CMakeTargetType.custom = CMakeTargetType('add_custom_target', 'SOURCES', |
| 135 | None, False) |
| 136 | |
| 137 | # See GetStringForOutputType in gn |
| 138 | cmake_target_types = { |
| 139 | 'unknown': CMakeTargetType.custom, |
| 140 | 'group': CMakeTargetType.custom, |
| 141 | 'executable': CMakeTargetType('add_executable', None, 'RUNTIME', True), |
| 142 | 'loadable_module': CMakeTargetType('add_library', 'MODULE', 'LIBRARY', True), |
| 143 | 'shared_library': CMakeTargetType('add_library', 'SHARED', 'LIBRARY', True), |
Kevin Lubick | d5c6e16 | 2019-02-01 15:34:04 -0500 | [diff] [blame] | 144 | 'static_library': CMakeTargetType('add_library', 'STATIC', 'ARCHIVE', True), |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 145 | 'source_set': CMakeTargetType('add_library', 'OBJECT', None, False), |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 146 | 'copy': CMakeTargetType.custom, |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 147 | 'action': CMakeTargetType.custom, |
| 148 | 'action_foreach': CMakeTargetType.custom, |
| 149 | 'bundle_data': CMakeTargetType.custom, |
| 150 | 'create_bundle': CMakeTargetType.custom, |
| 151 | } |
| 152 | |
| 153 | |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 154 | def FindFirstOf(s, a): |
| 155 | return min(s.find(i) for i in a if i in s) |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 156 | |
| 157 | |
| 158 | class Project(object): |
| 159 | def __init__(self, project_json): |
| 160 | self.targets = project_json['targets'] |
| 161 | build_settings = project_json['build_settings'] |
| 162 | self.root_path = build_settings['root_path'] |
| 163 | self.build_path = posixpath.join(self.root_path, |
| 164 | build_settings['build_dir'][2:]) |
| 165 | |
| 166 | def GetAbsolutePath(self, path): |
| 167 | if path.startswith("//"): |
| 168 | return self.root_path + "/" + path[2:] |
| 169 | else: |
| 170 | return path |
| 171 | |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 172 | def GetObjectSourceDependencies(self, gn_target_name, object_dependencies): |
| 173 | """All OBJECT libraries whose sources have not been absorbed.""" |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 174 | dependencies = self.targets[gn_target_name].get('deps', []) |
| 175 | for dependency in dependencies: |
| 176 | dependency_type = self.targets[dependency].get('type', None) |
| 177 | if dependency_type == 'source_set': |
| 178 | object_dependencies.add(dependency) |
| 179 | if dependency_type not in gn_target_types_that_absorb_objects: |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 180 | self.GetObjectSourceDependencies(dependency, object_dependencies) |
| 181 | |
| 182 | def GetObjectLibraryDependencies(self, gn_target_name, object_dependencies): |
| 183 | """All OBJECT libraries whose libraries have not been absorbed.""" |
| 184 | dependencies = self.targets[gn_target_name].get('deps', []) |
| 185 | for dependency in dependencies: |
| 186 | dependency_type = self.targets[dependency].get('type', None) |
| 187 | if dependency_type == 'source_set': |
| 188 | object_dependencies.add(dependency) |
| 189 | self.GetObjectLibraryDependencies(dependency, object_dependencies) |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 190 | |
| 191 | def GetCMakeTargetName(self, gn_target_name): |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 192 | # See <chromium>/src/tools/gn/label.cc#Resolve |
| 193 | # //base/test:test_support(//build/toolchain/win:msvc) |
| 194 | path_separator = FindFirstOf(gn_target_name, (':', '(')) |
| 195 | location = None |
| 196 | name = None |
| 197 | toolchain = None |
| 198 | if not path_separator: |
| 199 | location = gn_target_name[2:] |
| 200 | else: |
| 201 | location = gn_target_name[2:path_separator] |
| 202 | toolchain_separator = gn_target_name.find('(', path_separator) |
| 203 | if toolchain_separator == -1: |
| 204 | name = gn_target_name[path_separator + 1:] |
| 205 | else: |
| 206 | if toolchain_separator > path_separator: |
| 207 | name = gn_target_name[path_separator + 1:toolchain_separator] |
| 208 | assert gn_target_name.endswith(')') |
| 209 | toolchain = gn_target_name[toolchain_separator + 1:-1] |
| 210 | assert location or name |
| 211 | |
| 212 | cmake_target_name = None |
| 213 | if location.endswith('/' + name): |
| 214 | cmake_target_name = location |
| 215 | elif location: |
| 216 | cmake_target_name = location + '_' + name |
| 217 | else: |
| 218 | cmake_target_name = name |
| 219 | if toolchain: |
| 220 | cmake_target_name += '--' + toolchain |
| 221 | return CMakeTargetEscape(cmake_target_name) |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 222 | |
| 223 | |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 224 | class Target(object): |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 225 | def __init__(self, gn_target_name, project): |
| 226 | self.gn_name = gn_target_name |
| 227 | self.properties = project.targets[self.gn_name] |
| 228 | self.cmake_name = project.GetCMakeTargetName(self.gn_name) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 229 | self.gn_type = self.properties.get('type', None) |
| 230 | self.cmake_type = cmake_target_types.get(self.gn_type, None) |
| 231 | |
| 232 | |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 233 | def WriteAction(out, target, project, sources, synthetic_dependencies): |
| 234 | outputs = [] |
| 235 | output_directories = set() |
| 236 | for output in target.properties.get('outputs', []): |
| 237 | output_abs_path = project.GetAbsolutePath(output) |
| 238 | outputs.append(output_abs_path) |
| 239 | output_directory = posixpath.dirname(output_abs_path) |
| 240 | if output_directory: |
| 241 | output_directories.add(output_directory) |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 242 | outputs_name = '${target}__output' |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 243 | SetVariableList(out, outputs_name, outputs) |
| 244 | |
| 245 | out.write('add_custom_command(OUTPUT ') |
| 246 | WriteVariable(out, outputs_name) |
| 247 | out.write('\n') |
| 248 | |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 249 | if output_directories: |
| 250 | out.write(' COMMAND ${CMAKE_COMMAND} -E make_directory "') |
| 251 | out.write('" "'.join(map(CMakeStringEscape, output_directories))) |
| 252 | out.write('"\n') |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 253 | |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 254 | script = target.properties['script'] |
| 255 | arguments = target.properties['args'] |
| 256 | out.write(' COMMAND python "') |
| 257 | out.write(CMakeStringEscape(project.GetAbsolutePath(script))) |
| 258 | out.write('"') |
| 259 | if arguments: |
| 260 | out.write('\n "') |
| 261 | out.write('"\n "'.join(map(CMakeStringEscape, arguments))) |
| 262 | out.write('"') |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 263 | out.write('\n') |
| 264 | |
| 265 | out.write(' DEPENDS ') |
| 266 | for sources_type_name in sources.values(): |
| 267 | WriteVariable(out, sources_type_name, ' ') |
| 268 | out.write('\n') |
| 269 | |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 270 | #TODO: CMake 3.7 is introducing DEPFILE |
| 271 | |
| 272 | out.write(' WORKING_DIRECTORY "') |
| 273 | out.write(CMakeStringEscape(project.build_path)) |
| 274 | out.write('"\n') |
| 275 | |
| 276 | out.write(' COMMENT "Action: ${target}"\n') |
| 277 | |
| 278 | out.write(' VERBATIM)\n') |
| 279 | |
| 280 | synthetic_dependencies.add(outputs_name) |
| 281 | |
| 282 | |
| 283 | def ExpandPlaceholders(source, a): |
| 284 | source_dir, source_file_part = posixpath.split(source) |
| 285 | source_name_part, _ = posixpath.splitext(source_file_part) |
| 286 | #TODO: {{source_gen_dir}}, {{source_out_dir}}, {{response_file_name}} |
| 287 | return a.replace('{{source}}', source) \ |
| 288 | .replace('{{source_file_part}}', source_file_part) \ |
| 289 | .replace('{{source_name_part}}', source_name_part) \ |
| 290 | .replace('{{source_dir}}', source_dir) \ |
| 291 | .replace('{{source_root_relative_dir}}', source_dir) |
| 292 | |
| 293 | |
| 294 | def WriteActionForEach(out, target, project, sources, synthetic_dependencies): |
| 295 | all_outputs = target.properties.get('outputs', []) |
| 296 | inputs = target.properties.get('sources', []) |
| 297 | # TODO: consider expanding 'output_patterns' instead. |
| 298 | outputs_per_input = len(all_outputs) / len(inputs) |
| 299 | for count, source in enumerate(inputs): |
| 300 | source_abs_path = project.GetAbsolutePath(source) |
| 301 | |
| 302 | outputs = [] |
| 303 | output_directories = set() |
| 304 | for output in all_outputs[outputs_per_input * count: |
| 305 | outputs_per_input * (count+1)]: |
| 306 | output_abs_path = project.GetAbsolutePath(output) |
| 307 | outputs.append(output_abs_path) |
| 308 | output_directory = posixpath.dirname(output_abs_path) |
| 309 | if output_directory: |
| 310 | output_directories.add(output_directory) |
| 311 | outputs_name = '${target}__output_' + str(count) |
| 312 | SetVariableList(out, outputs_name, outputs) |
| 313 | |
| 314 | out.write('add_custom_command(OUTPUT ') |
| 315 | WriteVariable(out, outputs_name) |
| 316 | out.write('\n') |
| 317 | |
| 318 | if output_directories: |
| 319 | out.write(' COMMAND ${CMAKE_COMMAND} -E make_directory "') |
| 320 | out.write('" "'.join(map(CMakeStringEscape, output_directories))) |
| 321 | out.write('"\n') |
| 322 | |
| 323 | script = target.properties['script'] |
| 324 | # TODO: need to expand {{xxx}} in arguments |
| 325 | arguments = target.properties['args'] |
| 326 | out.write(' COMMAND python "') |
| 327 | out.write(CMakeStringEscape(project.GetAbsolutePath(script))) |
| 328 | out.write('"') |
| 329 | if arguments: |
| 330 | out.write('\n "') |
| 331 | expand = functools.partial(ExpandPlaceholders, source_abs_path) |
| 332 | out.write('"\n "'.join(map(CMakeStringEscape, map(expand,arguments)))) |
| 333 | out.write('"') |
| 334 | out.write('\n') |
| 335 | |
| 336 | out.write(' DEPENDS') |
| 337 | if 'input' in sources: |
| 338 | WriteVariable(out, sources['input'], ' ') |
| 339 | out.write(' "') |
| 340 | out.write(CMakeStringEscape(source_abs_path)) |
| 341 | out.write('"\n') |
| 342 | |
| 343 | #TODO: CMake 3.7 is introducing DEPFILE |
| 344 | |
| 345 | out.write(' WORKING_DIRECTORY "') |
| 346 | out.write(CMakeStringEscape(project.build_path)) |
| 347 | out.write('"\n') |
| 348 | |
| 349 | out.write(' COMMENT "Action ${target} on ') |
| 350 | out.write(CMakeStringEscape(source_abs_path)) |
| 351 | out.write('"\n') |
| 352 | |
| 353 | out.write(' VERBATIM)\n') |
| 354 | |
| 355 | synthetic_dependencies.add(outputs_name) |
| 356 | |
| 357 | |
| 358 | def WriteCopy(out, target, project, sources, synthetic_dependencies): |
| 359 | inputs = target.properties.get('sources', []) |
| 360 | raw_outputs = target.properties.get('outputs', []) |
| 361 | |
| 362 | # TODO: consider expanding 'output_patterns' instead. |
| 363 | outputs = [] |
| 364 | for output in raw_outputs: |
| 365 | output_abs_path = project.GetAbsolutePath(output) |
| 366 | outputs.append(output_abs_path) |
| 367 | outputs_name = '${target}__output' |
| 368 | SetVariableList(out, outputs_name, outputs) |
| 369 | |
| 370 | out.write('add_custom_command(OUTPUT ') |
| 371 | WriteVariable(out, outputs_name) |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 372 | out.write('\n') |
| 373 | |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 374 | for src, dst in zip(inputs, outputs): |
Kevin Lubick | 9a2bb09 | 2019-02-01 13:32:44 -0500 | [diff] [blame] | 375 | abs_src_path = CMakeStringEscape(project.GetAbsolutePath(src)) |
| 376 | # CMake distinguishes between copying files and copying directories but |
| 377 | # gn does not. We assume if the src has a period in its name then it is |
| 378 | # a file and otherwise a directory. |
| 379 | if "." in os.path.basename(abs_src_path): |
| 380 | out.write(' COMMAND ${CMAKE_COMMAND} -E copy "') |
| 381 | else: |
| 382 | out.write(' COMMAND ${CMAKE_COMMAND} -E copy_directory "') |
| 383 | out.write(abs_src_path) |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 384 | out.write('" "') |
| 385 | out.write(CMakeStringEscape(dst)) |
| 386 | out.write('"\n') |
| 387 | |
| 388 | out.write(' DEPENDS ') |
| 389 | for sources_type_name in sources.values(): |
| 390 | WriteVariable(out, sources_type_name, ' ') |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 391 | out.write('\n') |
| 392 | |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 393 | out.write(' WORKING_DIRECTORY "') |
| 394 | out.write(CMakeStringEscape(project.build_path)) |
| 395 | out.write('"\n') |
| 396 | |
| 397 | out.write(' COMMENT "Copy ${target}"\n') |
| 398 | |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 399 | out.write(' VERBATIM)\n') |
| 400 | |
| 401 | synthetic_dependencies.add(outputs_name) |
| 402 | |
| 403 | |
| 404 | def WriteCompilerFlags(out, target, project, sources): |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 405 | # Hack, set linker language to c if no c or cxx files present. |
| 406 | if not 'c' in sources and not 'cxx' in sources: |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 407 | SetCurrentTargetProperty(out, 'LINKER_LANGUAGE', ['C']) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 408 | |
| 409 | # Mark uncompiled sources as uncompiled. |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 410 | if 'input' in sources: |
| 411 | SetFilesProperty(out, sources['input'], 'HEADER_FILE_ONLY', ('True',), '') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 412 | if 'other' in sources: |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 413 | SetFilesProperty(out, sources['other'], 'HEADER_FILE_ONLY', ('True',), '') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 414 | |
| 415 | # Mark object sources as linkable. |
| 416 | if 'obj' in sources: |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 417 | SetFilesProperty(out, sources['obj'], 'EXTERNAL_OBJECT', ('True',), '') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 418 | |
| 419 | # TODO: 'output_name', 'output_dir', 'output_extension' |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 420 | # This includes using 'source_outputs' to direct compiler output. |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 421 | |
| 422 | # Includes |
| 423 | includes = target.properties.get('include_dirs', []) |
| 424 | if includes: |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 425 | out.write('set_property(TARGET "${target}" ') |
| 426 | out.write('APPEND PROPERTY INCLUDE_DIRECTORIES') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 427 | for include_dir in includes: |
| 428 | out.write('\n "') |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 429 | out.write(project.GetAbsolutePath(include_dir)) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 430 | out.write('"') |
| 431 | out.write(')\n') |
| 432 | |
| 433 | # Defines |
| 434 | defines = target.properties.get('defines', []) |
| 435 | if defines: |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 436 | SetCurrentTargetProperty(out, 'COMPILE_DEFINITIONS', defines, ';') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 437 | |
| 438 | # Compile flags |
| 439 | # "arflags", "asmflags", "cflags", |
| 440 | # "cflags_c", "clfags_cc", "cflags_objc", "clfags_objcc" |
| 441 | # CMake does not have per target lang compile flags. |
| 442 | # TODO: $<$<COMPILE_LANGUAGE:CXX>:cflags_cc style generator expression. |
| 443 | # http://public.kitware.com/Bug/view.php?id=14857 |
| 444 | flags = [] |
| 445 | flags.extend(target.properties.get('cflags', [])) |
| 446 | cflags_asm = target.properties.get('asmflags', []) |
| 447 | cflags_c = target.properties.get('cflags_c', []) |
| 448 | cflags_cxx = target.properties.get('cflags_cc', []) |
| 449 | if 'c' in sources and not any(k in sources for k in ('asm', 'cxx')): |
| 450 | flags.extend(cflags_c) |
| 451 | elif 'cxx' in sources and not any(k in sources for k in ('asm', 'c')): |
| 452 | flags.extend(cflags_cxx) |
| 453 | else: |
| 454 | # TODO: This is broken, one cannot generally set properties on files, |
| 455 | # as other targets may require different properties on the same files. |
| 456 | if 'asm' in sources and cflags_asm: |
| 457 | SetFilesProperty(out, sources['asm'], 'COMPILE_FLAGS', cflags_asm, ' ') |
| 458 | if 'c' in sources and cflags_c: |
| 459 | SetFilesProperty(out, sources['c'], 'COMPILE_FLAGS', cflags_c, ' ') |
| 460 | if 'cxx' in sources and cflags_cxx: |
| 461 | SetFilesProperty(out, sources['cxx'], 'COMPILE_FLAGS', cflags_cxx, ' ') |
| 462 | if flags: |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 463 | SetCurrentTargetProperty(out, 'COMPILE_FLAGS', flags, ' ') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 464 | |
| 465 | # Linker flags |
| 466 | ldflags = target.properties.get('ldflags', []) |
| 467 | if ldflags: |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 468 | SetCurrentTargetProperty(out, 'LINK_FLAGS', ldflags, ' ') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 469 | |
| 470 | |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 471 | gn_target_types_that_absorb_objects = ( |
| 472 | 'executable', |
| 473 | 'loadable_module', |
| 474 | 'shared_library', |
| 475 | 'static_library' |
| 476 | ) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 477 | |
| 478 | |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 479 | def WriteSourceVariables(out, target, project): |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 480 | # gn separates the sheep from the goats based on file extensions. |
| 481 | # A full separation is done here because of flag handing (see Compile flags). |
| 482 | source_types = {'cxx':[], 'c':[], 'asm':[], |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 483 | 'obj':[], 'obj_target':[], 'input':[], 'other':[]} |
| 484 | |
Herb Derby | 5699278 | 2018-05-18 15:09:41 -0400 | [diff] [blame] | 485 | all_sources = target.properties.get('sources', []) |
| 486 | |
| 487 | # As of cmake 3.11 add_library must have sources. If there are |
| 488 | # no sources, add empty.cpp as the file to compile. |
| 489 | if len(all_sources) == 0: |
| 490 | all_sources.append(posixpath.join(project.build_path, 'empty.cpp')) |
| 491 | |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 492 | # TODO .def files on Windows |
Herb Derby | 5699278 | 2018-05-18 15:09:41 -0400 | [diff] [blame] | 493 | for source in all_sources: |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 494 | _, ext = posixpath.splitext(source) |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 495 | source_abs_path = project.GetAbsolutePath(source) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 496 | source_types[source_file_types.get(ext, 'other')].append(source_abs_path) |
| 497 | |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 498 | for input_path in target.properties.get('inputs', []): |
| 499 | input_abs_path = project.GetAbsolutePath(input_path) |
| 500 | source_types['input'].append(input_abs_path) |
| 501 | |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 502 | # OBJECT library dependencies need to be listed as sources. |
| 503 | # Only executables and non-OBJECT libraries may reference an OBJECT library. |
| 504 | # https://gitlab.kitware.com/cmake/cmake/issues/14778 |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 505 | if target.gn_type in gn_target_types_that_absorb_objects: |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 506 | object_dependencies = set() |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 507 | project.GetObjectSourceDependencies(target.gn_name, object_dependencies) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 508 | for dependency in object_dependencies: |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 509 | cmake_dependency_name = project.GetCMakeTargetName(dependency) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 510 | obj_target_sources = '$<TARGET_OBJECTS:' + cmake_dependency_name + '>' |
| 511 | source_types['obj_target'].append(obj_target_sources) |
| 512 | |
| 513 | sources = {} |
| 514 | for source_type, sources_of_type in source_types.items(): |
| 515 | if sources_of_type: |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 516 | sources[source_type] = '${target}__' + source_type + '_srcs' |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 517 | SetVariableList(out, sources[source_type], sources_of_type) |
| 518 | return sources |
| 519 | |
| 520 | |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 521 | def WriteTarget(out, target, project): |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 522 | out.write('\n#') |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 523 | out.write(target.gn_name) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 524 | out.write('\n') |
| 525 | |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 526 | if target.cmake_type is None: |
| 527 | print ('Target %s has unknown target type %s, skipping.' % |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 528 | ( target.gn_name, target.gn_type ) ) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 529 | return |
| 530 | |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 531 | SetVariable(out, 'target', target.cmake_name) |
| 532 | |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 533 | sources = WriteSourceVariables(out, target, project) |
| 534 | |
| 535 | synthetic_dependencies = set() |
| 536 | if target.gn_type == 'action': |
| 537 | WriteAction(out, target, project, sources, synthetic_dependencies) |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 538 | if target.gn_type == 'action_foreach': |
| 539 | WriteActionForEach(out, target, project, sources, synthetic_dependencies) |
| 540 | if target.gn_type == 'copy': |
| 541 | WriteCopy(out, target, project, sources, synthetic_dependencies) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 542 | |
| 543 | out.write(target.cmake_type.command) |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 544 | out.write('("${target}"') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 545 | if target.cmake_type.modifier is not None: |
| 546 | out.write(' ') |
| 547 | out.write(target.cmake_type.modifier) |
| 548 | for sources_type_name in sources.values(): |
| 549 | WriteVariable(out, sources_type_name, ' ') |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 550 | if synthetic_dependencies: |
| 551 | out.write(' DEPENDS') |
| 552 | for synthetic_dependencie in synthetic_dependencies: |
| 553 | WriteVariable(out, synthetic_dependencie, ' ') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 554 | out.write(')\n') |
| 555 | |
| 556 | if target.cmake_type.command != 'add_custom_target': |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 557 | WriteCompilerFlags(out, target, project, sources) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 558 | |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 559 | libraries = set() |
| 560 | nonlibraries = set() |
| 561 | |
| 562 | dependencies = set(target.properties.get('deps', [])) |
| 563 | # Transitive OBJECT libraries are in sources. |
| 564 | # Those sources are dependent on the OBJECT library dependencies. |
| 565 | # Those sources cannot bring in library dependencies. |
| 566 | object_dependencies = set() |
| 567 | if target.gn_type != 'source_set': |
| 568 | project.GetObjectLibraryDependencies(target.gn_name, object_dependencies) |
| 569 | for object_dependency in object_dependencies: |
| 570 | dependencies.update(project.targets.get(object_dependency).get('deps', [])) |
| 571 | |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 572 | for dependency in dependencies: |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 573 | gn_dependency_type = project.targets.get(dependency, {}).get('type', None) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 574 | cmake_dependency_type = cmake_target_types.get(gn_dependency_type, None) |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 575 | cmake_dependency_name = project.GetCMakeTargetName(dependency) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 576 | if cmake_dependency_type.command != 'add_library': |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 577 | nonlibraries.add(cmake_dependency_name) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 578 | elif cmake_dependency_type.modifier != 'OBJECT': |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 579 | if target.cmake_type.is_linkable: |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 580 | libraries.add(cmake_dependency_name) |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 581 | else: |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 582 | nonlibraries.add(cmake_dependency_name) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 583 | |
| 584 | # Non-library dependencies. |
| 585 | if nonlibraries: |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 586 | out.write('add_dependencies("${target}"') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 587 | for nonlibrary in nonlibraries: |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 588 | out.write('\n "') |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 589 | out.write(nonlibrary) |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 590 | out.write('"') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 591 | out.write(')\n') |
| 592 | |
| 593 | # Non-OBJECT library dependencies. |
| 594 | external_libraries = target.properties.get('libs', []) |
| 595 | if target.cmake_type.is_linkable and (external_libraries or libraries): |
Ben Wagner | 187a771 | 2016-10-27 18:47:54 -0400 | [diff] [blame] | 596 | library_dirs = target.properties.get('lib_dirs', []) |
| 597 | if library_dirs: |
| 598 | SetVariableList(out, '${target}__library_directories', library_dirs) |
| 599 | |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 600 | system_libraries = [] |
| 601 | for external_library in external_libraries: |
| 602 | if '/' in external_library: |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 603 | libraries.add(project.GetAbsolutePath(external_library)) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 604 | else: |
| 605 | if external_library.endswith('.framework'): |
| 606 | external_library = external_library[:-len('.framework')] |
Ben Wagner | 187a771 | 2016-10-27 18:47:54 -0400 | [diff] [blame] | 607 | system_library = 'library__' + external_library |
| 608 | if library_dirs: |
| 609 | system_library = system_library + '__for_${target}' |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 610 | out.write('find_library("') |
| 611 | out.write(CMakeStringEscape(system_library)) |
| 612 | out.write('" "') |
| 613 | out.write(CMakeStringEscape(external_library)) |
Ben Wagner | 187a771 | 2016-10-27 18:47:54 -0400 | [diff] [blame] | 614 | out.write('"') |
| 615 | if library_dirs: |
| 616 | out.write(' PATHS "') |
| 617 | WriteVariable(out, '${target}__library_directories') |
| 618 | out.write('"') |
| 619 | out.write(')\n') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 620 | system_libraries.append(system_library) |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 621 | out.write('target_link_libraries("${target}"') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 622 | for library in libraries: |
| 623 | out.write('\n "') |
| 624 | out.write(CMakeStringEscape(library)) |
| 625 | out.write('"') |
| 626 | for system_library in system_libraries: |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 627 | WriteVariable(out, system_library, '\n "') |
| 628 | out.write('"') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 629 | out.write(')\n') |
| 630 | |
| 631 | |
| 632 | def WriteProject(project): |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 633 | out = open(posixpath.join(project.build_path, 'CMakeLists.txt'), 'w+') |
Ben Wagner | 82cecb6 | 2018-05-31 15:04:43 -0400 | [diff] [blame] | 634 | extName = posixpath.join(project.build_path, 'CMakeLists.ext') |
Ben Wagner | dc69dc7 | 2016-10-04 16:44:44 -0400 | [diff] [blame] | 635 | out.write('# Generated by gn_to_cmake.py.\n') |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 636 | out.write('cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)\n') |
Ben Wagner | 388faa0 | 2016-10-05 17:18:38 -0400 | [diff] [blame] | 637 | out.write('cmake_policy(VERSION 2.8.8)\n\n') |
| 638 | |
Herb Derby | 5699278 | 2018-05-18 15:09:41 -0400 | [diff] [blame] | 639 | out.write('file(WRITE "') |
Ben Wagner | 82cecb6 | 2018-05-31 15:04:43 -0400 | [diff] [blame] | 640 | out.write(CMakeStringEscape(posixpath.join(project.build_path, "empty.cpp"))) |
Herb Derby | 5699278 | 2018-05-18 15:09:41 -0400 | [diff] [blame] | 641 | out.write('")\n') |
| 642 | |
Ben Wagner | 388faa0 | 2016-10-05 17:18:38 -0400 | [diff] [blame] | 643 | # Update the gn generated ninja build. |
| 644 | # If a build file has changed, this will update CMakeLists.ext if |
| 645 | # gn gen out/config --ide=json --json-ide-script=../../gn/gn_to_cmake.py |
| 646 | # style was used to create this config. |
Ben Wagner | 4f0a011 | 2018-06-25 15:35:28 -0400 | [diff] [blame] | 647 | out.write('execute_process(COMMAND\n') |
| 648 | out.write(' ninja -C "') |
Ben Wagner | 388faa0 | 2016-10-05 17:18:38 -0400 | [diff] [blame] | 649 | out.write(CMakeStringEscape(project.build_path)) |
Ben Wagner | 4f0a011 | 2018-06-25 15:35:28 -0400 | [diff] [blame] | 650 | out.write('" build.ninja\n') |
| 651 | out.write(' RESULT_VARIABLE ninja_result)\n') |
| 652 | out.write('if (ninja_result)\n') |
| 653 | out.write(' message(WARNING ') |
| 654 | out.write('"Regeneration failed running ninja: ${ninja_result}")\n') |
| 655 | out.write('endif()\n') |
Ben Wagner | 388faa0 | 2016-10-05 17:18:38 -0400 | [diff] [blame] | 656 | |
Ben Wagner | 82cecb6 | 2018-05-31 15:04:43 -0400 | [diff] [blame] | 657 | out.write('include("') |
| 658 | out.write(CMakeStringEscape(extName)) |
| 659 | out.write('")\n') |
Ben Wagner | 388faa0 | 2016-10-05 17:18:38 -0400 | [diff] [blame] | 660 | out.close() |
| 661 | |
Ben Wagner | 82cecb6 | 2018-05-31 15:04:43 -0400 | [diff] [blame] | 662 | out = open(extName, 'w+') |
Ben Wagner | 388faa0 | 2016-10-05 17:18:38 -0400 | [diff] [blame] | 663 | out.write('# Generated by gn_to_cmake.py.\n') |
| 664 | out.write('cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)\n') |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 665 | out.write('cmake_policy(VERSION 2.8.8)\n') |
| 666 | |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 667 | # The following appears to be as-yet undocumented. |
| 668 | # http://public.kitware.com/Bug/view.php?id=8392 |
Ben Wagner | 388faa0 | 2016-10-05 17:18:38 -0400 | [diff] [blame] | 669 | out.write('enable_language(ASM)\n\n') |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 670 | # ASM-ATT does not support .S files. |
| 671 | # output.write('enable_language(ASM-ATT)\n') |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 672 | |
Ben Wagner | 388faa0 | 2016-10-05 17:18:38 -0400 | [diff] [blame] | 673 | # Current issues with automatic re-generation: |
| 674 | # The gn generated build.ninja target uses build.ninja.d |
| 675 | # but build.ninja.d does not contain the ide or gn. |
| 676 | # Currently the ide is not run if the project.json file is not changed |
| 677 | # but the ide needs to be run anyway if it has itself changed. |
| 678 | # This can be worked around by deleting the project.json file. |
| 679 | out.write('file(READ "') |
| 680 | gn_deps_file = posixpath.join(project.build_path, 'build.ninja.d') |
| 681 | out.write(CMakeStringEscape(gn_deps_file)) |
| 682 | out.write('" "gn_deps_string" OFFSET ') |
| 683 | out.write(str(len('build.ninja: '))) |
| 684 | out.write(')\n') |
| 685 | # One would think this would need to worry about escaped spaces |
| 686 | # but gn doesn't escape spaces here (it generates invalid .d files). |
| 687 | out.write('string(REPLACE " " ";" "gn_deps" ${gn_deps_string})\n') |
| 688 | out.write('foreach("gn_dep" ${gn_deps})\n') |
Ben Wagner | 4f0a011 | 2018-06-25 15:35:28 -0400 | [diff] [blame] | 689 | out.write(' configure_file("') |
| 690 | out.write(CMakeStringEscape(project.build_path)) |
| 691 | out.write('${gn_dep}" "CMakeLists.devnull" COPYONLY)\n') |
Ben Wagner | 388faa0 | 2016-10-05 17:18:38 -0400 | [diff] [blame] | 692 | out.write('endforeach("gn_dep")\n') |
| 693 | |
Ben Wagner | 4f0a011 | 2018-06-25 15:35:28 -0400 | [diff] [blame] | 694 | out.write('list(APPEND other_deps "') |
| 695 | out.write(CMakeStringEscape(os.path.abspath(__file__))) |
| 696 | out.write('")\n') |
| 697 | out.write('foreach("other_dep" ${other_deps})\n') |
| 698 | out.write(' configure_file("${other_dep}" "CMakeLists.devnull" COPYONLY)\n') |
| 699 | out.write('endforeach("other_dep")\n') |
| 700 | |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 701 | for target_name in project.targets.keys(): |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 702 | out.write('\n') |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 703 | WriteTarget(out, Target(target_name, project), project) |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 704 | |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 705 | |
| 706 | def main(): |
| 707 | if len(sys.argv) != 2: |
| 708 | print('Usage: ' + sys.argv[0] + ' <json_file_name>') |
| 709 | exit(1) |
| 710 | |
| 711 | json_path = sys.argv[1] |
| 712 | project = None |
| 713 | with open(json_path, 'r') as json_file: |
| 714 | project = json.loads(json_file.read()) |
| 715 | |
Ben Wagner | bc34404 | 2016-09-29 15:41:53 -0400 | [diff] [blame] | 716 | WriteProject(Project(project)) |
bungeman | e95ea08 | 2016-09-28 16:45:35 -0400 | [diff] [blame] | 717 | |
bungeman | 623ef92 | 2016-09-23 08:16:04 -0700 | [diff] [blame] | 718 | |
| 719 | if __name__ == "__main__": |
| 720 | main() |