makefile.c 173 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
///////////////////////////////////////////////////////////////////////////////
Elemer Lelik's avatar
Elemer Lelik committed
2
// Copyright (c) 2000-2015 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
4
5
6
7
8
9
10
11
12
13
14
15
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#if defined SOLARIS || defined SOLARIS8
Elemer Lelik's avatar
Elemer Lelik committed
16
#include <sys/utsname.h>
Elemer Lelik's avatar
Elemer Lelik committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#endif

#include "../common/memory.h"
#include "../common/path.h"
#include "../common/version_internal.h"
#include "../common/userinfo.h"
#include "ttcn3/ttcn3_preparser.h"
#include "asn1/asn1_preparser.h"

#ifdef LICENSE
#include "../common/license.h"
#endif

#include "xpather.h"

static const char *program_name = NULL;
static unsigned int error_count = 0;
static boolean suppress_warnings = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
35
36
void free_string2_list(struct string2_list* act_elem);
void free_string_list(struct string_list* act_elem);
Elemer Lelik's avatar
Elemer Lelik committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
void ERROR(const char *fmt, ...)
{
  va_list parameters;
  fprintf(stderr, "%s: error: ", program_name);
  va_start(parameters, fmt);
  vfprintf(stderr, fmt, parameters);
  va_end(parameters);
  fprintf(stderr, "\n");
  fflush(stderr);
  error_count++;
}

void WARNING(const char *fmt, ...)
{
  va_list parameters;
  if (suppress_warnings) return;
  fprintf(stderr, "%s: warning: ", program_name);
  va_start(parameters, fmt);
  vfprintf(stderr, fmt, parameters);
  va_end(parameters);
  putc('\n', stderr);
  fflush(stderr);
}

void NOTIFY(const char *fmt, ...)
{
  va_list parameters;
  va_start(parameters, fmt);
  vfprintf(stderr, fmt, parameters);
  va_end(parameters);
  putc('\n', stderr);
  fflush(stderr);
}

void DEBUG(unsigned level, const char *fmt, ...)
{
  va_list parameters;
  fprintf(stderr, "%*s", 2 * level, "");
  va_start(parameters, fmt);
  vfprintf(stderr, fmt, parameters);
  va_end(parameters);
  putc('\n', stderr);
  fflush(stderr);
}

void path_error(const char *fmt, ...)
{
  va_list ap;
  char *err_msg;
  va_start(ap, fmt);
  err_msg = mprintf_va_list(fmt, ap);
  va_end(ap);
  ERROR("%s", err_msg);
  Free(err_msg);
}


#if defined SOLARIS || defined SOLARIS8
/** Automatic detection of Solaris version based on uname() system call.
 * Distinguishing is needed because some socket functions use socklen_t
 * (which is an alias for unsigned int) as length arguments on Solaris 8.
 * On Solaris 2.6 the argument type is simply int and no socklen_t or other
 * alias exists.
 * Note: It was discovered later that Solaris 7 (which is used rarely within
 * Ericsson) already uses socklen_t thus the SOLARIS8 platform identifier is a
 * bit misleading. */
static const char *get_platform_string(void)
{
    struct utsname name;
    int major, minor;
    if (uname(&name) < 0) {
	WARNING("System call uname() failed: %s", strerror(errno));
	errno = 0;
	return "SOLARIS";
    }
    if (sscanf(name.release, "%d.%d", &major, &minor) == 2 && major == 5) {
	if (minor <= 6) return "SOLARIS";
	else return "SOLARIS8";
    } else {
	ERROR("Invalid OS release: %s", name.release);
	return "SOLARIS";
    }
}
#elif defined LINUX
#define get_platform_string() "LINUX"
#elif defined FREEBSD
#define get_platform_string() "FREEBSD"
#elif defined WIN32
#define get_platform_string() "WIN32"
#elif defined INTERIX
#define get_platform_string() "INTERIX"
#else
#error Platform was not set.
#endif

/** structure for describing TTCN-3 and ASN.1 modules */
struct module_struct {
  char *dir_name; /* directory of the TTCN-3 or ASN.1 file, it is NULL if the
		     file is in the current working directory */
  char *file_name; /* name of the TTCN-3 or ASN.1 file */
  char *module_name; /* name of the TTCN-3 or ASN.1 module */
  boolean is_regular; /* indicates whether the name of the source file follows
			 the default naming convention */
};

/** structure for describing test ports and other C/C++ modules */
struct user_struct {
  char *dir_name; /* directory of the C/C++ source files, it is NULL if the
		     files are in the current working directory */
  char *file_prefix; /* the common prefix of the header and source file */
  char *header_name; /* name of the C/C++ header file, which has .hh or .h or .hpp
			suffix, it is NULL if there is no header file */
  char *source_name; /* name of the C/C++ source file, which has .cc or .c or .cpp
			suffix, it is NULL if there is no source file */
  boolean has_hh_suffix; /* indicates whether the header file is present and
			    has .hh or .hpp suffix */
  boolean has_cc_suffix; /* indicates whether the source file is present and
			    has .cc or .cpp suffix */
};

/** structure for directories that pre-compiled files are taken from */
struct base_dir_struct {
  const char *dir_name; /* name of the directory */
  boolean has_modules; /* indicates whether there are TTCN-3/ASN.1 modules in
			  the directory (it is set to FALSE if dir_name
			  contains user C/C++ files only */
};

/** data structure that describes the information needed for the Makefile */
struct makefile_struct {
Elemer Lelik's avatar
Elemer Lelik committed
167
  char *project_name;
Elemer Lelik's avatar
Elemer Lelik committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
  size_t nTTCN3Modules;
  struct module_struct *TTCN3Modules;

  boolean preprocess;
  size_t nTTCN3PPModules;
  struct module_struct *TTCN3PPModules;

  boolean TTCN3ModulesRegular;
  boolean BaseTTCN3ModulesRegular;
  size_t nTTCN3IncludeFiles;
  char **TTCN3IncludeFiles;

  size_t nASN1Modules;
  struct module_struct *ASN1Modules;
  boolean ASN1ModulesRegular;
  boolean BaseASN1ModulesRegular;

  size_t nUserFiles;
  struct user_struct *UserFiles;
  boolean UserHeadersRegular;
  boolean UserSourcesRegular;
  boolean BaseUserHeadersRegular;
  boolean BaseUserSourcesRegular;

  size_t nOtherFiles;
  char **OtherFiles;

  boolean central_storage;
  size_t nBaseDirs;
  struct base_dir_struct *BaseDirs;
  char *working_dir;
  boolean gnu_make;
  boolean single_mode;
  char *output_file;
  char *ets_name;
  boolean force_overwrite;
  boolean use_runtime_2;
  boolean dynamic;
  boolean gcc_dep;
  char *code_splitting_mode;
  boolean coverage;
  char *tcov_file_name;
Elemer Lelik's avatar
Elemer Lelik committed
210
  struct string_list* profiled_file_list; /* not owned */
Elemer Lelik's avatar
Elemer Lelik committed
211
  boolean library;
Elemer Lelik's avatar
Elemer Lelik committed
212
213
  boolean linkingStrategy;
  boolean hierarchical;
Elemer Lelik's avatar
Elemer Lelik committed
214
215
216
  struct string_list* sub_project_dirs; /* not owned */
  struct string_list* ttcn3_prep_includes; /* not owned */
  struct string_list* ttcn3_prep_defines; /* not owned */
Elemer Lelik's avatar
Elemer Lelik committed
217
  struct string_list* ttcn3_prep_undefines; /* not owned */
Elemer Lelik's avatar
Elemer Lelik committed
218
219
  struct string_list* prep_includes; /* not owned */
  struct string_list* prep_defines; /* not owned */
Elemer Lelik's avatar
Elemer Lelik committed
220
  struct string_list* prep_undefines; /* not owned */
Elemer Lelik's avatar
Elemer Lelik committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
  boolean codesplittpd;
  boolean quietly;
  boolean disablesubtypecheck;
  const char *cxxcompiler;
  const char *optlevel;
  const char *optflags;
  boolean disableber;
  boolean disableraw;
  boolean disabletext;
  boolean disablexer;
  boolean disablejson;
  boolean forcexerinasn;
  boolean defaultasomit;
  boolean gccmsgformat;
  boolean linenumbersonlymsg;
  boolean includesourceinfo;
  boolean addsourcelineinfo;
  boolean suppresswarnings;
  boolean outparamboundness;
Elemer Lelik's avatar
Elemer Lelik committed
240
  boolean omit_in_value_list;
Elemer Lelik's avatar
Elemer Lelik committed
241
242
  boolean warnings_for_bad_variants;
  boolean disable_predef_ext_folder;
Elemer Lelik's avatar
Elemer Lelik committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  struct string_list* solspeclibraries; /* not owned */
  struct string_list* sol8speclibraries; /* not owned */
  struct string_list* linuxspeclibraries; /* not owned */
  struct string_list* freebsdspeclibraries; /* not owned */
  struct string_list* win32speclibraries; /* not owned */
  const char *ttcn3preprocessor;
  struct string_list* linkerlibraries; /* not owned */
  struct string_list* additionalObjects; /* not owned */
  struct string_list* linkerlibsearchpath; /* not owned */
  char* generatorCommandOutput; /* not owned */
  struct string2_list* target_placement_list; /* not owned */
};

/** Initializes structure \a makefile with empty lists and default settings. */
static void init_makefile_struct(struct makefile_struct *makefile)
{
Elemer Lelik's avatar
Elemer Lelik committed
259
  makefile->project_name = NULL;
Elemer Lelik's avatar
Elemer Lelik committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  makefile->nTTCN3Modules = 0;
  makefile->TTCN3Modules = NULL;
  makefile->preprocess = FALSE;
  makefile->nTTCN3PPModules = 0;
  makefile->TTCN3PPModules = NULL;
  makefile->TTCN3ModulesRegular = TRUE;
  makefile->BaseTTCN3ModulesRegular = TRUE;
  makefile->nTTCN3IncludeFiles = 0;
  makefile->TTCN3IncludeFiles = NULL;
  makefile->nASN1Modules = 0;
  makefile->ASN1Modules = NULL;
  makefile->ASN1ModulesRegular = TRUE;
  makefile->BaseASN1ModulesRegular = TRUE;
  makefile->nUserFiles = 0;
  makefile->UserFiles = NULL;
  makefile->UserHeadersRegular = TRUE;
  makefile->UserSourcesRegular = TRUE;
  makefile->BaseUserHeadersRegular = TRUE;
  makefile->BaseUserSourcesRegular = TRUE;
  makefile->nOtherFiles = 0;
  makefile->OtherFiles = NULL;
  makefile->central_storage = FALSE;
  makefile->nBaseDirs = 0;
  makefile->BaseDirs = NULL;
  makefile->working_dir = get_working_dir();
  makefile->gnu_make = FALSE;
  makefile->single_mode = FALSE;
  makefile->ets_name = NULL;
  makefile->output_file = NULL;
  makefile->force_overwrite = FALSE;
  makefile->use_runtime_2 = FALSE;
  makefile->dynamic = FALSE;
  makefile->gcc_dep = FALSE;
  makefile->code_splitting_mode = NULL;
  makefile->coverage = FALSE;
  makefile->tcov_file_name = NULL;
Elemer Lelik's avatar
Elemer Lelik committed
296
  makefile->profiled_file_list = NULL;
Elemer Lelik's avatar
Elemer Lelik committed
297
  makefile->library = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
298
299
  makefile->linkingStrategy = FALSE;
  makefile->hierarchical = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
300
301
302
303
304
  makefile->sub_project_dirs = NULL;
  makefile->ttcn3_prep_includes = NULL;
  makefile->prep_includes = NULL;
  makefile->prep_defines = NULL;
  makefile->outparamboundness = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
305
  makefile->omit_in_value_list = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
306
  makefile->warnings_for_bad_variants = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
  makefile->solspeclibraries = NULL;
  makefile->sol8speclibraries = NULL;
  makefile->linuxspeclibraries = NULL;
  makefile->freebsdspeclibraries = NULL;
  makefile->win32speclibraries = NULL;
  makefile->linkerlibraries = NULL;
  makefile->additionalObjects = NULL;
  makefile->linkerlibsearchpath = NULL;
  makefile->generatorCommandOutput = NULL;
  makefile->target_placement_list = NULL;
}

