Skip to content

Commit

Permalink
feat(cmake): add cmake adapter (#65)
Browse files Browse the repository at this point in the history
* feat(cmake): finish cmake adapter

* fix(cmake): tests

* fix: remove useless dep
  • Loading branch information
gabrielcolson authored Aug 22, 2021
1 parent 5b9bc6f commit 26dd47a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 10 deletions.
3 changes: 3 additions & 0 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package adapter

import (
"errors"
"github.com/c3pm-labs/c3pm/adapter/cmake_adapter"
"github.com/c3pm-labs/c3pm/adapter/defaultadapter"
"github.com/c3pm-labs/c3pm/adapter/irrlichtadapter"
"github.com/c3pm-labs/c3pm/adapter_interface"
Expand All @@ -16,6 +17,8 @@ func (AdapterGetterImp) FromPC(adp *manifest.AdapterConfig) (adapter_interface.A
return defaultadapter.New(AdapterGetterImp{}), nil
case adp.Name == "irrlicht" && adp.Version.String() == "0.0.1":
return irrlichtadapter.New(), nil
case adp.Name == "cmake" && adp.Version.String() == "0.0.1":
return cmake_adapter.New(), nil
default:
return nil, errors.New("only default adapter is supported")
}
Expand Down
79 changes: 79 additions & 0 deletions adapter/cmake_adapter/cmake_adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmake_adapter

import (
"fmt"
"github.com/c3pm-labs/c3pm/adapter_interface"
"github.com/c3pm-labs/c3pm/cmake"
"github.com/c3pm-labs/c3pm/config"
"gopkg.in/yaml.v2"
"os"
"path/filepath"
)

type Adapter struct{}

type Config struct {
Targets []string `yaml:"targets"`
Variables map[string]string `yaml:"variables"`
}

func (a Adapter) Build(pc *config.ProjectConfig) error {
cfg, err := parseConfig(pc.Manifest.Build.Config)
if err != nil {
return err
}

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

err = cmake.GenerateBuildFiles(".", buildDir, cfg.Variables)
if err != nil {
return err
}

for _, target := range cfg.Targets {
err = cmake.Build(buildDir, target)
if err != nil {
return err
}

err := os.Rename(filepath.Join(buildDir, target), filepath.Join(pc.ProjectRoot, target))
if err != nil {
return fmt.Errorf("failed to move target %s to project directory: %v", target, err)
}
}

return nil
}

func (a Adapter) Targets(pc *config.ProjectConfig) ([]string, error) {
cfg, err := parseConfig(pc.Manifest.Build.Config)
if err != nil {
return nil, err
}

return cfg.Targets, nil
}

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

func New() *Adapter {
return &Adapter{}
}

var _ adapter_interface.Adapter = (*Adapter)(nil)

func parseConfig(c interface{}) (*Config, error) {
out, err := yaml.Marshal(c)
if err != nil {
return nil, err
}

cfg := &Config{}
err = yaml.Unmarshal(out, cfg)
if err != nil {
return nil, err
}
return cfg, nil
}
5 changes: 3 additions & 2 deletions adapter/defaultadapter/defaultadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/bmatcuk/doublestar"
"github.com/c3pm-labs/c3pm/adapter_interface"
"github.com/c3pm-labs/c3pm/cmake"
"github.com/c3pm-labs/c3pm/config"
"github.com/c3pm-labs/c3pm/config/manifest"
"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), pc.Manifest.Name)
if err != nil {
return fmt.Errorf("build failed: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions adapter/defaultadapter/cmake.go → 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, target string) error {
return executeCMakeCLI("--build", buildDir, "--config", "Release")
}
10 changes: 5 additions & 5 deletions adapter/defaultadapter/cmake_test.go → cmake/cmake_test.go
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, "test1")
Ω(err).ShouldNot(HaveOccurred())
_, err = os.Stat(filepath.Join(BUILD_DIR, "test1"))
Ω(err).ShouldNot(HaveOccurred())
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down

0 comments on commit 26dd47a

Please sign in to comment.