hc
2024-03-25 edb30157bad0c0001c32b854271ace01d3b9a16a
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
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
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
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
1098
1099
1100
1101
1102
1103
1104
1105
1106
#!/usr/bin/env python3
#
# @ build_bios.py
# Builds BIOS using configuration files and dynamically
# imported functions from board directory
#
# Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>
# Copyright (c) 2021, American Megatrends International LLC.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
 
"""
This module builds BIOS using configuration files and dynamically
imported functions from board directory
"""
 
 
import os
import re
import sys
import glob
import signal
import shutil
import argparse
import traceback
import subprocess
from importlib import import_module
 
try:
    # python 2.7
    import ConfigParser as configparser
except ImportError:
    # python 3
    import configparser
 
 
def pre_build(build_config, build_type="DEBUG", silent=False, toolchain=None):
    """Sets the environment variables that shall be used for the build
 
        :param build_config: The build configuration as defined in the JSON
            configuration files
        :type build_config: Dictionary
        :param build_type: The build target, DEBUG, RELEASE, RELEASE_PDB,
            TEST_RELEASE
        :type build_type: String
        :param silent: Enables build in silent mode
        :type silent: Boolean
        :param toolchain: Specifies the tool chain tag to use for the build
        :type toolchain: String
        :returns: The updated environment variables
        :rtype: Dictionary
    """
 
    # get current environment variables
    config = os.environ.copy()
 
    # patch the build config
    build_config = patch_config(build_config)
 
    # update the current config with the build config
    config.update(build_config)
 
    # make the config and build python 2.7 compatible
    config = py_27_fix(config)
 
    # Set WORKSPACE environment.
    config["WORKSPACE"] = os.path.abspath(os.path.join("..", "..", "..", ""))
    print("Set WORKSPACE as: {}".format(config["WORKSPACE"]))
 
    # Check whether Git has been installed and been added to system path.
    try:
        subprocess.Popen(["git", "--help"], stdout=subprocess.PIPE)
    except OSError:
        print("The 'git' command is not recognized.")
        print("Please make sure that Git is installed\
                and has been added to system path.")
        sys.exit(1)
 
    # Create the Conf directory under WORKSPACE
    if not os.path.isdir(os.path.join(config["WORKSPACE"], "Conf")):
        try:
            # create directory
            os.makedirs(os.path.join(config["WORKSPACE"], "Conf"))
            # copy files to it
            config_template_path = os.path.join(config["WORKSPACE"],
                                                config["BASE_TOOLS_PATH"],
                                                "Conf")
            config_path = os.path.join(config["WORKSPACE"], "Conf")
            shutil.copyfile(config_template_path +
                            os.sep + "target.template",
                            config_path + os.sep + "target.txt")
            shutil.copyfile(config_template_path +
                            os.sep + "tools_def.template",
                            config_path + os.sep + "tools_def.txt")
            shutil.copyfile(config_template_path +
                            os.sep + "build_rule.template",
                            config_path + os.sep + "build_rule.txt")
        except OSError:
            print("Error while creating Conf")
            sys.exit(1)
 
    # Set other environments.
    # Basic Rule:
    # Platform override Silicon override Core
    # Source override Binary
    config["WORKSPACE_PLATFORM"] = os.path.join(config["WORKSPACE"],
                                                config["WORKSPACE_PLATFORM"])
    config["WORKSPACE_SILICON"] = os.path.join(config["WORKSPACE"],
                                               config["WORKSPACE_SILICON"])
    config["WORKSPACE_FEATURES"] = os.path.join(config["WORKSPACE"],
                                               config["WORKSPACE_FEATURES"])
    config["WORKSPACE_DRIVERS"] = os.path.join(config["WORKSPACE"],
                                               config["WORKSPACE_DRIVERS"])
    config["WORKSPACE_PLATFORM_BIN"] = \
        os.path.join(config["WORKSPACE"], config["WORKSPACE_PLATFORM_BIN"])
    config["WORKSPACE_SILICON_BIN"] = \
        os.path.join(config["WORKSPACE"], config["WORKSPACE_SILICON_BIN"])
    config["WORKSPACE_FSP_BIN"] = os.path.join(config["WORKSPACE"],
                                               config["WORKSPACE_FSP_BIN"])
 
    # add to package path
    config["PACKAGES_PATH"] = config["WORKSPACE_PLATFORM"]
    config["PACKAGES_PATH"] += os.pathsep + config["WORKSPACE_SILICON"]
    config["PACKAGES_PATH"] += os.pathsep + config["WORKSPACE_SILICON_BIN"]
    config["PACKAGES_PATH"] += os.pathsep + config["WORKSPACE_FEATURES"]
    # add all feature domains in WORKSPACE_FEATURES to package path
    for filename in os.listdir(config["WORKSPACE_FEATURES"]):
        filepath = os.path.join(config["WORKSPACE_FEATURES"], filename)
        # feature domains folder does not contain dec file
        if os.path.isdir(filepath) and \
          not glob.glob(os.path.join(filepath, "*.dec")):
            config["PACKAGES_PATH"] += os.pathsep + filepath
    config["PACKAGES_PATH"] += os.pathsep + config["WORKSPACE_DRIVERS"]
    config["PACKAGES_PATH"] += os.pathsep + \
        os.path.join(config["WORKSPACE"], config["WORKSPACE_FSP_BIN"])
    config["PACKAGES_PATH"] += os.pathsep + \
        os.path.join(config["WORKSPACE"], config["WORKSPACE_CORE"])
    config["PACKAGES_PATH"] += os.pathsep + os.path.join(config["WORKSPACE"])
    config["PACKAGES_PATH"] += os.pathsep + config["WORKSPACE_PLATFORM_BIN"]
    config["EDK_TOOLS_PATH"] = os.path.join(config["WORKSPACE"],
                                            config["EDK_TOOLS_PATH"])
    config["BASE_TOOLS_PATH"] = config["EDK_TOOLS_PATH"]
    config["EDK_TOOLS_BIN"] = os.path.join(config["WORKSPACE"],
                                           config["EDK_TOOLS_BIN"])
 
    #
    # Board may have different FSP binary between API and Dispatch modes.
    # In API mode if FSP_BIN_PKG_FOR_API_MODE is assigned, it should
    # override FSP_BIN_PKG.
    #
    if config.get("API_MODE_FSP_WRAPPER_BUILD", "FALSE") == "TRUE":
        if config.get("FSP_BIN_PKG_FOR_API_MODE") is not None:
            config['FSP_BIN_PKG'] = config['FSP_BIN_PKG_FOR_API_MODE']
 
    config["PLATFORM_FSP_BIN_PACKAGE"] = \
        os.path.join(config['WORKSPACE_FSP_BIN'], config['FSP_BIN_PKG'])
    config['PROJECT_DSC'] = os.path.join(config["WORKSPACE_PLATFORM"],
                                         config['PROJECT_DSC'])
    config['BOARD_PKG_PCD_DSC'] = os.path.join(config["WORKSPACE_PLATFORM"],
                                               config['BOARD_PKG_PCD_DSC'])
    config["CONF_PATH"] = os.path.join(config["WORKSPACE"], "Conf")
 
    # get the python path
    if os.environ.get("PYTHON_HOME") is None:
        if os.environ.get("PYTHONPATH") is not None:
            config["PYTHON_HOME"] = os.environ.get("PYTHONPATH")
        else:
            config["PYTHON_HOME"] = os.path.dirname(sys.executable)
            config["PYTHONPATH"] = config["PYTHON_HOME"]
 
    if config.get("PYTHON_HOME") is None or \
       not os.path.exists(config.get("PYTHON_HOME")):
        print("PYTHON_HOME environment variable is not found")
        sys.exit(1)
 
    # if python is installed, disable the binary base tools.
    # python is installed if this code is running :)
    if config.get("PYTHON_HOME") is not None:
        if config.get("EDK_TOOLS_BIN") is not None:
            del config["EDK_TOOLS_BIN"]
 
    # Run edk setup and  update config
    if os.name == 'nt':
        edk2_setup_cmd = [os.path.join(config["EFI_SOURCE"], "edksetup"),
                          "Rebuild"]
 
        if config.get("EDK_SETUP_OPTION") and \
           config["EDK_SETUP_OPTION"] != " ":
            edk2_setup_cmd.append(config["EDK_SETUP_OPTION"])
 
        _, _, result, return_code = execute_script(edk2_setup_cmd,
                                                   config,
                                                   collect_env=True,
                                                   shell=True)
        if return_code == 0 and result is not None and isinstance(result,
                                                                  dict):
            config.update(result)
 
    # nmake BaseTools source
    # and enable BaseTools source build
    shell = True
    command = ["nmake", "-f", os.path.join(config["BASE_TOOLS_PATH"],
                                           "Makefile")]
    if os.name == "posix":  # linux
        shell = False
        command = ["make", "-C", os.path.join(config["BASE_TOOLS_PATH"])]
 
    _, _, result, return_code = execute_script(command, config, shell=shell)
    if return_code != 0:
        build_failed(config)
 
    #
    # build platform silicon tools
    #
    # save the current workspace
    saved_work_directory = config["WORKSPACE"]
    # change the workspace to silicon tools directory
    config["WORKSPACE"] = os.path.join(config["WORKSPACE_SILICON"], "Tools")
 
    command = ["nmake"]
    if os.name == "posix":  # linux
        command = ["make"]
        # add path to generated FitGen binary to
        # environment path variable
        config["PATH"] += os.pathsep + \
                          os.path.join(config["BASE_TOOLS_PATH"],
                                       "Source", "C", "bin")
 
    # build the silicon tools
    _, _, result, return_code = execute_script(command, config, shell=shell)
    if return_code != 0:
        build_failed(config)
 
    # restore WORKSPACE environment variable
    config["WORKSPACE"] = saved_work_directory
 
    config["SILENT_MODE"] = 'TRUE' if silent else 'FALSE'
 
    print("==============================================")
 
    if os.path.isfile(os.path.join(config['WORKSPACE'], "Prep.log")):
        os.remove(os.path.join(config['WORKSPACE'], "Prep.log"))
 
    config["PROJECT"] = os.path.join(config["PLATFORM_BOARD_PACKAGE"],
                                     config["BOARD"])
 
    # Setup Build
    # @todo: Need better TOOL_CHAIN_TAG detection
    if toolchain is not None:
        config["TOOL_CHAIN_TAG"] = toolchain
    elif config.get("TOOL_CHAIN_TAG") is None:
        if os.name == 'nt':
            config["TOOL_CHAIN_TAG"] = "VS2015"
        else:
            config["TOOL_CHAIN_TAG"] = "GCC5"
 
    # echo Show CL revision
    config["PrepRELEASE"] = build_type
 
    if build_type == "DEBUG":
        config["TARGET"] = 'DEBUG'
        config["TARGET_SHORT"] = 'D'
    else:
        config["TARGET"] = 'RELEASE'
        config["TARGET_SHORT"] = 'R'
 
    # set BUILD_DIR_PATH path
    config["BUILD_DIR_PATH"] = os.path.join(config["WORKSPACE"],
                                            'Build',
                                            config["PROJECT"],
                                            "{}_{}".format(
                                                config["TARGET"],
                                                config["TOOL_CHAIN_TAG"]))
    # set BUILD_DIR path
    config["BUILD_DIR"] = os.path.join('Build',
                                       config["PROJECT"],
                                       "{}_{}".format(
                                           config["TARGET"],
                                           config["TOOL_CHAIN_TAG"]))
 
    config["BUILD_X64"] = os.path.join(config["BUILD_DIR_PATH"], 'X64')
    config["BUILD_IA32"] = os.path.join(config["BUILD_DIR_PATH"], 'IA32')
 
    if not os.path.isdir(config["BUILD_DIR_PATH"]):
        try:
            os.makedirs(config["BUILD_DIR_PATH"])
        except OSError:
            print("Error while creating Build folder")
            sys.exit(1)
 
    # Set FSP_WRAPPER_BUILD
    if config['FSP_WRAPPER_BUILD'] == "TRUE":
        # Create dummy Fsp_Rebased_S_padded.fd to build the BiosInfo.inf
        # if it is wrapper build, due to the SECTION inclusion
        open(os.path.join(config["WORKSPACE_FSP_BIN"],
                          config["FSP_BIN_PKG"],
                          "Fsp_Rebased_S_padded.fd"), 'w').close()
 
    if not os.path.isdir(config["BUILD_X64"]):
        try:
            os.mkdir(config["BUILD_X64"])
        except OSError:
            print("Error while creating {}".format(config["BUILD_X64"]))
            sys.exit(1)
 
    # update config file with changes
    update_target_file(config)
 
    # Additional pre build scripts for this platform
    result = pre_build_ex(config)
    if result is not None and isinstance(result, dict):
        config.update(result)
 
    # print user settings
    print("BIOS_SIZE_OPTION     = {}".format(config["BIOS_SIZE_OPTION"]))
    print("EFI_SOURCE           = {}".format(config["EFI_SOURCE"]))
    print("TARGET               = {}".format(config["TARGET"]))
    print("TARGET_ARCH          = {}".format("IA32 X64"))
    print("TOOL_CHAIN_TAG       = {}".format(config["TOOL_CHAIN_TAG"]))
    print("WORKSPACE            = {}".format(config["WORKSPACE"]))
    print("WORKSPACE_CORE       = {}".format(config["WORKSPACE_CORE"]))
    print("EXT_BUILD_FLAGS      = {}".format(config["EXT_BUILD_FLAGS"]))
 
    return config
 
 
