def cmake(source_dir, build_dir, cmake_args, env=None,
          clean_first=True, profiling=False, debug_trycompile=False,
          trace_cmake=False, summarize_options=False):
    """Call cmake with from a build dir for a source dir.
    cmake_args are added on the command line.

    :param env: defines the environment used when calling ``cmake``
                ``os.environ`` will remain unchanged
    :param clean_first: Clean the cmake cache
    :param summarize_options: Whether to call :py:func:`display_options` at the end

    For qibuild/CMake hackers:

    :param profiling: Profile CMake executions
    :param debug_trycompile: Call ``cmake`` with ``--debug-trycompile``
    :param trace_cmake: Call ``cmake`` with ``--trace`` The results
                        will be written in <build>/cmake.log

    """
    cmake_binary = qisys.command.find_program("cmake", env=env, raises=True)
    if not os.path.exists(source_dir):
        qisys.error.Error("source dir: %s does not exist, aborting")

    if not os.path.exists(build_dir):
        mess  = "Could not find build directory: %s \n" % build_dir
        qisys.error.Error(mess)

    # Always remove CMakeCache
    if clean_first:
        cache = os.path.join(build_dir, "CMakeCache.txt")
        qisys.sh.rm(cache)

    if debug_trycompile:
        cmake_args.append("--debug-trycompile")
    if profiling or trace_cmake:
        cmake_version = get_cmake_version(cmake_binary)
        if qisys.version.compare(cmake_version, "3.4") >= 0:
            cmake_args.append("--trace-expand")
        else:
            cmake_args.append("--trace")

    # Check that no one has made an in-source build
    in_source_cache = os.path.join(source_dir, "CMakeCache.txt")
    if os.path.exists(in_source_cache):
        # FIXME: better wording
        mess  = "You have run CMake from your sources\n"
        mess += in_source_cache % "CMakeCache.txt found here: %s\n"
        mess += "Please clean your sources and try again\n"
        qisys.error.Error(mess)

    # Check that the root CMakeLists file is correct
    root_cmake = os.path.join(source_dir, "CMakeLists.txt")
    check_root_cmake_list(root_cmake)

    # Add path to source to the list of args, and set buildir for
    # the current working dir.
    cmake_args += [source_dir]
    if not profiling and not trace_cmake:
        qisys.command.call([cmake_binary] + cmake_args, cwd=build_dir, env=env)
        if summarize_options:
            display_options(build_dir)
        return
    cmake_log = os.path.join(build_dir, "cmake.log")
    fp = open(cmake_log, "w")
    if profiling:
        ui.info(ui.green, "Running cmake for profiling ...")
    if trace_cmake:
        ui.info(ui.green, "Running cmake with --trace ...")
    ui.debug("Running cmake " + " ".join(cmake_args))
    retcode = subprocess.call([cmake_binary] + cmake_args, cwd=build_dir, env=env,
                   stdout=fp, stderr=fp)
    fp.close()
    if retcode != 0:
        mess = "CMake failed"
        if retcode < 0:
            mess += " (%s)" % qisys.command.str_from_signal(-retcode)
        ui.error(mess)
    ui.info(ui.green, "CMake trace saved in", ui.reset, ui.bold, cmake_log)
    if not profiling:
        return
    qibuild_dir = get_cmake_qibuild_dir()
    ui.info(ui.green, "Analyzing cmake logs ...")
    profiling_res = qibuild.cmake.profiling.parse_cmake_log(cmake_log, qibuild_dir)
    outdir = os.path.join(build_dir, "profile")
    qibuild.cmake.profiling.gen_annotations(profiling_res, outdir, qibuild_dir)
    ui.info(ui.green, "Annotations generated in", outdir)
dataset/ETHPy150Open aldebaran/qibuild/python/qibuild/cmake/__init__.py/cmake/swapped operands of `%`