Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(adapter/sfml): added build for SFML adapter #48

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"github.com/c3pm-labs/c3pm/adapter/defaultadapter"
"github.com/c3pm-labs/c3pm/adapter/irrlichtadapter"
"github.com/c3pm-labs/c3pm/adapter/sfml"
"github.com/c3pm-labs/c3pm/adapter_interface"
"github.com/c3pm-labs/c3pm/config/manifest"
)
Expand All @@ -14,6 +15,8 @@ func (AdapterGetterImp) FromPC(adp *manifest.AdapterConfig) (adapter_interface.A
switch {
case adp.Name == "c3pm" && adp.Version.String() == "0.0.1":
return defaultadapter.New(AdapterGetterImp{}), nil
case adp.Name == "sfml" && adp.Version.String() == "0.0.1":
return sfml.New(), nil
case adp.Name == "irrlicht" && adp.Version.String() == "0.0.1":
return irrlichtadapter.New(), nil
default:
Expand Down
18 changes: 11 additions & 7 deletions adapter/defaultadapter/cmakegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,24 @@ func dependenciesToCMake(pc *config.ProjectConfig, adapterGetter adapter_interfa
var depsConfig = ""
i := 0
for n, v := range pc.Manifest.Dependencies {
m, err := manifest.Load(filepath.Join(config.LibCachePath(n, v), "c3pm.yml"))
p, err := config.Load(config.LibCachePath(n, v))
if err != nil {
return nil, "", err
}
deps[i] = dependency{
Name: n,
Version: v,
Targets: m.Targets(),
IncludeDirs: m.Publish.IncludeDirs,
adp, err := adapterGetter.FromPC(p.Manifest.Build.Adapter)
if err != nil {
return nil, "", err
}
adp, err := adapterGetter.FromPC(m.Build.Adapter)
targets, err := adp.Targets(pc)
if err != nil {
return nil, "", err
}
deps[i] = dependency{
Name: n,
Version: v,
Targets: targets,
IncludeDirs: p.Manifest.Publish.IncludeDirs,
}
dependencyConfig, err := adp.CmakeConfig(pc)
if err != nil {
return nil, "", err
Expand Down
9 changes: 5 additions & 4 deletions adapter/defaultadapter/defaultadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/c3pm-labs/c3pm/adapter_interface"
"github.com/c3pm-labs/c3pm/config"
"github.com/c3pm-labs/c3pm/config/manifest"
"github.com/c3pm-labs/c3pm/internal/cmake"
"path/filepath"
)

Expand Down Expand Up @@ -52,12 +53,12 @@ func (a *DefaultAdapter) Build(pc *config.ProjectConfig) error {
return fmt.Errorf("error generating config files: %w", err)
}

err = cmakeGenerateBuildFiles(cmakeDirFromPc(pc), buildDirFromPc(pc), cmakeVariables)
err = cmake.GenerateBuildFiles(cmakeDirFromPc(pc), buildDirFromPc(pc), cmakeVariables)
if err != nil {
return fmt.Errorf("cmake build failed: %w", err)
}

err = cmakeBuild(buildDirFromPc(pc))
err = cmake.Build(buildDirFromPc(pc))
if err != nil {
return fmt.Errorf("build failed: %w", err)
}
Expand Down Expand Up @@ -87,8 +88,8 @@ func hasFileMatchingRule(rules []string, projectRoot string) (bool, error) {
return false, nil
}

func (a *DefaultAdapter) Targets(_ *config.ProjectConfig) ([]string, error) {
return nil, nil
func (a *DefaultAdapter) Targets(pc *config.ProjectConfig) ([]string, error) {
return []string{pc.Manifest.Name}, nil
}

func (a *DefaultAdapter) CmakeConfig(_ *config.ProjectConfig) (string, error) {
Expand Down
17 changes: 12 additions & 5 deletions adapter/defaultadapter/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,24 @@ target_include_directories(
{{- end }}
{{- end }}
)
{{range .Dependencies}}
find_library({{ .Name | ToUpper}} {{.Name}} "{{$c3pmGlobalDir}}/cache/{{.Name}}/{{.Version}}/")
{{end}}

{{- range $dep := .Dependencies}}
{{- range $target := $dep.Targets }}
find_library({{ $dep.Name | ToUpper}}-{{$target}} {{$target}} "{{$c3pmGlobalDir}}/cache/{{$dep.Name}}/{{$dep.Version}}/")
{{- end }}
{{- end}}

{{.DependenciesConfig}}

target_link_libraries(
{{.ProjectName}}
PUBLIC
{{range .Dependencies}}
$<$<NOT:$<STREQUAL:"{{"${"}}{{.Name|ToUpper}}{{"}"}}","{{.Name|ToUpper}}-NOTFOUND">>:{{"${"}}{{.Name|ToUpper}}{{"}"}}>
{{- range $dep := .Dependencies}}
{{- range $target := $dep.Targets }}
{{- $lib := $dep.Name|ToUpper}}
{{- $libname := printf "%v-%v" $lib $target }}
$<$<NOT:$<STREQUAL:"{{"${"}}{{$libname}}{{"}"}}","{{$libname}}-NOTFOUND">>:{{"${"}}{{$libname}}{{"}"}}>
{{- end }}
{{- end}}
)
`
Expand Down
2 changes: 1 addition & 1 deletion adapter/irrlichtadapter/irrlichtadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ func (a *IrrlichtAdapter) CmakeConfig(pc *config.ProjectConfig) (string, error)
}

func (a *IrrlichtAdapter) Targets(_ *config.ProjectConfig) ([]string, error) {
return nil, nil
return []string{"Irrlicht"}, nil
}
56 changes: 56 additions & 0 deletions adapter/sfml/sfml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package sfml

import (
"fmt"
"github.com/c3pm-labs/c3pm/config"
"github.com/c3pm-labs/c3pm/internal/cmake"
"path/filepath"
)

type Adapter struct{}

func (a *Adapter) Build(pc *config.ProjectConfig) error {
cmakeVariables := map[string]string{
"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_BUILD_TYPE": "Release",
"BUILD_SHARED_LIB": "OFF",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"BUILD_SHARED_LIB": "OFF",
"BUILD_SHARED_LIBS": "OFF",

'S' is missing, as seen in https://www.sfml-dev.org/tutorials/2.5/compile-with-cmake.php

}

sourceDir := pc.ProjectRoot
buildDir := filepath.Join(pc.ProjectRoot, "build")

err := cmake.GenerateBuildFiles(sourceDir, buildDir, cmakeVariables)
if err != nil {
return fmt.Errorf("cmake build failed: %w", err)
}

err = cmake.Build(buildDir)
if err != nil {
return fmt.Errorf("build failed: %w", err)
}

return nil
}

func (a *Adapter) Targets(_ *config.ProjectConfig) ([]string, error) {
return []string{
"sfml-system",
"sfml-window",
"sfml-graphics",
"sfml-network",
"sfml-audio",
}, nil
}

func (a *Adapter) CmakeConfig(_ *config.ProjectConfig) (string, error) {
return "", nil
}

func New() *Adapter {
return &Adapter{}
}
6 changes: 3 additions & 3 deletions adapter/defaultadapter/cmake.go → internal/cmake/cmake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// CMake is used internally by C3PM to manage the build and installation phases of using a C3PM project.
//
// More information about what the CMake CLI does can be found on CMake's website: https://cmake.org/cmake/help/latest/manual/cmake.1.html
package defaultadapter
package cmake

import (
"fmt"
Expand All @@ -28,7 +28,7 @@ func executeCMakeCLI(args ...string) error {
//C3PM uses CMake's -S option for setting the source directory, the -B option for the build directory, and the -D option for setting build variables.
//
//See CMake's documentation for more information: https://cmake.org/cmake/help/latest/manual/cmake.1.html#generate-a-project-buildsystem
func cmakeGenerateBuildFiles(sourceDir, buildDir string, variables map[string]string) error {
func GenerateBuildFiles(sourceDir, buildDir string, variables map[string]string) error {
args := []string{
"-S", sourceDir,
"-B", buildDir,
Expand All @@ -42,6 +42,6 @@ func cmakeGenerateBuildFiles(sourceDir, buildDir string, variables map[string]st
//cmakeBuild runs the CMake CLI to build a C3PM project
//
//See CMake's documentation for more information: https://cmake.org/cmake/help/latest/manual/cmake.1.html#build-a-project
func cmakeBuild(buildDir string) error {
func Build(buildDir string) error {
return executeCMakeCLI("--build", buildDir, "--config", "Release")
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package defaultadapter
package cmake

import (
"bufio"
Expand All @@ -19,13 +19,13 @@ var _ = Describe("CMake interaction", func() {
_ = os.RemoveAll(BUILD_DIR)
})
It("does generate the build directory", func() {
err := cmakeGenerateBuildFiles("../../test_helpers/projects/cmakeProject", BUILD_DIR, map[string]string{})
err := GenerateBuildFiles("../../test_helpers/projects/cmakeProject", BUILD_DIR, map[string]string{})
Ω(err).ShouldNot(HaveOccurred())
_, err = os.Stat(BUILD_DIR)
Ω(err).ShouldNot(HaveOccurred())
})
It("uses the variables added", func() {
err := cmakeGenerateBuildFiles("../../test_helpers/projects/cmakeProject", BUILD_DIR, map[string]string{"CMAKE_AR:FILEPATH": "/bin/ls"})
err := GenerateBuildFiles("../../test_helpers/projects/cmakeProject", BUILD_DIR, map[string]string{"CMAKE_AR:FILEPATH": "/bin/ls"})
Ω(err).ShouldNot(HaveOccurred())
_, err = os.Stat(BUILD_DIR)
Ω(err).ShouldNot(HaveOccurred())
Expand Down Expand Up @@ -55,13 +55,13 @@ var _ = Describe("CMake interaction", func() {
})
It("builds the project", func() {
// Generate files
err := cmakeGenerateBuildFiles("../../test_helpers/projects/cmakeProject", BUILD_DIR, map[string]string{})
err := GenerateBuildFiles("../../test_helpers/projects/cmakeProject", BUILD_DIR, map[string]string{})
Ω(err).ShouldNot(HaveOccurred())
_, err = os.Stat(BUILD_DIR)
Ω(err).ShouldNot(HaveOccurred())

// Build the project
err = cmakeBuild(BUILD_DIR)
err = Build(BUILD_DIR)
Ω(err).ShouldNot(HaveOccurred())
_, err = os.Stat(filepath.Join(BUILD_DIR, "test1"))
Ω(err).ShouldNot(HaveOccurred())
Expand Down
12 changes: 4 additions & 8 deletions test_helpers/projects/publishProjects/excludeManifest/c3pm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ description: includeAndExcludeFile for publish tests
version: 1.0.0
standard: "20"
license: UNLICENSED
files:
sources:
- '**/*.cpp'
includes:
- '**/*.hpp'
include_dirs: []
exported_dir: ""
exported_include_dirs: []
build:
adaper:
name: c3pm
version: 0.0.1
publish:
exclude:
- 'c3pm.yml'
Expand Down