From c54636b6c2606933bc48b7e3881d7e0927a33f6d Mon Sep 17 00:00:00 2001 From: Zaiming Shi Date: Fri, 26 Feb 2021 21:43:58 +0100 Subject: [PATCH 1/2] chore(build): inject emqx_vsn to all modules as attribute --- rebar.config | 3 ++- rebar.config.erl | 7 ++++--- src/emqx_mod_vsn.erl | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 src/emqx_mod_vsn.erl diff --git a/rebar.config b/rebar.config index e35ac8426..c37fa8070 100644 --- a/rebar.config +++ b/rebar.config @@ -11,7 +11,8 @@ {erl_opts, [warn_unused_vars,warn_shadow_vars,warn_unused_import, warn_obsolete_guard,compressed]}. -{overrides,[{add,[{extra_src_dirs, [{"etc", [{recursive,true}]}]}]} +{overrides,[{add,[ {extra_src_dirs, [{"etc", [{recursive,true}]}]} + , {erl_opts, [{parse_transform, emqx_mod_vsn}]}]} ]}. {extra_src_dirs, [{"etc", [{recursive,true}]}]}. diff --git a/rebar.config.erl b/rebar.config.erl index d8f0d74ec..2145fc212 100644 --- a/rebar.config.erl +++ b/rebar.config.erl @@ -52,7 +52,7 @@ test_deps() -> ]. default_compile_opts() -> - [compressed, deterministic, no_debug_info, warnings_as_errors, {parse_transform, mod_vsn}]. + [compressed, deterministic, no_debug_info, warnings_as_errors, {parse_transform, emqx_mod_vsn}]. profiles() -> [ {'emqx', [ {erl_opts, default_compile_opts()} @@ -67,11 +67,11 @@ profiles() -> , {'emqx-edge-pkg', [ {erl_opts, default_compile_opts()} , {relx, relx('emqx-edge-pkg')} ]} - , {check, [ {erl_opts, [debug_info, warnings_as_errors, {parse_transform, mod_vsn}]} + , {check, [ {erl_opts, [debug_info, warnings_as_errors, {parse_transform, emqx_mod_vsn}]} ]} , {test, [ {deps, test_deps()} , {plugins, test_plugins()} - , {erl_opts, [debug_info, {parse_transform, mod_vsn}] ++ erl_opts_i()} + , {erl_opts, [debug_info, {parse_transform, emqx_mod_vsn}] ++ erl_opts_i()} , {extra_src_dirs, [{"test", [{recursive,true}]}]} ]} ]. @@ -281,6 +281,7 @@ provide_bcrypt_release(ReleaseType) -> compile_and_load_pase_transforms(Dir) -> PtFiles = [ "apps/emqx_rule_engine/src/emqx_rule_actions_trans.erl" + , "src/emqx_mod_vsn.erl" ], CompileOpts = [verbose,report_errors,report_warnings,return_errors,debug_info], lists:foreach(fun(PtFile) -> {ok, _Mod} = compile:file(path(Dir, PtFile), CompileOpts) end, PtFiles). diff --git a/src/emqx_mod_vsn.erl b/src/emqx_mod_vsn.erl new file mode 100644 index 000000000..861398569 --- /dev/null +++ b/src/emqx_mod_vsn.erl @@ -0,0 +1,40 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2021 EMQ Technologies Co., Ltd. All Rights Reserved. +%% +%% Licensed under the Apache License, Version 2.0 (the "License"); +%% you may not use this file except in compliance with the License. +%% You may obtain a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, software +%% distributed under the License is distributed on an "AS IS" BASIS, +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%% See the License for the specific language governing permissions and +%% limitations under the License. +%%-------------------------------------------------------------------- + +%% This module provides a parse_transform to inject emqx version number +%% to all modules as a module attribute. +%% The module attribute is so far only used for beam reload inspection. +-module(emqx_mod_vsn). + +-export([parse_transform/2]). + +parse_transform(Form, _Opts) -> + case os:getenv("PKG_VSN") of + false -> Form; + Vsn -> trans(Form, {attribute, 1, emqx_vsn, Vsn}) + end. + +trans(Form, Injection) -> + trans(Form, Injection, []). + +trans([], _Injection, Acc) -> + lists:reverse(Acc); +trans([{eof, _} | _] = EOF, _Injection, Acc) -> + lists:reverse(Acc) ++ EOF; +trans([{attribute, _, module, _} = Module | Form], Injection, Acc) -> + lists:reverse(Acc) ++ [Module, Injection | Form]; +trans([H | T], Injection, Acc) -> + trans(T, Injection, [H | Acc]). From ddadece0b2316679f92bbc38c6b1549abde28e66 Mon Sep 17 00:00:00 2001 From: Zaiming Shi Date: Sun, 28 Feb 2021 12:12:28 +0100 Subject: [PATCH 2/2] chore(build): include emqx version as compile info --- rebar.config | 4 ---- rebar.config.erl | 41 +++++++++++++++++++++++++++++++---------- src/emqx_mod_vsn.erl | 40 ---------------------------------------- 3 files changed, 31 insertions(+), 54 deletions(-) delete mode 100644 src/emqx_mod_vsn.erl diff --git a/rebar.config b/rebar.config index c37fa8070..c692b185e 100644 --- a/rebar.config +++ b/rebar.config @@ -6,14 +6,10 @@ %% with rebar.config.erl module. Final result is written to %% rebar.config.rendered if environment DEBUG is set. -{minimum_otp_vsn, "21.3"}. {edoc_opts, [{preprocess,true}]}. {erl_opts, [warn_unused_vars,warn_shadow_vars,warn_unused_import, warn_obsolete_guard,compressed]}. -{overrides,[{add,[ {extra_src_dirs, [{"etc", [{recursive,true}]}]} - , {erl_opts, [{parse_transform, emqx_mod_vsn}]}]} - ]}. {extra_src_dirs, [{"etc", [{recursive,true}]}]}. {xref_checks,[undefined_function_calls,undefined_functions,locals_not_used, diff --git a/rebar.config.erl b/rebar.config.erl index 2145fc212..d216e8464 100644 --- a/rebar.config.erl +++ b/rebar.config.erl @@ -6,7 +6,7 @@ do(Dir, CONFIG) -> ok = compile_and_load_pase_transforms(Dir), C1 = deps(CONFIG), Config = dialyzer(C1), - dump(Config ++ coveralls() ++ config()). + dump(Config ++ [{overrides, overrides()}] ++ coveralls() ++ config()). bcrypt() -> {bcrypt, {git, "https://github.com/emqx/erlang-bcrypt.git", {branch, "0.6.0"}}}. @@ -19,6 +19,14 @@ deps(Config) -> end, lists:keystore(deps, 1, Config, {deps, OldDpes ++ MoreDeps}). +overrides() -> + [ {add, [ {extra_src_dirs, [{"etc", [{recursive,true}]}]} + , {erl_opts, [ deterministic + , {compile_info, [{emqx_vsn, get_vsn()}]} + ]} + ]} + ]. + config() -> [ {plugins, plugins()} , {profiles, profiles()} @@ -51,27 +59,41 @@ test_deps() -> , meck ]. -default_compile_opts() -> - [compressed, deterministic, no_debug_info, warnings_as_errors, {parse_transform, emqx_mod_vsn}]. +common_compile_opts() -> + [ deterministic + , {compile_info, [{emqx_vsn, get_vsn()}]} + ]. + +prod_compile_opts() -> + [ compressed + , no_debug_info + , warnings_as_errors + | common_compile_opts() + ]. + +test_compile_opts() -> + [ debug_info + | common_compile_opts() + ]. profiles() -> - [ {'emqx', [ {erl_opts, default_compile_opts()} + [ {'emqx', [ {erl_opts, prod_compile_opts()} , {relx, relx('emqx')} ]} - , {'emqx-pkg', [ {erl_opts, default_compile_opts()} + , {'emqx-pkg', [ {erl_opts, prod_compile_opts()} , {relx, relx('emqx-pkg')} ]} - , {'emqx-edge', [ {erl_opts, default_compile_opts()} + , {'emqx-edge', [ {erl_opts, prod_compile_opts()} , {relx, relx('emqx-edge')} ]} - , {'emqx-edge-pkg', [ {erl_opts, default_compile_opts()} + , {'emqx-edge-pkg', [ {erl_opts, prod_compile_opts()} , {relx, relx('emqx-edge-pkg')} ]} - , {check, [ {erl_opts, [debug_info, warnings_as_errors, {parse_transform, emqx_mod_vsn}]} + , {check, [ {erl_opts, test_compile_opts()} ]} , {test, [ {deps, test_deps()} , {plugins, test_plugins()} - , {erl_opts, [debug_info, {parse_transform, emqx_mod_vsn}] ++ erl_opts_i()} + , {erl_opts, test_compile_opts() ++ erl_opts_i()} , {extra_src_dirs, [{"test", [{recursive,true}]}]} ]} ]. @@ -281,7 +303,6 @@ provide_bcrypt_release(ReleaseType) -> compile_and_load_pase_transforms(Dir) -> PtFiles = [ "apps/emqx_rule_engine/src/emqx_rule_actions_trans.erl" - , "src/emqx_mod_vsn.erl" ], CompileOpts = [verbose,report_errors,report_warnings,return_errors,debug_info], lists:foreach(fun(PtFile) -> {ok, _Mod} = compile:file(path(Dir, PtFile), CompileOpts) end, PtFiles). diff --git a/src/emqx_mod_vsn.erl b/src/emqx_mod_vsn.erl deleted file mode 100644 index 861398569..000000000 --- a/src/emqx_mod_vsn.erl +++ /dev/null @@ -1,40 +0,0 @@ -%%-------------------------------------------------------------------- -%% Copyright (c) 2021 EMQ Technologies Co., Ltd. All Rights Reserved. -%% -%% Licensed under the Apache License, Version 2.0 (the "License"); -%% you may not use this file except in compliance with the License. -%% You may obtain a copy of the License at -%% -%% http://www.apache.org/licenses/LICENSE-2.0 -%% -%% Unless required by applicable law or agreed to in writing, software -%% distributed under the License is distributed on an "AS IS" BASIS, -%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -%% See the License for the specific language governing permissions and -%% limitations under the License. -%%-------------------------------------------------------------------- - -%% This module provides a parse_transform to inject emqx version number -%% to all modules as a module attribute. -%% The module attribute is so far only used for beam reload inspection. --module(emqx_mod_vsn). - --export([parse_transform/2]). - -parse_transform(Form, _Opts) -> - case os:getenv("PKG_VSN") of - false -> Form; - Vsn -> trans(Form, {attribute, 1, emqx_vsn, Vsn}) - end. - -trans(Form, Injection) -> - trans(Form, Injection, []). - -trans([], _Injection, Acc) -> - lists:reverse(Acc); -trans([{eof, _} | _] = EOF, _Injection, Acc) -> - lists:reverse(Acc) ++ EOF; -trans([{attribute, _, module, _} = Module | Form], Injection, Acc) -> - lists:reverse(Acc) ++ [Module, Injection | Form]; -trans([H | T], Injection, Acc) -> - trans(T, Injection, [H | Acc]).