/** Deallocates all memory associated with structure \a makefile. */
static void free_makefile_struct(const struct makefile_struct *makefile)
{
Elemer Lelik's avatar
Elemer Lelik committed
322
  Free(makefile->project_name);
Elemer Lelik's avatar
Elemer Lelik committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
  size_t i;
  for (i = 0; i < makefile->nTTCN3Modules; i++) {
    Free(makefile->TTCN3Modules[i].dir_name);
    Free(makefile->TTCN3Modules[i].file_name);
    Free(makefile->TTCN3Modules[i].module_name);
  }
  Free(makefile->TTCN3Modules);
  for (i = 0; i < makefile->nTTCN3PPModules; i++) {
    Free(makefile->TTCN3PPModules[i].dir_name);
    Free(makefile->TTCN3PPModules[i].file_name);
    Free(makefile->TTCN3PPModules[i].module_name);
  }
  Free(makefile->TTCN3PPModules);
  for (i = 0; i < makefile->nTTCN3IncludeFiles; i++)
    Free(makefile->TTCN3IncludeFiles[i]);
  Free(makefile->TTCN3IncludeFiles);
  for (i = 0; i < makefile->nASN1Modules; i++) {
    Free(makefile->ASN1Modules[i].dir_name);
    Free(makefile->ASN1Modules[i].file_name);
    Free(makefile->ASN1Modules[i].module_name);
  }
  Free(makefile->ASN1Modules);
  for (i = 0; i < makefile->nUserFiles; i++) {
    Free(makefile->UserFiles[i].dir_name);
    Free(makefile->UserFiles[i].file_prefix);
    Free(makefile->UserFiles[i].header_name);
    Free(makefile->UserFiles[i].source_name);
  }
  Free(makefile->UserFiles);
  for (i = 0; i < makefile->nOtherFiles; i++) Free(makefile->OtherFiles[i]);
  Free(makefile->OtherFiles);
  Free(makefile->BaseDirs);
  Free(makefile->working_dir);
  Free(makefile->ets_name);
  Free(makefile->output_file);
  Free(makefile->code_splitting_mode);
  Free(makefile->tcov_file_name);
}

/** Displays the contents of structure \a makefile as debug messages. */
static void dump_makefile_struct(const struct makefile_struct *makefile,
  unsigned level)
{
  size_t i;
  DEBUG(level, "Data used for Makefile generation:");
Elemer Lelik's avatar
Elemer Lelik committed
368
  DEBUG(level + 1, "TTCN-3 project name: %s", makefile->project_name);
Elemer Lelik's avatar
Elemer Lelik committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
  DEBUG(level + 1, "TTCN-3 modules: (%u pcs.)", makefile->nTTCN3Modules);
  for (i = 0; i < makefile->nTTCN3Modules; i++) {
    const struct module_struct *module = makefile->TTCN3Modules + i;
    DEBUG(level + 2, "Module name: %s", module->module_name);
    if (module->dir_name != NULL)
      DEBUG(level + 3, "Directory: %s", module->dir_name);
    DEBUG(level + 3, "File name: %s", module->file_name);
    DEBUG(level + 3, "Follows the naming convention: %s",
      module->is_regular ? "yes" : "no");
  }
  DEBUG(level + 1, "TTCN-3 preprocessing: %s",
        makefile->preprocess ? "yes" : "no");
  if (makefile->preprocess) {
    DEBUG(level + 1, "TTCN-3 modules to be preprocessed: (%u pcs.)",
          makefile->nTTCN3PPModules);
    for (i = 0; i < makefile->nTTCN3PPModules; i++) {
      const struct module_struct *module = makefile->TTCN3PPModules + i;
      DEBUG(level + 2, "Module name: %s", module->module_name);
      if (module->dir_name != NULL)
        DEBUG(level + 3, "Directory: %s", module->dir_name);
      DEBUG(level + 3, "File name: %s", module->file_name);
      DEBUG(level + 3, "Follows the naming convention: %s",
        module->is_regular ? "yes" : "no");
    }
    DEBUG(level + 1, "TTCN-3 include files: (%u pcs.)",
      makefile->nTTCN3IncludeFiles);
    for (i = 0; i < makefile->nTTCN3IncludeFiles; i++)
      DEBUG(level + 2, "File name: %s", makefile->TTCN3IncludeFiles[i]);
  }
  DEBUG(level + 1, "All local TTCN-3 modules follow the naming convention: %s",
    makefile->TTCN3ModulesRegular ? "yes" : "no");
  if (makefile->central_storage) DEBUG(level + 1, "All TTCN-3 modules from other "
    "directories follow the naming convention: %s",
    makefile->BaseTTCN3ModulesRegular ? "yes" : "no");
  DEBUG(level + 1, "ASN.1 modules: (%u pcs.)", makefile->nASN1Modules);
  for (i = 0; i < makefile->nASN1Modules; i++) {
    const struct module_struct *module = makefile->ASN1Modules + i;
    DEBUG(level + 2, "Module name: %s", module->module_name);
    if (module->dir_name != NULL)
      DEBUG(level + 3, "Directory: %s", module->dir_name);
    DEBUG(level + 3, "File name: %s", module->file_name);
    DEBUG(level + 3, "Follows the naming convention: %s",
      module->is_regular ? "yes" : "no");
  }
  DEBUG(level + 1, "All local ASN.1 modules follow the naming convention: %s",
    makefile->ASN1ModulesRegular ? "yes" : "no");
  if (makefile->central_storage) DEBUG(level + 1, "All ASN.1 modules from other "
    "directories follow the naming convention: %s",
    makefile->BaseASN1ModulesRegular ? "yes" : "no");
  DEBUG(level + 1, "User C/C++ modules: (%u pcs.)", makefile->nUserFiles);
  for (i = 0; i < makefile->nUserFiles; i++) {
    const struct user_struct *user = makefile->UserFiles + i;
    DEBUG(level + 2, "File prefix: %s", user->file_prefix);
    if (user->dir_name != NULL)
      DEBUG(level + 3, "Directory: %s", user->dir_name);
    if (user->header_name != NULL) {
      DEBUG(level + 3, "Header file: %s", user->header_name);
      DEBUG(level + 3, "Header file has .hh or .hpp suffix: %s",
Elemer Lelik's avatar
Elemer Lelik committed
427
      user->has_hh_suffix ? "yes" : "no");
Elemer Lelik's avatar
Elemer Lelik committed
428
429
430
431
    }
    if (user->source_name != NULL) {
      DEBUG(level + 3, "Source file: %s", user->source_name);
      DEBUG(level + 3, "Source file has .cc or .cpp suffix: %s",
Elemer Lelik's avatar
Elemer Lelik committed
432
      user->has_cc_suffix ? "yes" : "no");
Elemer Lelik's avatar
Elemer Lelik committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
      DEBUG(level + 3, "Object file: %s.o", user->file_prefix);
    }
  }
  DEBUG(level + 1, "All local C/C++ header files follow the naming "
    "convention: %s", makefile->UserHeadersRegular ? "yes" : "no");
  DEBUG(level + 1, "All local C/C++ source files follow the naming "
    "convention: %s", makefile->UserSourcesRegular ? "yes" : "no");
  if (makefile->central_storage) {
    DEBUG(level + 1, "All C/C++ header files from other directories follow the "
      "naming convention: %s", makefile->BaseUserHeadersRegular ? "yes" : "no");
    DEBUG(level + 1, "All C/C++ source files from other directories follow the "
      "naming convention: %s", makefile->BaseUserSourcesRegular ? "yes" : "no");
  }
  DEBUG(level + 1, "Other files: (%u pcs.)", makefile->nOtherFiles);
  for (i = 0; i < makefile->nOtherFiles; i++)
    DEBUG(level + 2, "File name: %s", makefile->OtherFiles[i]);
  DEBUG(level + 1, "Use pre-compiled files from central storage: %s",
    makefile->central_storage ? "yes" : "no");
  if (makefile->central_storage) {
    DEBUG(level + 1, "Directories of pre-compiled files: (%u pcs.)",
      makefile->nBaseDirs);
    for (i = 0; i < makefile->nBaseDirs; i++) {
      const struct base_dir_struct *base_dir = makefile->BaseDirs + i;
      DEBUG(level + 2, "Directory: %s", base_dir->dir_name);
      DEBUG(level + 3, "Has TTCN-3/ASN.1 modules: %s",
Elemer Lelik's avatar
Elemer Lelik committed
458
      base_dir->has_modules ? "yes" : "no");
Elemer Lelik's avatar
Elemer Lelik committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
    }
  }
  DEBUG(level + 1, "Working directory: %s",
    makefile->working_dir != NULL ? makefile->working_dir : "<unknown>");
  DEBUG(level + 1, "GNU make: %s", makefile->gnu_make ? "yes" : "no");
  DEBUG(level + 1, "Execution mode: %s",
    makefile->single_mode ? "single" : "parallel");
  DEBUG(level + 1, "Name of executable: %s",
    makefile->ets_name != NULL ? makefile->ets_name : "<unknown>");
  DEBUG(level + 1, "Output file: %s",
    makefile->output_file != NULL ? makefile->output_file : "<unknown>");
  DEBUG(level + 1, "Force overwrite: %s",
    makefile->force_overwrite ? "yes" : "no");
  DEBUG(level + 1, "Use function test runtime: %s",
    makefile->use_runtime_2 ? "yes" : "no");
  DEBUG(level + 1, "Use dynamic linking: %s",
    makefile->dynamic ? "yes" : "no");
  DEBUG(level + 1, "Code splitting mode: %s",
    makefile->code_splitting_mode != NULL ?
      makefile->code_splitting_mode : "<unknown>");
  DEBUG(level + 1, "Code coverage file: %s",
    makefile->tcov_file_name != NULL ?
      makefile->tcov_file_name : "<unknown>");
Elemer Lelik's avatar
Elemer Lelik committed
482
483
484
485
486
487
488
489
490
491
  if (makefile->profiled_file_list) {
    char* lists = mcopystr(makefile->profiled_file_list->str);
    struct string_list* iter = makefile->profiled_file_list->next;
    while(iter != NULL) {
      lists = mputprintf(lists, " %s", iter->str);
      iter = iter->next;
    }
    DEBUG(level + 1, "Profiled file list(s): %s", lists);
    Free(lists);
  }
Elemer Lelik's avatar
Elemer Lelik committed
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#ifdef COVERAGE_BUILD
  DEBUG(level + 1, "Enable coverage: %s", makefile->coverage ? "yes" : "no");
#endif
}

/** Returns the name of an existing file that is related to command line
 * argument \a argument. Tries the given list of suffixes. NULL pointer is
 * returned if no file was found. The returned string shall be deallocated
 * by the caller. */
static char *get_file_name_for_argument(const char *argument)
{
  static const char * const suffix_list[] = {
    "", ".ttcnpp", ".ttcnin", ".ttcn", ".ttcn3", ".3mp", ".asn", ".asn1",
    ".cc", ".c", ".cpp", ".hh", ".h",".hpp", ".cfg", ".prj", NULL
  };
  const char * const *suffix_ptr;
  for (suffix_ptr = suffix_list; *suffix_ptr != NULL; suffix_ptr++) {
    char *file_name = mputstr(mcopystr(argument), *suffix_ptr);
    if (get_path_status(file_name) == PS_FILE) return file_name;
    Free(file_name);
  }
  return NULL;
}

/** Converts \a path_name to an absolute directory using \a working_dir.
 * NULL pointer is returned if \a path_name does not contain a directory or
 * the resulting absolute directory is identical to \a working_dir.
 * The returned string shall be deallocated by the caller. */
