Skip to content

Commit

Permalink
feat(ccli): generate CMake config for compile database
Browse files Browse the repository at this point in the history
  • Loading branch information
STommydx committed Jul 27, 2024
1 parent b22213a commit e59023e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tools/ccli/repoinit/repoinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os/exec"
"path"
"path/filepath"
"strings"
"text/template"

"github.com/STommydx/cp-templates/tools/ccli/spinner"
"github.com/go-git/go-git/v5"
Expand Down Expand Up @@ -99,6 +101,39 @@ func Run(settings Settings) error {
return fmt.Errorf("failed to copy template file: %w", err)
}
}
cmakeConfig := struct {
ProjectName string
SourceFiles []struct {
Input string
Output string
}
}{
ProjectName: path.Base(settings.Directory),
}
files, err := os.ReadDir(settings.Directory)
if err != nil {
return fmt.Errorf("failed to read directory: %w", err)
}
for _, file := range files {
if filepath.Ext(file.Name()) == ".cpp" {
cmakeConfig.SourceFiles = append(cmakeConfig.SourceFiles, struct {
Input string
Output string
}{
Input: file.Name(),
Output: strings.ReplaceAll(file.Name(), ".cpp", ".out"),
})
}
}
cmakeListFile, err := os.Create(path.Join(settings.Directory, "CMakeLists.txt"))
if err != nil {
return fmt.Errorf("failed to create CMakeLists.txt file: %w", err)
}
defer cmakeListFile.Close()
cmakeListTemplate := template.Must(template.ParseFS(templatesFS, "templates/CMakeLists.txt.tmpl"))
if err := cmakeListTemplate.Execute(cmakeListFile, cmakeConfig); err != nil {
return fmt.Errorf("failed to execute CMakeLists.txt template: %w", err)
}
return nil
}); err != nil {
return err
Expand Down
25 changes: 25 additions & 0 deletions tools/ccli/repoinit/templates/CMakeLists.txt.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.14)

project(
{{ .ProjectName }}
)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()

set(CMAKE_CXX_STANDARD 20)
add_compile_options("$<$<CONFIG:Debug>:-Og>")
add_compile_options("$<$<CONFIG:Release>:-O2>")
add_compile_options(-Wall)
add_compile_options(-Wextra)
add_compile_options(-fsanitize=address)
add_compile_options(-fsanitize=undefined)
add_link_options(-fsanitize=address)
add_link_options(-fsanitize=undefined)

{{ range .SourceFiles -}}
add_executable({{ .Output }} {{ .Input }})
{{ end }}

0 comments on commit e59023e

Please sign in to comment.