Skip to content

Commit

Permalink
Merge pull request #16 from c3pm-labs/feat/improve-cmake
Browse files Browse the repository at this point in the history
feat(cmake): Improve cross platform cmake generation
  • Loading branch information
Codelax authored Jan 18, 2021
2 parents 87de4fc + 6ea57db commit 9bb3519
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
39 changes: 24 additions & 15 deletions cmakegen/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmakegen
import (
"bytes"
"fmt"
"strings"
"text/template"
)

Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmakegen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type CMakeVars struct {
Includes string
IncludeDirs string
ExportedDir string
Home string
C3pmGlobalDir string
Dependencies []Dependency
PublicIncludeDir string
LinuxConfig *manifest.LinuxConfig
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"
"path/filepath"
"runtime"
)

type ProjectConfig struct {
Expand Down Expand Up @@ -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")
}

Expand Down
13 changes: 8 additions & 5 deletions ctpm/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9bb3519

Please sign in to comment.