def build(config):
    """Builds the BIOS image
 
        :param config: The environment variables to be used
            in the build process
        :type config: Dictionary
        :returns: nothing
    """
 
    if config["FSP_WRAPPER_BUILD"] == "TRUE":
        pattern = "Fsp_Rebased.*\\.fd$"
        file_dir = os.path.join(config['WORKSPACE_FSP_BIN'],
                                config['FSP_BIN_PKG'])
        for item in os.listdir(file_dir):
            if re.search(pattern, item):
                os.remove(os.path.join(file_dir, item))
 
        command = [os.path.join(config['PYTHON_HOME'], "python"),
                   os.path.join(config['WORKSPACE_PLATFORM'],
                                config['PLATFORM_PACKAGE'],
                                'Tools', 'Fsp',
                                'RebaseFspBinBaseAddress.py'),
                   os.path.join(config['WORKSPACE_PLATFORM'],
                                config['FLASH_MAP_FDF']),
                   os.path.join(config['WORKSPACE_FSP_BIN'],
                                config['FSP_BIN_PKG']),
                   "Fsp.fd",
                   "0x0"]
 
        _, _, _, return_code = execute_script(command, config, shell=False)
 
        if return_code != 0:
            print("ERROR:RebaseFspBinBaseAddress failed")
            sys.exit(return_code)
 
        # create Fsp_Rebased.fd which is Fsp_Rebased_S.fd +
        # Fsp_Rebased_M + Fsp_Rebased_T
        with open(os.path.join(file_dir, "Fsp_Rebased_S.fd"), 'rb') as fsp_s, \
                open(os.path.join(file_dir,
                                  "Fsp_Rebased_M.fd"), 'rb') as fsp_m, \
                open(os.path.join(file_dir,
                                  "Fsp_Rebased_T.fd"), 'rb') as fsp_t:
 
            fsp_rebased = fsp_s.read() + fsp_m.read() + fsp_t.read()
            with open(os.path.join(file_dir,
                                   "Fsp_Rebased.fd"), 'wb') as new_fsp:
                new_fsp.write(fsp_rebased)
 
        if not os.path.isfile(os.path.join(file_dir, "Fsp_Rebased.fd")):
            print("!!! ERROR:failed to create fsp!!!")
            sys.exit(1)
 
    # Output the build variables the user has selected.
    print("==========================================")
    print(" User Selected build options:")
    print(" SILENT_MODE    = ", config.get("SILENT_MODE"))
    print(" REBUILD_MODE   = ", config.get("REBUILD_MODE"))
    print(" BUILD_ROM_ONLY = ", config.get("BUILD_ROM_ONLY"))
    print(" BINARY_CACHE_CMD_LINE = ", config.get("HASH"), config.get("BINARY_CACHE_CMD_LINE"))
    print("==========================================")
 
    command = ["build", "-n", config["NUMBER_OF_PROCESSORS"]]
 
    if config["REBUILD_MODE"] and config["REBUILD_MODE"] != "":
        command.append(config["REBUILD_MODE"])
 
    if config["EXT_BUILD_FLAGS"] and config["EXT_BUILD_FLAGS"] != "":
        ext_build_flags = config["EXT_BUILD_FLAGS"].split(" ")
        ext_build_flags = [x.strip() for x in ext_build_flags]
        ext_build_flags = [x for x in ext_build_flags if x != ""]
        command.extend(ext_build_flags)
 
    if config.get('BINARY_CACHE_CMD_LINE'):
        command.append(config['HASH'])
        command.append(config['BINARY_CACHE_CMD_LINE'])
 
    if config.get("SILENT_MODE", "FALSE") == "TRUE":
        command.append("--silent")
        command.append("--quiet")
 
    else:
        command.append("--log=" + config.get("BUILD_LOG", "Build.log"))
        command.append("--report-file=" +
                       config.get("BUILD_REPORT", "BuildReport.log"))
 
    if config.get("VERBOSE", "FALSE") == "TRUE":
        command.append("--verbose")
 
    if config.get("MAX_SOCKET") is not None:
        command.append("-D")
        command.append("MAX_SOCKET=" + config["MAX_SOCKET"])
 
    if config.get("API_MODE_FSP_WRAPPER_BUILD", "FALSE") == "TRUE":
        #Override PCD to enable API mode FSP wrapper.
        command.append("--pcd")
        command.append("gIntelFsp2WrapperTokenSpaceGuid.PcdFspModeSelection=1")
 
    shell = True
    if os.name == "posix":
        shell = False
 
    _, _, _, exit_code = execute_script(command, config, shell=shell)
    if exit_code != 0:
        build_failed(config)
 
    # Additional build scripts for this platform
    result = build_ex(config)
    if result is not None and isinstance(result, dict):
        config.update(result)
 
    return config
 
 