static char *get_dir_name(const char *path_name, const char *working_dir)
{
  char *dir_name = get_dir_from_path(path_name);
  if (dir_name != NULL) {
Elemer Lelik's avatar
Elemer Lelik committed
524
    char *absolute_dir = get_absolute_dir(dir_name, working_dir, TRUE);
Elemer Lelik's avatar
Elemer Lelik committed
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
    Free(dir_name);
    if (absolute_dir == NULL || working_dir == NULL) {
      /* an error occurred */
      return NULL;
    } else if (!strcmp(absolute_dir, working_dir)) {
      /* the directory is identical to the working dir */
      Free(absolute_dir);
      return NULL;
    } else return absolute_dir;
  } else return NULL;
}

/** Returns whether \a dirname1 and \a dirname2 contain the same (canonized
 * absolute) directories. NULL pointer is handled in a special way: it is
 * identical only to itself. */
static boolean is_same_directory(const char *dirname1, const char *dirname2)
{
  if (dirname1 == NULL) {
    if (dirname2 == NULL) return TRUE;
    else return FALSE;
  } else {
    if (dirname2 == NULL) return FALSE;
    else if (strcmp(dirname1, dirname2)) return FALSE;
    else return TRUE;
  }
}

/** Returns whether the file \a filename1 in directory \a dirname1 is identical
 * to file \a filename2 in directory \a dirname2. Only the directory names can
 * be NULL. */
static boolean is_same_file(const char *dirname1, const char *filename1,
  const char *dirname2, const char *filename2)
{
  /* first examine the file names for efficiency reasons */
  if (strcmp(filename1, filename2)) return FALSE;
  else return is_same_directory(dirname1, dirname2);
}

/** Determines whether the TTCN-3 or ASN.1 module identifiers \a module1 and
 * \a module2 are the same. Characters '-' and '_' in module names are not
 * distinguished. */
static boolean is_same_module(const char *module1, const char *module2)
{
  size_t i;
  for (i = 0; ; i++) {
    switch (module1[i]) {
    case '\0':
      if (module2[i] == '\0') return TRUE;
      else return FALSE;
    case '-':
    case '_':
      if (module2[i] != '-' && module2[i] != '_') return FALSE;
      break;
    default:
      if (module1[i] != module2[i]) return FALSE;
      break;
    }
  }
  return FALSE; /* to avoid warnings */
}

/** Determines the suffix (i.e. the character sequence following the last dot)
 * of file or path name \a file_name. NULL pointer is returned if \a file_name
 * does not contain any dot character or the last character of it is a dot.
 * The suffix is not copied, the returned pointer points to the tail of
 * \a file_name. */
static const char *get_suffix(const char *file_name)
{
  size_t last_dot = (size_t)-1;
  size_t i;
  for (i = 0; file_name[i] != '\0'; i++)
    if (file_name[i] == '.') last_dot = i;
  if (last_dot == (size_t)-1 || file_name[last_dot + 1] == '\0') return NULL;
  else return file_name + last_dot + 1;
}

/** Truncates the suffix (i.e. the last dot and the characters following it)
 * from \a file_name and returns a copy of the prefix of \a file_name.
 * If \a file_name does not have a suffix an exact copy of it is returned.
 * The returned string shall be deallocated by the caller. */
static char *cut_suffix(const char *file_name)
{
  char *ret_val;
  size_t last_dot = (size_t)-1;
  size_t i;
  for (i = 0; file_name[i] != '\0'; i++)
    if (file_name[i] == '.') last_dot = i;
  ret_val = mcopystr(file_name);
  if (last_dot != (size_t)-1) ret_val = mtruncstr(ret_val, last_dot);
  return ret_val;
}

/** Determines the name of the preprocessed file from \a file_name.
 *  It is assumed that \a file_name has ttcnpp suffix.
 *  The returned string shall be deallocated by the caller. */
static char *get_preprocessed_file_name(const char *file_name)
{
  char *ret_val = cut_suffix(file_name);
  ret_val = mputstr(ret_val, ".ttcn");
  return ret_val;
}

/** Check if any of the preprocessed ttcn file names with the preprocessed
 * (TTCN-3) suffix is equal to any other file given in the \a makefile */
static void check_preprocessed_filename_collision(
  struct makefile_struct *makefile)
{
  size_t i;
  if (makefile->nTTCN3PPModules == 0) {
    WARNING("TTCN-3 preprocessing (option `-p') is enabled, but no TTCN-3 "
      "files to be preprocessed were given for the Makefile.");
  }
  for (i = 0; i < makefile->nTTCN3PPModules; i++) {
    const struct module_struct *pp_module = makefile->TTCN3PPModules + i;
    /* name of the intermediate preprocessed file */
    char *preprocessed_name = get_preprocessed_file_name(pp_module->file_name);
    size_t j;
    for (j = 0; j < makefile->nTTCN3Modules; j++) {
      struct module_struct *module = makefile->TTCN3Modules + j;
      if (is_same_file(pp_module->dir_name, preprocessed_name,
	module->dir_name, module->file_name)) {
	if (is_same_module(pp_module->module_name, module->module_name)) {
          /* same file with the same module */
	  char *pp_pathname = compose_path_name(pp_module->dir_name,
	    pp_module->file_name);
	  char *m_pathname = compose_path_name(module->dir_name,
	    module->file_name);
	  WARNING("File `%s' containing TTCN-3 module `%s' is generated by "
	    "the preprocessor from `%s'. Removing the file from the list of "
	    "normal TTCN-3 modules.", m_pathname, module->module_name,
	    pp_pathname);
	  Free(pp_pathname);
	  Free(m_pathname);
	  Free(module->dir_name);
	  Free(module->file_name);
	  Free(module->module_name);
	  makefile->nTTCN3Modules--;
	  memmove(module, module + 1, (makefile->nTTCN3Modules - j) *
	    sizeof(*makefile->TTCN3Modules));
	  makefile->TTCN3Modules =
	    (struct module_struct*)Realloc(makefile->TTCN3Modules,
	    makefile->nTTCN3Modules * sizeof(*makefile->TTCN3Modules));
	} else {
          /* same file with different module */
	  char *pp_pathname = compose_path_name(pp_module->dir_name,
	    pp_module->file_name);
	  char *m_pathname = compose_path_name(module->dir_name,
	    module->file_name);
	  ERROR("Preprocessed intermediate file of `%s' (module `%s') clashes "
	    "with file `%s' containing TTCN-3 module `%s'.", pp_pathname,
	    pp_module->module_name, m_pathname, module->module_name);
	  Free(pp_pathname);
	  Free(m_pathname);
	}
      } else if (is_same_module(pp_module->module_name, module->module_name)) {
        /* different file with the same module */
	char *pp_pathname = compose_path_name(pp_module->dir_name,
	  pp_module->file_name);
	char *m_pathname = compose_path_name(module->dir_name,
	  module->file_name);
	ERROR("Both files `%s' and `%s' contain TTCN-3 module `%s'.",
	  pp_pathname, m_pathname, pp_module->module_name);
	Free(pp_pathname);
	Free(m_pathname);
      }
    }
    for (j = 0; j < makefile->nASN1Modules; j++) {
      struct module_struct *module = makefile->ASN1Modules + j;
      if (is_same_file(pp_module->dir_name, preprocessed_name,
	module->dir_name, module->file_name)) {
	char *pp_pathname = compose_path_name(pp_module->dir_name,
	  pp_module->file_name);
	char *m_pathname = compose_path_name(module->dir_name,
	  module->file_name);
	ERROR("Preprocessed intermediate file of `%s' (module `%s') clashes "
	  "with file `%s' containing ASN.1 module `%s'.", pp_pathname,
	  pp_module->module_name, m_pathname, module->module_name);
	Free(pp_pathname);
	Free(m_pathname);
      }
    }
    for (j = 0; j < makefile->nOtherFiles; j++) {
      char *dir_name = get_dir_name(makefile->OtherFiles[j],
                                    makefile->working_dir);
      char *file_name = get_file_from_path(makefile->OtherFiles[j]);
      if (is_same_file(pp_module->dir_name, preprocessed_name, dir_name,
	file_name)) {
	char *pp_pathname = compose_path_name(pp_module->dir_name,
	  pp_module->file_name);
	ERROR("Preprocessed intermediate file of `%s' (module `%s') clashes "
	  "with other file `%s'.", pp_pathname, pp_module->module_name,
	  makefile->OtherFiles[j]);
	Free(pp_pathname);
      }
      Free(dir_name);
      Free(file_name);
    }
    Free(preprocessed_name);
  }
}

/** Checks the name clash between existing module \a module and newly added
 * module with parameters \a path_name, \a dir_name, \a file_name,
 * \a module_name. Both the existing and the new module shall be of the same
 * kind, parameter \a kind shall contain the respective string (either "ASN.1"
 * or "TTCN-3"). If a clash is found the parameters of the new module except
 * \a path_name are deallocated and TRUE is returned. Otherwise FALSE is
 * returned. */
static boolean check_module_clash_same(const struct module_struct *module,
  const char *kind, const char *path_name, char *dir_name, char *file_name,
  char *module_name)
{
  if (is_same_module(module_name, module->module_name)) {
    if (is_same_file(dir_name, file_name,
        module->dir_name, module->file_name)) {
      /* the same file was given twice: just issue a warning */
      WARNING("File `%s' was given more than once for the Makefile.",
              path_name);
    } else {
      /* two different files contain the same module: this cannot be
       * resolved as the generated C++ files will clash */
      char *path_name1 = compose_path_name(module->dir_name,
	module->file_name);
      char *path_name2 = compose_path_name(dir_name, file_name);
      ERROR("Both files `%s' and `%s' contain %s module `%s'.",
	path_name1, path_name2, kind, module_name);
      Free(path_name1);
      Free(path_name2);
    }
    Free(file_name);
    Free(dir_name);
    Free(module_name);
    return TRUE;
  } else return FALSE;
}

/** Checks the name clash between existing module \a module and newly added
 * module with parameters \a dir_name, \a file_name, \a module_name. The two
 * modules shall be of different kinds (one is ASN.1, the other is TTCN-3).
 * Parameters \a kind1 and \a kind2 shall contain the respective strings. If a
 * clash is found the parameters of the new module are deallocated and TRUE is
 * returned. Otherwise FALSE is returned. */
static boolean check_module_clash_different(const struct module_struct *module,
  const char *kind1, char *dir_name, char *file_name, char *module_name,
  const char *kind2)
{
  if (is_same_module(module_name, module->module_name)) {
    /* two different files contain the same module: this cannot be resolved
     * as the generated C++ files will clash */
    char *path_name1 = compose_path_name(module->dir_name, module->file_name);
    char *path_name2 = compose_path_name(dir_name, file_name);
    ERROR("File `%s' containing %s module `%s' and file `%s' containing "
      "%s module `%s' cannot be used together in the same Makefile.",
      path_name1, kind1, module->module_name, path_name2, kind2, module_name);
    Free(path_name1);
    Free(path_name2);
    Free(file_name);
    Free(dir_name);
    Free(module_name);
    return TRUE;
  } else return FALSE;
}

/** Adds a TTCN-3 module to Makefile descriptor structure \a makefile.
 * The name of the TTCN-3 source file is \a path_name, the module identifier
 * is \a module_name. It is checked whether a file or module with the same name
 * already exists in \a makefile and an appropriate warning or error is
 * reported. */
