Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cxx): introduce gtest #20

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions template/cxx/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

"cxx_build_tool": ["xmake", "cmake"],
"cxx_standard_version": "20",
"cxx_standard_required": true,
"cxx_extensions_required": false,
"cxx_project_type": ["binary", "library"],
"cxx_share_enabled": ["SHARED", "STATIC"],


"__gh_slug": "{{ cookiecutter.github_username }}/{{ cookiecutter.project_slug }}",
"__prompts__": {
Expand Down
18 changes: 12 additions & 6 deletions template/cxx/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@

cxx_build_tool = "{{cookiecutter.cxx_build_tool}}"

cmake_file = "CMakeLists.txt"
xmake_file = "xmake.lua"
cmake_root = "CMakeLists.txt"
cmake_test = "tests/CMakeLists.txt"
xmake_root = "xmake.lua"
xmake_test = "tests/xmake.lua"

if cxx_build_tool == "cmake":
if os.path.exists(xmake_file):
os.remove(xmake_file)
if os.path.exists(xmake_root):
os.remove(xmake_root)
if os.path.exists(xmake_test):
os.remove(xmake_test)
elif cxx_build_tool == "xmake":
if os.path.exists(cmake_file):
os.remove(cmake_file)
if os.path.exists(cmake_root):
os.remove(cmake_root)
if os.path.exists(cmake_test):
os.remove(cmake_test)
else:
raise ValueError(f"Unknown cxx_build_tool: {cxx_build_tool}")

Expand Down
57 changes: 24 additions & 33 deletions template/cxx/{{cookiecutter.project_slug}}/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
cmake_minimum_required(VERSION 3.28)
project({{cookiecutter.project_slug}} LANGUAGES CXX)
project({{cookiecutter.project_slug}}
VERSION 0.0.1
DESCRIPTION "{{cookiecutter.project_desc}}"
HOMEPAGE_URL "https://github.com/{{cookiecutter.__gh_slug}}"
LANGUAGES CXX
)

# C++ Standard settings
set(CMAKE_CXX_STANDARD {{cookiecutter.cxx_standard_version}})
set(CMAKE_CXX_STANDARD_REQUIRED {{cookiecutter.cxx_standard_required}})
set(CMAKE_CXX_EXTENSIONS {{cookiecutter.cxx_extensions_required}}) # Enable or disable compiler-specific features
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) # Enable or disable compiler-specific features

# Enable compile commands for clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Specify architecture for macOS
if(APPLE)
set(CMAKE_OSX_ARCHITECTURES "arm64")
endif()

# Set output directory for binaries
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
Expand All @@ -24,36 +34,17 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY})

