Skip to content

Commit

Permalink
Refactorizare CMake 2024 (#23)
Browse files Browse the repository at this point in the history
* Copiază fișierele auxiliare în directorul de build

* MSVC: compilare mai rapidă (?), codificare UTF-8, scoate telemetria (?)

* Readme: cerințe de nivel înalt, instrucțiuni de compilare

* Script pentru CMake

* Update la versiuni mai noi pe macOS
  • Loading branch information
mcmarius authored Aug 2, 2024
1 parent be6bd16 commit 8dc55cc
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 38 deletions.
24 changes: 11 additions & 13 deletions .github/actions/configure-cmake/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,22 @@ runs:
if: runner.os == 'Windows' && matrix.cxx == 'g++'
run: |
PATH="$(pwd)/gcc/mingw64/bin:${PATH}"
# because GH Actions does not properly prepend to $PATH and still chooses older gcc
CMAKE_GENERATOR=${{ matrix.cmake_generator }} cmake -S ${GITHUB_WORKSPACE} -B ${{ env.BUILD_DIR }} \
-DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} \
-DCMAKE_INSTALL_PREFIX=${GITHUB_WORKSPACE}/artifacts \
-DGITHUB_ACTIONS=${GITHUB_ACTIONS} \
-DWARNINGS_AS_ERRORS=${{ inputs.warnings_as_errors }} \
${{ inputs.custom_flags }}
# because GH Actions does not properly prepend to $PATH and still chooses VM gcc
export CMAKE_GENERATOR=${{ matrix.cmake_generator }}
bash ./scripts/cmake.sh configure -s ${GITHUB_WORKSPACE} -b ${{ env.BUILD_DIR }} \
-t ${{ env.BUILD_TYPE }} \
-i ${GITHUB_WORKSPACE}/artifacts \
-c "-DWARNINGS_AS_ERRORS=${{ inputs.warnings_as_errors }} -DGITHUB_ACTIONS=${GITHUB_ACTIONS} ${{ inputs.custom_flags }}"
- name: Configure CMake
shell: bash
if: ${{ !(runner.os == 'Windows' && matrix.cxx == 'g++') }}
run: |
CMAKE_GENERATOR=${{ matrix.cmake_generator }} cmake -S ${GITHUB_WORKSPACE} -B ${{ env.BUILD_DIR }} \
-DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} \
-DCMAKE_INSTALL_PREFIX=${GITHUB_WORKSPACE}/artifacts \
-DGITHUB_ACTIONS=${GITHUB_ACTIONS} \
-DWARNINGS_AS_ERRORS=${{ inputs.warnings_as_errors }} \
${{ inputs.custom_flags }}
export CMAKE_GENERATOR=${{ matrix.cmake_generator }}
bash ./scripts/cmake.sh configure -s ${GITHUB_WORKSPACE} -b ${{ env.BUILD_DIR }} \
-t ${{ env.BUILD_TYPE }} \
-i ${GITHUB_WORKSPACE}/artifacts \
-c "-DWARNINGS_AS_ERRORS=${{ inputs.warnings_as_errors }} -DGITHUB_ACTIONS=${GITHUB_ACTIONS} ${{ inputs.custom_flags }}"
# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
# https://github.com/GuillaumeFalourd/poc-github-actions/blob/main/.github/workflows/36-local-action.yml
8 changes: 4 additions & 4 deletions .github/actions/process-artifacts/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ runs:
run: |
if [[ "${BUILD_TYPE}" =~ "Deb" ]]; then
mkdir lib
cp /Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/lib/darwin/libclang_rt.asan_osx_dynamic.dylib lib
cp /Library/Developer/CommandLineTools/usr/lib/clang/15.0.0/lib/darwin/libclang_rt.asan_osx_dynamic.dylib lib
install_name_tool -change @rpath/libclang_rt.asan_osx_dynamic.dylib @executable_path/lib/libclang_rt.asan_osx_dynamic.dylib ${{ env.ZIP_NAME }}/${{ env.EXECUTABLE_NAME }}
mv lib ${{ env.ZIP_NAME }}/
fi
Expand All @@ -27,9 +27,9 @@ runs:
if: runner.os == 'macOS' && startsWith(matrix.cxx, 'g++')
run: |
mkdir lib
cp /usr/local/opt/gcc@12/lib/gcc/12/libstdc++.6.dylib /usr/local/opt/gcc@12/lib/gcc/12/libgcc_s.1.1.dylib lib
install_name_tool -change /usr/local/opt/gcc@12/lib/gcc/12/libstdc++.6.dylib @executable_path/lib/libstdc++.6.dylib ${{ env.ZIP_NAME }}/${{ env.EXECUTABLE_NAME }}
install_name_tool -change /usr/local/opt/gcc@12/lib/gcc/12/libgcc_s.1.1.dylib @executable_path/lib/libgcc_s.1.1.dylib ${{ env.ZIP_NAME }}/${{ env.EXECUTABLE_NAME }}
cp /opt/homebrew/lib/gcc/13/libstdc++.6.dylib /opt/homebrew/lib/gcc/13/libgcc_s.1.1.dylib lib
install_name_tool -change /usr/local/opt/gcc@13/lib/gcc/13/libstdc++.6.dylib @executable_path/lib/libstdc++.6.dylib ${{ env.ZIP_NAME }}/${{ env.EXECUTABLE_NAME }}
install_name_tool -change /usr/local/opt/gcc@13/lib/gcc/13/libgcc_s.1.1.dylib @executable_path/lib/libgcc_s.1.1.dylib ${{ env.ZIP_NAME }}/${{ env.EXECUTABLE_NAME }}
mv lib ${{ env.ZIP_NAME }}/
- name: Copy missing dlls
Expand Down
25 changes: 14 additions & 11 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ env:
# NOTE: update executable name if it is changed in CMakeLists.txt
EXECUTABLE_NAME: "oop"
INPUT_FILENAME: "tastatura.txt"
BIN_DIR: "bin"
BIN_DIR: "bin" # update in cmake/Options.cmake:6 if changing name here
BUILD_DIR: "build"
EXT_DIR: "ext"
GEN_DIR: "generated"
Expand Down Expand Up @@ -78,6 +78,7 @@ jobs:
env:
CC: ${{ matrix.c }}
CXX: ${{ matrix.cxx }}
VSCMD_SKIP_SENDTELEMETRY: 1

