-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathfastddsgen.java
1698 lines (1462 loc) · 59.9 KB
/
fastddsgen.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
167
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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
368
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
427
428
429
430
431
432
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
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
524
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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.eprosima.fastdds;
import com.eprosima.fastcdr.idl.generator.TypesGenerator;
import com.eprosima.fastdds.exceptions.BadArgumentException;
import com.eprosima.fastdds.idl.grammar.Context;
import com.eprosima.fastdds.solution.Project;
import com.eprosima.fastdds.solution.Solution;
import com.eprosima.fastdds.util.Utils;
import com.eprosima.fastdds.util.VSConfiguration;
import com.eprosima.idl.generator.manager.TemplateExtension;
import com.eprosima.idl.generator.manager.TemplateGroup;
import com.eprosima.idl.generator.manager.TemplateManager;
import com.eprosima.idl.parser.grammar.IDLLexer;
import com.eprosima.idl.parser.grammar.IDLParser;
import com.eprosima.idl.parser.tree.AnnotationDeclaration;
import com.eprosima.idl.parser.tree.AnnotationMember;
import com.eprosima.idl.parser.tree.Specification;
import com.eprosima.idl.parser.typecode.Kind;
import com.eprosima.idl.parser.typecode.PrimitiveTypeCode;
import com.eprosima.idl.parser.typecode.TypeCode;
import com.eprosima.idl.util.Util;
import com.eprosima.log.ColorMessage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.IOError;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.Map;
import java.util.HashMap;
import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateErrorListener;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.antlr.stringtemplate.language.DefaultTemplateLexer;
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CommonTokenStream;
// TODO: Implement Solution & Project in com.eprosima.fastdds.solution
public class fastddsgen
{
/*
* ----------------------------------------------------------------------------------------
*
* Attributes
*/
private static ArrayList<String> m_platforms = null;
private Vector<String> m_idlFiles;
protected static String m_appEnv = "FASTRTPSHOME";
private String m_exampleOption = null;
private boolean m_ppDisable = false; //TODO
private boolean m_replace = false;
private String m_ppPath = null;
private final String m_defaultOutputDir = "." + File.separator;
private String m_outputDir = m_defaultOutputDir;
private String m_tempDir = null;
protected static String m_appName = "fastddsgen";
private boolean m_publishercode = true;
private boolean m_subscribercode = true;
private boolean m_atLeastOneStructure = false;
protected static String m_localAppProduct = "fastrtps";
private ArrayList<String> m_includePaths = new ArrayList<String>();
// Mapping where the key holds the path to the template file and the value the wanted output file name
private Map<String, String> m_customStgOutput = new HashMap<String, String>();
private static VSConfiguration m_vsconfigurations[] = {
new VSConfiguration("Debug DLL", "Win32", true, true),
new VSConfiguration("Release DLL", "Win32", false, true),
new VSConfiguration("Debug", "Win32", true, false),
new VSConfiguration("Release", "Win32", false, false)
};
private String m_os = null;
private boolean fusion_ = false;
//! Default package used in Java files.
private String m_package = "";
// Generates type naming compatible with ROS 2
private boolean m_type_ros2 = false;
// Generate TypeObject files?
private boolean m_type_object_files = false;
// Generate string and sequence types compatible with C?
private boolean m_typesc = false;
// Generate python binding files
private boolean m_python = false;
private boolean m_case_sensitive = false;
// Testing
private boolean m_test = false;
// Use to know the programming language
public enum LANGUAGE
{
CPP,
JAVA
};
private LANGUAGE m_languageOption = LANGUAGE.CPP; // Default language -> CPP
/*
* ----------------------------------------------------------------------------------------
*
* Constructor
*/
public fastddsgen(
String [] args) throws BadArgumentException
{
int count = 0;
String arg;
// Detect OS
m_os = System.getProperty("os.name");
m_idlFiles = new Vector<String>();
// Check arguments
while (count < args.length)
{
arg = args[count++];
if (!arg.startsWith("-"))
{
m_idlFiles.add(Paths.get(arg).normalize().toString());
}
else if (arg.equals("-example"))
{
if (count < args.length)
{
m_exampleOption = args[count++];
if (!m_platforms.contains(m_exampleOption))
{
throw new BadArgumentException("Unknown example arch " + m_exampleOption);
}
}
else
{
throw new BadArgumentException("No architecture speficied after -example argument");
}
}
else if (arg.equals("-language"))
{
if (count < args.length)
{
String languageOption = args[count++];
if (languageOption.equalsIgnoreCase("c++"))
{
m_languageOption = LANGUAGE.CPP;
}
else if (languageOption.equalsIgnoreCase("java"))
{
m_languageOption = LANGUAGE.JAVA;
}
else
{
throw new BadArgumentException("Unknown language " + languageOption);
}
}
else
{
throw new BadArgumentException("No language specified after -language argument");
}
}
else if (arg.equals("-package"))
{
if (count < args.length)
{
m_package = args[count++];
}
else
{
throw new BadArgumentException("No package after -package argument");
}
}
else if (arg.equals("-ppPath"))
{
if (count < args.length)
{
m_ppPath = args[count++];
}
else
{
throw new BadArgumentException("No URL specified after -ppPath argument");
}
}
else if (arg.equals("-extrastg"))
{
if (count + 1 < args.length)
{
m_customStgOutput.put(args[count++], args[count++]);
}
else
{
throw new BadArgumentException("Missing arguments for -extrastg");
}
}
else if (arg.equals("-ppDisable"))
{
m_ppDisable = true;
}
else if (arg.equals("-replace"))
{
m_replace = true;
}
else if (arg.equals("-d"))
{
if (count < args.length)
{
m_outputDir = Utils.addFileSeparator(args[count++]);
}
else
{
throw new BadArgumentException("No URL specified after -d argument");
}
}
else if (arg.equals("-t"))
{
if (count < args.length)
{
m_tempDir = Utils.addFileSeparator(args[count++]);
}
else
{
throw new BadArgumentException("No temporary directory specified after -t argument");
}
}
else if (arg.equals("-version"))
{
showVersion();
System.exit(0);
}
else if (arg.equals("-help"))
{
printHelp();
System.exit(0);
}
else if (arg.equals("-fusion"))
{
fusion_ = true;
}
else if (arg.equals("-typeros2"))
{
m_type_ros2 = true;
}
else if (arg.equals("-typeobject"))
{
m_type_object_files = true;
}
else if (arg.equals("-typesc"))
{
m_typesc = true;
}
else if (arg.equals("-python"))
{
m_python = true;
}
else if (arg.equals("-test"))
{
m_test = true;
}
else if (arg.equals("-I"))
{
if (count < args.length)
{
String pathStr = args[count++];
if (!isIncludePathDuplicated(pathStr))
{
m_includePaths.add("-I".concat(pathStr));
}
}
else
{
throw new BadArgumentException("No include directory specified after -I argument");
}
}
else if (arg.equals("-cs"))
{
m_case_sensitive = true;
}
else // TODO: More options: -rpm, -debug
{
throw new BadArgumentException("Unknown argument " + arg);
}
}
if (null != m_exampleOption && m_python)
{
throw new BadArgumentException("-example and -python currently are incompatible");
}
if (m_idlFiles.isEmpty())
{
throw new BadArgumentException("No input files given");
}
}
/*
* ----------------------------------------------------------------------------------------
*
* Listener classes
*/
class TemplateErrorListener implements StringTemplateErrorListener
{
public void error(
String arg0,
Throwable arg1)
{
System.out.println(ColorMessage.error() + arg0);
arg1.printStackTrace();
}
public void warning(
String arg0)
{
System.out.println(ColorMessage.warning() + arg0);
}
}
/*
* ----------------------------------------------------------------------------------------
*
* Main methods
*/
public boolean execute()
{
if (!m_outputDir.equals(m_defaultOutputDir))
{
File dir = new File(m_outputDir);
if (!dir.exists())
{
System.out.println(ColorMessage.error() + "The specified output directory does not exist");
return false;
}
}
boolean returnedValue = globalInit();
if (returnedValue)
{
Solution solution = new Solution(m_languageOption, m_exampleOption,
getVersion(), m_publishercode, m_subscribercode);
// Load string templates
System.out.println("Loading templates from " + System.getProperty("java.class.path"));
// Add path of custom templates to manager search path
String extraPaths = "";
for (Map.Entry<String, String> entry : m_customStgOutput.entrySet())
{
Path path = Paths.get(entry.getKey()).getParent();
if (path != null)
{
extraPaths += ":" + path.toString().replace("\\", "/");
}
else
{
extraPaths += ":./";
}
}
String templatePaths = "com/eprosima/fastdds/idl/templates:com/eprosima/fastcdr/idl/templates" + extraPaths;
System.out.println("Template resource folders: " + templatePaths);
TemplateManager.setGroupLoaderDirectories(templatePaths);
// In local for all products
if (m_os.contains("Windows"))
{
solution.addInclude("$(" + m_appEnv + ")/include");
solution.addLibraryPath("$(" + m_appEnv + ")/lib");
if (m_exampleOption != null)
{
solution.addLibraryPath("$(" + m_appEnv + ")/lib/" + m_exampleOption);
solution.addLibraryPath("$(" + m_appEnv + ")/lib/" + m_exampleOption + "/VC/static");
}
}
// If Java, include jni headers
if (m_languageOption == LANGUAGE.JAVA)
{
solution.addInclude("$(JAVA_HOME)/include");
if (m_exampleOption != null && m_exampleOption.contains("Linux"))
{
solution.addInclude("$(JAVA_HOME)/include/linux");
}
}
if ((m_exampleOption != null || m_test) && !m_exampleOption.contains("Win"))
{
solution.addLibrary("fastcdr");
}
// Add product library
solution.addLibrary("fastrtps");
ArrayList<String> includedIDL = new ArrayList<String>();
for (int count = 0; returnedValue && (count < m_idlFiles.size()); ++count)
{
Project project = process(m_idlFiles.get(count), true);
for (String include : project.getIDLIncludeFiles())
{
//System.out.println(ColorMessage.error() + m_idlFiles.get(count) + " includes " + include);
includedIDL.add(include);
}
if (project != null)
{
System.out.println("Adding project: " + project.getFile());
if (!solution.existsProject(project.getFile()))
{
solution.addProject(project);
}
}
else
{
returnedValue = false;
}
}
// Add include idl files
for (String included : includedIDL)
{
Project inner = process(included, false);
if (inner != null && !solution.existsProject(inner.getFile()))
{
System.out.println("Adding project: " + inner.getFile());
solution.addProject(inner);
}
}
if (returnedValue && m_python)
{
returnedValue = genSwigCMake(solution);
}
// Generate solution
if (returnedValue && (m_exampleOption != null) || m_test)
{
if ((returnedValue = genSolution(solution)) == false)
{
System.out.println(ColorMessage.error() + "While the solution was being generated");
}
}
}
return returnedValue;
}
/*
* ----------------------------------------------------------------------------------------
*
* Auxiliary methods
*/
public static boolean loadPlatforms()
{
boolean returnedValue = false;
fastddsgen.m_platforms = new ArrayList<String>();
fastddsgen.m_platforms.add("i86Win32VS2019");
fastddsgen.m_platforms.add("x64Win64VS2019");
fastddsgen.m_platforms.add("i86Linux2.6gcc");
fastddsgen.m_platforms.add("x64Linux2.6gcc");
fastddsgen.m_platforms.add("armLinux2.6gcc");
fastddsgen.m_platforms.add("CMake");
returnedValue = true;
return returnedValue;
}
private String getVersion()
{
try
{
//InputStream input = this.getClass().getResourceAsStream("/fastrtps_version.h");
InputStream input = this.getClass().getClassLoader().getResourceAsStream("version");
byte[] b = new byte[input.available()];
input.read(b);
String text = new String(b);
int beginindex = text.indexOf("=");
return text.substring(beginindex + 1);
}
catch (Exception ex)
{
System.out.println(ColorMessage.error() + "Getting version. " + ex.getMessage());
}
return "";
}
private void showVersion()
{
String version = getVersion();
System.out.println(m_appName + " version " + version);
}
private boolean isIncludePathDuplicated(String pathToCheck)
{
try
{
Path path = Paths.get(pathToCheck);
String absPath = path.toAbsolutePath().toString();
boolean isDuplicateFound = false;
for (String includePath : m_includePaths)
{
// include paths are prefixed with "-I"
if (includePath.length() <= 2)
{
continue;
}
String absIncludePath = Paths.get(includePath.substring(2)).toAbsolutePath().toString();
if (absPath.toLowerCase().equals(absIncludePath.toLowerCase()))
{
isDuplicateFound = true;
break;
}
}
if (isDuplicateFound)
{
return true;
}
}
catch (InvalidPathException | IOError | SecurityException ex)
{
// path operations failed, just returning false
}
return false;
}
public static void printHelp()
{
System.out.println(m_appName + " usage:");
System.out.println("\t" + m_appName + " [options] <file> [<file> ...]");
System.out.println("\twhere the options are:");
System.out.println("\t\t-help: shows this help");
System.out.println("\t\t-version: shows the current version of eProsima Fast DDS gen.");
System.out.println(
"\t\t-example <platform>: Generates a solution for a specific platform (example: x64Win64VS2019)");
System.out.println("\t\t\tSupported platforms:");
for (int count = 0; count < m_platforms.size(); ++count)
{
System.out.println("\t\t\t * " + m_platforms.get(count));
}
//System.out.println("\t\t-language <C++>: Programming language (default: C++).");
System.out.println("\t\t-replace: replaces existing generated files.");
System.out.println("\t\t-ppDisable: disables the preprocessor.");
System.out.println("\t\t-ppPath: specifies the preprocessor path.");
System.out.println("\t\t-extrastg <template file> <output file name>: specifies a custom template, template location must be in classpath.");
System.out.println("\t\t-typeros2: generates type naming compatible with ROS2.");
System.out.println("\t\t-I <path>: add directory to preprocessor include paths.");
System.out.println("\t\t-d <path>: sets an output directory for generated files.");
System.out.println("\t\t-t <temp dir>: sets a specific directory as a temporary directory.");
System.out.print("\t\t-typeobject: generates TypeObject files to automatically register the types as");
System.out.println(" dynamic.");
System.out.println("\t\t-cs: IDL grammar apply case sensitive matching.");
System.out.println("\t\t-test: executes FastDDSGen tests.");
System.out.println("\t\t-python: generates python bindings for the generated types.");
System.out.println("\tand the supported input files are:");
System.out.println("\t* IDL files.");
}
public boolean globalInit()
{
// Set the temporary folder
if (m_tempDir == null)
{
if (m_os.contains("Windows"))
{
String tempPath = System.getenv("TEMP");
if (tempPath == null)
{
tempPath = System.getenv("TMP");
}
m_tempDir = tempPath;
}
else if (m_os.contains("Linux") || m_os.contains("Mac"))
{
m_tempDir = "/tmp/";
}
}
if (m_tempDir.charAt(m_tempDir.length() - 1) != File.separatorChar)
{
m_tempDir += File.separator;
}
return true;
}
private Project process(
String idlFilename,
boolean processCustomTemplates)
{
Project project = null;
System.out.println("Processing the file " + idlFilename + "...");
try
{
// Protocol CDR
project = parseIDL(idlFilename, processCustomTemplates); // TODO: Quitar archivos copiados TypesHeader.stg, TypesSource.stg, PubSubTypeHeader.stg de la carpeta com.eprosima.fastdds.idl.templates
}
catch (Exception ioe)
{
System.out.println(ColorMessage.error() + "Cannot generate the files");
if (!ioe.getMessage().equals(""))
{
System.out.println(ioe.getMessage());
}
}
return project;
}
private Project parseIDL(
String idlFilename,
boolean processCustomTemplates)
{
boolean returnedValue = false;
String idlParseFileName = idlFilename;
Project project = null;
String onlyFileName = Util.getIDLFileNameOnly(idlFilename);
if (!m_ppDisable)
{
idlParseFileName = callPreprocessor(idlFilename);
}
if (idlParseFileName != null)
{
Context ctx = new Context(onlyFileName, idlFilename, m_includePaths, m_subscribercode, m_publishercode,
m_localAppProduct, m_type_object_files, m_typesc, m_type_ros2);
if (m_case_sensitive)
{
ctx.ignore_case(false);
}
if (fusion_)
{
ctx.setActivateFusion(true);
}
// Create default @Key annotation.
AnnotationDeclaration keyann = ctx.createAnnotationDeclaration("Key", null);
keyann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));
// Create default @Topic annotation.
AnnotationDeclaration topicann = ctx.createAnnotationDeclaration("Topic", null);
topicann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(Kind.KIND_BOOLEAN), "true"));
// Create template manager
TemplateManager tmanager = new TemplateManager("FastCdrCommon:eprosima:Common", ctx, m_typesc);
List<TemplateExtension> extensions = new ArrayList<TemplateExtension>();
// Load common types template
/// Add extension for @key related function definitions for each struct_type.
extensions.add(new TemplateExtension("struct_type", "keyFunctionHeadersStruct"));
tmanager.addGroup("TypesHeader", extensions);
if (m_type_object_files)
{
tmanager.addGroup("TypeObjectHeader", extensions);
}
extensions.clear();
/// Add extension for @key related function declarations for each struct_type.
extensions.add(new TemplateExtension("struct_type", "keyFunctionSourcesStruct"));
tmanager.addGroup("TypesSource", extensions);
if (m_type_object_files)
{
tmanager.addGroup("TypeObjectSource", extensions);
}
extensions.clear();
/// Add extension for @key related preprocessor definitions in main for each struct typecode.
extensions.add(new TemplateExtension("main", "keyFunctionSourcesMain"));
tmanager.addGroup("TypesSource", extensions);
// Load Types common templates
tmanager.addGroup("DDSPubSubTypeHeader");
tmanager.addGroup("DDSPubSubTypeSource");
// Load Publisher templates
tmanager.addGroup("DDSPublisherHeader");
tmanager.addGroup("DDSPublisherSource");
// Load Subscriber templates
tmanager.addGroup("DDSSubscriberHeader");
tmanager.addGroup("DDSSubscriberSource");
// Load PubSubMain template
tmanager.addGroup("DDSPubSubMain");
if (m_test)
{
// Load test template
tmanager.addGroup("SerializationTestSource");
tmanager.addGroup("SerializationHeader");
tmanager.addGroup("SerializationSource");
}
// Add JNI sources.
if (m_languageOption == LANGUAGE.JAVA)
{
tmanager.addGroup("JNIHeader");
tmanager.addGroup("JNISource");
tmanager.addGroup("JavaSource");
// Set package in context.
ctx.setPackage(m_package);
}
if (m_python)
{
tmanager.addGroup("TypesSwigInterface");
tmanager.addGroup("DDSPubSubTypeSwigInterface");
}
// Load custom templates into manager
if (processCustomTemplates)
{
for (Map.Entry<String, String> entry : m_customStgOutput.entrySet())
{
System.out.println("Loading custom template " + entry.getKey() + "...");
Path path = Paths.get(entry.getKey());
String templateName = path.getFileName().toString().substring(0, path.getFileName().toString().lastIndexOf('.'));
tmanager.addGroup(templateName);
}
}
// Create main template
TemplateGroup maintemplates = tmanager.createTemplateGroup("main");
maintemplates.setAttribute("ctx", ctx);
try
{
ANTLRFileStream input = new ANTLRFileStream(idlParseFileName);
IDLLexer lexer = new IDLLexer(input);
lexer.setContext(ctx);
CommonTokenStream tokens = new CommonTokenStream(lexer);
IDLParser parser = new IDLParser(tokens);
// Pass the finelame without the extension
Specification specification = parser.specification(ctx, tmanager, maintemplates).spec;
returnedValue = specification != null;
}
catch (FileNotFoundException ex)
{
System.out.println(ColorMessage.error(
"FileNotFounException") + "The File " + idlParseFileName + " was not found.");
}/* catch (ParseException ex) {
System.out.println(ColorMessage.error("ParseException") + ex.getMessage());
}*/
catch (Exception ex)
{
System.out.println(ColorMessage.error("Exception") + ex.getMessage());
}
if (returnedValue)
{
// Create information of project for solution
project = new Project(ctx, idlFilename, ctx.getDependencies());
// Create all custom files for template
if (processCustomTemplates)
{
for (Map.Entry<String, String> entry : m_customStgOutput.entrySet())
{
Path path = Paths.get(entry.getKey());
String templateName = path.getFileName().toString().substring(0, path.getFileName().toString().lastIndexOf('.'));
System.out.println("Generating from custom " + templateName + " to " + entry.getValue());
if (returnedValue = Utils.writeFile(m_outputDir + entry.getValue(), maintemplates.getTemplate(templateName), m_replace))
{
// Try to determine if the file is a header file.
if (entry.getValue().contains(".hpp") || entry.getValue().contains(".h"))
{
project.addCommonIncludeFile(entry.getValue());
}
else
{
project.addCommonSrcFile(ctx.getFilename() + entry.getValue());
}
}
else
{
break;
}
}
}
System.out.println("Generating Type definition files...");
if ((returnedValue) && (returnedValue =
Utils.writeFile(m_outputDir + ctx.getFilename() + ".h",
maintemplates.getTemplate("TypesHeader"),
m_replace)))
{
if (returnedValue =
Utils.writeFile(m_outputDir + ctx.getFilename() + ".cxx",
maintemplates.getTemplate("TypesSource"), m_replace))
{
project.addCommonIncludeFile(ctx.getFilename() + ".h");
project.addCommonSrcFile(ctx.getFilename() + ".cxx");
if (m_type_object_files)
{
System.out.println("Generating TypeObject files...");
if (returnedValue = Utils.writeFile(m_outputDir + ctx.getFilename() + "TypeObject.h",
maintemplates.getTemplate("TypeObjectHeader"), m_replace))
{
if (returnedValue = Utils.writeFile(m_outputDir + ctx.getFilename() + "TypeObject.cxx",
maintemplates.getTemplate("TypeObjectSource"), m_replace))
{
project.addCommonIncludeFile(ctx.getFilename() + "TypeObject.h");
project.addCommonSrcFile(ctx.getFilename() + "TypeObject.cxx");
}
}
}
if (m_python)
{
System.out.println("Generating Swig interface files...");
if (returnedValue =
Utils.writeFile(m_outputDir + ctx.getFilename() + ".i",
maintemplates.getTemplate("TypesSwigInterface"), m_replace))
{
}
}
}
}
if (m_test)
{
System.out.println("Generating Serialization Test file...");
String fileName = m_outputDir + ctx.getFilename() + "SerializationTest.cpp";
returnedValue =
Utils.writeFile(fileName, maintemplates.getTemplate("SerializationTestSource"), m_replace);
System.out.println("Generating Serialization Source file...");
String fileNameS = m_outputDir + ctx.getFilename() + "Serialization.cpp";
returnedValue =
Utils.writeFile(fileNameS, maintemplates.getTemplate("SerializationSource"), m_replace);
System.out.println("Generating Serialization Header file...");
String fileNameH = m_outputDir + ctx.getFilename() + "Serialization.h";
returnedValue =
Utils.writeFile(fileNameH, maintemplates.getTemplate("SerializationHeader"), m_replace);
}
// TODO: Uncomment following lines and create templates
if (ctx.existsLastStructure())
{
m_atLeastOneStructure = true;
project.setHasStruct(true);
System.out.println("Generating TopicDataTypes files...");
if (returnedValue =
Utils.writeFile(m_outputDir + ctx.getFilename() + "PubSubTypes.h",
maintemplates.getTemplate("DDSPubSubTypeHeader"), m_replace))
{
if (returnedValue =
Utils.writeFile(m_outputDir + ctx.getFilename() + "PubSubTypes.cxx",
maintemplates.getTemplate("DDSPubSubTypeSource"), m_replace))
{
project.addProjectIncludeFile(ctx.getFilename() + "PubSubTypes.h");
project.addProjectSrcFile(ctx.getFilename() + "PubSubTypes.cxx");
if (m_python)
{
System.out.println("Generating Swig interface files...");
returnedValue = Utils.writeFile(
m_outputDir + ctx.getFilename() + "PubSubTypes.i",
maintemplates.getTemplate("DDSPubSubTypeSwigInterface"), m_replace);
}
}
}
if (m_exampleOption != null)
{
System.out.println("Generating Publisher files...");
if (returnedValue =
Utils.writeFile(m_outputDir + ctx.getFilename() + "Publisher.h",
maintemplates.getTemplate("DDSPublisherHeader"), m_replace))
{
if (returnedValue =
Utils.writeFile(m_outputDir + ctx.getFilename() + "Publisher.cxx",
maintemplates.getTemplate("DDSPublisherSource"), m_replace))
{
project.addProjectIncludeFile(ctx.getFilename() + "Publisher.h");
project.addProjectSrcFile(ctx.getFilename() + "Publisher.cxx");
}
}
System.out.println("Generating Subscriber files...");
if (returnedValue =
Utils.writeFile(m_outputDir + ctx.getFilename() + "Subscriber.h",
maintemplates.getTemplate("DDSSubscriberHeader"), m_replace))
{
if (returnedValue =
Utils.writeFile(m_outputDir + ctx.getFilename() + "Subscriber.cxx",
maintemplates.getTemplate("DDSSubscriberSource"), m_replace))
{
project.addProjectIncludeFile(ctx.getFilename() + "Subscriber.h");
project.addProjectSrcFile(ctx.getFilename() + "Subscriber.cxx");
}
}
System.out.println("Generating main file...");
if (returnedValue =
Utils.writeFile(m_outputDir + ctx.getFilename() + "PubSubMain.cxx",
maintemplates.getTemplate("DDSPubSubMain"), m_replace))
{
project.addProjectSrcFile(ctx.getFilename() + "PubSubMain.cxx");
}
}
}
}
// Java support (Java classes and JNI code)
if (returnedValue && m_languageOption == LANGUAGE.JAVA)
{
String outputDir = m_outputDir;
// Make directories from package.
if (!m_package.isEmpty())
{
outputDir = m_outputDir + File.separator + m_package.replace('.', File.separatorChar);
File dirs = new File(outputDir);
if (!dirs.exists())
{
if (!dirs.mkdirs())
{
System.out.println(ColorMessage.error() + "Cannot create directories for Java packages.");
return null;
}
}
}
// Java classes.