-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add handling of erlang version based compiler macros
The mechanism has been copied from LFE and is more general than necessary here but this makes it easier to maintain. We are even future rebar compliant.
- Loading branch information
Showing
3 changed files
with
111 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#! /usr/bin/env escript | ||
%% -*- mode: erlang; indent-tabs-mode: nil -*- | ||
%% Define a number of compiler options. We first work out the current | ||
%% Erlang version and from the we can define the various options. | ||
|
||
%% Bloody useful. | ||
-define(IF(Test,True,False), case Test of true -> True; false -> False end). | ||
|
||
%% Define the makefile variables HAS_MAPS and HAS_FULL_KEYS depending | ||
%% on whether this version of erlang has maps (17) and general map | ||
%% keys (18), or NEW_CORE_REC for new core definition of records (19). | ||
|
||
-define(HAS_MAPS_OPT, "-DHAS_MAPS=true"). | ||
-define(FULL_KEYS_OPT, "-DHAS_FULL_KEYS=true"). | ||
-define(NEW_REC_OPT, "-DNEW_REC_CORE=true"). | ||
-define(NEW_RAND_OPT, "-DNEW_RAND=true"). | ||
|
||
main(_) -> | ||
Version = otp_release(), | ||
CompOpts = comp_opts(Version), | ||
file:write_file("comp_opts.mk", "COMP_OPTS = " ++ CompOpts ++ "\n"). | ||
|
||
comp_opts(Version) -> | ||
Copts0 = "-DERLANG_VERSION=" ++ Version, | ||
Copts1 = ?IF(Version >= "17", Copts0 ++ " " ++ ?HAS_MAPS_OPT, Copts0), | ||
Copts2 = ?IF(Version >= "18", Copts1 ++ " " ++ ?FULL_KEYS_OPT, Copts1), | ||
Copts3 = ?IF(Version >= "19", | ||
Copts2 ++ append_copts([?NEW_REC_OPT,?NEW_RAND_OPT]), | ||
Copts2), | ||
Copts3. | ||
|
||
append_copts([Copt|Copts]) -> | ||
" " ++ Copt ++ append_copts(Copts); | ||
append_copts([]) -> []. | ||
|
||
%% Get the major release number. | ||
%% We have stolen the idea for this code from rebar3. | ||
|
||
otp_release() -> | ||
case erlang:system_info(otp_release) of | ||
[$R,N1,N2|_] when is_integer(N1), is_integer(N2) -> | ||
%% If OTP <= R16, take the digits. | ||
[N1,N2]; | ||
Rel -> | ||
%% If OTP >= 17.x, erlang:system_info(otp_release) returns just | ||
%% the major version number. | ||
Rel | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
%% -*- mode: erlang; indent-tabs-mode: nil -*- | ||
|
||
Conf0 = CONFIG, | ||
HasOpt = {d,'HAS_MAPS',true}, | ||
FullOpt = {d,'HAS_FULL_KEYS',true}, | ||
RecOpt = {d,'NEW_REC_CORE',true}, | ||
RandOpt = {d,'NEW_RAND',true}, | ||
|
||
Version = case erlang:system_info(otp_release) of | ||
[$R,N1,N2|_] when is_integer(N1), is_integer(N2) -> | ||
[N1,N2]; | ||
Rel -> Rel | ||
end, | ||
|
||
Copts0 = [{d,'ERLANG_VERSION',Version}], | ||
Copts1 = if Version >= "17" -> Copts0 ++ [HasOpt]; | ||
true -> Copts0 | ||
end, | ||
Copts2 = if Version >= "18" -> Copts1 ++ [FullOpt]; | ||
true -> Copts1 | ||
end, | ||
Copts3 = if Version >= "19" -> Copts2 ++ [RecOpt,RandOpt]; | ||
true -> Copts2 | ||
end, | ||
Copts = Copts3, %This is it | ||
|
||
case lists:keyfind(erl_opts, 1, Conf0) of | ||
{erl_opts,Opts} -> %Existing erl_opts | ||
NewOpts = {erl_opts,Opts ++ Copts}, | ||
lists:keyreplace(erl_opts, 1, Conf0, NewOpts); | ||
false -> %No erl_opts | ||
Conf0 ++ [{erl_opts,Copts}] | ||
end. |