From 41fa1ad0f7adaee9295fbdd405d48a59cacb7bed Mon Sep 17 00:00:00 2001 From: Patrick Li Date: Fri, 9 Dec 2022 22:09:59 -0800 Subject: [PATCH 1/4] Change to using c++ driver. --- compiler/main.stanza | 6 +++--- compiler/system-dependencies.stanza | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/main.stanza b/compiler/main.stanza index ed173e8cd..115f9cb66 100644 --- a/compiler/main.stanza +++ b/compiler/main.stanza @@ -129,9 +129,9 @@ defn build-system (verbose?:True|False) : ;Compiler name emit-arg $ switch(platform) : - `os-x : "cc" - `linux : "cc" - `windows : "gcc" + `os-x : "c++" + `linux : "c++" + `windows : "g++" ;All files emit-arg(asm) diff --git a/compiler/system-dependencies.stanza b/compiler/system-dependencies.stanza index 22c31b49a..3ba690ea2 100644 --- a/compiler/system-dependencies.stanza +++ b/compiler/system-dependencies.stanza @@ -10,8 +10,8 @@ public defn system-dependencies (platform:Symbol) -> ProjDependencies : val ccfiles = Vector() val ccflags = Vector() - ;C Standard - add(ccflags, "-std=gnu99") + ;C++ Standard + add(ccflags, "-std=gnu++11") ;Use static linking on windows if platform == `windows : From d2f0ab7fa35f306273d13cb1ec701daf8f9f3eb2 Mon Sep 17 00:00:00 2001 From: Patrick Li Date: Fri, 9 Dec 2022 22:38:15 -0800 Subject: [PATCH 2/4] Change back to C driver, but include C++ libraries. --- compiler/main.stanza | 55 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/compiler/main.stanza b/compiler/main.stanza index 115f9cb66..b069f6bdf 100644 --- a/compiler/main.stanza +++ b/compiler/main.stanza @@ -128,17 +128,30 @@ defn build-system (verbose?:True|False) : defn emit-args (ss:Seqable) : do(emit-arg, ss) ;Compiler name - emit-arg $ switch(platform) : - `os-x : "c++" - `linux : "c++" - `windows : "g++" + val cc-prog = switch(platform) : + `os-x : "cc" + `linux : "cc" + `windows : "gcc" + emit-arg(cc-prog) ;All files emit-arg(asm) emit-args(ccfiles) - ;All flags + ;User flags emit-args(ccflags) + + ;If a C++ library hasn't already been included, then + ;include it. + if not contains?(ccflags, "-lc++") and + not contains?(ccflags, "-lstdc++") : + val libcpp = switch(platform) : + `os-x : "-lc++" + `linux : "-lstdc++" + `windows : "-lstdc++" + emit-arg(libcpp) + + ;Output file. emit-args(["-o" output]) ;Output for debugging @@ -148,11 +161,18 @@ defn build-system (verbose?:True|False) : for a in args do : println("%~" % [a]) - ;Call system - val return-code = call-system(args[0], to-tuple(args)) - - ;Return true if successful - return-code == 0 + ;First directly try calling the cc-prog driver. + try : + ;Call system + val return-code = call-system(to-tuple(args)) + ;Return true if successful + return-code == 0 + + ;If this particular type of error occurs, then + ;double-check whether GCC is installed. + catch (e:ProcessLaunchError) : + ensure-cc-installed(cc) + throw(e) defmethod call-shell (this, platform:Symbol, command:String) : if verbose? : @@ -183,6 +203,21 @@ defn build-system (verbose?:True|False) : println("Delete temporary file %~." % [file]) delete-file(file) +;Helper: Check whether the given cc driver is installed. +;If it isn't, then an error is thrown asking the user to install it. +;If it is installed, or if some error occurs when attempting to check +;then false is returned. +defn ensure-cc-installed (cc:String) -> False : + try : + call-system([cc "--version"]) + false + catch (e:ProcessLaunchError) : + val msg = "Stanza requires a C compiler to be installed and executable via \ + the %~ command. Please double-check this and run again." + throw(Exception(msg % [cc])) + catch (e2) : + false + ;============================================================ ;================= Common Stanza Flags ====================== ;============================================================ From 28ec4eb3eb6c52f90c5d2dafa964c6213084d8d0 Mon Sep 17 00:00:00 2001 From: Patrick Li Date: Fri, 9 Dec 2022 22:38:59 -0800 Subject: [PATCH 3/4] Typo. --- compiler/main.stanza | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/main.stanza b/compiler/main.stanza index b069f6bdf..71a4b0ad5 100644 --- a/compiler/main.stanza +++ b/compiler/main.stanza @@ -171,7 +171,7 @@ defn build-system (verbose?:True|False) : ;If this particular type of error occurs, then ;double-check whether GCC is installed. catch (e:ProcessLaunchError) : - ensure-cc-installed(cc) + ensure-cc-installed(cc-prog) throw(e) defmethod call-shell (this, platform:Symbol, command:String) : From 3da1a94d870cac3c5e765162546441e53c5f3bb7 Mon Sep 17 00:00:00 2001 From: Patrick Li Date: Fri, 9 Dec 2022 22:47:19 -0800 Subject: [PATCH 4/4] Add standard C++ library. --- compiler/main.stanza | 10 ---------- compiler/system-dependencies.stanza | 10 ++++++++-- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/compiler/main.stanza b/compiler/main.stanza index 71a4b0ad5..922a1064e 100644 --- a/compiler/main.stanza +++ b/compiler/main.stanza @@ -141,16 +141,6 @@ defn build-system (verbose?:True|False) : ;User flags emit-args(ccflags) - ;If a C++ library hasn't already been included, then - ;include it. - if not contains?(ccflags, "-lc++") and - not contains?(ccflags, "-lstdc++") : - val libcpp = switch(platform) : - `os-x : "-lc++" - `linux : "-lstdc++" - `windows : "-lstdc++" - emit-arg(libcpp) - ;Output file. emit-args(["-o" output]) diff --git a/compiler/system-dependencies.stanza b/compiler/system-dependencies.stanza index 3ba690ea2..920d1facc 100644 --- a/compiler/system-dependencies.stanza +++ b/compiler/system-dependencies.stanza @@ -10,8 +10,8 @@ public defn system-dependencies (platform:Symbol) -> ProjDependencies : val ccfiles = Vector() val ccflags = Vector() - ;C++ Standard - add(ccflags, "-std=gnu++11") + ;C Standard + add(ccflags, "-std=gnu11") ;Use static linking on windows if platform == `windows : @@ -40,6 +40,12 @@ public defn system-dependencies (platform:Symbol) -> ProjDependencies : if platform == `linux : add(ccflags, "-D_GNU_SOURCE") + ;Include standard C++ library. + switch(platform) : + `os-x : add(ccflags, "-lc++") + `linux : add(ccflags, "-lstdc++") + `windows : add(ccflags, "-lstdc++") + ;Stanza include dir add(ccflags, to-string("-I%_" % [system-filepath(StanzaIncludeDir)]))