From 775fd02ef24727639399d2664561d9c550cdafa8 Mon Sep 17 00:00:00 2001
From: Bernie Innocenti <bernie@codewiz.org>
Date: Sun, 2 Oct 2022 19:14:14 -0700
Subject: [PATCH] Update to gcc 12.2 and clang 15.0.0

---
 .gitignore        |  3 +++
 Makefile          | 61 ++++++++++++++++++++++++++++++++++++-----------
 compile_flags.txt |  6 +++++
 helloworld.cc     | 11 +++++----
 main.cc           | 16 +++++++------
 5 files changed, 72 insertions(+), 25 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 compile_flags.txt

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..cef70c1
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+build
+gcm.cache
+hellomodules
diff --git a/Makefile b/Makefile
index 764d97e..faff626 100644
--- a/Makefile
+++ b/Makefile
@@ -1,25 +1,58 @@
-#CXXFLAGS = -fmodules-ts -std=c++20
-#CXX = g++
+BUILD_DIR := build
+PREBUILT_DIR := $(BUILD_DIR)/prebuilt
+DIRS := $(BUILD_DIR) $(PREBUILT_DIR)
+
+#USE_CLANG := YES
+
+ifdef USE_CLANG
+
+CXX := clang++
+CXXFLAGS := \
+	-std=c++2b -stdlib=libc++ \
+	-fimplicit-modules -fimplicit-module-maps \
+	-fprebuilt-module-path=$(PREBUILT_DIR) \
+	-fmodules-cache-path=$(PREBUILT_DIR)
+#	-fmodule-map-file=/usr/include/c++/v1/module.modulemap
+#	-fbuiltin-module-map
+
+MODFLAGS := -Xclang -emit-module-interface
+
+else # COMPILER != clang
+
+CXX := g++
+CXXFLAGS := -std=c++2b -fmodules-ts
+MODFLAGS :=
+
+# libstdc++ 12.2 does not bundle pre-built modules:
+# g++ -std=c++2b -fmodules-ts -fmodule-header=system -x c++-system-header string iostream
+
+endif
 
-CXX = clang++
-CXXFLAGS = -std=c++20 -fimplicit-modules -fprebuilt-module-path=. -fmodules-cache-path=foo
 
 APP = hellomodules
-OBJS = main.o helloworld.o
-MODS = helloworld.pcm
+SRCS = main.cc helloworld.cc
+MODS = helloworld
 
-all: $(APP)
+OBJS = $(patsubst %.cc, build/%.o, $(SRCS))
+PCMS = $(patsubst %, $(PREBUILT_DIR)/%.pcm, $(MODS))
+
+all: $(DIRS) $(APP)
 
 clean:
 	rm -f $(APP)
 	rm -f $(OBJS)
-	rm -f $(MODS)
+	rm -f $(PCMS)
+	rm -rf $(PREBUILT_DIR)
+	rm -rf gcm.cache
+
+$(DIRS):
+	mkdir -p $(DIRS)
 
-$(APP): $(OBJS)
-	$(CXX) $(CXXFLAGS) -o $@ $?
+$(APP): $(OBJS) $(PCMS)
+	$(CXX) $(CXXFLAGS) -o $@ $(OBJS)
 
-main.o: main.cc $(MODS)
-helloworld.o: helloworld.cc
+$(BUILD_DIR)/%.o: %.cc $(PCMS)
+	$(CXX) $(CXXFLAGS) -o $@ -c $<
 
-helloworld.pcm: helloworld.cc
-	$(CXX) $(CXXFLAGS) -Xclang -emit-module-interface -c helloworld.cc -o helloworld.pcm
+$(PREBUILT_DIR)/%.pcm: %.cc
+	$(CXX) $(MODFLAGS) $(CXXFLAGS) -o $@ -c $<
diff --git a/compile_flags.txt b/compile_flags.txt
new file mode 100644
index 0000000..82dcf06
--- /dev/null
+++ b/compile_flags.txt
@@ -0,0 +1,6 @@
+-std=c++2b
+-stdlib=libc++
+-fimplicit-modules
+-fimplicit-module-maps
+-fprebuilt-module-path=build/prebuilt
+-fmodules-cache-path=build/prebuilt
diff --git a/helloworld.cc b/helloworld.cc
index c2075f0..0e4312d 100644
--- a/helloworld.cc
+++ b/helloworld.cc
@@ -1,12 +1,15 @@
 module;
 
-#include <iostream>
-
 export module helloworld;
 
+import <string>;
+import <iostream>;
+
 export class HelloWorld {
 public:
-    void hello() {
-        std::cout << "hello\n";
+    std::string hello() {
+        std::cout << "hello, ";
+        std::string s = "world!";
+        return s;
     }
 };
diff --git a/main.cc b/main.cc
index 9221a35..03bce82 100644
--- a/main.cc
+++ b/main.cc
@@ -1,13 +1,15 @@
-//#include <string>
-
 import helloworld;
-//import <string>;
 
-int main() {
-    //std::string s = "foo";
+#ifdef __clang__
+    import std;
+#else  // not yet in GCC 12.2 :-(
+    import <string>;
+    import <iostream>;
+#endif
 
+int main() {
     helloworld:HelloWorld h;
-    h.hello();
-
+    std::string s = h.hello();
+    std::cout << s << '\n';
     return 0;
 }
-- 
2.34.1