Skip to content

Commit 72cc187

Browse files
committed
fix(cmake): global directory location, home folder on windows and add find_library
1 parent 9116e0c commit 72cc187

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

cmakegen/exec.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmakegen
33
import (
44
"bytes"
55
"fmt"
6+
"strings"
67
"text/template"
78
)
89

@@ -14,32 +15,37 @@ set(CMAKE_CXX_STANDARD {{.LanguageStandard}})
1415
add_executable({{.ProjectName}})
1516
1617
target_sources({{.ProjectName}} PRIVATE {{.Sources}} {{.Includes}})
17-
{{$home:=.Home}}
18+
{{$c3pmGlobalDir:=.C3pmGlobalDir}}
1819
1920
target_include_directories(
2021
{{.ProjectName}} PRIVATE {{.IncludeDirs}}
2122
{{ range .Dependencies }}
2223
{{$name:=.Name}}
2324
{{$version:=.Version}}
2425
{{ range .ExportedIncludeDirs }}
25-
{{$home}}/.c3pm/cache/{{$name}}/{{$version}}/{{.}}
26+
{{$c3pmGlobalDir}}/cache/{{$name}}/{{$version}}/{{.}}
2627
{{ end }}
2728
{{end}}
2829
)
30+
{{range .Dependencies}}
31+
find_library({{ .Name | ToUpper}} {{.Name}} "{{$c3pmGlobalDir}}/cache/{{.Name}}/{{.Version}}/lib")
32+
{{end}}
2933
3034
target_link_libraries(
3135
{{.ProjectName}}
3236
PUBLIC
33-
{{ range .Dependencies }}
34-
-L{{$home}}/.c3pm/cache/{{.Name}}/{{.Version}}/lib
35-
{{ range .Targets }} -l{{.}} {{end}}
37+
{{range .Dependencies}}
38+
{{"${"}}{{.Name|ToUpper}}{{"}"}}
3639
{{end}}
3740
)
3841
`
3942

4043
func executable(v CMakeVars) (string, error) {
44+
funcMap := template.FuncMap{
45+
"ToUpper": strings.ToUpper,
46+
}
4147
cmake := bytes.NewBuffer([]byte{})
42-
tmpl, err := template.New("cmakeExecutable").Parse(addPlatformSpecificCmake(executableTemplate, v))
48+
tmpl, err := template.New("cmakeExecutable").Funcs(funcMap).Parse(addPlatformSpecificCmake(executableTemplate, v))
4349
if err != nil {
4450
return "", fmt.Errorf("could not parse cmake template: %w", err)
4551
}

cmakegen/gen.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type CMakeVars struct {
2626
Includes string
2727
IncludeDirs string
2828
ExportedDir string
29-
Home string
29+
C3pmGlobalDir string
3030
Dependencies []Dependency
3131
PublicIncludeDir string
3232
LinuxConfig *manifest.LinuxConfig
@@ -103,7 +103,7 @@ func varsFromProjectConfig(pc *config.ProjectConfig) (CMakeVars, error) {
103103
Includes: filesSliceToCMake(pc.Manifest.Files.Includes),
104104
IncludeDirs: filesSliceToCMake(pc.Manifest.Files.IncludeDirs),
105105
ExportedDir: filepath.ToSlash(filepath.Join(pc.ProjectRoot, pc.Manifest.Files.ExportedDir)),
106-
Home: filepath.ToSlash(os.Getenv("HOME")),
106+
C3pmGlobalDir: filepath.ToSlash(config.GlobalC3pmDirPath()),
107107
Dependencies: dependencies,
108108
LinuxConfig: pc.Manifest.LinuxConfig,
109109
LanguageStandard: pc.Manifest.Standard,

config/config.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"path"
77
"path/filepath"
8+
"runtime"
89
)
910

1011
type ProjectConfig struct {
@@ -48,7 +49,12 @@ func GlobalC3pmDirPath() string {
4849
if dir := os.Getenv("C3PM_USER_DIR"); dir != "" {
4950
return dir
5051
}
51-
homeDir := os.Getenv("HOME")
52+
var homeDir string
53+
if runtime.GOOS == "windows" {
54+
homeDir = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
55+
} else {
56+
homeDir = os.Getenv("HOME")
57+
}
5258
return path.Join(homeDir, ".c3pm")
5359
}
5460

0 commit comments

Comments
 (0)