add function to get scilab version as string and revision
[scilab.git] / scilab / modules / core / src / c / getversion.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - DIGITEO - Allan CORNET
4 *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13 *
14 */
15 /*--------------------------------------------------------------------------*/
16 #include <string.h>
17 #include <stdio.h>
18 #include "getversion.h"
19 #include "version.h"
20 #include "configvariable_interface.h"
21 #include "scilabDefaults.h"
22 #include "getenvc.h"
23 #include "os_string.h"
24 #include "with_module.h"
25 #include "loadversion.h"
26 #include "freeArrayOfString.h"
27 #include "sci_malloc.h"
28 #include "../../../io/includes/getenvc.h"
29 #include "charEncoding.h"
30
31 /*--------------------------------------------------------------------------*/
32 /*wide char*/
33 #define TCLSCI_MODULE_NAME L"tclsci"
34 #define TCLTK_OPTION_STRING L"tk"
35 #define MODELICAC_OPTION_STRING L"modelicac"
36 #define X86_STRING L"x86"
37 #define X64_STRING L"x64"
38 #define ICC_STRING L"ICC"
39 #define VC_STRING L"VC++"
40 #define GCC_STRING L"GCC"
41 #define PGI_STRING L"PGI"
42 #define SUN_STRING L"SUN"
43 #define UNKNOW_STRING L"UKN"
44 #define RELEASE_STRING L"release"
45 #define DEBUG_STRING L"debug"
46 #define DEFAULT_VERSION_ARRAY_SIZE 4
47 #define SCILAB_STRING L"scilab"
48 /*--------------------------------------------------------------------------*/
49 int *getScilabVersion(int *sizeArrayReturned)
50 {
51     int *returnedArray = (int *)MALLOC(sizeof(int) * DEFAULT_VERSION_ARRAY_SIZE);
52     *sizeArrayReturned = 0;
53     if (returnedArray)
54     {
55         returnedArray[0] = (int)SCI_VERSION_MAJOR;
56         returnedArray[1] = (int)SCI_VERSION_MINOR;
57         returnedArray[2] = (int)SCI_VERSION_MAINTENANCE;
58         returnedArray[3] = (int)SCI_VERSION_TIMESTAMP;
59
60         *sizeArrayReturned = DEFAULT_VERSION_ARRAY_SIZE;
61     }
62     return returnedArray;
63 }
64 /*--------------------------------------------------------------------------*/
65 char* getScilabVersionAsString(void)
66 {
67     return os_strdup(SCI_VERSION);
68 }
69 /*--------------------------------------------------------------------------*/
70 wchar_t *getScilabVersionAsWideString(void)
71 {
72     return os_wcsdup(SCI_VERSION_WIDE_STRING);
73 }
74 /*--------------------------------------------------------------------------*/
75 int getScilabVersionMajor()
76 {
77     return (int)SCI_VERSION_MAJOR;
78 }
79 /*--------------------------------------------------------------------------*/
80 int getScilabVersionMinor()
81 {
82     return (int)SCI_VERSION_MINOR;
83 }
84 /*--------------------------------------------------------------------------*/
85 int getScilabVersionMaintenance()
86 {
87     return (int)SCI_VERSION_MAINTENANCE;
88 }
89 /*--------------------------------------------------------------------------*/
90 int getScilabVersionTimestamp()
91 {
92     return (int)SCI_VERSION_TIMESTAMP;
93 }
94 /*--------------------------------------------------------------------------*/
95 char* getScilabVersionRevision()
96 {
97     return os_strdup(SCI_VERSION_REVISION);
98 }
99 /*--------------------------------------------------------------------------*/
100 char* getScilabVersionNumberAsString()
101 {
102     char version[16] = {0};
103     sprintf(version, "%d.%d.%d", getScilabVersionMajor(), getScilabVersionMinor(), getScilabVersionMaintenance());
104     return os_strdup(version);
105 }
106 /*--------------------------------------------------------------------------*/
107 int* getModuleVersion(wchar_t* _pwstModule, int* sizeArrayReturned)
108 {
109     int *returnedArray = NULL;
110
111     if (_pwstModule)
112     {
113         if (wcscmp(_pwstModule, SCILAB_STRING) == 0)
114         {
115             returnedArray = getScilabVersion(sizeArrayReturned);
116             return returnedArray;
117         }
118     }
119
120     if (with_module(_pwstModule))
121     {
122 #define LineMax 1024
123         wchar_t versionstring[LineMax];
124         int version_module_major = 0;
125         int version_module_minor = 0;
126         int version_module_maintenance = 0;
127         int version_module_revision = 0;
128
129         if (getversionmodule(_pwstModule, &version_module_major, &version_module_minor,
130                              &version_module_maintenance, versionstring,
131                              &version_module_revision))
132         {
133             returnedArray = (int *)MALLOC(sizeof(int) * DEFAULT_VERSION_ARRAY_SIZE);
134             if (returnedArray)
135             {
136                 returnedArray[0] = version_module_major;
137                 returnedArray[1] = version_module_minor;
138                 returnedArray[2] = version_module_maintenance;
139                 returnedArray[3] = version_module_revision;
140                 *sizeArrayReturned = DEFAULT_VERSION_ARRAY_SIZE;
141             }
142             else
143             {
144                 *sizeArrayReturned = 0;
145             }
146         }
147     }
148     else
149     {
150         *sizeArrayReturned = 0;
151     }
152     return returnedArray;
153 }
154 /*--------------------------------------------------------------------------*/
155 wchar_t* getModuleVersionInfoAsString(wchar_t* _pwstModule)
156 {
157     wchar_t* infoString = NULL;
158
159     if (_pwstModule)
160     {
161         if (wcscmp(_pwstModule, SCILAB_STRING) == 0)
162         {
163             infoString = getScilabVersionAsWideString();
164             return infoString;
165         }
166     }
167
168     if (with_module(_pwstModule))
169     {
170 #define LineMax 1024
171         wchar_t versionstring[LineMax];
172         int version_module_major = 0;
173         int version_module_minor = 0;
174         int version_module_maintenance = 0;
175         int version_module_revision = 0;
176         if (getversionmodule(_pwstModule, &version_module_major, &version_module_minor,
177                              &version_module_maintenance, versionstring,
178                              &version_module_revision))
179         {
180             infoString = os_wcsdup(versionstring);
181         }
182     }
183     return infoString;
184 }
185 /*--------------------------------------------------------------------------*/
186 wchar_t** getScilabVersionOptions(int *sizeArrayReturned)
187 {
188     wchar_t** options = NULL;
189     int nbOptions = 2;
190     *sizeArrayReturned = 0;
191
192     options = (wchar_t**)MALLOC(sizeof(wchar_t*) * nbOptions);
193     if (options)
194     {
195         options[0] = getCompilerUsedToBuildScilab();
196         options[1] = getCompilerArchitecture();
197
198         if (with_tk())
199         {
200             options = (wchar_t**)REALLOC(options, sizeof(wchar_t*) * (nbOptions + 1));
201             if (options)
202             {
203                 options[nbOptions] = os_wcsdup(TCLTK_OPTION_STRING);
204                 nbOptions++;
205             }
206             else
207             {
208                 freeArrayOfWideString(options, nbOptions);
209                 return NULL;
210             }
211         }
212
213         if (with_modelica_compiler())
214         {
215             options = (wchar_t**)REALLOC(options, sizeof(wchar_t*) * (nbOptions + 1));
216             if (options)
217             {
218                 options[nbOptions] = os_wcsdup(MODELICAC_OPTION_STRING);
219                 nbOptions++;
220             }
221             else
222             {
223                 freeArrayOfWideString(options, nbOptions);
224                 return NULL;
225             }
226         }
227
228         options = (wchar_t**)REALLOC(options, sizeof(wchar_t*) * (nbOptions + 1));
229         if (options)
230         {
231             options[nbOptions] = getReleaseMode();
232             nbOptions++;
233         }
234         else
235         {
236             freeArrayOfWideString(options, nbOptions);
237             return NULL;
238         }
239
240         options = (wchar_t**)REALLOC(options, sizeof(wchar_t*) * (nbOptions + 1));
241         if (options)
242         {
243             options[nbOptions] = getReleaseDate();
244             nbOptions++;
245         }
246         else
247         {
248             freeArrayOfWideString(options, nbOptions);
249             return NULL;
250         }
251
252         options = (wchar_t**)REALLOC(options, sizeof(wchar_t*) * (nbOptions + 1));
253         if (options)
254         {
255             options[nbOptions] = getReleaseTime();
256             nbOptions++;
257         }
258         else
259         {
260             freeArrayOfWideString(options, nbOptions);
261             return NULL;
262         }
263
264         *sizeArrayReturned = nbOptions;
265     }
266     return options;
267 }
268 /*--------------------------------------------------------------------------*/
269 wchar_t* getReleaseMode(void)
270 {
271 #ifdef NDEBUG
272     return os_wcsdup(RELEASE_STRING);
273 #else
274     return os_wcsdup(DEBUG_STRING);
275 #endif
276 }
277 /*--------------------------------------------------------------------------*/
278 wchar_t* getReleaseDate(void)
279 {
280     return to_wide_string(__DATE__);
281 }
282 /*--------------------------------------------------------------------------*/
283 wchar_t *getReleaseTime(void)
284 {
285     return to_wide_string(__TIME__);
286 }
287 /*--------------------------------------------------------------------------*/
288 wchar_t* getCompilerUsedToBuildScilab(void)
289 {
290 #ifdef __INTEL_COMPILER
291     return os_wcsdup(ICC_STRING);
292 #else
293 #ifdef _MSC_VER
294     return os_wcsdup(VC_STRING);
295 #else
296 #ifdef __GNUC__
297     return os_wcsdup(GCC_STRING);
298 #else
299 #ifdef __PGI
300     return os_wcsdup(PGI_STRING);
301 #else
302 #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
303     return os_wcsdup(SUN_STRING);
304 #else
305     return os_wcsdup(UNKNOW_STRING);
306 #endif
307 #endif
308 #endif
309 #endif
310 #endif
311 }
312 /*--------------------------------------------------------------------------*/
313 wchar_t* getCompilerArchitecture(void)
314 {
315 #ifdef _MSC_VER
316 #ifdef _WIN64
317     return os_wcsdup(X64_STRING);
318 #else
319     return os_wcsdup(X86_STRING);
320 #endif
321 #else
322 #ifdef _LP64
323     return os_wcsdup(X64_STRING);
324 #else
325     return os_wcsdup(X86_STRING);
326 #endif
327 #endif
328 }
329 /*--------------------------------------------------------------------------*/
330 BOOL with_modelica_compiler(void)
331 {
332     char *fullpathModelicac = searchEnv(ModelicacName, "PATH");
333     if (fullpathModelicac)
334     {
335         FREE(fullpathModelicac);
336         fullpathModelicac = NULL;
337         return TRUE;
338     }
339     return FALSE;
340 }
341 /*--------------------------------------------------------------------------*/
342 BOOL with_tk(void)
343 {
344     return with_module(TCLSCI_MODULE_NAME) && (getScilabMode() != SCILAB_NWNI);
345 }
346 /*--------------------------------------------------------------------------*/