From eda341026b1b308ffad5c1b5b9ec401857f9926c Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Wed, 16 Apr 2025 13:09:54 +0800 Subject: [PATCH 01/12] init cpp repo --- .clang-format | 4 +++ .clangd | 28 +++++++++++++++ .envrc | 3 ++ .gitignore | 4 +++ Makefile | 26 ++++++++++++++ flake.lock | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 63 ++++++++++++++++++++++++++++++++++ main.cxx | 54 +++++++++++++++++++++++++++++ 8 files changed, 276 insertions(+) create mode 100644 .clang-format create mode 100644 .clangd create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 main.cxx diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..06e3c51 --- /dev/null +++ b/.clang-format @@ -0,0 +1,4 @@ +--- +Language: Cpp +BasedOnStyle: Google +... diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..d839602 --- /dev/null +++ b/.clangd @@ -0,0 +1,28 @@ +CompileFlags: + Add: + - -Xclang + - -fno-validate-pch + - -fretain-comments-from-system-headers + Compiler: clang++ +Index: + Background: true +Diagnostics: + ClangTidy: + Add: + - modernize-* + - performance-* + - cppcoreguidelines-* + ClangTidyChecks: true + Options: + UseChecks: + - "*" + WarningAsErrors: cppcoreguidelines-* +InlayHints: + Enabled: true + ParameterNames: true + DeducedTypes: true + Designators: true +Completion: + AllScopes: true +SemanticTokens: + Enabled: true diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..5bfe4a2 --- /dev/null +++ b/.envrc @@ -0,0 +1,3 @@ +watch_file ./flake.* + +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f3bea30 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.direnv/ +/build/ +/.cache/ +/compile_commands.json diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dfd2d4e --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +.RECIPEPREFIX = > +.DEFAULT_GOAL = build/main.out +CXX = clang++ +CXXFLAGS += -std=c++26 -Wno-experimental-header-units +LDFLAGS += -std=c++26 +SYSTEMHEADER_FLAGS = -Wno-deprecated-builtins -Wno-pragma-system-header-outside-header -Wno-keyword-compat +build_module_path = $(let first rest,$1,$(if $(rest),-fmodule-file=$(first) $(call build_module_path,$(rest)),-fmodule-file=$(first))) + +build/main.out: build/main.o +> $(CXX) $(LDFLAGS) $^ -o $@ + +build/main.o: main.cxx build/print.pcm build/type_traits.pcm +> $(CXX) $(CXXFLAGS) -c $(filter-out %.pcm,$^) $(call build_module_path,$(filter %.pcm,$^)) +> @mv $(patsubst build/%,%,$@) $@ + +build/%.pcm: +> $(CXX) $(CXXFLAGS) -xc++-system-header --precompile $(patsubst build/%.pcm,%,$@) -o $@ $(SYSTEMHEADER_FLAGS) + +.PHONY: clean run +clean: +> @-rm -rf ./build/* +bear: clean +> bear -- $(MAKE) + +run: $(.DEFAULT_GOAL) +> @./$(.DEFAULT_GOAL) diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..e975658 --- /dev/null +++ b/flake.lock @@ -0,0 +1,94 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "id": "flake-utils", + "type": "indirect" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1745377448, + "narHash": "sha256-jhZDfXVKdD7TSEGgzFJQvEEZ2K65UMiqW5YJ2aIqxMA=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "507b63021ada5fee621b6ca371c4fca9ca46f52c", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-cmake": { + "locked": { + "lastModified": 1745570837, + "narHash": "sha256-8MwKbng4lvA5wgBbOx0+VohonuMnUd62xJ6gE4tdXdU=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "ceecbb5d1184da5866a824b7accbb4a8c07a73eb", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "pull/394444/head", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-gcc15": { + "locked": { + "lastModified": 1745644951, + "narHash": "sha256-Rqnc3VvR2T6vJ2JwXhX6h9zwSYjWITk3YdOUKcCM284=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "281c0c16c9c6e2e44e78334f55bbd43dc51c0d70", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "pull/401948/head", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "nixpkgs-cmake": "nixpkgs-cmake", + "nixpkgs-gcc15": "nixpkgs-gcc15" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..fa53e73 --- /dev/null +++ b/flake.nix @@ -0,0 +1,63 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + nixpkgs-gcc15.url = "github:NixOS/nixpkgs/?ref=pull/401948/head"; + nixpkgs-cmake.url = "github:NixOS/nixpkgs/?ref=pull/394444/head"; + }; + outputs = + { + nixpkgs-cmake, + nixpkgs-gcc15, + nixpkgs, + flake-utils, + ... + }: + flake-utils.lib.eachDefaultSystem ( + system: + let + inherit + (import nixpkgs-cmake { + inherit system; + config = { + cudaSupport = true; + }; + }) + cmake + ; + inherit + (import nixpkgs-gcc15 { + inherit system; + config = { + cudaSupport = true; + }; + }) + gcc15 + gcc15Stdenv + ; + pkgs = import nixpkgs { + inherit system; + config = { + cudaSupport = true; + }; + }; + in + { + devShells.default = + pkgs.mkShell.override + { + stdenv = pkgs.llvmPackages_20.libcxxStdenv; + # stdenv = gcc15Stdenv; + } + { + packages = with pkgs; [ + llvmPackages_20.clang-tools + gnumake + llvmPackages_20.clang + bear + # cmake + # gcc15 + ]; + }; + } + ); +} diff --git a/main.cxx b/main.cxx new file mode 100644 index 0000000..358ccec --- /dev/null +++ b/main.cxx @@ -0,0 +1,54 @@ +import ; +import ; + +template +struct Builder; +template +struct BuildMixin {}; + +template <> +struct BuildMixin { + auto build(this auto&& self) { return self.p1 + self.p2; } +}; + +template +struct Param1Mixin {}; + +template <> +struct Param1Mixin { + auto fill_param1(this auto&& self) { + self.p1 = 1; + return Builder::param2_v>( + self); + } +}; + +template +struct Param2Mixin {}; + +template <> +struct Param2Mixin { + auto fill_param2(this auto&& self) { + self.p2 = 1; + return Builder::param1_v, true>( + self); + } +}; + +template +struct Builder : BuildMixin, + Param1Mixin, + Param2Mixin { + const constinit static auto param1_v = param1; + const constinit static auto param2_v = param2; + int p1; + int p2; + Builder() : p1(0), p2(0) {} + Builder(auto&& other) : p1(other.p1), p2(other.p2) {} +}; + +auto main() -> int { + Builder builder; + auto ret = builder.fill_param1().fill_param2().build(); + std::println("{}", ret); +} From 0017c36e2fbf531443e19a29d129a127e16d8a1b Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Mon, 28 Apr 2025 13:58:27 +0800 Subject: [PATCH 02/12] add requires for function --- main.cxx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/main.cxx b/main.cxx index 358ccec..540e1f8 100644 --- a/main.cxx +++ b/main.cxx @@ -8,7 +8,11 @@ struct BuildMixin {}; template <> struct BuildMixin { - auto build(this auto&& self) { return self.p1 + self.p2; } + auto build(this auto&& self) + requires requires { self.p1 + self.p2; } + { + return self.p1 + self.p2; + } }; template @@ -16,7 +20,9 @@ struct Param1Mixin {}; template <> struct Param1Mixin { - auto fill_param1(this auto&& self) { + auto fill_param1(this auto&& self) + requires requires { self.p1; } + { self.p1 = 1; return Builder::param2_v>( self); @@ -28,7 +34,9 @@ struct Param2Mixin {}; template <> struct Param2Mixin { - auto fill_param2(this auto&& self) { + auto fill_param2(this auto&& self) + requires requires { self.p2; } + { self.p2 = 1; return Builder::param1_v, true>( self); From aba9ddc048fa5ca81fc5eb74be7beca192db7753 Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Mon, 28 Apr 2025 13:58:27 +0800 Subject: [PATCH 03/12] use divided module --- .clangd | 10 +------- .gitignore | 1 - .helix/languages.toml | 28 ++++++++++++++++++++++ Makefile | 32 ++++++++++++++++++------- builder.cxxm | 56 +++++++++++++++++++++++++++++++++++++++++++ compile_commands.json | 29 ++++++++++++++++++++++ main.cxx | 56 +------------------------------------------ 7 files changed, 138 insertions(+), 74 deletions(-) create mode 100644 .helix/languages.toml create mode 100644 builder.cxxm create mode 100644 compile_commands.json diff --git a/.clangd b/.clangd index d839602..9729b9e 100644 --- a/.clangd +++ b/.clangd @@ -1,22 +1,16 @@ CompileFlags: Add: + - -std=c++26 - -Xclang - -fno-validate-pch - -fretain-comments-from-system-headers Compiler: clang++ -Index: - Background: true Diagnostics: ClangTidy: Add: - modernize-* - performance-* - cppcoreguidelines-* - ClangTidyChecks: true - Options: - UseChecks: - - "*" - WarningAsErrors: cppcoreguidelines-* InlayHints: Enabled: true ParameterNames: true @@ -24,5 +18,3 @@ InlayHints: Designators: true Completion: AllScopes: true -SemanticTokens: - Enabled: true diff --git a/.gitignore b/.gitignore index f3bea30..d00e3a1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ /.direnv/ /build/ /.cache/ -/compile_commands.json diff --git a/.helix/languages.toml b/.helix/languages.toml new file mode 100644 index 0000000..a85688c --- /dev/null +++ b/.helix/languages.toml @@ -0,0 +1,28 @@ +[[language]] +name = "cpp" +file-types = [ + "cc", + "hh", + "c++", + "cpp", + "hpp", + "h", + "ipp", + "tpp", + "cxx", + "hxx", + "ixx", + "txx", + "ino", + "C", + "H", + "cu", + "cuh", + "cppm", + "cxxm", + "h++", + "ii", + "inl", + { glob = ".hpp.in" }, + { glob = ".h.in" }, +] diff --git a/Makefile b/Makefile index dfd2d4e..4cd9558 100644 --- a/Makefile +++ b/Makefile @@ -1,26 +1,40 @@ .RECIPEPREFIX = > .DEFAULT_GOAL = build/main.out CXX = clang++ -CXXFLAGS += -std=c++26 -Wno-experimental-header-units +CXXFLAGS += -std=c++26 -Wno-experimental-header-units -Wno-unused-command-line-argument LDFLAGS += -std=c++26 SYSTEMHEADER_FLAGS = -Wno-deprecated-builtins -Wno-pragma-system-header-outside-header -Wno-keyword-compat -build_module_path = $(let first rest,$1,$(if $(rest),-fmodule-file=$(first) $(call build_module_path,$(rest)),-fmodule-file=$(first))) +module_pair = $(if $(findstring user,$1),$(patsubst build/user/%.pcm,%=,$1))$1 +build_module_path = $(if $1,$(let first rest,$1,$(if $(rest),-fmodule-file=$(call module_pair,$(first)) $(call build_module_path,$(rest)),-fmodule-file=$(call module_pair,$(first))))) +object_build = $(filter-out %.pcm,$^) $(call build_module_path,$(filter %.pcm,$^)) +autodir = @mkdir -p $(@D) -build/main.out: build/main.o +build/main.out: build/main.o build/builder.o +> $(call autodir) > $(CXX) $(LDFLAGS) $^ -o $@ -build/main.o: main.cxx build/print.pcm build/type_traits.pcm -> $(CXX) $(CXXFLAGS) -c $(filter-out %.pcm,$^) $(call build_module_path,$(filter %.pcm,$^)) +build/main.o:: main.cxx build/system/print.pcm build/user/builder.pcm +build/builder.o:: builder.cxxm build/system/type_traits.pcm +build/user/builder.pcm:: build/system/type_traits.pcm + +build/%.o:: +> $(call autodir) +> $(CXX) $(CXXFLAGS) -c $(call object_build) > @mv $(patsubst build/%,%,$@) $@ -build/%.pcm: -> $(CXX) $(CXXFLAGS) -xc++-system-header --precompile $(patsubst build/%.pcm,%,$@) -o $@ $(SYSTEMHEADER_FLAGS) +build/user/%.pcm:: %.cxxm +> $(call autodir) +> $(CXX) $(CXXFLAGS) --precompile -o $@ $(call object_build) + +build/system/%.pcm: +> $(call autodir) +> $(CXX) $(CXXFLAGS) -xc++-system-header --precompile $(patsubst build/system/%.pcm,%,$@) -o $@ $(SYSTEMHEADER_FLAGS) .PHONY: clean run clean: -> @-rm -rf ./build/* +> @-rm -rf ./build bear: clean -> bear -- $(MAKE) +> bear --append -- $(MAKE) run: $(.DEFAULT_GOAL) > @./$(.DEFAULT_GOAL) diff --git a/builder.cxxm b/builder.cxxm new file mode 100644 index 0000000..7d91a94 --- /dev/null +++ b/builder.cxxm @@ -0,0 +1,56 @@ +export module builder; +import ; + +export template +struct Builder; +template +struct BuildMixin {}; + +template <> +struct BuildMixin { + auto build(this auto&& self) + requires requires { self.p1 + self.p2; } + { + return self.p1 + self.p2; + } +}; + +template +struct Param1Mixin {}; + +template <> +struct Param1Mixin { + auto fill_param1(this auto&& self) + requires requires { self.p1; } + { + self.p1 = 1; + return Builder::param2_v>( + self); + } +}; + +template +struct Param2Mixin {}; + +template <> +struct Param2Mixin { + auto fill_param2(this auto&& self) + requires requires { self.p2; } + { + self.p2 = 1; + return Builder::param1_v, true>( + self); + } +}; + +template +struct Builder : BuildMixin, + Param1Mixin, + Param2Mixin { + const constinit static auto param1_v = param1; + const constinit static auto param2_v = param2; + int p1; + int p2; + Builder() : p1(0), p2(0) {} + Builder(auto&& other) : p1(other.p1), p2(other.p2) {} +}; diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..f224f89 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1,29 @@ +[ + { + "arguments": [ + "/nix/store/k50vj1b6vpz84n88vk09gxsgcyqrg00m-clang-wrapper-20.1.3/bin/clang++", + "-std=c++26", + "-Wno-experimental-header-units", + "-Wno-unused-command-line-argument", + "-c", + "-fmodule-file=build/system/print.pcm", + "-fmodule-file=builder=build/user/builder.pcm", + "main.cxx" + ], + "directory": "/home/david/code/cxx", + "file": "/home/david/code/cxx/main.cxx" + }, + { + "arguments": [ + "/nix/store/k50vj1b6vpz84n88vk09gxsgcyqrg00m-clang-wrapper-20.1.3/bin/clang++", + "-std=c++26", + "-Wno-experimental-header-units", + "-Wno-unused-command-line-argument", + "-c", + "-fmodule-file=build/system/type_traits.pcm", + "builder.cxxm" + ], + "directory": "/home/david/code/cxx", + "file": "/home/david/code/cxx/builder.cxxm" + } +] diff --git a/main.cxx b/main.cxx index 540e1f8..b87f7c4 100644 --- a/main.cxx +++ b/main.cxx @@ -1,59 +1,5 @@ import ; -import ; - -template -struct Builder; -template -struct BuildMixin {}; - -template <> -struct BuildMixin { - auto build(this auto&& self) - requires requires { self.p1 + self.p2; } - { - return self.p1 + self.p2; - } -}; - -template -struct Param1Mixin {}; - -template <> -struct Param1Mixin { - auto fill_param1(this auto&& self) - requires requires { self.p1; } - { - self.p1 = 1; - return Builder::param2_v>( - self); - } -}; - -template -struct Param2Mixin {}; - -template <> -struct Param2Mixin { - auto fill_param2(this auto&& self) - requires requires { self.p2; } - { - self.p2 = 1; - return Builder::param1_v, true>( - self); - } -}; - -template -struct Builder : BuildMixin, - Param1Mixin, - Param2Mixin { - const constinit static auto param1_v = param1; - const constinit static auto param2_v = param2; - int p1; - int p2; - Builder() : p1(0), p2(0) {} - Builder(auto&& other) : p1(other.p1), p2(other.p2) {} -}; +import builder; auto main() -> int { Builder builder; From df884fd17ed126571cb580b2eabff049081fd894 Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Mon, 28 Apr 2025 18:55:59 +0800 Subject: [PATCH 04/12] use as makefile recipeprefix for more editor support --- Makefile | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 4cd9558..141602b 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,3 @@ -.RECIPEPREFIX = > .DEFAULT_GOAL = build/main.out CXX = clang++ CXXFLAGS += -std=c++26 -Wno-experimental-header-units -Wno-unused-command-line-argument @@ -10,31 +9,31 @@ object_build = $(filter-out %.pcm,$^) $(call build_module_path,$(filter %.pcm,$^ autodir = @mkdir -p $(@D) build/main.out: build/main.o build/builder.o -> $(call autodir) -> $(CXX) $(LDFLAGS) $^ -o $@ + $(call autodir) + $(CXX) $(LDFLAGS) $^ -o $@ build/main.o:: main.cxx build/system/print.pcm build/user/builder.pcm build/builder.o:: builder.cxxm build/system/type_traits.pcm build/user/builder.pcm:: build/system/type_traits.pcm build/%.o:: -> $(call autodir) -> $(CXX) $(CXXFLAGS) -c $(call object_build) -> @mv $(patsubst build/%,%,$@) $@ + $(call autodir) + $(CXX) $(CXXFLAGS) -c $(call object_build) + @mv $(patsubst build/%,%,$@) $@ build/user/%.pcm:: %.cxxm -> $(call autodir) -> $(CXX) $(CXXFLAGS) --precompile -o $@ $(call object_build) + $(call autodir) + $(CXX) $(CXXFLAGS) --precompile -o $@ $(call object_build) build/system/%.pcm: -> $(call autodir) -> $(CXX) $(CXXFLAGS) -xc++-system-header --precompile $(patsubst build/system/%.pcm,%,$@) -o $@ $(SYSTEMHEADER_FLAGS) + $(call autodir) + $(CXX) $(CXXFLAGS) -xc++-system-header --precompile $(patsubst build/system/%.pcm,%,$@) -o $@ $(SYSTEMHEADER_FLAGS) .PHONY: clean run clean: -> @-rm -rf ./build + @-rm -rf ./build bear: clean -> bear --append -- $(MAKE) + bear --append -- $(MAKE) run: $(.DEFAULT_GOAL) -> @./$(.DEFAULT_GOAL) + @./$(.DEFAULT_GOAL) From 1a2bea5d2b35ca38fd87a28204d8d00cd0c4ce6a Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Mon, 28 Apr 2025 19:15:29 +0800 Subject: [PATCH 05/12] add zeditor support --- .zed/settings.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .zed/settings.json diff --git a/.zed/settings.json b/.zed/settings.json new file mode 100644 index 0000000..f2bd701 --- /dev/null +++ b/.zed/settings.json @@ -0,0 +1,5 @@ +{ + "file_types": { + "C++": ["cxxm"] + } +} From d81f487b03d23ce3a9c1a693da9cf2699f64347c Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Mon, 28 Apr 2025 19:15:29 +0800 Subject: [PATCH 06/12] fix: recipe `bear` isn't phony recipe --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 141602b..80b5272 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ build/system/%.pcm: $(call autodir) $(CXX) $(CXXFLAGS) -xc++-system-header --precompile $(patsubst build/system/%.pcm,%,$@) -o $@ $(SYSTEMHEADER_FLAGS) -.PHONY: clean run +.PHONY: clean run bear clean: @-rm -rf ./build bear: clean From 88b32bd3f05a1f178fe9b4367c3db52df868504d Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Wed, 16 Apr 2025 13:09:54 +0800 Subject: [PATCH 07/12] init cpp repo --- .clang-format | 4 +++ .clangd | 28 +++++++++++++++ .envrc | 3 ++ .gitignore | 4 +++ Makefile | 26 ++++++++++++++ flake.lock | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 60 ++++++++++++++++++++++++++++++++ main.cxx | 54 +++++++++++++++++++++++++++++ 8 files changed, 273 insertions(+) create mode 100644 .clang-format create mode 100644 .clangd create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 main.cxx diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..06e3c51 --- /dev/null +++ b/.clang-format @@ -0,0 +1,4 @@ +--- +Language: Cpp +BasedOnStyle: Google +... diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..d839602 --- /dev/null +++ b/.clangd @@ -0,0 +1,28 @@ +CompileFlags: + Add: + - -Xclang + - -fno-validate-pch + - -fretain-comments-from-system-headers + Compiler: clang++ +Index: + Background: true +Diagnostics: + ClangTidy: + Add: + - modernize-* + - performance-* + - cppcoreguidelines-* + ClangTidyChecks: true + Options: + UseChecks: + - "*" + WarningAsErrors: cppcoreguidelines-* +InlayHints: + Enabled: true + ParameterNames: true + DeducedTypes: true + Designators: true +Completion: + AllScopes: true +SemanticTokens: + Enabled: true diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..5bfe4a2 --- /dev/null +++ b/.envrc @@ -0,0 +1,3 @@ +watch_file ./flake.* + +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f3bea30 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.direnv/ +/build/ +/.cache/ +/compile_commands.json diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dfd2d4e --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +.RECIPEPREFIX = > +.DEFAULT_GOAL = build/main.out +CXX = clang++ +CXXFLAGS += -std=c++26 -Wno-experimental-header-units +LDFLAGS += -std=c++26 +SYSTEMHEADER_FLAGS = -Wno-deprecated-builtins -Wno-pragma-system-header-outside-header -Wno-keyword-compat +build_module_path = $(let first rest,$1,$(if $(rest),-fmodule-file=$(first) $(call build_module_path,$(rest)),-fmodule-file=$(first))) + +build/main.out: build/main.o +> $(CXX) $(LDFLAGS) $^ -o $@ + +build/main.o: main.cxx build/print.pcm build/type_traits.pcm +> $(CXX) $(CXXFLAGS) -c $(filter-out %.pcm,$^) $(call build_module_path,$(filter %.pcm,$^)) +> @mv $(patsubst build/%,%,$@) $@ + +build/%.pcm: +> $(CXX) $(CXXFLAGS) -xc++-system-header --precompile $(patsubst build/%.pcm,%,$@) -o $@ $(SYSTEMHEADER_FLAGS) + +.PHONY: clean run +clean: +> @-rm -rf ./build/* +bear: clean +> bear -- $(MAKE) + +run: $(.DEFAULT_GOAL) +> @./$(.DEFAULT_GOAL) diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..e975658 --- /dev/null +++ b/flake.lock @@ -0,0 +1,94 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "id": "flake-utils", + "type": "indirect" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1745377448, + "narHash": "sha256-jhZDfXVKdD7TSEGgzFJQvEEZ2K65UMiqW5YJ2aIqxMA=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "507b63021ada5fee621b6ca371c4fca9ca46f52c", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-cmake": { + "locked": { + "lastModified": 1745570837, + "narHash": "sha256-8MwKbng4lvA5wgBbOx0+VohonuMnUd62xJ6gE4tdXdU=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "ceecbb5d1184da5866a824b7accbb4a8c07a73eb", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "pull/394444/head", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-gcc15": { + "locked": { + "lastModified": 1745644951, + "narHash": "sha256-Rqnc3VvR2T6vJ2JwXhX6h9zwSYjWITk3YdOUKcCM284=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "281c0c16c9c6e2e44e78334f55bbd43dc51c0d70", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "pull/401948/head", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "nixpkgs-cmake": "nixpkgs-cmake", + "nixpkgs-gcc15": "nixpkgs-gcc15" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..07dfee9 --- /dev/null +++ b/flake.nix @@ -0,0 +1,60 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + nixpkgs-gcc15.url = "github:NixOS/nixpkgs/?ref=pull/401948/head"; + nixpkgs-cmake.url = "github:NixOS/nixpkgs/?ref=pull/394444/head"; + }; + outputs = { + nixpkgs-cmake, + nixpkgs-gcc15, + nixpkgs, + flake-utils, + ... + }: + flake-utils.lib.eachDefaultSystem ( + system: let + inherit + (import nixpkgs-cmake { + inherit system; + config = { + cudaSupport = true; + }; + }) + cmake + ; + inherit + (import nixpkgs-gcc15 { + inherit system; + config = { + cudaSupport = true; + }; + }) + gcc15 + gcc15Stdenv + ; + pkgs = import nixpkgs { + inherit system; + config = { + cudaSupport = true; + }; + }; + in { + devShells.default = + pkgs.mkShell.override + { + stdenv = pkgs.llvmPackages_20.libcxxStdenv; + # stdenv = gcc15Stdenv; + } + { + packages = with pkgs; [ + llvmPackages_20.clang-tools + gnumake + llvmPackages_20.clang + bear + # cmake + # gcc15 + ]; + }; + } + ); +} diff --git a/main.cxx b/main.cxx new file mode 100644 index 0000000..358ccec --- /dev/null +++ b/main.cxx @@ -0,0 +1,54 @@ +import ; +import ; + +template +struct Builder; +template +struct BuildMixin {}; + +template <> +struct BuildMixin { + auto build(this auto&& self) { return self.p1 + self.p2; } +}; + +template +struct Param1Mixin {}; + +template <> +struct Param1Mixin { + auto fill_param1(this auto&& self) { + self.p1 = 1; + return Builder::param2_v>( + self); + } +}; + +template +struct Param2Mixin {}; + +template <> +struct Param2Mixin { + auto fill_param2(this auto&& self) { + self.p2 = 1; + return Builder::param1_v, true>( + self); + } +}; + +template +struct Builder : BuildMixin, + Param1Mixin, + Param2Mixin { + const constinit static auto param1_v = param1; + const constinit static auto param2_v = param2; + int p1; + int p2; + Builder() : p1(0), p2(0) {} + Builder(auto&& other) : p1(other.p1), p2(other.p2) {} +}; + +auto main() -> int { + Builder builder; + auto ret = builder.fill_param1().fill_param2().build(); + std::println("{}", ret); +} From 0534572646dceeb3949d8037c57e1a1916f8257a Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Mon, 28 Apr 2025 13:58:27 +0800 Subject: [PATCH 08/12] add requires for function --- main.cxx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/main.cxx b/main.cxx index 358ccec..540e1f8 100644 --- a/main.cxx +++ b/main.cxx @@ -8,7 +8,11 @@ struct BuildMixin {}; template <> struct BuildMixin { - auto build(this auto&& self) { return self.p1 + self.p2; } + auto build(this auto&& self) + requires requires { self.p1 + self.p2; } + { + return self.p1 + self.p2; + } }; template @@ -16,7 +20,9 @@ struct Param1Mixin {}; template <> struct Param1Mixin { - auto fill_param1(this auto&& self) { + auto fill_param1(this auto&& self) + requires requires { self.p1; } + { self.p1 = 1; return Builder::param2_v>( self); @@ -28,7 +34,9 @@ struct Param2Mixin {}; template <> struct Param2Mixin { - auto fill_param2(this auto&& self) { + auto fill_param2(this auto&& self) + requires requires { self.p2; } + { self.p2 = 1; return Builder::param1_v, true>( self); From e0a242e12b40576992657de89b1ee9de4e1b3f74 Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Mon, 28 Apr 2025 13:58:27 +0800 Subject: [PATCH 09/12] use divided module --- .clangd | 10 +------- .gitignore | 1 - .helix/languages.toml | 28 ++++++++++++++++++++++ Makefile | 32 ++++++++++++++++++------- builder.cxxm | 56 +++++++++++++++++++++++++++++++++++++++++++ compile_commands.json | 29 ++++++++++++++++++++++ main.cxx | 56 +------------------------------------------ 7 files changed, 138 insertions(+), 74 deletions(-) create mode 100644 .helix/languages.toml create mode 100644 builder.cxxm create mode 100644 compile_commands.json diff --git a/.clangd b/.clangd index d839602..9729b9e 100644 --- a/.clangd +++ b/.clangd @@ -1,22 +1,16 @@ CompileFlags: Add: + - -std=c++26 - -Xclang - -fno-validate-pch - -fretain-comments-from-system-headers Compiler: clang++ -Index: - Background: true Diagnostics: ClangTidy: Add: - modernize-* - performance-* - cppcoreguidelines-* - ClangTidyChecks: true - Options: - UseChecks: - - "*" - WarningAsErrors: cppcoreguidelines-* InlayHints: Enabled: true ParameterNames: true @@ -24,5 +18,3 @@ InlayHints: Designators: true Completion: AllScopes: true -SemanticTokens: - Enabled: true diff --git a/.gitignore b/.gitignore index f3bea30..d00e3a1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ /.direnv/ /build/ /.cache/ -/compile_commands.json diff --git a/.helix/languages.toml b/.helix/languages.toml new file mode 100644 index 0000000..a85688c --- /dev/null +++ b/.helix/languages.toml @@ -0,0 +1,28 @@ +[[language]] +name = "cpp" +file-types = [ + "cc", + "hh", + "c++", + "cpp", + "hpp", + "h", + "ipp", + "tpp", + "cxx", + "hxx", + "ixx", + "txx", + "ino", + "C", + "H", + "cu", + "cuh", + "cppm", + "cxxm", + "h++", + "ii", + "inl", + { glob = ".hpp.in" }, + { glob = ".h.in" }, +] diff --git a/Makefile b/Makefile index dfd2d4e..4cd9558 100644 --- a/Makefile +++ b/Makefile @@ -1,26 +1,40 @@ .RECIPEPREFIX = > .DEFAULT_GOAL = build/main.out CXX = clang++ -CXXFLAGS += -std=c++26 -Wno-experimental-header-units +CXXFLAGS += -std=c++26 -Wno-experimental-header-units -Wno-unused-command-line-argument LDFLAGS += -std=c++26 SYSTEMHEADER_FLAGS = -Wno-deprecated-builtins -Wno-pragma-system-header-outside-header -Wno-keyword-compat -build_module_path = $(let first rest,$1,$(if $(rest),-fmodule-file=$(first) $(call build_module_path,$(rest)),-fmodule-file=$(first))) +module_pair = $(if $(findstring user,$1),$(patsubst build/user/%.pcm,%=,$1))$1 +build_module_path = $(if $1,$(let first rest,$1,$(if $(rest),-fmodule-file=$(call module_pair,$(first)) $(call build_module_path,$(rest)),-fmodule-file=$(call module_pair,$(first))))) +object_build = $(filter-out %.pcm,$^) $(call build_module_path,$(filter %.pcm,$^)) +autodir = @mkdir -p $(@D) -build/main.out: build/main.o +build/main.out: build/main.o build/builder.o +> $(call autodir) > $(CXX) $(LDFLAGS) $^ -o $@ -build/main.o: main.cxx build/print.pcm build/type_traits.pcm -> $(CXX) $(CXXFLAGS) -c $(filter-out %.pcm,$^) $(call build_module_path,$(filter %.pcm,$^)) +build/main.o:: main.cxx build/system/print.pcm build/user/builder.pcm +build/builder.o:: builder.cxxm build/system/type_traits.pcm +build/user/builder.pcm:: build/system/type_traits.pcm + +build/%.o:: +> $(call autodir) +> $(CXX) $(CXXFLAGS) -c $(call object_build) > @mv $(patsubst build/%,%,$@) $@ -build/%.pcm: -> $(CXX) $(CXXFLAGS) -xc++-system-header --precompile $(patsubst build/%.pcm,%,$@) -o $@ $(SYSTEMHEADER_FLAGS) +build/user/%.pcm:: %.cxxm +> $(call autodir) +> $(CXX) $(CXXFLAGS) --precompile -o $@ $(call object_build) + +build/system/%.pcm: +> $(call autodir) +> $(CXX) $(CXXFLAGS) -xc++-system-header --precompile $(patsubst build/system/%.pcm,%,$@) -o $@ $(SYSTEMHEADER_FLAGS) .PHONY: clean run clean: -> @-rm -rf ./build/* +> @-rm -rf ./build bear: clean -> bear -- $(MAKE) +> bear --append -- $(MAKE) run: $(.DEFAULT_GOAL) > @./$(.DEFAULT_GOAL) diff --git a/builder.cxxm b/builder.cxxm new file mode 100644 index 0000000..7d91a94 --- /dev/null +++ b/builder.cxxm @@ -0,0 +1,56 @@ +export module builder; +import ; + +export template +struct Builder; +template +struct BuildMixin {}; + +template <> +struct BuildMixin { + auto build(this auto&& self) + requires requires { self.p1 + self.p2; } + { + return self.p1 + self.p2; + } +}; + +template +struct Param1Mixin {}; + +template <> +struct Param1Mixin { + auto fill_param1(this auto&& self) + requires requires { self.p1; } + { + self.p1 = 1; + return Builder::param2_v>( + self); + } +}; + +template +struct Param2Mixin {}; + +template <> +struct Param2Mixin { + auto fill_param2(this auto&& self) + requires requires { self.p2; } + { + self.p2 = 1; + return Builder::param1_v, true>( + self); + } +}; + +template +struct Builder : BuildMixin, + Param1Mixin, + Param2Mixin { + const constinit static auto param1_v = param1; + const constinit static auto param2_v = param2; + int p1; + int p2; + Builder() : p1(0), p2(0) {} + Builder(auto&& other) : p1(other.p1), p2(other.p2) {} +}; diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..f224f89 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1,29 @@ +[ + { + "arguments": [ + "/nix/store/k50vj1b6vpz84n88vk09gxsgcyqrg00m-clang-wrapper-20.1.3/bin/clang++", + "-std=c++26", + "-Wno-experimental-header-units", + "-Wno-unused-command-line-argument", + "-c", + "-fmodule-file=build/system/print.pcm", + "-fmodule-file=builder=build/user/builder.pcm", + "main.cxx" + ], + "directory": "/home/david/code/cxx", + "file": "/home/david/code/cxx/main.cxx" + }, + { + "arguments": [ + "/nix/store/k50vj1b6vpz84n88vk09gxsgcyqrg00m-clang-wrapper-20.1.3/bin/clang++", + "-std=c++26", + "-Wno-experimental-header-units", + "-Wno-unused-command-line-argument", + "-c", + "-fmodule-file=build/system/type_traits.pcm", + "builder.cxxm" + ], + "directory": "/home/david/code/cxx", + "file": "/home/david/code/cxx/builder.cxxm" + } +] diff --git a/main.cxx b/main.cxx index 540e1f8..b87f7c4 100644 --- a/main.cxx +++ b/main.cxx @@ -1,59 +1,5 @@ import ; -import ; - -template -struct Builder; -template -struct BuildMixin {}; - -template <> -struct BuildMixin { - auto build(this auto&& self) - requires requires { self.p1 + self.p2; } - { - return self.p1 + self.p2; - } -}; - -template -struct Param1Mixin {}; - -template <> -struct Param1Mixin { - auto fill_param1(this auto&& self) - requires requires { self.p1; } - { - self.p1 = 1; - return Builder::param2_v>( - self); - } -}; - -template -struct Param2Mixin {}; - -template <> -struct Param2Mixin { - auto fill_param2(this auto&& self) - requires requires { self.p2; } - { - self.p2 = 1; - return Builder::param1_v, true>( - self); - } -}; - -template -struct Builder : BuildMixin, - Param1Mixin, - Param2Mixin { - const constinit static auto param1_v = param1; - const constinit static auto param2_v = param2; - int p1; - int p2; - Builder() : p1(0), p2(0) {} - Builder(auto&& other) : p1(other.p1), p2(other.p2) {} -}; +import builder; auto main() -> int { Builder builder; From bbc9936aba909430b7653805ea90f1a0e3751629 Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Mon, 28 Apr 2025 18:55:59 +0800 Subject: [PATCH 10/12] use as makefile recipeprefix for more editor support --- Makefile | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 4cd9558..141602b 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,3 @@ -.RECIPEPREFIX = > .DEFAULT_GOAL = build/main.out CXX = clang++ CXXFLAGS += -std=c++26 -Wno-experimental-header-units -Wno-unused-command-line-argument @@ -10,31 +9,31 @@ object_build = $(filter-out %.pcm,$^) $(call build_module_path,$(filter %.pcm,$^ autodir = @mkdir -p $(@D) build/main.out: build/main.o build/builder.o -> $(call autodir) -> $(CXX) $(LDFLAGS) $^ -o $@ + $(call autodir) + $(CXX) $(LDFLAGS) $^ -o $@ build/main.o:: main.cxx build/system/print.pcm build/user/builder.pcm build/builder.o:: builder.cxxm build/system/type_traits.pcm build/user/builder.pcm:: build/system/type_traits.pcm build/%.o:: -> $(call autodir) -> $(CXX) $(CXXFLAGS) -c $(call object_build) -> @mv $(patsubst build/%,%,$@) $@ + $(call autodir) + $(CXX) $(CXXFLAGS) -c $(call object_build) + @mv $(patsubst build/%,%,$@) $@ build/user/%.pcm:: %.cxxm -> $(call autodir) -> $(CXX) $(CXXFLAGS) --precompile -o $@ $(call object_build) + $(call autodir) + $(CXX) $(CXXFLAGS) --precompile -o $@ $(call object_build) build/system/%.pcm: -> $(call autodir) -> $(CXX) $(CXXFLAGS) -xc++-system-header --precompile $(patsubst build/system/%.pcm,%,$@) -o $@ $(SYSTEMHEADER_FLAGS) + $(call autodir) + $(CXX) $(CXXFLAGS) -xc++-system-header --precompile $(patsubst build/system/%.pcm,%,$@) -o $@ $(SYSTEMHEADER_FLAGS) .PHONY: clean run clean: -> @-rm -rf ./build + @-rm -rf ./build bear: clean -> bear --append -- $(MAKE) + bear --append -- $(MAKE) run: $(.DEFAULT_GOAL) -> @./$(.DEFAULT_GOAL) + @./$(.DEFAULT_GOAL) From 0a15b379bb35f567af601fbd2b7cc3402508a80d Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Mon, 28 Apr 2025 19:15:29 +0800 Subject: [PATCH 11/12] add zeditor support --- .zed/settings.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .zed/settings.json diff --git a/.zed/settings.json b/.zed/settings.json new file mode 100644 index 0000000..f2bd701 --- /dev/null +++ b/.zed/settings.json @@ -0,0 +1,5 @@ +{ + "file_types": { + "C++": ["cxxm"] + } +} From fbd06884b66ad51b431dcc91e6466433175db1fb Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Mon, 28 Apr 2025 19:15:29 +0800 Subject: [PATCH 12/12] fix: recipe `bear` isn't phony recipe --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 141602b..80b5272 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ build/system/%.pcm: $(call autodir) $(CXX) $(CXXFLAGS) -xc++-system-header --precompile $(patsubst build/system/%.pcm,%,$@) -o $@ $(SYSTEMHEADER_FLAGS) -.PHONY: clean run +.PHONY: clean run bear clean: @-rm -rf ./build bear: clean