Skip to content

Commit 6e022aa

Browse files
committed
Improve configuration file parsing and CC backend compiler detection
- Add support for empty config values, which now use default settings - Modify config file parsing to handle duplicate keys and strip prefixes - Update CC backend to dynamically detect compiler from OCaml configuration - Update example config file with clarified comments about configuration behavior
1 parent c94954d commit 6e022aa

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

arrayjit/lib/cc_backend.ml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,24 @@ let name = "cc"
1515
let optimization_level () =
1616
Int.of_string @@ Utils.get_global_arg ~default:"3" ~arg_name:"cc_backend_optimization_level"
1717

18-
let compiler_command () = Utils.get_global_arg ~default:"cc" ~arg_name:"cc_backend_compiler_command"
18+
let compiler_command =
19+
let default =
20+
lazy
21+
(let ic = Unix.open_process_in "ocamlc -config" in
22+
let rec find_compiler () =
23+
match In_channel.input_line ic with
24+
| None -> "cc" (* Default fallback *)
25+
| Some line ->
26+
if String.is_prefix line ~prefix:"c_compiler: " then
27+
String.drop_prefix line 12 (* Length of "c_compiler: " *)
28+
else find_compiler ()
29+
in
30+
let compiler = find_compiler () in
31+
ignore (Unix.close_process_in ic);
32+
compiler)
33+
in
34+
fun () ->
35+
Utils.get_global_arg ~default:(Lazy.force default) ~arg_name:"cc_backend_compiler_command"
1936

2037
module Tn = Tnode
2138

arrayjit/lib/utils.ml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ let filename_of_parts = function
115115

116116
let config_file_args =
117117
match read_cmdline_or_env_var "no_config_file" with
118-
| None | Some "false" ->
118+
| None | Some "false" -> (
119119
let read = Stdio.In_channel.read_lines in
120120
let fname, config_lines =
121121
let rev_dirs = List.rev @@ filename_parts @@ Stdlib.Sys.getcwd () in
@@ -141,13 +141,19 @@ let config_file_args =
141141
lowercase @@ strip ~drop:(fun c -> equal_char '-' c || equal_char ' ' c) key)
142142
in
143143
let key =
144-
if String.is_prefix key ~prefix:"ocannl" then String.drop_prefix key 6 else key
144+
if String.is_prefix key ~prefix:"ocannl" then
145+
String.drop_prefix key 6 |> String.strip ~drop:(equal_char '_')
146+
else key
145147
in
146-
Some (String.strip ~drop:(equal_char '_') key, v)
148+
if String.is_empty v then None else Some (key, v)
147149
| l ->
148150
failwith @@ "OCANNL: invalid syntax in the config file " ^ fname
149151
^ ", should have a single '=' on each non-empty line, found: " ^ String.concat l)
150-
|> Hashtbl.of_alist_exn (module String)
152+
|> Hashtbl.of_alist (module String)
153+
|> function
154+
| `Ok h -> h
155+
| `Duplicate_key key ->
156+
failwith @@ "OCANNL: duplicate key in config file " ^ fname ^ ": " ^ key)
151157
| Some _ ->
152158
Stdio.printf "\nWelcome to OCANNL! Configuration defaults file is disabled.\n%!";
153159
Hashtbl.create (module String)

ocannl_config.example

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
# and in ancestor directories. The source of the `log_level` config is always printed,
1212
# the sources of other configs are printed when the log level > 0.
1313
# The configuration values below are (one of) the defaults.
14+
#
15+
# Repeating fields are disallowed. If the value is an empty string, the default value
16+
# is used (as if the config was not present).
1417

1518
# Configurations that are stored as `Utils.settings`:
1619

@@ -59,8 +62,8 @@ cuda_printf_fifo_size=
5962
# The `-O` argument to the compiler executable for the `cc` backend.
6063
cc_backend_optimization_level=3
6164

62-
# The `cc` backend compiler executable name.
63-
cc_backend_compiler_command=cc
65+
# The `cc` backend compiler executable name. By default, `ocamlc -config` field `c_compiler` is used.
66+
cc_backend_compiler_command=
6467

6568
# The `-O` argument to `gcc` for the `gcc` backend (using gccjit).
6669
gccjit_backend_optimization_level=3

0 commit comments

Comments
 (0)