aboutsummaryrefslogtreecommitdiffstats
path: root/lib/common_test/test_server/ts_autoconf_win32.erl
blob: 52e5ac8e695001c04c873d780cbd884a0ad1c717 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1997-2016. 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.
%% 
%% %CopyrightEnd%
%%

%%% Purpose : Autoconf for Windows.

-module(ts_autoconf_win32).
-export([configure/0]).

-include("ts.hrl").

configure() ->
    case variables() of
	{ok, Vars} ->
	    ts_lib:subst_file("conf_vars.in", "conf_vars", Vars);
	Error ->
	    Error
    end.

variables() ->
    run_tests(tests(), []).

run_tests([{Prompt, Tester}|Rest], Vars) ->
    io:format("checking ~s... ", [Prompt]),
    case catch Tester(Vars) of
	{'EXIT', Reason} ->
	    io:format("FAILED~nExit status: ~p~n", [Reason]),
	    {error, auto_conf_failed};
	{Result, NewVars} ->
	    io:format("~s~n", [lists:concat([Result])]),
	    run_tests(Rest, NewVars)
    end;
run_tests([], Vars) ->
    {ok, Vars}.

%%% The tests.

tests() ->
    [{"host system type", fun system_type/1},
     {"CPU type", fun cpu/1},
     {"for C compiler", fun c_compiler/1},
     {"for make program", fun make/1},
     {"for location of SSL libraries", fun ssl/1},
     {"for location of Java compiler", fun javac/1}].

system_type(Vars) ->
    Os = case os:type() of
	     {win32, nt} ->
		 case os:version() of
		     {4,_,_} -> "Windows NT";
		     {5,0,_} -> "Windows 2000";
		     {5,1,_} -> "Windows XP";
		     {5,2,_} -> "Windows 2003";
		     {6,0,_} -> "Windows Vista";
		     {6,1,_} -> "Windows 7";
		     {_,_,_} -> "Windows NCC-1701-D"
		 end;
	     {win32, windows} -> 
		 case os:version() of
		     {4,0,_} ->  "Windows 95";
		     {4,10,_} -> "Windows 98"
		 end;
	     {win32, _} -> "Windows"
	 end,
    {Os, [{host_os, Os}, {host, "win32"}|Vars]}.

cpu(Vars) ->
    Arch = os:getenv("PROCESSOR_ARCHITECTURE"),
    Level0 = os:getenv("PROCESSOR_Level"),
    Cpu = case {Arch, Level0} of
	      {"x86", Level} when is_list(Level) ->
		  "i" ++ Level ++ "86";
	      {Other, _Level} when is_list(Other) ->
		  Other;
	      {false, _} ->
		  "i386"
	  end,
    {Cpu, [{host_cpu, Cpu}|Vars]}.

c_compiler(Vars) ->
    try
	CompTests = [{msc, fun visual_cxx/1},
		     {gnuc, fun mingw32/1}],
	%% First try to find the same compiler that the system
	%% was built with...
	UsedCompiler = case erlang:system_info(c_compiler_used) of
			   {UsedCmplr, _} ->
			       case lists:keysearch(UsedCmplr, 1, CompTests) of
				   {value, {UsedCmplr, CompTest}} ->
				       CompTest(Vars);
				   _ -> 
				       ok
			       end,
			       UsedCmplr;
			   undefined ->
			       undefined
		       end,
	%% ... then try to find a compiler...
	lists:foreach(fun ({Cmplr, _CmplrTst}) when Cmplr =:= UsedCompiler ->
			      ok; % Have already checked for this one
			  ({_Cmplr, CmplrTst}) ->
			      CmplrTst(Vars)
		      end,
		      CompTests),
	{no, Vars}
    catch
	throw:{_Path, _NewVars} = Res -> Res
    end.

visual_cxx(Vars) ->
    case os:find_executable("cl") of
	false ->
	    {no, Vars};
	Path when is_list(Path) ->
	    {DEFAULT_THR_LIB,
	     ERTS_THR_LIB,
	     DLL,
	     DBG_LINK,
	     DBG_COMP,
	     OPT} =
		case is_debug_build() of
		    true ->
			{"-MTd ",
			 "-MDd ",
			 "-LDd ",
			 "-link -debug -pdb:none ",
			 "-Z7 -DDEBUG",
			 " "};
		    false ->
			{"-MT ",
			 "-MD ",
			 "-LD ",
			 "-Zi -link ",
			 "-Zi ",
			 "-Ox "}
		end,
	    WIN32 = "-D__WIN32__ ",
	    ERTS_CFLAGS = ERTS_THR_LIB ++ WIN32 ++ OPT ++ DBG_COMP,
	    LIBS = "ws2_32.lib",
	    CC = "cl -nologo",
	    throw({Path, [{'CC', CC},
			  {'LD', CC},
			  {'SHLIB_LD', CC},
			  {'SHLIB_LDFLAGS', ERTS_THR_LIB ++ DLL},
			  {'SHLIB_LDLIBS', DBG_LINK ++ "kernel32.lib"},
			  {'SHLIB_EXTRACT_ALL', ""},
			  {'CFLAGS', DEFAULT_THR_LIB ++ WIN32 ++ DBG_COMP},
			  {'EI_CFLAGS', DEFAULT_THR_LIB ++ WIN32 ++ DBG_COMP},
			  {'ERTS_CFLAGS', ERTS_CFLAGS},
			  {'SHLIB_CFLAGS', ERTS_CFLAGS++DLL},
			  {'CROSSLDFLAGS', ""},
			  {'DEFS', common_c_defs()},
			  {'SHLIB_SUFFIX', ".dll"},
			  {'ERTS_LIBS', ERTS_THR_LIB ++ LIBS},
			  {'LIBS', DEFAULT_THR_LIB ++ DBG_LINK ++ LIBS},
			  {obj,".obj"},
			  {exe, ".exe"},
			  {test_c_compiler, "{msc, undefined}"}
			  | Vars]})
    end.
	
mingw32(Vars) ->
    Gcc = "mingw32-gcc",
    case os:find_executable(Gcc) of
	false ->
	    {no, Vars};
	Path when is_list(Path) ->
	    {DBG_COMP,
	     OPT} =
		case is_debug_build() of
		    true ->
			{"-DDEBUG",
			 " "};
		    false ->
			{" ",
			 "-O2 "}
		end,
	    WIN32 = "-D__WIN32__ ",
	    ERTS_CFLAGS = WIN32 ++ "-g " ++ OPT ++ DBG_COMP,
	    LIBS = "-lws2_32",
	    CC = Gcc,
	    throw({Path, [{'CC', CC},
			  {'LD', CC},
			  {'SHLIB_LD', CC},
			  {'SHLIB_LDFLAGS', "-shared "},
			  {'SHLIB_LDLIBS', " -lkernel32"},
			  {'SHLIB_EXTRACT_ALL', ""},
			  {'CFLAGS', WIN32 ++ DBG_COMP},
			  {'EI_CFLAGS', WIN32 ++ DBG_COMP},
			  {'ERTS_CFLAGS', ERTS_CFLAGS},
			  {'SHLIB_CFLAGS', ERTS_CFLAGS},
			  {'CROSSLDFLAGS', ""},
			  {'DEFS', common_c_defs()},
			  {'SHLIB_SUFFIX', ".dll"},
			  {'ERTS_LIBS', LIBS},
			  {'LIBS', LIBS},
			  {obj,".o"},
			  {exe, ".exe"},
			  {test_c_compiler, "{gnuc, undefined}"}
			  | Vars]})
    end.

common_c_defs() ->
    "-DHAVE_STRERROR=1".

make(Vars) ->
    try
	find_make("nmake -nologo", Vars),
	find_make("mingw32-make", Vars)
    catch
	throw:{_Path, _NewVars} = Res -> Res
    end.

find_make(MakeCmd, Vars) ->
    [Make|_] = string:tokens(MakeCmd, " \t"),
    case os:find_executable(Make) of
	false ->
	    {no, Vars};
	Path when is_list(Path) ->
	    throw({Path, [{make_command, MakeCmd} | Vars]})
    end.

ssl(Vars) ->
    {"win32",[{'SSLEAY_ROOT',"win32"}|Vars]}.

javac(Vars) ->
    case os:find_executable("javac") of
	false ->
	    {no, Vars};
	Path when is_list(Path) ->
	    {Path, [{'JAVAC', "javac"} | Vars]}
    end.

is_debug_build() ->
    case catch string:str(erlang:system_info(system_version), "debug") of
	Int when is_integer(Int), Int > 0 ->
	    true;
	_ ->
	    false
    end.