3 Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 #include "VfrCompiler.h"
27 CVfrCompiler::SET_RUN_STATUS (
28 IN COMPILER_RUN_STATUS Status
35 CVfrCompiler::IS_RUN_STATUS (
36 IN COMPILER_RUN_STATUS Status
39 return mRunStatus
== Status
;
43 CVfrCompiler::OptionInitialization (
50 mOptions
.VfrFileName
[0] = '\0';
51 mOptions
.RecordListFile
[0] = '\0';
52 mOptions
.CreateRecordListFile
= FALSE
;
53 mOptions
.CreateIfrPkgFile
= FALSE
;
54 mOptions
.PkgOutputFileName
[0] = '\0';
55 mOptions
.COutputFileName
[0] = '\0';
56 mOptions
.OutputDirectory
[0] = '\0';
57 mOptions
.PreprocessorOutputFileName
[0] = '\0';
58 mOptions
.VfrBaseFileName
[0] = '\0';
59 mOptions
.IncludePaths
= NULL
;
60 mOptions
.CPreprocessorOptions
= NULL
;
62 for (Index
= 1; (Index
< Argc
) && (Argv
[Index
][0] == '-'); Index
++) {
63 if ((_stricmp(Argv
[Index
], "-?") == 0) || (_stricmp(Argv
[Index
], "-h") == 0)) {
65 SET_RUN_STATUS (STATUS_DEAD
);
67 } else if (_stricmp(Argv
[Index
], "-l") == 0) {
68 mOptions
.CreateRecordListFile
= TRUE
;
69 gCIfrRecordInfoDB
.TurnOn ();
70 } else if (_stricmp(Argv
[Index
], "-i") == 0) {
72 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
73 printf ("%s -i - missing path argument\n", PROGRAM_NAME
);
77 AppendIncludePath(Argv
[Index
]);
78 } else if (_stricmp(Argv
[Index
], "-od") == 0) {
80 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
81 printf ("%s -od - missing output directory name\n", PROGRAM_NAME
);
84 strcpy (mOptions
.OutputDirectory
, Argv
[Index
]);
85 } else if (_stricmp(Argv
[Index
], "-ibin") == 0) {
86 mOptions
.CreateIfrPkgFile
= TRUE
;
87 } else if (_stricmp(Argv
[Index
], "-nostrings") == 0) {
88 } else if (_stricmp(Argv
[Index
], "-ppflag") == 0) {
90 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
91 printf ("%s -od - missing C-preprocessor argument\n", PROGRAM_NAME
);
95 AppendCPreprocessorOptions (Argv
[Index
]);
97 printf ("%s unrecognized option %s\n", PROGRAM_NAME
, Argv
[Index
]);
103 if (Index
!= Argc
- 1) {
104 printf ("%s must specify VFR file name", PROGRAM_NAME
);
108 strcpy (mOptions
.VfrFileName
, Argv
[Index
]);
111 if (SetBaseFileName() != 0) {
114 if (SetPkgOutputFileName () != 0) {
117 if (SetCOutputFileName() != 0) {
120 if (SetPreprocessorOutputFileName () != 0) {
123 if (SetRecordListFileName () != 0) {
129 SET_RUN_STATUS (STATUS_FAILED
);
131 mOptions
.VfrFileName
[0] = '\0';
132 mOptions
.RecordListFile
[0] = '\0';
133 mOptions
.CreateRecordListFile
= FALSE
;
134 mOptions
.CreateIfrPkgFile
= FALSE
;
135 mOptions
.PkgOutputFileName
[0] = '\0';
136 mOptions
.COutputFileName
[0] = '\0';
137 mOptions
.OutputDirectory
[0] = '\0';
138 mOptions
.PreprocessorOutputFileName
[0] = '\0';
139 mOptions
.VfrBaseFileName
[0] = '\0';
140 if (mOptions
.IncludePaths
!= NULL
) {
141 delete mOptions
.IncludePaths
;
142 mOptions
.IncludePaths
= NULL
;
144 if (mOptions
.CPreprocessorOptions
!= NULL
) {
145 delete mOptions
.CPreprocessorOptions
;
146 mOptions
.CPreprocessorOptions
= NULL
;
151 CVfrCompiler::AppendIncludePath (
156 INT8
*IncludePaths
= NULL
;
158 Len
= strlen (" -I ") + strlen (PathStr
) + 1;
159 if (mOptions
.IncludePaths
!= NULL
) {
160 Len
+= strlen (mOptions
.IncludePaths
);
162 IncludePaths
= new INT8
[Len
];
163 if (IncludePaths
== NULL
) {
164 printf ("%s memory allocation failure\n", PROGRAM_NAME
);
167 IncludePaths
[0] = '\0';
168 if (mOptions
.IncludePaths
!= NULL
) {
169 strcat (IncludePaths
, mOptions
.IncludePaths
);
171 strcat (IncludePaths
, " -I ");
172 strcat (IncludePaths
, PathStr
);
173 if (mOptions
.IncludePaths
!= NULL
) {
174 delete mOptions
.IncludePaths
;
176 mOptions
.IncludePaths
= IncludePaths
;
180 CVfrCompiler::AppendCPreprocessorOptions (
187 Len
= strlen (Options
) + strlen (" ") + 1;
188 if (mOptions
.CPreprocessorOptions
!= NULL
) {
189 Len
+= strlen (mOptions
.CPreprocessorOptions
);
193 printf ("%s memory allocation failure\n", PROGRAM_NAME
);
197 if (mOptions
.CPreprocessorOptions
!= NULL
) {
198 strcat (Opt
, mOptions
.CPreprocessorOptions
);
201 strcat (Opt
, Options
);
202 if (mOptions
.CPreprocessorOptions
!= NULL
) {
203 delete mOptions
.CPreprocessorOptions
;
205 mOptions
.CPreprocessorOptions
= Opt
;
209 CVfrCompiler::SetBaseFileName (
213 INT8
*pFileName
, *pPath
, *pExt
;
215 if (mOptions
.VfrFileName
[0] == '\0') {
219 pFileName
= mOptions
.VfrFileName
;
220 while ((pPath
= strchr (pFileName
, '\\')) != NULL
) {
221 pFileName
= pPath
+ 1;
224 if (pFileName
== NULL
) {
228 if ((pExt
= strchr (pFileName
, '.')) == NULL
) {
232 strncpy (mOptions
.VfrBaseFileName
, pFileName
, pExt
- pFileName
);
233 mOptions
.VfrBaseFileName
[pExt
- pFileName
] = '\0';
239 CVfrCompiler::SetPkgOutputFileName (
243 if (mOptions
.VfrBaseFileName
[0] == '\0') {
247 strcpy (mOptions
.PkgOutputFileName
, mOptions
.OutputDirectory
);
248 strcat (mOptions
.PkgOutputFileName
, mOptions
.VfrBaseFileName
);
249 strcat (mOptions
.PkgOutputFileName
, VFR_PACKAGE_FILENAME_EXTENSION
);
255 CVfrCompiler::SetCOutputFileName (
259 if (mOptions
.VfrBaseFileName
[0] == '\0') {
263 strcpy (mOptions
.COutputFileName
, mOptions
.OutputDirectory
);
264 strcat (mOptions
.COutputFileName
, mOptions
.VfrBaseFileName
);
265 strcat (mOptions
.COutputFileName
, ".c");
271 CVfrCompiler::SetPreprocessorOutputFileName (
275 if (mOptions
.VfrBaseFileName
[0] == '\0') {
279 strcpy (mOptions
.PreprocessorOutputFileName
, mOptions
.OutputDirectory
);
280 strcat (mOptions
.PreprocessorOutputFileName
, mOptions
.VfrBaseFileName
);
281 strcat (mOptions
.PreprocessorOutputFileName
, VFR_PREPROCESS_FILENAME_EXTENSION
);
287 CVfrCompiler::SetRecordListFileName (
291 if (mOptions
.VfrBaseFileName
[0] == '\0') {
295 strcpy (mOptions
.RecordListFile
, mOptions
.OutputDirectory
);
296 strcat (mOptions
.RecordListFile
, mOptions
.VfrBaseFileName
);
297 strcat (mOptions
.RecordListFile
, VFR_RECORDLIST_FILENAME_EXTENSION
);
302 CVfrCompiler::CVfrCompiler (
307 mPreProcessCmd
= PREPROCESSOR_COMMAND
;
308 mPreProcessOpt
= PREPROCESSOR_OPTIONS
;
310 OptionInitialization(Argc
, Argv
);
312 if ((IS_RUN_STATUS(STATUS_FAILED
)) || (IS_RUN_STATUS(STATUS_DEAD
))) {
316 SET_RUN_STATUS(STATUS_INITIALIZED
);
319 CVfrCompiler::~CVfrCompiler (
323 if (mOptions
.IncludePaths
!= NULL
) {
324 delete mOptions
.IncludePaths
;
325 mOptions
.IncludePaths
= NULL
;
328 if (mOptions
.CPreprocessorOptions
!= NULL
) {
329 delete mOptions
.CPreprocessorOptions
;
330 mOptions
.CPreprocessorOptions
= NULL
;
333 SET_RUN_STATUS(STATUS_DEAD
);
337 CVfrCompiler::Usage (
342 CONST INT8
*Help
[] = {
344 "VfrCompile version " VFR_COMPILER_VERSION
,
346 " Usage: VfrCompile {options} [VfrFile]",
348 " where options include:",
349 " -? or -h prints this help",
350 " -l create an output IFR listing file",
351 " -i IncPath add IncPath to the search path for VFR included files",
352 " -od OutputDir deposit all output files to directory OutputDir (default=cwd)",
353 " -ibin create an IFR HII pack file"
354 " -ppflag C-preprocessor argument",
355 " where parameters include:",
356 " VfrFile name of the input VFR script file",
360 for (Index
= 0; Help
[Index
] != NULL
; Index
++) {
361 fprintf (stdout
, "%s\n", Help
[Index
]);
366 CVfrCompiler::PreProcess (
370 FILE *pVfrFile
= NULL
;
372 INT8
*PreProcessCmd
= NULL
;
374 if (!IS_RUN_STATUS(STATUS_INITIALIZED
)) {
378 if ((pVfrFile
= fopen (mOptions
.VfrFileName
, "r")) == NULL
) {
379 printf ("%s could not open input VFR file - %s\n", PROGRAM_NAME
, mOptions
.VfrFileName
);
384 CmdLen
= strlen (mPreProcessCmd
) + strlen (mPreProcessOpt
) +
385 strlen (mOptions
.VfrFileName
) + strlen (mOptions
.PreprocessorOutputFileName
);
386 if (mOptions
.CPreprocessorOptions
!= NULL
) {
387 CmdLen
+= strlen (mOptions
.CPreprocessorOptions
);
389 if (mOptions
.IncludePaths
!= NULL
) {
390 CmdLen
+= strlen (mOptions
.IncludePaths
);
393 PreProcessCmd
= new INT8
[CmdLen
+ 10];
394 if (PreProcessCmd
== NULL
) {
395 printf ("%s could not allocate memory\n", PROGRAM_NAME
);
398 strcpy (PreProcessCmd
, mPreProcessCmd
), strcat (PreProcessCmd
, " ");
399 strcat (PreProcessCmd
, mPreProcessOpt
), strcat (PreProcessCmd
, " ");
400 if (mOptions
.IncludePaths
!= NULL
) {
401 strcat (PreProcessCmd
, mOptions
.IncludePaths
), strcat (PreProcessCmd
, " ");
403 if (mOptions
.CPreprocessorOptions
!= NULL
) {
404 strcat (PreProcessCmd
, mOptions
.CPreprocessorOptions
), strcat (PreProcessCmd
, " ");
406 strcat (PreProcessCmd
, mOptions
.VfrFileName
), strcat (PreProcessCmd
, " > ");
407 strcat (PreProcessCmd
, mOptions
.PreprocessorOutputFileName
);
409 if (system (PreProcessCmd
) != 0) {
410 printf ("%s failed to spawn C preprocessor on VFR file \n\t - %s\n", PROGRAM_NAME
, PreProcessCmd
);
414 delete PreProcessCmd
;
415 SET_RUN_STATUS (STATUS_PREPROCESSED
);
419 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
420 SET_RUN_STATUS (STATUS_FAILED
);
422 delete PreProcessCmd
;
425 extern UINT8
VfrParserStart (IN
FILE *);
428 CVfrCompiler::Compile (
432 FILE *VfrFile
= NULL
;
434 if (!IS_RUN_STATUS(STATUS_PREPROCESSED
)) {
438 if ((VfrFile
= fopen (mOptions
.PreprocessorOutputFileName
, "r")) == NULL
) {
439 printf ("%s failed to open input VFR preprocessor output file - %s\n", PROGRAM_NAME
, mOptions
.PreprocessorOutputFileName
);
443 if (VfrParserStart (VfrFile
) != 0) {
449 if (gCFormPkg
.HavePendingUnassigned () == TRUE
) {
450 gCFormPkg
.PendingAssignPrintAll ();
454 SET_RUN_STATUS (STATUS_COMPILEED
);
458 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
459 printf ("%s compile error!\n", PROGRAM_NAME
);
460 SET_RUN_STATUS (STATUS_FAILED
);
462 if (VfrFile
!= NULL
) {
468 CVfrCompiler::GenBinary (
474 if (!IS_RUN_STATUS(STATUS_COMPILEED
)) {
478 if (mOptions
.CreateIfrPkgFile
== TRUE
) {
479 if ((pFile
= fopen (mOptions
.PkgOutputFileName
, "wb")) == NULL
) {
480 printf ("can not open PkgFileName\n", mOptions
.PkgOutputFileName
);
483 if (gCFormPkg
.BuildPkg (pFile
) != VFR_RETURN_SUCCESS
) {
490 SET_RUN_STATUS (STATUS_GENBINARY
);
494 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
495 SET_RUN_STATUS (STATUS_FAILED
);
499 static const char *gSourceFileHeader
[] = {
501 "// DO NOT EDIT -- auto-generated file",
503 "// This file is generated by the vfrcompiler utility",
509 CVfrCompiler::GenCFile (
516 if (!IS_RUN_STATUS(STATUS_GENBINARY
)) {
520 if ((pFile
= fopen (mOptions
.COutputFileName
, "w")) == NULL
) {
521 printf ("failed to open output C file - %s\n", mOptions
.COutputFileName
);
525 for (Index
= 0; gSourceFileHeader
[Index
] != NULL
; Index
++) {
526 fprintf (pFile
, "%s\n", gSourceFileHeader
[Index
]);
529 gCVfrBufferConfig
.OutputCFile (pFile
, mOptions
.VfrBaseFileName
);
531 if (gCFormPkg
.GenCFile (mOptions
.VfrBaseFileName
, pFile
) != VFR_RETURN_SUCCESS
) {
537 SET_RUN_STATUS (STATUS_FINISHED
);
541 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
542 SET_RUN_STATUS (STATUS_FAILED
);
547 CVfrCompiler::GenRecordListFile (
551 FILE *pInFile
= NULL
;
552 FILE *pOutFile
= NULL
;
553 INT8 LineBuf
[MAX_LINE_LEN
];
556 if (mOptions
.CreateRecordListFile
== TRUE
) {
557 if ((mOptions
.PreprocessorOutputFileName
[0] == '\0') || (mOptions
.RecordListFile
[0] == '\0')) {
561 if ((pInFile
= fopen (mOptions
.PreprocessorOutputFileName
, "r")) == NULL
) {
562 printf ("%s failed to open input VFR preprocessor output file - %s\n", PROGRAM_NAME
, mOptions
.PreprocessorOutputFileName
);
566 if ((pOutFile
= fopen (mOptions
.RecordListFile
, "w")) == NULL
) {
567 printf ("%s failed to open record list file for writing - %s\n", PROGRAM_NAME
, mOptions
.RecordListFile
);
571 fprintf (pOutFile
, "//\n// VFR compiler version " VFR_COMPILER_VERSION
"\n//\n");
573 while (!feof (pInFile
)) {
574 if (fgets (LineBuf
, MAX_LINE_LEN
, pInFile
) != NULL
) {
575 fprintf (pOutFile
, "%s", LineBuf
);
577 gCIfrRecordInfoDB
.IfrRecordOutput (pOutFile
, LineNo
);
597 COMPILER_RUN_STATUS Status
;
598 CVfrCompiler
Compiler(Argc
, Argv
);
600 Compiler
.PreProcess();
602 Compiler
.GenBinary();
604 Compiler
.GenRecordListFile ();
606 Status
= Compiler
.RunStatus ();
607 if ((Status
== STATUS_DEAD
) || (Status
== STATUS_FAILED
)) {