Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit de0582a

Browse files
author
Sauyon Lee
committed
autobuilder: extract out attempted build commands
1 parent cd63ea8 commit de0582a

File tree

2 files changed

+84
-7
lines changed

2 files changed

+84
-7
lines changed

extractor/autobuilder/autobuilder.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}

extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"runtime"
1414
"strings"
1515

16+
"github.com/github/codeql-go/extractor/autobuilder"
1617
"github.com/github/codeql-go/extractor/util"
1718
)
1819

@@ -403,13 +404,8 @@ func main() {
403404
inst := util.Getenv("CODEQL_EXTRACTOR_GO_BUILD_COMMAND", "LGTM_INDEX_BUILD_COMMAND")
404405
shouldInstallDependencies := false
405406
if inst == "" {
406-
// if there is a build file, run the corresponding build tool
407-
buildSucceeded := tryBuild("Makefile", "make") ||
408-
tryBuild("makefile", "make") ||
409-
tryBuild("GNUmakefile", "make") ||
410-
tryBuild("build.ninja", "ninja") ||
411-
tryBuild("build", "./build") ||
412-
tryBuild("build.sh", "./build.sh")
407+
// try to build the project
408+
buildSucceeded := autobuilder.Autobuild()
413409

414410
if !buildSucceeded {
415411
// Build failed; we'll try to install dependencies ourselves

0 commit comments

Comments
 (0)