2## EPITECH PROJECT, 2024
8# The variables in charge of tracking the files for the program
11SRC = $(SRC_DIR)/main.py \
13# The variables concerning unit testing
17TEST_FILES = $(TEST_DIR)/test_main.py \
18 $(TEST_DIR)/test_constants.py \
20# Coverage report location
22COVERAGE_DIR = ./coverage_data
28# Level precision of the build process
32# The name of the python environement that will be used
36# The location in which the dependencies used during the build process
39BUILD_LOCATION = ./build/
41# The location of the final built binary
43BINARY_LOCATION = ./dist/
45# The desired destinations for the binary
47BINARY_DESTINATION_ONE = ./
49BINARY_DESTINATION_TWO = ../../
51# The python binary that is available on the system
53CC = $(shell command -v python3.9 2>/dev/null || \
54 command -v python3 2>/dev/null || \
55 command -v python 2>/dev/null || \
56 command -v py 2>/dev/null \
59# Break the code if no python instance was found
62 $(error Python interpreter not found. Please install Python.)
65# Search for pip version
66MY_PIP_BIN = pip3 # Please update it manually {the automation check seems to break the environment}
68# Break the code if no python instance was found
71 $(error Pip interpreter not found. Please install a pip interpreter.)
74# Silence the outputs we wish not to listen to
78C_BACKGROUND = \033[48;5;16m
79C_RED = \033[38;5;9m$(C_BACKGROUND)
80C_PINK = \033[38;5;206m$(C_BACKGROUND)
81C_CYAN = \033[38;5;87m$(C_BACKGROUND)
82C_BLUE = \033[38;5;45m$(C_BACKGROUND)
83C_WHITE = \033[38;5;15m$(C_BACKGROUND)
84C_GREEN = \033[38;5;46m$(C_BACKGROUND)
85C_RESET = \033[0m$(C_BACKGROUND)
86C_YELLOW = \033[38;5;226m$(C_BACKGROUND)
89all: install_dependencies build_binary update_binary_location clean
91my_all: create_environement all
93# Create the python environement
95 @echo -e "$(C_CYAN)Creating python environment$(C_RESET)"
96 $(SILENT) $(CC) -m venv $(ENV_NAME)
98# Install the python dependencies
100 $(SILENT) echo -e "$(C_CYAN)Upgrading pip$(C_RESET)" &&\
101 $(CC) -m pip install --upgrade pip && \
102 . $(ENV_NAME)/bin/activate && \
103 $(MY_PIP_BIN) install --upgrade pip && \
105 $(MY_PIP_BIN) list && \
106 echo -e "$(C_CYAN)Installing python dependencies$(C_RESET)" &&\
107 $(MY_PIP_BIN) install -r requirements.txt
109# Activate the python environement (
110# this is a shortcut to help you keep your
111# environement up to date and also avoid mistakes in the name or process
113activate_environement: install_dependencies
114 @echo -e "$(C_PINK)Activating environement '$(ENV_NAME)'$(C_RESET)"
115 $(SILENT) . ./$(ENV_NAME)/bin/activate && \
116 export PYTHONPATH=.:$$PYTHONPATH; \
117 echo "Environement activated" && \
120 echo "Environement deactivated" && \
121 echo "Exit code: $$STATUS" && \
124activate: activate_environement
126# Build the python code as a binary
128 @echo -e "$(C_BLUE)Building binary '$(C_YELLOW)$(NAME)$(C_BLUE)'$(C_RESET)"
129 $(SILENT) . ./$(ENV_NAME)/bin/activate && \
132 --workpath $(BUILD_LOCATION) --distpath $(BINARY_LOCATION) \
133 --onefile --noconfirm \
135 --log-level=$(LOG_LEVEL) \
137 @echo -ne "$(C_BLUE)Binary '$(C_YELLOW)$(NAME)$(C_BLUE)' "
138 @echo -e "$(C_GREEN)built$(C_RESET)"
140# Update the location of the binary so that it can be easily accessed
141update_binary_location:
142 @echo -e "$(C_PINK)Updating binary locations$(C_RESET)"
143 $(SILENT) cp -vf $(BINARY_LOCATION)$(NAME) $(BINARY_DESTINATION_ONE)
144 $(SILENT) cp -vf $(BINARY_LOCATION)$(NAME) $(BINARY_DESTINATION_TWO)
145 @echo -e "$(C_PINK)Binary locations $(C_GREEN)updated$(C_RESET)"
147# Clean the cache projects
150 @echo -e "$(C_YELLOW)Cleaning $(C_CYAN)cache and build data$(C_RESET)"
151# Removing the spec files that were used for the executable
152 $(SILENT) rm -vf *.spec
153# Removing the build and distribution folder of the executable
154 $(SILENT) rm -rvf $(BUILD_LOCATION)
155 $(SILENT) rm -rvf $(BINARY_LOCATION)
156# Removing python runtime cache
157 $(SILENT) find . -type d -name __pycache__ -exec rm -rvf {} +
158 @echo -e "$(C_GREEN)Cleaned $(C_CYAN)cache and build data$(C_RESET)"
160# Clean the binaries produced
162 @echo -ne "$(C_YELLOW)Cleaning $(C_CYAN)environement "
163 @echo -e "'$(C_RED)$(ENV_NAME)$(C_CYAN)'$(C_RESET)"
164 $(SILENT) rm -rf ./$(ENV_NAME)
165 @echo -ne "$(C_CYAN)Environement '$(C_RED)$(ENV_NAME)$(C_CYAN)' "
166 @echo -e "$(C_GREEN)cleaned$(C_RESET)"
168# Clean the coverage produced
170 @echo -e "$(C_YELLOW)Cleaning coverage$(C_RESET)"
171 $(SILENT) rm -rf $(COVERAGE_DIR)
172 $(SILENT) rm -rf .coverage
173 @echo -e "$(C_GREEN)Coverage cleaned$(C_RESET)"
175# Proceed to removing the python environement
178# Proceed to a full environement wipe (
179# ex: usefull when changing python version
181fclean: clean clean_coverage
182 $(SILENT) rm -vf $(BINARY_DESTINATION_ONE)$(NAME)
183 $(SILENT) rm -vf $(BINARY_DESTINATION_TWO)$(NAME)
185# Run the tests for the programs
186tests_run: install_dependencies
187 @echo -e "$(C_RED)Running unit tests$(C_RESET)"
188# Updating the path for python to find the imports
189 $(SILENT) export PYTHONPATH=.:$$PYTHONPATH; \
190 . ./$(ENV_NAME)/bin/activate && \
191 $(CC) -m unittest discover -s $(TEST_DIR)
192 @echo -e "$(C_RED)Unit tests $(C_GREEN)run$(C_RESET)"
194# Check the coverage for the programs
195coverage: install_dependencies
196 @echo -e "$(C_CYAN)Generating coverage report$(C_RESET)"
197 $(SILENT) mkdir -p $(COVERAGE_DIR)
198 $(SILENT) . ./$(ENV_NAME)/bin/activate && \
200 coverage run --data-file $(COVERAGE_DIR)/report.coverage \
201 -m unittest discover -s $(TEST_DIR) && \
203 coverage run --branch --data-file $(COVERAGE_DIR)/branch_report.coverage \
204 -m unittest discover -s $(TEST_DIR) && \
207 --data-file $(COVERAGE_DIR)/report.coverage > \
208 $(COVERAGE_DIR)/report.txt && \
211 --data-file $(COVERAGE_DIR)/branch_report.coverage > \
212 $(COVERAGE_DIR)/branch_report.txt && \
214 cat $(COVERAGE_DIR)/report.txt && \
215 cat $(COVERAGE_DIR)/branch_report.txt && \
217 coverage html --directory $(COVERAGE_DIR)/html_report \
218 --data-file=$(COVERAGE_DIR)/report.coverage && \
220 coverage html --directory $(COVERAGE_DIR)/branch_html_report \
221 --data-file=$(COVERAGE_DIR)/branch_report.coverage
222 @echo -e "$(C_CYAN)Coverage report $(C_GREEN)generated$(C_RESET)"
224# Create the debug versions for the program (no idea what to put in it)
228# Rule to re-build everything
232# Clean the slate, remove the environement and start anew
233my_re: fclean fclean_env my_all
235# Disable silent build
237 @echo -e "$(C_PINK)Silent mode is $(C_RED)inactive$(C_RESET)"
239 $(eval LOG_LEVEL=INFO)
241# This is a no operation function, it is used
242# by the parent makefile for silent builds
244 @echo -e "$(C_PINK)Silent mode is $(C_GREEN)active$(C_RESET)"
246 $(eval LOG_LEVEL=WARN)
250run: clean install_dependencies
251 $(SILENT) echo -e "$(C_CYAN) Activating environement $(C_RESET)" && \
252 . ./$(ENV_NAME)/bin/activate && \
253 echo -e "$(C_CYAN) Starting program $(C_RESET)" && \
254 $(CC) $(SRC_DIR)/main.py && \
256 echo -e "$(C_CYAN) Environement deactivated $(C_RESET)"
258# The .PHONY to to avoid functions being overridden
260.PHONY: all create_environement install_dependencies activate_environement \
261 activate build_binary update_binary_location clean clean_env \
262 clean_coverage fclean tests_run coverage debug re noop silent run \
263 my_all fclean_env my_re