def post_build(config):
    """Post build process of BIOS image
 
    :param config: The environment variables to be used in the build process
    :type config: Dictionary
    :returns: nothing
    """
    print("Running post_build to complete the build process.")
    board_fd = config["BOARD"].upper()
    final_fd = os.path.join(config["BUILD_DIR_PATH"], "FV",
                            "{}.fd".format(board_fd))
 
    if config["BIOS_INFO_GUID"]:
        # Generate the fit table
        print("Generating FIT ...")
        if os.path.isfile(final_fd):
            temp_fd = os.path.join(config["BUILD_DIR_PATH"], "FV",
                                   "{}_.fd".format(board_fd))
            shell = True
            command = ["FitGen", "-D",
                       final_fd, temp_fd, "-NA",
                       "-I", config["BIOS_INFO_GUID"]]   #@todo: Need mechanism to add additional options to the FitGen command line
 
            if os.name == "posix": # linux
                shell = False
 
            _, _, result, return_code = execute_script(command, config, shell=shell)
            if return_code != 0:
                print("Error while generating fit")
            else:
                # copy output to final binary
                shutil.copyfile(temp_fd, final_fd)
                # remove temp file
                os.remove(temp_fd)
        else:
            print("{} does not exist".format(final_fd))
            # remove temp file
 
    # Additional build scripts for this platform
    result = post_build_ex(config)
    if result is not None and isinstance(result, dict):
        config.update(result)
 
    # cleanup
    pattern = "Fsp_Rebased.*\\.fd$"
    file_dir = os.path.join(config['WORKSPACE_FSP_BIN'],
                            config['FSP_BIN_PKG'])
    for item in os.listdir(file_dir):
        if re.search(pattern, item):
            os.remove(os.path.join(file_dir, item))
 
    if config.get("DYNAMIC_BUILD_INIT_FILES") is not None:
        for item in config["DYNAMIC_BUILD_INIT_FILES"].split(","):
            try:
                os.remove(item)  # remove __init__.py
                os.remove(item + "c")  # remove __init__.pyc as well
            except OSError:
                pass
 
    print("Done")
    if os.path.isfile(final_fd):
        print("Fd file can be found at {}".format(final_fd))
 
