summaryrefslogtreecommitdiffstats
path: root/src/tools/utils/sbePrime.py
blob: 0c94ee76663ac07bab573735523c78565a44d5a0 (plain)
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
#!/usr/bin/python
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/tools/utils/sbePrime.py $
#
# OpenPOWER sbe Project
#
# Contributors Listed Below - COPYRIGHT 2016
#
#
# 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.
#
# IBM_PROLOG_END_TAG
'''
###########################################################
#    @file    sbePrime.py
#    @author: George Keishing <gkeishin@in.ibm.com>
#    @brief   Main Module to support developer compilation
#             and patching.
#
#    Created on March 03, 2016
#    ----------------------------------------------------
#    @version  Developer      Date       Description
#    ----------------------------------------------------
#      1.0     gkeishin     022/03/16     Initial create
###########################################################
'''

#-------------------------
#  Imports
#-------------------------
import getopt
import os, sys, glob
import shutil
import stat # for File permission op

# Libraries/utility funcs and user define const
import sbeCmvcConstants as errorcode
import sbeCmvcUtility as utilcode
import sbePatchUtility as utilpatch

#-------------------------
# Main Function
#-------------------------
def main():

    #------------------------------------------
    # Usage tool option
    #------------------------------------------
    def usage():
        print " \n"
        print "  :: Command line USAGE options for Copying SBE FW files for compilation :: \n"
        print "  sbeDistribute.py -s <Sandbox Name> -i <file1,file2...>"

        print " \n"
        print "   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
        print "   |       By default NO argument is needed as an input .                            |"
        print "   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
        print " \n  ***** Options Supported  *****"
        print " \t  -p,--patch     = [ Optional ] Patch Simics related files on Sandbox "
        print " \t  -s,--sb        = [ Optional ] Sandbox base name"
        print " \t                                By default it picks up the ppe Repo test_sb sandbox"
        print " \t                                but if you have created a sandbox of your own manually "
        print " \t                                and you want to compile, use this option then only"
        print " \t  -i,--files     = [ Optional ] Firmware Files coma ',' separated input file1,file2"
        print " \t                                Only the pre-define listed files bellow:"
        print " \t                                sbe_sp_intf.H,simics.tar,sbe_pibmem.bin,sbe_seeprom.bin"
        print " \t  -r,--rc_file   = [ Optional ] The RC file for the sandbox (with absolute path)"
        print " \t  -n,--no_build  = [ Optional ] Flag to determine if sbei component should be compiled"
        print " \t  -h,--help      = Help"
        print "  ------------------------------------------------------------------------------------"

    #------------------------------------------
    # Exit from this Main
    #------------------------------------------
    def exit_main(rc):
        if rc == errorcode.HELP_EXIT:
           print "  [ HELP DOCUMENTATION ]\n"
           sys.exit(0)

        if rc == errorcode.ERROR_BUILD_FAILED:
            print "  Compilation Failed .. Error "

        if rc == errorcode.SUCCESS_DEV_EXIT:
           print "  [ Files Copy done! ]\n"
           sys.exit(0)

        if rc:
           print "\n  [ ERROR - MAIN ] Exiting with error code = ", rc
           sys.exit(rc)
        else:
           print "\n  Fips Sandbox compilation and simics patching completed [ OK ] "
           sys.exit(0)

    #------------------------------------------
    # Local var place name holder's
    #------------------------------------------
    sim_patch    = "None"
    sandbox_name = "None"
    path_name    = "None" # PPE Repo
    file_name    = "None"
    rc_file      = "None"
    build        = "1"

    #----------------------------
    # Read command line args
    #----------------------------
    opts, args = getopt.getopt(sys.argv[1:],"p:s:i:h:r:n",['patch=', 'sb=', 'files=', 'help', 'rc_file=', 'no_build'])
    for opt, arg in opts:
       if opt in ('-h', '--help'):
           usage()
           exit_main(errorcode.HELP_EXIT)
       elif opt in ('-p', '--patch'):
           sim_patch = arg
       elif opt in ('-s', '--sb'):
           sandbox_name = arg
       elif opt in ('-i', '--files'):
           file_name = arg
       elif opt in ('-r', '--rc_file'):
           rc_file = arg
       elif opt in ('--no_build'):
           build = "0"
       else:
           usage()
           exit_main(errorcode.ERROR_EXIT)

    #------------------------------------------------------
    # Make sure that it has passed atleast one arg with it
    #------------------------------------------------------
    if len(sys.argv)<1:
        usage()
        exit_main(errorcode.ERROR_SYS_EXIT)

    #---------------------------------------------
    # Callling the Func defs in order
    #---------------------------------------------

    #------------------------------
    # 1) User input params/ Check ENV
    #------------------------------
    print "\n [ Checking SBE user ENV Pre-req ] "
    # Get it from ENV
    if path_name == "None":
        # Get the PPE path
        l_ppe_path = utilcode.utilppeSbENV("SBEROOT")
        if l_ppe_path == "None":
            print "\n  Couldn't find PPE repo info from ENV currently set... "
            print "  [ ERROR ] PPE Repo ENV Setting Path  : %s " % l_ppe_path
            exit_main(errorcode.ERROR_SETTING)
        else:
            print "  PPE Repo path Setting\t :  %s "% l_ppe_path
            path_name = l_ppe_path

    #-----------------------------------
    # 2) Get the Sanbox and repo paths
    #-----------------------------------
    # Get the base path of the fips sandbox
    if sandbox_name == "None":
        # Find the sanbox name and base from ENV
        # User must have done workon fips sandbox to work
        sandbox_path = utilcode.utilFind_ENV_string("SANDBOXBASE").rstrip('\n')
    else:
        sandbox_path = utilcode.utilFind_sb_base(sandbox_name).rstrip('\n')
    print "  Fips Sandbox path\t : ",sandbox_path

    #-----------------------------------
    # 3) Get the Sanbox root path
    #-----------------------------------
    if sandbox_name == "None":
        sandbox_root = utilcode.utilFind_ENV_string("SANDBOXROOT").rstrip('\n')
    else:
