1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2005-2017. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20%%
21
22-module(test_compile_options).
23
24-include_lib("common_test/include/ct.hrl").
25
26
27-export([wrong_path/1,comp/2,path/1,noobj/1,
28	 record_name_prefix/1,verbose/1,maps/1]).
29
30%% OTP-5689
31wrong_path(Config) ->
32    Pid=spawn(?MODULE,comp,[self(),Config]),
33    receive
34	_Err ->
35	    ok
36    after 10000 ->
37	    exit(Pid,failure),
38	    error
39    end.
40
41comp(Parent,Config) ->
42    DataDir = proplists:get_value(data_dir,Config),
43    OutDir = proplists:get_value(priv_dir,Config),
44    Err=asn1ct:compile(DataDir++"NoImport",[{i,OutDir},{i,filename:join([DataDir,"subdir"])},{outdir,OutDir}]),
45    Parent!Err.
46
47%% OTP-5701
48
49path(Config) ->
50    DataDir = proplists:get_value(data_dir,Config),
51    OutDir = proplists:get_value(priv_dir,Config),
52    {ok,CWD} = file:get_cwd(),
53    file:set_cwd(filename:join([DataDir,subdir])),
54
55    ok = asn1ct:compile("../MyMerge.set.asn",[{outdir,OutDir}]),
56
57    ok=outfiles_check(OutDir),
58    outfiles_remove(OutDir),
59
60    file:set_cwd(filename:join([DataDir,subdir,subsubdir])),
61    ok = asn1ct:compile('../../MyMerge.set.asn',[{i,'..'},{outdir,OutDir}]),
62
63    ok=outfiles_check(OutDir,outfiles2()),
64    file:set_cwd(CWD),
65    ok.
66
67noobj(Config) ->
68    DataDir = proplists:get_value(data_dir,Config),
69    OutDir = proplists:get_value(priv_dir,Config),
70
71    code:purge('P-Record'),
72    file:delete(filename:join([OutDir,'P-Record.erl'])),
73    file:delete(filename:join([OutDir,'P-Record.beam'])),
74    ok=asn1ct:compile(filename:join([DataDir,"P-Record"]),
75			    [noobj,{outdir,OutDir}]),
76    {ok,_} = file:read_file_info(filename:join([OutDir,
77						      "P-Record.erl"])),
78    {error,enoent} =
79	file:read_file_info(filename:join([OutDir,"P-Record.beam"])),
80    {ok,_} = c:c(filename:join([OutDir,'P-Record']),
81		       [{i,OutDir},{outdir,OutDir}]),
82    {file,_} = code:is_loaded('P-Record'),
83
84    code:purge('P-Record'),
85    code:delete('P-Record'),
86    code:purge('p_record'),
87    code:delete('p_record'),
88    file:delete(filename:join([OutDir,'P-Record.erl'])),
89    file:delete(filename:join([OutDir,'P-Record.beam'])),
90    file:delete(filename:join([OutDir,'p_record.erl'])),
91    file:delete(filename:join([OutDir,'p_record.beam'])),
92    ok = asn1ct:compile(filename:join([DataDir,"p_record.set.asn"]),
93			[asn1config,ber,noobj,{outdir,OutDir}]),
94    {error,enoent} =
95	file:read_file_info(filename:join([OutDir,"P-Record.beam"])),
96    {error,enoent} =
97	file:read_file_info(filename:join([OutDir,"P-Record.erl"])),
98    {error,enoent} =
99	file:read_file_info(filename:join([OutDir,"p_record.beam"])),
100    io:format("read_file_info: p_record.erl~n",[]),
101    {ok,_} =
102	file:read_file_info(filename:join([OutDir,"p_record.erl"])),
103    io:format("c:c: p_record.erl~n",[]),
104    {ok,_} = c:c(filename:join([OutDir,'p_record']),
105		       [{i,OutDir},{outdir,OutDir}]),
106    io:format("code:is_loaded: p_record.erl~n",[]),
107    {file,_} = code:is_loaded('p_record'),
108    io:format("file:delete: p_record.erl~n",[]),
109    file:delete(filename:join([OutDir,'p_record.erl'])),
110    file:delete(filename:join([OutDir,'p_record.beam'])).
111
112verbose(Config) when is_list(Config) ->
113    DataDir = proplists:get_value(data_dir,Config),
114    OutDir = proplists:get_value(priv_dir,Config),
115    Asn1File = filename:join([DataDir,"Comment.asn"]),
116
117    %% Test verbose compile
118    test_server:capture_start(),
119    ok = asn1ct:compile(Asn1File, [{i,DataDir},{outdir,OutDir},noobj,verbose]),
120    test_server:capture_stop(),
121    [Line0|_] = test_server:capture_get(),
122    true = lists:prefix("Erlang ASN.1 compiler", Line0),
123
124    %% Test non-verbose compile
125    test_server:capture_start(),
126    ok = asn1ct:compile(Asn1File, [{i,DataDir},{outdir,OutDir},noobj]),
127    test_server:capture_stop(),
128    [] = test_server:capture_get(),
129    ok.
130
131maps(Config) ->
132    DataDir = proplists:get_value(data_dir, Config),
133    OutDir = proplists:get_value(case_dir, Config),
134    InFile = filename:join(DataDir, "P-Record"),
135
136    do_maps(ber, InFile, OutDir),
137    do_maps(per, InFile, OutDir),
138    do_maps(uper, InFile, OutDir).
139
140do_maps(Erule, InFile, OutDir) ->
141    Opts = [Erule,maps,{outdir,OutDir}],
142    ok = asn1ct:compile(InFile, Opts),
143
144    %% Make sure that no .hrl files are generated.
145    [] = filelib:wildcard(filename:join(OutDir, "*.hrl")),
146
147    %% Remove all generated files.
148    All = filelib:wildcard(filename:join(OutDir, "*")),
149    _ = [file:delete(N) || N <- All],
150
151    ok.
152
153outfiles_check(OutDir) ->
154    outfiles_check(OutDir,outfiles1()).
155
156
157outfiles_check(_OutDir,[])->
158    ok;
159outfiles_check(OutDir,[H|T]) ->
160    io:format("File: ~p~n",[filename:join([OutDir,H])]),
161    {ok,_}=file:read_file_info(filename:join([OutDir,H])),
162    outfiles_check(OutDir,T).
163
164outfiles1() ->
165    ["MyMerge.asn1db","MyMerge.beam",
166     "MyMerge.erl","MyMerge.hrl"].
167outfiles2() ->
168    ["MyMerge.beam","MyMerge.asn1db","MyMerge.erl"].
169
170outfiles_remove(OutDir) ->
171    lists:foreach(fun(F)-> file:delete(filename:join([OutDir,F])) end,
172		  outfiles1()).
173
174record_name_prefix(Config) ->
175    DataDir = proplists:get_value(data_dir,Config),
176    OutDir = proplists:get_value(priv_dir,Config),
177    ok = b_SeqIn(DataDir,OutDir),
178    ok = a_SeqIn(DataDir,OutDir).
179
180b_SeqIn(DataDir,OutDir) ->
181    asn1ct:compile(filename:join([DataDir,'Seq']),
182		   [{record_name_prefix,"b_"},{outdir,OutDir}]),
183    io:format("FileName: ~p~nOutDir:~p~n",
184	      [filename:join([DataDir,'b_SeqIn']),OutDir]),
185    {ok,_} = compile:file(filename:join([DataDir,'b_SeqIn']),
186			  [{i,OutDir}]),
187    'b_SeqIn' = b_SeqIn:record_name(),
188    ok.
189
190a_SeqIn(DataDir,OutDir) ->
191    asn1ct:compile(filename:join([DataDir,'Seq']),
192		   [{record_name_prefix,"a_"},{outdir,OutDir}]),
193    {ok,_} = compile:file(filename:join([DataDir,'a_SeqIn']),
194			  [{i,OutDir}]),
195    'a_SeqIn' = a_SeqIn:record_name(),
196    ok.
197