66#include < sw/driver/target/native.h>
77
88static ::cl::opt<path> integrate_cmake_deps (" cmake-deps" , ::cl::sub(subcommand_integrate));
9+ static ::cl::opt<path> integrate_waf_deps (" waf-deps" , ::cl::sub(subcommand_integrate));
910
1011struct CMakeEmitter : primitives::Emitter
1112{
@@ -80,12 +81,14 @@ SUBCOMMAND_DECL(integrate)
8081 ctx.addLine (" # sw autogenerated file" );
8182 ctx.addLine (" #" );
8283 ctx.emptyLines ();
84+
85+ // targets
86+ ctx.addLine (" # targets" );
8387 for (auto &s : b.solutions )
8488 {
8589 for (auto &[pkg, t] : s.getChildren ())
8690 {
8791 auto &nt = *t->as <sw::NativeExecutedTarget>();
88-
8992 if (t->getType () == sw::TargetType::NativeExecutable)
9093 continue ;
9194
@@ -158,8 +161,13 @@ SUBCOMMAND_DECL(integrate)
158161 // props2
159162 ctx.increaseIndent (" set_target_properties(" + pkg.toString () + " PROPERTIES" );
160163
161- ctx.addLine (" IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG \" CXX\" " );
162- ctx.addLine (" IMPORTED_LOCATION_" + toCmakeString (s.Settings .Native .ConfigurationType ) + " \" " + normalize_path (nt.getImportLibrary ()) + " \" " );
164+ // TODO: detect C/CXX language from target files
165+ ctx.addLine (" IMPORTED_LINK_INTERFACE_LANGUAGES_" + toCmakeString (s.Settings .Native .ConfigurationType ) + " \" CXX\" " );
166+
167+ // IMPORTED_LOCATION = path to .dll/.so or static .lib/.a
168+ ctx.addLine (" IMPORTED_LOCATION_" + toCmakeString (s.Settings .Native .ConfigurationType ) + " \" " + normalize_path (nt.getOutputFile ()) + " \" " );
169+ // IMPORTED_IMPLIB = path to .lib (import)
170+ ctx.addLine (" IMPORTED_IMPLIB_" + toCmakeString (s.Settings .Native .ConfigurationType ) + " \" " + normalize_path (nt.getImportLibrary ()) + " \" " );
163171
164172 ctx.decreaseIndent (" )" );
165173 ctx.emptyLines ();
@@ -184,13 +192,14 @@ SUBCOMMAND_DECL(integrate)
184192 }
185193 break ;
186194 }
195+
187196 // deps
197+ ctx.addLine (" # dependencies" );
188198 for (auto &s : b.solutions )
189199 {
190200 for (auto &[pkg, t] : s.getChildren ())
191201 {
192202 auto &nt = *t->as <sw::NativeExecutedTarget>();
193-
194203 if (t->getType () == sw::TargetType::NativeExecutable)
195204 continue ;
196205
@@ -200,5 +209,114 @@ SUBCOMMAND_DECL(integrate)
200209 break ;
201210 }
202211 write_file_if_different (integrate_cmake_deps.parent_path () / " CMakeLists.txt" , ctx.getText ());
212+
213+ return ;
214+ }
215+
216+ if (!integrate_waf_deps.empty ())
217+ {
218+ auto lines = read_lines (integrate_waf_deps);
219+
220+ auto swctx = createSwContext ();
221+ sw::Build b (swctx);
222+ b.Local = false ;
223+ b.load_packages (StringSet (lines.begin (), lines.end ()));
224+ b.prepare (); // or step?
225+
226+ // https://waf.io/apidocs/_modules/waflib/Tools/c_config.html#parse_flags
227+ primitives::Emitter ctx;
228+
229+ ctx.increaseIndent (" def configure(ctx):" );
230+
231+ for (auto &s : b.solutions )
232+ {
233+ for (auto &[pkg, t] : s.getChildren ())
234+ {
235+ auto &nt = *t->as <sw::NativeExecutedTarget>();
236+ if (t->getType () == sw::TargetType::NativeExecutable)
237+ continue ;
238+
239+ ctx.addLine (" # " + pkg.toString ());
240+ ctx.increaseIndent (" for lib in [" );
241+ for (auto i = pkg.version .getLevel (); i >= 0 ; i--)
242+ {
243+ if (i)
244+ ctx.addLine (" '" + pkg.ppath .toString () + " -" + pkg.version .toString (i) + " '," );
245+ else
246+ ctx.addLine (" '" + pkg.ppath .toString () + " '," );
247+ }
248+ ctx.decreaseIndent (" ]:" );
249+ ctx.increaseIndent ();
250+
251+ auto remove_ext = [](const auto &p)
252+ {
253+ return p.parent_path () / p.stem ();
254+ };
255+
256+ ctx.addLine (" ctx.parse_flags('-l" + normalize_path (remove_ext (nt.getImportLibrary ())) + " ', lib)" );
257+
258+ std::function<void (sw::NativeExecutedTarget&)> process;
259+ std::unordered_set<sw::NativeExecutedTarget *> visited;
260+ process = [&process, &s, &ctx, &remove_ext, &visited](auto &nt)
261+ {
262+ if (visited.find (&nt) != visited.end ())
263+ return ;
264+ visited.insert (&nt);
265+
266+ // defs
267+ for (auto &[k,v] : nt.Public .Definitions )
268+ {
269+ if (v.empty ())
270+ ctx.addLine (" ctx.parse_flags('-D" + k + " ', lib)" );
271+ else
272+ ctx.addLine (" ctx.parse_flags('-D" + k + " =" + v.toString () + " ', lib)" );
273+ }
274+ for (auto &[k,v] : nt.Interface .Definitions )
275+ {
276+ if (v.empty ())
277+ ctx.addLine (" ctx.parse_flags('-D" + k + " ', lib)" );
278+ else
279+ ctx.addLine (" ctx.parse_flags('-D" + k + " =" + v.toString () + " ', lib)" );
280+ }
281+
282+ // idirs
283+ for (auto &d : nt.Public .IncludeDirectories )
284+ ctx.addLine (" ctx.parse_flags('-I" + normalize_path (d) + " ', lib)" );
285+ for (auto &d : nt.Interface .IncludeDirectories )
286+ ctx.addLine (" ctx.parse_flags('-I" + normalize_path (d) + " ', lib)" );
287+
288+ // libs
289+ for (auto &d : nt.Public .LinkLibraries )
290+ ctx.addLine (" ctx.parse_flags('-l" + normalize_path (remove_ext (d)) + " ', lib)" );
291+ for (auto &d : nt.Interface .LinkLibraries )
292+ ctx.addLine (" ctx.parse_flags('-l" + normalize_path (remove_ext (d)) + " ', lib)" );
293+
294+ // deps
295+ for (auto &d : nt.Dependencies )
296+ {
297+ auto t = s.children [d->getResolvedPackage ()];
298+ if (t->getType () == sw::TargetType::NativeExecutable)
299+ continue ;
300+
301+ auto &nt = *s.getChildren ().find (d->getResolvedPackage ())->second ->as <sw::NativeExecutedTarget>();
302+ ctx.addLine (" ctx.parse_flags('-l" + normalize_path (remove_ext (nt.getImportLibrary ())) + " ', lib)" );
303+
304+ process (nt);
305+ }
306+ };
307+ process (nt);
308+
309+ ctx.decreaseIndent ();
310+ ctx.emptyLines ();
311+ //
312+ }
313+ break ;
314+ }
315+
316+ write_file_if_different (" wscript" , ctx.getText ());
317+
318+ return ;
203319 }
320+
321+ SW_UNIMPLEMENTED;
204322}
0 commit comments