Overhaul makefile, improve y/n logic

This commit is contained in:
Tyler Beckman 2024-09-19 21:43:10 -06:00
parent 120cba49eb
commit f85955134c
Signed by: Ty
GPG key ID: 2813440C772555A4
2 changed files with 51 additions and 7 deletions

View file

@ -1,24 +1,68 @@
TARGET = L1B TARGET = L1B
SRC_FILES = main.cpp SRC_FILES = main.cpp
# NO EDITS BELOW THIS LINE # Tyler's custom makefile extensions
.DEFAULT_GOAL := all # Necessary so `make` doesn't run the "pack" target, as it is declared before "all"
.PHONY: pack clean-run c run
## Adds only the necessary files for build into a .tar.gz file, named appropriately
ARCHIVED_FILES = Makefile $(SRC_FILES) $(SRC_FILES:.cpp=.h) $(SRC_FILES:.cpp=.hpp)
pack:
tar --ignore-failed-read -czvf $(TARGET).tar.gz $(shell echo $(ARCHIVED_FILES) | xargs ls -d 2>/dev/null)
## Runs the pack target and then attempts to build & run the program to make sure it functions correctly
pack-test: pack
$(eval TMP := $(shell mktemp -d))
tar -xvzf $(TARGET).tar.gz --directory $(TMP)
make -C $(TMP)
$(TMP)/$(TARGET)
rm -rf $(TMP)
## An extension of the clean command that is shorter to type and removes a potential .tar.gz file
c: clean
$(DEL) -f $(TARGET).tar.gz
## Simply builds and then executes the program
run: all
./$(TARGET)
# NO EDITS NEEDED BELOW THIS LINE
CXX = g++ CXX = g++
CXXFLAGS = -O2
CXXFLAGS_DEBUG = -g
CXXFLAGS_WARN = -Wall -Wextra -Wunreachable-code -Wshadow -Wpedantic
CPPVERSION = -std=c++17
OBJECTS = $(SRC_FILES:.cpp=.o) OBJECTS = $(SRC_FILES:.cpp=.o)
ifeq ($(shell echo "Windows"), "Windows") ifeq ($(shell echo "Windows"), "Windows")
TARGET := $(TARGET).exe TARGET := $(TARGET).exe
DEL = del DEL = del
Q =
else else
DEL = rm -f DEL = rm -f
Q = "
endif endif
all: $(TARGET) all: $(TARGET)
$(TARGET): $(OBJECTS) $(TARGET): $(OBJECTS)
$(CXX) -std=c++17 -o $@ $^ $(CXX) -o $@ $^
%.o: %.cpp .cpp.o:
$(CXX) -std=c++17 -o $@ -c $< $(CXX) $(CXXFLAGS) $(CPPVERSION) $(CXXFLAGS_DEBUG) $(CXXFLAGS_WARN) -o $@ -c $<
clean: clean:
$(DEL) $(TARGET) $(OBJECTS) $(DEL) -f $(TARGET) $(OBJECTS) Makefile.bak
depend:
@sed -i.bak '/^# DEPENDENCIES/,$$d' Makefile
@$(DEL) sed*
@echo $(Q)# DEPENDENCIES$(Q) >> Makefile
@$(CXX) -MM $(SRC_FILES) >> Makefile
.PHONY: all clean depend
# DEPENDENCIES
main.o: main.cpp

View file

@ -60,11 +60,11 @@ int main(void) {
std::cout << "This number is in the fourth quartile" << std::endl; std::cout << "This number is in the fourth quartile" << std::endl;
} }
char runAgainInput = 'N'; char runAgainInput;
std::cout << "Do you want another random value? (Y/N) "; std::cout << "Do you want another random value? (Y/N) ";
std::cin >> runAgainInput; std::cin >> runAgainInput;
if (runAgainInput == 'N') { if (runAgainInput != 'Y' && runAgainInput != 'y') {
std::cout << "Have a nice day!" << std::endl; std::cout << "Have a nice day!" << std::endl;
break; break;
} }