# NOTE: replace with another version if this one does not work
# For more versions, see https://winlibs.com or
Expand Down Expand Up @@ -124,19 +125,19 @@ jobs:
# This env runs valgrind
runs_valgrind: true

- os: macos-12
- os: macos-14
c: clang
cxx: clang++
name: "ASan: macOS 12 Apple Clang 14"
name: "ASan: macOS 14 Apple Clang 15"
cmake_flags: "-DUSE_ASAN=ON"
# cmake_generator:
# This env runs address sanitizers
runs_asan: true

- os: macos-12
c: gcc-12
cxx: g++-12
name: "macOS 12 GCC 12"
- os: macos-14
c: gcc-13
cxx: g++-13
name: "macOS 14 GCC 13"
# cmake_flags:
# cmake_generator: Ninja

Expand All @@ -145,7 +146,9 @@ jobs:
cxx: cl
name: "ASan: Windows 2022 MSVC 19.40"
cmake_flags: "-DUSE_ASAN=ON"
# cmake_generator: Ninja
# Ninja is not faster on MSVC because... MSVC
# cmake_generator: "Ninja"
# cmake_generator: "Ninja Multi-Config"
# This env runs address sanitizers
runs_asan: true

Expand Down Expand Up @@ -181,12 +184,12 @@ jobs:
warnings_as_errors: 'ON'

- name: Build
# Execute the build using N jobs (-jN)
run: cmake --build "${BUILD_DIR}" --config ${BUILD_TYPE} -j6
run: bash ./scripts/cmake.sh build

- name: Install
# Use CMake to "install" build artifacts (only interested in CMake registered targets) to our custom artifacts directory
run: cmake --install "${BUILD_DIR}" --config ${BUILD_TYPE} --prefix artifacts
run: |
bash ./scripts/cmake.sh install -i artifacts
- name: Move artifacts
run: |
Expand Down
16 changes: 9 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.26)

# NOTE: update executable name in .github/workflows/cmake.yml:25 when changing executable name in this file
# for now, the project name is used as the executable name
Expand Down Expand Up @@ -53,11 +53,13 @@ target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE generated/include)
###############################################################################

