Skip to content

Commit

Permalink
[merge] You can now set compiler flags in BUILD.plz files
Browse files Browse the repository at this point in the history
* build_compiler_opts:
  [build] Gracefully error when a module has conflicting compiler options
  [build] Add a compiler_opts field to program entrie
  [build] Fix indentation in ninja rules
  [build] Simplify error message creation for interpreting TOML files
  [build] Add a utility function for default results from TOML searches
  [build] Refactor the TOML search_result type
  [build] Refactor how modules are processed
  [build] rename the pc/pl flags ninja variables
  • Loading branch information
PaulBone committed Sep 2, 2024
2 parents 3cbd140 + 18e8908 commit 4fe0d7b
Show file tree
Hide file tree
Showing 12 changed files with 319 additions and 112 deletions.
294 changes: 182 additions & 112 deletions src/build.m

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/context.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
%
:- func context_string(string, context) = string.

%-----------------------------------------------------------------------%

:- func context_earliest(context, context) = context.

%-----------------------------------------------------------------------%
%-----------------------------------------------------------------------%

Expand Down Expand Up @@ -88,5 +92,9 @@
Pretty = format("%s:%d", [s(File), i(Line)])
).

%-----------------------------------------------------------------------%

context_earliest(C1, C2) = ( if compare((<), C1, C2) then C1 else C2 ).

%-----------------------------------------------------------------------%
%-----------------------------------------------------------------------%
15 changes: 15 additions & 0 deletions src/util.mercury.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
:- import_module getopt.
:- import_module io.
:- import_module list.
:- import_module map.
:- import_module maybe.
:- import_module pair.
:- import_module set.
Expand Down Expand Up @@ -111,6 +112,11 @@

:- func handle_bool_option(option_table(O), O, T, T) = T.

%-----------------------------------------------------------------------%

:- pred map_set_or_update(func(V) = V, K, V, map(K, V), map(K, V)).
:- mode map_set_or_update(in, in, in, in, out) is det.

%-----------------------------------------------------------------------%
%-----------------------------------------------------------------------%
:- implementation.
Expand Down Expand Up @@ -347,5 +353,14 @@
Result = False
).

%-----------------------------------------------------------------------%

map_set_or_update(UpdateFn, Key, Value, !Map) :-
( if search(!.Map, Key, OldValue) then
det_update(Key, UpdateFn(OldValue), !Map)
else
set(Key, Value, !Map)
).

%-----------------------------------------------------------------------%
%-----------------------------------------------------------------------%
45 changes: 45 additions & 0 deletions src/util.result.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
:- import_module io.
:- import_module cord.
:- import_module list.
:- import_module map.
:- import_module maybe.

:- import_module context.
Expand Down Expand Up @@ -97,6 +98,23 @@ func error_or_warning(E) = error_or_warning

:- func result_map((func(T) = U), result(T, E)) = result(U, E).

%-----------------------------------------------------------------------%

% foldl over a list except the accumulator includes a result that must
% be unpact before processing the next item. If mercury had monads this
% would be bind.
%
:- pred foldl_result(pred(X, A, result(A, E)), list(X),
A, result(A, E)).
:- mode foldl_result(pred(in, in, out) is det, in, in, out) is det.

% Set or update the value within a map at the given key. if the update
% function fails then return that error.
%
:- pred map_set_or_update_result(func(V) = result(V, E),
K, V, map(K, V), result(map(K, V), E)).
:- mode map_set_or_update_result(in, in, in, in, out) is det.

%-----------------------------------------------------------------------%

:- func errors_map((func(E1) = E2), errors(E1)) = errors(E2).
Expand Down Expand Up @@ -205,6 +223,33 @@ func error_or_warning(E) = error_or_warning

%-----------------------------------------------------------------------%

foldl_result(_, [], Acc, ok(Acc)).
foldl_result(Pred, [X | Xs], Acc0, MaybeAcc) :-
Pred(X, Acc0, MaybeAcc1),
( MaybeAcc1 = ok(Acc1),
foldl_result(Pred, Xs, Acc1, MaybeAcc)
; MaybeAcc1 = errors(Error),
MaybeAcc = errors(Error)
).

%-----------------------------------------------------------------------%

map_set_or_update_result(UpdateFn, Key, Value, !.Map, MaybeMap) :-
( if search(!.Map, Key, Old) then
MaybeNew = UpdateFn(Old),
( MaybeNew = ok(New),
det_update(Key, New, !Map),
MaybeMap = ok(!.Map)
; MaybeNew = errors(Error),
MaybeMap = errors(Error)
)
else
set(Key, Value, !Map),
MaybeMap = ok(!.Map)
).

%-----------------------------------------------------------------------%

errors_map(Func, Errors) = map(error_map(Func), Errors).

:- func error_map((func(E1) = E2), error(E1)) = error(E2).
Expand Down
8 changes: 8 additions & 0 deletions tests/build/options_compiler_01.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This is free and unencumbered software released into the public domain.
# See ../LICENSE.unlicense

[options_compiler_01]
type = program
modules = [ OptionsCompiler01 ]
compiler_opts = "--no-simplify"

1 change: 1 addition & 0 deletions tests/build/options_compiler_01.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
15 changes: 15 additions & 0 deletions tests/build/options_compiler_01.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* vim: ft=plasma
* This is free and unencumbered software released into the public domain.
* See ../LICENSE.unlicense
*/

module OptionsCompiler01

entrypoint
func hello() uses IO -> Int {
print!("Hello world\n")

return 0
}

10 changes: 10 additions & 0 deletions tests/build/options_compiler_02.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This is free and unencumbered software released into the public domain.
# See ../LICENSE.unlicense

# PLZTEST type compile_failure

[options_compiler_02]
type = program
modules = [ OptionsCompiler02 ]
compiler_opts = "--nonexistant-option"

1 change: 1 addition & 0 deletions tests/build/options_compiler_02.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error processing command line options: unrecognized option `--nonexistant-option'
15 changes: 15 additions & 0 deletions tests/build/options_compiler_02.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* vim: ft=plasma
* This is free and unencumbered software released into the public domain.
* See ../LICENSE.unlicense
*/

module OptionsCompiler02

entrypoint
func hello() uses IO -> Int {
print!("Hello world\n")

return 0
}

17 changes: 17 additions & 0 deletions tests/build/options_compiler_03.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This is free and unencumbered software released into the public domain.
# See ../LICENSE.unlicense

# PLZTEST type compile_failure

# This test asks the build system to build the same module with different
# settings, it should fail.

[options_compiler_03a]
type = program
modules = [ OptionsCompiler03a, OptionsCompiler03 ]
compiler_opts = "--no-simplify"

[options_compiler_03b]
type = program
modules = [ OptionsCompiler03b, OptionsCompiler03 ]

2 changes: 2 additions & 0 deletions tests/build/options_compiler_03.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
options_compiler_03.build:11: Flags set for the same module in different
programs do not match

0 comments on commit 4fe0d7b

Please sign in to comment.