summaryrefslogtreecommitdiffstats
path: root/tools/gn/visual_studio_writer.h
blob: 5b578ed3cbab3b968672fc707ed60d0c0cf373e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef TOOLS_GN_VISUAL_STUDIO_WRITER_H_
#define TOOLS_GN_VISUAL_STUDIO_WRITER_H_

#include <iosfwd>
#include <string>
#include <vector>

#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "tools/gn/path_output.h"

namespace base {
class FilePath;
}

class Builder;
class BuildSettings;
class Err;
class Target;

class VisualStudioWriter {
 public:
  enum Version {
    Vs2013 = 1,  // Visual Studio 2013
    Vs2015       // Visual Studio 2015
  };

  // Writes Visual Studio project and solution files. |sln_name| is the optional
  // solution file name ("all" is used if not specified). |dir_filters| is
  // optional semicolon-separated list of label patterns used to limit the set
  // of generated projects. Only matching targets will be included to the
  // solution. On failure will populate |err| and will return false.
  static bool RunAndWriteFiles(const BuildSettings* build_settings,
                               Builder* builder,
                               Version version,
                               const std::string& sln_name,
                               const std::string& dir_filters,
                               Err* err);

 private:
  FRIEND_TEST_ALL_PREFIXES(VisualStudioWriterTest, ResolveSolutionFolders);
  FRIEND_TEST_ALL_PREFIXES(VisualStudioWriterTest,
                           ResolveSolutionFolders_AbsPath);

  // Solution project or folder.
  struct SolutionEntry {
    SolutionEntry(const std::string& name,
                  const std::string& path,
                  const std::string& guid);
    virtual ~SolutionEntry();

    // Entry name. For projects must be unique in the solution.
    std::string name;
    // Absolute project file or folder directory path.
    std::string path;
    // GUID-like string.
    std::string guid;
    // Pointer to parent folder. nullptr if entry has no parent.
    SolutionEntry* parent_folder;
  };

  struct SolutionProject : public SolutionEntry {
    SolutionProject(const std::string& name,
                    const std::string& path,
                    const std::string& guid,
                    const std::string& label_dir_path,
                    const std::string& config_platform);
    ~SolutionProject() override;

    // Absolute label dir path.
    std::string label_dir_path;
    // Configuration platform. May be different than solution config platform.
    std::string config_platform;
  };

  using SolutionProjects = std::vector<SolutionProject*>;
  using SolutionFolders = std::vector<SolutionEntry*>;

  VisualStudioWriter(const BuildSettings* build_settings,
                     const char* config_platform,
                     Version version);
  ~VisualStudioWriter();

  bool WriteProjectFiles(const Target* target, Err* err);
  bool WriteProjectFileContents(std::ostream& out,
                                const SolutionProject& solution_project,
                                const Target* target,
                                Err* err);
  void WriteFiltersFileContents(std::ostream& out, const Target* target);
  bool WriteSolutionFile(const std::string& sln_name, Err* err);
  void WriteSolutionFileContents(std::ostream& out,
                                 const base::FilePath& solution_dir_path);

  // Resolves all solution folders (parent folders for projects) into |folders_|
  // and updates |root_folder_dir_|. Also sets |parent_folder| for |projects_|.
  void ResolveSolutionFolders();

  std::string GetNinjaTarget(const Target* target);

  const BuildSettings* build_settings_;

  // Toolset version.
  const char* toolset_version_;

  // Project version.
  const char* project_version_;

  // Visual Studio version string.
  const char* version_string_;

  // Platform for solution configuration (Win32, x64). Some projects may be
  // configured for different platform.
  const char* config_platform_;

  // All projects contained by solution.
  SolutionProjects projects_;

  // Absolute root solution folder path.
  std::string root_folder_path_;

  // Folders for all solution projects.
  SolutionFolders folders_;

  // Semicolon-separated Windows SDK include directories.
  std::string windows_kits_include_dirs_;

  // Path formatter for ninja targets.
  PathOutput ninja_path_output_;

  DISALLOW_COPY_AND_ASSIGN(VisualStudioWriter);
};

#endif  // TOOLS_GN_VISUAL_STUDIO_WRITER_H_