Skip to content

Commit

Permalink
Merge autoadd of c++ libraries during linking.
Browse files Browse the repository at this point in the history
  • Loading branch information
CuppoJava committed Dec 10, 2022
2 parents 9629989 + 3da1a94 commit ffc9a17
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
41 changes: 33 additions & 8 deletions compiler/main.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,20 @@ defn build-system (verbose?:True|False) :
defn emit-args (ss:Seqable<String>) : do(emit-arg, ss)

;Compiler name
emit-arg $ switch(platform) :
val cc-prog = switch(platform) :
`os-x : "cc"
`linux : "cc"
`windows : "gcc"
`windows : "gcc"
emit-arg(cc-prog)

;All files
emit-arg(asm)
emit-args(ccfiles)

;All flags
;User flags
emit-args(ccflags)

;Output file.
emit-args(["-o" output])

;Output for debugging
Expand All @@ -148,11 +151,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-prog)
throw(e)

defmethod call-shell (this, platform:Symbol, command:String) :
if verbose? :
Expand Down Expand Up @@ -183,6 +193,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 ======================
;============================================================
Expand Down
8 changes: 7 additions & 1 deletion compiler/system-dependencies.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public defn system-dependencies (platform:Symbol) -> ProjDependencies :
val ccflags = Vector<String>()

;C Standard
add(ccflags, "-std=gnu99")
add(ccflags, "-std=gnu11")

;Use static linking on windows
if platform == `windows :
Expand Down Expand Up @@ -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)]))

Expand Down

0 comments on commit ffc9a17

Please sign in to comment.