# copy binaries to "bin" folder; these are uploaded as artifacts on each release
# update name in .github/workflows/cmake.yml:27 when changing "bin" name here
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
# install(DIRECTORY some_dir1 some_dir2 DESTINATION bin)
# install(FILES some_file1.txt some_file2.md DESTINATION bin)
install(FILES tastatura.txt DESTINATION bin)
# DESTINATION_DIR is set as "bin" in cmake/Options.cmake:6
install(TARGETS ${PROJECT_NAME} DESTINATION ${DESTINATION_DIR})
if(APPLE)
install(FILES launcher.command DESTINATION bin)
install(FILES launcher.command DESTINATION ${DESTINATION_DIR})
endif()

include(cmake/CopyHelper.cmake)
copy_files(FILES tastatura.txt)
# copy_files(FILES tastatura.txt config.json DIRECTORY images sounds)
# copy_files(DIRECTORY images sounds)
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ O cerință nu se consideră îndeplinită dacă este realizată doar ca o serie
- [ ] funcții și atribute `static`
- [ ] STL
- [ ] cât mai multe `const`
- [ ] funcții *de nivel înalt*, de eliminat cât mai mulți getters/setters/funcții low-level
- [ ] la sfârșit: commit separat cu adăugarea unei noi clase derivate fără a modifica restul codului, **pe lângă cele 3 derivate deja adăugate** din aceeași ierarhie
- noua derivată nu poate fi una existentă care a fost ștearsă și adăugată din nou
- [ ] tag de `git`: de exemplu `v0.2`
Expand All @@ -71,6 +72,34 @@ O cerință nu se consideră îndeplinită dacă este realizată doar ca o serie
<!-- - [ ] o specializare pe funcție/clasă șablon -->
- [ ] tag de `git`: de exemplu `v0.3` sau `v1.0`

## Instrucțiuni de compilare

Proiectul este configurat cu CMake.

Instrucțiuni pentru terminal:

1. Pasul de configurare
```sh
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
```

La acest pas putem cere să generăm fișiere de proiect pentru diverse medii de lucru.


2. Pasul de compilare
```sh
cmake --build build --config Debug --parallel 6
```

Cu opțiunea `parallel` specificăm numărul de fișiere compilate în paralel.

3. Pasul de instalare (opțional)
```sh
cmake --install build --config Debug --prefix install_dir
```

Vezi și [`scripts/cmake.sh`](scripts/cmake.sh).

## Resurse

- adăugați trimiteri către resursele externe care v-au ajutat sau pe care le-ați folosit
2 changes: 1 addition & 1 deletion cmake/CompilerFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ endif()
# custom compiler flags
message("Compiler: ${CMAKE_CXX_COMPILER_ID} version ${CMAKE_CXX_COMPILER_VERSION}")
if(MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /permissive- /wd4244 /wd4267 /wd4996 /external:anglebrackets /external:W0)
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /permissive- /wd4244 /wd4267 /wd4996 /external:anglebrackets /external:W0 /utf-8 /MP)
else()
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -pedantic)
endif()
Expand Down
31 changes: 31 additions & 0 deletions cmake/CopyHelper.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# helper function to copy files to build directory and install directory without duplicating file names across commands
function(copy_files)
set(options OPTIONAL FAST)
set(oneValueArgs)
set(multiValueArgs FILES DIRECTORY)
cmake_parse_arguments(PARSE_ARGV 0 ARG "${options}" "${oneValueArgs}" "${multiValueArgs}")

# copy files to build dir
foreach(file ${ARG_FILES})
add_custom_command(
TARGET ${PROJECT_NAME} POST_BUILD
COMMENT "Copying ${file}..."
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/${file} $<TARGET_FILE_DIR:${PROJECT_NAME}>)
# ${CMAKE_CURRENT_BINARY_DIR})
endforeach()

# copy folders to build dir
foreach(dir ${ARG_DIRECTORY})
add_custom_command(
TARGET ${PROJECT_NAME} POST_BUILD
COMMENT "Copying directory ${dir}..."
COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different
${CMAKE_SOURCE_DIR}/${dir} $<TARGET_FILE_DIR:${PROJECT_NAME}>/${dir})
# ${CMAKE_CURRENT_BINARY_DIR}/${dir})
endforeach()

# copy files and folders to install dir
install(FILES ${ARG_FILES} DESTINATION ${DESTINATION_DIR})
install(DIRECTORY ${ARG_DIRECTORY} DESTINATION ${DESTINATION_DIR})
endfunction()
4 changes: 4 additions & 0 deletions cmake/Options.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
option(WARNINGS_AS_ERRORS "Treat warnings as errors" OFF)
option(USE_ASAN "Use Address Sanitizer" OFF)
option(USE_MSAN "Use Memory Sanitizer" OFF)
option(CMAKE_COLOR_DIAGNOSTICS "Enable color diagnostics" ON)

