3 !a_e9@sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ye Wnek reZ YnXejjdZeddkreeddkrejZnejZddZdd Zd d Zd;d dZddZddddddddde jddddddZeedZded<eed<d d!Z d"d#gd$eee e j!d%j"d&d'j#ed(d)d*d+gd,d-d.d/d0gd1d-d.gZ$d2d3Z%d4d5Z&d6d7Z'd8d9Z(e)d:kre(dS)<N.3cCs tjdS)Na This script generates Makefiles that can be used either on Windows (with NMake) or Linux (with GNU Make). The Makefiles consist only of variable definitions; they are intended to be used with a common Makefile that defines the actual rules. The Makefiles are generated from JSON specifications. These are simple key-value dicts, but can contain variables and computation, as well as comments (lines whose first character is "#"). If the JSON file contains the following information: { # 'T was brillig, and the slithy toves "FOO": "BAR", "BAZ": "{FOO}", # Did gyre and gimble in the wabe; "QUUX": 30 "XYZZY": "__eval 5 if {QUUZ} < 5 else min({QUUX}, 60)" } then the resulting Makefile will look like H_FOO = BAR H_BAZ = BAR H_QUUX = 30 H_XYZZY = 30 The language used for evaluation is highly restricted; arbitrary python is not allowed. JSON values that are lists will be joined with whitespace: { "FOO": ["BAR", "BAZ", "QUX"] } -> H_FOO = BAR BAZ QUX As a special case, if a key is equal to "DEF", "INC" (and maybe more, read the code) the list of values is treated as a list of defines or include paths. Thus, they have -D or /D, or -I or /I, prepended to them as appropriate for the platform. { "DEF": ["DEBUG", "FOO=BAR"] } on Linux -> H_DEF = -DDEBUG -DFOO=BAR Pathnames are written with a forward slash in the JSON file. In each value, all slashes are replaced with backslashes if generating Makefiles for Windows. If you wish to generate a slash even on Windows, use two slashes---that will be changed into a single forward slash on both Windows and Linux. { "INC": [ "my/cool/directory" ], "DEF": [ "HALF=//2" ] } On Windows -> H_INC = /Imy\cool\directory H_DEF = /DHALF=/2 When invoked, this script walks the directory tree looking for files called "Makefile.json". It reads that file and dumps a Makefile in that same directory. We assume that this script lives in the same directory as a Makefile called "Makefile.common", which contains the actual Make rules. The final line of each of the generated Makefiles will be an include statement, including Makefile.common. )textwrapdedentrrN/home/aniruddha/Desktop/UnitTestSetup/test/cbmc/proofs/make_proof_makefiles.pyprolog<sHr cCst|}|jj}WdQRXdjdd|D}ytj|tjd}Wn6tjj k r~t j t j d|tjdYnX|S)N cSs$g|]}|r|jjd r|qS)#)lstrip startswith).0linerrr sz)load_json_config_file..)object_pairs_hookzparsing file %sr)openread splitlinesjoinjsonloads collections OrderedDictdecoderJSONDecodeError traceback print_excloggingerrorsysexit)filehandlelinesZ no_commentsdatarrr load_json_config_files  r'c Csttjj|d}tj}d|kr8tjd|tddjdd|dD|d<tj}xl|j D]`\}}t |t rg}x$|D]}|j t |||||d qWdj|||<qft |||||||<qfWd |jkst|d jd krd |d <nt|d jd krd|d <dd|j D}dt|d} | t|jtjjdd} ttjj|dd*} | jdjdj|t|d| dWdQRXdS)Nz Makefile.jsonZOBJSz3Expected a list of object files in %s/Makefile.jsonr css|]}|jds|VqdS)z _harness.gotoN)endswith)rorrr sz dump_makefile..ZOBJS_EXCEPT_HARNESSTEXPECTEDtrueZ SUCCESSFULfalseFAILUREcSsg|]\}}d||fqS)z H_%s = %sr)rkvrrr rsz!dump_makefile..z..%szpath-sepMakefilewz6{contents} {include} {common_dir_path}Makefile.commonr z makefile-inc)contentsincludecommon_dir_path)r'ospathrrrrr r"items isinstancelistappendcomputekeysstrlower_platform_choiceslensplitseprwriteformat) Zdyrsystemr&makefileso_farnamevalueZ new_valueitemr6r$rrr dump_makefiles<      rMFc Cst|tttfsDtjtdtjj |d|t |t t |t dt |}y|j f|}WnNtk r}z2tjtdtjj |d||t |t dWYdd}~XnX|dtddkrtjdd|}tjdt|d |}tjdd|} n8|tddj} tjd | t| tjj |d||} |d krLd t|d | f} n$|dkrld t|d| f} n| } |ryd||| f||<Wntk r| ||<YnXtjd| |n| ||<tjd|| | S)Nzx in file %s, the key '%s' has value '%s', which is of the wrong type (%s)z Makefile.jsonrz in file %s, the key '%s' has value '%s', which contains the variable %s, but that variable has not previously been set in the fileZ__evalz//Z__DOUBLE_SLASH__/z path-sep-rezAbout to evaluate '%s'DEFz%s%sdefineINCr5z%s %sz&Appending final value '%s' to key '%s'z Key '%s' set to final value '%s')r: basestringfloatintrr wrapr7r8rr?typer"rFKeyErrorrBresubrAstripdebug eval_exprinfo) rKrIrGkeyharnessZ appendingZ var_subbedeZtmpZ evaluatedZto_evalZ final_valuerrr r=sR          r=c shytj|ddj}Wn:tk rNtjtjtd||t dYnXfdd|S)z: Safe evaluation of purely arithmetic expressions eval)modezw in file %s at key '%s', value '%s' contained the expression '%s' which is an invalid expressionrcstj|t|tr|jSt|tjrr|j}|dk rZ|dk rZtjt dt d|rh|j S|j St|tj r|j}|jd}|jd}|||St|tjr|j}|j}|j}|||St|tjrVttd}|jj|kr(tjt d|jjt d|jd}|jd}||jj||Sy\tjtjtjtjtjtj tj!tj"tj#tj$tj%tj&tj'tj(tj)tj*tj+tj,tj-tj.i t/|St0k rtjt dt dYnXdS) NTFzo in file %s at key '%s', there was an invalid guard for an if statement.rr)maxminza in file %s at key '%s', there was an invalid call to %s()zl in file %s at key '%s', there was expression that was impossible to evaluate)1rr[r:ast_numnastZIfExpZtestr rUr"bodyZorelseZCompareleftZ comparatorsZopsZBinOprightopZCallrcrdfuncidargsZEqoperatoreqZNotEqneZLtltZLtEleZGtgtZGtEgeZAddaddZSubrYZMultmulZDivfloordivrVrW)ZnodeZguardrirjrkZ valid_calls)eval_single_noder_r^rr ry s`                  z#eval_expr..eval_single_node) rgparserh SyntaxErrorrrrr rUr")Z expr_stringr_r^rKZtreer)ryr_r^r r\s?r\linuxrNz-Dz-Ir5)platformzpath-sepz path-sep-rerPr5z makefile-incwin32\z/Dz/Iz!INCLUDE)r|windowsdarwinr}macoscCs,x&tjD]\}}tj|dkr |Sq WdS)Nr}r|)rAr9r!r}) arg_stringZos_datarrr default_platformcsrz-sz--systemOSz which operating system to generate makefiles for. Defaults to the current platform (%(default)s); choices are {choices}z[%s]z, )choices)flagsmetavarrdefaulthelpz-vz --verbosezverbose output store_true)rractionz-wz--very-verbosezvery verbose outputcCs>tjttjd}x"tD]}|jd}|j||qW|jS)N) descriptionformatter_classr)argparseArgumentParserr RawDescriptionHelpFormatter_argspop add_argument parse_args)Zparsargrrrr get_argss   rcCsXdjtjjtd}|jr,tj|tjdn(|j rDtj|tj dntj|tj ddS)Nz#{script}: %(levelname)s %(message)s)script)rFlevel) rFr7r8basename__file__Z very_verboser basicConfigDEBUGverboseINFOWARNING)rnfmtrrr set_up_loggingsrcCstjddtjdd|S)Nz\s+r(r )rXrY)stringrrr rUsrUcCs@t}t|x,tjdD]\}}}d|krt||jqWdS)Nrz Makefile.json)rrr7walkrMrG)rnroot_fylesrrr mains r__main__)F)*rrgrrrror7Zos.pathr}rXr!rrrR NameErrorr?Zpython_versionrCZ_platrTZConstantreZNumr r'rMr=r\escaperAdictZ_mac_osrrrFrrrrrUr__name__rrrr sx K- 8R