Skip to content

Commit 26dd47a

Browse files
feat(cmake): add cmake adapter (#65)
* feat(cmake): finish cmake adapter * fix(cmake): tests * fix: remove useless dep
1 parent 5b9bc6f commit 26dd47a

File tree

6 files changed

+94
-10
lines changed

6 files changed

+94
-10
lines changed

adapter/adapter.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package adapter
22

33
import (
44
"errors"
5+
"github.com/c3pm-labs/c3pm/adapter/cmake_adapter"
56
"github.com/c3pm-labs/c3pm/adapter/defaultadapter"
67
"github.com/c3pm-labs/c3pm/adapter/irrlichtadapter"
78
"github.com/c3pm-labs/c3pm/adapter_interface"
@@ -16,6 +17,8 @@ func (AdapterGetterImp) FromPC(adp *manifest.AdapterConfig) (adapter_interface.A
1617
return defaultadapter.New(AdapterGetterImp{}), nil
1718
case adp.Name == "irrlicht" && adp.Version.String() == "0.0.1":
1819
return irrlichtadapter.New(), nil
20+
case adp.Name == "cmake" && adp.Version.String() == "0.0.1":
21+
return cmake_adapter.New(), nil
1922
default:
2023
return nil, errors.New("only default adapter is supported")
2124
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package cmake_adapter
2+
3+
import (
4+
"fmt"
5+
"github.com/c3pm-labs/c3pm/adapter_interface"
6+
"github.com/c3pm-labs/c3pm/cmake"
7+
"github.com/c3pm-labs/c3pm/config"
8+
"gopkg.in/yaml.v2"
9+
"os"
10+
"path/filepath"
11+
)
12+
13+
type Adapter struct{}
14+
15+
type Config struct {
16+
Targets []string `yaml:"targets"`
17+
Variables map[string]string `yaml:"variables"`
18+
}
19+
20+
func (a Adapter) Build(pc *config.ProjectConfig) error {
21+
cfg, err := parseConfig(pc.Manifest.Build.Config)
22+
if err != nil {
23+
return err
24+
}
25+
26+
buildDir := filepath.Join(pc.LocalC3PMDirPath(), "build")
27+
28+
err = cmake.GenerateBuildFiles(".", buildDir, cfg.Variables)
29+
if err != nil {
30+
return err
31+
}
32+
33+
for _, target := range cfg.Targets {
34+
err = cmake.Build(buildDir, target)
35+
if err != nil {
36+
return err
37+
}
38+
39+
err := os.Rename(filepath.Join(buildDir, target), filepath.Join(pc.ProjectRoot, target))
40+
if err != nil {
41+
return fmt.Errorf("failed to move target %s to project directory: %v", target, err)
42+
}
43+
}
44+
45+
return nil
46+
}
47+
48+
func (a Adapter) Targets(pc *config.ProjectConfig) ([]string, error) {
49+
cfg, err := parseConfig(pc.Manifest.Build.Config)
50+
if err != nil {
51+
return nil, err
52+
}
53+
54+
return cfg.Targets, nil
55+
}
56+
57+
func (a Adapter) CmakeConfig(*config.ProjectConfig) (string, error) {
58+
return "", nil
59+
}
60+
61+
func New() *Adapter {
62+
return &Adapter{}
63+
}
64+
65+
var _ adapter_interface.Adapter = (*Adapter)(nil)
66+
67+
func parseConfig(c interface{}) (*Config, error) {
68+
out, err := yaml.Marshal(c)
69+
if err != nil {
70+
return nil, err
71+
}
72+
73+
cfg := &Config{}
74+
err = yaml.Unmarshal(out, cfg)
75+
if err != nil {
76+
return nil, err
77+
}
78+
return cfg, nil
79+
}

adapter/defaultadapter/defaultadapter.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/bmatcuk/doublestar"
66
"github.com/c3pm-labs/c3pm/adapter_interface"
7+
"github.com/c3pm-labs/c3pm/cmake"
78
"github.com/c3pm-labs/c3pm/config"
89
"github.com/c3pm-labs/c3pm/config/manifest"
910
"path/filepath"
@@ -52,12 +53,12 @@ func (a *DefaultAdapter) Build(pc *config.ProjectConfig) error {
5253
return fmt.Errorf("error generating config files: %w", err)
5354
}
5455

55-
err = cmakeGenerateBuildFiles(cmakeDirFromPc(pc), buildDirFromPc(pc), cmakeVariables)
56+
err = cmake.GenerateBuildFiles(cmakeDirFromPc(pc), buildDirFromPc(pc), cmakeVariables)
5657
if err != nil {
5758
return fmt.Errorf("cmake build failed: %w", err)
5859
}
5960

60-
err = cmakeBuild(buildDirFromPc(pc))
61+
err = cmake.Build(buildDirFromPc(pc), pc.Manifest.Name)
6162
if err != nil {
6263
return fmt.Errorf("build failed: %w", err)
6364
}

adapter/defaultadapter/cmake.go renamed to cmake/cmake.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// CMake is used internally by C3PM to manage the build and installation phases of using a C3PM project.
33
//
44
// 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
5-
package defaultadapter
5+
package cmake
66

77
import (
88
"fmt"
@@ -28,7 +28,7 @@ func executeCMakeCLI(args ...string) error {
2828
//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.
2929
//
3030
//See CMake's documentation for more information: https://cmake.org/cmake/help/latest/manual/cmake.1.html#generate-a-project-buildsystem
31-
func cmakeGenerateBuildFiles(sourceDir, buildDir string, variables map[string]string) error {
31+
func GenerateBuildFiles(sourceDir, buildDir string, variables map[string]string) error {
3232
args := []string{
3333
"-S", sourceDir,
3434
"-B", buildDir,
@@ -42,6 +42,6 @@ func cmakeGenerateBuildFiles(sourceDir, buildDir string, variables map[string]st
4242
//cmakeBuild runs the CMake CLI to build a C3PM project
4343
//
4444
//See CMake's documentation for more information: https://cmake.org/cmake/help/latest/manual/cmake.1.html#build-a-project
45-
func cmakeBuild(buildDir string) error {
45+
func Build(buildDir string, target string) error {
4646
return executeCMakeCLI("--build", buildDir, "--config", "Release")
4747
}

adapter/defaultadapter/cmake_test.go renamed to cmake/cmake_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package defaultadapter
1+
package cmake
22

33
import (
44
"bufio"
@@ -19,13 +19,13 @@ var _ = Describe("CMake interaction", func() {
1919
_ = os.RemoveAll(BUILD_DIR)
2020
})
2121
It("does generate the build directory", func() {
22-
err := cmakeGenerateBuildFiles("../../test_helpers/projects/cmakeProject", BUILD_DIR, map[string]string{})
22+
err := GenerateBuildFiles("../../test_helpers/projects/cmakeProject", BUILD_DIR, map[string]string{})
2323
Ω(err).ShouldNot(HaveOccurred())
2424
_, err = os.Stat(BUILD_DIR)
2525
Ω(err).ShouldNot(HaveOccurred())
2626
})
2727
It("uses the variables added", func() {
28-
err := cmakeGenerateBuildFiles("../../test_helpers/projects/cmakeProject", BUILD_DIR, map[string]string{"CMAKE_AR:FILEPATH": "/bin/ls"})
28+
err := GenerateBuildFiles("../../test_helpers/projects/cmakeProject", BUILD_DIR, map[string]string{"CMAKE_AR:FILEPATH": "/bin/ls"})
2929
Ω(err).ShouldNot(HaveOccurred())
3030
_, err = os.Stat(BUILD_DIR)
3131
Ω(err).ShouldNot(HaveOccurred())
@@ -55,13 +55,13 @@ var _ = Describe("CMake interaction", func() {
5555
})
5656
It("builds the project", func() {
5757
// Generate files
58-
err := cmakeGenerateBuildFiles("../../test_helpers/projects/cmakeProject", BUILD_DIR, map[string]string{})
58+
err := GenerateBuildFiles("../../test_helpers/projects/cmakeProject", BUILD_DIR, map[string]string{})
5959
Ω(err).ShouldNot(HaveOccurred())
6060
_, err = os.Stat(BUILD_DIR)
6161
Ω(err).ShouldNot(HaveOccurred())
6262

6363
// Build the project
64-
err = cmakeBuild(BUILD_DIR)
64+
err = Build(BUILD_DIR, "test1")
6565
Ω(err).ShouldNot(HaveOccurred())
6666
_, err = os.Stat(filepath.Join(BUILD_DIR, "test1"))
6767
Ω(err).ShouldNot(HaveOccurred())

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
7979
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
8080
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
8181
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
82+
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
8283
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
8384
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
8485
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=

0 commit comments

Comments
 (0)