-CXXFLAGS = -fmodules-ts -std=c++20
-CC = g++
+BUILD_DIR := build
+PREBUILT_DIR := $(BUILD_DIR)/prebuilt
+DIRS := $(BUILD_DIR) $(PREBUILT_DIR)
-main: main.o helloworld.o
+#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
+
+
+APP = hellomodules
+SRCS = main.cc helloworld.cc
+MODS = helloworld
+
+OBJS = $(patsubst %.cc, build/%.o, $(SRCS))
+PCMS = $(patsubst %, $(PREBUILT_DIR)/%.pcm, $(MODS))
+
+all: $(DIRS) $(APP)
clean:
- rm main *.o
+ rm -f $(APP)
+ rm -f $(OBJS)
+ rm -f $(PCMS)
+ rm -rf $(PREBUILT_DIR)
+ rm -rf gcm.cache
+
+$(DIRS):
+ mkdir -p $(DIRS)
+
+$(APP): $(OBJS) $(PCMS)
+ $(CXX) $(CXXFLAGS) -o $@ $(OBJS)
+
+$(BUILD_DIR)/%.o: %.cc $(PCMS)
+ $(CXX) $(CXXFLAGS) -o $@ -c $<
-main.o: main.cc
-helloworld.o: helloworld.cc
+$(PREBUILT_DIR)/%.pcm: %.cc
+ $(CXX) $(MODFLAGS) $(CXXFLAGS) -o $@ -c $<