package gtest import ( "io" "log" "os" "golang.flu0r1ne.net/planr" "path" "text/template" ) type cmakeUnit struct { ExeNm string File string Srcdir string IncludeSrc bool Srcs string CompilerOptions string }; func cmakeUnits(e []executable, dirs planr.DirConfig) []cmakeUnit { units := make([]cmakeUnit, len(e)) for i, exe := range e { testpath := path.Join(dirs.Tests(), exe.testpath) srclist := srcList(dirs.Src(), exe.srcs) units[i] = cmakeUnit { ExeNm: exe.exeNm, File: testpath, IncludeSrc: exe.includeSrc, Srcdir: dirs.Src(), Srcs: srclist, CompilerOptions: compilerOptionsCmake(exe), } } return units } func generateCmakeScript(out string, units []cmakeUnit) { file, err := os.OpenFile(out, 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", out, err) } writeCmakeBoilerplate(file) tmpl := unitTemplate() for _, unit := range units { if err := tmpl.Execute(file, unit); err != nil { log.Fatalf("Failed to generate unit %s: %v", unit.ExeNm, err); } } } func compilerOptionsCmake(e executable) string { if e.compilerOptions == "" { return "" } return "target_compile_options(\"" + e.exeNm + "\" PRIVATE " + e.compilerOptions + ")"; } // TODO: Add comments func unitTemplate() *template.Template { tmpl, err := template.New("gtest_unit").Parse(` ################################################ ## {{.ExeNm}} add_executable( "{{.ExeNm}}" "{{.File}}" {{.Srcs}} ) {{.CompilerOptions}} {{ if .IncludeSrc }} target_include_directories("{{.ExeNm}}" PRIVATE "{{.Srcdir}}") {{ end }} target_link_libraries( "{{.ExeNm}}" gtest_main ) gtest_discover_tests( "{{.ExeNm}}" ) `) if err != nil { log.Fatalf("Cannot load Gtest unit template %v", err) } return tmpl } const GOOGLE_TEST_URL = "https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip" func writeCmakeBoilerplate(w io.Writer) { tmpl := boilderTemplate() tmpl.Execute(w, struct { Url string Version string }{ Url: GOOGLE_TEST_URL, Version: planr.VERSION, }) } // TODO: Make CXX Version Editable func boilderTemplate() *template.Template { tmpl, err := template.New("gtest_boilerplate").Parse(` # AUTOMATICALLY GENERATED BY PLANR VERSION {{.Version}} cmake_minimum_required (VERSION 3.1.0) project(PlanRGtestAdapter) include(FetchContent) FetchContent_Declare( googletest URL {{.Url}} ) set(CMAKE_CXX_STANDARD 17) include(GoogleTest) FetchContent_MakeAvailable(googletest) `) if err != nil { log.Fatalf("Cannot load Gtest Cmake boilerplate") } return tmpl }