def build_failed(config):
    """Displays results when build fails
 
        :param config: The environment variables used in the build process
        :type config: Dictionary
        :returns: nothing
    """
    print(" The EDKII BIOS Build has failed!")
    # clean up
    if config.get("DYNAMIC_BUILD_INIT_FILES") is not None:
        for item in config["DYNAMIC_BUILD_INIT_FILES"].split(","):
            if os.path.isfile(item):
                try:
                    os.remove(item)  # remove __init__.py
                    os.remove(item + "c")  # remove __init__.pyc as well
                except OSError:
                    pass
    sys.exit(1)
 
 
def import_platform_lib(path, function):
    """Imports custom functions for the platforms being built
 
        :param path: the location of the custom build script to be executed
        :type path: String
        :param path: the function to be executed
        :type path: String
        :returns: nothing
    """
    if path.endswith(".py"):
        path = path[:-3]
    path = path.replace(os.sep, ".")
    module = import_module(path)
    lib = getattr(module, function)
    return lib
 
 
def pre_build_ex(config):
    """ An extension of the pre_build process as defined platform
        specific pre_build setup script
 
        :param config: The environment variables used in the pre build process
        :type config: Dictionary
        :returns: config dictionary
        :rtype: Dictionary
    """
    if config.get("ADDITIONAL_SCRIPTS"):
        try:
            platform_function =\
                import_platform_lib(config["ADDITIONAL_SCRIPTS"],
                                    "pre_build_ex")
            functions = {"execute_script": execute_script}
            return platform_function(config, functions)
        except ImportError as error:
            print(config["ADDITIONAL_SCRIPTS"], str(error))
            build_failed(config)
    return None
 
 
