1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
20 * Szabo, Janos Zoltan – initial implementation
21 * Zalanyi, Balazs Andor
24 ******************************************************************************/
25 /* Main program for the TTCN-3 compiler */
34 #include "../../common/memory.h"
35 #include "../../common/version_internal.h"
36 #include "../../common/path.h"
37 #include "../../common/userinfo.h"
44 #include "../../common/license.h"
50 #define COMMENT_PREFIX "// "
52 static unsigned int nof_updated_files
=0;
53 static unsigned int nof_uptodate_files
=0;
54 unsigned int nof_notupdated_files
=0;
56 static boolean
skip_over_lines(FILE *fp
, size_t nof_lines
)
59 while (i
< nof_lines
) {
72 static boolean
update_file(const char *file_name
, FILE *fp
, size_t skip_lines
)
74 boolean files_differ
= FALSE
;
76 FILE *target
= fopen(file_name
, "r");
77 if (target
== NULL
) files_differ
= TRUE
;
79 if (!files_differ
) files_differ
= skip_over_lines(target
, skip_lines
);
82 if (fseek(fp
, 0L, SEEK_SET
)) {
83 ERROR("Seek operation failed on a temporary file when trying to "
84 "update output file `%s': %s", file_name
, strerror(errno
));
86 files_differ
= skip_over_lines(fp
, skip_lines
);
90 /* compare the rest of the files */
92 char buf1
[BUFSIZE
], buf2
[BUFSIZE
];
93 int len1
= fread(buf1
, 1, BUFSIZE
, fp
);
94 int len2
= fread(buf2
, 1, BUFSIZE
, target
);
95 if ((len1
!= len2
) || memcmp(buf1
, buf2
, len1
)) {
98 } else if (len1
< BUFSIZE
) break;
102 if (target
!= NULL
) fclose(target
);
105 if (fseek(fp
, 0L, SEEK_SET
)) {
106 ERROR("Seek operation failed on a temporary file when trying to "
107 "update output file `%s': %s", file_name
, strerror(errno
));
109 target
= fopen(file_name
, "w");
110 if (target
== NULL
) {
111 ERROR("Cannot open output file `%s' for writing: %s", file_name
,
115 /* copy the contents of fp into target */
118 size_t len
= fread(buf
, 1, BUFSIZE
, fp
);
119 if (fwrite(buf
, 1, len
, target
) != len
) {
120 ERROR("Cannot write to output file `%s': %s", file_name
,
124 if (len
< BUFSIZE
) break;
132 FILE *open_output_file(const char *file_name
, boolean
*is_temporary
)
135 switch (get_path_status(file_name
)) {
137 if (force_overwrite
) *is_temporary
= FALSE
;
138 else *is_temporary
= TRUE
;
141 ERROR("Output file `%s' is a directory", file_name
);
145 *is_temporary
= FALSE
;
150 ERROR("Creation of a temporary file failed when trying to update "
151 "output file `%s': %s", file_name
, strerror(errno
));
155 fp
= fopen(file_name
, "w");
157 ERROR("Cannot open output file `%s' for writing: %s", file_name
,
165 void close_output_file(const char *file_name
, FILE *fp
,
166 boolean is_temporary
, size_t skip_lines
)
169 if (update_file(file_name
, fp
, skip_lines
)) {
170 NOTIFY("File `%s' was updated.", file_name
);
173 if (display_up_to_date
) {
174 NOTIFY("File `%s' was up-to-date.", file_name
);
176 DEBUG(1, "File `%s' was up-to-date.", file_name
);
178 nof_uptodate_files
++;
181 NOTIFY("File `%s' was generated.", file_name
);
187 void write_output(output_struct
*output
, const char *module_name
,
188 const char *module_dispname
, const char *filename_suffix
, boolean is_ttcn
,
189 boolean has_circular_import
, boolean is_module
)
191 char *header_name
, *source_name
, *user_info
;
193 boolean is_temporary
;
195 if (output_dir
!= NULL
) {
196 header_name
= mprintf("%s/%s%s.hh", output_dir
,
197 duplicate_underscores
? module_name
: module_dispname
,
199 source_name
= mprintf("%s/%s%s.cc", output_dir
,
200 duplicate_underscores
? module_name
: module_dispname
,
203 header_name
= mprintf("%s%s.hh",
204 duplicate_underscores
? module_name
: module_dispname
,
206 source_name
= mprintf("%s%s.cc",
207 duplicate_underscores
? module_name
: module_dispname
,
210 user_info
= get_user_info();
214 DEBUG(1, "Writing file `%s' ... ", header_name
);
216 fp
= open_output_file(header_name
, &is_temporary
);
218 fprintf(fp
, "// This C++ header file was generated by the %s compiler\n"
219 "// of the TTCN-3 Test Executor version " PRODUCT_NUMBER
"\n"
221 COPYRIGHT_STRING
"\n\n"
222 "// Do not edit this file unless you know what you are doing.\n\n"
225 is_ttcn
? "TTCN-3" : "ASN.1",
226 user_info
, module_name
, module_name
);
228 /* check if the generated code matches the runtime */
230 "#if%sdef TITAN_RUNTIME_2\n"
231 "#error Generated code does not match with used runtime.\\\n"
234 use_runtime_2
? "n" : "",
236 "Code was generated with -R option but -DTITAN_RUNTIME_2 was not used.":
237 "Code was generated without -R option but -DTITAN_RUNTIME_2 was used.");
239 /* #include directives must be the first in order to make GCC's
240 * precompiled headers working. */
241 if (output
->header
.includes
!= NULL
) {
242 fputs("\n/* Header file includes */\n\n", fp
);
243 fputs(output
->header
.includes
, fp
);
245 "#if TTCN3_VERSION != %d\n"
246 "#error Version mismatch detected.\\\n"
247 " Please check the version of the %s compiler and the "
250 TTCN3_VERSION
, is_ttcn
? "TTCN-3" : "ASN.1");
251 /* Check the platform matches */
252 #if defined SOLARIS || defined SOLARIS8
253 fputs("#if !defined SOLARIS && !defined SOLARIS8 \n",fp
);
254 fputs(" #error This file should be compiled on SOLARIS or SOLARIS8 \n",fp
);
255 fputs("#endif \n",fp
);
260 "#error This file should be compiled on %s\n"
261 "#endif\n", expected_platform
, expected_platform
);
267 if (!has_circular_import
)
268 fprintf(fp
, "\nnamespace %s {\n", module_name
);
270 if (output
->header
.class_decls
!= NULL
) {
271 if (has_circular_import
) {
272 fprintf(fp
, "\n#undef %s_HH\n"
273 "#endif\n", module_name
);
274 fprintf(fp
, "\nnamespace %s {\n", module_name
);
276 fputs("\n/* Forward declarations of classes */\n\n",fp
);
277 fputs(output
->header
.class_decls
, fp
);
278 if (has_circular_import
) {
279 fputs("\n} /* end of namespace */\n", fp
);
280 fprintf(fp
, "\n#ifndef %s_HH\n"
281 "#define %s_HH\n", module_name
, module_name
);
285 if (has_circular_import
)
286 fprintf(fp
, "\nnamespace %s {\n", module_name
);
288 if (output
->header
.typedefs
!= NULL
) {
289 fputs("\n/* Type definitions */\n\n", fp
);
290 fputs(output
->header
.typedefs
, fp
);
293 if (output
->header
.class_defs
!= NULL
) {
294 fputs("\n/* Class definitions */\n\n", fp
);
295 fputs(output
->header
.class_defs
, fp
);
298 if (output
->header
.function_prototypes
!= NULL
) {
299 fputs("\n/* Function prototypes */\n\n", fp
);
300 fputs(output
->header
.function_prototypes
, fp
);
303 if (output
->header
.global_vars
!= NULL
) {
304 fputs("\n/* Global variable declarations */\n\n", fp
);
305 fputs(output
->header
.global_vars
, fp
);
308 fputs("\n} /* end of namespace */\n", fp
);
310 if (output
->header
.testport_includes
!= NULL
) {
311 fputs("\n/* Test port header files */\n\n", fp
);
312 fputs(output
->header
.testport_includes
, fp
);
315 fputs("\n#endif\n", fp
);
317 close_output_file(header_name
, fp
, is_temporary
, 10);
321 DEBUG(1, "Writing file `%s' ... ", source_name
);
323 fp
= open_output_file(source_name
, &is_temporary
);
325 fprintf(fp
, "// This C++ source file was generated by the %s compiler\n"
326 "// of the TTCN-3 Test Executor version " PRODUCT_NUMBER
"\n"
328 COPYRIGHT_STRING
"\n\n"
329 "// Do not edit this file unless you know what you are doing.\n",
330 is_ttcn
? "TTCN-3" : "ASN.1", user_info
);
332 if (output
->source
.includes
!= NULL
) {
333 fputs("\n/* Including header files */\n\n", fp
);
334 fputs(output
->source
.includes
, fp
);
337 fprintf(fp
, "\nnamespace %s {\n", module_name
);
339 if (output
->source
.static_function_prototypes
!= NULL
) {
340 fputs("\n/* Prototypes of static functions */\n\n", fp
);
341 fputs(output
->source
.static_function_prototypes
, fp
);
344 if (output
->source
.static_conversion_function_prototypes
!= NULL
) {
345 fputs("\n/* Prototypes of static conversion functions */\n\n", fp
);
346 fputs(output
->source
.static_conversion_function_prototypes
, fp
);
349 if (output
->source
.string_literals
!= NULL
) {
350 fputs("\n/* Literal string constants */\n\n", fp
);
351 fputs(output
->source
.string_literals
, fp
);
354 if (output
->source
.class_defs
!= NULL
) {
355 fputs("\n/* Class definitions for internal use */\n\n", fp
);
356 fputs(output
->source
.class_defs
, fp
);
359 if (output
->source
.global_vars
!= NULL
) {
360 fputs("\n/* Global variable definitions */\n\n", fp
);
361 fputs(output
->source
.global_vars
, fp
);
364 if (output
->source
.methods
!= NULL
) {
365 fputs("\n/* Member functions of C++ classes */\n\n", fp
);
366 fputs(output
->source
.methods
, fp
);
369 if (output
->source
.function_bodies
!= NULL
) {
370 fputs("\n/* Bodies of functions, altsteps and testcases */\n\n", fp
);
371 fputs(output
->source
.function_bodies
, fp
);
374 if (output
->source
.static_function_bodies
!= NULL
) {
375 fputs("\n/* Bodies of static functions */\n\n", fp
);
376 fputs(output
->source
.static_function_bodies
, fp
);
379 if (output
->source
.static_conversion_function_bodies
!= NULL
) {
380 fputs("\n/* Bodies of static conversion functions */\n\n", fp
);
381 fputs(output
->source
.static_conversion_function_bodies
, fp
);
384 fputs("\n} /* end of namespace */\n", fp
);
386 close_output_file(source_name
, fp
, is_temporary
, 7);
393 void report_nof_updated_files(void)
395 DEBUG(1, "%u files updated, %u were up-to-date, %u excluded from code"
397 nof_updated_files
, nof_uptodate_files
, nof_notupdated_files
);
398 if(nof_updated_files
!=0)
399 NOTIFY("%u file%s updated.", nof_updated_files
,
400 nof_updated_files
> 1 ? "s were" : " was");
402 NOTIFY("None of the files needed update.");