From f90a14d5d723c5d2b87f2eaa19f441dec33bb9b2 Mon Sep 17 00:00:00 2001 From: Flu0r1ne Date: Wed, 4 Aug 2021 14:32:22 -0500 Subject: Prototyped build pipeline --- adapters/gtest_adapter.go | 149 +++++++++++++++++++++++++++++++++++++--------- adapters/template_defs.go | 50 ++++++++++++++++ 2 files changed, 170 insertions(+), 29 deletions(-) create mode 100644 adapters/template_defs.go (limited to 'adapters') diff --git a/adapters/gtest_adapter.go b/adapters/gtest_adapter.go index 4cbaa9c..fd06a48 100644 --- a/adapters/gtest_adapter.go +++ b/adapters/gtest_adapter.go @@ -1,41 +1,85 @@ package adapters import ( - "fmt" + "bytes" + "log" + "os" + "path" + "strings" + "text/template" + "github.com/BurntSushi/toml" "golang.flu0r1ne.net/planr" - "log" ) /* CONFIGURATION */ - type GtestDefaults struct { - Name *string - Suite *string - File *string + Name *string + Suite *string + Testfile *string + Test_root *string + Srcs *[]string + Srcs_root *string } func (child *GtestDefaults) Inherit(p interface{}) { parent := p.(*GtestDefaults) - if(child.Name == nil) { - child.Name = parent.Name - } + if(child.Name == nil) { child.Name = parent.Name } + if(child.Suite == nil) { child.Suite = parent.Suite } + if(child.Testfile == nil) { child.Testfile = parent.Testfile } + if(child.Test_root == nil) { child.Test_root = parent.Test_root } + if(child.Srcs == nil) { child.Srcs = parent.Srcs } + if(child.Srcs_root == nil) { child.Srcs_root = parent.Srcs_root } +} + + +type GtestConfig struct { + GtestDefaults +} + - if(child.Suite == nil) { - child.Suite = parent.Suite +func (cfg GtestConfig) joinTests(path_ string) string { + if cfg.Test_root == nil { + return planr.JoinConfigDir("tests", path_) } + + return planr.JoinConfigDir(*cfg.Test_root, path_) +} - if(child.File == nil) { - child.File = parent.File +func (cfg GtestConfig) joinSrcs(path_ string) string { + if cfg.Srcs_root == nil { + return planr.JoinConfigDir("../src", path_) } + + return planr.JoinConfigDir(*cfg.Srcs_root, path_) } +func (cfg GtestConfig) srcList() string { + var srcList string -type GtestConfig struct { - GtestDefaults + if cfg.Srcs != nil { + srcs := make([]string, len(*cfg.Srcs)) + for i, src := range *cfg.Srcs { + srcs[i] = cfg.joinSrcs(src) + } + + srcList = strings.Join(srcs, "\n ") + } + + return srcList +} + +func (g GtestConfig) EnsureSatisfied(path string) { + if g.Name == nil { + log.Fatalf("\"name\" is not defined for unit: %s\n", path) + } else if g.Suite == nil { + log.Fatalf("\"suite\" is not defined for unit: %s\n", path) + } else if g.Testfile == nil { + log.Fatalf("\"testfile\" is not defined for unit: %s\n", path) + } } func primitiveDecode(primitive toml.Primitive, config interface{}) { @@ -64,34 +108,81 @@ func ParseDefaultConfig(prim toml.Primitive) planr.InheritableConfig { BUILD PROCESS */ -type GtestAdapter struct {} +type GtestAdapter struct { + unitTmpl *template.Template + cbuf bytes.Buffer +} -func (a GtestAdapter) Config() planr.AdapterConfig { +func (a *GtestAdapter) Config() planr.AdapterConfig { return planr.AdapterConfig { Name: "gtest", ParseConfig: ParseConfig, ParseDefaultConfig: ParseDefaultConfig, - } + } } -func (a GtestAdapter) InitializeBuild() { - fmt.Println("Initializing"); +func (a *GtestAdapter) InitializeBuild() { + a.unitTmpl = UnitTemplate() + a.cbuf = bytes.Buffer{} + + WriteCMakeBoiler(&a.cbuf) } -func (a GtestAdapter) Build(tc planr.TestCase) { - fmt.Printf("Building %v\n", tc); +const GTEST_CMAKE = "CMakeLists.txt" + +func Chdir(dir string) { + if err := os.Chdir(dir); err != nil { + log.Fatal(err) + } } -func (a GtestAdapter) FinalizeBuild() { - fmt.Println("Finalizing"); +func (a *GtestAdapter) Build(tc planr.TestCase) { + cfg := tc.AdapterConfig("gtest").(*GtestConfig) + cfg.EnsureSatisfied(tc.Path) + + cname := tc.Cname + testfile := cfg.joinTests(*cfg.Testfile) + srcList := cfg.srcList() + + err := a.unitTmpl.Execute(&a.cbuf, struct {Cname, File, Srcs string} { + cname, testfile, srcList, + }) + + if err != nil { + log.Fatal(err) + } } -func (a GtestAdapter) Evaluate(tc planr.TestCase) planr.TestResult { - fmt.Printf("Evaluating %v\n", tc); +func (a *GtestAdapter) FinalizeBuild() { + dir := a.Config().ConfigDir() + cmakeFile := path.Join(dir, GTEST_CMAKE) + + file, err := os.OpenFile(cmakeFile, os.O_RDWR | os.O_CREATE, 0644) + defer func () { + err := file.Close() + + if err != nil { + log.Fatal(err) + } + }() + + if err != nil { + log.Fatalf("Could not open CMakeFile (%s)\n%v", cmakeFile, err) + } - return planr.TestResult {} + file.Write(a.cbuf.Bytes()) + + Chdir(dir) + + planr.RunCmd("cmake", "-S", ".", "-B", ".") } -func (a GtestAdapter) Cleanup() { - fmt.Printf("Cleaning\n") +func (a *GtestAdapter) Make() { + planr.RunCmd("make", "-k") } + +func (a *GtestAdapter) Evaluate(tc planr.TestCase) planr.TestResult { + return planr.TestResult {} +} + +func (a *GtestAdapter) Cleanup() { } diff --git a/adapters/template_defs.go b/adapters/template_defs.go new file mode 100644 index 0000000..54f2840 --- /dev/null +++ b/adapters/template_defs.go @@ -0,0 +1,50 @@ +package adapters + +import ( + "io" + "log" + "text/template" +) + + +func UnitTemplate() *template.Template { + tmpl, err := template.New("gtest_unit").Parse(` +add_executable( + {{.Cname}} + {{.File}} + {{.Srcs}} +) + +target_link_libraries( + {{.Cname}} + gtest_main +) + +gtest_discover_tests( + {{.Cname}} +) +`) + + if err != nil { + log.Fatalf("Cannot load Gtest Unit Template %v", err) + } + + return tmpl +} + +func WriteCMakeBoiler(w io.Writer) { + w.Write([]byte(` +cmake_minimum_required (VERSION 3.1.0) + +project(PlanRGtestAdapter) + +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip +) + +include(GoogleTest) +FetchContent_MakeAvailable(googletest) +`)) +} -- cgit v1.2.3