Created
October 27, 2024 23:21
-
-
Save KRMisha/7f796201167780f7e5ae2217445836f2 to your computer and use it in GitHub Desktop.
How to set up tests in a Makefile (https://github.com/KRMisha/Makefile) using Catch2 with Conan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/.gitignore b/.gitignore | |
index 542fa54..2a5e664 100644 | |
--- a/.gitignore | |
+++ b/.gitignore | |
@@ -2,6 +2,9 @@ | |
.DS_Store | |
Thumbs.db | |
+# Python virtual environment for Conan | |
+.venv/ | |
+ | |
# Build output | |
/build/ | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[requires] | |
catch2/3.7.1 | |
[generators] | |
MakeDeps |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/foo.cpp | |
#include "foo.h" | |
int foo() | |
{ | |
return 5; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/foo.h | |
#pragma once | |
int foo(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/Makefile b/Makefile | |
index ae5c329..536142b 100644 | |
--- a/Makefile | |
+++ b/Makefile | |
@@ -26,7 +26,7 @@ TEST_SRCS := $(sort $(shell find $(TEST_DIR) -name '*.cpp' 2> /dev/null)) | |
# Includes | |
INCLUDE_DIR = | |
INCLUDES = $(addprefix -I,$(SRC_DIR) $(INCLUDE_DIR)) | |
-TEST_INCLUDES = -I$(TEST_DIR) | |
+TEST_INCLUDES = -I$(TEST_DIR) $(CONAN_INCLUDE_DIRS_CATCH2) | |
# C preprocessor settings | |
CPPFLAGS = $(INCLUDES) -MMD -MP | |
@@ -38,11 +38,11 @@ WARNINGS = -Wall -Wpedantic -Wextra | |
# Linker flags | |
LDFLAGS = | |
-TEST_LDFLAGS = | |
+TEST_LDFLAGS = $(CONAN_LIB_DIRS_CATCH2) | |
# Libraries to link | |
LDLIBS = | |
-TEST_LDLIBS = | |
+TEST_LDLIBS = $(CONAN_LIBS_CATCH2_CATCH2_WITH_MAIN) $(CONAN_LIBS_CATCH2__CATCH2) $(CONAN_SYSTEM_LIBS_CATCH2) | |
# Target OS detection | |
ifeq ($(OS),Windows_NT) # OS is a preexisting environment variable on Windows | |
@@ -103,9 +103,21 @@ else | |
CXXFLAGS += -O0 -g | |
endif | |
-# Object and bin directories | |
+# Object, bin, and Conan directories | |
OBJ_DIR := $(BUILD_DIR)/obj | |
BIN_DIR := $(BUILD_DIR)/bin | |
+CONAN_DIR := $(BUILD_DIR)/conan | |
+ | |
+# Conan | |
+ifneq ($(MAKECMDGOALS),clean) | |
+ CONAN_DEFINE_FLAG = -D | |
+ CONAN_INCLUDE_DIR_FLAG = -isystem | |
+ CONAN_LIB_DIR_FLAG = -L | |
+ CONAN_BIN_DIR_FLAG = -L | |
+ CONAN_LIB_FLAG = -l | |
+ CONAN_SYSTEM_LIB_FLAG = -l | |
+ include $(CONAN_DIR)/conandeps.mk | |
+endif | |
# Object files | |
MAIN_SRC = $(SRC_DIR)/main.cpp | |
@@ -134,6 +146,12 @@ FILES := $(shell find $(SRC_DIR) $(TEST_DIR) $(INCLUDE_DIR) -name '*.cpp' -o -na | |
.PHONY: all | |
all: $(BIN_DIR)/$(EXEC) $(if $(TEST_SRCS),$(BIN_DIR)/$(TEST_EXEC)) | |
+# Generate Conan dependencies | |
+$(CONAN_DIR)/conandeps.mk: conanfile.txt | |
+ @echo "Generating: $@" | |
+ @mkdir -p $(@D) | |
+ @conan install . --output-folder=$(CONAN_DIR) --build=missing | |
+ | |
# Build executable | |
$(BIN_DIR)/$(EXEC): $(MAIN_OBJ) $(SRC_OBJS_WITHOUT_MAIN) | |
@echo "Building executable: $@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tests/test_foo.cpp | |
#include <catch2/catch_test_macros.hpp> | |
#include "foo.h" | |
TEST_CASE("foo() returns 5", "[foo]") | |
{ | |
REQUIRE(foo() == 5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment