add ilib_language to retrieve compiler capabilities
[scilab.git] / scilab / modules / dynamic_link / macros / windows / dlwGenerateMakefile.sci
1 // Copyright (C) DIGITEO - 2010-2011 - Allan CORNET
2 //
3 // Copyright (C) 2012 - 2016 - Scilab Enterprises
4 //
5 // This file is hereby licensed under the terms of the GNU GPL v2.0,
6 // pursuant to article 5.3.4 of the CeCILL v.2.1.
7 // This file was originally licensed under the terms of the CeCILL v2.1,
8 // and continues to be available under such terms.
9 // For more information, see the COPYING file which you should have received
10 // along with this program.
11 //=============================================================================
12 function Makename = dlwGenerateMakefile(name, ..
13     tables, ..
14     files, ..
15     libs, ..
16     libname, ..
17     with_gateway, ..
18     ldflags, ..
19     cflags, ..
20     fflags, ..
21     cc)
22
23     [lhs,rhs] = argn(0);
24     if rhs < 4 then
25         error(msprintf(gettext("%s: Wrong number of input argument(s): at least %d expected.\n"), "dlwGenerateMakefile", 4));
26     end
27
28     if argn(2) < 6 then
29         with_gateway = %t;
30         ldflags = "";
31         cflags = "";
32         fflags = "";
33         cc = "";
34     end
35
36     if ~isdef("makename") then
37         makename = "";
38     end
39
40     // change table if necessary
41     if tables <> [] then
42         if typeof(tables)<>"list" then
43             tables = list(tables)
44         end
45         L = length(tables);
46
47         for it = 1:L
48             table = tables(it);
49             [mt, nt] = size(table);
50
51             if nt == 2 then
52                 col= "csci";
53                 table = [table, col(ones(mt,1))];
54                 nt=3;
55             end
56
57             if nt<>3 then
58                 error(msprintf(gettext("%s: Wrong size for input argument #%d.\n") ,"dlwGenerateMakefile", 2));
59             end
60             tables(it) = table;
61         end
62     end
63
64     if isempty(makename) then
65         Makename = dlwGetDefltMakefileName() + dlwGetMakefileExt();
66     else
67         Makename = makename + dlwGetMakefileExt();
68     end
69
70     if length(libname) > 0  & strncpy(libname, 3) <> "lib" then
71         libname = "lib" + libname;
72     end
73
74     if getenv("__USE_DEPRECATED_STACK_FUNCTIONS__", "NO") == "YES" then
75         cflags = cflags + " -D__USE_DEPRECATED_STACK_FUNCTIONS__";
76     end
77
78     ilib_gen_Make_win32(name, tables, files, libs, libname, Makename, with_gateway, ldflags, cflags, fflags)
79
80 endfunction
81 //=============================================================================
82 function ilib_gen_Make_win32(name, ..
83     tables, ..
84     files, ..
85     libs, ..
86     libname, ..
87     Makename, ..
88     with_gateway, ..
89     ldflags, ..
90     cflags, ..
91     fflags)
92
93     managed_ext = [".cxx", ".cpp", ".c", ".f90", ".f"];
94
95     SCIDIR = SCI;
96     LIBRARY = name;
97     FILES_SRC = "";
98     OBJS = "";
99     OBJS_WITH_PATH = "";
100     FORTRAN_RUNTIME = "";
101     OTHERLIBS = "";
102     CC = "";
103     CFLAGS = cflags;
104     MEXCFLAGS = "";
105     FFLAGS = fflags;
106     MEXFFLAGS = "";
107     LDFLAGS = ldflags;
108
109     SCILAB_INCLUDES = dlwGetScilabIncludes();
110     SCILAB_INCLUDES = "-I""" + SCILAB_INCLUDES + """";
111     SCILAB_INCLUDES = [SCILAB_INCLUDES(1:$-1) + " \"; SCILAB_INCLUDES($)];
112     SCILAB_INCLUDES = strcat(SCILAB_INCLUDES, ascii(10));
113
114     SCILAB_LIBS = dlwGetScilabLibraries();
115     SCILAB_LIBS = """$(SCIDIR)/bin/" + SCILAB_LIBS + """";
116     SCILAB_LIBS = [SCILAB_LIBS(1:$-1) + " \"; SCILAB_LIBS($)];
117     SCILAB_LIBS = strcat(SCILAB_LIBS, ascii(10));
118
119     if isempty(libname) then
120         LIBRARY = name;
121     else
122         LIBRARY = libname;
123     end
124
125     FILES_SRC_MATRIX = [];
126
127     [path_Make, file_Make, ext_Make] = fileparts(Makename);
128
129     for i=1:size(files,"*")
130         [path_f, file_f, ext_f] = fileparts(files(i));
131         FILENAME = [];
132         FILE_FOUNDED = %f;
133         for y = managed_ext(:)'
134             if (FILE_FOUNDED == %f) then
135                 if (fileinfo(path_f + file_f + y) <> []) | (fileinfo(path_Make + file_f + y) <> []) then
136                     FILENAME = path_f + file_f + y;
137                     FILE_FOUNDED = %t;
138                 end
139             end
140         end
141         FILES_SRC_MATRIX = [FILES_SRC_MATRIX , FILENAME];
142     end
143
144     if typeof(tables) <> "list" then
145         tables = list(tables);
146     end
147     L = length(tables);
148
149     if with_gateway then
150         if L == 1 then
151             FILES_SRC_MATRIX = [FILES_SRC_MATRIX , name + ".cpp"];
152         else
153             for i=1:L
154                 FILES_SRC_MATRIX = [FILES_SRC_MATRIX , name + string(i) + ".cpp"];
155             end
156         end
157     end
158
159     for it=1:L
160         table = tables(it);
161         [mt,nt] = size(table);
162
163         for i=1:mt
164             if table(i,3)=="cmex" | table(i,3)=="fmex" | table(i,3)=="Fmex" then
165                 MEXCFLAGS = "-Dmexfunction_=mex" + table(i,2) + "_ -DmexFunction=mex_" + table(i,2);
166                 MEXFFLAGS = "-Dmexfunction=mex" + table(i,2);
167                 if table(i,3)=="cmex" then
168                     if find(listfiles() == table(i,2)+".cpp") then
169                         filenameMex = table(i,2) + ".cpp";
170                     else
171                         filenameMex = table(i,2) + ".c";
172                     end
173                 else
174                     filenameMex = table(i,2) + ".f";
175                 end
176                 if grep(FILES_SRC_MATRIX,filenameMex) == [] then
177                     FILES_SRC_MATRIX = [FILES_SRC_MATRIX , filenameMex];
178                 end
179             end
180         end
181     end
182
183     if isempty(FILES_SRC_MATRIX) | ~and(isfile(FILES_SRC_MATRIX)) then
184         error(999, msprintf(_("%s: Wrong value for input argument #%d: existing file(s) expected.\n"), "ilib_gen_Make", 3));
185     end
186
187     //update DEBUG_SCILAB_DYNAMIC_LINK to map with Scilab compilation mode
188     val = getenv("DEBUG_SCILAB_DYNAMIC_LINK","");
189     if val <> "YES" & val <> "NO" & isDebug() then
190         setenv("DEBUG_SCILAB_DYNAMIC_LINK","YES");
191         CFLAGS = CFLAGS + " -D_DEBUG";
192     else
193         setenv("DEBUG_SCILAB_DYNAMIC_LINK","");
194         CFLAGS = CFLAGS + " -DNDEBUG";
195     end
196
197     // remove duplicated files
198     FILES_SRC_MATRIX = unique(FILES_SRC_MATRIX);
199
200     FILES_SRC = strcat(FILES_SRC_MATRIX," ");
201
202     OBJ_DEST_PATH = "";
203     if (getenv("DEBUG_SCILAB_DYNAMIC_LINK","") == "YES") then
204         OBJ_DEST_PATH = "Debug/";
205     else
206         OBJ_DEST_PATH = "Release/";
207     end
208
209     OBJS_MATRIX = [];
210     OBJS_WITH_PATH_MATRIX = [];
211
212     for y = 1:size(FILES_SRC_MATRIX,"*")
213         [path_f, file_f, ext_f] = fileparts(FILES_SRC_MATRIX(y));
214         OBJS_MATRIX = [OBJS_MATRIX, path_f + file_f + ".obj"];
215         OBJS_WITH_PATH_MATRIX = [OBJS_WITH_PATH_MATRIX, OBJ_DEST_PATH + path_f + file_f + ".obj"];
216     end
217
218     OBJS = strcat(OBJS_MATRIX, " ");
219     OBJS_WITH_PATH =  strcat(OBJS_WITH_PATH_MATRIX, " ");
220
221     if ( or(fileext(FILES_SRC_MATRIX) == ".f90") | or(fileext(FILES_SRC_MATRIX) == ".f") ) then
222         if findmsifortcompiler() <> "unknown" then
223             if (getenv("DEBUG_SCILAB_DYNAMIC_LINK","NO") == "NO") then
224                 FORTRAN_RUNTIME = "libifcoremd.lib libmmd.lib";
225             else
226                 FORTRAN_RUNTIME = "libifcoremdd.lib libmmdd.lib";
227             end
228         end
229     end
230
231     for x=libs(:)'
232         if (x <> [] & x <> "") then
233             if OTHERLIBS <> "" then
234                 OTHERLIBS = OTHERLIBS + " " + x + ".lib";
235             else
236                 OTHERLIBS = x + ".lib";
237             end
238         end
239     end
240
241     if isfile(SCI+"/modules/dynamic_link/src/scripts/TEMPLATE_MAKEFILE.VC") then
242         MAKEFILE_VC = mgetl(SCI+"/modules/dynamic_link/src/scripts/TEMPLATE_MAKEFILE.VC");
243     else
244         MAKEFILE_VC = "";
245     end
246
247     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__SCI__", SCIDIR);
248     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__SCILAB_INCLUDES__", SCILAB_INCLUDES);
249     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__SCILAB_LIBS__",SCILAB_LIBS);
250     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__LIBNAME__", LIBRARY);
251     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__FILES_SRC__", FILES_SRC);
252     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__OBJS__", OBJS);
253     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__OBJS_WITH_PATH__", OBJS_WITH_PATH);
254     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__FORTRAN_RUNTIME__", FORTRAN_RUNTIME);
255     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__OTHERSLIBS__", OTHERLIBS);
256
257     if CC <> "" then
258         MAKEFILE_VC = strsubst(MAKEFILE_VC, "__CC__",CC);
259     else
260         MAKEFILE_VC = strsubst(MAKEFILE_VC, "CC = __CC__","#CC = ");
261     end
262
263     CFLAGS = CFLAGS + " -D" + convstr(name, "u") + "_GW_EXPORTS";
264     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__CFLAGS__", CFLAGS);
265     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__MEXCFLAGS__", MEXCFLAGS);
266     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__FFLAGS__", FFLAGS);
267     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__MEXFFLAGS__", MEXFFLAGS);
268     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__LDFLAGS__", LDFLAGS);
269
270     if ( MAKEFILE_VC <> "") then
271         mputl(MAKEFILE_VC, Makename);
272
273         if ilib_verbose() > 1 then
274             disp(MAKEFILE_VC);
275         end
276
277     else
278         // TEMPLATE_MAKEFILE.VC not found
279         warning(SCI+"/modules/dynamic_link/src/scripts/TEMPLATE_MAKEFILE.VC"+ _("not found.") );
280     end
281
282     //restore DEBUG_SCILAB_DYNAMIC_LINK
283     setenv("DEBUG_SCILAB_DYNAMIC_LINK", val);
284
285 endfunction
286 //=============================================================================