|
| 1 | +// Package autobuilder implements a simple system that attempts to run build commands for common |
| 2 | +// build frameworks, if the relevant files exist. |
| 3 | +package autobuilder |
| 4 | + |
| 5 | +import ( |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + |
| 10 | + "github.com/github/codeql-go/extractor/util" |
| 11 | +) |
| 12 | + |
| 13 | +// CheckExtracted sets whether the autobuilder should check whether source files have been extracted |
| 14 | +// to the CodeQL source directory as well as whether the build command executed successfully. |
| 15 | +var CheckExtracted = false |
| 16 | + |
| 17 | +// checkEmpty checks whether a directory either doesn't exist or is empty. |
| 18 | +func checkEmpty(dir string) (bool, error) { |
| 19 | + if !util.DirExists(dir) { |
| 20 | + return true, nil |
| 21 | + } |
| 22 | + |
| 23 | + d, err := os.Open(dir) |
| 24 | + if err != nil { |
| 25 | + return false, err |
| 26 | + } |
| 27 | + defer d.Close() |
| 28 | + |
| 29 | + names, err := d.Readdirnames(-1) |
| 30 | + if err != nil { |
| 31 | + return false, err |
| 32 | + } |
| 33 | + return len(names) == 0, nil |
| 34 | +} |
| 35 | + |
| 36 | +// checkExtractorRun checks whether the CodeQL Go extractor has run, by checking if the source |
| 37 | +// archive directory is empty or not. |
| 38 | +func checkExtractorRun() bool { |
| 39 | + srcDir := os.Getenv("CODEQL_EXTRACTOR_GO_SOURCE_ARCHIVE_DIR") |
| 40 | + if srcDir != "" { |
| 41 | + empty, err := checkEmpty(srcDir) |
| 42 | + if err != nil { |
| 43 | + log.Fatalf("Unable to read source archive directory %s.", srcDir) |
| 44 | + } |
| 45 | + if empty { |
| 46 | + log.Printf("No Go code seen; continuing to try other builds.") |
| 47 | + return false |
| 48 | + } |
| 49 | + return true |
| 50 | + } else { |
| 51 | + log.Fatalf("No source directory set.") |
| 52 | + return false |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// tryBuildIfExists tries to run the command `cmd args...` if the file `buildFile` exists and is not |
| 57 | +// a directory. Returns true if the command was successful and false if not. |
| 58 | +func tryBuildIfExists(buildFile, cmd string, args ...string) bool { |
| 59 | + if util.FileExists(buildFile) { |
| 60 | + log.Printf("%s found.\n", buildFile) |
| 61 | + return tryBuild(cmd, args...) |
| 62 | + } |
| 63 | + return false |
| 64 | +} |
| 65 | + |
| 66 | +// tryBuild tries to run `cmd args...`, returning true if successful and false if not. |
| 67 | +func tryBuild(cmd string, args ...string) bool { |
| 68 | + log.Printf("Trying build command %s %v", cmd, args) |
| 69 | + res := util.RunCmd(exec.Command(cmd, args...)) |
| 70 | + return res && (!CheckExtracted || checkExtractorRun()) |
| 71 | +} |
| 72 | + |
| 73 | +// Autobuild attempts to detect build system and run the corresponding command. |
| 74 | +func Autobuild() bool { |
| 75 | + return tryBuildIfExists("Makefile", "make") || |
| 76 | + tryBuildIfExists("makefile", "make") || |
| 77 | + tryBuildIfExists("GNUmakefile", "make") || |
| 78 | + tryBuildIfExists("build.ninja", "ninja") || |
| 79 | + tryBuildIfExists("build", "./build") || |
| 80 | + tryBuildIfExists("build.sh", "./build.sh") |
| 81 | +} |
0 commit comments