-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathproject_file_writers.h
206 lines (179 loc) · 5.33 KB
/
project_file_writers.h
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#pragma once
namespace swiftwinrt
{
static void write_root_cmake(std::map<std::string, std::vector<std::string_view>>& namespaces)
{
writer w;
auto path = w.root_directory() / "CMakeLists.txt";
if (!settings.has_project_type(project_type::cmake))
{
return;
}
w.write("add_subdirectory(CWinRT)\n");
for (auto&& [module, _] : namespaces)
{
w.write("add_subdirectory(%)\n", module);
}
w.flush_to_file(path);
}
static void write_cmake_lists(std::string_view const& module, std::set<std::string> const& depends, std::vector<std::string_view> const& namespaces)
{
writer w;
w.type_namespace = module;
w.c_mod = settings.get_c_module_name();
auto path = w.project_directory() / "CMakeLists.txt";
if (!settings.has_project_type(project_type::cmake))
{
return;
}
auto content = R"(
set(GENERATED_FILES_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(SWIFTWINRT_GENERATED_FILES
% %+Generics.swift
)
%
add_library(% SHARED
${SWIFTWINRT_GENERATED_FILES}%
)
install(TARGETS %
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
COMPONENT lib)
target_link_libraries(% PRIVATE
%
%)
target_include_directories(%
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_include_directories(%
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
PRIVATE ${GENERATED_FILES_DIR})
)";
w.write(content,
bind_each([&](writer& w, const std::string_view ns) {
w.write(" %.swift\n", ns);
w.write(" %+ABI.swift\n", ns);
w.write(" %+Impl.swift\n", ns);
}, namespaces),
module,
settings.support == module ? "file(GLOB SUPPORT_FILES ${GENERATED_FILES_DIR}/Support/*)" : "",
module,
settings.support == module ? "\n ${SUPPORT_FILES}" : "",
module,
module,
w.c_mod,
bind_list("\n ", depends),
module,
module);
w.save_cmake();
}
// Write a package.swift file which includes the full enclosure of Swift modules being generated (including CWinRT) which are generated
// by the project. This is useful for our test project, which just creates a single module.
static void write_multimodule_package_swift(std::map<std::string, std::set<std::string>>& module_dependencies)
{
writer w;
auto path = w.root_directory() / "Package.swift";
if (!settings.has_project_type(project_type::spm))
{
return;
}
auto exclude_cmake = settings.has_project_type(project_type::cmake) ? R"(,
exclude: [ "CMakeLists.txt" ])" : "";
auto package = R"(// swift-tools-version: 5.7
// WARNING: Generated by a tool. Do not hand-edit!
import PackageDescription
let package = Package(
name: "WinRT",
products: [%
],
targets: [
.target(
name: "CWinRT",%
)%
]
)
)";
w.write(package,
bind([&](writer& w) {
for (auto&& [module, _] : module_dependencies)
{
w.write(R"(
.library(name: "%", targets: ["%"]),)", module, module);
}}),
exclude_cmake,
bind([&](writer& w) {
for (auto [module, dependencies] : module_dependencies)
{
w.write(R"(
.target(
name: "%",
dependencies: [
"CWinRT",%
],
path: "swift/%"%
),
)", module, bind([&](writer& w) {
for (auto&& ns : dependencies)
{
w.write(R"(
"%",)", ns);
}}),
module);
}}
), exclude_cmake);
w.flush_to_file(path);
}
static void write_singlemodule_package_swift(std::string_view const& module, std::set<std::string> const& depends)
{
writer w;
w.type_namespace = module;
auto path = w.root_directory() / module / "Package.swift";
if (!settings.has_project_type(project_type::spm))
{
return;
}
auto exclude_cmake = settings.has_project_type(project_type::cmake) ? R"(,
exclude: [ "CMakeLists.txt" ])" : "";
auto package = R"(// swift-tools-version: 5.7
// WARNING: Generated by a tool. Do not hand-edit!
import PackageDescription
let package = Package(
name: "%",
products: [
.library(name: "%", type: .dynamic, targets: ["%"]),
],
dependencies: [
.package(path: "../CWinRT"),%
],
targets: [
.target(
name: "%",
dependencies: [
"CWinRT",%
],
path: "."%
),
]
)
)";
w.write(package,
module,
module, module,
bind([&](writer& w) {
for (auto&& module : depends)
{
w.write(R"(
.package(path: "../%"),)", module);
}}),
module,
bind([&](writer& w) {
for (auto module : depends)
{
w.write(R"(
"%",)", module);
}}
), exclude_cmake);
w.flush_to_file(path);
}
}