Skip to content

Commit

Permalink
Merge pull request #1 from moerastrid/cmake-template
Browse files Browse the repository at this point in the history
feat: ✨ add cmake example
  • Loading branch information
moerastrid authored Jul 3, 2023
2 parents 9c3a7bf + 87279e5 commit 404eaf4
Show file tree
Hide file tree
Showing 9 changed files with 833 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/build.yml
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
17 changes: 17 additions & 0 deletions .gitignore
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
31 changes: 31 additions & 0 deletions CMakeLists.txt
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()
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions include/hello-world.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <string>

std::string hello_world();
6 changes: 6 additions & 0 deletions src/hello-world.cpp
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!";
}
9 changes: 9 additions & 0 deletions src/main.cpp
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;
}
42 changes: 42 additions & 0 deletions test/CMakeLists.txt
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()
8 changes: 8 additions & 0 deletions test/test.cpp
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!");
}

0 comments on commit 404eaf4

Please sign in to comment.