def build_ex(config):
    """ An extension of the build process as defined platform
        specific build setup script
 
        :param config: The environment variables used in the build process
        :type config: Dictionary
        :returns: config dictionary
        :rtype: Dictionary
    """
    if config.get("ADDITIONAL_SCRIPTS"):
        try:
            platform_function =\
                import_platform_lib(config["ADDITIONAL_SCRIPTS"],
                                    "build_ex")
            functions = {"execute_script": execute_script}
            return platform_function(config, functions)
        except ImportError as error:
            print("error", config["ADDITIONAL_SCRIPTS"], str(error))
            build_failed(config)
    return None
 
 
def post_build_ex(config):
    """ An extension of the post build process as defined platform
        specific build setup script
 
        :param config: The environment variables used in the post build
            process
        :type config: Dictionary
        :returns: config dictionary
        :rtype: Dictionary
    """
    if config.get("ADDITIONAL_SCRIPTS"):
        try:
            platform_function =\
                import_platform_lib(config["ADDITIONAL_SCRIPTS"],
                                    "post_build_ex")
            functions = {"execute_script": execute_script}
            return platform_function(config, functions)
        except ImportError as error:
            print(config["ADDITIONAL_SCRIPTS"], str(error))
            build_failed(config)
    return None
 
 
def clean_ex(config):
    """ An extension of the platform cleaning
 
        :param config: The environment variables used in the clean process
        :type config: Dictionary
        :returns: config dictionary
        :rtype: Dictionary
    """
    if config.get("ADDITIONAL_SCRIPTS"):
        try:
            platform_function =\
                import_platform_lib(config["ADDITIONAL_SCRIPTS"],
                                    "clean_ex")
            functions = {"execute_script": execute_script}
            return platform_function(config, functions)
        except ImportError as error:
            print(config["ADDITIONAL_SCRIPTS"], str(error))
            build_failed(config)
    return None
 
 