static void add_ttcn3_module(struct makefile_struct *makefile,
  const char *path_name, char *module_name)
{
  struct module_struct *module;
  char *dir_name = get_dir_name(path_name, makefile->working_dir);
  char *file_name = get_file_from_path(path_name);
  const char *suffix = get_suffix(file_name);
  size_t i;
  boolean is_preprocessed = FALSE;

  if (suffix != NULL) {
    if (!strcmp(suffix, "ttcnpp")) {
      if (makefile->preprocess) is_preprocessed = TRUE;
      else WARNING("The suffix of TTCN-3 file `%s' indicates that it should be "
        "preprocessed, but TTCN-3 preprocessing is not enabled. The file "
        "will be added to the list of normal TTCN-3 modules in the Makefile.",
        path_name);
    } else if (!strcmp(suffix, "ttcnin")) {
      WARNING("The suffix of file `%s' indicates that it should be a "
        "preprocessor include file, but it contains a TTCN-3 module named `%s'. "
        "The file will be added to the list of normal TTCN-3 modules in the "
        "Makefile.", path_name, module_name);
    }
  }

  for (i = 0; i < makefile->nASN1Modules; i++) {
    if (check_module_clash_different(makefile->ASN1Modules + i, "ASN.1",
      dir_name, file_name, module_name, "TTCN-3")) return;
  }
  /* never entered if suffix is NULL */
  if (is_preprocessed) {
    char *file_prefix;
    for (i = 0; i < makefile->nTTCN3PPModules; i++) {
      if (check_module_clash_same(makefile->TTCN3PPModules + i, "TTCN-3",
          path_name, dir_name, file_name, module_name)) return;
    }
    /* clashes with normal TTCN-3 modules will be checked (and maybe resolved)
     * in \a check_preprocessed_filename_collision() */
    /* add it to the list of TTCN-3 modules to be preprocessed */
    makefile->TTCN3PPModules = (struct module_struct*)
      Realloc(makefile->TTCN3PPModules,
        (makefile->nTTCN3PPModules + 1) * sizeof(*makefile->TTCN3PPModules));
    module = makefile->TTCN3PPModules + makefile->nTTCN3PPModules;
    makefile->nTTCN3PPModules++;
    module->dir_name = dir_name;
    module->file_name = file_name;
    module->module_name = module_name;
    file_prefix = cut_suffix(file_name);
    if (!strcmp(file_prefix, module_name)) module->is_regular = TRUE;
    else module->is_regular = FALSE;
    Free(file_prefix);
  } else {
    /* the file is not preprocessed */
    for (i = 0; i < makefile->nTTCN3Modules; i++) {
      if (check_module_clash_same(makefile->TTCN3Modules + i, "TTCN-3",
          path_name, dir_name, file_name, module_name)) return;
    }
    /* clashes with preprocessed TTCN-3 modules will be checked (and maybe
     * resolved) in \a check_preprocessed_filename_collision() */
    /* add it to the list of normal TTCN-3 modules */
    makefile->TTCN3Modules = (struct module_struct*)
      Realloc(makefile->TTCN3Modules,
        (makefile->nTTCN3Modules + 1) * sizeof(*makefile->TTCN3Modules));
    module = makefile->TTCN3Modules + makefile->nTTCN3Modules;
    makefile->nTTCN3Modules++;
    module->dir_name = dir_name;
    module->file_name = file_name;
    module->module_name = module_name;
    if (suffix != NULL && !strcmp(suffix, "ttcn")) {
      char *file_prefix = cut_suffix(file_name);
      if (!strcmp(file_prefix, module_name)) module->is_regular = TRUE;
      else module->is_regular = FALSE;
      Free(file_prefix);
    } else {
      module->is_regular = FALSE;
    }
  }
}

/** ASN.1 filename shall contain no hyphen */
static boolean is_valid_asn1_filename(const char* file_name)
{
  if (0 == strchr(file_name, '-')) {
    return TRUE;
  }
  return FALSE;
}

/** Adds an ASN.1 module to Makefile descriptor structure \a makefile.
 * The name of the ASN.1 source file is \a path_name, the module identifier
 * is \a module_name. It is checked whether a file or module with the same name
 * already exists in \a makefile and an appropriate warning or error is
 * reported. */
static void add_asn1_module(struct makefile_struct *makefile,
  const char *path_name, char *module_name)
{
  struct module_struct *module;
  char *dir_name = get_dir_name(path_name, makefile->working_dir);
  char *file_name = get_file_from_path(path_name);
  const char *suffix = get_suffix(file_name);
  size_t i;
  for (i = 0; i < makefile->nASN1Modules; i++) {
    if (check_module_clash_same(makefile->ASN1Modules + i, "ASN.1", path_name,
      dir_name, file_name, module_name)) return;
  }
  for (i = 0; i < makefile->nTTCN3Modules; i++) {
    if (check_module_clash_different(makefile->TTCN3Modules + i, "TTCN-3",
      dir_name, file_name, module_name, "ASN.1")) return;
  }
  if (makefile->preprocess) {
    for (i = 0; i < makefile->nTTCN3PPModules; i++) {
      if (check_module_clash_different(makefile->TTCN3PPModules + i, "TTCN-3",
	dir_name, file_name, module_name, "ASN.1")) return;
    }
  }
  makefile->ASN1Modules = (struct module_struct*)
    Realloc(makefile->ASN1Modules,
      (makefile->nASN1Modules + 1) * sizeof(*makefile->ASN1Modules));
  module = makefile->ASN1Modules + makefile->nASN1Modules;
  makefile->nASN1Modules++;
  module->dir_name = dir_name;
  module->file_name = file_name;
  module->module_name = module_name;
  if (suffix != NULL && !strcmp(suffix, "asn")) {
    char *file_prefix = cut_suffix(file_name);
    /* replace all '_' with '-' in file name prefix */
    for (i = 0; file_prefix[i] != '\0'; i++)
      if (file_prefix[i] == '_') file_prefix[i] = '-';
    if (!strcmp(file_prefix, module_name)) module->is_regular = TRUE;
    else module->is_regular = FALSE;
    Free(file_prefix);
  } else {
    module->is_regular = FALSE;
  }
}

/** Adds the file named \a path_name to the list of files pointed by \a list_ptr
 * and \a list_size. The suffix or contents of \a path_name are not examined,
 * only duplicate entries are checked. In case of duplicate entries warning is
 * reported only if argument \a report_warning is set to TRUE. */
static void add_path_to_list(size_t *list_size, char ***list_ptr,
  const char *path_name, const char *working_dir, boolean report_warning)
{
  size_t i;
  char *dir_name = get_dir_name(path_name, working_dir);
  char *file_name = get_file_from_path(path_name);
  char *canonized_path_name = compose_path_name(dir_name, file_name);
  Free(dir_name);
  Free(file_name);
  for (i = 0; i < *list_size; i++) {
    if (!strcmp(canonized_path_name, (*list_ptr)[i])) {
      if (report_warning) WARNING("File `%s' was given more than once for the "
Elemer Lelik's avatar
Elemer Lelik committed
945
                                  "Makefile.", path_name);
Elemer Lelik's avatar
Elemer Lelik committed
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
      Free(canonized_path_name);
      return;
    }
  }
  *list_ptr = (char**)Realloc(*list_ptr, (*list_size + 1) * sizeof(**list_ptr));
  (*list_ptr)[*list_size] = canonized_path_name;
  (*list_size)++;
}

/** Adds a C/C++ header or source file or an other file named \a path_name to
 * Makefile descriptor structure \a makefile. The file is classified based on
 * its suffix and not by content. If the file clashes with existing files or
 * modules the appropriate warning or error is generated. */
static void add_user_file(struct makefile_struct *makefile,
  const char *path_name)
{
  const char *suffix = get_suffix(path_name);
  if (suffix != NULL) {
    if (!strcmp(suffix, "ttcn") || !strcmp(suffix, "ttcn3") ||
Elemer Lelik's avatar
Elemer Lelik committed
965
        !strcmp(suffix, "3mp") || !strcmp(suffix, "ttcnpp")) {
Elemer Lelik's avatar
Elemer Lelik committed
966
967
968
      /* The file content was already checked. Since it doesn't look like
       * a valid TTCN-3 file, these suffixes are suspect */
      WARNING("File `%s' does not contain a valid TTCN-3 module. "
Elemer Lelik's avatar
Elemer Lelik committed
969
970
971
              "It will be added to the Makefile as other file.", path_name);
    }
    else if (!strcmp(suffix, "ttcnin")) {
Elemer Lelik's avatar
Elemer Lelik committed
972
973
      /* this is a TTCN-3 include file */
      if (makefile->preprocess) {
Elemer Lelik's avatar
Elemer Lelik committed
974
975
976
977
978
979
980
981
        add_path_to_list(&makefile->nTTCN3IncludeFiles,
        &makefile->TTCN3IncludeFiles, path_name, makefile->working_dir, TRUE);
        return;
      } 
      else {
        WARNING("The suffix of file `%s' indicates that it is a TTCN-3 "
                "include file, but TTCN-3 preprocessing is not enabled. The file "
                "will be added to the Makefile as other file.", path_name);
Elemer Lelik's avatar
Elemer Lelik committed
982
      }
Elemer Lelik's avatar
Elemer Lelik committed
983
984
    } 
    else if (!strcmp(suffix, "asn") || !strcmp(suffix, "asn1")) {
Elemer Lelik's avatar
Elemer Lelik committed
985
986
987
      /* The file content was already checked. Since it doesn't look like
       * a valid ASN.1 file, these suffixes are suspect */
      WARNING("File `%s' does not contain a valid ASN.1 module. "
Elemer Lelik's avatar
Elemer Lelik committed
988
989
990
              "It will be added to the Makefile as other file.", path_name);
    }
    else if (!strcmp(suffix, "cc") || !strcmp(suffix, "c") || !strcmp(suffix, "cpp")) {
Elemer Lelik's avatar
Elemer Lelik committed
991
992
993
994
995
996
997
      /* this is a source file */
      char *dir_name = get_dir_name(path_name, makefile->working_dir);
      char *file_name = get_file_from_path(path_name);
      char *file_prefix = cut_suffix(file_name);
      struct user_struct *user;
      size_t i;
      for (i = 0; i < makefile->nUserFiles; i++) {
Elemer Lelik's avatar
Elemer Lelik committed
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
        user = makefile->UserFiles + i;
        if (!strcmp(file_prefix, user->file_prefix)) {
          if (user->source_name != NULL) {
          /* the source file is already present */
            if (is_same_file(dir_name, file_name,
              user->dir_name, user->source_name)) {
              WARNING("File `%s' was given more than once for the Makefile.", path_name);
            }
            else {
              char *path_name1 = compose_path_name(user->dir_name, user->source_name);
              char *path_name2 = compose_path_name(dir_name, file_name);
              ERROR("C/C++ source files `%s' and `%s' cannot be used together "
                    "in the same Makefile.", path_name1, path_name2);
              Free(path_name1);
              Free(path_name2);
            }
          }
          else {
            /* a header file with the same prefix is already present */
            if (is_same_directory(dir_name, user->dir_name)) {
              user->source_name = file_name;
              file_name = NULL;
              if (!strcmp(suffix, "cc") || !strcmp(suffix, "cpp")) 
                user->has_cc_suffix = TRUE;
            }
            else {
              char *path_name1 = compose_path_name(dir_name, file_name);
              char *path_name2 = compose_path_name(user->dir_name, user->header_name);
              ERROR("C/C++ source file `%s' cannot be used together with "
              "header file `%s' in the same Makefile.", path_name1,
              path_name2);
              Free(path_name1);
              Free(path_name2);
            }
          }
          Free(dir_name);
          Free(file_name);
          Free(file_prefix);
          return;
        }
Elemer Lelik's avatar
Elemer Lelik committed
1038
1039
      }
      makefile->UserFiles = (struct user_struct*)Realloc(makefile->UserFiles,
Elemer Lelik's avatar
Elemer Lelik committed
1040
      (makefile->nUserFiles + 1) * sizeof(*makefile->UserFiles));
Elemer Lelik's avatar
Elemer Lelik committed
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
      user = makefile->UserFiles + makefile->nUserFiles;
      makefile->nUserFiles++;
      user->dir_name = dir_name;
      user->file_prefix = file_prefix;
      user->header_name = NULL;
      user->source_name = file_name;
      user->has_hh_suffix = FALSE;
      if (!strcmp(suffix, "cc") || !strcmp(suffix, "cpp")) user->has_cc_suffix = TRUE;
      else user->has_cc_suffix = FALSE;
      return;
Elemer Lelik's avatar
Elemer Lelik committed
1051
1052
    }
    else if (!strcmp(suffix, "hh") || !strcmp(suffix, "h")) {
Elemer Lelik's avatar
Elemer Lelik committed
1053
1054
1055
1056
1057
1058
1059
      /* this is a header file */
      char *dir_name = get_dir_name(path_name, makefile->working_dir);
      char *file_name = get_file_from_path(path_name);
      char *file_prefix = cut_suffix(file_name);
      struct user_struct *user;
      size_t i;
      for (i = 0; i < makefile->nUserFiles; i++) {
Elemer Lelik's avatar
Elemer Lelik committed
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
        user = makefile->UserFiles + i;
        if (!strcmp(file_prefix, user->file_prefix)) {
          if (user->header_name != NULL) {
          /* the header file is already present */
            if (is_same_file(dir_name, file_name, user->dir_name, user->header_name)) {
              WARNING("File `%s' was given more than once for the Makefile.", path_name);
            }
            else {
              char *path_name1 = compose_path_name(user->dir_name, user->header_name);
              char *path_name2 = compose_path_name(dir_name, file_name);
              ERROR("C/C++ header files `%s' and `%s' cannot be used together "
              "in the same Makefile.", path_name1, path_name2);
              Free(path_name1);
              Free(path_name2);
            }
          }
          else {
          /* a source file with the same prefix is already present */
            if (is_same_directory(dir_name, user->dir_name)) {
              user->header_name = file_name;
              file_name = NULL;
              if (!strcmp(suffix, "hh") || !strcmp(suffix, "hpp")) 
                user->has_hh_suffix = TRUE;
            }
            else {
              char *path_name1 = compose_path_name(dir_name, file_name);
              char *path_name2 = compose_path_name(user->dir_name, user->source_name);
              ERROR("C/C++ header file `%s' cannot be used together with "
                    "source file `%s' in the same Makefile.", path_name1, path_name2);
              Free(path_name1);
              Free(path_name2);
            }
          }
          Free(dir_name);
          Free(file_name);
          Free(file_prefix);
          return;
        }
Elemer Lelik's avatar
Elemer Lelik committed
1098
1099
      }
      makefile->UserFiles = (struct user_struct*)Realloc(makefile->UserFiles,
Elemer Lelik's avatar
Elemer Lelik committed
1100
        (makefile->nUserFiles + 1) * sizeof(*makefile->UserFiles));
Elemer Lelik's avatar
Elemer Lelik committed
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
      user = makefile->UserFiles + makefile->nUserFiles;
      makefile->nUserFiles++;
      user->dir_name = dir_name;
      user->file_prefix = file_prefix;
      user->header_name = file_name;
      user->source_name = NULL;
      if (!strcmp(suffix, "hh") || !strcmp(suffix, "hpp")) user->has_hh_suffix = TRUE;
      else user->has_hh_suffix = FALSE;
      user->has_cc_suffix = FALSE;
      return;
    }
  } /* end if (suffix != NULL) */
  /* treat the file as other file if it was not handled yet */
  add_path_to_list(&makefile->nOtherFiles, &makefile->OtherFiles, path_name,
    makefile->working_dir, TRUE);
}

