summaryrefslogtreecommitdiff
path: root/adapters/gtest/templating.go
blob: 2380db955fa797fd9058264fc3445f2a6d5556e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
}