From 093994655cab66168de6851c3b25ae6b5e941d1e Mon Sep 17 00:00:00 2001
From: Cameron Katri <me@cameronkatri.com>
Date: Wed, 30 Mar 2022 03:26:13 -0400
Subject: [PATCH] v.builder: fix iOS compilation from non-macOS, allow -cc to
 override the default cross compiler (#13866)

---
 vlib/v/builder/cc.v   | 14 ++------------
 vlib/v/gen/c/cgen.v   |  1 +
 vlib/v/pref/default.v | 24 ++++++++++++++++++++----
 3 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v
index 00830104f4..7fadc6da78 100644
--- a/vlib/v/builder/cc.v
+++ b/vlib/v/builder/cc.v
@@ -526,17 +526,7 @@ pub fn (mut v Builder) cc() {
 		// try to compile with the choosen compiler
 		// if compilation fails, retry again with another
 		mut ccompiler := v.pref.ccompiler
-		if v.pref.os == .ios {
-			ios_sdk := if v.pref.is_ios_simulator { 'iphonesimulator' } else { 'iphoneos' }
-			ios_sdk_path_res := os.execute_or_exit('xcrun --sdk $ios_sdk --show-sdk-path')
-			mut isysroot := ios_sdk_path_res.output.replace('\n', '')
-			arch := if v.pref.is_ios_simulator {
-				'-arch x86_64'
-			} else {
-				'-arch armv7 -arch armv7s -arch arm64'
-			}
-			ccompiler = 'xcrun --sdk iphoneos clang -isysroot $isysroot $arch'
-		} else if v.pref.os == .wasm32 {
+		if v.pref.os == .wasm32 {
 			ccompiler = 'clang'
 		}
 		v.setup_ccompiler_options(ccompiler)
@@ -622,7 +612,7 @@ pub fn (mut v Builder) cc() {
 				}
 				if v.pref.retry_compilation {
 					tcc_output = res
-					v.pref.ccompiler = pref.default_c_compiler()
+					v.pref.default_c_compiler()
 					if v.pref.is_verbose {
 						eprintln('Compilation with tcc failed. Retrying with $v.pref.ccompiler ...')
 					}
diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v
index 90323891f8..c48094e7e3 100644
--- a/vlib/v/gen/c/cgen.v
+++ b/vlib/v/gen/c/cgen.v
@@ -652,6 +652,7 @@ pub fn (mut g Gen) init() {
 				g.cheaders.writeln('#include <stddef.h>')
 			} else {
 				g.cheaders.writeln(get_guarded_include_text('<inttypes.h>', 'The C compiler can not find <inttypes.h>. Please install build-essentials')) // int64_t etc
+				g.cheaders.writeln(get_guarded_include_text('<stdbool.h>', 'The C compiler can not find <stdbool.h>. Please install build-essentials')) // bool, true, false
 				g.cheaders.writeln(get_guarded_include_text('<stddef.h>', 'The C compiler can not find <stddef.h>. Please install build-essentials')) // size_t, ptrdiff_t
 			}
 		}
diff --git a/vlib/v/pref/default.v b/vlib/v/pref/default.v
index 3d538fe34c..8e101fc24c 100644
--- a/vlib/v/pref/default.v
+++ b/vlib/v/pref/default.v
@@ -92,7 +92,7 @@ pub fn (mut p Preferences) fill_with_defaults() {
 	//
 	p.try_to_use_tcc_by_default()
 	if p.ccompiler == '' {
-		p.ccompiler = default_c_compiler()
+		p.default_c_compiler()
 	}
 	p.find_cc_if_cross_compiling()
 	p.ccompiler_type = cc_from_string(p.ccompiler)
@@ -203,16 +203,32 @@ pub fn default_tcc_compiler() string {
 	return ''
 }
 
-pub fn default_c_compiler() string {
+pub fn (mut p Preferences) default_c_compiler() {
 	// fast_clang := '/usr/local/Cellar/llvm/8.0.0/bin/clang'
 	// if os.exists(fast_clang) {
 	// return fast_clang
 	// }
 	// TODO fix $if after 'string'
 	$if windows {
-		return 'gcc'
+		p.ccompiler = 'gcc'
+		return
 	}
-	return 'cc'
+	if p.os == .ios {
+		$if !ios {
+			ios_sdk := if p.is_ios_simulator { 'iphonesimulator' } else { 'iphoneos' }
+			ios_sdk_path_res := os.execute_or_exit('xcrun --sdk $ios_sdk --show-sdk-path')
+			mut isysroot := ios_sdk_path_res.output.replace('\n', '')
+			arch := if p.is_ios_simulator {
+				'-arch x86_64'
+			} else {
+				'-arch armv7 -arch armv7s -arch arm64'
+			}
+			p.ccompiler = 'xcrun --sdk iphoneos clang -isysroot $isysroot $arch'
+			return
+		}
+	}
+	p.ccompiler = 'cc'
+	return
 }
 
 pub fn vexe_path() string {