/** Removes the generated C++ header and/or source files of module \a module
 * from Makefile descriptor structure \a makefile. A warning is displayed if
 * such file is found. */
static void drop_generated_files(struct makefile_struct *makefile,
  const struct module_struct *module)
{
  char *module_name = mcopystr(module->module_name);
  size_t i;
  /* transform all '-' characters in ASN.1 module name to '_' */
  for (i = 0; module_name[i] != '\0'; i++)
    if (module_name[i] == '-') module_name[i] = '_';
  for (i = 0; i < makefile->nUserFiles; i++) {
    struct user_struct *user = makefile->UserFiles + i;
    if (!strcmp(module_name, user->file_prefix)) {
      char *m_pathname = compose_path_name(module->dir_name, module->file_name);
      /** Note: if central storage is used the generated C++ files are placed
       * into the same directory as the TTCN-3/ASN.1 modules, otherwise the
       * files are generated into the working directory. */
      boolean is_same_dir = is_same_directory(user->dir_name,
	makefile->central_storage ? module->dir_name : NULL);
      if (user->header_name != NULL) {
	char *u_pathname = compose_path_name(user->dir_name,
	  user->header_name);
	if (is_same_dir && user->has_hh_suffix) {
	  WARNING("Header file `%s' is generated from module `%s' (file `%s'). "
	    "Removing it from the list of user files.", u_pathname,
	    module->module_name, m_pathname);
	} else {
	  ERROR("Header file `%s' cannot be used together with module `%s' "
	    "(file `%s') in the same Makefile.", u_pathname,
	    module->module_name, m_pathname);
	}
	Free(u_pathname);
      }
      if (user->source_name != NULL) {
	char *u_pathname = compose_path_name(user->dir_name,
	  user->source_name);
	if (is_same_dir && user->has_cc_suffix) {
	  WARNING("Source file `%s' is generated from module `%s' (file "
	    "`%s'). Removing it from the list of user files.", u_pathname,
	    module->module_name, m_pathname);
	} else {
	  ERROR("Source file `%s' cannot be used together with module "
	    "`%s' (file `%s') in the same Makefile.", u_pathname,
	    module->module_name, m_pathname);
	}
	Free(u_pathname);
      }
      Free(m_pathname);
      Free(user->dir_name);
      Free(user->file_prefix);
      Free(user->header_name);
      Free(user->source_name);
      makefile->nUserFiles--;
      memmove(user, user + 1, (makefile->nUserFiles - i) *
	sizeof(*makefile->UserFiles));
      makefile->UserFiles = (struct user_struct*)Realloc(makefile->UserFiles,
	makefile->nUserFiles * sizeof(*makefile->UserFiles));
      break;
    }
  }
  Free(module_name);
}

/** Drops all C++ header and source files of the Makefile descriptor structure
 * \a makefile that are generated from its TTCN-3 or ASN.1 modules. */
static void filter_out_generated_files(struct makefile_struct *makefile)
{
  size_t i;
  for (i = 0; i < makefile->nTTCN3Modules; i++) {
    drop_generated_files(makefile, makefile->TTCN3Modules + i);
  }
  if (makefile->preprocess) {
    for (i = 0; i < makefile->nTTCN3PPModules; i++) {
      drop_generated_files(makefile, makefile->TTCN3PPModules + i);
    }
  }
  for (i = 0; i < makefile->nASN1Modules; i++) {
    drop_generated_files(makefile, makefile->ASN1Modules + i);
  }
}

/** Completes the list of user C/C++ header and source files in \a makefile.
 * If only the source file was given the function looks for the corresponding
 * header file or vice versa. */
static void complete_user_files(const struct makefile_struct *makefile)
{
  size_t i;
  for (i = 0; i < makefile->nUserFiles; i++) {
    struct user_struct *user = makefile->UserFiles + i;
    if (user->header_name == NULL) {
      static const char * const suffix_list[] = { "hh", "h", "hpp", NULL };
      const char * const *suffix_ptr;
      for (suffix_ptr = suffix_list; *suffix_ptr != NULL; suffix_ptr++) {
Elemer Lelik's avatar
Elemer Lelik committed
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
        char *file_name = mprintf("%s.%s", user->file_prefix, *suffix_ptr);
        char *path_name = compose_path_name(user->dir_name, file_name);
        if (get_path_status(path_name) == PS_FILE) {
          Free(path_name);
          user->header_name = file_name;
          if (!strcmp(*suffix_ptr, "hh") || !strcmp(*suffix_ptr, "hpp"))
            user->has_hh_suffix = TRUE;
          break;
        }
        Free(file_name);
        Free(path_name);
Elemer Lelik's avatar
Elemer Lelik committed
1223
      }
Elemer Lelik's avatar
Elemer Lelik committed
1224
1225
    }
    else if (user->source_name == NULL) {
Elemer Lelik's avatar
Elemer Lelik committed
1226
1227
1228
      static const char * const suffix_list[] = { "cc", "c", "cpp", NULL };
      const char * const *suffix_ptr;
      for (suffix_ptr = suffix_list; *suffix_ptr != NULL; suffix_ptr++) {
Elemer Lelik's avatar
Elemer Lelik committed
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
        char *file_name = mprintf("%s.%s", user->file_prefix, *suffix_ptr);
        char *path_name = compose_path_name(user->dir_name, file_name);
        if (get_path_status(path_name) == PS_FILE) {
          Free(path_name);
          user->source_name = file_name;
          if (!strcmp(*suffix_ptr, "cc") || !strcmp(*suffix_ptr, "cpp"))
            user->has_cc_suffix = TRUE;
          break;
        }
        Free(file_name);
        Free(path_name);
Elemer Lelik's avatar
Elemer Lelik committed
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
      }
    }
  }
}

/** Converts the directory name pointed by \a dir_ptr to a relative pathname
 * based on \a working_dir. The original directory name is deallocated and
 * replaced with a new string. Nothing happens if \a dir_ptr points to NULL. */
static void replace_dir_with_relative(char **dir_ptr, const char *working_dir)
{
  if (*dir_ptr != NULL) {
    char *rel_dir = get_relative_dir(*dir_ptr, working_dir);
    Free(*dir_ptr);
    *dir_ptr = rel_dir;
  }
}

/** Converts the directory part of path name pointed by \a path_ptr to a relative
 * pathname based on \a working_dir. The original path name is deallocated and
 * replaced with a new string. */
static void convert_path_to_relative(char **path_ptr, const char *working_dir)
{
  char *dir_name = get_dir_name(*path_ptr, working_dir);
  if (dir_name != NULL) {
    char *file_name = get_file_from_path(*path_ptr);
    replace_dir_with_relative(&dir_name, working_dir);
    Free(*path_ptr);
    *path_ptr = compose_path_name(dir_name, file_name);
    Free(file_name);
    Free(dir_name);
  }
}

/** Converts all directories used by the Makefile descriptor structure
 * \a makefile to relative pathnames based on the working directory stored in
 * \a makefile. */
static void convert_dirs_to_relative(struct makefile_struct *makefile)
{
  size_t i;
  for (i = 0; i < makefile->nTTCN3Modules; i++) {
    replace_dir_with_relative(&makefile->TTCN3Modules[i].dir_name,
      makefile->working_dir);
  }
  if (makefile->preprocess) {
    for (i = 0; i < makefile->nTTCN3PPModules; i++) {
      replace_dir_with_relative(&makefile->TTCN3PPModules[i].dir_name,
                                makefile->working_dir);
    }
    for (i = 0; i < makefile->nTTCN3IncludeFiles; i++) {
      convert_path_to_relative(makefile->TTCN3IncludeFiles + i,
	makefile->working_dir);
    }
  }
  for (i = 0; i < makefile->nASN1Modules; i++) {
    replace_dir_with_relative(&makefile->ASN1Modules[i].dir_name,
      makefile->working_dir);
  }
  for (i = 0; i < makefile->nUserFiles; i++) {
    replace_dir_with_relative(&makefile->UserFiles[i].dir_name,
      makefile->working_dir);
  }
  for (i = 0; i < makefile->nOtherFiles; i++) {
    convert_path_to_relative(makefile->OtherFiles + i, makefile->working_dir);
  }
  if (makefile->ets_name != NULL)
    convert_path_to_relative(&makefile->ets_name, makefile->working_dir);
}

/* Returns whether the string \a file_name contains special characters. */
static boolean has_special_chars(const char *file_name)
{
  if (file_name != NULL) {
    size_t i;
    for (i = 0; ; i++) {
      int c = (unsigned char)file_name[i];
      switch (c) {
      case '\0':
	return FALSE;
      case ' ':
      case '*':
      case '?':
      case '[':
      case ']':
      case '<':
      case '=':
      case '>':
      case '|':
      case '&':
      case '$':
      case '%':
      case '{':
      case '}':
      case ';':
      case ':':
      case '(':
      case ')':
      case '#':
      case '!':
      case '\'':
      case '"':
      case '`':
      case '\\':
	return TRUE;
      default:
	if (!isascii(c) || !isprint(c)) return TRUE;
	break;
      }
    }
  }
  return FALSE;
}

/** Checks whether the path name composed of \a dir_name and \a file_name
 * contains special characters that are not allowed in the Makefile. Parameter
 * \a what contains the description of the corresponding file. */
