-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from moerastrid/cmake-template
feat: ✨ add cmake example
- Loading branch information
Showing
9 changed files
with
833 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
name: build | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
build: | ||
# strategy to run on mac and linux | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest, macos-latest] | ||
runs-on: ${{ matrix.os }} | ||
timeout-minutes: 5 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Configure CMake | ||
# Configure CMake in a 'build' subdirectory. | ||
run: cmake -B ${{github.workspace}}/build | ||
- name: Build | ||
# Build your program with the given configuration | ||
run: cmake --build ${{github.workspace}}/build | ||
|
||
test: | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest, macos-latest] | ||
runs-on: ${{ matrix.os }} | ||
timeout-minutes: 5 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Configure CMake | ||
# Configure CMake in a 'build' subdirectory. | ||
run: cmake -B ${{github.workspace}}/build -DBUILD_TESTS=yes -DCMAKE_BUILD_TYPE=Debug | ||
- name: Build | ||
# Build your program with the given configuration | ||
run: cmake --build ${{github.workspace}}/build | ||
- name: Test | ||
run: ctest --output-on-failure --test-dir ${{github.workspace}}/build |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# CMake generated files # | ||
################### | ||
build | ||
|
||
# OS generated files # | ||
###################### | ||
.DS_Store | ||
.DS_Store? | ||
._* | ||
.Spotlight-V100 | ||
.Trashes | ||
ehthumbs.db | ||
Thumbs.db | ||
|
||
# VScode # | ||
########## | ||
.vscode |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
cmake_minimum_required(VERSION 3.21.0) | ||
|
||
project(webserv) | ||
|
||
# Sourcefiles with main function (excluded from testing) | ||
set (SRCS_MAIN | ||
src/main.cpp | ||
) | ||
|
||
set (SRCS | ||
src/hello-world.cpp | ||
) | ||
set (EXEC_NAME webserv) | ||
|
||
# Compiler settings | ||
set (CMAKE_CXX_STANDARD 20) | ||
set (CMAKE_CXX_STANDARD_REQUIRED ON) | ||
set (CMAKE_CXX_FLAGS "-Wall -Werror -Wextra") | ||
set (CMAKE_CXX_FLAGS_DEBUG "-g -fsanitize=address") #cmake -DCMAKE_BUILD_TYPE=DEBUG to add debug flags | ||
set (CMAKE_EXPORT_COMPILE_COMMANDS ON) #vscode include path checking | ||
|
||
# Make executable with output name specified under EXEC_NAME and sources specified under SRCS and SRCS_MAIN | ||
add_executable(${EXEC_NAME} ${SRCS} ${SRCS_MAIN}) | ||
target_include_directories(${EXEC_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) | ||
|
||
#-------------------- TESTING ------------------------------- | ||
|
||
if ((PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME AND BUILD_TESTS) OR WEBSERV_BUILD_TESTS) | ||
add_subdirectory(test) | ||
enable_testing() | ||
endif() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
std::string hello_world(); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include "hello-world.hpp" | ||
|
||
std::string hello_world() | ||
{ | ||
return "Hello, World!"; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include <iostream> | ||
|
||
#include "hello-world.hpp" | ||
|
||
int main() | ||
{ | ||
std::cout << hello_world() << std::endl; | ||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
set(TEST_EXECUTABLE_NAME minirt_unit_tests) | ||
|
||
set(TEST_SRCS | ||
test.cpp | ||
) | ||
|
||
# Download GoogleTest | ||
# -------------------------------------------------- | ||
include(GoogleTest) | ||
include(FetchContent) | ||
|
||
cmake_policy(SET CMP0135 NEW) | ||
FetchContent_Declare( | ||
googletest | ||
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip | ||
) | ||
FetchContent_MakeAvailable(googletest) | ||
|
||
list(TRANSFORM SRCS PREPEND ../ OUTPUT_VARIABLE PROJECT_SRCS) | ||
|
||
# Tests | ||
# -------------------------------------------------- | ||
add_executable( | ||
${TEST_EXECUTABLE_NAME} | ||
${PROJECT_SRCS} | ||
${TEST_SRCS} | ||
) | ||
|
||
# Add include directories from the main project | ||
target_include_directories(${TEST_EXECUTABLE_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include) | ||
|
||
# Link gtest to the test executable | ||
target_link_libraries( | ||
${TEST_EXECUTABLE_NAME} | ||
gtest_main | ||
) | ||
|
||
# Add tests to CTest | ||
# Set working directory to the the testing folder so that the test can find their test files | ||
# -------------------------------------------------- | ||
gtest_discover_tests(${TEST_EXECUTABLE_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} TEST_PREFIX "miniRT/") | ||
enable_testing() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include "hello-world.hpp" | ||
|
||
TEST(hello_world, basic) | ||
{ | ||
EXPECT_EQ(hello_world(), "Hello, World!"); | ||
} |