ctplt 0.0.1

A package manager + build system for C and C++
Documentation
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
mod index_map;

use std::{
	collections::BTreeMap,
	fs,
	io::Write,
	path::{Path, PathBuf},
	sync::Arc,
};

use starlark::values::OwnedFrozenValue;
use uuid::Uuid;

use crate::{
	link_type::LinkPtr, //
	misc::{join_parent, Sources},
	object_library::ObjectLibrary,
	project::{Project, ProjectInfo},
	starlark_context::{StarContext, StarContextCompiler},
	starlark_generator::eval_vars,
	starlark_object_library::StarGeneratorVars,
	static_library::StaticLibrary,
	target::{LinkTarget, Target},
	toolchain::{Toolchain, VcxprojProfile},
	GlobalOptions,
};

use index_map::IndexMap;

const VS_CPP_GUID: &str = "8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942";

#[derive(Clone)]
struct VsProject {
	name: String,
	guid: String,
	vcxproj_path: String,
	dependencies: Vec<VsProject>,
	has_nasm: bool,
}

fn input_path(src: &Path, project_path: &Path) -> String {
	if src.is_relative() {
		project_path.join(src)
	} else {
		src.to_owned()
	}
	.to_str()
	.unwrap()
	.trim_start_matches(r"\\?\")
	.to_owned()
}

enum CStd {
	C11,
	C17,
}

impl CStd {
	fn as_str(&self) -> &str {
		match self {
			CStd::C11 => "stdc11",
			CStd::C17 => "stdc17",
		}
	}
}

enum CppStd {
	Cpp11,
	Cpp14,
	Cpp17,
	Cpp20,
}

impl CppStd {
	fn as_str(&self) -> &str {
		match self {
			CppStd::Cpp11 => "stdcpp11",
			CppStd::Cpp14 => "stdcpp14",
			CppStd::Cpp17 => "stdcpp17",
			CppStd::Cpp20 => "stdcpp20",
		}
	}
}

struct Options {
	c_standard: Option<CStd>,
	cpp_standard: Option<CppStd>,
}

impl VsProject {
	fn to_sln_project_section(&self) -> String {
		let proj_name = &self.name;
		let vcxproj_path = &self.vcxproj_path;
		let guid = self.guid.to_string().to_ascii_uppercase();
		let mut ret = format!(
			r#"Project("{{{VS_CPP_GUID}}}") = "{proj_name}", "{vcxproj_path}", "{{{guid}}}"
"#
		);
		if !self.dependencies.is_empty() {
			ret += "	ProjectSection(ProjectDependencies) = postProject\n";
		}
		for dep in &self.dependencies {
			let dep_guid = &dep.guid.to_string().to_ascii_uppercase();
			ret += &format!("		{{{dep_guid}}} = {{{dep_guid}}}\n");
		}
		if !self.dependencies.is_empty() {
			ret += "	EndProjectSection\n";
		}
		ret += "EndProject\n";
		ret
	}
}

struct ProfileFragment {
	vcxproj: VcxprojProfile,
	nasm_assemble_flags: Vec<String>,
}

fn item_definition_group(
	platform: &str,
	profile_name: &str,
	profile: &ProfileFragment,
	sources: &Sources,
	include_dirs: &[String],
	defines: &[String],
	opts: &Options,
) -> Result<String, String> {
	let mut ret = format!(
		r#"  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='{profile_name}|{platform}'">
"#
	);

	if !sources.c.is_empty() || !sources.cpp.is_empty() {
		ret += &cl_compile(&profile.vcxproj, include_dirs, defines, opts, sources.cpp.is_empty());
	}
	if !sources.nasm.is_empty() {
		ret += &nasm_compile(profile, platform, include_dirs, defines)?;
	}
	if !profile.vcxproj.link.is_empty() {
		ret += "    <Link>\n";
		for (key, val) in &profile.vcxproj.link {
			ret += &format!("      <{key}>{val}</{key}>\n")
		}
		ret += "    </Link>\n";
	}
	ret += "  </ItemDefinitionGroup>\n";

	Ok(ret)
}

fn item_group_conditional(sources: &Sources, project_info: &ProjectInfo, platform: &str) -> String {
	let item_group_tag = format!(
		r#"  <ItemGroup Condition="'$(Platform)'=='{platform}'">
"#
	);
	let mut ret = String::new();
	if !sources.c.is_empty() {
		ret += &item_group_tag;
		for src in &sources.c {
			let input = input_path(&src.full, &project_info.path);
			ret += &format!("    <ClCompile Include=\"{input}\" />\n");
		}
		ret += "  </ItemGroup>\n";
	}
	if !sources.cpp.is_empty() {
		ret += &item_group_tag;
		for src in &sources.cpp {
			let input = input_path(&src.full, &project_info.path);
			ret += &format!("    <ClCompile Include=\"{input}\" />\n");
		}
		ret += "  </ItemGroup>\n";
	}
	if !sources.nasm.is_empty() {
		ret += &item_group_tag;
		for src in &sources.nasm {
			let input = input_path(&src.full, &project_info.path);
			ret += &format!("    <NASM Include=\"{input}\" />\n");
		}
		ret += "  </ItemGroup>\n";
	}
	ret
}

fn cl_compile(
	profile: &VcxprojProfile,
	include_dirs: &[String],
	defines: &[String],
	opts: &Options,
	compile_as_c: bool,
) -> String {
	let mut ret = "    <ClCompile>\n".to_owned();

	for (key, val) in &profile.cl_compile {
		ret += &format!("      <{key}>{val}</{key}>\n");
	}

	if compile_as_c {
		if let Some(c_std) = &opts.c_standard {
			ret += "      <LanguageStandard_C>";
			ret += c_std.as_str();
			ret += "</LanguageStandard_C>\n";
			ret += "      <CompileAs>CompileAsC</CompileAs>\n";
		}
	} else if let Some(cpp_std) = &opts.cpp_standard {
		ret += "      <LanguageStandard>";
		ret += cpp_std.as_str();
		ret += "</LanguageStandard>\n";
	}

	ret += "      <AdditionalIncludeDirectories>";
	ret += &include_dirs
		.iter()
		.chain(&["%(AdditionalIncludeDirectories)".to_owned()])
		.fold(String::new(), |acc, x| acc + ";" + x);
	ret += "</AdditionalIncludeDirectories>\n";

	ret += "      <ConformanceMode>true</ConformanceMode>\n";

	// TODO(Travers): Add global options for warnings
	// <WarningLevel>Level4</WarningLevel>
	// <TreatWarningAsError>false</TreatWarningAsError>
	// TODO(Travers): Add other definitions and compile flags
	ret += "      <PreprocessorDefinitions>";
	ret += &profile
		.preprocessor_definitions
		.iter()
		.chain(defines)
		.chain([&"%(PreprocessorDefinitions)".to_owned()])
		.fold(String::new(), |acc, x| acc + x + ";");
	ret += "</PreprocessorDefinitions>\n";
	// ret += r#"      <ObjectFileName>$(IntDir)</ObjectFileName>
	ret += "    </ClCompile>\n";
	ret
}

fn nasm_compile(
	profile: &ProfileFragment,
	platform: &str,
	include_dirs: &[String],
	defines: &[String],
) -> Result<String, String> {
	let mut ret = "    <NASM>\n".to_owned();

	ret += "      <Format>";
	ret += map_platform_to_nasm_format(platform)?;
	ret += "</Format>\n";

	ret += "      <IncludePaths>";
	ret += &include_dirs.join(";");
	ret += "</IncludePaths>\n";

	ret += "      <Define>";
	ret += &profile
		.vcxproj
		.preprocessor_definitions
		.iter()
		.chain(defines)
		.fold(String::new(), |acc, x| acc + x + ";");
	ret += "%(PreprocessorDefinitions)</Define>\n"; // TODO(Travers): Check this

	ret += "      <AdditionalOptions>";
	ret += &profile.nasm_assemble_flags.join(" ");
	ret += "</AdditionalOptions>\n";

	ret += "    </NASM>\n";
	Ok(ret)
}

struct TargetData {
	name: String,
	sources: Sources,
	includes: Vec<String>,
	defines: Vec<String>,
	links: Vec<LinkPtr>,
	generator_vars: Option<OwnedFrozenValue>,
}

struct VcxprojOpts {
	build_dir: PathBuf,
	profiles: BTreeMap<String, ProfileFragment>,
	msvc_platforms: Vec<String>,
	opts: Options,
}

pub struct Msvc {}

impl Msvc {
	pub fn generate(
		project: Arc<Project>,
		build_dir: &Path,
		toolchain: Toolchain,
		global_opts: GlobalOptions,
	) -> Result<(), String> {
		if toolchain.msvc_platforms.is_empty() {
			return Err("Toolchain doesn't contain any msvc_platforms, required for MSVC generator".to_owned());
		}
		let mut guid_map = IndexMap::new();
		let c_standard = match global_opts.c_standard {
			None => None,
			Some(x) => match x.as_str() {
				"11" => Some(CStd::C11),
				"17" => Some(CStd::C17),
				_ => {
					return Err(format!(
						"Unrecognized value for option for \"c_standard\": \"{x}\". Accepted values are \"17\", \"11\"",
					))
				}
			},
		};
		let cpp_standard = match global_opts.cpp_standard {
			None => None,
			Some(x) => match x.as_str() {
				"11" => Some(CppStd::Cpp11),
				"14" => Some(CppStd::Cpp14),
				"17" => Some(CppStd::Cpp17),
				"20" => Some(CppStd::Cpp20),
				_ => {
					return Err(format!(
						"Unrecognized value for option for \"cpp_standard\": \"{x}\". Accepted values are \"20\", \"17\", \"14\", \"11\"",
					))
				}
			},
		};
		let profiles = toolchain
			.profile
			.iter()
			.filter_map(|x| {
				x.1.vcxproj.as_ref().map(|prof| {
					(
						x.0.clone(),
						ProfileFragment {
							vcxproj: prof.clone(),
							nasm_assemble_flags: x.1.nasm_assemble_flags.clone(),
						},
					)
				})
			})
			.collect::<BTreeMap<String, ProfileFragment>>();
		if profiles.is_empty() {
			return Err(
				"Toolchain doesn't contain any profiles with a \"vcxproj\" section, required for MSVC generator"
					.to_owned(),
			);
		}
		let proj_opts = VcxprojOpts {
			build_dir: build_dir.to_owned(),
			profiles,
			msvc_platforms: toolchain.msvc_platforms,
			opts: Options { c_standard, cpp_standard },
		};
		Self::generate_inner(&project, &proj_opts, &mut guid_map)?;

		let mut sln_content = r#"Microsoft Visual Studio Solution File, Format Version 12.00
"#
		.to_string();

		// Reverse iterate to put the most important projects at the top of the Solution Explorer
		for proj in guid_map.iter().rev() {
			sln_content += &proj.to_sln_project_section();
		}
		sln_content += r#"Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
"#;
		for profile in &toolchain.profile {
			let profile_name = profile.0;
			for platform in &proj_opts.msvc_platforms {
				sln_content += &format!("\t\t{profile_name}|{platform} = {profile_name}|{platform}\n");
			}
		}
		sln_content += "	EndGlobalSection\n";

		sln_content += "	GlobalSection(ProjectConfigurationPlatforms) = postSolution\n";
		for proj in &guid_map {
			let guid = &proj.guid.to_string().to_ascii_uppercase();
			for profile in &toolchain.profile {
				let prof_name = profile.0;
				for platform in &proj_opts.msvc_platforms {
					sln_content += &format!("		{{{guid}}}.{prof_name}|{platform}.ActiveCfg = {prof_name}|{platform}\n");
					sln_content += &format!("		{{{guid}}}.{prof_name}|{platform}.Build.0 = {prof_name}|{platform}\n");
				}
			}
		}
		sln_content += "	EndGlobalSection\n";

		let sln_guid = Uuid::new_v4().to_string().to_ascii_uppercase();
		sln_content += &format!(
			r#"	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(ExtensibilityGlobals) = postSolution
		SolutionGuid = {{{sln_guid}}}
	EndGlobalSection
"#
		);
		sln_content += "EndGlobal\n";

		let sln_pathbuf = build_dir.join(project.info.name.clone() + ".sln");
		write_file(&sln_pathbuf, &sln_content)?;

		if guid_map.iter().any(|x| x.has_nasm) {
			if let Some(nasm_assembler) = toolchain.nasm_assembler {
				write_file(&build_dir.join("nasm.xml"), NASM_XML_CONTENT)?;
				write_file(&build_dir.join("nasm.props"), &nasm_props_content(&nasm_assembler.cmd()))?;
				write_file(&build_dir.join("nasm.targets"), NASM_TARGETS_CONTENT)?;
			} else {
				return Err(
					"Toolchain does not contain a NASM assembler, required for files in this project".to_owned()
				);
			}
		}

		Ok(())
	}

	fn generate_inner(project: &Arc<Project>, proj_opts: &VcxprojOpts, guid_map: &mut IndexMap) -> Result<(), String> {
		for subproject in &project.dependencies {
			Self::generate_inner(subproject, proj_opts, guid_map)?;
		}

		for lib in &project.static_libraries {
			if !guid_map.contains_key(&LinkPtr::Static(lib.clone())) {
				add_static_lib(lib, proj_opts, guid_map)?;
			}
		}
		for lib in &project.object_libraries {
			if !guid_map.contains_key(&LinkPtr::Object(lib.clone())) {
				add_object_lib(lib, proj_opts, guid_map)?;
			}
		}
		for exe in &project.executables {
			let configuration_type = "Application";
			let project_info = &exe.project().info;
			let target_data = TargetData {
				name: exe.name.clone(),
				sources: exe.sources.clone(),
				// Visual Studio doesn't seem to support extended-length name syntax
				includes: exe
					.public_includes_recursive()
					.into_iter()
					.map(|x| x.to_string_lossy().trim_start_matches(r"\\?\").to_owned())
					.collect::<Vec<String>>(),
				defines: exe.public_defines_recursive(),
				links: exe.links.clone(),
				generator_vars: exe.generator_vars.clone(),
			};
			let vsproj = make_vcxproj(proj_opts, guid_map, configuration_type, project_info, &target_data)?;
			guid_map.insert_exe(vsproj);
		}
		Ok(())
	}
}

fn add_static_lib(
	lib: &Arc<StaticLibrary>,
	proj_opts: &VcxprojOpts,
	guid_map: &mut IndexMap,
) -> Result<VsProject, String> {
	log::debug!("add_static_lib: {}", lib.name);
	let project_info = &lib.project().info;
	let mut includes = lib.public_includes_recursive();
	includes.extend_from_slice(&lib.private_includes());
	let includes = includes
		.into_iter()
		// Visual Studio doesn't seem to support extended-length name syntax
		.map(|x| x.to_string_lossy().trim_start_matches(r"\\?\").to_owned())
		.collect::<Vec<String>>();
	let mut defines = lib.public_defines_recursive();
	defines.extend_from_slice(lib.private_defines());
	let links = lib
		.link_private
		.iter()
		.cloned()
		.chain(lib.link_public.iter().cloned())
		.collect();
	let target_data = TargetData {
		name: lib.name.clone(),
		sources: lib.sources.clone(),
		includes,
		defines,
		links,
		generator_vars: lib.generator_vars.clone(),
	};
	let vsproj = make_vcxproj(proj_opts, guid_map, "StaticLibrary", project_info, &target_data)?;
	let link_ptr = LinkPtr::Static(lib.clone());
	guid_map.insert(link_ptr, vsproj.clone());
	Ok(vsproj)
}

fn add_object_lib(
	lib: &Arc<ObjectLibrary>,
	proj_opts: &VcxprojOpts,
	guid_map: &mut IndexMap,
) -> Result<VsProject, String> {
	log::debug!("add_object_lib: {}", lib.name);
	let project_info = &lib.project().info;
	let mut includes = lib.public_includes_recursive();
	includes.extend_from_slice(&lib.private_includes());
	let includes = includes
		.into_iter()
		// Visual Studio doesn't seem to support extended-length name syntax
		.map(|x| x.to_string_lossy().trim_start_matches(r"\\?\").to_owned())
		.collect::<Vec<String>>();
	let mut defines = lib.public_defines_recursive();
	defines.extend_from_slice(lib.private_defines());
	let links = lib
		.link_private
		.iter()
		.cloned()
		.chain(lib.link_public.iter().cloned())
		.collect();
	let target_data = TargetData {
		name: lib.name.clone(),
		sources: lib.sources.clone(),
		includes,
		defines,
		links,
		generator_vars: lib.generator_vars.clone(),
	};
	let vsproj = make_vcxproj(proj_opts, guid_map, "StaticLibrary", project_info, &target_data)?;
	guid_map.insert(LinkPtr::Object(lib.clone()), vsproj.clone());
	Ok(vsproj)
}

fn make_vcxproj(
	proj_opts: &VcxprojOpts,
	guid_map: &mut IndexMap,
	configuration_type: &str,
	project_info: &ProjectInfo,
	target_data: &TargetData,
) -> Result<VsProject, String> {
	let target_name = &target_data.name;
	let sources = &target_data.sources;

	log::debug!("make_vcxproj: {target_name}");
	if !target_data.sources.c.is_empty() && !target_data.sources.cpp.is_empty() {
		return Err(format!("This generator does not support mixing C and C++ sources. Consider splitting them into separate libraries. Target: {target_name}"));
	}
	const PLATFORM_TOOLSET: &str = "v143";
	let target_guid = Uuid::new_v4().to_string().to_ascii_uppercase();
	let mut out_str = r#"<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
"#
	.to_owned();
	for platform in &proj_opts.msvc_platforms {
		for profile_name in proj_opts.profiles.keys() {
			out_str += &format!(
				r#"    <ProjectConfiguration Include="{profile_name}|{platform}">
      <Configuration>{profile_name}</Configuration>
      <Platform>{platform}</Platform>
    </ProjectConfiguration>
"#
			);
		}
	}
	out_str += "  </ItemGroup>\n";
	out_str += &format!(
		r#"  <PropertyGroup Label="Globals">
    <VCProjectVersion>16.0</VCProjectVersion>
    <Keyword>Win32Proj</Keyword>
    <ProjectGuid>{{{target_guid}}}</ProjectGuid>
    <RootNamespace>{target_name}</RootNamespace>
    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.default.props" />
"#
	);
	for platform in &proj_opts.msvc_platforms {
		for (profile_name, profile_cfg) in &proj_opts.profiles {
			out_str += &format!(
				r#"  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='{profile_name}|{platform}'" Label="Configuration">
    <ConfigurationType>{configuration_type}</ConfigurationType>
    <PlatformToolset>{PLATFORM_TOOLSET}</PlatformToolset>
"#
			);
			// <UseDebugLibraries>true</UseDebugLibraries>
			// <CharacterSet>MultiByte</CharacterSet>
			// <WholeProgramOptimization>true</WholeProgramOptimization>
			for (prop_name, prop_val) in &profile_cfg.vcxproj.property_group {
				out_str += &format!("    <{prop_name}>{prop_val}</{prop_name}>\n");
			}
			out_str += "  </PropertyGroup>\n";
		}
	}
	out_str += r#"  <Import Project="$(VCTargetsPath)\\Microsoft.Cpp.props" />
  <ImportGroup Label="ExtensionSettings">
"#;

	let mut item_definition_groups = Vec::new();
	let mut item_groups = Vec::new();
	let mut has_nasm = !sources.nasm.is_empty();
	for platform in &proj_opts.msvc_platforms {
		let generator_vars = if let Some(gen_func) = &target_data.generator_vars {
			let target_triple = map_platform_to_target_triple(platform)?;
			let star_context = StarContext {
				c_compiler: Some(StarContextCompiler { target_triple: target_triple.to_owned() }),
				cpp_compiler: Some(StarContextCompiler { target_triple: target_triple.to_owned() }),
			};
			eval_vars(gen_func, star_context.clone(), "generator_vars")?
		} else {
			StarGeneratorVars::default()
		};
		let generator_sources = Sources::from_slice(&generator_vars.sources, &project_info.path)?;
		has_nasm |= !generator_sources.nasm.is_empty();
		let sources_gen = sources.extended_with(&generator_sources);
		let includes_gen = target_data
			.includes
			.clone()
			.into_iter()
			.chain(
				generator_vars
					.include_dirs
					.iter()
					.map(|x| join_parent(&project_info.path, x).full.to_string_lossy().to_string()),
			)
			.collect::<Vec<_>>();
		let defines_gen = target_data
			.defines
			.clone()
			.into_iter()
			.chain(generator_vars.defines.clone())
			.collect::<Vec<_>>();
		for (profile_name, profile) in &proj_opts.profiles {
			item_definition_groups.push(item_definition_group(
				platform,
				profile_name,
				profile,
				&sources_gen,
				&includes_gen,
				&defines_gen,
				&proj_opts.opts,
			)?);
		}
		item_groups.push(item_group_conditional(&generator_sources, project_info, platform));
	}
	// Make these variables immutable
	let item_definition_groups = item_definition_groups;
	let item_groups = item_groups;
	let has_nasm = has_nasm;

	if has_nasm {
		out_str += r#"    <Import Project="..\..\nasm.props" />
"#;
	}
	out_str += r#"  </ImportGroup>
  <ImportGroup Label="Shared">
  </ImportGroup>
"#;
	for platform in &proj_opts.msvc_platforms {
		for profile_name in proj_opts.profiles.keys() {
			out_str += &format!(
				r#"  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='{profile_name}|{platform}'">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
"#
			);
		}
	}

	out_str += "  <PropertyGroup Label=\"UserMacros\" />\n";

	for item in item_definition_groups {
		out_str += &item;
	}
	for item in item_groups {
		out_str += &item;
	}
	if !sources.c.is_empty() {
		out_str += "  <ItemGroup>\n";
		for src in &sources.c {
			let input = input_path(&src.full, &project_info.path);
			out_str += &format!("    <ClCompile Include=\"{input}\" />\n");
		}
		out_str += "  </ItemGroup>\n";
	}
	if !sources.cpp.is_empty() {
		out_str += "  <ItemGroup>\n";
		for src in &sources.cpp {
			let input = input_path(&src.full, &project_info.path);
			out_str += &format!("    <ClCompile Include=\"{input}\" />\n");
		}
		out_str += "  </ItemGroup>\n";
	}
	if !sources.nasm.is_empty() {
		out_str += "  <ItemGroup>\n";
		for src in &sources.nasm {
			let input = input_path(&src.full, &project_info.path);
			out_str += &format!("    <NASM Include=\"{input}\" />\n");
		}
		out_str += "  </ItemGroup>\n";
	}

	let mut dependencies = Vec::new();
	if !target_data.links.is_empty() {
		out_str += "  <ItemGroup>\n";
		out_str += &add_project_references(&target_data.links, proj_opts, guid_map, &mut dependencies)?;
		out_str += "  </ItemGroup>\n";
	}
	out_str += r#"  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <ImportGroup Label="ExtensionTargets">
"#;
	if has_nasm {
		out_str += r#"    <Import Project="..\..\nasm.targets" />
"#;
	}
	out_str += "  </ImportGroup>\n";
	out_str += "</Project>\n";
	let vcxproj_pathbuf = PathBuf::from(&project_info.name)
		.join(target_name)
		.join(target_name.to_owned() + ".vcxproj");
	let vcxproj_pathbuf_abs = proj_opts.build_dir.join(&vcxproj_pathbuf);
	let vcxproj_path = vcxproj_pathbuf.to_string_lossy().into_owned();
	let vsproj = VsProject {
		name: target_name.to_owned(),
		guid: target_guid,
		vcxproj_path,
		dependencies,
		has_nasm,
	};

	if let Err(e) = fs::create_dir_all(vcxproj_pathbuf_abs.parent().unwrap()) {
		return Err(format!("Error creating directory for \"{}\": {}", vcxproj_pathbuf.to_string_lossy(), e));
	};
	write_file(&vcxproj_pathbuf_abs, &out_str)?;
	Ok(vsproj)
}

fn add_project_references(
	project_links: &Vec<LinkPtr>,
	proj_opts: &VcxprojOpts,
	guid_map: &mut IndexMap,
	dependencies: &mut Vec<VsProject>,
) -> Result<String, String> {
	log::debug!("add_project_references() {}", project_links.len());
	let mut out_str = String::new();
	for link in project_links {
		log::debug!("   link: {}", link.name());
		let mut add_dependency = |proj_ref: &VsProject| {
			log::debug!("   add_dependency() {}", proj_ref.name);
			dependencies.push(proj_ref.clone());
			let proj_ref_include = proj_opts.build_dir.join(&proj_ref.vcxproj_path);
			out_str += &format!(
				r#"    <ProjectReference Include="{}">
      <Project>{{{}}}</Project>
      <Name>{}</Name>
      <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </ProjectReference>
"#,
				proj_ref_include.to_string_lossy(),
				proj_ref.guid,
				link.name()
			);
		};
		log::debug!("   match link: {}", link.name());
		match link {
			LinkPtr::Static(static_lib) => {
				let proj_ref = match guid_map.get(link) {
					Some(x) => x,
					None => {
						add_static_lib(static_lib, proj_opts, guid_map)?;
						guid_map.get(link).unwrap()
					}
				};
				add_dependency(proj_ref);
			}
			LinkPtr::Object(obj_lib) => {
				let proj_ref = match guid_map.get(link) {
					Some(x) => x,
					None => {
						add_object_lib(obj_lib, proj_opts, guid_map)?;
						guid_map.get(link).unwrap()
					}
				};
				add_dependency(proj_ref);
			}
			LinkPtr::Interface(_) => {
				out_str += &add_project_references(&link.public_links(), proj_opts, guid_map, dependencies)?;
			}
		}
	}
	Ok(out_str)
}

fn write_file(filepath: &Path, content: &str) -> Result<(), String> {
	let mut f = match fs::File::create(filepath) {
		Ok(x) => x,
		Err(e) => return Err(format!("Error creating file at \"{}\": {}", filepath.to_string_lossy(), e)),
	};
	if let Err(e) = f.write_all(content.as_bytes()) {
		return Err(format!("Error writing to {}: {}", filepath.to_string_lossy(), e));
	}
	Ok(())
}

const NASM_XML_CONTENT: &str = include_str!("msvc/nasm.xml");
const NASM_TARGETS_CONTENT: &str = include_str!("msvc/nasm.targets");

fn nasm_props_content(nasm_cmd: &[String]) -> String {
	let mapped_vec = nasm_cmd.iter().map(|x| format!("\"{}\"", x)).collect::<Vec<String>>();
	format!(include_str!("msvc/nasm.props"), mapped_vec.join(" "))
}

fn map_platform_to_target_triple(platform: &str) -> Result<&'static str, String> {
	let platform_target = [
		("ARM", "aarch32-pc-windows-msvc"),
		("ARM64", "aarch64-pc-windows-msvc"),
		("Win32", "i686-pc-windows-msvc"),
		("x64", "x86_64-pc-windows-msvc"),
	];
	match platform_target.iter().find(|x| platform == x.0) {
		Some(x) => Ok(x.1),
		None => Err(format!(
			"Unknown platform: {platform}. Known platforms are {}",
			platform_target.map(|x| format!("\"{}\"", x.0)).join(", ")
		)),
	}
}

fn map_platform_to_nasm_format(platform: &str) -> Result<&'static str, String> {
	let platform_target = [("Win32", "win32"), ("x64", "win64")];
	match platform_target.iter().find(|x| platform == x.0) {
		Some(x) => Ok(x.1),
		None => Err(format!(
			"Unknown platform: {platform}. Known platforms are {}",
			platform_target.map(|x| format!("\"{}\"", x.0)).join(", ")
		)),
	}
}