static void check_special_chars_in_path(const char *dir_name,
  const char *file_name, const char *what)
{
  if (has_special_chars(dir_name) || has_special_chars(file_name)) {
    char *path_name = compose_path_name(dir_name, file_name);
    ERROR("The name of %s `%s' contains special characters that cannot be "
      "handled properly by the `make' utility and/or the shell.", what,
      path_name);
    Free(path_name);
  }
}

/** Checks whether the directory names or file names that will be used in the
 * generated Makefile contain special characters that cannot be handled by the
 * "make" utility. */
static void check_special_chars(const struct makefile_struct *makefile)
{
  size_t i;
  for (i = 0; i < makefile->nTTCN3Modules; i++) {
    check_special_chars_in_path(makefile->TTCN3Modules[i].dir_name,
      makefile->TTCN3Modules[i].file_name, "TTCN-3 file");
  }
  if (makefile->preprocess) {
    for (i = 0; i < makefile->nTTCN3PPModules; i++) {
      check_special_chars_in_path(makefile->TTCN3PPModules[i].dir_name,
	makefile->TTCN3PPModules[i].file_name,
	"TTCN-3 file to be preprocessed");
    }
  }
  for (i = 0; i < makefile->nASN1Modules; i++) {
    check_special_chars_in_path(makefile->ASN1Modules[i].dir_name,
      makefile->ASN1Modules[i].file_name, "ASN.1 file");
  }
  for (i = 0; i < makefile->nUserFiles; i++) {
    const struct user_struct *user = makefile->UserFiles + i;
    if (user->source_name != NULL)
      check_special_chars_in_path(user->dir_name, user->source_name,
	"C/C++ source file");
    else check_special_chars_in_path(user->dir_name, user->header_name,
	"C/C++ header file");
  }
  for (i = 0; i < makefile->nOtherFiles; i++) {
    check_special_chars_in_path(NULL, makefile->OtherFiles[i], "other file");
  }
}

/** Adds base directory \a dir_name to Makefile descriptor structure
 * \a makefile. Flag \a has_modules indicates whether \a dir_name contains
 * TTCN-3 and/or ASN.1 modules. The new directory is ignored if it is already
 * added to \a makefile. */
static void add_base_dir(struct makefile_struct *makefile,
  const char *dir_name, boolean has_modules)
{
  struct base_dir_struct *base_dir;
  if (dir_name != NULL) {
    size_t i;
    for (i = 0; i < makefile->nBaseDirs; i++) {
      base_dir = makefile->BaseDirs + i;
      if (!strcmp(dir_name, base_dir->dir_name)) {
	if (has_modules) base_dir->has_modules = TRUE;
	return;
      }
    }
    makefile->BaseDirs = (struct base_dir_struct*)Realloc(makefile->BaseDirs,
      (makefile->nBaseDirs + 1) * sizeof(*makefile->BaseDirs));
    base_dir = makefile->BaseDirs + makefile->nBaseDirs;
    makefile->nBaseDirs++;
    base_dir->dir_name = dir_name;
    base_dir->has_modules = has_modules;
  }
}

/** Collects all directories that are used in the Makefile descriptor structure
 * \a makefile in order to use pre-compiled files from them. */
static void collect_base_dirs(struct makefile_struct *makefile)
{
  size_t i;
  for (i = 0; i < makefile->nTTCN3Modules; i++) {
    add_base_dir(makefile, makefile->TTCN3Modules[i].dir_name, TRUE);
  }
  if (makefile->preprocess) {
    for (i = 0; i < makefile->nTTCN3PPModules; i++) {
      add_base_dir(makefile, makefile->TTCN3PPModules[i].dir_name, TRUE);
    }
  }
  for (i = 0; i < makefile->nASN1Modules; i++) {
    add_base_dir(makefile, makefile->ASN1Modules[i].dir_name, TRUE);
  }
  for (i = 0; i < makefile->nUserFiles; i++) {
    add_base_dir(makefile, makefile->UserFiles[i].dir_name, FALSE);
  }
  if (makefile->nBaseDirs == 0) {
    WARNING("Usage of pre-compiled files from central storage (option `-c') "
      "is enabled, but all given files are located in the current working "
      "directory.");
  }
}

/** Checks whether the TTCN-3, ASN.1 and C++ files follow the default naming
 * convention and sets the appropriate flags accordingly. */
static void check_naming_convention(struct makefile_struct *makefile)
{
  /* initially set all flags to true */
  makefile->TTCN3ModulesRegular = TRUE;
  makefile->BaseTTCN3ModulesRegular = TRUE;
  makefile->ASN1ModulesRegular = TRUE;
  makefile->BaseASN1ModulesRegular = TRUE;
  makefile->UserHeadersRegular = TRUE;
  makefile->UserSourcesRegular = TRUE;
  makefile->BaseUserHeadersRegular = TRUE;
  makefile->BaseUserSourcesRegular = TRUE;
  if (makefile->central_storage) {
    /* this project (Makefile) will use pre-compiled files from other
       directories */
    size_t i;
    for (i = 0; i < makefile->nTTCN3Modules; i++) {
      const struct module_struct *module = makefile->TTCN3Modules + i;
      if (module->dir_name != NULL) {
Elemer Lelik's avatar
Elemer Lelik committed
1473
1474
1475
1476
        if (!module->is_regular) makefile->BaseTTCN3ModulesRegular = FALSE;
      } 
      else {
        if (!module->is_regular) makefile->TTCN3ModulesRegular = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
1477
1478
      }
      if (!makefile->TTCN3ModulesRegular && !makefile->BaseTTCN3ModulesRegular)
Elemer Lelik's avatar
Elemer Lelik committed
1479
        break;
Elemer Lelik's avatar
Elemer Lelik committed
1480
1481
1482
    }
    /* ttcnpp files are ttcn files */
    if ((makefile->TTCN3ModulesRegular || makefile->BaseTTCN3ModulesRegular) &&
Elemer Lelik's avatar
Elemer Lelik committed
1483
         makefile->preprocess) {
Elemer Lelik's avatar
Elemer Lelik committed
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
      for (i = 0; i < makefile->nTTCN3PPModules; i++) {
        const struct module_struct *module = makefile->TTCN3PPModules + i;
        if (module->dir_name != NULL) {
          if (!module->is_regular) makefile->BaseTTCN3ModulesRegular = FALSE;
        } else {
          if (!module->is_regular) makefile->TTCN3ModulesRegular = FALSE;
        }
        if (!makefile->TTCN3ModulesRegular && !makefile->BaseTTCN3ModulesRegular)
        break;
      }
    }
    for (i = 0; i < makefile->nASN1Modules; i++) {
      const struct module_struct *module = makefile->ASN1Modules + i;
      if (module->dir_name != NULL) {
Elemer Lelik's avatar
Elemer Lelik committed
1498
1499
1500
1501
        if (!module->is_regular) makefile->BaseASN1ModulesRegular = FALSE;
      }
      else {
        if (!module->is_regular) makefile->ASN1ModulesRegular = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
1502
1503
      }
      if (!makefile->ASN1ModulesRegular && !makefile->BaseASN1ModulesRegular)
Elemer Lelik's avatar
Elemer Lelik committed
1504
        break;
Elemer Lelik's avatar
Elemer Lelik committed
1505
1506
1507
1508
    }
    for (i = 0; i < makefile->nUserFiles; i++) {
      const struct user_struct *user = makefile->UserFiles + i;
      if (user->dir_name != NULL) {
Elemer Lelik's avatar
Elemer Lelik committed
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
        if (!user->has_cc_suffix)
          makefile->BaseUserSourcesRegular = FALSE;
        if (!user->has_cc_suffix || !user->has_hh_suffix)
          makefile->BaseUserHeadersRegular = FALSE;
      }
      else {
        if (!user->has_cc_suffix)
          makefile->UserSourcesRegular = FALSE;
        if (!user->has_cc_suffix || !user->has_hh_suffix)
          makefile->UserHeadersRegular = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
1519
1520
      }
      if (!makefile->UserHeadersRegular && !makefile->UserSourcesRegular &&
Elemer Lelik's avatar
Elemer Lelik committed
1521
1522
          !makefile->BaseUserHeadersRegular &&
          !makefile->BaseUserSourcesRegular) break;
Elemer Lelik's avatar
Elemer Lelik committed
1523
1524
1525
1526
1527
1528
1529
    }
  } else {
    /* this project (Makefile) will-be stand-alone */
    size_t i;
    for (i = 0; i < makefile->nTTCN3Modules; i++) {
      const struct module_struct *module = makefile->TTCN3Modules + i;
      if (!module->is_regular || module->dir_name != NULL) {
Elemer Lelik's avatar
Elemer Lelik committed
1530
1531
        makefile->TTCN3ModulesRegular = FALSE;
        break;
Elemer Lelik's avatar
Elemer Lelik committed
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
      }
    }
    if (makefile->TTCN3ModulesRegular && makefile->preprocess) {
      for (i = 0; i < makefile->nTTCN3PPModules; i++) {
        const struct module_struct *module = makefile->TTCN3PPModules + i;
        if (!module->is_regular || module->dir_name != NULL) {
          makefile->TTCN3ModulesRegular = FALSE;
          break;
        }
      }
    }
    for (i = 0; i < makefile->nASN1Modules; i++) {
      const struct module_struct *module = makefile->ASN1Modules + i;
      if (!module->is_regular || module->dir_name != NULL) {
Elemer Lelik's avatar
Elemer Lelik committed
1546
1547
        makefile->ASN1ModulesRegular = FALSE;
        break;
Elemer Lelik's avatar
Elemer Lelik committed
1548
1549
1550
1551
1552
      }
    }
    for (i = 0; i < makefile->nUserFiles; i++) {
      const struct user_struct *user = makefile->UserFiles + i;
      if (!user->has_cc_suffix)
Elemer Lelik's avatar
Elemer Lelik committed
1553
        makefile->UserSourcesRegular = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
1554
      if (!user->has_cc_suffix || !user->has_hh_suffix)
Elemer Lelik's avatar
Elemer Lelik committed
1555
        makefile->UserHeadersRegular = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
1556
      if (!makefile->UserHeadersRegular && !makefile->UserSourcesRegular)
Elemer Lelik's avatar
Elemer Lelik committed
1557
        break;
Elemer Lelik's avatar
Elemer Lelik committed
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
    }
  }
}

/** Prints the name of the TTCN-3 or ASN.1 source file that belongs to module
 * \a module to file \a fp. */
static void print_file_name(FILE *fp, const struct module_struct *module)
{
  char *path_name = compose_path_name(module->dir_name, module->file_name);
  fprintf(fp, " %s", path_name);
  Free(path_name);
}

/** Prints the name of the preprocessed TTCN-3 source file that belongs to
 *  module \a module to file \a fp. */
static void print_preprocessed_file_name(FILE *fp,
                                         const struct module_struct *module)
{
  char *preprocessed_name = get_preprocessed_file_name(module->file_name);
  char *path_name = compose_path_name(module->dir_name, preprocessed_name);
  fprintf(fp, " %s", path_name);
  Free(path_name);
  Free(preprocessed_name);
}

/** Prints the name of the generated header, source or object file of module
 * \a module to file \a fp. The name of the directory is added only if
 * \a add_directory is TRUE. Parameter \a suffix shall be "hh", "cc", "hpp", "cpp" or "o". */
static void print_generated_file_name(FILE *fp,
  const struct module_struct *module, boolean add_directory, const char *suffix)
{
  char *file_name = mcopystr(module->module_name);
  /* replace '-' with '_' */
  size_t i;
  for (i = 0; file_name[i] != '\0'; i++)
    if (file_name[i] == '-') file_name[i] = '_';
  /* append the suffix */
  file_name = mputprintf(file_name, "%s", suffix);
  /* add the directory name if necessary */
  if (add_directory) {
    char *path_name = compose_path_name(module->dir_name, file_name);
    Free(file_name);
    file_name = path_name;
  }
  fprintf(fp, " %s", file_name);
  Free(file_name);
}

/** Prints the name of the user C/C++ header file of user module \a user if the
 * above file exists. */
