#675 fix wrong generated version for double number
[libmodbus.git] / src / win32 / configure.js
blob5a579b9e5ea7ef03d568cb20695053997ba9f40c
1 /* Configure script for modbus.dll, specific for Windows with Scripting Host.
3 * Inspired by configure.js from libxml2
5 * oldfaber < oldfaber _at_ gmail _dot_ com >
7 */
9 /* The source directory, relative to the one where this file resides. */
10 var srcDir = "..";
11 /* Base name of what we are building. */
12 var baseName = "modbus";
13 /* Configure file template and output file */
14 var configFile = srcDir + "\\..\\configure.ac";
15 /* Input and output files for the modbus-version.h include */
16 var newfile;
17 /* Version strings for the binary distribution. Will be filled later in the code. */
18 var verMajor;
19 var verMinor;
20 var verMicro;
21 /* modbus features. */
22 var dryRun = false;
23 /* Win32 build options. NOT used yet */
24 var compiler = "msvc";
25 /* Local stuff */
26 var error = 0;
27 /* Filename */
28 var newFile;
30 /* Displays the details about how to use this script. */
31 function usage() {
32 var txt;
34 txt = "Usage:\n";
35 txt += " cscript " + WScript.ScriptName + " <options>\n";
36 txt += " cscript " + WScript.ScriptName + " help\n\n";
37 txt +=
38 "Options can be specified in the form <option>=<value>, where the value is\n";
39 txt += "either 'yes' or 'no', if not stated otherwise.\n\n";
40 txt +=
41 "\nModbus library configure options, default value given in parentheses:\n\n";
42 txt +=
43 " dry-run: Run configure without creating files (" +
44 (dryRun ? "yes" : "no") +
45 ")\n";
46 txt += "\nWin32 build options, default value given in parentheses:\n\n";
47 txt += " compiler: Compiler to be used [msvc|mingw] (" + compiler + ")\n";
48 WScript.Echo(txt);
51 /* read the version from the configuration file */
52 function readVersion() {
53 var fso, cf, ln, s;
54 fso = new ActiveXObject("Scripting.FileSystemObject");
55 cf = fso.OpenTextFile(configFile, 1);
56 while (cf.AtEndOfStream !== true) {
57 ln = cf.ReadLine();
58 s = new String(ln);
59 if (s.search(/^m4_define\(\[libmodbus_version_major/) != -1) {
60 verMajor = s.substr(s.indexOf(",") + 3, 1);
61 } else if (s.search(/^m4_define\(\[libmodbus_version_minor/) != -1) {
62 verMinor = s.substr(s.indexOf(",") + 3, 1);
63 } else if (s.search(/^m4_define\(\[libmodbus_version_micro/) != -1) {
64 str_start_idx = s.indexOf(",") + 3;
65 num_str_length = s.length - str_start_idx - 2;
66 verMicro = s.substr(str_start_idx, num_str_length);
69 cf.Close();
72 /* create the versioned file */
73 function createVersionedFile(newfile, unversioned) {
74 var fso, ofi, of, ln, s;
75 fso = new ActiveXObject("Scripting.FileSystemObject");
76 ofi = fso.OpenTextFile(unversioned, 1);
77 if (!dryRun) {
78 of = fso.CreateTextFile(newfile, true);
80 while (ofi.AtEndOfStream !== true) {
81 ln = ofi.ReadLine();
82 s = new String(ln);
83 if (!dryRun && s.search(/\@LIBMODBUS_VERSION_MAJOR\@/) != -1) {
84 of.WriteLine(s.replace(/\@LIBMODBUS_VERSION_MAJOR\@/, verMajor));
85 } else if (!dryRun && s.search(/\@LIBMODBUS_VERSION_MINOR\@/) != -1) {
86 of.WriteLine(s.replace(/\@LIBMODBUS_VERSION_MINOR\@/, verMinor));
87 } else if (!dryRun && s.search(/\@LIBMODBUS_VERSION_MICRO\@/) != -1) {
88 of.WriteLine(s.replace(/\@LIBMODBUS_VERSION_MICRO\@/, verMicro));
89 } else if (!dryRun && s.search(/\@LIBMODBUS_VERSION\@/) != -1) {
90 of.WriteLine(
91 s.replace(
92 /\@LIBMODBUS_VERSION\@/,
93 verMajor + "." + verMinor + "." + verMicro
96 } else {
97 if (!dryRun) {
98 of.WriteLine(ln);
102 ofi.Close();
103 if (!dryRun) {
104 of.Close();
109 * main(),
110 * Execution begins here.
113 // Parse the command-line arguments.
114 for (i = 0; i < WScript.Arguments.length && error === 0; i++) {
115 var arg, opt;
116 arg = WScript.Arguments(i);
117 opt = arg.substring(0, arg.indexOf("="));
118 if (opt.length > 0) {
119 if (opt == "dry-run") {
120 var str = arg.substring(opt.length + 1, arg.length);
121 if (opt == 1 || opt == "yes") {
122 dryRun = true;
124 } else if (opt == "compiler") {
125 compiler = arg.substring(opt.length + 1, arg.length);
126 } else {
127 error = 1;
129 } else if (i === 0) {
130 if (arg == "help") {
131 usage();
132 WScript.Quit(0);
134 } else {
135 error = 1;
139 // If we fail here, it is because the user supplied an unrecognised argument.
140 if (error !== 0) {
141 usage();
142 WScript.Quit(error);
145 // Read the the version.
146 readVersion();
147 if (error !== 0) {
148 WScript.Echo("Version discovery failed, aborting.");
149 WScript.Quit(error);
152 newfile = srcDir + "\\modbus-version.h";
153 createVersionedFile(newfile, srcDir + "\\modbus-version.h.in");
154 if (error !== 0) {
155 WScript.Echo("Creation of " + newfile + " failed, aborting.");
156 WScript.Quit(error);
158 WScript.Echo(newfile + " created.");
160 newfile = "modbus.dll.manifest";
161 createVersionedFile(newfile, "modbus.dll.manifest.in");
162 if (error !== 0) {
163 WScript.Echo("Creation of " + newfile + " failed, aborting.");
164 WScript.Quit(error);
166 WScript.Echo(newfile + " created.");
168 newfile = "config.h";
169 createVersionedFile(newfile, "config.h.win32");
170 if (error !== 0) {
171 WScript.Echo("Creation of " + newfile + " failed, aborting.");
172 WScript.Quit(error);
174 WScript.Echo(newfile + " created.");
176 WScript.Echo("\nLibmodbus configuration completed\n");