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
.SkipCPreprocessor
= FALSE
;
61 mOptions
.CPreprocessorOptions
= NULL
;
63 for (Index
= 1; (Index
< Argc
) && (Argv
[Index
][0] == '-'); Index
++) {
64 if ((_stricmp(Argv
[Index
], "-?") == 0) || (_stricmp(Argv
[Index
], "-h") == 0)) {
66 SET_RUN_STATUS (STATUS_DEAD
);
68 } else if (_stricmp(Argv
[Index
], "-l") == 0) {
69 mOptions
.CreateRecordListFile
= TRUE
;
70 gCIfrRecordInfoDB
.TurnOn ();
71 } else if (_stricmp(Argv
[Index
], "-i") == 0) {
73 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
74 printf ("%s -i - missing path argument\n", UTILITY_NAME
);
78 AppendIncludePath(Argv
[Index
]);
79 } else if (_stricmp(Argv
[Index
], "-od") == 0) {
81 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
82 printf ("%s -od - missing output directory name\n", UTILITY_NAME
);
85 strcpy (mOptions
.OutputDirectory
, Argv
[Index
]);
86 strcat (mOptions
.OutputDirectory
, "\\");
87 } else if (_stricmp(Argv
[Index
], "-ibin") == 0) {
88 mOptions
.CreateIfrPkgFile
= TRUE
;
89 } else if (_stricmp(Argv
[Index
], "-nostrings") == 0) {
90 } else if (_stricmp(Argv
[Index
], "-nopp") == 0) {
91 mOptions
.SkipCPreprocessor
= TRUE
;
92 } else if (_stricmp(Argv
[Index
], "-ppflag") == 0) {
94 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
95 printf ("%s -od - missing C-preprocessor argument\n", UTILITY_NAME
);
99 AppendCPreprocessorOptions (Argv
[Index
]);
101 printf ("%s unrecognized option %s\n", UTILITY_NAME
, Argv
[Index
]);
107 if (Index
!= Argc
- 1) {
108 printf ("%s must specify VFR file name\n", UTILITY_NAME
);
112 strcpy (mOptions
.VfrFileName
, Argv
[Index
]);
115 if (SetBaseFileName() != 0) {
118 if (SetPkgOutputFileName () != 0) {
121 if (SetCOutputFileName() != 0) {
124 if (SetPreprocessorOutputFileName () != 0) {
127 if (SetRecordListFileName () != 0) {
133 SET_RUN_STATUS (STATUS_FAILED
);
135 mOptions
.VfrFileName
[0] = '\0';
136 mOptions
.RecordListFile
[0] = '\0';
137 mOptions
.CreateRecordListFile
= FALSE
;
138 mOptions
.CreateIfrPkgFile
= FALSE
;
139 mOptions
.PkgOutputFileName
[0] = '\0';
140 mOptions
.COutputFileName
[0] = '\0';
141 mOptions
.OutputDirectory
[0] = '\0';
142 mOptions
.PreprocessorOutputFileName
[0] = '\0';
143 mOptions
.VfrBaseFileName
[0] = '\0';
144 if (mOptions
.IncludePaths
!= NULL
) {
145 delete mOptions
.IncludePaths
;
146 mOptions
.IncludePaths
= NULL
;
148 if (mOptions
.CPreprocessorOptions
!= NULL
) {
149 delete mOptions
.CPreprocessorOptions
;
150 mOptions
.CPreprocessorOptions
= NULL
;
155 CVfrCompiler::AppendIncludePath (
160 INT8
*IncludePaths
= NULL
;
162 Len
= strlen (" -I ") + strlen (PathStr
) + 1;
163 if (mOptions
.IncludePaths
!= NULL
) {
164 Len
+= strlen (mOptions
.IncludePaths
);
166 IncludePaths
= new INT8
[Len
];
167 if (IncludePaths
== NULL
) {
168 printf ("%s memory allocation failure\n", UTILITY_NAME
);
171 IncludePaths
[0] = '\0';
172 if (mOptions
.IncludePaths
!= NULL
) {
173 strcat (IncludePaths
, mOptions
.IncludePaths
);
175 strcat (IncludePaths
, " -I ");
176 strcat (IncludePaths
, PathStr
);
177 if (mOptions
.IncludePaths
!= NULL
) {
178 delete mOptions
.IncludePaths
;
180 mOptions
.IncludePaths
= IncludePaths
;
184 CVfrCompiler::AppendCPreprocessorOptions (
191 Len
= strlen (Options
) + strlen (" ") + 1;
192 if (mOptions
.CPreprocessorOptions
!= NULL
) {
193 Len
+= strlen (mOptions
.CPreprocessorOptions
);
197 printf ("%s memory allocation failure\n", UTILITY_NAME
);
201 if (mOptions
.CPreprocessorOptions
!= NULL
) {
202 strcat (Opt
, mOptions
.CPreprocessorOptions
);
205 strcat (Opt
, Options
);
206 if (mOptions
.CPreprocessorOptions
!= NULL
) {
207 delete mOptions
.CPreprocessorOptions
;
209 mOptions
.CPreprocessorOptions
= Opt
;
213 CVfrCompiler::SetBaseFileName (
217 INT8
*pFileName
, *pPath
, *pExt
;
219 if (mOptions
.VfrFileName
[0] == '\0') {
223 pFileName
= mOptions
.VfrFileName
;
225 ((pPath
= strchr (pFileName
, '\\')) != NULL
) ||
226 ((pPath
= strchr (pFileName
, '/')) != NULL
)
229 pFileName
= pPath
+ 1;
232 if (pFileName
== NULL
) {
236 if ((pExt
= strchr (pFileName
, '.')) == NULL
) {
240 strncpy (mOptions
.VfrBaseFileName
, pFileName
, pExt
- pFileName
);
241 mOptions
.VfrBaseFileName
[pExt
- pFileName
] = '\0';
247 CVfrCompiler::SetPkgOutputFileName (
251 if (mOptions
.VfrBaseFileName
[0] == '\0') {
255 strcpy (mOptions
.PkgOutputFileName
, mOptions
.OutputDirectory
);
256 strcat (mOptions
.PkgOutputFileName
, mOptions
.VfrBaseFileName
);
257 strcat (mOptions
.PkgOutputFileName
, VFR_PACKAGE_FILENAME_EXTENSION
);
263 CVfrCompiler::SetCOutputFileName (
267 if (mOptions
.VfrBaseFileName
[0] == '\0') {
271 strcpy (mOptions
.COutputFileName
, mOptions
.OutputDirectory
);
272 strcat (mOptions
.COutputFileName
, mOptions
.VfrBaseFileName
);
273 strcat (mOptions
.COutputFileName
, ".c");
279 CVfrCompiler::SetPreprocessorOutputFileName (
283 if (mOptions
.VfrBaseFileName
[0] == '\0') {
287 strcpy (mOptions
.PreprocessorOutputFileName
, mOptions
.OutputDirectory
);
288 strcat (mOptions
.PreprocessorOutputFileName
, mOptions
.VfrBaseFileName
);
289 strcat (mOptions
.PreprocessorOutputFileName
, VFR_PREPROCESS_FILENAME_EXTENSION
);
295 CVfrCompiler::SetRecordListFileName (
299 if (mOptions
.VfrBaseFileName
[0] == '\0') {
303 strcpy (mOptions
.RecordListFile
, mOptions
.OutputDirectory
);
304 strcat (mOptions
.RecordListFile
, mOptions
.VfrBaseFileName
);
305 strcat (mOptions
.RecordListFile
, VFR_RECORDLIST_FILENAME_EXTENSION
);
310 CVfrCompiler::CVfrCompiler (
315 mPreProcessCmd
= PREPROCESSOR_COMMAND
;
316 mPreProcessOpt
= PREPROCESSOR_OPTIONS
;
318 OptionInitialization(Argc
, Argv
);
320 if ((IS_RUN_STATUS(STATUS_FAILED
)) || (IS_RUN_STATUS(STATUS_DEAD
))) {
324 SET_RUN_STATUS(STATUS_INITIALIZED
);
327 CVfrCompiler::~CVfrCompiler (
331 if (mOptions
.IncludePaths
!= NULL
) {
332 delete mOptions
.IncludePaths
;
333 mOptions
.IncludePaths
= NULL
;
336 if (mOptions
.CPreprocessorOptions
!= NULL
) {
337 delete mOptions
.CPreprocessorOptions
;
338 mOptions
.CPreprocessorOptions
= NULL
;
341 SET_RUN_STATUS(STATUS_DEAD
);
345 CVfrCompiler::Usage (
350 const char *Str
[] = {
351 UTILITY_NAME
" "UTILITY_VERSION
" - Intel UEFI VFR Compiler Utility",
352 " Copyright (C), 2004 - 2008 Intel Corporation",
353 #if ( defined(UTILITY_BUILD) && defined(UTILITY_VENDOR) )
354 " Built from "UTILITY_BUILD
", project of "UTILITY_VENDOR
,
358 " "UTILITY_NAME
" [OPTION] VFRFILE",
362 " -? or -h print this help",
363 " -l create an output IFR listing file",
364 " -i IncPath add IncPath to the search path for VFR included files",
365 " -od OutputDir deposit all output files to directory OutputDir (default=cwd)",
366 " -ibin create an IFR HII pack file",
367 " -ppflag CFlags pass Flags as C-preprocessor-flag",
368 " -v or -version print version information",
372 for (Index
= 0; Str
[Index
] != NULL
; Index
++) {
373 fprintf (stdout
, "%s\n", Str
[Index
]);
379 CVfrCompiler::PreProcess (
383 FILE *pVfrFile
= NULL
;
385 INT8
*PreProcessCmd
= NULL
;
387 if (!IS_RUN_STATUS(STATUS_INITIALIZED
)) {
391 if (mOptions
.SkipCPreprocessor
== TRUE
) {
395 if ((pVfrFile
= fopen (mOptions
.VfrFileName
, "r")) == NULL
) {
396 printf ("%s could not open input VFR file - %s\n", UTILITY_NAME
, mOptions
.VfrFileName
);
401 CmdLen
= strlen (mPreProcessCmd
) + strlen (mPreProcessOpt
) +
402 strlen (mOptions
.VfrFileName
) + strlen (mOptions
.PreprocessorOutputFileName
);
403 if (mOptions
.CPreprocessorOptions
!= NULL
) {
404 CmdLen
+= strlen (mOptions
.CPreprocessorOptions
);
406 if (mOptions
.IncludePaths
!= NULL
) {
407 CmdLen
+= strlen (mOptions
.IncludePaths
);
410 PreProcessCmd
= new INT8
[CmdLen
+ 10];
411 if (PreProcessCmd
== NULL
) {
412 printf ("%s could not allocate memory\n", UTILITY_NAME
);
415 strcpy (PreProcessCmd
, mPreProcessCmd
), strcat (PreProcessCmd
, " ");
416 strcat (PreProcessCmd
, mPreProcessOpt
), strcat (PreProcessCmd
, " ");
417 if (mOptions
.IncludePaths
!= NULL
) {
418 strcat (PreProcessCmd
, mOptions
.IncludePaths
), strcat (PreProcessCmd
, " ");
420 if (mOptions
.CPreprocessorOptions
!= NULL
) {
421 strcat (PreProcessCmd
, mOptions
.CPreprocessorOptions
), strcat (PreProcessCmd
, " ");
423 strcat (PreProcessCmd
, mOptions
.VfrFileName
), strcat (PreProcessCmd
, " > ");
424 strcat (PreProcessCmd
, mOptions
.PreprocessorOutputFileName
);
426 if (system (PreProcessCmd
) != 0) {
427 printf ("%s failed to spawn C preprocessor on VFR file \n\t - %s\n", UTILITY_NAME
, PreProcessCmd
);
431 delete PreProcessCmd
;
434 SET_RUN_STATUS (STATUS_PREPROCESSED
);
438 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
439 SET_RUN_STATUS (STATUS_FAILED
);
441 delete PreProcessCmd
;
444 extern UINT8
VfrParserStart (IN
FILE *);
447 CVfrCompiler::Compile (
451 FILE *pInFile
= NULL
;
452 INT8
*InFileName
= NULL
;
454 if (!IS_RUN_STATUS(STATUS_PREPROCESSED
)) {
458 InFileName
= (mOptions
.SkipCPreprocessor
== TRUE
) ? mOptions
.VfrFileName
: mOptions
.PreprocessorOutputFileName
;
460 gCVfrErrorHandle
.SetInputFile (InFileName
);
462 if ((pInFile
= fopen (InFileName
, "r")) == NULL
) {
463 printf ("%s failed to open input file - %s\n", UTILITY_NAME
, InFileName
);
467 if (VfrParserStart (pInFile
) != 0) {
473 if (gCFormPkg
.HavePendingUnassigned () == TRUE
) {
474 gCFormPkg
.PendingAssignPrintAll ();
478 SET_RUN_STATUS (STATUS_COMPILEED
);
482 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
483 printf ("%s compile error!\n", UTILITY_NAME
);
484 SET_RUN_STATUS (STATUS_FAILED
);
486 if (pInFile
!= NULL
) {
492 CVfrCompiler::GenBinary (
498 if (!IS_RUN_STATUS(STATUS_COMPILEED
)) {
502 if (mOptions
.CreateIfrPkgFile
== TRUE
) {
503 if ((pFile
= fopen (mOptions
.PkgOutputFileName
, "wb")) == NULL
) {
504 printf ("can not open %s\n", mOptions
.PkgOutputFileName
);
507 if (gCFormPkg
.BuildPkg (pFile
) != VFR_RETURN_SUCCESS
) {
514 SET_RUN_STATUS (STATUS_GENBINARY
);
518 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
519 SET_RUN_STATUS (STATUS_FAILED
);
523 static const char *gSourceFileHeader
[] = {
525 "// DO NOT EDIT -- auto-generated file",
527 "// This file is generated by the vfrcompiler utility",
533 CVfrCompiler::GenCFile (
540 if (!IS_RUN_STATUS(STATUS_GENBINARY
)) {
544 if ((pFile
= fopen (mOptions
.COutputFileName
, "w")) == NULL
) {
545 printf ("failed to open output C file - %s\n", mOptions
.COutputFileName
);
549 for (Index
= 0; gSourceFileHeader
[Index
] != NULL
; Index
++) {
550 fprintf (pFile
, "%s\n", gSourceFileHeader
[Index
]);
553 gCVfrBufferConfig
.OutputCFile (pFile
, mOptions
.VfrBaseFileName
);
555 if (gCFormPkg
.GenCFile (mOptions
.VfrBaseFileName
, pFile
) != VFR_RETURN_SUCCESS
) {
561 SET_RUN_STATUS (STATUS_FINISHED
);
565 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
566 SET_RUN_STATUS (STATUS_FAILED
);
571 CVfrCompiler::GenRecordListFile (
575 INT8
*InFileName
= NULL
;
576 FILE *pInFile
= NULL
;
577 FILE *pOutFile
= NULL
;
578 INT8 LineBuf
[MAX_LINE_LEN
];
581 InFileName
= (mOptions
.SkipCPreprocessor
== TRUE
) ? mOptions
.VfrFileName
: mOptions
.PreprocessorOutputFileName
;
583 if (mOptions
.CreateRecordListFile
== TRUE
) {
584 if ((InFileName
[0] == '\0') || (mOptions
.RecordListFile
[0] == '\0')) {
588 if ((pInFile
= fopen (InFileName
, "r")) == NULL
) {
589 printf ("%s failed to open input VFR preprocessor output file - %s\n", UTILITY_NAME
, InFileName
);
593 if ((pOutFile
= fopen (mOptions
.RecordListFile
, "w")) == NULL
) {
594 printf ("%s failed to open record list file for writing - %s\n", UTILITY_NAME
, mOptions
.RecordListFile
);
598 fprintf (pOutFile
, "//\n// VFR compiler version " UTILITY_VERSION
"\n//\n");
600 while (!feof (pInFile
)) {
601 if (fgets (LineBuf
, MAX_LINE_LEN
, pInFile
) != NULL
) {
602 fprintf (pOutFile
, "%s", LineBuf
);
604 gCIfrRecordInfoDB
.IfrRecordOutput (pOutFile
, LineNo
);
624 COMPILER_RUN_STATUS Status
;
625 CVfrCompiler
Compiler(Argc
, Argv
);
627 Compiler
.PreProcess();
629 Compiler
.GenBinary();
631 Compiler
.GenRecordListFile ();
633 Status
= Compiler
.RunStatus ();
634 if ((Status
== STATUS_DEAD
) || (Status
== STATUS_FAILED
)) {