static void print_header_name(FILE *fp, const struct user_struct *user)
{
  if (user->header_name != NULL) {
    char *path_name = compose_path_name(user->dir_name, user->header_name);
    fprintf(fp, " %s", path_name);
    Free(path_name);
  }
}

/** Prints the name of the user C/C++ source file of user module \a user if the
 * above file exists. */
static void print_source_name(FILE *fp, const struct user_struct *user)
{
  if (user->source_name != NULL) {
    char *path_name = compose_path_name(user->dir_name, user->source_name);
    fprintf(fp, " %s", path_name);
    Free(path_name);
  }
}

/** Prints the name of the user C/C++ object file of user module \a user if the
 * above file exists (i.e. the respective source file is present). */
static void print_object_name(FILE *fp, const struct user_struct *user)
{
  if (user->source_name != NULL) {
    char *file_name = mprintf("%s.o", user->file_prefix);
    char *path_name = compose_path_name(user->dir_name, file_name);
    Free(file_name);
    fprintf(fp, " %s", path_name);
    Free(path_name);
  }
}

static void print_shared_object_name(FILE *fp, const struct user_struct *user)
{
  if (user->source_name != NULL) {
    char *file_name = mprintf("%s.so", user->file_prefix);
    char *path_name = compose_path_name(user->dir_name, file_name);
    Free(file_name);
    fprintf(fp, " %s", path_name);
    Free(path_name);
  }
}
/** Prints the splitted files' names for a given module. */
static void print_splitted_file_names(FILE *fp,
  const struct makefile_struct *makefile, const struct module_struct *module)
{
  if (strcmp(makefile->code_splitting_mode, "-U type") == 0) {
    print_generated_file_name(fp, module, FALSE, "_seq.cc");
    print_generated_file_name(fp, module, FALSE, "_set.cc");
    print_generated_file_name(fp, module, FALSE, "_seqof.cc");
    print_generated_file_name(fp, module, FALSE, "_setof.cc");
    print_generated_file_name(fp, module, FALSE, "_union.cc");
  }
}

static void fprint_extra_targets(FILE* fp, struct string2_list* target_placement_list, const char* placement)
{
  struct string2_list* act_elem = target_placement_list;
  while (act_elem) {
    if (act_elem->str1 && act_elem->str2 && (strcmp(act_elem->str2,placement)==0)) {
      fprintf(fp, " %s", act_elem->str1);
    }
    act_elem = act_elem->next;
  }
}

#undef COMMENT_PREFIX
#define COMMENT_PREFIX "# "

