diff --git a/adapter/adapter.go b/adapter/adapter.go index a01a175..e0b3fc9 100644 --- a/adapter/adapter.go +++ b/adapter/adapter.go @@ -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" @@ -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") } diff --git a/adapter/cmake_adapter/cmake_adapter.go b/adapter/cmake_adapter/cmake_adapter.go new file mode 100644 index 0000000..0e5ede9 --- /dev/null +++ b/adapter/cmake_adapter/cmake_adapter.go @@ -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 +} diff --git a/adapter/defaultadapter/defaultadapter.go b/adapter/defaultadapter/defaultadapter.go index 9000b80..1f572ff 100644 --- a/adapter/defaultadapter/defaultadapter.go +++ b/adapter/defaultadapter/defaultadapter.go @@ -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" @@ -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) } diff --git a/adapter/defaultadapter/cmake.go b/cmake/cmake.go similarity index 90% rename from adapter/defaultadapter/cmake.go rename to cmake/cmake.go index 2b62cf1..8900710 100644 --- a/adapter/defaultadapter/cmake.go +++ b/cmake/cmake.go @@ -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" @@ -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, @@ -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") } diff --git a/adapter/defaultadapter/cmake_test.go b/cmake/cmake_test.go similarity index 80% rename from adapter/defaultadapter/cmake_test.go rename to cmake/cmake_test.go index a050117..f2b3b95 100644 --- a/adapter/defaultadapter/cmake_test.go +++ b/cmake/cmake_test.go @@ -1,4 +1,4 @@ -package defaultadapter +package cmake import ( "bufio" @@ -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()) @@ -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()) diff --git a/go.sum b/go.sum index 3fb000f..cfb5e83 100644 --- a/go.sum +++ b/go.sum @@ -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=