# update name in .github/workflows/cmake.yml:27 when changing "bin" name here
set(DESTINATION_DIR "bin")

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "${PROJECT_SOURCE_DIR}/install_dir" CACHE PATH "..." FORCE)
Expand Down
4 changes: 2 additions & 2 deletions scripts/build_cppcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ while getopts ":b:o:v:" opt; do
;;
v) CPPCHECK_VER="${OPTARG}"
;;
*) printf "Unknown option; available options: \n\
*) printf "Unknown option %s; available options: \n\
-b (build dir)\n\
-o (cmake opts)\n\
-v (cppcheck version)\n"
-v (cppcheck version)\n" "${opt}"
exit 1
;;
esac
Expand Down
126 changes: 126 additions & 0 deletions scripts/cmake.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/usr/bin/bash

DEFAULT_BUILD_DIR="build"
DEFAULT_BUILD_TYPE="Debug"
DEFAULT_INSTALL_DIR="install_dir"

configure() {
# cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
#
BUILD_DIR="${DEFAULT_BUILD_DIR}"
BUILD_TYPE="${DEFAULT_BUILD_TYPE}"
INSTALL_DIR="${DEFAULT_INSTALL_DIR}"
SOURCE_DIR="."
CMAKE_OPTS=()

while getopts ":b:c:g:i:s:t:" opt; do
case "${opt}" in
b) BUILD_DIR="${OPTARG}"
;;
c) IFS=" " read -r -a CMAKE_OPTS <<< "${OPTARG}"
;;
g) export CMAKE_GENERATOR="${OPTARG}"
;;
i) INSTALL_DIR="${OPTARG}"
;;
s) SOURCE_DIR="${OPTARG}"
;;
t) BUILD_TYPE="${OPTARG}"
;;
*) printf "Unknown option %s; available options: \n\
-b (build dir)\n\
-c (custom CMake options)\n\
-g (generator)\n\
-i (install dir prefix)\n\
-s (source dir)\n\
-t (build type)\n" "${opt}"
exit 1
;;
esac
done

cmake -S "${SOURCE_DIR}" \
-B "${BUILD_DIR}" \
-DCMAKE_BUILD_TYPE="${BUILD_TYPE}" \
-DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}" \
"${CMAKE_OPTS[@]}"
}

build() {
# cmake --build build --config Debug -j6
#
BUILD_DIR="${DEFAULT_BUILD_DIR}"
BUILD_TYPE="${DEFAULT_BUILD_TYPE}"
NPROC=6

while getopts ":b:h:j:t:" opt; do
case "${opt}" in
b) BUILD_DIR="${OPTARG}"
;;
j) NPROC="${OPTARG}"
;;
t) BUILD_TYPE="${OPTARG}"
;;
h) printf "Unknown option %s; available options: \n\
-b (build dir)\n\
-j (number of jobs for parallel build)\n\
-t (build type)\n" "${opt}"
exit 1
;;
*)
;;
esac
shift $((OPTIND-1))
done

cmake --build "${BUILD_DIR}" --config "${BUILD_TYPE}" -j "${NPROC}" "$@"
}

install() {
# cmake --install build --config Debug --prefix install_dir
#
BUILD_DIR="${DEFAULT_BUILD_DIR}"
BUILD_TYPE="${DEFAULT_BUILD_TYPE}"
INSTALL_DIR="${DEFAULT_INSTALL_DIR}"
while getopts ":b:i:t:" opt; do
case "${opt}" in
b) BUILD_DIR="${OPTARG}"
;;
i) INSTALL_DIR="${OPTARG}"
;;
t) BUILD_TYPE="${OPTARG}"
;;
*) printf "Unknown option %s; available options: \n\
-b (build dir)\n\
-i (install dir prefix)\n\
-t (build type)\n" "${opt}"
exit 1
;;
esac
shift $((OPTIND-1))
done

cmake --install "${BUILD_DIR}" --config "${BUILD_TYPE}" --prefix "${INSTALL_DIR}"
}


case "$1" in
configure)
shift
configure "$@"
;;
build)
shift
build "$@"
;;
install)
shift
install "$@"
;;
*) printf "Unknown option %s; available options: \n\
configure\n\
build\n\
install\n" "${opt}"
exit 1
esac

0 comments on commit 8dc55cc

Please sign in to comment.