MCPcopy Create free account
hub / github.com/celer-pkg/celer / GenerateCMakeLists

Method GenerateCMakeLists

generator/generator.go:46–102  ·  view source on GitHub ↗
(repoDir, libName, libVersion string)

Source from the content-addressed store, hash-verified

44}
45
46func (t *targetConfig) GenerateCMakeLists(repoDir, libName, libVersion string) error {
47 // Create the cmake directory.
48 if err := os.MkdirAll(filepath.Join(repoDir, "cmake"), os.ModePerm); err != nil {
49 return err
50 }
51
52 // Write the Config.cmake.in file.
53 configBytes, err := templates.ReadFile("templates/Config.cmake.in")
54 if err != nil {
55 return err
56 }
57 if err := os.WriteFile(filepath.Join(repoDir, "cmake", "Config.cmake.in"), configBytes, os.ModePerm); err != nil {
58 return err
59 }
60
61 // Generate CMakeLists.txt with the template.
62 tempalte := expr.If(len(t.Components) == 0, "templates/single/CMakeLists.txt.in", "templates/components/CMakeLists.txt.in")
63 cmakeListBytes, err := templates.ReadFile(tempalte)
64 if err != nil {
65 return err
66 }
67
68 // Set namespace to libName if it is empty.
69 namespace := t.libInfo.GetNamespace()
70 if namespace == "" {
71 namespace = libName
72 }
73
74 // If version is empty or invalid, set to 0.0.1
75 if libVersion == "" || !t.isValidVersionFormat(libVersion) {
76 libVersion = "0.0.1"
77 }
78
79 // Replace the placeholders with the actual values.
80 content := string(cmakeListBytes)
81 content = strings.ReplaceAll(content, "@NAMESPACE@", namespace)
82 content = strings.ReplaceAll(content, "@LIBNAME@", libName)
83 content = strings.ReplaceAll(content, "@VERSION@", libVersion)
84
85 // Replace filenames.
86 if len(t.Filenames) == 0 {
87 content = strings.ReplaceAll(content, "@FILENAMES@", t.Filename)
88 } else {
89 content = strings.ReplaceAll(content, "@FILENAMES@", strings.Join(t.Filenames, " "))
90 }
91
92 // Generate components if any.
93 if strings.Contains(content, "@COMPONENTS@") {
94 components, err := t.generateComponents()
95 if err != nil {
96 return err
97 }
98 content = strings.ReplaceAll(content, "@COMPONENTS@", components)
99 }
100
101 return os.WriteFile(filepath.Join(repoDir, "CMakeLists.txt"), []byte(content), os.ModePerm)
102}
103

Callers 1

CloneMethod · 0.80

Calls 6

isValidVersionFormatMethod · 0.95
generateComponentsMethod · 0.95
IfFunction · 0.92
MkdirAllMethod · 0.80
WriteFileMethod · 0.80
GetNamespaceMethod · 0.65

Tested by

no test coverage detected