#        sandbox_root = utilcode.utilFind_ENV_string("SANDBOXRC").rstrip('\n')
        sandbox_root = utilcode.utilFind_sb_rc(sandbox_name).rstrip('\n')

    if sandbox_root == "None":
        print "  ** [ ERROR ] Something Fishy about the ENV set -OR- Option used.. Please check manually ** "
        usage()
        exit_main(errorcode.ERROR_SETTING)
    else:
        print "  Sandbox root path\t : ",sandbox_root
    
    #---------------------------------------------
    # sim setup if user initiates
    #---------------------------------------------
    if sim_patch != "None": 
        #---------------------------------------------
        # Create sandbox for simics
        #---------------------------------------------
        rc_sb = utilpatch.utilExecuteShell(path_name,"None","sandbox-create")
        if rc_sb == errorcode.SUCCESS_EXIT:
            print "  Sandbox Created.. [ OK ] \n"
        else:
            print "  Sandbox Create.. [ ERROR ]",rc_sb
            exit_main(rc_sb)

        #----------------------------------------
        # Patch up the simics patches files
        #----------------------------------------

        print "\n  *** Update Simics patches onto Sandbox *** \n "
        # Pre sim setup
        rc_shell = utilpatch.utilExecuteShell(path_name,sandbox_path,"workarounds.presimsetup")
        if rc_shell == errorcode.SUCCESS_EXIT:
            print "  presimsetup [ OK ]\n"
        else:
            print "  presimsetup [ ERROR ] : ",rc_shell
            exit_main(rc_shell)

        # Patch the simics files
        rc_sim = utilpatch.utilPatchSimics(sandbox_path,sandbox_root)
        if rc_sim != errorcode.SUCCESS_EXIT:
            exit_main(rc_sim)
        else:
            print "  Patch the simics files on Sandbox [ OK ] \n"

        # Post sim setup
        rc_shell = utilpatch.utilExecuteShell(path_name,sandbox_path,"workarounds.postsimsetup")
        if rc_shell == errorcode.SUCCESS_EXIT:
            print "  postsimsetup [ OK ]\n"
            # Clean exit Get out from here
            exit_main(errorcode.SUCCESS_EXIT)
        else:
            print "  postsimsetup [ ERROR ] : ",rc_shell
            exit_main(rc_shell)


    #----------------------------------------
    # 4) Copy the files from repo to sandbox
    #----------------------------------------
    # Find the files and copy to the sanbox dir 
    # Just take a quick check if the Sandbox exist or not
    if sandbox_path != "None":
        if os.path.isdir(sandbox_path) == True:
           rc_copy = utilcode.utilCopyFileToSandbox(path_name,sandbox_path,file_name)
           if rc_copy == errorcode.SUCCESS_DEV_EXIT:
               print "  Files Copied to Fips Sandbox : [ OK ]"
           else:
               exit_main(rc_copy)
        else:
            print "  Sandbox : %s [ Either doesn't exist or do workon to fips sb to load the ENV.. ]" % os.path.basename(sandbox_path)
            print "               - OR - "
            print "  [ Optional ] You can specify your sandbox name as input as well"
            print "               -s <fips_sandbox Name >"
            usage()
            exit_main(errorcode.ERROR_SANDBOX_EXIST)
    else:
        print "  Please Check your fips Sandbox and retry"
        exit_main(errorcode.ERROR_SANDBOX_EXIST)

    sb_name=os.path.basename(sandbox_path)
    print "\n  Sandbox :",sb_name

    if build == "1":
        #----------------------------------------
        # 5) Wite the hook file into shell file
        #----------------------------------------
        # Write the compile shell hook on the fips sandbox location
        hook_file = utilcode.utilWriteShell_hooks(sandbox_path)

        #----------------------------------------
        # 6) Compile the code 
        #----------------------------------------
        # Use the hook script to compile the code
        if sandbox_name == "None":
            compile_cmd="workon -m ppc  " + sb_name + " -c " + hook_file + " -rc " + sandbox_root +"/sbesandboxrc"
        else:
            if rc_file == "None":
                compile_cmd="workon -m ppc  " + sb_name + " -c " + hook_file + " -rc " + sandbox_root +"/.sandboxrc"
            else:
                print " getting rc file from user \n"
                compile_cmd="workon -m ppc  " + sb_name + " -c " + hook_file + " -rc " + rc_file
        print "\n  [ COMPILE ] Executing :%s \n"%compile_cmd
        rc = os.system(compile_cmd)

        print "  Compilation returned rc :",rc
        if rc != 0:
            exit_main(errorcode.ERROR_BUILD_FAILED)

    # Clean exit
    exit_main(errorcode.SUCCESS_EXIT)


if __name__=="__main__":
    main()

OpenPOWER on IntegriCloud