tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef TOOLS_GN_VISUAL_STUDIO_WRITER_H_ |
| 6 | #define TOOLS_GN_VISUAL_STUDIO_WRITER_H_ |
| 7 | |
| 8 | #include <iosfwd> |
jbroman | 3150001 | 2016-04-18 21:22:18 | [diff] [blame] | 9 | #include <memory> |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "base/gtest_prod_util.h" |
| 14 | #include "base/macros.h" |
ngg | d8d1d8e7 | 2016-02-24 13:05:44 | [diff] [blame] | 15 | #include "tools/gn/path_output.h" |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 16 | |
| 17 | namespace base { |
| 18 | class FilePath; |
| 19 | } |
| 20 | |
| 21 | class Builder; |
| 22 | class BuildSettings; |
| 23 | class Err; |
ngg | 3dbc813 | 2016-04-25 10:37:14 | [diff] [blame] | 24 | class SourceFile; |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 25 | class Target; |
| 26 | |
| 27 | class VisualStudioWriter { |
| 28 | public: |
ngg | d0bc0fa | 2016-02-22 23:18:48 | [diff] [blame] | 29 | enum Version { |
| 30 | Vs2013 = 1, // Visual Studio 2013 |
alexis.menard | c20ee868 | 2017-03-13 18:40:54 | [diff] [blame] | 31 | Vs2015, // Visual Studio 2015 |
| 32 | Vs2017 // Visual Studio 2017 |
ngg | d0bc0fa | 2016-02-22 23:18:48 | [diff] [blame] | 33 | }; |
| 34 | |
tmoniuszko | 014e81d | 2016-02-26 12:24:51 | [diff] [blame] | 35 | // Writes Visual Studio project and solution files. |sln_name| is the optional |
tmoniuszko | 64edbcf | 2016-08-03 11:34:17 | [diff] [blame] | 36 | // solution file name ("all" is used if not specified). |filters| is optional |
| 37 | // semicolon-separated list of label patterns used to limit the set of |
| 38 | // generated projects. Only matching targets and their dependencies (unless |
| 39 | // |no_deps| is true) will be included to the solution. On failure will |
Andrew Boyarshin | 8480d866 | 2017-06-17 14:34:30 | [diff] [blame] | 40 | // populate |err| and will return false. |win_sdk| is the Windows SDK version |
| 41 | // which will be used by Visual Studio IntelliSense. |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 42 | static bool RunAndWriteFiles(const BuildSettings* build_settings, |
brettw | 8293c35 | 2016-07-26 20:38:37 | [diff] [blame] | 43 | const Builder& builder, |
ngg | d0bc0fa | 2016-02-22 23:18:48 | [diff] [blame] | 44 | Version version, |
tmoniuszko | 014e81d | 2016-02-26 12:24:51 | [diff] [blame] | 45 | const std::string& sln_name, |
tmoniuszko | 64edbcf | 2016-08-03 11:34:17 | [diff] [blame] | 46 | const std::string& filters, |
Andrew Boyarshin | 8480d866 | 2017-06-17 14:34:30 | [diff] [blame] | 47 | const std::string& win_sdk, |
tmoniuszko | 64edbcf | 2016-08-03 11:34:17 | [diff] [blame] | 48 | bool no_deps, |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 49 | Err* err); |
| 50 | |
| 51 | private: |
| 52 | FRIEND_TEST_ALL_PREFIXES(VisualStudioWriterTest, ResolveSolutionFolders); |
| 53 | FRIEND_TEST_ALL_PREFIXES(VisualStudioWriterTest, |
| 54 | ResolveSolutionFolders_AbsPath); |
| 55 | |
| 56 | // Solution project or folder. |
| 57 | struct SolutionEntry { |
| 58 | SolutionEntry(const std::string& name, |
| 59 | const std::string& path, |
| 60 | const std::string& guid); |
tmoniuszko | bc312ac4 | 2016-02-04 10:53:33 | [diff] [blame] | 61 | virtual ~SolutionEntry(); |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 62 | |
| 63 | // Entry name. For projects must be unique in the solution. |
| 64 | std::string name; |
| 65 | // Absolute project file or folder directory path. |
| 66 | std::string path; |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 67 | // GUID-like string. |
| 68 | std::string guid; |
| 69 | // Pointer to parent folder. nullptr if entry has no parent. |
| 70 | SolutionEntry* parent_folder; |
| 71 | }; |
| 72 | |
tmoniuszko | bc312ac4 | 2016-02-04 10:53:33 | [diff] [blame] | 73 | struct SolutionProject : public SolutionEntry { |
| 74 | SolutionProject(const std::string& name, |
| 75 | const std::string& path, |
| 76 | const std::string& guid, |
| 77 | const std::string& label_dir_path, |
| 78 | const std::string& config_platform); |
| 79 | ~SolutionProject() override; |
| 80 | |
| 81 | // Absolute label dir path. |
| 82 | std::string label_dir_path; |
| 83 | // Configuration platform. May be different than solution config platform. |
| 84 | std::string config_platform; |
| 85 | }; |
| 86 | |
ngg | 3dbc813 | 2016-04-25 10:37:14 | [diff] [blame] | 87 | struct SourceFileCompileTypePair { |
| 88 | SourceFileCompileTypePair(const SourceFile* file, const char* compile_type); |
| 89 | ~SourceFileCompileTypePair(); |
| 90 | |
| 91 | // Source file. |
| 92 | const SourceFile* file; |
| 93 | // Compile type string. |
| 94 | const char* compile_type; |
| 95 | }; |
| 96 | |
jbroman | 3150001 | 2016-04-18 21:22:18 | [diff] [blame] | 97 | using SolutionProjects = std::vector<std::unique_ptr<SolutionProject>>; |
| 98 | using SolutionFolders = std::vector<std::unique_ptr<SolutionEntry>>; |
ngg | 3dbc813 | 2016-04-25 10:37:14 | [diff] [blame] | 99 | using SourceFileCompileTypePairs = std::vector<SourceFileCompileTypePair>; |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 100 | |
tmoniuszko | 014e81d | 2016-02-26 12:24:51 | [diff] [blame] | 101 | VisualStudioWriter(const BuildSettings* build_settings, |
| 102 | const char* config_platform, |
Andrew Boyarshin | 8480d866 | 2017-06-17 14:34:30 | [diff] [blame] | 103 | Version version, |
| 104 | const std::string& win_kit); |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 105 | ~VisualStudioWriter(); |
| 106 | |
| 107 | bool WriteProjectFiles(const Target* target, Err* err); |
| 108 | bool WriteProjectFileContents(std::ostream& out, |
tmoniuszko | bc312ac4 | 2016-02-04 10:53:33 | [diff] [blame] | 109 | const SolutionProject& solution_project, |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 110 | const Target* target, |
ngg | 3dbc813 | 2016-04-25 10:37:14 | [diff] [blame] | 111 | SourceFileCompileTypePairs* source_types, |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 112 | Err* err); |
ngg | 3dbc813 | 2016-04-25 10:37:14 | [diff] [blame] | 113 | void WriteFiltersFileContents(std::ostream& out, |
| 114 | const Target* target, |
| 115 | const SourceFileCompileTypePairs& source_types); |
tmoniuszko | 014e81d | 2016-02-26 12:24:51 | [diff] [blame] | 116 | bool WriteSolutionFile(const std::string& sln_name, Err* err); |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 117 | void WriteSolutionFileContents(std::ostream& out, |
| 118 | const base::FilePath& solution_dir_path); |
| 119 | |
| 120 | // Resolves all solution folders (parent folders for projects) into |folders_| |
| 121 | // and updates |root_folder_dir_|. Also sets |parent_folder| for |projects_|. |
| 122 | void ResolveSolutionFolders(); |
| 123 | |
ngg | d8d1d8e7 | 2016-02-24 13:05:44 | [diff] [blame] | 124 | std::string GetNinjaTarget(const Target* target); |
| 125 | |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 126 | const BuildSettings* build_settings_; |
| 127 | |
ngg | d0bc0fa | 2016-02-22 23:18:48 | [diff] [blame] | 128 | // Toolset version. |
| 129 | const char* toolset_version_; |
| 130 | |
| 131 | // Project version. |
| 132 | const char* project_version_; |
| 133 | |
| 134 | // Visual Studio version string. |
| 135 | const char* version_string_; |
| 136 | |
tmoniuszko | bc312ac4 | 2016-02-04 10:53:33 | [diff] [blame] | 137 | // Platform for solution configuration (Win32, x64). Some projects may be |
| 138 | // configured for different platform. |
ngg | 5dd34fc | 2016-02-25 14:51:58 | [diff] [blame] | 139 | const char* config_platform_; |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 140 | |
| 141 | // All projects contained by solution. |
tmoniuszko | bc312ac4 | 2016-02-04 10:53:33 | [diff] [blame] | 142 | SolutionProjects projects_; |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 143 | |
| 144 | // Absolute root solution folder path. |
| 145 | std::string root_folder_path_; |
| 146 | |
| 147 | // Folders for all solution projects. |
tmoniuszko | bc312ac4 | 2016-02-04 10:53:33 | [diff] [blame] | 148 | SolutionFolders folders_; |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 149 | |
| 150 | // Semicolon-separated Windows SDK include directories. |
| 151 | std::string windows_kits_include_dirs_; |
| 152 | |
ngg | d8d1d8e7 | 2016-02-24 13:05:44 | [diff] [blame] | 153 | // Path formatter for ninja targets. |
| 154 | PathOutput ninja_path_output_; |
| 155 | |
Andrew Boyarshin | 8480d866 | 2017-06-17 14:34:30 | [diff] [blame] | 156 | // Windows 10 SDK version string (e.g. 10.0.14393.0) |
| 157 | std::string windows_sdk_version_; |
| 158 | |
tmoniuszko | 050f2069 | 2016-01-29 16:15:36 | [diff] [blame] | 159 | DISALLOW_COPY_AND_ASSIGN(VisualStudioWriter); |
| 160 | }; |
| 161 | |
| 162 | #endif // TOOLS_GN_VISUAL_STUDIO_WRITER_H_ |