From e59023e5b8226966d9695f7bb67d56257281a151 Mon Sep 17 00:00:00 2001 From: Tommy Li Date: Sat, 27 Jul 2024 21:07:40 +0800 Subject: [PATCH] feat(ccli): generate CMake config for compile database --- tools/ccli/repoinit/repoinit.go | 35 +++++++++++++++++++ .../repoinit/templates/CMakeLists.txt.tmpl | 25 +++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 tools/ccli/repoinit/templates/CMakeLists.txt.tmpl diff --git a/tools/ccli/repoinit/repoinit.go b/tools/ccli/repoinit/repoinit.go index 4ddb632..049bf57 100644 --- a/tools/ccli/repoinit/repoinit.go +++ b/tools/ccli/repoinit/repoinit.go @@ -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" @@ -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 diff --git a/tools/ccli/repoinit/templates/CMakeLists.txt.tmpl b/tools/ccli/repoinit/templates/CMakeLists.txt.tmpl new file mode 100644 index 0000000..39d7861 --- /dev/null +++ b/tools/ccli/repoinit/templates/CMakeLists.txt.tmpl @@ -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("$<$:-Og>") +add_compile_options("$<$:-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 }}