Skip to content
Open
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
90 changes: 68 additions & 22 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,47 +1,83 @@



cmake_minimum_required(VERSION 3.16)
project(ItemManagerProject LANGUAGES CXX)

# -----------------------------------
# Compiler and Build Configuration
# Compiler and build configuration
# -----------------------------------

# Only run this logic if this is the top-level project
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
set(SUPPORTED_CXX_STANDARDS 20 17 14)

# Ensure a dummy source exists for try_compile checks
set(_dummy_dir "${PROJECT_SOURCE_DIR}/cmake")
set(_dummy_src "${_dummy_dir}/dummy.cpp")
if(NOT EXISTS "${_dummy_src}")
file(MAKE_DIRECTORY "${_dummy_dir}")
file(WRITE "${_dummy_src}" "#include <iostream>\nint main(){std::cout<<\"ok\";return 0;}\n")
endif()

foreach(standard IN LISTS SUPPORTED_CXX_STANDARDS)
set(CMAKE_CXX_STANDARD ${standard})
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

try_compile(COMPILER_SUPPORTS_STD
${CMAKE_BINARY_DIR}/std_check
${PROJECT_SOURCE_DIR}/cmake/dummy.cpp
${_dummy_src}
CMAKE_FLAGS "-DCMAKE_CXX_STANDARD=${standard}"
)

if(COMPILER_SUPPORTS_STD)
message(STATUS " Using C++${standard}")
message(STATUS "Using C++${standard}")
break()
endif()
endforeach()

if(NOT COMPILER_SUPPORTS_STD)
message(FATAL_ERROR "No supported C++ standard found! Please use GCC 11 or higher.")
message(FATAL_ERROR "No supported C++ standard found. Please use GCC 11+, Clang 13+, or MSVC 2019+.")
endif()
endif()

# Default build type for single-config generators (respect multi-config like VS/Xcode)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose build type (Debug, Release, RelWithDebInfo, MinSizeRel)" FORCE)
endif()

# -----------------------------------
# Build Settings
# Compiler detection and flags (portable)
# -----------------------------------
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "-g -O1 -Wall -Wextra -pedantic")
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")

if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
message(STATUS "Detected Clang compiler")
add_compile_options(-g -O1 -Wall -Wextra -Wpedantic -Wno-unused-parameter)

elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
message(STATUS "Detected AppleClang compiler (macOS)")
add_compile_options(-g -O1 -Wall -Wextra -Wpedantic -Wno-unused-parameter)

elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
message(STATUS "Detected GCC compiler")
add_compile_options(-g -O1 -Wall -Wextra -pedantic)

elseif(MSVC)
message(STATUS "Detected MSVC compiler")
add_compile_options(/W4 /EHsc /Zc:__cplusplus /std:c++20)
else()
message(WARNING "Unknown compiler: ${CMAKE_CXX_COMPILER_ID}")
endif()

# Optional: allow static libstdc++/libgcc on Linux only (off by default)
option(ENABLE_STATIC_STDLIB "Link libstdc++/libgcc statically on Linux (may affect portability)" OFF)
if(ENABLE_STATIC_STDLIB AND CMAKE_SYSTEM_NAME STREQUAL "Linux" AND (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
add_link_options(-static-libstdc++ -static-libgcc)
endif()

# -----------------------------------
# External Dependencies
# External dependencies
# -----------------------------------

include(FetchContent)
Expand All @@ -54,16 +90,17 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(json)

# GoogleTest
# GoogleTest (do not use system-installed to keep cross-platform consistency)
# GoogleTest (use release tarball instead of git clone)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.1
URL https://github.com/google/googletest/archive/refs/tags/release-1.12.1.zip
)
FetchContent_MakeAvailable(googletest)


# -----------------------------------
# ItemManager Library
# ItemManager library
# -----------------------------------

add_library(ItemManagerLib STATIC
Expand All @@ -79,10 +116,13 @@ target_include_directories(ItemManagerLib
${json_SOURCE_DIR}/single_include
)

target_link_libraries(ItemManagerLib PUBLIC nlohmann_json::nlohmann_json)
target_link_libraries(ItemManagerLib
PUBLIC
nlohmann_json::nlohmann_json
)

# -----------------------------------
# Test Executable
# Test executable
# -----------------------------------

add_executable(TestApp
Expand All @@ -96,14 +136,20 @@ target_include_directories(TestApp
${json_SOURCE_DIR}/single_include
)

target_link_libraries(TestApp PRIVATE
ItemManagerLib
gtest
gtest_main
target_link_libraries(TestApp
PRIVATE
ItemManagerLib
gtest
gtest_main
)

# Color output for MSVC GoogleTest (optional quality-of-life)
if(MSVC)
target_compile_definitions(TestApp PRIVATE GTEST_COLOR=1)
endif()

# -----------------------------------
# Enable Test Discovery
# Enable test discovery
# -----------------------------------

include(GoogleTest)
Expand All @@ -113,6 +159,6 @@ gtest_discover_tests(TestApp
)

# -----------------------------------
# Output Summary
# Output summary
# -----------------------------------
message(STATUS "Project Configured Successfully.")
message(STATUS "Project configured successfully.")
26 changes: 21 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,37 @@ Flow from Item Registration to Export
## Installation

### Windows:
Option 1:
. Make sure you have CMake and the Visual Studio 2022(C++) installed before you run the following commands.</br>
```bash
cd Smart_Store
cmake -B build-msvc -G "Visual Studio 17 2022" && cmake --build
build-msvc --config Debug
cd build-msvc
cd Debug
./TestApp
```
Option 2:
. Make sure you have CMake and the GCC compiler installed before you run the following commands.</br>
. If still having issues, set up WSL (Windows Subsystem for Linux) or use a Linux environment </br>
and run the following commands.


### Linux / macOS:
. Make sure you have CMake and the GCC compiler installed before you run the following commands.</br></br>
. Make sure you have CMake and the GCC and Clang compilers installed before you run the following commands.</br></br>


```bash
"GCC"
git clone https://github.com/gem870/Smart_Store.git
cd Smart_Store
mkdir build
cd build
cmake ..
cmake --build .
cmake -B build-gcc -DCMAKE_CXX_COMPILER=g++ && cmake --build build-gcc
```
```bash
"Clang"
git clone https://github.com/gem870/Smart_Store.git
cd Smart_Store
cmake -B build-clang -DCMAKE_CXX_COMPILER=clang++ && cmake --build build-clang
```
# To output on the console.
### For PowerShell use:
Expand Down
Loading