From f75508a3a27ff6e4ec5cbfe6827d61fb9998bedf Mon Sep 17 00:00:00 2001 From: "Ian A. Mason" Date: Wed, 5 Jul 2017 11:00:07 -0700 Subject: [PATCH] Now we use go's flags to parse the 'get-bc' command line. Not really impressed with flags. No long forms. --- cmd/gclang++/main.go | 4 +- cmd/gclang/main.go | 3 +- cmd/get-bc/main.go | 2 +- shared/extractor.go | 183 ++++++++++++++++++++++++------------------- 4 files changed, 104 insertions(+), 88 deletions(-) diff --git a/cmd/gclang++/main.go b/cmd/gclang++/main.go index ff8e162..b90436c 100644 --- a/cmd/gclang++/main.go +++ b/cmd/gclang++/main.go @@ -1,8 +1,8 @@ package main import ( - "os" "github.com/SRI-CSL/gllvm/shared" + "os" ) func main() { @@ -10,9 +10,7 @@ func main() { var args = os.Args args = args[1:] - exitCode := shared.Compile(args, "clang++") - shared.LogInfo("Calling %v returned %v\n", os.Args, exitCode) diff --git a/cmd/gclang/main.go b/cmd/gclang/main.go index 318e991..014e7d6 100644 --- a/cmd/gclang/main.go +++ b/cmd/gclang/main.go @@ -1,8 +1,8 @@ package main import ( - "os" "github.com/SRI-CSL/gllvm/shared" + "os" ) func main() { @@ -10,7 +10,6 @@ func main() { var args = os.Args args = args[1:] - exitCode := shared.Compile(args, "clang") shared.LogInfo("Calling %v returned %v\n", os.Args, exitCode) diff --git a/cmd/get-bc/main.go b/cmd/get-bc/main.go index 0147cf4..7809a2b 100644 --- a/cmd/get-bc/main.go +++ b/cmd/get-bc/main.go @@ -1,8 +1,8 @@ package main import ( - "os" "github.com/SRI-CSL/gllvm/shared" + "os" ) func main() { diff --git a/shared/extractor.go b/shared/extractor.go index 74aef46..451b619 100644 --- a/shared/extractor.go +++ b/shared/extractor.go @@ -3,6 +3,7 @@ package shared import ( "debug/elf" "debug/macho" + "flag" "io/ioutil" "os" "path" @@ -12,45 +13,21 @@ import ( ) type extractionArgs struct { - InputFile string - InputType int - OutputFile string - LinkerName string - ArchiverName string - ArArgs []string - ObjectTypeInArchive int // Type of file that can be put into an archive - Extractor func(string) []string - IsVerbose bool - IsWriteManifest bool - IsBuildBitcodeArchive bool + InputFile string + InputType int + OutputFile string + LinkerName string + ArchiverName string + ArArgs []string + ObjectTypeInArchive int // Type of file that can be put into an archive + Extractor func(string) []string + Verbose bool + WriteManifest bool + BuildBitcodeArchive bool } func Extract(args []string) { - ea := parseExtractionArgs(args) - - switch ea.InputType { - case fileTypeELFEXECUTABLE, - fileTypeELFSHARED, - fileTypeELFOBJECT, - fileTypeMACHEXECUTABLE, - fileTypeMACHSHARED, - fileTypeMACHOBJECT: - handleExecutable(ea) - case fileTypeARCHIVE: - handleArchive(ea) - default: - LogFatal("Incorrect input file type %v.", ea.InputType) - } - -} - -func parseExtractionArgs(args []string) extractionArgs { - origArgs := args - // Initializing args to defaults - ea := extractionArgs{ - LinkerName: "llvm-link", - ArchiverName: "llvm-ar", - } + ea := parseSwitches() // Checking environment variables if LLVMLINKName != "" { @@ -70,49 +47,11 @@ func parseExtractionArgs(args []string) extractionArgs { } } - // Parsing cli input. FIXME: "get-bc -mb libfoo.a" should work just like "get-bc -m -b libfoo.a" - for len(args) > 0 { - switch arg := args[0]; arg { - case "-b": - ea.IsBuildBitcodeArchive = true - args = args[1:] - case "-v": - ea.IsVerbose = true - args = args[1:] - case "-m": - ea.IsWriteManifest = true - args = args[1:] - case "-o": - if len(args) < 2 { - LogFatal("There was an error parsing the arguments: %v.", origArgs) - } - ea.OutputFile = args[1] - args = args[2:] - default: - ea.InputFile = arg - args = args[1:] - } - } - - // Sanity-check the parsed arguments - if len(ea.InputFile) == 0 { - LogFatal("No input file was given.") - } - if _, err := os.Stat(ea.InputFile); os.IsNotExist(err) { - LogFatal("The input file %s does not exist.", ea.InputFile) - } - realPath, err := filepath.EvalSymlinks(ea.InputFile) - if err != nil { - LogFatal("There was an error getting the real path of %s.", ea.InputFile) - } - ea.InputFile = realPath - ea.InputType = getFileType(realPath) - // Set arguments according to runtime OS switch platform := runtime.GOOS; platform { case "freebsd", "linux": ea.Extractor = extractSectionUnix - if ea.IsVerbose { + if ea.Verbose { ea.ArArgs = append(ea.ArArgs, "xv") } else { ea.ArArgs = append(ea.ArArgs, "x") @@ -121,7 +60,7 @@ func parseExtractionArgs(args []string) extractionArgs { case "darwin": ea.Extractor = extractSectionDarwin ea.ArArgs = append(ea.ArArgs, "-x") - if ea.IsVerbose { + if ea.Verbose { ea.ArArgs = append(ea.ArArgs, "-v") } ea.ObjectTypeInArchive = fileTypeMACHOBJECT @@ -133,7 +72,7 @@ func parseExtractionArgs(args []string) extractionArgs { if ea.OutputFile == "" { if ea.InputType == fileTypeARCHIVE { var ext string - if ea.IsBuildBitcodeArchive { + if ea.BuildBitcodeArchive { ext = ".a.bc" } else { ext = ".bca" @@ -144,7 +83,86 @@ func parseExtractionArgs(args []string) extractionArgs { } } - return ea + switch ea.InputType { + case fileTypeELFEXECUTABLE, + fileTypeELFSHARED, + fileTypeELFOBJECT, + fileTypeMACHEXECUTABLE, + fileTypeMACHSHARED, + fileTypeMACHOBJECT: + handleExecutable(ea) + case fileTypeARCHIVE: + handleArchive(ea) + default: + LogFatal("Incorrect input file type %v.", ea.InputType) + } + +} + +func parseSwitches() (ea extractionArgs) { + ea = extractionArgs{ + LinkerName: "llvm-link", + ArchiverName: "llvm-ar", + } + + verbosePtr := flag.Bool("v", false, "verbose mode") + + writeManifestPtr := flag.Bool("m", false, "write the manifest") + + buildBitcodeArchive := flag.Bool("b", false, "build a bitcode module(FIXME? should this be archive)") + + outputFilePtr := flag.String("o", "", "the output file") + + archiverNamePtr := flag.String("a", "", "the llvm archiver") + + linkerNamePtr := flag.String("l", "", "the llvm linker") + + flag.Parse() + + ea.Verbose = *verbosePtr + ea.WriteManifest = *writeManifestPtr + ea.BuildBitcodeArchive = *buildBitcodeArchive + + if *archiverNamePtr != "" { + ea.ArchiverName = *archiverNamePtr + } + + if *linkerNamePtr != "" { + ea.LinkerName = *linkerNamePtr + } + + ea.OutputFile = *outputFilePtr + + inputFiles := flag.Args() + + LogInfo("ea.Verbose: %v\n", ea.Verbose) + LogInfo("ea.WriteManifest: %v\n", ea.WriteManifest) + LogInfo("ea.BuildBitcodeArchive: %v\n", ea.BuildBitcodeArchive) + LogInfo("ea.ArchiverName: %v\n", ea.ArchiverName) + LogInfo("ea.LinkerName: %v\n", ea.LinkerName) + LogInfo("ea.OutputFile: %v\n", ea.OutputFile) + + if len(inputFiles) != 1 { + LogFatal("Can currently only deal with exactly one input file, sorry. You gave me %v\n.", len(inputFiles)) + } + + ea.InputFile = inputFiles[0] + + LogInfo("ea.InputFile: %v\n", ea.InputFile) + + if _, err := os.Stat(ea.InputFile); os.IsNotExist(err) { + LogFatal("The input file %s does not exist.", ea.InputFile) + } + realPath, err := filepath.EvalSymlinks(ea.InputFile) + if err != nil { + LogFatal("There was an error getting the real path of %s.", ea.InputFile) + } + ea.InputFile = realPath + ea.InputType = getFileType(realPath) + + LogInfo("ea.InputFile real path: %v\n", ea.InputFile) + + return } func handleExecutable(ea extractionArgs) { @@ -156,7 +174,7 @@ func handleExecutable(ea extractionArgs) { extractTimeLinkFiles(ea, filesToLink) // Write manifest - if ea.IsWriteManifest { + if ea.WriteManifest { writeManifest(ea, filesToLink, artifactPaths) } } @@ -202,14 +220,14 @@ func handleArchive(ea extractionArgs) { filepath.Walk(tmpDirName, walkHandlingFunc) // Build archive - if ea.IsBuildBitcodeArchive { + if ea.BuildBitcodeArchive { extractTimeLinkFiles(ea, bcFiles) } else { archiveBcFiles(ea, bcFiles) } // Write manifest - if ea.IsWriteManifest { + if ea.WriteManifest { writeManifest(ea, bcFiles, artifactFiles) } } @@ -231,6 +249,7 @@ func archiveBcFiles(ea extractionArgs, bcFiles []string) { args = append(args, "rs", absOutputFile) args = append(args, bcFilesInDir...) success, err := execCmd(ea.ArchiverName, args, dir) + LogInfo("ea.ArchiverName = %s, args = %v, dir = %s\n", ea.ArchiverName, args, dir) if !success { LogFatal("There was an error creating the bitcode archive: %v.\n", err) } @@ -240,7 +259,7 @@ func archiveBcFiles(ea extractionArgs, bcFiles []string) { func extractTimeLinkFiles(ea extractionArgs, filesToLink []string) { var linkArgs []string - if ea.IsVerbose { + if ea.Verbose { linkArgs = append(linkArgs, "-v") } linkArgs = append(linkArgs, "-o", ea.OutputFile)