/** Prints the Makefile based on structure \a makefile. */
static void print_makefile(struct makefile_struct *makefile)
{
Elemer Lelik's avatar
Elemer Lelik committed
1681
1682
1683
1684
1685
1686
1687
  boolean add_refd_prjs = FALSE;
  if (makefile->linkingStrategy && makefile->hierarchical) {
    add_refd_prjs = hasSubProject(makefile->project_name);
  }
  else {
    add_refd_prjs = makefile->sub_project_dirs && makefile->sub_project_dirs->str;
  }
Elemer Lelik's avatar
Elemer Lelik committed
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
  NOTIFY("Generating Makefile skeleton...");

  if (makefile->force_overwrite ||
      get_path_status(makefile->output_file) == PS_NONEXISTENT) {
    size_t i;
    char *user_info;
    const char* cxx;
    const char* cpp;
    const char *rm_command = makefile->gnu_make ? "$(RM)" : "rm -f";
    FILE *fp;
    boolean run_compiler = (makefile->nASN1Modules > 0)
      || (makefile->nTTCN3Modules) || (makefile->nTTCN3PPModules > 0);

    expstring_t titan_dir = 0;
    const char * last_slash = strrchr(program_name, '/');
    if (last_slash != NULL) {
      size_t path_len = last_slash - program_name;
      titan_dir = mcopystr(program_name);
      /* Chop off the program name, and the /bin before it (if any) */
      if (path_len >= 4
        && memcmp(titan_dir + path_len - 4, "/bin", 4) == 0) {
        titan_dir = mtruncstr(titan_dir, path_len - 4);
      }
      else {
        titan_dir = mtruncstr(titan_dir, path_len);
      }
    }


    fp = fopen(makefile->output_file, "w");
    if (fp == NULL){
      ERROR("Cannot open output file `%s' for writing: %s",
Elemer Lelik's avatar
Elemer Lelik committed
1720
      makefile->output_file, strerror(errno));
Elemer Lelik's avatar
Elemer Lelik committed
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
      return;
    }
    user_info = get_user_info();
    fprintf(fp, "# This Makefile was generated by the Makefile Generator\n"
      "# of the TTCN-3 Test Executor version " PRODUCT_NUMBER "\n"
      "# for %s\n"
      COPYRIGHT_STRING "\n\n"
      "# The following make commands are available:\n"
      "# - make, make all      Builds the %s.\n"
      "# - make archive        Archives all source files.\n"
      "# - make check          Checks the semantics of TTCN-3 and ASN.1 "
Elemer Lelik's avatar
Elemer Lelik committed
1732
1733
1734
      "modules.\n"
      "%s" // clean:
      "%s" //clean-all
Elemer Lelik's avatar
Elemer Lelik committed
1735
1736
1737
1738
1739
1740
      "# - make compile        Translates TTCN-3 and ASN.1 modules to C++.\n"
      "# - make dep            Creates/updates dependency list.\n"
      "# - make executable     Builds the executable test suite.\n"
      "# - make library        Builds the library archive.\n"
      "# - make objects        Builds the object files without linking the "
      "executable.\n", user_info,
Elemer Lelik's avatar
Elemer Lelik committed
1741
1742
1743
1744
1745
1746
      makefile->library ? "library archive." : "executable test suite",
      (makefile->linkingStrategy && makefile->hierarchical) ?
      "# - make clean          Removes generated files from project.\n" :
      "# - make clean          Removes all generated files.\n",
      (makefile->linkingStrategy && makefile->hierarchical) ?
      "# - make clean-all      Removes all generated files from the project hierarchy.\n" : "");
Elemer Lelik's avatar
Elemer Lelik committed
1747
1748
1749
1750
1751
1752
1753
1754
    Free(user_info);
    if (makefile->dynamic)
      fprintf(fp, "# - make shared_objects Builds the shared object files "
              "without linking the executable.\n");
    if (makefile->preprocess)
      fputs("# - make preprocess     Preprocess TTCN-3 files.\n", fp);
    if (makefile->central_storage) {
      fputs("# WARNING! This Makefile uses pre-compiled files from the "
Elemer Lelik's avatar
Elemer Lelik committed
1755
            "following directories:\n", fp);
Elemer Lelik's avatar
Elemer Lelik committed
1756
1757
1758
      for (i = 0; i < makefile->nBaseDirs; i++)
        fprintf(fp, "# %s\n", makefile->BaseDirs[i].dir_name);
      fputs("# The executable tests will be consistent only if all directories "
Elemer Lelik's avatar
Elemer Lelik committed
1759
1760
1761
1762
            "use\n"
            "# the same platform and the same version of TTCN-3 Test Executor "
            "and\n"
            "# C++ compiler with the same command line switches.\n\n", fp);
Elemer Lelik's avatar
Elemer Lelik committed
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
    }
    if (makefile->gnu_make) {
      fputs("# WARNING! This Makefile can be used with GNU make only.\n"
            "# Other versions of make may report syntax errors in it.\n\n"
            "#\n"
            "# Do NOT touch this line...\n"
            "#\n"
            ".PHONY: all shared_objects executable library objects check clean dep archive", fp);
      if (makefile->preprocess) fputs(" preprocess", fp);
      if (add_refd_prjs) {
Elemer Lelik's avatar
Elemer Lelik committed
1773
1774
1775
1776
        fprintf(fp, "\\\n referenced-all referenced-shared_objects referenced-executable referenced-library referenced-objects referenced-check"
              "\\\n referenced-clean%s",
              (makefile->linkingStrategy && makefile->hierarchical) ?
              "-all" : "");
Elemer Lelik's avatar
Elemer Lelik committed
1777
1778
1779
1780
1781
1782
1783
1784
1785
      }
      fprint_extra_targets(fp, makefile->target_placement_list, "PHONY");

      if (makefile->gcc_dep) {
        fputs("\n\n.SUFFIXES: .d", fp);
      }
      fputs("\n\n", fp);
    }

Elemer Lelik's avatar
Elemer Lelik committed
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
    if (makefile->linkingStrategy) {
      const char* tpd_name = getTPDFileName(makefile->project_name);
      if (tpd_name) {
        fputs("# Titan Project Descriptor file what this Makefile is generated from.\n", fp);
        fprintf(fp, "TPD = %s\n\n", tpd_name);
      }
      const char* root_dir = getPathToRootDir(makefile->project_name);
      if (root_dir) {
        fputs("# Relative path to top directory at OS level.\n", fp);
        fprintf(fp, "ROOT_DIR = %s\n\n", root_dir);
      }
    }

Elemer Lelik's avatar
Elemer Lelik committed
1799
    if (add_refd_prjs) {
Elemer Lelik's avatar
Elemer Lelik committed
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
      struct string_list* act_elem = NULL;
      struct string_list* head = NULL;
      if (makefile->linkingStrategy && makefile->hierarchical) {// pair with free_string_list
        head = act_elem = getRefWorkingDirs(makefile->project_name);
      }
      else {
        act_elem = makefile->sub_project_dirs;
      }
      if (!makefile->linkingStrategy)
        fputs("# This is the top level makefile of a Makefile hierarchy generated from\n", fp);
      fputs("# Titan Project Descriptor hierarchy. List of referenced project\n"
Elemer Lelik's avatar
Elemer Lelik committed
1811
1812
1813
1814
1815
1816
1817
            "# working directories (ordered by dependencies):\n", fp);
      while (act_elem) {
        if (act_elem->str) {
          fprintf(fp, "# %s\n", act_elem->str);
        }
        act_elem = act_elem->next;
      }
Elemer Lelik's avatar
Elemer Lelik committed
1818
1819
1820
      if (makefile->linkingStrategy && makefile->hierarchical) { // pair with getRefWorkingDirs
        free_string_list(head);
      }
Elemer Lelik's avatar
Elemer Lelik committed
1821
      fputs("REFERENCED_PROJECT_DIRS = ", fp);
Elemer Lelik's avatar
Elemer Lelik committed
1822
1823
1824
1825
1826
1827
      if (makefile->linkingStrategy && makefile->hierarchical) {
        head = act_elem = getRefWorkingDirs(makefile->project_name); // pair with free_string_list
      }
      else {
        act_elem = makefile->sub_project_dirs;
      }
Elemer Lelik's avatar
Elemer Lelik committed
1828
1829
1830
1831
1832
1833
1834
      while (act_elem) {
        if (act_elem->str) {
          fprintf(fp, "%s ", act_elem->str);
        }
        act_elem = act_elem->next;
      }
      fputs("\n\n", fp);
Elemer Lelik's avatar
Elemer Lelik committed
1835
1836
1837
      if (makefile->linkingStrategy && makefile->hierarchical) {// pair with getRefWorkingDirs
        free_string_list(head);
      }
Elemer Lelik's avatar
Elemer Lelik committed
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
    }

    fprintf(fp, "#\n"
          "# Set these variables...\n"
          "#\n\n"
          "# The path of your TTCN-3 Test Executor installation:\n"
          "# Uncomment this line to override the environment variable.\n"
          "%s"
          "# TTCN3_DIR = %s\n"
      , titan_dir ?
        "# The value below points to the location of the TITAN version\n"
        "# that generated this makefile.\n" : ""
      , titan_dir ? titan_dir : "");
    if (titan_dir) Free(titan_dir);

    if (makefile->cxxcompiler) {
      cxx = makefile->cxxcompiler;
    } else {
      cxx = "g++";
    }

    fprintf(fp, "\n# Your platform: (SOLARIS, SOLARIS8, LINUX, FREEBSD or "
      "WIN32)\n"
      "PLATFORM = %s\n\n"
      "# Your C++ compiler:\n"
      "# (if you change the platform, you may need to change the compiler)\n"
      "CXX = %s \n\n", get_platform_string(), cxx);


    if (makefile->preprocess || makefile->ttcn3preprocessor) {
      if (makefile->ttcn3preprocessor) {
        cpp = makefile->ttcn3preprocessor;
      } else {
        cpp = "cpp";
      }
      fprintf(fp,"# C preprocessor used for TTCN-3 files:\n"
          "CPP = %s\n\n", cpp);
    }

    fputs("# Flags for the C++ preprocessor (and makedepend as well):\n"
          "CPPFLAGS = -D$(PLATFORM) -I$(TTCN3_DIR)/include", fp);

    if (makefile->use_runtime_2) fputs(" -DTITAN_RUNTIME_2", fp);

    for (i = 0; i < makefile->nBaseDirs; i++) {
      fprintf(fp, " -I%s", makefile->BaseDirs[i].dir_name);
    }

    if (makefile->prep_includes) {
      struct string_list* act_elem = makefile->prep_includes;
      while (act_elem) {
        if (act_elem->str) {
          fprintf(fp, " -I%s", act_elem->str);
        }
        act_elem = act_elem->next;
      }
    }

    if (makefile->prep_defines) {
      struct string_list* act_elem = makefile->prep_defines;
      while (act_elem) {
        if (act_elem->str) {
          fprintf(fp, " -D%s", act_elem->str);
        }
        act_elem = act_elem->next;
      }
    }

Elemer Lelik's avatar
Elemer Lelik committed
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
    if (makefile->prep_undefines) {
      struct string_list* act_elem = makefile->prep_undefines;
      while (act_elem) {
        if (act_elem->str) {
          fprintf(fp, " -U%s", act_elem->str);
        }
        act_elem = act_elem->next;
      }
    }

Elemer Lelik's avatar
Elemer Lelik committed
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
    fputs("\n\n", fp);

    if (makefile->gcc_dep) {
      fprintf(fp, "# Flags for dependency generation\n"
        "CXXDEPFLAGS = -%s\n\n", strstr(cxx, "g++") ? "MM" : "xM1");
    }

    if (makefile->preprocess || makefile->ttcn3_prep_includes ||  makefile->ttcn3_prep_defines) {
      fputs("# Flags for preprocessing TTCN-3 files:\n"
            "CPPFLAGS_TTCN3 =", fp);

      if (makefile->preprocess) {
        for (i = 0; i < makefile->nBaseDirs; i++) {
          fprintf(fp, " -I%s", makefile->BaseDirs[i].dir_name);
        }
      }
      if (makefile->ttcn3_prep_includes) {
        struct string_list* act_elem = makefile->ttcn3_prep_includes;
        while (act_elem) {
          if (act_elem->str) {
            fprintf(fp, " -I%s", act_elem->str);
          }
          act_elem = act_elem->next;
        }
      }
      if (makefile->ttcn3_prep_defines) {
        struct string_list* act_elem = makefile->ttcn3_prep_defines;
        while (act_elem) {
          if (act_elem->str) {
            fprintf(fp, " -D%s", act_elem->str);
          }
          act_elem = act_elem->next;
        }
      }
Elemer Lelik's avatar
Elemer Lelik committed
1950
1951
1952
1953
1954
1955
1956
1957
1958
      if (makefile->ttcn3_prep_undefines) {
        struct string_list* act_elem = makefile->ttcn3_prep_undefines;
        while (act_elem) {
          if (act_elem->str) {
            fprintf(fp, " -U%s", act_elem->str);
          }
          act_elem = act_elem->next;
        }
      }
Elemer Lelik's avatar
Elemer Lelik committed
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
      fputs("\n\n", fp);
    }

    /* code splitting: command line argument wins */
    if (makefile->code_splitting_mode == NULL) {
      if (makefile->codesplittpd) {
        makefile->code_splitting_mode = mcopystr("-U type");
      }
    }

    fprintf(fp, "# Flags for the C++ compiler:\n"
          "CXXFLAGS = %s%s %s %s\n\n"
          "# Flags for the linker:\n"
          "LDFLAGS = %s%s\n\n"
          "ifeq ($(PLATFORM), WIN32)\n"
          "# Silence linker warnings.\n"
          "LDFLAGS += -Wl,--enable-auto-import,--enable-runtime-pseudo-reloc\n"
          "endif\n\n"
          "# Utility to create library files\n"
          "AR = ar\n"
          "ARFLAGS = \n\n"
          "# Flags for the TTCN-3 and ASN.1 compiler:\n"
Elemer Lelik's avatar
Elemer Lelik committed
1981
          "COMPILER_FLAGS =%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n\n"
Elemer Lelik's avatar
Elemer Lelik committed
1982
1983
1984
1985
1986
          "# Execution mode: (either ttcn3 or ttcn3-parallel)\n"
          "TTCN3_LIB = ttcn3%s%s%s\n\n"
#ifdef LICENSE
          "# The path of your OpenSSL installation:\n"
          "# If you do not have your own one, leave it unchanged.\n"
Elemer Lelik's avatar
Elemer Lelik committed
1987
          "%sOPENSSL_DIR = $(TTCN3_DIR)\n\n"
Elemer Lelik's avatar
Elemer Lelik committed
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
#endif
          "# The path of your libxml2 installation:\n"
          "# If you do not have your own one, leave it unchanged.\n"
          "XMLDIR = $(TTCN3_DIR)\n\n"
          "# Directory to store the archived source files:\n",
          makefile->dynamic ? "-Wall -fPIC" : "-Wall", /* CXXFLAGS */
          makefile->coverage ? " -fprofile-arcs -ftest-coverage -g" : "", /* CXXFLAGS COVERAGE */
          makefile->optlevel ? makefile->optlevel : "", /* CXXFLAGS optimization level */
          makefile->optflags ? makefile->optflags : "", /* CXXFLAGS optimization level */
          makefile->dynamic ? "-fPIC" : "", /* LDFLAGS */
          makefile->coverage ? " -fprofile-arcs -ftest-coverage -g -lgcov" : "", /* LDFLAGS COVERAGE */
          /* COMPILER_FLAGS */
          makefile->use_runtime_2 ? " -L -R " : " -L ",
          (makefile->code_splitting_mode ? makefile->code_splitting_mode : ""),
          (makefile->quietly ? " -q" : ""),
          (makefile->disablesubtypecheck ? " -y" : ""),
          (makefile->disableber ? " -b" : ""),
          (makefile->disableraw ? " -r" : ""),
          (makefile->disabletext ? " -x" : ""),
          (makefile->disablexer ? " -X" : ""),
          (makefile->disablejson ? " -j" : ""),
          (makefile->forcexerinasn ? " -a" : ""),
          (makefile->defaultasomit ? " -d" : ""),
          (makefile->gccmsgformat ? " -g" : ""),
          (makefile->linenumbersonlymsg ? " -i" : ""),
          (makefile->includesourceinfo ? " -l" : ""),
          /*(makefile->addsourcelineinfo ? " -L" : ""),*/
          (makefile->suppresswarnings ? " -w" : ""),
          (makefile->outparamboundness ? " -Y" : ""),
Elemer Lelik's avatar
Elemer Lelik committed
2017
          (makefile->omit_in_value_list ? " -M" : ""),
Elemer Lelik's avatar
Elemer Lelik committed
2018
          (makefile->warnings_for_bad_variants ? " -E" : ""),
Elemer Lelik's avatar
Elemer Lelik committed
2019
          (makefile->tcov_file_name ? makefile->tcov_file_name : ""),
Elemer Lelik's avatar
Elemer Lelik committed
2020
          (makefile->profiled_file_list ? " -z $(PROFILED_FILE_LIST)" : ""),
Elemer Lelik's avatar
Elemer Lelik committed
2021
2022
2023
2024
          /* end of COMPILER FLAGS */
          (makefile->use_runtime_2 ? "-rt2"    : ""), /* TTCN3_LIB */
          (makefile->single_mode   ? ""        : "-parallel"),
          (makefile->dynamic       ? "-dynamic": "")
Elemer Lelik's avatar
Elemer Lelik committed
2025
2026
2027
#ifdef LICENSE
          ,(makefile->disable_predef_ext_folder ? "# " : "")
#endif 
Elemer Lelik's avatar
Elemer Lelik committed
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
          );
    if (!makefile->gnu_make) {
      fputs("# Note: you can set any directory except ./archive\n", fp);
    }
    fputs("ARCHIVE_DIR = backup\n\n"
          "#\n"
          "# You may change these variables. Add your files if necessary...\n"
          "#\n\n"
          "# TTCN-3 modules of this project:\n"
          "TTCN3_MODULES =", fp);
    for (i = 0; i < makefile->nTTCN3Modules; i++) {
      const struct module_struct *module = makefile->TTCN3Modules + i;
      if (module->dir_name == NULL || !makefile->central_storage)
        /* If the file is in the current directory or
         * is not in the current directory but central directory is not used,
         * it goes into TTCN3_MODULES */
        print_file_name(fp, module);
    }
    fprint_extra_targets(fp, makefile->target_placement_list, "TTCN3_MODULES");
    if (makefile->preprocess) {
      fputs("\n\n"
Elemer Lelik's avatar
Elemer Lelik committed
2049
      "# TTCN-3 modules to preprocess:\n"
Elemer Lelik's avatar
Elemer Lelik committed
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
            "TTCN3_PP_MODULES =", fp);
      for (i = 0; i < makefile->nTTCN3PPModules; i++) {
        const struct module_struct *module = makefile->TTCN3PPModules + i;
        if (module->dir_name == NULL || !makefile->central_storage)
          print_file_name(fp, module);
      }
      fprint_extra_targets(fp, makefile->target_placement_list, "TTCN3_PP_MODULES");
    }
    if (makefile->central_storage) {
      fputs("\n\n"
Elemer Lelik's avatar
Elemer Lelik committed
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
      "# TTCN-3 modules used from central project(s):\n"
      "BASE_TTCN3_MODULES =", fp);
      if (!makefile->linkingStrategy) {
        for (i = 0; i < makefile->nTTCN3Modules; i++) {
          const struct module_struct *module = makefile->TTCN3Modules + i;
          /* Central storage used AND file is not in the current directory => it goes into BASE_TTCN3_MODULES */
          if (module->dir_name != NULL) print_file_name(fp, module);
        }
        if (makefile->preprocess) {
          fputs("\n\n"
          "# TTCN-3 modules to preprocess used from central project(s):\n"
          "BASE_TTCN3_PP_MODULES =", fp);
          for (i = 0; i < makefile->nTTCN3PPModules; i++) {
            const struct module_struct *module = makefile->TTCN3PPModules + i;
            if (module->dir_name != NULL && !isTtcnPPFileInLibrary(module->file_name))
              print_file_name(fp, module);
          }
        }
Elemer Lelik's avatar
Elemer Lelik committed
2078
      }
Elemer Lelik's avatar
Elemer Lelik committed
2079
2080
2081
2082
2083
2084
2085
      else { // new linking strategy
        for (i = 0; i < makefile->nTTCN3Modules; i++) {
          const struct module_struct *module = makefile->TTCN3Modules + i;
          /* Central storage used AND file is not in the current directory => it goes into BASE_TTCN3_MODULES */
          if (module->dir_name != NULL && !isTtcn3ModuleInLibrary(module->module_name))
            print_file_name(fp, module);
        }
Elemer Lelik's avatar
Elemer Lelik committed
2086
        fputs("\n\n"
Elemer Lelik's avatar
Elemer Lelik committed
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
        "# TTCN-3 library linked modules used from central project(s):\n"
        "BASE2_TTCN3_MODULES =", fp);
        for (i = 0; i < makefile->nTTCN3Modules; i++) {
          const struct module_struct *module = makefile->TTCN3Modules + i;
          /* Central storage used AND file is not in the current directory => it goes into BASE_TTCN3_MODULES */
          if (module->dir_name != NULL && isTtcn3ModuleInLibrary(module->module_name))
            print_file_name(fp, module);
        }
        if (makefile->preprocess) {
          fputs("\n\n"
          "# TTCN-3 modules to preprocess used from central project(s):\n