def get_environment_variables(std_out_str, marker):
    """Gets the environment variables from a process
 
        :param std_out_str: The std_out pipe
        :type std_out_str: String
        :param marker: A begining and end mark of environment
            variables printed to std_out
        :type marker: String
        :returns: The environment variables read from the process' std_out pipe
        :rtype: Tuple
    """
    start_env_update = False
    environment_vars = {}
    out_put = ""
    for line in std_out_str.split("\n"):
        if start_env_update and len(line.split("=")) == 2:
            key, value = line.split("=")
            environment_vars[key] = value
        else:
            out_put += "\n" + line.replace(marker, "")
 
        if marker in line:
            if start_env_update:
                start_env_update = False
            else:
                start_env_update = True
    return (out_put, environment_vars)
 
 
def execute_script(command, env_variables, collect_env=False,
                   enable_std_pipe=False, shell=True):
    """launches a process that executes a script/shell command passed to it
 
        :param command: The command/script with its commandline
            arguments to be executed
        :type command:  List:String
        :param env_variables: Environment variables passed to the process
        :type env_variables: String
        :param collect_env: Enables the collection of environment variables
            when process execution is done
        :type collect_env: Boolean
        :param enable_std_pipe: Enables process out to be piped to
        :type enable_std_pipe: String
        :returns: a tuple of std_out, stderr , environment variables,
            return code
        :rtype: Tuple: (std_out, stderr , enVar, return_code)
    """
 
    print("Calling " + " ".join(command))
 
    env_marker = '-----env-----'
    env = {}
    kwarg = {"env": env_variables,
             "universal_newlines": True,
             "shell": shell,
             "cwd": env_variables["WORKSPACE"]}
 
    if enable_std_pipe or collect_env:
        kwarg["stdout"] = subprocess.PIPE
        kwarg["stderr"] = subprocess.PIPE
 
    # collect environment variables
    if collect_env:
        # get the binary that prints environment variables based on os
        if os.name == 'nt':
            get_var_command = "set"
        else:
            get_var_command = "env"
        # modify the command to print the environment variables
        if isinstance(command, list):
            command += ["&&", "echo", env_marker, "&&",
                        get_var_command, "&&", "echo", env_marker]
        else:
            command += " " + " ".join(["&&", "echo", env_marker,
                                       "&&", get_var_command,
                                       "&&", "echo", env_marker])
 
    # execute the command
    execute = subprocess.Popen(command, **kwarg)
    std_out, stderr = execute.communicate()
    code = execute.returncode
 
    # wait for process to be done
    execute.wait()
 
    # if collect environment variables
    if collect_env:
        # get the new environment variables
        std_out, env = get_environment_variables(std_out, env_marker)
    return (std_out, stderr, env, code)
 
 
def patch_config(config):
    """ An extension of the platform cleaning
 
        :param config: The environment variables used in the build process
        :type config: Dictionary
        :returns: config dictionary
        :rtype: Dictionary
    """
    new_config = {}
    for key in config:
        new_config[str(key)] = str(config[key].replace("/", os.sep))
    return new_config
 
 
def py_27_fix(config):
    """  Prepares build for python 2.7 => build
        :param config: The environment variables used in the build process
        :type config: Dictionary
        :returns: config dictionary
        :rtype: Dictionary
    """
    if not sys.version_info > (3, 0):
        path_list = []
        # create __init__.py in directories in this path
        if config.get("ADDITIONAL_SCRIPTS"):
            # get the directory
            path_to_directory =\
                os.path.dirname(config.get("ADDITIONAL_SCRIPTS"))
            path = ""
            for directories in path_to_directory.split(os.sep):
                path += directories + os.sep
                init_file = path + os.sep + "__init__.py"
                if not os.path.isfile(init_file):
                    open(init_file, 'w').close()
                    path_list.append(init_file)
            config["DYNAMIC_BUILD_INIT_FILES"] = ",".join(path_list)
 
    return config
 
 
def clean(build_config, board=False):
    """Cleans the build workspace
 
        :param config: The environment variables used in the build process
        :type config: Dictionary
        :param board: This flag specifies specific board clean
        :type board: Bool
        :returns: nothing
    """
 
    # patch the config
    build_config = patch_config(build_config)
 
    # get current environment variables
    config = os.environ.copy()
 
    # update it with the build variables
    config.update(build_config)
 
    if config.get('WORKSPACE') is None or not config.get('WORKSPACE'):
        config["WORKSPACE"] =\
                os.path.abspath(os.path.join("..", "..", "..", ""))
 
    # build cleanall
    print("Cleaning directories...")
 
    if board:
        platform_pkg = config.get("PLATFORM_BOARD_PACKAGE", None)
        if platform_pkg is None or\
            not os.path.isdir(os.path.join(config['WORKSPACE'],
                                           "Build", platform_pkg)):
            print("Platform package not found")
            sys.exit(1)
        else:
            print("Removing " + os.path.join(config['WORKSPACE'],
                                             "Build", platform_pkg))
            shutil.rmtree(os.path.join(config['WORKSPACE'],
                                       "Build", platform_pkg))
 
    else:
        if os.path.isdir(os.path.join(config['WORKSPACE'], "Build")):
            print("Removing " + os.path.join(config['WORKSPACE'], "Build"))
            shutil.rmtree(os.path.join(config['WORKSPACE'], "Build"))
 
    if os.path.isdir(os.path.join(config['WORKSPACE'], "Conf")):
        print("Removing " + os.path.join(config['WORKSPACE'], "Conf"))
        shutil.rmtree(os.path.join(config['WORKSPACE'], "Conf"))
 
    print("Cleaning files...")
 
    if os.path.isfile(os.path.join(config['WORKSPACE'],
                                   config.get("BUILD_REPORT",
                                              "BuildReport.log"))):
        print("Removing ", os.path.join(config['WORKSPACE'],
                                        config.get("BUILD_REPORT",
                                                   "BuildReport.log")))
        os.remove(os.path.join(config['WORKSPACE'],
                               config.get("BUILD_REPORT", "BuildReport.log")))
 
    print("  All done...")
 
    sys.exit(0)
 
 
