Skip to content

Commit

Permalink
TOOLS: Add some CMake scripts
Browse files Browse the repository at this point in the history
Add some CMake scripts:
- All, Build, Run, Test, SetDebug, SetRelease
  • Loading branch information
slali87 committed Oct 17, 2024
1 parent ff648d2 commit a4273d6
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 37 deletions.
12 changes: 3 additions & 9 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,13 @@ jobs:
- name: Build
run: |
cd ${{ github.event.repository.name }}
cmake --preset=conan-release
cmake --build --preset=conan-release
cmake --preset=conan-debug
cmake --build --preset=conan-debug
cmake -P cmake/Build.cmake
- name: Run the application and the tests
run: |
cd ${{ github.event.repository.name }}
./build/Release/src/app/CppSampleProject
./build/Release/test/CppSampleProjectTest
cmake -P cmake/Run.cmake
cmake -P cmake/Test.cmake
ctest --test-dir ./build/Release/test
./build/Debug/src/app/CppSampleProject
./build/Debug/test/CppSampleProjectTest
ctest --test-dir ./build/Debug/test
- name: Run Valgrind
if: runner.os == 'Linux'
run: |
Expand Down
27 changes: 9 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,19 @@ cmake -P cmake/Setup.cmake

### **Steps of build:**
```
cmake --preset=conan-release
cmake --build --preset=conan-release
```
Same for debug:
```
cmake --preset=conan-debug
cmake --build --preset=conan-debug
cmake -P cmake/Build.cmake
```

### **Commands of run:**
```
./build/Release/src/app/CppSampleProject
./build/Release/test/CppSampleProjectTest
ctest --test-dir ./build/Release/test
cmake -P cmake/Run.cmake
cmake -P cmake/Test.cmake
```
The first command runs the application, the second runs the tests.
The third runs the tests by **ctest**.
Same for debug:
The **ctest** command also works:
```
./build/Debug/src/app/CppSampleProject
./build/Debug/test/CppSampleProjectTest
ctest --test-dir ./build/Debug/test
ctest --test-dir ./build/Release/test # For Release
ctest --test-dir ./build/Debug/test # For Debug
```

### **Delete the build directory:**
Expand All @@ -119,13 +110,13 @@ cmake -P cmake/Clean.cmake

### **Run all steps (including: deps, setup, config, build, run, test):**
```
TODO: ./run.sh all
cmake -P cmake/All.cmake
```

### **Commands to switch betwen Release and Debug mode:**
```
TODO: ./run.sh setRel
TODO: ./run.sh setDeb
cmake -P cmake/SetRelease.cmake
cmake -P cmake/SetDebug.cmake
```
The default mode is Release.

Expand Down
10 changes: 0 additions & 10 deletions ToDo.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
- **Add Cmake scripts to build, run and test**
.

- **Refactor Cmake scripts**
.

- **Implement all**
.

- **Implement debug, release**
.

---
- **Use 'ubuntu-latest' in CI again**
Use 'ubuntu-latest' in CI again when it will be 'ubuntu-24.04', Probably after October 30th , 2024
15 changes: 15 additions & 0 deletions cmake/All.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.28)


execute_process(COMMAND cmake -P cmake/Setup.cmake
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
execute_process(COMMAND cmake -P cmake/Build.cmake
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
execute_process(COMMAND cmake -P cmake/Run.cmake
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
execute_process(COMMAND cmake -P cmake/Test.cmake
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
19 changes: 19 additions & 0 deletions cmake/Build.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.28)

if(EXISTS "./build/BuildType/Release")
execute_process(COMMAND cmake --preset=conan-release
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
execute_process(COMMAND cmake --build --preset=conan-release
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
elseif(EXISTS "./build/BuildType/Debug")
execute_process(COMMAND cmake --preset=conan-debug
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
execute_process(COMMAND cmake --build --preset=conan-debug
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
else()
message(FATAL_ERROR "BuildType has not been found")
endif()
16 changes: 16 additions & 0 deletions cmake/Run.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.28)

get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
string(REPLACE " " "_" ProjectId ${ProjectId})

if(EXISTS "./build/BuildType/Release")
execute_process(COMMAND "./build/Release/src/app/${ProjectId}"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
elseif(EXISTS "./build/BuildType/Debug")
execute_process(COMMAND "./build/Debug/src/app/${ProjectId}"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
else()
message(FATAL_ERROR "BuildType has not been found")
endif()
11 changes: 11 additions & 0 deletions cmake/SetDebug.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.28)

if(NOT EXISTS "./build/BuildType/")
file(MAKE_DIRECTORY "./build/BuildType/")
endif()

if(EXISTS "./build/BuildType/Release")
file(RENAME "./build/BuildType/Release" "./build/BuildType/Debug")
elseif(NOT EXISTS "./build/BuildType/Debug")
file(TOUCH "./build/BuildType/Debug")
endif()
11 changes: 11 additions & 0 deletions cmake/SetRelease.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.28)

if(NOT EXISTS "./build/BuildType/")
file(MAKE_DIRECTORY "./build/BuildType/")
endif()

if(EXISTS "./build/BuildType/Debug")
file(RENAME "./build/BuildType/Debug" "./build/BuildType/Release")
elseif(NOT EXISTS "./build/BuildType/Release")
file(TOUCH "./build/BuildType/Release")
endif()
6 changes: 6 additions & 0 deletions cmake/Setup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ execute_process(COMMAND conan install . -s "&:build_type=TestCov" -pr:a=${Profil
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)

if(NOT EXISTS "./build/BuildType/")
execute_process(COMMAND cmake -P cmake/SetRelease.cmake
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
endif()

# set git hooks
execute_process(COMMAND git config core.hooksPath ${CMAKE_SOURCE_DIR}/tools/git/hooks
COMMAND_ERROR_IS_FATAL ANY)
Expand Down
16 changes: 16 additions & 0 deletions cmake/Test.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.28)

get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
string(REPLACE " " "_" ProjectId ${ProjectId})

if(EXISTS "./build/BuildType/Release")
execute_process(COMMAND "./build/Release/test/${ProjectId}Test"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
elseif(EXISTS "./build/BuildType/Debug")
execute_process(COMMAND "./build/Debug/test/${ProjectId}Test"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
else()
message(FATAL_ERROR "BuildType has not been found")
endif()

0 comments on commit a4273d6

Please sign in to comment.