fix trouble with debug/release detection on Windows dynamic_link
[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     debugVal = getenv("DEBUG_SCILAB_DYNAMIC_LINK","");
189     val = debugVal;
190     if val <> "NO" & val <> "YES" then
191         if isDebug() then
192             val = "YES";
193         else
194             val = "NO";
195         end
196     end
197
198     if val == "YES" then
199         setenv("DEBUG_SCILAB_DYNAMIC_LINK","YES");
200         CFLAGS = CFLAGS + " -D_DEBUG";
201     else
202         setenv("DEBUG_SCILAB_DYNAMIC_LINK","NO");
203         CFLAGS = CFLAGS + " -DNDEBUG";
204     end
205
206     // remove duplicated files
207     FILES_SRC_MATRIX = unique(FILES_SRC_MATRIX);
208
209     FILES_SRC = strcat(FILES_SRC_MATRIX," ");
210
211     OBJ_DEST_PATH = "";
212     if (getenv("DEBUG_SCILAB_DYNAMIC_LINK","") == "YES") then
213         OBJ_DEST_PATH = "Debug/";
214     else
215         OBJ_DEST_PATH = "Release/";
216     end
217
218     OBJS_MATRIX = [];
219     OBJS_WITH_PATH_MATRIX = [];
220
221     for y = 1:size(FILES_SRC_MATRIX,"*")
222         [path_f, file_f, ext_f] = fileparts(FILES_SRC_MATRIX(y));
223         OBJS_MATRIX = [OBJS_MATRIX, path_f + file_f + ".obj"];
224         OBJS_WITH_PATH_MATRIX = [OBJS_WITH_PATH_MATRIX, OBJ_DEST_PATH + path_f + file_f + ".obj"];
225     end
226
227     OBJS = strcat(OBJS_MATRIX, " ");
228     OBJS_WITH_PATH =  strcat(OBJS_WITH_PATH_MATRIX, " ");
229
230     if ( or(fileext(FILES_SRC_MATRIX) == ".f90") | or(fileext(FILES_SRC_MATRIX) == ".f") ) then
231         if findmsifortcompiler() <> "unknown" then
232             if (getenv("DEBUG_SCILAB_DYNAMIC_LINK","NO") == "NO") then
233                 FORTRAN_RUNTIME = "libifcoremd.lib libmmd.lib";
234             else
235                 FORTRAN_RUNTIME = "libifcoremdd.lib libmmdd.lib";
236             end
237         end
238     end
239
240     for x=libs(:)'
241         if (x <> [] & x <> "") then
242             if OTHERLIBS <> "" then
243                 OTHERLIBS = OTHERLIBS + " " + x + ".lib";
244             else
245                 OTHERLIBS = x + ".lib";
246             end
247         end
248     end
249
250     if isfile(SCI+"/modules/dynamic_link/src/scripts/TEMPLATE_MAKEFILE.VC") then
251         MAKEFILE_VC = mgetl(SCI+"/modules/dynamic_link/src/scripts/TEMPLATE_MAKEFILE.VC");
252     else
253         MAKEFILE_VC = "";
254     end
255
256     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__SCI__", SCIDIR);
257     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__SCILAB_INCLUDES__", SCILAB_INCLUDES);
258     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__SCILAB_LIBS__",SCILAB_LIBS);
259     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__LIBNAME__", LIBRARY);
260     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__FILES_SRC__", FILES_SRC);
261     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__OBJS__", OBJS);
262     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__OBJS_WITH_PATH__", OBJS_WITH_PATH);
263     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__FORTRAN_RUNTIME__", FORTRAN_RUNTIME);
264     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__OTHERSLIBS__", OTHERLIBS);
265
266     if CC <> "" then
267         MAKEFILE_VC = strsubst(MAKEFILE_VC, "__CC__",CC);
268     else
269         MAKEFILE_VC = strsubst(MAKEFILE_VC, "CC = __CC__","#CC = ");
270     end
271
272     CFLAGS = CFLAGS + " -D" + convstr(name, "u") + "_GW_EXPORTS";
273     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__CFLAGS__", CFLAGS);
274     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__MEXCFLAGS__", MEXCFLAGS);
275     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__FFLAGS__", FFLAGS);
276     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__MEXFFLAGS__", MEXFFLAGS);
277     MAKEFILE_VC = strsubst(MAKEFILE_VC, "__LDFLAGS__", LDFLAGS);
278
279     if ( MAKEFILE_VC <> "") then
280         mputl(MAKEFILE_VC, Makename);
281
282         if ilib_verbose() > 1 then
283             disp(MAKEFILE_VC);
284         end
285
286     else
287         // TEMPLATE_MAKEFILE.VC not found
288         warning(SCI+"/modules/dynamic_link/src/scripts/TEMPLATE_MAKEFILE.VC"+ _("not found.") );
289     end
290
291     //restore DEBUG_SCILAB_DYNAMIC_LINK
292     setenv("DEBUG_SCILAB_DYNAMIC_LINK", debugVal);
293
294 endfunction
295 //=============================================================================