def update_target_file(config):
    """Updates Conf's target file that will be used in the build
 
        :param config: The environment variables used in the build process
        :type config: Dictionary
        :returns: True if update was successful and False if update fails
        :rtype: Boolean
    """
    contents = None
    result = False
    with open(os.path.join(config["CONF_PATH"], "target.txt"), 'r') as target:
        contents = target.readlines()
        options_list = ['ACTIVE_PLATFORM', 'TARGET',
                        'TARGET_ARCH', 'TOOL_CHAIN_TAG', 'BUILD_RULE_CONF']
        modified = []
 
        # remove these options from the config file
        for line in contents:
            if line.replace(" ", "")[0] != '#' and\
             any(opt in line for opt in options_list):
                continue
            modified.append(line)
 
        # replace with config options provided
        string = "{} = {}\n".format("ACTIVE_PLATFORM",
                                    os.path.join(
                                        config['WORKSPACE_PLATFORM'],
                                        config['PLATFORM_BOARD_PACKAGE'],
                                        config['BOARD'],
                                        config['PROJECT_DSC']))
        modified.append(string)
 
        string = "{} = {}\n".format("TARGET", config['TARGET'])
        modified.append(string)
 
        string = "TARGET_ARCH = IA32 X64\n"
        modified.append(string)
 
        string = "{} = {}\n".format("TOOL_CHAIN_TAG", config['TOOL_CHAIN_TAG'])
        modified.append(string)
 
        string = "{} = {}\n".format("BUILD_RULE_CONF",
                                    os.path.join("Conf", "build_rule.txt"))
        modified.append(string)
 
    if modified is not None:
        with open(os.path.join(config["WORKSPACE"],
                               "Conf", "target.txt"), 'w') as target:
            for line in modified:
                target.write(line)
            result = True
 
    return result
 
 
def get_config():
    """Reads the default projects config file
 
        :returns: The config defined in the the Build.cfg file
        :rtype: Dictionary
    """
    config_file = configparser.RawConfigParser()
    config_file.optionxform = str
    config_file.read('build.cfg')
    config_dictionary = {}
    for section in config_file.sections():
        dictionary = dict(config_file.items(section))
        config_dictionary[section] = dictionary
    return config_dictionary
 
 
def get_platform_config(platform_name, config_data):
    """ Reads the platform specific config file
 
        param platform_name: The name of the platform to be built
        :type platform_name: String
        param configData: The environment variables to be
            used in the build process
        :type configData: Dictionary
        :returns: The config defined in the the Build.cfg file
        :rtype: Dictionary
    """
    config = {}
 
    platform_data = config_data.get("PLATFORMS")
    path = platform_data.get(platform_name)
    config_file = configparser.RawConfigParser()
    config_file.optionxform = str
    config_file.read(path)
    for section in config_file.sections():
        config[section] = dict(config_file.items(section))
 
    return config
 
 
def get_cmd_config_arguments(arguments):
    """Get commandline config arguments
 
    param arguments: The environment variables to be used in the build process
    :type arguments: argparse
    :returns: The config dictionary built from the commandline arguments
    :rtype: Dictionary
    """
    result = {}
    if arguments.capsule is True:
        result["CAPSULE_BUILD"] = "1"
 
    if arguments.performance is True:
        result["PERFORMANCE_BUILD"] = "TRUE"
 
    if arguments.fsp is True:
        result["FSP_WRAPPER_BUILD"] = "TRUE"
 
    if arguments.fspapi is True:
        result["API_MODE_FSP_WRAPPER_BUILD"] = "TRUE"
 
    if not arguments.UseHashCache:
        result['BINARY_CACHE_CMD_LINE'] = ''
    elif arguments.BinCacheDest:
        result['HASH'] = '--hash'
        result['BINARY_CACHE_CMD_LINE'] = '--binary-destination=%s' % arguments.BinCacheDest
    elif arguments.BinCacheSource:
        result['HASH'] = '--hash'
        result['BINARY_CACHE_CMD_LINE'] = '--binary-source=%s' % arguments.BinCacheSource
    else:
        result['BINARY_CACHE_CMD_LINE'] = ''
 
    return result
 
 
