Skip to content

Commit

Permalink
* On Unix-like systems (Linux/macOS/BSD etc., including WSL) it's now…
Browse files Browse the repository at this point in the history
… possible to set custom C/C++ compiler for ficus programs using CC and CXX environment variables, e.g.

"CC=gcc-10 bin/ficus -O3 -run examples/mandelbrot.fx"
* "-Ofast" option is added that means passing "-Ofast" to GCC/Clang. Maybe, later on -Ofast will also turn on some extra optimizations at Ficus level.
  • Loading branch information
vpisarev committed Dec 13, 2021
1 parent 4dfc83e commit 857014a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
17 changes: 13 additions & 4 deletions compiler/Compiler.fx
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ fun k2c_all(kmods: kmodule_t list)
fun run_cc(cmods: C_form.cmodule_t list, ficus_root: string) {
val osinfo = Sys.osname(true)
val opt_level = Options.opt.optimize_level
val opt_level_str = if opt_level <= 3 {string(opt_level)} else {"fast"}
val enable_openmp = Options.opt.enable_openmp
val runtime_include_path = Filename.normalize(ficus_root, "runtime")
val runtime_lib_path = Filename.normalize(ficus_root, "runtime/lib")
Expand All @@ -398,10 +399,19 @@ fun run_cc(cmods: C_form.cmodule_t list, ficus_root: string) {
("win", "cl", "cl", ".obj", "/c /Fo", "/Fe", "", cflags, "/nologo /F10485760 kernel32.lib advapi32.lib")
} else {
// unix or hopefully something more or less compatible with it
val c_comp = Sys.getenv("CC", "cc")
val cpp_comp_name = Sys.getenv("CXX", "c++")
val cpp_comp = f"{cpp_comp_name} -std=c++11"
val (os, libpath, cflags, clibs) =
if osinfo.contains("Darwin") {
val (omp_cflags, omp_lib) =
if enable_openmp { ("-Xclang -fopenmp", " -lomp") }
if enable_openmp {
if c_comp.contains("gcc") {
("-fopenmp", " -lgomp")
} else {
("-Xclang -fopenmp", " -lomp")
}
}
else { ("", "") }
val (libpath, cflags, clibs) =
if osinfo.contains("x86_64") {
Expand All @@ -424,11 +434,10 @@ fun run_cc(cmods: C_form.cmodule_t list, ficus_root: string) {
} else {
("", "", "", "")
}
val c_comp = "cc"
val cpp_comp = "c++ -std=c++11"
val common_cflags = "-Wno-unknown-warning-option -Wno-dangling-else -Wno-static-in-inline -Wno-parentheses"
val ggdb_opt = if opt_level == 0 { " -ggdb" } else { "" }
val cflags = f"-O{opt_level}{ggdb_opt} {cflags} {common_cflags} -I{runtime_include_path}"

val cflags = f"-O{opt_level_str}{ggdb_opt} {cflags} {common_cflags} -I{runtime_include_path}"
val clibs = (if libpath!="" {f"-L{runtime_lib_path}/{libpath} "} else {""}) + f"-lm {clibs}"
(os, c_comp, cpp_comp, ".o", "-c -o ", "-o ", "-l", cflags, clibs)
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/Options.fx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ where options can be some of:
except for the most essential ones
-O1 Optimization level 1 (default): enable most of the optimizations
-O3 Optimization level 3: enable all optimizations
-Ofast Optimization level 3; passes '-Ofast' to C/C++ compiler
-no-openmp Disable OpenMP (OpenMP is enabled by default)
-debug Turn on debug information, disable optimizations
(but it can be overwritten with further -On)
Expand Down Expand Up @@ -158,6 +159,8 @@ fun parse_options(): bool {
opt.optimize_level = 1; next
| "-O3" :: next =>
opt.optimize_level = 3; next
| "-Ofast" :: next =>
opt.optimize_level = 100; next
| "-no-openmp" :: next =>
opt.enable_openmp = false; next
| "-debug" :: next =>
Expand Down
6 changes: 6 additions & 0 deletions lib/Sys.fx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ fun getenv(name: string): string = @ccode {
return fx_status;
}

fun getenv(name: string, defval: string)
{
val s = getenv(name)
if s != "" {s} else {defval}
}

fun getpath(name: string): string list
{
val pathsep = if win32 {';'} else {':'}
Expand Down

0 comments on commit 857014a

Please sign in to comment.