diff --git a/cmakegen/exec.go b/cmakegen/exec.go index cd904dd..c0275a3 100644 --- a/cmakegen/exec.go +++ b/cmakegen/exec.go @@ -3,6 +3,7 @@ package cmakegen import ( "bytes" "fmt" + "strings" "text/template" ) @@ -13,33 +14,41 @@ set(CMAKE_CXX_STANDARD {{.LanguageStandard}}) add_executable({{.ProjectName}}) -target_sources({{.ProjectName}} PRIVATE {{.Sources}} {{.Includes}}) -{{$home:=.Home}} +target_sources({{.ProjectName}} PRIVATE + {{.Sources -}} + {{.Includes}} +) +{{$c3pmGlobalDir:=.C3pmGlobalDir}} target_include_directories( - {{.ProjectName}} PRIVATE {{.IncludeDirs}} - {{ range .Dependencies }} - {{$name:=.Name}} - {{$version:=.Version}} - {{ range .ExportedIncludeDirs }} - {{$home}}/.c3pm/cache/{{$name}}/{{$version}}/{{.}} - {{ end }} - {{end}} + {{- .ProjectName}} PRIVATE {{.IncludeDirs}} + {{- range .Dependencies }} + {{- $name:=.Name }} + {{- $version:=.Version}} + {{- range .ExportedIncludeDirs }} + {{ $c3pmGlobalDir }}/cache/{{$name}}/{{$version}}/{{.}} + {{- end }} + {{- end }} ) +{{range .Dependencies}} +find_library({{ .Name | ToUpper}} {{.Name}} "{{$c3pmGlobalDir}}/cache/{{.Name}}/{{.Version}}/lib") +{{end}} target_link_libraries( {{.ProjectName}} PUBLIC - {{ range .Dependencies }} - -L{{$home}}/.c3pm/cache/{{.Name}}/{{.Version}}/lib - {{ range .Targets }} -l{{.}} {{end}} - {{end}} + {{range .Dependencies}} + {{"${"}}{{.Name|ToUpper}}{{"}"}} + {{- end}} ) ` func executable(v CMakeVars) (string, error) { + funcMap := template.FuncMap{ + "ToUpper": strings.ToUpper, + } cmake := bytes.NewBuffer([]byte{}) - tmpl, err := template.New("cmakeExecutable").Parse(addPlatformSpecificCmake(executableTemplate, v)) + tmpl, err := template.New("cmakeExecutable").Funcs(funcMap).Parse(addPlatformSpecificCmake(executableTemplate, v)) if err != nil { return "", fmt.Errorf("could not parse cmake template: %w", err) } diff --git a/cmakegen/gen.go b/cmakegen/gen.go index 7042014..18ae07b 100644 --- a/cmakegen/gen.go +++ b/cmakegen/gen.go @@ -26,7 +26,7 @@ type CMakeVars struct { Includes string IncludeDirs string ExportedDir string - Home string + C3pmGlobalDir string Dependencies []Dependency PublicIncludeDir string LinuxConfig *manifest.LinuxConfig @@ -103,7 +103,7 @@ func varsFromProjectConfig(pc *config.ProjectConfig) (CMakeVars, error) { Includes: filesSliceToCMake(pc.Manifest.Files.Includes), IncludeDirs: filesSliceToCMake(pc.Manifest.Files.IncludeDirs), ExportedDir: filepath.ToSlash(filepath.Join(pc.ProjectRoot, pc.Manifest.Files.ExportedDir)), - Home: filepath.ToSlash(os.Getenv("HOME")), + C3pmGlobalDir: filepath.ToSlash(config.GlobalC3pmDirPath()), Dependencies: dependencies, LinuxConfig: pc.Manifest.LinuxConfig, LanguageStandard: pc.Manifest.Standard, diff --git a/config/config.go b/config/config.go index 5ca3dcd..23a0023 100644 --- a/config/config.go +++ b/config/config.go @@ -5,6 +5,7 @@ import ( "os" "path" "path/filepath" + "runtime" ) type ProjectConfig struct { @@ -48,7 +49,12 @@ func GlobalC3pmDirPath() string { if dir := os.Getenv("C3PM_USER_DIR"); dir != "" { return dir } - homeDir := os.Getenv("HOME") + var homeDir string + if runtime.GOOS == "windows" { + homeDir = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") + } else { + homeDir = os.Getenv("HOME") + } return path.Join(homeDir, ".c3pm") } diff --git a/ctpm/build.go b/ctpm/build.go index adf956d..bc78672 100644 --- a/ctpm/build.go +++ b/ctpm/build.go @@ -10,11 +10,14 @@ import ( func Build(pc *config.ProjectConfig) error { cmakeVariables := map[string]string{ - "CMAKE_LIBRARY_OUTPUT_DIRECTORY": pc.ProjectRoot, - "CMAKE_ARCHIVE_OUTPUT_DIRECTORY": pc.ProjectRoot, - "CMAKE_RUNTIME_OUTPUT_DIRECTORY": pc.ProjectRoot, - "CMAKE_INSTALL_PREFIX": filepath.ToSlash(filepath.Join(config.GlobalC3pmDirPath(), "cache", pc.Manifest.Name, pc.Manifest.Version.String())), - "CMAKE_BUILD_TYPE": "Release", + "CMAKE_LIBRARY_OUTPUT_DIRECTORY": pc.ProjectRoot, + "CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE": pc.ProjectRoot, + "CMAKE_ARCHIVE_OUTPUT_DIRECTORY": pc.ProjectRoot, + "CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE": pc.ProjectRoot, + "CMAKE_RUNTIME_OUTPUT_DIRECTORY": pc.ProjectRoot, + "CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE": pc.ProjectRoot, + "CMAKE_INSTALL_PREFIX": filepath.ToSlash(filepath.Join(config.GlobalC3pmDirPath(), "cache", pc.Manifest.Name, pc.Manifest.Version.String())), + "CMAKE_BUILD_TYPE": "Release", // Useful for Windows build //"MSVC_TOOLSET_VERSION": "141", //"MSVC_VERSION": "1916",