# Source files
file(GLOB SOURCES CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/src/*.h ${CMAKE_SOURCE_DIR}/src/*.cpp)

# Speed up the compile by ccache
if(NOT MSVC)
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
message(STATUS "Found CCache: ${CCACHE_PROGRAM}")
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_PROGRAM})
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ${CCACHE_PROGRAM})
endif()
endif()

# Specify architecture for macOS
if(APPLE)
set(CMAKE_OSX_ARCHITECTURES "arm64")
endif()
# Collect source files
file(GLOB_RECURSE SOURCES ${CMAKE_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE HEADERS ${CMAKE_SOURCE_DIR}/include/*.h)

{% if cookiecutter.cxx_project_type == "binary" -%}
# Binary project setup
add_executable({{cookiecutter.project_slug}} ${SOURCES})
# Create library target
add_library({{cookiecutter.package_name}}_lib ${SOURCES} ${HEADERS})
target_include_directories({{cookiecutter.package_name}}_lib PUBLIC ${CMAKE_SOURCE_DIR}/include)

{% if cookiecutter.cxx_share_enabled == "STATIC" -%}
# Additional settings for static binary on non-macOS platforms
if(NOT APPLE)
target_link_libraries({{cookiecutter.project_slug}} PUBLIC "-static")
endif()
{% endif %}
# Create executable target
add_executable({{cookiecutter.project_slug}} ${CMAKE_SOURCE_DIR}/src/main.cpp)
target_link_libraries({{cookiecutter.project_slug}} PRIVATE {{cookiecutter.package_name}}_lib)

{%- else -%}
# Library project setup
add_library({{cookiecutter.project_slug}} {{cookiecutter.cxx_share_enabled}} ${SOURCES})
{%- endif %}
# Add tests subdirectory
add_subdirectory(tests)
4 changes: 0 additions & 4 deletions template/cxx/{{cookiecutter.project_slug}}/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ COPY .. .
RUN cmake -B build && cmake --build build --target {{cookiecutter.project_slug}} --config Release --parallel 8

# DEPLOYING
{% if cookiecutter.cxx_project_type == "binary" and not cookiecutter.cxx_share_enabled -%}
FROM scratch
{%- else -%}
FROM rockylinux:9-minimal
{%- endif %}

COPY --from=builder /app/build/{{cookiecutter.project_slug}} /{{cookiecutter.project_slug}}

Expand Down
6 changes: 4 additions & 2 deletions template/cxx/{{cookiecutter.project_slug}}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ build:
@xmake
{%- endif %}

{% if cookiecutter.cxx_project_type == "binary" -%}
# run
run:
@./build/bin/$(APP_NAME)
{%- endif %}

# test
test:
@./build/bin/$(APP_NAME)-tests

# build image
image:
Expand Down
3 changes: 3 additions & 0 deletions template/cxx/{{cookiecutter.project_slug}}/include/hi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void say_hi();
4 changes: 4 additions & 0 deletions template/cxx/{{cookiecutter.project_slug}}/include/my_lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

int add(int a, int b);
int sub(int a, int b);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "library.h"
#include "hi.h"

#include <iostream>

void hello() {
void say_hi() {
std::cout << "Hello, World!" << std::endl;
}
6 changes: 0 additions & 6 deletions template/cxx/{{cookiecutter.project_slug}}/src/library.h

This file was deleted.

10 changes: 7 additions & 3 deletions template/cxx/{{cookiecutter.project_slug}}/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include <iostream>
#include "hi.h"
#include "my_lib.h"

int main() {
std::cout << "Hello, World!" << std::endl;
int main()
{
say_hi();
std::cout << "add(1, 2) = " << add(1, 2) << std::endl;
std::cout << "sub(1, 2) = " << sub(1, 2) << std::endl;
return 0;
}

11 changes: 11 additions & 0 deletions template/cxx/{{cookiecutter.project_slug}}/src/my_lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "my_lib.h"

int add(const int a, const int b)
{
return a + b;
}

int sub(const int a, const int b)
{
return a - b;
}
29 changes: 29 additions & 0 deletions template/cxx/{{cookiecutter.project_slug}}/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Enable testing
enable_testing()

# Fetch and make available Google Test
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.15.2.tar.gz
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# Collect test source files
file(GLOB_RECURSE TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)

# Create test executable
add_executable({{cookiecutter.project_slug}}-tests ${TEST_SOURCES})

# Link the test executable with Google Test
target_link_libraries({{cookiecutter.project_slug}}-tests
PRIVATE
{{cookiecutter.package_name}}_lib
GTest::gtest_main
)

# Discover tests
include(GoogleTest)
gtest_discover_tests({{cookiecutter.project_slug}}-tests)
31 changes: 31 additions & 0 deletions template/cxx/{{cookiecutter.project_slug}}/tests/test_my_lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "gtest/gtest.h"
#include "my_lib.h"

int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

TEST(AddTest, PositiveNumbers)
{
EXPECT_EQ(add(1, 2), 3);
EXPECT_EQ(add(10, 5), 15);
}

TEST(AddTest, NegativeNumbers)
{
EXPECT_EQ(add(-1, -2), -3);
EXPECT_EQ(add(-5, 5), 0);
}

TEST(SubTest, PositiveNumbers)
{
EXPECT_EQ(sub(10, 5), 5);
EXPECT_EQ(sub(5, 10), -5);
}

TEST(SubTest, NegativeNumbers)
{
EXPECT_EQ(sub(-10, -5), -5);
EXPECT_EQ(sub(-5, -10), 5);
}
9 changes: 9 additions & 0 deletions template/cxx/{{cookiecutter.project_slug}}/tests/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Add Google Test package
add_requires("gtest")

-- Add test target
target("{{cookiecutter.project_slug}}-tests")
set_kind("binary")
add_files("*.cpp")
add_deps("{{cookiecutter.package_name}}_lib")
add_packages("gtest")
33 changes: 14 additions & 19 deletions template/cxx/{{cookiecutter.project_slug}}/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@
set_project("{{cookiecutter.project_slug}}")
set_languages("c++{{cookiecutter.cxx_standard_version}}")

{% if cookiecutter.cxx_project_type == "binary" -%}
-- Binary project setup
-- Include directories
add_includedirs("include")

-- Add targets
target("{{cookiecutter.package_name}}_lib")
set_targetdir("build/lib")
set_kind("static")
add_files("src/*.cpp")
add_headerfiles("include/*.h")

target("{{cookiecutter.project_slug}}")
set_targetdir("build/bin")
set_kind("binary")
add_files("src/*.cpp")
add_headerfiles("src/*.h")
add_files("src/main.cpp")
add_deps("{{cookiecutter.package_name}}_lib")

{%- if cookiecutter.cxx_share_enabled == "STATIC" -%}
-- Static link settings for non-macOS platforms
if not is_plat("macosx") then
add_ldflags("-static", {force = true})
end
{%- endif %}

{%- else -%}
-- Library project setup
target("{{cookiecutter.project_slug}}")
set_targetdir("build/lib")
set_kind("{{cookiecutter.cxx_share_enabled | lower}}")
add_files("src/*.cpp")
add_headerfiles("src/*.h")
{%- endif %}
-- Add tests
includes("tests")