16d528ed9Sopenharmony_ci// Copyright 2018 The Chromium Authors. All rights reserved.
26d528ed9Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
36d528ed9Sopenharmony_ci// found in the LICENSE file.
46d528ed9Sopenharmony_ci
56d528ed9Sopenharmony_ci#ifndef TOOLS_GN_NINJA_TARGET_COMMAND_WRITER_H_
66d528ed9Sopenharmony_ci#define TOOLS_GN_NINJA_TARGET_COMMAND_WRITER_H_
76d528ed9Sopenharmony_ci
86d528ed9Sopenharmony_ci#include <string_view>
96d528ed9Sopenharmony_ci
106d528ed9Sopenharmony_ci#include "base/json/string_escape.h"
116d528ed9Sopenharmony_ci#include "gn/config_values_extractors.h"
126d528ed9Sopenharmony_ci#include "gn/escape.h"
136d528ed9Sopenharmony_ci#include "gn/filesystem_utils.h"
146d528ed9Sopenharmony_ci#include "gn/frameworks_utils.h"
156d528ed9Sopenharmony_ci#include "gn/path_output.h"
166d528ed9Sopenharmony_ci#include "gn/target.h"
176d528ed9Sopenharmony_ci#include "gn/toolchain.h"
186d528ed9Sopenharmony_ci#include "gn/variables.h"
196d528ed9Sopenharmony_ci
206d528ed9Sopenharmony_cistruct DefineWriter {
216d528ed9Sopenharmony_ci  DefineWriter() { options.mode = ESCAPE_NINJA_COMMAND; }
226d528ed9Sopenharmony_ci  DefineWriter(EscapingMode mode) { options.mode = mode; }
236d528ed9Sopenharmony_ci
246d528ed9Sopenharmony_ci  void operator()(const std::string& s, std::ostream& out) const {
256d528ed9Sopenharmony_ci    out << " ";
266d528ed9Sopenharmony_ci    EscapeStringToStream(out, "-D" + s, options);
276d528ed9Sopenharmony_ci  }
286d528ed9Sopenharmony_ci
296d528ed9Sopenharmony_ci  EscapeOptions options;
306d528ed9Sopenharmony_ci};
316d528ed9Sopenharmony_ci
326d528ed9Sopenharmony_cistruct FrameworkDirsWriter {
336d528ed9Sopenharmony_ci  FrameworkDirsWriter(PathOutput& path_output, const std::string& tool_switch)
346d528ed9Sopenharmony_ci      : path_output_(path_output), tool_switch_(tool_switch) {}
356d528ed9Sopenharmony_ci
366d528ed9Sopenharmony_ci  ~FrameworkDirsWriter() = default;
376d528ed9Sopenharmony_ci
386d528ed9Sopenharmony_ci  void operator()(const SourceDir& d, std::ostream& out) const {
396d528ed9Sopenharmony_ci    std::ostringstream path_out;
406d528ed9Sopenharmony_ci    path_output_.WriteDir(path_out, d, PathOutput::DIR_NO_LAST_SLASH);
416d528ed9Sopenharmony_ci    const std::string& path = path_out.str();
426d528ed9Sopenharmony_ci    if (path[0] == '"')
436d528ed9Sopenharmony_ci      out << " \"" << tool_switch_ << path.substr(1);
446d528ed9Sopenharmony_ci    else
456d528ed9Sopenharmony_ci      out << " " << tool_switch_ << path;
466d528ed9Sopenharmony_ci  }
476d528ed9Sopenharmony_ci
486d528ed9Sopenharmony_ci  PathOutput& path_output_;
496d528ed9Sopenharmony_ci  std::string tool_switch_;
506d528ed9Sopenharmony_ci};
516d528ed9Sopenharmony_ci
526d528ed9Sopenharmony_cistruct FrameworksWriter {
536d528ed9Sopenharmony_ci  explicit FrameworksWriter(const std::string& tool_switch)
546d528ed9Sopenharmony_ci      : FrameworksWriter(ESCAPE_NINJA_COMMAND, tool_switch) {}
556d528ed9Sopenharmony_ci  FrameworksWriter(EscapingMode mode, const std::string& tool_switch)
566d528ed9Sopenharmony_ci      : tool_switch_(tool_switch) {
576d528ed9Sopenharmony_ci    options_.mode = mode;
586d528ed9Sopenharmony_ci  }
596d528ed9Sopenharmony_ci
606d528ed9Sopenharmony_ci  void operator()(const std::string& s, std::ostream& out) const {
616d528ed9Sopenharmony_ci    out << " " << tool_switch_;
626d528ed9Sopenharmony_ci    std::string_view framework_name = GetFrameworkName(s);
636d528ed9Sopenharmony_ci    EscapeStringToStream(out, framework_name, options_);
646d528ed9Sopenharmony_ci  }
656d528ed9Sopenharmony_ci
666d528ed9Sopenharmony_ci  EscapeOptions options_;
676d528ed9Sopenharmony_ci  std::string tool_switch_;
686d528ed9Sopenharmony_ci};
696d528ed9Sopenharmony_ci
706d528ed9Sopenharmony_cistruct IncludeWriter {
716d528ed9Sopenharmony_ci  explicit IncludeWriter(PathOutput& path_output) : path_output_(path_output) {}
726d528ed9Sopenharmony_ci  ~IncludeWriter() = default;
736d528ed9Sopenharmony_ci
746d528ed9Sopenharmony_ci  void operator()(const SourceDir& d, std::ostream& out) const {
756d528ed9Sopenharmony_ci    std::ostringstream path_out;
766d528ed9Sopenharmony_ci    path_output_.WriteDir(path_out, d, PathOutput::DIR_NO_LAST_SLASH);
776d528ed9Sopenharmony_ci    const std::string& path = path_out.str();
786d528ed9Sopenharmony_ci    if (path[0] == '"')
796d528ed9Sopenharmony_ci      out << " \"-I" << path.substr(1);
806d528ed9Sopenharmony_ci    else
816d528ed9Sopenharmony_ci      out << " -I" << path;
826d528ed9Sopenharmony_ci  }
836d528ed9Sopenharmony_ci
846d528ed9Sopenharmony_ci  PathOutput& path_output_;
856d528ed9Sopenharmony_ci};
866d528ed9Sopenharmony_ci
876d528ed9Sopenharmony_ci// has_precompiled_headers is set when this substitution matches a tool type
886d528ed9Sopenharmony_ci// that supports precompiled headers, and this target supports precompiled
896d528ed9Sopenharmony_ci// headers. It doesn't indicate if the tool has precompiled headers (this
906d528ed9Sopenharmony_ci// will be looked up by this function).
916d528ed9Sopenharmony_ci//
926d528ed9Sopenharmony_ci// The tool_type indicates the corresponding tool for flags that are
936d528ed9Sopenharmony_ci// tool-specific (e.g. "cflags_c"). For non-tool-specific flags (e.g.
946d528ed9Sopenharmony_ci// "defines") tool_type should be TYPE_NONE.
956d528ed9Sopenharmony_civoid WriteOneFlag(RecursiveWriterConfig config,
966d528ed9Sopenharmony_ci                  const Target* target,
976d528ed9Sopenharmony_ci                  const Substitution* subst_enum,
986d528ed9Sopenharmony_ci                  bool has_precompiled_headers,
996d528ed9Sopenharmony_ci                  const char* tool_name,
1006d528ed9Sopenharmony_ci                  const std::vector<std::string>& (ConfigValues::*getter)()
1016d528ed9Sopenharmony_ci                      const,
1026d528ed9Sopenharmony_ci                  EscapeOptions flag_escape_options,
1036d528ed9Sopenharmony_ci                  PathOutput& path_output,
1046d528ed9Sopenharmony_ci                  std::ostream& out,
1056d528ed9Sopenharmony_ci                  bool write_substitution = true,
1066d528ed9Sopenharmony_ci                  bool indent = false);
1076d528ed9Sopenharmony_ci
1086d528ed9Sopenharmony_ci// Fills |outputs| with the object or gch file for the precompiled header of the
1096d528ed9Sopenharmony_ci// given type (flag type and tool type must match).
1106d528ed9Sopenharmony_civoid GetPCHOutputFiles(const Target* target,
1116d528ed9Sopenharmony_ci                       const char* tool_name,
1126d528ed9Sopenharmony_ci                       std::vector<OutputFile>* outputs);
1136d528ed9Sopenharmony_ci
1146d528ed9Sopenharmony_cistd::string GetGCCPCHOutputExtension(const char* tool_name);
1156d528ed9Sopenharmony_cistd::string GetWindowsPCHObjectExtension(const char* tool_name,
1166d528ed9Sopenharmony_ci                                         const std::string& obj_extension);
1176d528ed9Sopenharmony_ci
1186d528ed9Sopenharmony_ci#endif  // TOOLS_GN_NINJA_TARGET_COMMAND_WRITER_H_
119