def get_cmd_arguments(build_config):
    """ Get commandline inputs from user
 
        param config_data: The environment variables to be
            used in the build process
        :type config_data: Dictionary
        :returns: The commandline arguments input by the user
        :rtype: argparse object
    """
 
    class PrintPlatforms(argparse.Action):
        """ this is an argparse action that lists the available platforms
        """
        def __call__(self, parser, namespace, values, option_string=None):
            print("Platforms:")
            for key in build_config.get("PLATFORMS"):
                print("    " + key)
            setattr(namespace, self.dest, values)
            sys.exit(0)
 
    # get the build commands
    parser = argparse.ArgumentParser(description="Build Help")
    parser.add_argument('--platform', '-p', dest="platform",
                        help='the platform to build',
                        choices=build_config.get("PLATFORMS"),
                        required=('-l' not in sys.argv and
                                  '--cleanall' not in sys.argv))
 
    parser.add_argument('--toolchain', '-t', dest="toolchain",
                        help="using the Tool Chain Tagname to build \
                            the platform,overriding \
                            target.txt's TOOL_CHAIN_TAG definition")
 
    parser.add_argument("--DEBUG", '-d', help="debug flag",
                        action='store_const', dest="target",
                        const="DEBUG", default="DEBUG")
 
    parser.add_argument("--RELEASE", '-r', help="release flag",
                        action='store_const',
                        dest="target", const="RELEASE")
 
    parser.add_argument("--TEST_RELEASE", '-tr', help="test Release flag",
                        action='store_const',
                        dest="target", const="TEST_RELEASE")
 
    parser.add_argument("--RELEASE_PDB", '-rp', help="release flag",
                        action='store_const', dest="target",
                        const="RELEASE_PDB")
 
    parser.add_argument('--list', '-l', action=PrintPlatforms,
                        help='lists available platforms', nargs=0)
 
    parser.add_argument('--cleanall', dest='clean_all',
                        help='cleans all', action='store_true')
 
    parser.add_argument('--clean', dest='clean',
                        help='cleans specific platform', action='store_true')
 
    parser.add_argument("--capsule", help="capsule build enabled",
                        action='store_true', dest="capsule")
 
    parser.add_argument("--silent", help="silent build enabled",
                        action='store_true', dest="silent")
 
    parser.add_argument("--performance", help="performance build enabled",
                        action='store_true', dest="performance")
 
    parser.add_argument("--fsp", help="fsp wrapper build enabled",
                        action='store_true', dest="fsp")
 
    parser.add_argument("--fspapi", help="API mode fsp wrapper build enabled",
                        action='store_true', dest="fspapi")
 
    parser.add_argument("--hash", action="store_true", dest="UseHashCache", default=False,
                        help="Enable hash-based caching during build process.")
 
    parser.add_argument("--binary-destination", help="Generate a cache of binary \
                            files in the specified directory.",
                        action='store', dest="BinCacheDest")
 
    parser.add_argument("--binary-source", help="Consume a cache of binary files \
                                from the specified directory.",
                        action='store', dest="BinCacheSource")
 
    return parser.parse_args()
 
 
def keyboard_interruption(int_signal, int_frame):
    """ Catches a keyboard interruption handler
 
        param int_signal: The signal this handler is called with
        :type int_signal: Signal
        param int_frame: The signal this handler is called with
        :type int_frame: frame
        :rtype: nothing
    """
    print("Signal #: {} Frame: {}".format(int_signal, int_frame))
    print("Quiting...")
    sys.exit(0)
 
 
def main():
    """ The main function of this module
        :rtype: nothing
    """
    # to quit the build
    signal.signal(signal.SIGINT, keyboard_interruption)
 
    # get general build configurations
    build_config = get_config()
 
    # get commandline parameters
    arguments = get_cmd_arguments(build_config)
 
    if arguments.clean_all:
        clean(build_config.get("DEFAULT_CONFIG"))
 
    # get platform specific config
    platform_config = get_platform_config(arguments.platform, build_config)
 
    # update general build config with platform specific config
    config = build_config.get("DEFAULT_CONFIG")
    config.update(platform_config.get("CONFIG"))
 
    # if user selected clean
    if arguments.clean:
        clean(config, board=True)
 
    # Override config with cmd arguments
    cmd_config_args = get_cmd_config_arguments(arguments)
    config.update(cmd_config_args)
 
    # get pre_build configurations
    config = pre_build(config,
                       build_type=arguments.target,
                       toolchain=arguments.toolchain,
                       silent=arguments.silent)
 
    # build selected platform
    config = build(config)
 
    # post build
    post_build(config)
 
 
if __name__ == "__main__":
    try:
        EXIT_CODE = 0
        main()
    except Exception as error:
        EXIT_CODE = 1
        traceback.print_exc()
    sys.exit(EXIT_CODE)