11cb0ef41Sopenharmony_ci# Copyright (c) 2013 Google Inc. All rights reserved.
21cb0ef41Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci# found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci"""cmake output module
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciThis module is under development and should be considered experimental.
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciThis module produces cmake (2.8.8+) input as its output. One CMakeLists.txt is
101cb0ef41Sopenharmony_cicreated for each configuration.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciThis module's original purpose was to support editing in IDEs like KDevelop
131cb0ef41Sopenharmony_ciwhich use CMake for project management. It is also possible to use CMake to
141cb0ef41Sopenharmony_cigenerate projects for other IDEs such as eclipse cdt and code::blocks. QtCreator
151cb0ef41Sopenharmony_ciwill convert the CMakeLists.txt to a code::blocks cbp for the editor to read,
161cb0ef41Sopenharmony_cibut build using CMake. As a result QtCreator editor is unaware of compiler
171cb0ef41Sopenharmony_cidefines. The generated CMakeLists.txt can also be used to build on Linux. There
181cb0ef41Sopenharmony_ciis currently no support for building on platforms other than Linux.
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciThe generated CMakeLists.txt should properly compile all projects. However,
211cb0ef41Sopenharmony_cithere is a mismatch between gyp and cmake with regard to linking. All attempts
221cb0ef41Sopenharmony_ciare made to work around this, but CMake sometimes sees -Wl,--start-group as a
231cb0ef41Sopenharmony_cilibrary and incorrectly repeats it. As a result the output of this generator
241cb0ef41Sopenharmony_cishould not be relied on for building.
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciWhen using with kdevelop, use version 4.4+. Previous versions of kdevelop will
271cb0ef41Sopenharmony_cinot be able to find the header file directories described in the generated
281cb0ef41Sopenharmony_ciCMakeLists.txt file.
291cb0ef41Sopenharmony_ci"""
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciimport multiprocessing
331cb0ef41Sopenharmony_ciimport os
341cb0ef41Sopenharmony_ciimport signal
351cb0ef41Sopenharmony_ciimport subprocess
361cb0ef41Sopenharmony_ciimport gyp.common
371cb0ef41Sopenharmony_ciimport gyp.xcode_emulation
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci_maketrans = str.maketrans
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_cigenerator_default_variables = {
421cb0ef41Sopenharmony_ci    "EXECUTABLE_PREFIX": "",
431cb0ef41Sopenharmony_ci    "EXECUTABLE_SUFFIX": "",
441cb0ef41Sopenharmony_ci    "STATIC_LIB_PREFIX": "lib",
451cb0ef41Sopenharmony_ci    "STATIC_LIB_SUFFIX": ".a",
461cb0ef41Sopenharmony_ci    "SHARED_LIB_PREFIX": "lib",
471cb0ef41Sopenharmony_ci    "SHARED_LIB_SUFFIX": ".so",
481cb0ef41Sopenharmony_ci    "SHARED_LIB_DIR": "${builddir}/lib.${TOOLSET}",
491cb0ef41Sopenharmony_ci    "LIB_DIR": "${obj}.${TOOLSET}",
501cb0ef41Sopenharmony_ci    "INTERMEDIATE_DIR": "${obj}.${TOOLSET}/${TARGET}/geni",
511cb0ef41Sopenharmony_ci    "SHARED_INTERMEDIATE_DIR": "${obj}/gen",
521cb0ef41Sopenharmony_ci    "PRODUCT_DIR": "${builddir}",
531cb0ef41Sopenharmony_ci    "RULE_INPUT_PATH": "${RULE_INPUT_PATH}",
541cb0ef41Sopenharmony_ci    "RULE_INPUT_DIRNAME": "${RULE_INPUT_DIRNAME}",
551cb0ef41Sopenharmony_ci    "RULE_INPUT_NAME": "${RULE_INPUT_NAME}",
561cb0ef41Sopenharmony_ci    "RULE_INPUT_ROOT": "${RULE_INPUT_ROOT}",
571cb0ef41Sopenharmony_ci    "RULE_INPUT_EXT": "${RULE_INPUT_EXT}",
581cb0ef41Sopenharmony_ci    "CONFIGURATION_NAME": "${configuration}",
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ciFULL_PATH_VARS = ("${CMAKE_CURRENT_LIST_DIR}", "${builddir}", "${obj}")
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_cigenerator_supports_multiple_toolsets = True
641cb0ef41Sopenharmony_cigenerator_wants_static_library_dependencies_adjusted = True
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ciCOMPILABLE_EXTENSIONS = {
671cb0ef41Sopenharmony_ci    ".c": "cc",
681cb0ef41Sopenharmony_ci    ".cc": "cxx",
691cb0ef41Sopenharmony_ci    ".cpp": "cxx",
701cb0ef41Sopenharmony_ci    ".cxx": "cxx",
711cb0ef41Sopenharmony_ci    ".s": "s",  # cc
721cb0ef41Sopenharmony_ci    ".S": "s",  # cc
731cb0ef41Sopenharmony_ci}
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_cidef RemovePrefix(a, prefix):
771cb0ef41Sopenharmony_ci    """Returns 'a' without 'prefix' if it starts with 'prefix'."""
781cb0ef41Sopenharmony_ci    return a[len(prefix) :] if a.startswith(prefix) else a
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_cidef CalculateVariables(default_variables, params):
821cb0ef41Sopenharmony_ci    """Calculate additional variables for use in the build (called by gyp)."""
831cb0ef41Sopenharmony_ci    default_variables.setdefault("OS", gyp.common.GetFlavor(params))
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_cidef Compilable(filename):
871cb0ef41Sopenharmony_ci    """Return true if the file is compilable (should be in OBJS)."""
881cb0ef41Sopenharmony_ci    return any(filename.endswith(e) for e in COMPILABLE_EXTENSIONS)
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_cidef Linkable(filename):
921cb0ef41Sopenharmony_ci    """Return true if the file is linkable (should be on the link line)."""
931cb0ef41Sopenharmony_ci    return filename.endswith(".o")
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_cidef NormjoinPathForceCMakeSource(base_path, rel_path):
971cb0ef41Sopenharmony_ci    """Resolves rel_path against base_path and returns the result.
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  If rel_path is an absolute path it is returned unchanged.
1001cb0ef41Sopenharmony_ci  Otherwise it is resolved against base_path and normalized.
1011cb0ef41Sopenharmony_ci  If the result is a relative path, it is forced to be relative to the
1021cb0ef41Sopenharmony_ci  CMakeLists.txt.
1031cb0ef41Sopenharmony_ci  """
1041cb0ef41Sopenharmony_ci    if os.path.isabs(rel_path):
1051cb0ef41Sopenharmony_ci        return rel_path
1061cb0ef41Sopenharmony_ci    if any([rel_path.startswith(var) for var in FULL_PATH_VARS]):
1071cb0ef41Sopenharmony_ci        return rel_path
1081cb0ef41Sopenharmony_ci    # TODO: do we need to check base_path for absolute variables as well?
1091cb0ef41Sopenharmony_ci    return os.path.join(
1101cb0ef41Sopenharmony_ci        "${CMAKE_CURRENT_LIST_DIR}", os.path.normpath(os.path.join(base_path, rel_path))
1111cb0ef41Sopenharmony_ci    )
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_cidef NormjoinPath(base_path, rel_path):
1151cb0ef41Sopenharmony_ci    """Resolves rel_path against base_path and returns the result.
1161cb0ef41Sopenharmony_ci  TODO: what is this really used for?
1171cb0ef41Sopenharmony_ci  If rel_path begins with '$' it is returned unchanged.
1181cb0ef41Sopenharmony_ci  Otherwise it is resolved against base_path if relative, then normalized.
1191cb0ef41Sopenharmony_ci  """
1201cb0ef41Sopenharmony_ci    if rel_path.startswith("$") and not rel_path.startswith("${configuration}"):
1211cb0ef41Sopenharmony_ci        return rel_path
1221cb0ef41Sopenharmony_ci    return os.path.normpath(os.path.join(base_path, rel_path))
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_cidef CMakeStringEscape(a):
1261cb0ef41Sopenharmony_ci    """Escapes the string 'a' for use inside a CMake string.
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci  This means escaping
1291cb0ef41Sopenharmony_ci  '\' otherwise it may be seen as modifying the next character
1301cb0ef41Sopenharmony_ci  '"' otherwise it will end the string
1311cb0ef41Sopenharmony_ci  ';' otherwise the string becomes a list
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci  The following do not need to be escaped
1341cb0ef41Sopenharmony_ci  '#' when the lexer is in string state, this does not start a comment
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci  The following are yet unknown
1371cb0ef41Sopenharmony_ci  '$' generator variables (like ${obj}) must not be escaped,
1381cb0ef41Sopenharmony_ci      but text $ should be escaped
1391cb0ef41Sopenharmony_ci      what is wanted is to know which $ come from generator variables
1401cb0ef41Sopenharmony_ci  """
1411cb0ef41Sopenharmony_ci    return a.replace("\\", "\\\\").replace(";", "\\;").replace('"', '\\"')
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_cidef SetFileProperty(output, source_name, property_name, values, sep):
1451cb0ef41Sopenharmony_ci    """Given a set of source file, sets the given property on them."""
1461cb0ef41Sopenharmony_ci    output.write("set_source_files_properties(")
1471cb0ef41Sopenharmony_ci    output.write(source_name)
1481cb0ef41Sopenharmony_ci    output.write(" PROPERTIES ")
1491cb0ef41Sopenharmony_ci    output.write(property_name)
1501cb0ef41Sopenharmony_ci    output.write(' "')
1511cb0ef41Sopenharmony_ci    for value in values:
1521cb0ef41Sopenharmony_ci        output.write(CMakeStringEscape(value))
1531cb0ef41Sopenharmony_ci        output.write(sep)
1541cb0ef41Sopenharmony_ci    output.write('")\n')
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_cidef SetFilesProperty(output, variable, property_name, values, sep):
1581cb0ef41Sopenharmony_ci    """Given a set of source files, sets the given property on them."""
1591cb0ef41Sopenharmony_ci    output.write("set_source_files_properties(")
1601cb0ef41Sopenharmony_ci    WriteVariable(output, variable)
1611cb0ef41Sopenharmony_ci    output.write(" PROPERTIES ")
1621cb0ef41Sopenharmony_ci    output.write(property_name)
1631cb0ef41Sopenharmony_ci    output.write(' "')
1641cb0ef41Sopenharmony_ci    for value in values:
1651cb0ef41Sopenharmony_ci        output.write(CMakeStringEscape(value))
1661cb0ef41Sopenharmony_ci        output.write(sep)
1671cb0ef41Sopenharmony_ci    output.write('")\n')
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_cidef SetTargetProperty(output, target_name, property_name, values, sep=""):
1711cb0ef41Sopenharmony_ci    """Given a target, sets the given property."""
1721cb0ef41Sopenharmony_ci    output.write("set_target_properties(")
1731cb0ef41Sopenharmony_ci    output.write(target_name)
1741cb0ef41Sopenharmony_ci    output.write(" PROPERTIES ")
1751cb0ef41Sopenharmony_ci    output.write(property_name)
1761cb0ef41Sopenharmony_ci    output.write(' "')
1771cb0ef41Sopenharmony_ci    for value in values:
1781cb0ef41Sopenharmony_ci        output.write(CMakeStringEscape(value))
1791cb0ef41Sopenharmony_ci        output.write(sep)
1801cb0ef41Sopenharmony_ci    output.write('")\n')
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_cidef SetVariable(output, variable_name, value):
1841cb0ef41Sopenharmony_ci    """Sets a CMake variable."""
1851cb0ef41Sopenharmony_ci    output.write("set(")
1861cb0ef41Sopenharmony_ci    output.write(variable_name)
1871cb0ef41Sopenharmony_ci    output.write(' "')
1881cb0ef41Sopenharmony_ci    output.write(CMakeStringEscape(value))
1891cb0ef41Sopenharmony_ci    output.write('")\n')
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_cidef SetVariableList(output, variable_name, values):
1931cb0ef41Sopenharmony_ci    """Sets a CMake variable to a list."""
1941cb0ef41Sopenharmony_ci    if not values:
1951cb0ef41Sopenharmony_ci        return SetVariable(output, variable_name, "")
1961cb0ef41Sopenharmony_ci    if len(values) == 1:
1971cb0ef41Sopenharmony_ci        return SetVariable(output, variable_name, values[0])
1981cb0ef41Sopenharmony_ci    output.write("list(APPEND ")
1991cb0ef41Sopenharmony_ci    output.write(variable_name)
2001cb0ef41Sopenharmony_ci    output.write('\n  "')
2011cb0ef41Sopenharmony_ci    output.write('"\n  "'.join([CMakeStringEscape(value) for value in values]))
2021cb0ef41Sopenharmony_ci    output.write('")\n')
2031cb0ef41Sopenharmony_ci
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_cidef UnsetVariable(output, variable_name):
2061cb0ef41Sopenharmony_ci    """Unsets a CMake variable."""
2071cb0ef41Sopenharmony_ci    output.write("unset(")
2081cb0ef41Sopenharmony_ci    output.write(variable_name)
2091cb0ef41Sopenharmony_ci    output.write(")\n")
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_cidef WriteVariable(output, variable_name, prepend=None):
2131cb0ef41Sopenharmony_ci    if prepend:
2141cb0ef41Sopenharmony_ci        output.write(prepend)
2151cb0ef41Sopenharmony_ci    output.write("${")
2161cb0ef41Sopenharmony_ci    output.write(variable_name)
2171cb0ef41Sopenharmony_ci    output.write("}")
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ciclass CMakeTargetType:
2211cb0ef41Sopenharmony_ci    def __init__(self, command, modifier, property_modifier):
2221cb0ef41Sopenharmony_ci        self.command = command
2231cb0ef41Sopenharmony_ci        self.modifier = modifier
2241cb0ef41Sopenharmony_ci        self.property_modifier = property_modifier
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_cicmake_target_type_from_gyp_target_type = {
2281cb0ef41Sopenharmony_ci    "executable": CMakeTargetType("add_executable", None, "RUNTIME"),
2291cb0ef41Sopenharmony_ci    "static_library": CMakeTargetType("add_library", "STATIC", "ARCHIVE"),
2301cb0ef41Sopenharmony_ci    "shared_library": CMakeTargetType("add_library", "SHARED", "LIBRARY"),
2311cb0ef41Sopenharmony_ci    "loadable_module": CMakeTargetType("add_library", "MODULE", "LIBRARY"),
2321cb0ef41Sopenharmony_ci    "none": CMakeTargetType("add_custom_target", "SOURCES", None),
2331cb0ef41Sopenharmony_ci}
2341cb0ef41Sopenharmony_ci
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_cidef StringToCMakeTargetName(a):
2371cb0ef41Sopenharmony_ci    """Converts the given string 'a' to a valid CMake target name.
2381cb0ef41Sopenharmony_ci
2391cb0ef41Sopenharmony_ci  All invalid characters are replaced by '_'.
2401cb0ef41Sopenharmony_ci  Invalid for cmake: ' ', '/', '(', ')', '"'
2411cb0ef41Sopenharmony_ci  Invalid for make: ':'
2421cb0ef41Sopenharmony_ci  Invalid for unknown reasons but cause failures: '.'
2431cb0ef41Sopenharmony_ci  """
2441cb0ef41Sopenharmony_ci    return a.translate(_maketrans(' /():."', "_______"))
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ci
2471cb0ef41Sopenharmony_cidef WriteActions(target_name, actions, extra_sources, extra_deps, path_to_gyp, output):
2481cb0ef41Sopenharmony_ci    """Write CMake for the 'actions' in the target.
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_ci  Args:
2511cb0ef41Sopenharmony_ci    target_name: the name of the CMake target being generated.
2521cb0ef41Sopenharmony_ci    actions: the Gyp 'actions' dict for this target.
2531cb0ef41Sopenharmony_ci    extra_sources: [(<cmake_src>, <src>)] to append with generated source files.
2541cb0ef41Sopenharmony_ci    extra_deps: [<cmake_taget>] to append with generated targets.
2551cb0ef41Sopenharmony_ci    path_to_gyp: relative path from CMakeLists.txt being generated to
2561cb0ef41Sopenharmony_ci        the Gyp file in which the target being generated is defined.
2571cb0ef41Sopenharmony_ci  """
2581cb0ef41Sopenharmony_ci    for action in actions:
2591cb0ef41Sopenharmony_ci        action_name = StringToCMakeTargetName(action["action_name"])
2601cb0ef41Sopenharmony_ci        action_target_name = f"{target_name}__{action_name}"
2611cb0ef41Sopenharmony_ci
2621cb0ef41Sopenharmony_ci        inputs = action["inputs"]
2631cb0ef41Sopenharmony_ci        inputs_name = action_target_name + "__input"
2641cb0ef41Sopenharmony_ci        SetVariableList(
2651cb0ef41Sopenharmony_ci            output,
2661cb0ef41Sopenharmony_ci            inputs_name,
2671cb0ef41Sopenharmony_ci            [NormjoinPathForceCMakeSource(path_to_gyp, dep) for dep in inputs],
2681cb0ef41Sopenharmony_ci        )
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ci        outputs = action["outputs"]
2711cb0ef41Sopenharmony_ci        cmake_outputs = [
2721cb0ef41Sopenharmony_ci            NormjoinPathForceCMakeSource(path_to_gyp, out) for out in outputs
2731cb0ef41Sopenharmony_ci        ]
2741cb0ef41Sopenharmony_ci        outputs_name = action_target_name + "__output"
2751cb0ef41Sopenharmony_ci        SetVariableList(output, outputs_name, cmake_outputs)
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_ci        # Build up a list of outputs.
2781cb0ef41Sopenharmony_ci        # Collect the output dirs we'll need.
2791cb0ef41Sopenharmony_ci        dirs = {dir for dir in (os.path.dirname(o) for o in outputs) if dir}
2801cb0ef41Sopenharmony_ci
2811cb0ef41Sopenharmony_ci        if int(action.get("process_outputs_as_sources", False)):
2821cb0ef41Sopenharmony_ci            extra_sources.extend(zip(cmake_outputs, outputs))
2831cb0ef41Sopenharmony_ci
2841cb0ef41Sopenharmony_ci        # add_custom_command
2851cb0ef41Sopenharmony_ci        output.write("add_custom_command(OUTPUT ")
2861cb0ef41Sopenharmony_ci        WriteVariable(output, outputs_name)
2871cb0ef41Sopenharmony_ci        output.write("\n")
2881cb0ef41Sopenharmony_ci
2891cb0ef41Sopenharmony_ci        if len(dirs) > 0:
2901cb0ef41Sopenharmony_ci            for directory in dirs:
2911cb0ef41Sopenharmony_ci                output.write("  COMMAND ${CMAKE_COMMAND} -E make_directory ")
2921cb0ef41Sopenharmony_ci                output.write(directory)
2931cb0ef41Sopenharmony_ci                output.write("\n")
2941cb0ef41Sopenharmony_ci
2951cb0ef41Sopenharmony_ci        output.write("  COMMAND ")
2961cb0ef41Sopenharmony_ci        output.write(gyp.common.EncodePOSIXShellList(action["action"]))
2971cb0ef41Sopenharmony_ci        output.write("\n")
2981cb0ef41Sopenharmony_ci
2991cb0ef41Sopenharmony_ci        output.write("  DEPENDS ")
3001cb0ef41Sopenharmony_ci        WriteVariable(output, inputs_name)
3011cb0ef41Sopenharmony_ci        output.write("\n")
3021cb0ef41Sopenharmony_ci
3031cb0ef41Sopenharmony_ci        output.write("  WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/")
3041cb0ef41Sopenharmony_ci        output.write(path_to_gyp)
3051cb0ef41Sopenharmony_ci        output.write("\n")
3061cb0ef41Sopenharmony_ci
3071cb0ef41Sopenharmony_ci        output.write("  COMMENT ")
3081cb0ef41Sopenharmony_ci        if "message" in action:
3091cb0ef41Sopenharmony_ci            output.write(action["message"])
3101cb0ef41Sopenharmony_ci        else:
3111cb0ef41Sopenharmony_ci            output.write(action_target_name)
3121cb0ef41Sopenharmony_ci        output.write("\n")
3131cb0ef41Sopenharmony_ci
3141cb0ef41Sopenharmony_ci        output.write("  VERBATIM\n")
3151cb0ef41Sopenharmony_ci        output.write(")\n")
3161cb0ef41Sopenharmony_ci
3171cb0ef41Sopenharmony_ci        # add_custom_target
3181cb0ef41Sopenharmony_ci        output.write("add_custom_target(")
3191cb0ef41Sopenharmony_ci        output.write(action_target_name)
3201cb0ef41Sopenharmony_ci        output.write("\n  DEPENDS ")
3211cb0ef41Sopenharmony_ci        WriteVariable(output, outputs_name)
3221cb0ef41Sopenharmony_ci        output.write("\n  SOURCES ")
3231cb0ef41Sopenharmony_ci        WriteVariable(output, inputs_name)
3241cb0ef41Sopenharmony_ci        output.write("\n)\n")
3251cb0ef41Sopenharmony_ci
3261cb0ef41Sopenharmony_ci        extra_deps.append(action_target_name)
3271cb0ef41Sopenharmony_ci
3281cb0ef41Sopenharmony_ci
3291cb0ef41Sopenharmony_cidef NormjoinRulePathForceCMakeSource(base_path, rel_path, rule_source):
3301cb0ef41Sopenharmony_ci    if rel_path.startswith(("${RULE_INPUT_PATH}", "${RULE_INPUT_DIRNAME}")):
3311cb0ef41Sopenharmony_ci        if any([rule_source.startswith(var) for var in FULL_PATH_VARS]):
3321cb0ef41Sopenharmony_ci            return rel_path
3331cb0ef41Sopenharmony_ci    return NormjoinPathForceCMakeSource(base_path, rel_path)
3341cb0ef41Sopenharmony_ci
3351cb0ef41Sopenharmony_ci
3361cb0ef41Sopenharmony_cidef WriteRules(target_name, rules, extra_sources, extra_deps, path_to_gyp, output):
3371cb0ef41Sopenharmony_ci    """Write CMake for the 'rules' in the target.
3381cb0ef41Sopenharmony_ci
3391cb0ef41Sopenharmony_ci  Args:
3401cb0ef41Sopenharmony_ci    target_name: the name of the CMake target being generated.
3411cb0ef41Sopenharmony_ci    actions: the Gyp 'actions' dict for this target.
3421cb0ef41Sopenharmony_ci    extra_sources: [(<cmake_src>, <src>)] to append with generated source files.
3431cb0ef41Sopenharmony_ci    extra_deps: [<cmake_taget>] to append with generated targets.
3441cb0ef41Sopenharmony_ci    path_to_gyp: relative path from CMakeLists.txt being generated to
3451cb0ef41Sopenharmony_ci        the Gyp file in which the target being generated is defined.
3461cb0ef41Sopenharmony_ci  """
3471cb0ef41Sopenharmony_ci    for rule in rules:
3481cb0ef41Sopenharmony_ci        rule_name = StringToCMakeTargetName(target_name + "__" + rule["rule_name"])
3491cb0ef41Sopenharmony_ci
3501cb0ef41Sopenharmony_ci        inputs = rule.get("inputs", [])
3511cb0ef41Sopenharmony_ci        inputs_name = rule_name + "__input"
3521cb0ef41Sopenharmony_ci        SetVariableList(
3531cb0ef41Sopenharmony_ci            output,
3541cb0ef41Sopenharmony_ci            inputs_name,
3551cb0ef41Sopenharmony_ci            [NormjoinPathForceCMakeSource(path_to_gyp, dep) for dep in inputs],
3561cb0ef41Sopenharmony_ci        )
3571cb0ef41Sopenharmony_ci        outputs = rule["outputs"]
3581cb0ef41Sopenharmony_ci        var_outputs = []
3591cb0ef41Sopenharmony_ci
3601cb0ef41Sopenharmony_ci        for count, rule_source in enumerate(rule.get("rule_sources", [])):
3611cb0ef41Sopenharmony_ci            action_name = rule_name + "_" + str(count)
3621cb0ef41Sopenharmony_ci
3631cb0ef41Sopenharmony_ci            rule_source_dirname, rule_source_basename = os.path.split(rule_source)
3641cb0ef41Sopenharmony_ci            rule_source_root, rule_source_ext = os.path.splitext(rule_source_basename)
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_ci            SetVariable(output, "RULE_INPUT_PATH", rule_source)
3671cb0ef41Sopenharmony_ci            SetVariable(output, "RULE_INPUT_DIRNAME", rule_source_dirname)
3681cb0ef41Sopenharmony_ci            SetVariable(output, "RULE_INPUT_NAME", rule_source_basename)
3691cb0ef41Sopenharmony_ci            SetVariable(output, "RULE_INPUT_ROOT", rule_source_root)
3701cb0ef41Sopenharmony_ci            SetVariable(output, "RULE_INPUT_EXT", rule_source_ext)
3711cb0ef41Sopenharmony_ci
3721cb0ef41Sopenharmony_ci            # Build up a list of outputs.
3731cb0ef41Sopenharmony_ci            # Collect the output dirs we'll need.
3741cb0ef41Sopenharmony_ci            dirs = {dir for dir in (os.path.dirname(o) for o in outputs) if dir}
3751cb0ef41Sopenharmony_ci
3761cb0ef41Sopenharmony_ci            # Create variables for the output, as 'local' variable will be unset.
3771cb0ef41Sopenharmony_ci            these_outputs = []
3781cb0ef41Sopenharmony_ci            for output_index, out in enumerate(outputs):
3791cb0ef41Sopenharmony_ci                output_name = action_name + "_" + str(output_index)
3801cb0ef41Sopenharmony_ci                SetVariable(
3811cb0ef41Sopenharmony_ci                    output,
3821cb0ef41Sopenharmony_ci                    output_name,
3831cb0ef41Sopenharmony_ci                    NormjoinRulePathForceCMakeSource(path_to_gyp, out, rule_source),
3841cb0ef41Sopenharmony_ci                )
3851cb0ef41Sopenharmony_ci                if int(rule.get("process_outputs_as_sources", False)):
3861cb0ef41Sopenharmony_ci                    extra_sources.append(("${" + output_name + "}", out))
3871cb0ef41Sopenharmony_ci                these_outputs.append("${" + output_name + "}")
3881cb0ef41Sopenharmony_ci                var_outputs.append("${" + output_name + "}")
3891cb0ef41Sopenharmony_ci
3901cb0ef41Sopenharmony_ci            # add_custom_command
3911cb0ef41Sopenharmony_ci            output.write("add_custom_command(OUTPUT\n")
3921cb0ef41Sopenharmony_ci            for out in these_outputs:
3931cb0ef41Sopenharmony_ci                output.write("  ")
3941cb0ef41Sopenharmony_ci                output.write(out)
3951cb0ef41Sopenharmony_ci                output.write("\n")
3961cb0ef41Sopenharmony_ci
3971cb0ef41Sopenharmony_ci            for directory in dirs:
3981cb0ef41Sopenharmony_ci                output.write("  COMMAND ${CMAKE_COMMAND} -E make_directory ")
3991cb0ef41Sopenharmony_ci                output.write(directory)
4001cb0ef41Sopenharmony_ci                output.write("\n")
4011cb0ef41Sopenharmony_ci
4021cb0ef41Sopenharmony_ci            output.write("  COMMAND ")
4031cb0ef41Sopenharmony_ci            output.write(gyp.common.EncodePOSIXShellList(rule["action"]))
4041cb0ef41Sopenharmony_ci            output.write("\n")
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_ci            output.write("  DEPENDS ")
4071cb0ef41Sopenharmony_ci            WriteVariable(output, inputs_name)
4081cb0ef41Sopenharmony_ci            output.write(" ")
4091cb0ef41Sopenharmony_ci            output.write(NormjoinPath(path_to_gyp, rule_source))
4101cb0ef41Sopenharmony_ci            output.write("\n")
4111cb0ef41Sopenharmony_ci
4121cb0ef41Sopenharmony_ci            # CMAKE_CURRENT_LIST_DIR is where the CMakeLists.txt lives.
4131cb0ef41Sopenharmony_ci            # The cwd is the current build directory.
4141cb0ef41Sopenharmony_ci            output.write("  WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/")
4151cb0ef41Sopenharmony_ci            output.write(path_to_gyp)
4161cb0ef41Sopenharmony_ci            output.write("\n")
4171cb0ef41Sopenharmony_ci
4181cb0ef41Sopenharmony_ci            output.write("  COMMENT ")
4191cb0ef41Sopenharmony_ci            if "message" in rule:
4201cb0ef41Sopenharmony_ci                output.write(rule["message"])
4211cb0ef41Sopenharmony_ci            else:
4221cb0ef41Sopenharmony_ci                output.write(action_name)
4231cb0ef41Sopenharmony_ci            output.write("\n")
4241cb0ef41Sopenharmony_ci
4251cb0ef41Sopenharmony_ci            output.write("  VERBATIM\n")
4261cb0ef41Sopenharmony_ci            output.write(")\n")
4271cb0ef41Sopenharmony_ci
4281cb0ef41Sopenharmony_ci            UnsetVariable(output, "RULE_INPUT_PATH")
4291cb0ef41Sopenharmony_ci            UnsetVariable(output, "RULE_INPUT_DIRNAME")
4301cb0ef41Sopenharmony_ci            UnsetVariable(output, "RULE_INPUT_NAME")
4311cb0ef41Sopenharmony_ci            UnsetVariable(output, "RULE_INPUT_ROOT")
4321cb0ef41Sopenharmony_ci            UnsetVariable(output, "RULE_INPUT_EXT")
4331cb0ef41Sopenharmony_ci
4341cb0ef41Sopenharmony_ci        # add_custom_target
4351cb0ef41Sopenharmony_ci        output.write("add_custom_target(")
4361cb0ef41Sopenharmony_ci        output.write(rule_name)
4371cb0ef41Sopenharmony_ci        output.write(" DEPENDS\n")
4381cb0ef41Sopenharmony_ci        for out in var_outputs:
4391cb0ef41Sopenharmony_ci            output.write("  ")
4401cb0ef41Sopenharmony_ci            output.write(out)
4411cb0ef41Sopenharmony_ci            output.write("\n")
4421cb0ef41Sopenharmony_ci        output.write("SOURCES ")
4431cb0ef41Sopenharmony_ci        WriteVariable(output, inputs_name)
4441cb0ef41Sopenharmony_ci        output.write("\n")
4451cb0ef41Sopenharmony_ci        for rule_source in rule.get("rule_sources", []):
4461cb0ef41Sopenharmony_ci            output.write("  ")
4471cb0ef41Sopenharmony_ci            output.write(NormjoinPath(path_to_gyp, rule_source))
4481cb0ef41Sopenharmony_ci            output.write("\n")
4491cb0ef41Sopenharmony_ci        output.write(")\n")
4501cb0ef41Sopenharmony_ci
4511cb0ef41Sopenharmony_ci        extra_deps.append(rule_name)
4521cb0ef41Sopenharmony_ci
4531cb0ef41Sopenharmony_ci
4541cb0ef41Sopenharmony_cidef WriteCopies(target_name, copies, extra_deps, path_to_gyp, output):
4551cb0ef41Sopenharmony_ci    """Write CMake for the 'copies' in the target.
4561cb0ef41Sopenharmony_ci
4571cb0ef41Sopenharmony_ci  Args:
4581cb0ef41Sopenharmony_ci    target_name: the name of the CMake target being generated.
4591cb0ef41Sopenharmony_ci    actions: the Gyp 'actions' dict for this target.
4601cb0ef41Sopenharmony_ci    extra_deps: [<cmake_taget>] to append with generated targets.
4611cb0ef41Sopenharmony_ci    path_to_gyp: relative path from CMakeLists.txt being generated to
4621cb0ef41Sopenharmony_ci        the Gyp file in which the target being generated is defined.
4631cb0ef41Sopenharmony_ci  """
4641cb0ef41Sopenharmony_ci    copy_name = target_name + "__copies"
4651cb0ef41Sopenharmony_ci
4661cb0ef41Sopenharmony_ci    # CMake gets upset with custom targets with OUTPUT which specify no output.
4671cb0ef41Sopenharmony_ci    have_copies = any(copy["files"] for copy in copies)
4681cb0ef41Sopenharmony_ci    if not have_copies:
4691cb0ef41Sopenharmony_ci        output.write("add_custom_target(")
4701cb0ef41Sopenharmony_ci        output.write(copy_name)
4711cb0ef41Sopenharmony_ci        output.write(")\n")
4721cb0ef41Sopenharmony_ci        extra_deps.append(copy_name)
4731cb0ef41Sopenharmony_ci        return
4741cb0ef41Sopenharmony_ci
4751cb0ef41Sopenharmony_ci    class Copy:
4761cb0ef41Sopenharmony_ci        def __init__(self, ext, command):
4771cb0ef41Sopenharmony_ci            self.cmake_inputs = []
4781cb0ef41Sopenharmony_ci            self.cmake_outputs = []
4791cb0ef41Sopenharmony_ci            self.gyp_inputs = []
4801cb0ef41Sopenharmony_ci            self.gyp_outputs = []
4811cb0ef41Sopenharmony_ci            self.ext = ext
4821cb0ef41Sopenharmony_ci            self.inputs_name = None
4831cb0ef41Sopenharmony_ci            self.outputs_name = None
4841cb0ef41Sopenharmony_ci            self.command = command
4851cb0ef41Sopenharmony_ci
4861cb0ef41Sopenharmony_ci    file_copy = Copy("", "copy")
4871cb0ef41Sopenharmony_ci    dir_copy = Copy("_dirs", "copy_directory")
4881cb0ef41Sopenharmony_ci
4891cb0ef41Sopenharmony_ci    for copy in copies:
4901cb0ef41Sopenharmony_ci        files = copy["files"]
4911cb0ef41Sopenharmony_ci        destination = copy["destination"]
4921cb0ef41Sopenharmony_ci        for src in files:
4931cb0ef41Sopenharmony_ci            path = os.path.normpath(src)
4941cb0ef41Sopenharmony_ci            basename = os.path.split(path)[1]
4951cb0ef41Sopenharmony_ci            dst = os.path.join(destination, basename)
4961cb0ef41Sopenharmony_ci
4971cb0ef41Sopenharmony_ci            copy = file_copy if os.path.basename(src) else dir_copy
4981cb0ef41Sopenharmony_ci
4991cb0ef41Sopenharmony_ci            copy.cmake_inputs.append(NormjoinPathForceCMakeSource(path_to_gyp, src))
5001cb0ef41Sopenharmony_ci            copy.cmake_outputs.append(NormjoinPathForceCMakeSource(path_to_gyp, dst))
5011cb0ef41Sopenharmony_ci            copy.gyp_inputs.append(src)
5021cb0ef41Sopenharmony_ci            copy.gyp_outputs.append(dst)
5031cb0ef41Sopenharmony_ci
5041cb0ef41Sopenharmony_ci    for copy in (file_copy, dir_copy):
5051cb0ef41Sopenharmony_ci        if copy.cmake_inputs:
5061cb0ef41Sopenharmony_ci            copy.inputs_name = copy_name + "__input" + copy.ext
5071cb0ef41Sopenharmony_ci            SetVariableList(output, copy.inputs_name, copy.cmake_inputs)
5081cb0ef41Sopenharmony_ci
5091cb0ef41Sopenharmony_ci            copy.outputs_name = copy_name + "__output" + copy.ext
5101cb0ef41Sopenharmony_ci            SetVariableList(output, copy.outputs_name, copy.cmake_outputs)
5111cb0ef41Sopenharmony_ci
5121cb0ef41Sopenharmony_ci    # add_custom_command
5131cb0ef41Sopenharmony_ci    output.write("add_custom_command(\n")
5141cb0ef41Sopenharmony_ci
5151cb0ef41Sopenharmony_ci    output.write("OUTPUT")
5161cb0ef41Sopenharmony_ci    for copy in (file_copy, dir_copy):
5171cb0ef41Sopenharmony_ci        if copy.outputs_name:
5181cb0ef41Sopenharmony_ci            WriteVariable(output, copy.outputs_name, " ")
5191cb0ef41Sopenharmony_ci    output.write("\n")
5201cb0ef41Sopenharmony_ci
5211cb0ef41Sopenharmony_ci    for copy in (file_copy, dir_copy):
5221cb0ef41Sopenharmony_ci        for src, dst in zip(copy.gyp_inputs, copy.gyp_outputs):
5231cb0ef41Sopenharmony_ci            # 'cmake -E copy src dst' will create the 'dst' directory if needed.
5241cb0ef41Sopenharmony_ci            output.write("COMMAND ${CMAKE_COMMAND} -E %s " % copy.command)
5251cb0ef41Sopenharmony_ci            output.write(src)
5261cb0ef41Sopenharmony_ci            output.write(" ")
5271cb0ef41Sopenharmony_ci            output.write(dst)
5281cb0ef41Sopenharmony_ci            output.write("\n")
5291cb0ef41Sopenharmony_ci
5301cb0ef41Sopenharmony_ci    output.write("DEPENDS")
5311cb0ef41Sopenharmony_ci    for copy in (file_copy, dir_copy):
5321cb0ef41Sopenharmony_ci        if copy.inputs_name:
5331cb0ef41Sopenharmony_ci            WriteVariable(output, copy.inputs_name, " ")
5341cb0ef41Sopenharmony_ci    output.write("\n")
5351cb0ef41Sopenharmony_ci
5361cb0ef41Sopenharmony_ci    output.write("WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/")
5371cb0ef41Sopenharmony_ci    output.write(path_to_gyp)
5381cb0ef41Sopenharmony_ci    output.write("\n")
5391cb0ef41Sopenharmony_ci
5401cb0ef41Sopenharmony_ci    output.write("COMMENT Copying for ")
5411cb0ef41Sopenharmony_ci    output.write(target_name)
5421cb0ef41Sopenharmony_ci    output.write("\n")
5431cb0ef41Sopenharmony_ci
5441cb0ef41Sopenharmony_ci    output.write("VERBATIM\n")
5451cb0ef41Sopenharmony_ci    output.write(")\n")
5461cb0ef41Sopenharmony_ci
5471cb0ef41Sopenharmony_ci    # add_custom_target
5481cb0ef41Sopenharmony_ci    output.write("add_custom_target(")
5491cb0ef41Sopenharmony_ci    output.write(copy_name)
5501cb0ef41Sopenharmony_ci    output.write("\n  DEPENDS")
5511cb0ef41Sopenharmony_ci    for copy in (file_copy, dir_copy):
5521cb0ef41Sopenharmony_ci        if copy.outputs_name:
5531cb0ef41Sopenharmony_ci            WriteVariable(output, copy.outputs_name, " ")
5541cb0ef41Sopenharmony_ci    output.write("\n  SOURCES")
5551cb0ef41Sopenharmony_ci    if file_copy.inputs_name:
5561cb0ef41Sopenharmony_ci        WriteVariable(output, file_copy.inputs_name, " ")
5571cb0ef41Sopenharmony_ci    output.write("\n)\n")
5581cb0ef41Sopenharmony_ci
5591cb0ef41Sopenharmony_ci    extra_deps.append(copy_name)
5601cb0ef41Sopenharmony_ci
5611cb0ef41Sopenharmony_ci
5621cb0ef41Sopenharmony_cidef CreateCMakeTargetBaseName(qualified_target):
5631cb0ef41Sopenharmony_ci    """This is the name we would like the target to have."""
5641cb0ef41Sopenharmony_ci    _, gyp_target_name, gyp_target_toolset = gyp.common.ParseQualifiedTarget(
5651cb0ef41Sopenharmony_ci        qualified_target
5661cb0ef41Sopenharmony_ci    )
5671cb0ef41Sopenharmony_ci    cmake_target_base_name = gyp_target_name
5681cb0ef41Sopenharmony_ci    if gyp_target_toolset and gyp_target_toolset != "target":
5691cb0ef41Sopenharmony_ci        cmake_target_base_name += "_" + gyp_target_toolset
5701cb0ef41Sopenharmony_ci    return StringToCMakeTargetName(cmake_target_base_name)
5711cb0ef41Sopenharmony_ci
5721cb0ef41Sopenharmony_ci
5731cb0ef41Sopenharmony_cidef CreateCMakeTargetFullName(qualified_target):
5741cb0ef41Sopenharmony_ci    """An unambiguous name for the target."""
5751cb0ef41Sopenharmony_ci    gyp_file, gyp_target_name, gyp_target_toolset = gyp.common.ParseQualifiedTarget(
5761cb0ef41Sopenharmony_ci        qualified_target
5771cb0ef41Sopenharmony_ci    )
5781cb0ef41Sopenharmony_ci    cmake_target_full_name = gyp_file + ":" + gyp_target_name
5791cb0ef41Sopenharmony_ci    if gyp_target_toolset and gyp_target_toolset != "target":
5801cb0ef41Sopenharmony_ci        cmake_target_full_name += "_" + gyp_target_toolset
5811cb0ef41Sopenharmony_ci    return StringToCMakeTargetName(cmake_target_full_name)
5821cb0ef41Sopenharmony_ci
5831cb0ef41Sopenharmony_ci
5841cb0ef41Sopenharmony_ciclass CMakeNamer:
5851cb0ef41Sopenharmony_ci    """Converts Gyp target names into CMake target names.
5861cb0ef41Sopenharmony_ci
5871cb0ef41Sopenharmony_ci  CMake requires that target names be globally unique. One way to ensure
5881cb0ef41Sopenharmony_ci  this is to fully qualify the names of the targets. Unfortunately, this
5891cb0ef41Sopenharmony_ci  ends up with all targets looking like "chrome_chrome_gyp_chrome" instead
5901cb0ef41Sopenharmony_ci  of just "chrome". If this generator were only interested in building, it
5911cb0ef41Sopenharmony_ci  would be possible to fully qualify all target names, then create
5921cb0ef41Sopenharmony_ci  unqualified target names which depend on all qualified targets which
5931cb0ef41Sopenharmony_ci  should have had that name. This is more or less what the 'make' generator
5941cb0ef41Sopenharmony_ci  does with aliases. However, one goal of this generator is to create CMake
5951cb0ef41Sopenharmony_ci  files for use with IDEs, and fully qualified names are not as user
5961cb0ef41Sopenharmony_ci  friendly.
5971cb0ef41Sopenharmony_ci
5981cb0ef41Sopenharmony_ci  Since target name collision is rare, we do the above only when required.
5991cb0ef41Sopenharmony_ci
6001cb0ef41Sopenharmony_ci  Toolset variants are always qualified from the base, as this is required for
6011cb0ef41Sopenharmony_ci  building. However, it also makes sense for an IDE, as it is possible for
6021cb0ef41Sopenharmony_ci  defines to be different.
6031cb0ef41Sopenharmony_ci  """
6041cb0ef41Sopenharmony_ci
6051cb0ef41Sopenharmony_ci    def __init__(self, target_list):
6061cb0ef41Sopenharmony_ci        self.cmake_target_base_names_conficting = set()
6071cb0ef41Sopenharmony_ci
6081cb0ef41Sopenharmony_ci        cmake_target_base_names_seen = set()
6091cb0ef41Sopenharmony_ci        for qualified_target in target_list:
6101cb0ef41Sopenharmony_ci            cmake_target_base_name = CreateCMakeTargetBaseName(qualified_target)
6111cb0ef41Sopenharmony_ci
6121cb0ef41Sopenharmony_ci            if cmake_target_base_name not in cmake_target_base_names_seen:
6131cb0ef41Sopenharmony_ci                cmake_target_base_names_seen.add(cmake_target_base_name)
6141cb0ef41Sopenharmony_ci            else:
6151cb0ef41Sopenharmony_ci                self.cmake_target_base_names_conficting.add(cmake_target_base_name)
6161cb0ef41Sopenharmony_ci
6171cb0ef41Sopenharmony_ci    def CreateCMakeTargetName(self, qualified_target):
6181cb0ef41Sopenharmony_ci        base_name = CreateCMakeTargetBaseName(qualified_target)
6191cb0ef41Sopenharmony_ci        if base_name in self.cmake_target_base_names_conficting:
6201cb0ef41Sopenharmony_ci            return CreateCMakeTargetFullName(qualified_target)
6211cb0ef41Sopenharmony_ci        return base_name
6221cb0ef41Sopenharmony_ci
6231cb0ef41Sopenharmony_ci
6241cb0ef41Sopenharmony_cidef WriteTarget(
6251cb0ef41Sopenharmony_ci    namer,
6261cb0ef41Sopenharmony_ci    qualified_target,
6271cb0ef41Sopenharmony_ci    target_dicts,
6281cb0ef41Sopenharmony_ci    build_dir,
6291cb0ef41Sopenharmony_ci    config_to_use,
6301cb0ef41Sopenharmony_ci    options,
6311cb0ef41Sopenharmony_ci    generator_flags,
6321cb0ef41Sopenharmony_ci    all_qualified_targets,
6331cb0ef41Sopenharmony_ci    flavor,
6341cb0ef41Sopenharmony_ci    output,
6351cb0ef41Sopenharmony_ci):
6361cb0ef41Sopenharmony_ci    # The make generator does this always.
6371cb0ef41Sopenharmony_ci    # TODO: It would be nice to be able to tell CMake all dependencies.
6381cb0ef41Sopenharmony_ci    circular_libs = generator_flags.get("circular", True)
6391cb0ef41Sopenharmony_ci
6401cb0ef41Sopenharmony_ci    if not generator_flags.get("standalone", False):
6411cb0ef41Sopenharmony_ci        output.write("\n#")
6421cb0ef41Sopenharmony_ci        output.write(qualified_target)
6431cb0ef41Sopenharmony_ci        output.write("\n")
6441cb0ef41Sopenharmony_ci
6451cb0ef41Sopenharmony_ci    gyp_file, _, _ = gyp.common.ParseQualifiedTarget(qualified_target)
6461cb0ef41Sopenharmony_ci    rel_gyp_file = gyp.common.RelativePath(gyp_file, options.toplevel_dir)
6471cb0ef41Sopenharmony_ci    rel_gyp_dir = os.path.dirname(rel_gyp_file)
6481cb0ef41Sopenharmony_ci
6491cb0ef41Sopenharmony_ci    # Relative path from build dir to top dir.
6501cb0ef41Sopenharmony_ci    build_to_top = gyp.common.InvertRelativePath(build_dir, options.toplevel_dir)
6511cb0ef41Sopenharmony_ci    # Relative path from build dir to gyp dir.
6521cb0ef41Sopenharmony_ci    build_to_gyp = os.path.join(build_to_top, rel_gyp_dir)
6531cb0ef41Sopenharmony_ci
6541cb0ef41Sopenharmony_ci    path_from_cmakelists_to_gyp = build_to_gyp
6551cb0ef41Sopenharmony_ci
6561cb0ef41Sopenharmony_ci    spec = target_dicts.get(qualified_target, {})
6571cb0ef41Sopenharmony_ci    config = spec.get("configurations", {}).get(config_to_use, {})
6581cb0ef41Sopenharmony_ci
6591cb0ef41Sopenharmony_ci    xcode_settings = None
6601cb0ef41Sopenharmony_ci    if flavor == "mac":
6611cb0ef41Sopenharmony_ci        xcode_settings = gyp.xcode_emulation.XcodeSettings(spec)
6621cb0ef41Sopenharmony_ci
6631cb0ef41Sopenharmony_ci    target_name = spec.get("target_name", "<missing target name>")
6641cb0ef41Sopenharmony_ci    target_type = spec.get("type", "<missing target type>")
6651cb0ef41Sopenharmony_ci    target_toolset = spec.get("toolset")
6661cb0ef41Sopenharmony_ci
6671cb0ef41Sopenharmony_ci    cmake_target_type = cmake_target_type_from_gyp_target_type.get(target_type)
6681cb0ef41Sopenharmony_ci    if cmake_target_type is None:
6691cb0ef41Sopenharmony_ci        print(
6701cb0ef41Sopenharmony_ci            "Target %s has unknown target type %s, skipping."
6711cb0ef41Sopenharmony_ci            % (target_name, target_type)
6721cb0ef41Sopenharmony_ci        )
6731cb0ef41Sopenharmony_ci        return
6741cb0ef41Sopenharmony_ci
6751cb0ef41Sopenharmony_ci    SetVariable(output, "TARGET", target_name)
6761cb0ef41Sopenharmony_ci    SetVariable(output, "TOOLSET", target_toolset)
6771cb0ef41Sopenharmony_ci
6781cb0ef41Sopenharmony_ci    cmake_target_name = namer.CreateCMakeTargetName(qualified_target)
6791cb0ef41Sopenharmony_ci
6801cb0ef41Sopenharmony_ci    extra_sources = []
6811cb0ef41Sopenharmony_ci    extra_deps = []
6821cb0ef41Sopenharmony_ci
6831cb0ef41Sopenharmony_ci    # Actions must come first, since they can generate more OBJs for use below.
6841cb0ef41Sopenharmony_ci    if "actions" in spec:
6851cb0ef41Sopenharmony_ci        WriteActions(
6861cb0ef41Sopenharmony_ci            cmake_target_name,
6871cb0ef41Sopenharmony_ci            spec["actions"],
6881cb0ef41Sopenharmony_ci            extra_sources,
6891cb0ef41Sopenharmony_ci            extra_deps,
6901cb0ef41Sopenharmony_ci            path_from_cmakelists_to_gyp,
6911cb0ef41Sopenharmony_ci            output,
6921cb0ef41Sopenharmony_ci        )
6931cb0ef41Sopenharmony_ci
6941cb0ef41Sopenharmony_ci    # Rules must be early like actions.
6951cb0ef41Sopenharmony_ci    if "rules" in spec:
6961cb0ef41Sopenharmony_ci        WriteRules(
6971cb0ef41Sopenharmony_ci            cmake_target_name,
6981cb0ef41Sopenharmony_ci            spec["rules"],
6991cb0ef41Sopenharmony_ci            extra_sources,
7001cb0ef41Sopenharmony_ci            extra_deps,
7011cb0ef41Sopenharmony_ci            path_from_cmakelists_to_gyp,
7021cb0ef41Sopenharmony_ci            output,
7031cb0ef41Sopenharmony_ci        )
7041cb0ef41Sopenharmony_ci
7051cb0ef41Sopenharmony_ci    # Copies
7061cb0ef41Sopenharmony_ci    if "copies" in spec:
7071cb0ef41Sopenharmony_ci        WriteCopies(
7081cb0ef41Sopenharmony_ci            cmake_target_name,
7091cb0ef41Sopenharmony_ci            spec["copies"],
7101cb0ef41Sopenharmony_ci            extra_deps,
7111cb0ef41Sopenharmony_ci            path_from_cmakelists_to_gyp,
7121cb0ef41Sopenharmony_ci            output,
7131cb0ef41Sopenharmony_ci        )
7141cb0ef41Sopenharmony_ci
7151cb0ef41Sopenharmony_ci    # Target and sources
7161cb0ef41Sopenharmony_ci    srcs = spec.get("sources", [])
7171cb0ef41Sopenharmony_ci
7181cb0ef41Sopenharmony_ci    # Gyp separates the sheep from the goats based on file extensions.
7191cb0ef41Sopenharmony_ci    # A full separation is done here because of flag handing (see below).
7201cb0ef41Sopenharmony_ci    s_sources = []
7211cb0ef41Sopenharmony_ci    c_sources = []
7221cb0ef41Sopenharmony_ci    cxx_sources = []
7231cb0ef41Sopenharmony_ci    linkable_sources = []
7241cb0ef41Sopenharmony_ci    other_sources = []
7251cb0ef41Sopenharmony_ci    for src in srcs:
7261cb0ef41Sopenharmony_ci        _, ext = os.path.splitext(src)
7271cb0ef41Sopenharmony_ci        src_type = COMPILABLE_EXTENSIONS.get(ext, None)
7281cb0ef41Sopenharmony_ci        src_norm_path = NormjoinPath(path_from_cmakelists_to_gyp, src)
7291cb0ef41Sopenharmony_ci
7301cb0ef41Sopenharmony_ci        if src_type == "s":
7311cb0ef41Sopenharmony_ci            s_sources.append(src_norm_path)
7321cb0ef41Sopenharmony_ci        elif src_type == "cc":
7331cb0ef41Sopenharmony_ci            c_sources.append(src_norm_path)
7341cb0ef41Sopenharmony_ci        elif src_type == "cxx":
7351cb0ef41Sopenharmony_ci            cxx_sources.append(src_norm_path)
7361cb0ef41Sopenharmony_ci        elif Linkable(ext):
7371cb0ef41Sopenharmony_ci            linkable_sources.append(src_norm_path)
7381cb0ef41Sopenharmony_ci        else:
7391cb0ef41Sopenharmony_ci            other_sources.append(src_norm_path)
7401cb0ef41Sopenharmony_ci
7411cb0ef41Sopenharmony_ci    for extra_source in extra_sources:
7421cb0ef41Sopenharmony_ci        src, real_source = extra_source
7431cb0ef41Sopenharmony_ci        _, ext = os.path.splitext(real_source)
7441cb0ef41Sopenharmony_ci        src_type = COMPILABLE_EXTENSIONS.get(ext, None)
7451cb0ef41Sopenharmony_ci
7461cb0ef41Sopenharmony_ci        if src_type == "s":
7471cb0ef41Sopenharmony_ci            s_sources.append(src)
7481cb0ef41Sopenharmony_ci        elif src_type == "cc":
7491cb0ef41Sopenharmony_ci            c_sources.append(src)
7501cb0ef41Sopenharmony_ci        elif src_type == "cxx":
7511cb0ef41Sopenharmony_ci            cxx_sources.append(src)
7521cb0ef41Sopenharmony_ci        elif Linkable(ext):
7531cb0ef41Sopenharmony_ci            linkable_sources.append(src)
7541cb0ef41Sopenharmony_ci        else:
7551cb0ef41Sopenharmony_ci            other_sources.append(src)
7561cb0ef41Sopenharmony_ci
7571cb0ef41Sopenharmony_ci    s_sources_name = None
7581cb0ef41Sopenharmony_ci    if s_sources:
7591cb0ef41Sopenharmony_ci        s_sources_name = cmake_target_name + "__asm_srcs"
7601cb0ef41Sopenharmony_ci        SetVariableList(output, s_sources_name, s_sources)
7611cb0ef41Sopenharmony_ci
7621cb0ef41Sopenharmony_ci    c_sources_name = None
7631cb0ef41Sopenharmony_ci    if c_sources:
7641cb0ef41Sopenharmony_ci        c_sources_name = cmake_target_name + "__c_srcs"
7651cb0ef41Sopenharmony_ci        SetVariableList(output, c_sources_name, c_sources)
7661cb0ef41Sopenharmony_ci
7671cb0ef41Sopenharmony_ci    cxx_sources_name = None
7681cb0ef41Sopenharmony_ci    if cxx_sources:
7691cb0ef41Sopenharmony_ci        cxx_sources_name = cmake_target_name + "__cxx_srcs"
7701cb0ef41Sopenharmony_ci        SetVariableList(output, cxx_sources_name, cxx_sources)
7711cb0ef41Sopenharmony_ci
7721cb0ef41Sopenharmony_ci    linkable_sources_name = None
7731cb0ef41Sopenharmony_ci    if linkable_sources:
7741cb0ef41Sopenharmony_ci        linkable_sources_name = cmake_target_name + "__linkable_srcs"
7751cb0ef41Sopenharmony_ci        SetVariableList(output, linkable_sources_name, linkable_sources)
7761cb0ef41Sopenharmony_ci
7771cb0ef41Sopenharmony_ci    other_sources_name = None
7781cb0ef41Sopenharmony_ci    if other_sources:
7791cb0ef41Sopenharmony_ci        other_sources_name = cmake_target_name + "__other_srcs"
7801cb0ef41Sopenharmony_ci        SetVariableList(output, other_sources_name, other_sources)
7811cb0ef41Sopenharmony_ci
7821cb0ef41Sopenharmony_ci    # CMake gets upset when executable targets provide no sources.
7831cb0ef41Sopenharmony_ci    # http://www.cmake.org/pipermail/cmake/2010-July/038461.html
7841cb0ef41Sopenharmony_ci    dummy_sources_name = None
7851cb0ef41Sopenharmony_ci    has_sources = (
7861cb0ef41Sopenharmony_ci        s_sources_name
7871cb0ef41Sopenharmony_ci        or c_sources_name
7881cb0ef41Sopenharmony_ci        or cxx_sources_name
7891cb0ef41Sopenharmony_ci        or linkable_sources_name
7901cb0ef41Sopenharmony_ci        or other_sources_name
7911cb0ef41Sopenharmony_ci    )
7921cb0ef41Sopenharmony_ci    if target_type == "executable" and not has_sources:
7931cb0ef41Sopenharmony_ci        dummy_sources_name = cmake_target_name + "__dummy_srcs"
7941cb0ef41Sopenharmony_ci        SetVariable(
7951cb0ef41Sopenharmony_ci            output, dummy_sources_name, "${obj}.${TOOLSET}/${TARGET}/genc/dummy.c"
7961cb0ef41Sopenharmony_ci        )
7971cb0ef41Sopenharmony_ci        output.write('if(NOT EXISTS "')
7981cb0ef41Sopenharmony_ci        WriteVariable(output, dummy_sources_name)
7991cb0ef41Sopenharmony_ci        output.write('")\n')
8001cb0ef41Sopenharmony_ci        output.write('  file(WRITE "')
8011cb0ef41Sopenharmony_ci        WriteVariable(output, dummy_sources_name)
8021cb0ef41Sopenharmony_ci        output.write('" "")\n')
8031cb0ef41Sopenharmony_ci        output.write("endif()\n")
8041cb0ef41Sopenharmony_ci
8051cb0ef41Sopenharmony_ci    # CMake is opposed to setting linker directories and considers the practice
8061cb0ef41Sopenharmony_ci    # of setting linker directories dangerous. Instead, it favors the use of
8071cb0ef41Sopenharmony_ci    # find_library and passing absolute paths to target_link_libraries.
8081cb0ef41Sopenharmony_ci    # However, CMake does provide the command link_directories, which adds
8091cb0ef41Sopenharmony_ci    # link directories to targets defined after it is called.
8101cb0ef41Sopenharmony_ci    # As a result, link_directories must come before the target definition.
8111cb0ef41Sopenharmony_ci    # CMake unfortunately has no means of removing entries from LINK_DIRECTORIES.
8121cb0ef41Sopenharmony_ci    library_dirs = config.get("library_dirs")
8131cb0ef41Sopenharmony_ci    if library_dirs is not None:
8141cb0ef41Sopenharmony_ci        output.write("link_directories(")
8151cb0ef41Sopenharmony_ci        for library_dir in library_dirs:
8161cb0ef41Sopenharmony_ci            output.write(" ")
8171cb0ef41Sopenharmony_ci            output.write(NormjoinPath(path_from_cmakelists_to_gyp, library_dir))
8181cb0ef41Sopenharmony_ci            output.write("\n")
8191cb0ef41Sopenharmony_ci        output.write(")\n")
8201cb0ef41Sopenharmony_ci
8211cb0ef41Sopenharmony_ci    output.write(cmake_target_type.command)
8221cb0ef41Sopenharmony_ci    output.write("(")
8231cb0ef41Sopenharmony_ci    output.write(cmake_target_name)
8241cb0ef41Sopenharmony_ci
8251cb0ef41Sopenharmony_ci    if cmake_target_type.modifier is not None:
8261cb0ef41Sopenharmony_ci        output.write(" ")
8271cb0ef41Sopenharmony_ci        output.write(cmake_target_type.modifier)
8281cb0ef41Sopenharmony_ci
8291cb0ef41Sopenharmony_ci    if s_sources_name:
8301cb0ef41Sopenharmony_ci        WriteVariable(output, s_sources_name, " ")
8311cb0ef41Sopenharmony_ci    if c_sources_name:
8321cb0ef41Sopenharmony_ci        WriteVariable(output, c_sources_name, " ")
8331cb0ef41Sopenharmony_ci    if cxx_sources_name:
8341cb0ef41Sopenharmony_ci        WriteVariable(output, cxx_sources_name, " ")
8351cb0ef41Sopenharmony_ci    if linkable_sources_name:
8361cb0ef41Sopenharmony_ci        WriteVariable(output, linkable_sources_name, " ")
8371cb0ef41Sopenharmony_ci    if other_sources_name:
8381cb0ef41Sopenharmony_ci        WriteVariable(output, other_sources_name, " ")
8391cb0ef41Sopenharmony_ci    if dummy_sources_name:
8401cb0ef41Sopenharmony_ci        WriteVariable(output, dummy_sources_name, " ")
8411cb0ef41Sopenharmony_ci
8421cb0ef41Sopenharmony_ci    output.write(")\n")
8431cb0ef41Sopenharmony_ci
8441cb0ef41Sopenharmony_ci    # Let CMake know if the 'all' target should depend on this target.
8451cb0ef41Sopenharmony_ci    exclude_from_all = (
8461cb0ef41Sopenharmony_ci        "TRUE" if qualified_target not in all_qualified_targets else "FALSE"
8471cb0ef41Sopenharmony_ci    )
8481cb0ef41Sopenharmony_ci    SetTargetProperty(output, cmake_target_name, "EXCLUDE_FROM_ALL", exclude_from_all)
8491cb0ef41Sopenharmony_ci    for extra_target_name in extra_deps:
8501cb0ef41Sopenharmony_ci        SetTargetProperty(
8511cb0ef41Sopenharmony_ci            output, extra_target_name, "EXCLUDE_FROM_ALL", exclude_from_all
8521cb0ef41Sopenharmony_ci        )
8531cb0ef41Sopenharmony_ci
8541cb0ef41Sopenharmony_ci    # Output name and location.
8551cb0ef41Sopenharmony_ci    if target_type != "none":
8561cb0ef41Sopenharmony_ci        # Link as 'C' if there are no other files
8571cb0ef41Sopenharmony_ci        if not c_sources and not cxx_sources:
8581cb0ef41Sopenharmony_ci            SetTargetProperty(output, cmake_target_name, "LINKER_LANGUAGE", ["C"])
8591cb0ef41Sopenharmony_ci
8601cb0ef41Sopenharmony_ci        # Mark uncompiled sources as uncompiled.
8611cb0ef41Sopenharmony_ci        if other_sources_name:
8621cb0ef41Sopenharmony_ci            output.write("set_source_files_properties(")
8631cb0ef41Sopenharmony_ci            WriteVariable(output, other_sources_name, "")
8641cb0ef41Sopenharmony_ci            output.write(' PROPERTIES HEADER_FILE_ONLY "TRUE")\n')
8651cb0ef41Sopenharmony_ci
8661cb0ef41Sopenharmony_ci        # Mark object sources as linkable.
8671cb0ef41Sopenharmony_ci        if linkable_sources_name:
8681cb0ef41Sopenharmony_ci            output.write("set_source_files_properties(")
8691cb0ef41Sopenharmony_ci            WriteVariable(output, other_sources_name, "")
8701cb0ef41Sopenharmony_ci            output.write(' PROPERTIES EXTERNAL_OBJECT "TRUE")\n')
8711cb0ef41Sopenharmony_ci
8721cb0ef41Sopenharmony_ci        # Output directory
8731cb0ef41Sopenharmony_ci        target_output_directory = spec.get("product_dir")
8741cb0ef41Sopenharmony_ci        if target_output_directory is None:
8751cb0ef41Sopenharmony_ci            if target_type in ("executable", "loadable_module"):
8761cb0ef41Sopenharmony_ci                target_output_directory = generator_default_variables["PRODUCT_DIR"]
8771cb0ef41Sopenharmony_ci            elif target_type == "shared_library":
8781cb0ef41Sopenharmony_ci                target_output_directory = "${builddir}/lib.${TOOLSET}"
8791cb0ef41Sopenharmony_ci            elif spec.get("standalone_static_library", False):
8801cb0ef41Sopenharmony_ci                target_output_directory = generator_default_variables["PRODUCT_DIR"]
8811cb0ef41Sopenharmony_ci            else:
8821cb0ef41Sopenharmony_ci                base_path = gyp.common.RelativePath(
8831cb0ef41Sopenharmony_ci                    os.path.dirname(gyp_file), options.toplevel_dir
8841cb0ef41Sopenharmony_ci                )
8851cb0ef41Sopenharmony_ci                target_output_directory = "${obj}.${TOOLSET}"
8861cb0ef41Sopenharmony_ci                target_output_directory = os.path.join(
8871cb0ef41Sopenharmony_ci                    target_output_directory, base_path
8881cb0ef41Sopenharmony_ci                )
8891cb0ef41Sopenharmony_ci
8901cb0ef41Sopenharmony_ci        cmake_target_output_directory = NormjoinPathForceCMakeSource(
8911cb0ef41Sopenharmony_ci            path_from_cmakelists_to_gyp, target_output_directory
8921cb0ef41Sopenharmony_ci        )
8931cb0ef41Sopenharmony_ci        SetTargetProperty(
8941cb0ef41Sopenharmony_ci            output,
8951cb0ef41Sopenharmony_ci            cmake_target_name,
8961cb0ef41Sopenharmony_ci            cmake_target_type.property_modifier + "_OUTPUT_DIRECTORY",
8971cb0ef41Sopenharmony_ci            cmake_target_output_directory,
8981cb0ef41Sopenharmony_ci        )
8991cb0ef41Sopenharmony_ci
9001cb0ef41Sopenharmony_ci        # Output name
9011cb0ef41Sopenharmony_ci        default_product_prefix = ""
9021cb0ef41Sopenharmony_ci        default_product_name = target_name
9031cb0ef41Sopenharmony_ci        default_product_ext = ""
9041cb0ef41Sopenharmony_ci        if target_type == "static_library":
9051cb0ef41Sopenharmony_ci            static_library_prefix = generator_default_variables["STATIC_LIB_PREFIX"]
9061cb0ef41Sopenharmony_ci            default_product_name = RemovePrefix(
9071cb0ef41Sopenharmony_ci                default_product_name, static_library_prefix
9081cb0ef41Sopenharmony_ci            )
9091cb0ef41Sopenharmony_ci            default_product_prefix = static_library_prefix
9101cb0ef41Sopenharmony_ci            default_product_ext = generator_default_variables["STATIC_LIB_SUFFIX"]
9111cb0ef41Sopenharmony_ci
9121cb0ef41Sopenharmony_ci        elif target_type in ("loadable_module", "shared_library"):
9131cb0ef41Sopenharmony_ci            shared_library_prefix = generator_default_variables["SHARED_LIB_PREFIX"]
9141cb0ef41Sopenharmony_ci            default_product_name = RemovePrefix(
9151cb0ef41Sopenharmony_ci                default_product_name, shared_library_prefix
9161cb0ef41Sopenharmony_ci            )
9171cb0ef41Sopenharmony_ci            default_product_prefix = shared_library_prefix
9181cb0ef41Sopenharmony_ci            default_product_ext = generator_default_variables["SHARED_LIB_SUFFIX"]
9191cb0ef41Sopenharmony_ci
9201cb0ef41Sopenharmony_ci        elif target_type != "executable":
9211cb0ef41Sopenharmony_ci            print(
9221cb0ef41Sopenharmony_ci                "ERROR: What output file should be generated?",
9231cb0ef41Sopenharmony_ci                "type",
9241cb0ef41Sopenharmony_ci                target_type,
9251cb0ef41Sopenharmony_ci                "target",
9261cb0ef41Sopenharmony_ci                target_name,
9271cb0ef41Sopenharmony_ci            )
9281cb0ef41Sopenharmony_ci
9291cb0ef41Sopenharmony_ci        product_prefix = spec.get("product_prefix", default_product_prefix)
9301cb0ef41Sopenharmony_ci        product_name = spec.get("product_name", default_product_name)
9311cb0ef41Sopenharmony_ci        product_ext = spec.get("product_extension")
9321cb0ef41Sopenharmony_ci        if product_ext:
9331cb0ef41Sopenharmony_ci            product_ext = "." + product_ext
9341cb0ef41Sopenharmony_ci        else:
9351cb0ef41Sopenharmony_ci            product_ext = default_product_ext
9361cb0ef41Sopenharmony_ci
9371cb0ef41Sopenharmony_ci        SetTargetProperty(output, cmake_target_name, "PREFIX", product_prefix)
9381cb0ef41Sopenharmony_ci        SetTargetProperty(
9391cb0ef41Sopenharmony_ci            output,
9401cb0ef41Sopenharmony_ci            cmake_target_name,
9411cb0ef41Sopenharmony_ci            cmake_target_type.property_modifier + "_OUTPUT_NAME",
9421cb0ef41Sopenharmony_ci            product_name,
9431cb0ef41Sopenharmony_ci        )
9441cb0ef41Sopenharmony_ci        SetTargetProperty(output, cmake_target_name, "SUFFIX", product_ext)
9451cb0ef41Sopenharmony_ci
9461cb0ef41Sopenharmony_ci        # Make the output of this target referenceable as a source.
9471cb0ef41Sopenharmony_ci        cmake_target_output_basename = product_prefix + product_name + product_ext
9481cb0ef41Sopenharmony_ci        cmake_target_output = os.path.join(
9491cb0ef41Sopenharmony_ci            cmake_target_output_directory, cmake_target_output_basename
9501cb0ef41Sopenharmony_ci        )
9511cb0ef41Sopenharmony_ci        SetFileProperty(output, cmake_target_output, "GENERATED", ["TRUE"], "")
9521cb0ef41Sopenharmony_ci
9531cb0ef41Sopenharmony_ci        # Includes
9541cb0ef41Sopenharmony_ci        includes = config.get("include_dirs")
9551cb0ef41Sopenharmony_ci        if includes:
9561cb0ef41Sopenharmony_ci            # This (target include directories) is what requires CMake 2.8.8
9571cb0ef41Sopenharmony_ci            includes_name = cmake_target_name + "__include_dirs"
9581cb0ef41Sopenharmony_ci            SetVariableList(
9591cb0ef41Sopenharmony_ci                output,
9601cb0ef41Sopenharmony_ci                includes_name,
9611cb0ef41Sopenharmony_ci                [
9621cb0ef41Sopenharmony_ci                    NormjoinPathForceCMakeSource(path_from_cmakelists_to_gyp, include)
9631cb0ef41Sopenharmony_ci                    for include in includes
9641cb0ef41Sopenharmony_ci                ],
9651cb0ef41Sopenharmony_ci            )
9661cb0ef41Sopenharmony_ci            output.write("set_property(TARGET ")
9671cb0ef41Sopenharmony_ci            output.write(cmake_target_name)
9681cb0ef41Sopenharmony_ci            output.write(" APPEND PROPERTY INCLUDE_DIRECTORIES ")
9691cb0ef41Sopenharmony_ci            WriteVariable(output, includes_name, "")
9701cb0ef41Sopenharmony_ci            output.write(")\n")
9711cb0ef41Sopenharmony_ci
9721cb0ef41Sopenharmony_ci        # Defines
9731cb0ef41Sopenharmony_ci        defines = config.get("defines")
9741cb0ef41Sopenharmony_ci        if defines is not None:
9751cb0ef41Sopenharmony_ci            SetTargetProperty(
9761cb0ef41Sopenharmony_ci                output, cmake_target_name, "COMPILE_DEFINITIONS", defines, ";"
9771cb0ef41Sopenharmony_ci            )
9781cb0ef41Sopenharmony_ci
9791cb0ef41Sopenharmony_ci        # Compile Flags - http://www.cmake.org/Bug/view.php?id=6493
9801cb0ef41Sopenharmony_ci        # CMake currently does not have target C and CXX flags.
9811cb0ef41Sopenharmony_ci        # So, instead of doing...
9821cb0ef41Sopenharmony_ci
9831cb0ef41Sopenharmony_ci        # cflags_c = config.get('cflags_c')
9841cb0ef41Sopenharmony_ci        # if cflags_c is not None:
9851cb0ef41Sopenharmony_ci        #   SetTargetProperty(output, cmake_target_name,
9861cb0ef41Sopenharmony_ci        #                       'C_COMPILE_FLAGS', cflags_c, ' ')
9871cb0ef41Sopenharmony_ci
9881cb0ef41Sopenharmony_ci        # cflags_cc = config.get('cflags_cc')
9891cb0ef41Sopenharmony_ci        # if cflags_cc is not None:
9901cb0ef41Sopenharmony_ci        #   SetTargetProperty(output, cmake_target_name,
9911cb0ef41Sopenharmony_ci        #                       'CXX_COMPILE_FLAGS', cflags_cc, ' ')
9921cb0ef41Sopenharmony_ci
9931cb0ef41Sopenharmony_ci        # Instead we must...
9941cb0ef41Sopenharmony_ci        cflags = config.get("cflags", [])
9951cb0ef41Sopenharmony_ci        cflags_c = config.get("cflags_c", [])
9961cb0ef41Sopenharmony_ci        cflags_cxx = config.get("cflags_cc", [])
9971cb0ef41Sopenharmony_ci        if xcode_settings:
9981cb0ef41Sopenharmony_ci            cflags = xcode_settings.GetCflags(config_to_use)
9991cb0ef41Sopenharmony_ci            cflags_c = xcode_settings.GetCflagsC(config_to_use)
10001cb0ef41Sopenharmony_ci            cflags_cxx = xcode_settings.GetCflagsCC(config_to_use)
10011cb0ef41Sopenharmony_ci            # cflags_objc = xcode_settings.GetCflagsObjC(config_to_use)
10021cb0ef41Sopenharmony_ci            # cflags_objcc = xcode_settings.GetCflagsObjCC(config_to_use)
10031cb0ef41Sopenharmony_ci
10041cb0ef41Sopenharmony_ci        if (not cflags_c or not c_sources) and (not cflags_cxx or not cxx_sources):
10051cb0ef41Sopenharmony_ci            SetTargetProperty(output, cmake_target_name, "COMPILE_FLAGS", cflags, " ")
10061cb0ef41Sopenharmony_ci
10071cb0ef41Sopenharmony_ci        elif c_sources and not (s_sources or cxx_sources):
10081cb0ef41Sopenharmony_ci            flags = []
10091cb0ef41Sopenharmony_ci            flags.extend(cflags)
10101cb0ef41Sopenharmony_ci            flags.extend(cflags_c)
10111cb0ef41Sopenharmony_ci            SetTargetProperty(output, cmake_target_name, "COMPILE_FLAGS", flags, " ")
10121cb0ef41Sopenharmony_ci
10131cb0ef41Sopenharmony_ci        elif cxx_sources and not (s_sources or c_sources):
10141cb0ef41Sopenharmony_ci            flags = []
10151cb0ef41Sopenharmony_ci            flags.extend(cflags)
10161cb0ef41Sopenharmony_ci            flags.extend(cflags_cxx)
10171cb0ef41Sopenharmony_ci            SetTargetProperty(output, cmake_target_name, "COMPILE_FLAGS", flags, " ")
10181cb0ef41Sopenharmony_ci
10191cb0ef41Sopenharmony_ci        else:
10201cb0ef41Sopenharmony_ci            # TODO: This is broken, one cannot generally set properties on files,
10211cb0ef41Sopenharmony_ci            # as other targets may require different properties on the same files.
10221cb0ef41Sopenharmony_ci            if s_sources and cflags:
10231cb0ef41Sopenharmony_ci                SetFilesProperty(output, s_sources_name, "COMPILE_FLAGS", cflags, " ")
10241cb0ef41Sopenharmony_ci
10251cb0ef41Sopenharmony_ci            if c_sources and (cflags or cflags_c):
10261cb0ef41Sopenharmony_ci                flags = []
10271cb0ef41Sopenharmony_ci                flags.extend(cflags)
10281cb0ef41Sopenharmony_ci                flags.extend(cflags_c)
10291cb0ef41Sopenharmony_ci                SetFilesProperty(output, c_sources_name, "COMPILE_FLAGS", flags, " ")
10301cb0ef41Sopenharmony_ci
10311cb0ef41Sopenharmony_ci            if cxx_sources and (cflags or cflags_cxx):
10321cb0ef41Sopenharmony_ci                flags = []
10331cb0ef41Sopenharmony_ci                flags.extend(cflags)
10341cb0ef41Sopenharmony_ci                flags.extend(cflags_cxx)
10351cb0ef41Sopenharmony_ci                SetFilesProperty(output, cxx_sources_name, "COMPILE_FLAGS", flags, " ")
10361cb0ef41Sopenharmony_ci
10371cb0ef41Sopenharmony_ci        # Linker flags
10381cb0ef41Sopenharmony_ci        ldflags = config.get("ldflags")
10391cb0ef41Sopenharmony_ci        if ldflags is not None:
10401cb0ef41Sopenharmony_ci            SetTargetProperty(output, cmake_target_name, "LINK_FLAGS", ldflags, " ")
10411cb0ef41Sopenharmony_ci
10421cb0ef41Sopenharmony_ci        # XCode settings
10431cb0ef41Sopenharmony_ci        xcode_settings = config.get("xcode_settings", {})
10441cb0ef41Sopenharmony_ci        for xcode_setting, xcode_value in xcode_settings.items():
10451cb0ef41Sopenharmony_ci            SetTargetProperty(
10461cb0ef41Sopenharmony_ci                output,
10471cb0ef41Sopenharmony_ci                cmake_target_name,
10481cb0ef41Sopenharmony_ci                "XCODE_ATTRIBUTE_%s" % xcode_setting,
10491cb0ef41Sopenharmony_ci                xcode_value,
10501cb0ef41Sopenharmony_ci                "" if isinstance(xcode_value, str) else " ",
10511cb0ef41Sopenharmony_ci            )
10521cb0ef41Sopenharmony_ci
10531cb0ef41Sopenharmony_ci    # Note on Dependencies and Libraries:
10541cb0ef41Sopenharmony_ci    # CMake wants to handle link order, resolving the link line up front.
10551cb0ef41Sopenharmony_ci    # Gyp does not retain or enforce specifying enough information to do so.
10561cb0ef41Sopenharmony_ci    # So do as other gyp generators and use --start-group and --end-group.
10571cb0ef41Sopenharmony_ci    # Give CMake as little information as possible so that it doesn't mess it up.
10581cb0ef41Sopenharmony_ci
10591cb0ef41Sopenharmony_ci    # Dependencies
10601cb0ef41Sopenharmony_ci    rawDeps = spec.get("dependencies", [])
10611cb0ef41Sopenharmony_ci
10621cb0ef41Sopenharmony_ci    static_deps = []
10631cb0ef41Sopenharmony_ci    shared_deps = []
10641cb0ef41Sopenharmony_ci    other_deps = []
10651cb0ef41Sopenharmony_ci    for rawDep in rawDeps:
10661cb0ef41Sopenharmony_ci        dep_cmake_name = namer.CreateCMakeTargetName(rawDep)
10671cb0ef41Sopenharmony_ci        dep_spec = target_dicts.get(rawDep, {})
10681cb0ef41Sopenharmony_ci        dep_target_type = dep_spec.get("type", None)
10691cb0ef41Sopenharmony_ci
10701cb0ef41Sopenharmony_ci        if dep_target_type == "static_library":
10711cb0ef41Sopenharmony_ci            static_deps.append(dep_cmake_name)
10721cb0ef41Sopenharmony_ci        elif dep_target_type == "shared_library":
10731cb0ef41Sopenharmony_ci            shared_deps.append(dep_cmake_name)
10741cb0ef41Sopenharmony_ci        else:
10751cb0ef41Sopenharmony_ci            other_deps.append(dep_cmake_name)
10761cb0ef41Sopenharmony_ci
10771cb0ef41Sopenharmony_ci    # ensure all external dependencies are complete before internal dependencies
10781cb0ef41Sopenharmony_ci    # extra_deps currently only depend on their own deps, so otherwise run early
10791cb0ef41Sopenharmony_ci    if static_deps or shared_deps or other_deps:
10801cb0ef41Sopenharmony_ci        for extra_dep in extra_deps:
10811cb0ef41Sopenharmony_ci            output.write("add_dependencies(")
10821cb0ef41Sopenharmony_ci            output.write(extra_dep)
10831cb0ef41Sopenharmony_ci            output.write("\n")
10841cb0ef41Sopenharmony_ci            for deps in (static_deps, shared_deps, other_deps):
10851cb0ef41Sopenharmony_ci                for dep in gyp.common.uniquer(deps):
10861cb0ef41Sopenharmony_ci                    output.write("  ")
10871cb0ef41Sopenharmony_ci                    output.write(dep)
10881cb0ef41Sopenharmony_ci                    output.write("\n")
10891cb0ef41Sopenharmony_ci            output.write(")\n")
10901cb0ef41Sopenharmony_ci
10911cb0ef41Sopenharmony_ci    linkable = target_type in ("executable", "loadable_module", "shared_library")
10921cb0ef41Sopenharmony_ci    other_deps.extend(extra_deps)
10931cb0ef41Sopenharmony_ci    if other_deps or (not linkable and (static_deps or shared_deps)):
10941cb0ef41Sopenharmony_ci        output.write("add_dependencies(")
10951cb0ef41Sopenharmony_ci        output.write(cmake_target_name)
10961cb0ef41Sopenharmony_ci        output.write("\n")
10971cb0ef41Sopenharmony_ci        for dep in gyp.common.uniquer(other_deps):
10981cb0ef41Sopenharmony_ci            output.write("  ")
10991cb0ef41Sopenharmony_ci            output.write(dep)
11001cb0ef41Sopenharmony_ci            output.write("\n")
11011cb0ef41Sopenharmony_ci        if not linkable:
11021cb0ef41Sopenharmony_ci            for deps in (static_deps, shared_deps):
11031cb0ef41Sopenharmony_ci                for lib_dep in gyp.common.uniquer(deps):
11041cb0ef41Sopenharmony_ci                    output.write("  ")
11051cb0ef41Sopenharmony_ci                    output.write(lib_dep)
11061cb0ef41Sopenharmony_ci                    output.write("\n")
11071cb0ef41Sopenharmony_ci        output.write(")\n")
11081cb0ef41Sopenharmony_ci
11091cb0ef41Sopenharmony_ci    # Libraries
11101cb0ef41Sopenharmony_ci    if linkable:
11111cb0ef41Sopenharmony_ci        external_libs = [lib for lib in spec.get("libraries", []) if len(lib) > 0]
11121cb0ef41Sopenharmony_ci        if external_libs or static_deps or shared_deps:
11131cb0ef41Sopenharmony_ci            output.write("target_link_libraries(")
11141cb0ef41Sopenharmony_ci            output.write(cmake_target_name)
11151cb0ef41Sopenharmony_ci            output.write("\n")
11161cb0ef41Sopenharmony_ci            if static_deps:
11171cb0ef41Sopenharmony_ci                write_group = circular_libs and len(static_deps) > 1 and flavor != "mac"
11181cb0ef41Sopenharmony_ci                if write_group:
11191cb0ef41Sopenharmony_ci                    output.write("-Wl,--start-group\n")
11201cb0ef41Sopenharmony_ci                for dep in gyp.common.uniquer(static_deps):
11211cb0ef41Sopenharmony_ci                    output.write("  ")
11221cb0ef41Sopenharmony_ci                    output.write(dep)
11231cb0ef41Sopenharmony_ci                    output.write("\n")
11241cb0ef41Sopenharmony_ci                if write_group:
11251cb0ef41Sopenharmony_ci                    output.write("-Wl,--end-group\n")
11261cb0ef41Sopenharmony_ci            if shared_deps:
11271cb0ef41Sopenharmony_ci                for dep in gyp.common.uniquer(shared_deps):
11281cb0ef41Sopenharmony_ci                    output.write("  ")
11291cb0ef41Sopenharmony_ci                    output.write(dep)
11301cb0ef41Sopenharmony_ci                    output.write("\n")
11311cb0ef41Sopenharmony_ci            if external_libs:
11321cb0ef41Sopenharmony_ci                for lib in gyp.common.uniquer(external_libs):
11331cb0ef41Sopenharmony_ci                    output.write('  "')
11341cb0ef41Sopenharmony_ci                    output.write(RemovePrefix(lib, "$(SDKROOT)"))
11351cb0ef41Sopenharmony_ci                    output.write('"\n')
11361cb0ef41Sopenharmony_ci
11371cb0ef41Sopenharmony_ci            output.write(")\n")
11381cb0ef41Sopenharmony_ci
11391cb0ef41Sopenharmony_ci    UnsetVariable(output, "TOOLSET")
11401cb0ef41Sopenharmony_ci    UnsetVariable(output, "TARGET")
11411cb0ef41Sopenharmony_ci
11421cb0ef41Sopenharmony_ci
11431cb0ef41Sopenharmony_cidef GenerateOutputForConfig(target_list, target_dicts, data, params, config_to_use):
11441cb0ef41Sopenharmony_ci    options = params["options"]
11451cb0ef41Sopenharmony_ci    generator_flags = params["generator_flags"]
11461cb0ef41Sopenharmony_ci    flavor = gyp.common.GetFlavor(params)
11471cb0ef41Sopenharmony_ci
11481cb0ef41Sopenharmony_ci    # generator_dir: relative path from pwd to where make puts build files.
11491cb0ef41Sopenharmony_ci    # Makes migrating from make to cmake easier, cmake doesn't put anything here.
11501cb0ef41Sopenharmony_ci    # Each Gyp configuration creates a different CMakeLists.txt file
11511cb0ef41Sopenharmony_ci    # to avoid incompatibilities between Gyp and CMake configurations.
11521cb0ef41Sopenharmony_ci    generator_dir = os.path.relpath(options.generator_output or ".")
11531cb0ef41Sopenharmony_ci
11541cb0ef41Sopenharmony_ci    # output_dir: relative path from generator_dir to the build directory.
11551cb0ef41Sopenharmony_ci    output_dir = generator_flags.get("output_dir", "out")
11561cb0ef41Sopenharmony_ci
11571cb0ef41Sopenharmony_ci    # build_dir: relative path from source root to our output files.
11581cb0ef41Sopenharmony_ci    # e.g. "out/Debug"
11591cb0ef41Sopenharmony_ci    build_dir = os.path.normpath(os.path.join(generator_dir, output_dir, config_to_use))
11601cb0ef41Sopenharmony_ci
11611cb0ef41Sopenharmony_ci    toplevel_build = os.path.join(options.toplevel_dir, build_dir)
11621cb0ef41Sopenharmony_ci
11631cb0ef41Sopenharmony_ci    output_file = os.path.join(toplevel_build, "CMakeLists.txt")
11641cb0ef41Sopenharmony_ci    gyp.common.EnsureDirExists(output_file)
11651cb0ef41Sopenharmony_ci
11661cb0ef41Sopenharmony_ci    output = open(output_file, "w")
11671cb0ef41Sopenharmony_ci    output.write("cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)\n")
11681cb0ef41Sopenharmony_ci    output.write("cmake_policy(VERSION 2.8.8)\n")
11691cb0ef41Sopenharmony_ci
11701cb0ef41Sopenharmony_ci    gyp_file, project_target, _ = gyp.common.ParseQualifiedTarget(target_list[-1])
11711cb0ef41Sopenharmony_ci    output.write("project(")
11721cb0ef41Sopenharmony_ci    output.write(project_target)
11731cb0ef41Sopenharmony_ci    output.write(")\n")
11741cb0ef41Sopenharmony_ci
11751cb0ef41Sopenharmony_ci    SetVariable(output, "configuration", config_to_use)
11761cb0ef41Sopenharmony_ci
11771cb0ef41Sopenharmony_ci    ar = None
11781cb0ef41Sopenharmony_ci    cc = None
11791cb0ef41Sopenharmony_ci    cxx = None
11801cb0ef41Sopenharmony_ci
11811cb0ef41Sopenharmony_ci    make_global_settings = data[gyp_file].get("make_global_settings", [])
11821cb0ef41Sopenharmony_ci    build_to_top = gyp.common.InvertRelativePath(build_dir, options.toplevel_dir)
11831cb0ef41Sopenharmony_ci    for key, value in make_global_settings:
11841cb0ef41Sopenharmony_ci        if key == "AR":
11851cb0ef41Sopenharmony_ci            ar = os.path.join(build_to_top, value)
11861cb0ef41Sopenharmony_ci        if key == "CC":
11871cb0ef41Sopenharmony_ci            cc = os.path.join(build_to_top, value)
11881cb0ef41Sopenharmony_ci        if key == "CXX":
11891cb0ef41Sopenharmony_ci            cxx = os.path.join(build_to_top, value)
11901cb0ef41Sopenharmony_ci
11911cb0ef41Sopenharmony_ci    ar = gyp.common.GetEnvironFallback(["AR_target", "AR"], ar)
11921cb0ef41Sopenharmony_ci    cc = gyp.common.GetEnvironFallback(["CC_target", "CC"], cc)
11931cb0ef41Sopenharmony_ci    cxx = gyp.common.GetEnvironFallback(["CXX_target", "CXX"], cxx)
11941cb0ef41Sopenharmony_ci
11951cb0ef41Sopenharmony_ci    if ar:
11961cb0ef41Sopenharmony_ci        SetVariable(output, "CMAKE_AR", ar)
11971cb0ef41Sopenharmony_ci    if cc:
11981cb0ef41Sopenharmony_ci        SetVariable(output, "CMAKE_C_COMPILER", cc)
11991cb0ef41Sopenharmony_ci    if cxx:
12001cb0ef41Sopenharmony_ci        SetVariable(output, "CMAKE_CXX_COMPILER", cxx)
12011cb0ef41Sopenharmony_ci
12021cb0ef41Sopenharmony_ci    # The following appears to be as-yet undocumented.
12031cb0ef41Sopenharmony_ci    # http://public.kitware.com/Bug/view.php?id=8392
12041cb0ef41Sopenharmony_ci    output.write("enable_language(ASM)\n")
12051cb0ef41Sopenharmony_ci    # ASM-ATT does not support .S files.
12061cb0ef41Sopenharmony_ci    # output.write('enable_language(ASM-ATT)\n')
12071cb0ef41Sopenharmony_ci
12081cb0ef41Sopenharmony_ci    if cc:
12091cb0ef41Sopenharmony_ci        SetVariable(output, "CMAKE_ASM_COMPILER", cc)
12101cb0ef41Sopenharmony_ci
12111cb0ef41Sopenharmony_ci    SetVariable(output, "builddir", "${CMAKE_CURRENT_BINARY_DIR}")
12121cb0ef41Sopenharmony_ci    SetVariable(output, "obj", "${builddir}/obj")
12131cb0ef41Sopenharmony_ci    output.write("\n")
12141cb0ef41Sopenharmony_ci
12151cb0ef41Sopenharmony_ci    # TODO: Undocumented/unsupported (the CMake Java generator depends on it).
12161cb0ef41Sopenharmony_ci    # CMake by default names the object resulting from foo.c to be foo.c.o.
12171cb0ef41Sopenharmony_ci    # Gyp traditionally names the object resulting from foo.c foo.o.
12181cb0ef41Sopenharmony_ci    # This should be irrelevant, but some targets extract .o files from .a
12191cb0ef41Sopenharmony_ci    # and depend on the name of the extracted .o files.
12201cb0ef41Sopenharmony_ci    output.write("set(CMAKE_C_OUTPUT_EXTENSION_REPLACE 1)\n")
12211cb0ef41Sopenharmony_ci    output.write("set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE 1)\n")
12221cb0ef41Sopenharmony_ci    output.write("\n")
12231cb0ef41Sopenharmony_ci
12241cb0ef41Sopenharmony_ci    # Force ninja to use rsp files. Otherwise link and ar lines can get too long,
12251cb0ef41Sopenharmony_ci    # resulting in 'Argument list too long' errors.
12261cb0ef41Sopenharmony_ci    # However, rsp files don't work correctly on Mac.
12271cb0ef41Sopenharmony_ci    if flavor != "mac":
12281cb0ef41Sopenharmony_ci        output.write("set(CMAKE_NINJA_FORCE_RESPONSE_FILE 1)\n")
12291cb0ef41Sopenharmony_ci    output.write("\n")
12301cb0ef41Sopenharmony_ci
12311cb0ef41Sopenharmony_ci    namer = CMakeNamer(target_list)
12321cb0ef41Sopenharmony_ci
12331cb0ef41Sopenharmony_ci    # The list of targets upon which the 'all' target should depend.
12341cb0ef41Sopenharmony_ci    # CMake has it's own implicit 'all' target, one is not created explicitly.
12351cb0ef41Sopenharmony_ci    all_qualified_targets = set()
12361cb0ef41Sopenharmony_ci    for build_file in params["build_files"]:
12371cb0ef41Sopenharmony_ci        for qualified_target in gyp.common.AllTargets(
12381cb0ef41Sopenharmony_ci            target_list, target_dicts, os.path.normpath(build_file)
12391cb0ef41Sopenharmony_ci        ):
12401cb0ef41Sopenharmony_ci            all_qualified_targets.add(qualified_target)
12411cb0ef41Sopenharmony_ci
12421cb0ef41Sopenharmony_ci    for qualified_target in target_list:
12431cb0ef41Sopenharmony_ci        if flavor == "mac":
12441cb0ef41Sopenharmony_ci            gyp_file, _, _ = gyp.common.ParseQualifiedTarget(qualified_target)
12451cb0ef41Sopenharmony_ci            spec = target_dicts[qualified_target]
12461cb0ef41Sopenharmony_ci            gyp.xcode_emulation.MergeGlobalXcodeSettingsToSpec(data[gyp_file], spec)
12471cb0ef41Sopenharmony_ci
12481cb0ef41Sopenharmony_ci        WriteTarget(
12491cb0ef41Sopenharmony_ci            namer,
12501cb0ef41Sopenharmony_ci            qualified_target,
12511cb0ef41Sopenharmony_ci            target_dicts,
12521cb0ef41Sopenharmony_ci            build_dir,
12531cb0ef41Sopenharmony_ci            config_to_use,
12541cb0ef41Sopenharmony_ci            options,
12551cb0ef41Sopenharmony_ci            generator_flags,
12561cb0ef41Sopenharmony_ci            all_qualified_targets,
12571cb0ef41Sopenharmony_ci            flavor,
12581cb0ef41Sopenharmony_ci            output,
12591cb0ef41Sopenharmony_ci        )
12601cb0ef41Sopenharmony_ci
12611cb0ef41Sopenharmony_ci    output.close()
12621cb0ef41Sopenharmony_ci
12631cb0ef41Sopenharmony_ci
12641cb0ef41Sopenharmony_cidef PerformBuild(data, configurations, params):
12651cb0ef41Sopenharmony_ci    options = params["options"]
12661cb0ef41Sopenharmony_ci    generator_flags = params["generator_flags"]
12671cb0ef41Sopenharmony_ci
12681cb0ef41Sopenharmony_ci    # generator_dir: relative path from pwd to where make puts build files.
12691cb0ef41Sopenharmony_ci    # Makes migrating from make to cmake easier, cmake doesn't put anything here.
12701cb0ef41Sopenharmony_ci    generator_dir = os.path.relpath(options.generator_output or ".")
12711cb0ef41Sopenharmony_ci
12721cb0ef41Sopenharmony_ci    # output_dir: relative path from generator_dir to the build directory.
12731cb0ef41Sopenharmony_ci    output_dir = generator_flags.get("output_dir", "out")
12741cb0ef41Sopenharmony_ci
12751cb0ef41Sopenharmony_ci    for config_name in configurations:
12761cb0ef41Sopenharmony_ci        # build_dir: relative path from source root to our output files.
12771cb0ef41Sopenharmony_ci        # e.g. "out/Debug"
12781cb0ef41Sopenharmony_ci        build_dir = os.path.normpath(
12791cb0ef41Sopenharmony_ci            os.path.join(generator_dir, output_dir, config_name)
12801cb0ef41Sopenharmony_ci        )
12811cb0ef41Sopenharmony_ci        arguments = ["cmake", "-G", "Ninja"]
12821cb0ef41Sopenharmony_ci        print(f"Generating [{config_name}]: {arguments}")
12831cb0ef41Sopenharmony_ci        subprocess.check_call(arguments, cwd=build_dir)
12841cb0ef41Sopenharmony_ci
12851cb0ef41Sopenharmony_ci        arguments = ["ninja", "-C", build_dir]
12861cb0ef41Sopenharmony_ci        print(f"Building [{config_name}]: {arguments}")
12871cb0ef41Sopenharmony_ci        subprocess.check_call(arguments)
12881cb0ef41Sopenharmony_ci
12891cb0ef41Sopenharmony_ci
12901cb0ef41Sopenharmony_cidef CallGenerateOutputForConfig(arglist):
12911cb0ef41Sopenharmony_ci    # Ignore the interrupt signal so that the parent process catches it and
12921cb0ef41Sopenharmony_ci    # kills all multiprocessing children.
12931cb0ef41Sopenharmony_ci    signal.signal(signal.SIGINT, signal.SIG_IGN)
12941cb0ef41Sopenharmony_ci
12951cb0ef41Sopenharmony_ci    target_list, target_dicts, data, params, config_name = arglist
12961cb0ef41Sopenharmony_ci    GenerateOutputForConfig(target_list, target_dicts, data, params, config_name)
12971cb0ef41Sopenharmony_ci
12981cb0ef41Sopenharmony_ci
12991cb0ef41Sopenharmony_cidef GenerateOutput(target_list, target_dicts, data, params):
13001cb0ef41Sopenharmony_ci    user_config = params.get("generator_flags", {}).get("config", None)
13011cb0ef41Sopenharmony_ci    if user_config:
13021cb0ef41Sopenharmony_ci        GenerateOutputForConfig(target_list, target_dicts, data, params, user_config)
13031cb0ef41Sopenharmony_ci    else:
13041cb0ef41Sopenharmony_ci        config_names = target_dicts[target_list[0]]["configurations"]
13051cb0ef41Sopenharmony_ci        if params["parallel"]:
13061cb0ef41Sopenharmony_ci            try:
13071cb0ef41Sopenharmony_ci                pool = multiprocessing.Pool(len(config_names))
13081cb0ef41Sopenharmony_ci                arglists = []
13091cb0ef41Sopenharmony_ci                for config_name in config_names:
13101cb0ef41Sopenharmony_ci                    arglists.append(
13111cb0ef41Sopenharmony_ci                        (target_list, target_dicts, data, params, config_name)
13121cb0ef41Sopenharmony_ci                    )
13131cb0ef41Sopenharmony_ci                    pool.map(CallGenerateOutputForConfig, arglists)
13141cb0ef41Sopenharmony_ci            except KeyboardInterrupt as e:
13151cb0ef41Sopenharmony_ci                pool.terminate()
13161cb0ef41Sopenharmony_ci                raise e
13171cb0ef41Sopenharmony_ci        else:
13181cb0ef41Sopenharmony_ci            for config_name in config_names:
13191cb0ef41Sopenharmony_ci                GenerateOutputForConfig(
13201cb0ef41Sopenharmony_ci                    target_list, target_dicts, data, params, config_name
13211cb0ef41Sopenharmony_ci                )
1322