Skip to content

Commit

Permalink
Merge pull request #265 from Z1Turn0/main
Browse files Browse the repository at this point in the history
Optimize compile time
  • Loading branch information
geihob authored Jan 31, 2024
2 parents 521e458 + e48903b commit aeb37b4
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 107 deletions.
60 changes: 60 additions & 0 deletions .github/actions/check-loser-homework/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: 'Check Loser Homework'
description: 'Use cmake to check loser homework'
inputs:
shell:
description: 'shell to use'
required: false
default: bash
build-path:
description: 'path to build'
required: false
default: 'build'
extra-gen-params:
description: 'extra cmake generation parameters'
required: false
default: ''
use-ninja:
description: 'use ninja to build'
required: false
default: true
runs:
using: "composite"
steps:
- uses: seanmiddleditch/gha-setup-ninja@master
if: inputs.use-ninja == 'true'
- uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- run: pip install -r requirements.txt
shell: ${{ inputs.shell }}

- name: Configure CMake
if: inputs.use-ninja == 'false'
run: cmake -B ${{ inputs.build-path }} ${{ inputs.extra-gen-params}}
shell: ${{ inputs.shell }}

- name: Build
if: inputs.use-ninja == 'false'
run: cmake --build ${{ inputs.build-path }} --target build_loser_homework
shell: ${{ inputs.shell }}

- name: Check run
if: inputs.use-ninja == 'false'
run: cmake --build ${{ inputs.build-path }} --target run_loser_homework
shell: ${{ inputs.shell }}

- name: Configure CMake with Ninja
if: inputs.use-ninja == 'true'
run: cmake -B ${{ inputs.build-path }} -G Ninja ${{ inputs.extra-gen-params}}
shell: ${{ inputs.shell }}

- name: Build with Ninja
if: inputs.use-ninja == 'true'
run: ninja -C ${{ inputs.build-path }} build_loser_homework
shell: ${{ inputs.shell }}

- name: Check run with Ninja
if: inputs.use-ninja == 'true'
run: ninja -C ${{ inputs.build-path }} run_loser_homework
shell: ${{ inputs.shell }}
57 changes: 18 additions & 39 deletions .github/workflows/check-all-loser-homework.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
branches: [ "main" ]
paths:
- '.github/workflows/**'
- '.github/actions/**'
- '**.cmake'
- 'CMakeLists.txt'
- 'src/群友提交/**'
Expand All @@ -16,6 +17,7 @@ on:
branches: [ "main" ]
paths:
- '.github/workflows/**'
- '.github/actions/**'
- '**.cmake'
- 'CMakeLists.txt'
- 'src/群友提交/**'
Expand All @@ -31,20 +33,12 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- run: pip install -r requirements.txt

- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DCMAKE_CXX_COMPILER=$cxx_compiler

- name: Build
run: cmake --build ${{github.workspace}}/build --target build_loser_homework

- name: Check run
run: cmake --build ${{github.workspace}}/build --target run_loser_homework
- name: Check loser homework
uses: ./.github/actions/check-loser-homework
with:
build-path: ${{github.workspace}}/build
extra-gen-params: "-DCMAKE_CXX_COMPILER=$cxx_compiler"

check-in-clang:
runs-on: ubuntu-latest
Expand All @@ -53,43 +47,28 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- run: pip install -r requirements.txt

- name: Install clang 17
run: |
wget https://apt.llvm.org/llvm.sh
chmod u+x llvm.sh
echo | sudo ./llvm.sh 17
- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DCMAKE_CXX_COMPILER=$cxx_compiler

- name: Build
run: cmake --build ${{github.workspace}}/build --target build_loser_homework

- name: Check run
run: cmake --build ${{github.workspace}}/build --target run_loser_homework
- name: Check loser homework
uses: ./.github/actions/check-loser-homework
with:
build-path: ${{github.workspace}}/build
extra-gen-params: "-DCMAKE_CXX_COMPILER=$cxx_compiler"

check-in-msvc:
runs-on: windows-2022

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- run: pip install -r requirements.txt

- name: Configure CMake
run: cmake -B ${{github.workspace}}/build

- name: Build
run: cmake --build ${{github.workspace}}/build --target build_loser_homework

- name: Check run
run: cmake --build ${{github.workspace}}/build --target run_loser_homework
- name: Check loser homework
uses: ./.github/actions/check-loser-homework
with:
build-path: ${{github.workspace}}/build
shell: pwsh
use-ninja: false
61 changes: 16 additions & 45 deletions .github/workflows/check-changed-loser-homework.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- run: pip install -r requirements.txt

- name: Only keep cpp changed files
if: needs.get-changed-files.outputs.any_changed == 'true'
Expand All @@ -79,17 +74,12 @@ jobs:
eval "$find_command"
find ${{ env.src_path }} -type f \( -name "*.cpp" -o -name "*.cxx" -o -name "*.cc" \)
- name: Configure CMake
if: needs.get-changed-files.outputs.any_changed == 'true'
run: cmake -B ${{github.workspace}}/build -DCMAKE_CXX_COMPILER=$cxx_compiler

- name: Build
if: needs.get-changed-files.outputs.any_changed == 'true'
run: cmake --build ${{github.workspace}}/build --target build_loser_homework

- name: Check run
- name: Check loser homework
if: needs.get-changed-files.outputs.any_changed == 'true'
run: cmake --build ${{github.workspace}}/build --target run_loser_homework
uses: ./.github/actions/check-loser-homework
with:
build-path: ${{github.workspace}}/build
extra-gen-params: "-DCMAKE_CXX_COMPILER=$cxx_compiler"

check-in-clang:
needs: get-changed-files
Expand All @@ -99,11 +89,6 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- run: pip install -r requirements.txt

- name: Only keep cpp changed files
if: needs.get-changed-files.outputs.any_changed == 'true'
Expand All @@ -128,29 +113,19 @@ jobs:
chmod u+x llvm.sh
echo | sudo ./llvm.sh 17
- name: Configure CMake
if: needs.get-changed-files.outputs.any_changed == 'true'
run: cmake -B ${{github.workspace}}/build -DCMAKE_CXX_COMPILER=$cxx_compiler

- name: Build
- name: Check loser homework
if: needs.get-changed-files.outputs.any_changed == 'true'
run: cmake --build ${{github.workspace}}/build --target build_loser_homework

- name: Check run
if: needs.get-changed-files.outputs.any_changed == 'true'
run: cmake --build ${{github.workspace}}/build --target run_loser_homework
uses: ./.github/actions/check-loser-homework
with:
build-path: ${{github.workspace}}/build
extra-gen-params: "-DCMAKE_CXX_COMPILER=$cxx_compiler"

check-in-msvc:
needs: get-changed-files
runs-on: windows-2022

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- run: pip install -r requirements.txt

- name: Only keep cpp changed files
if: needs.get-changed-files.outputs.any_changed == 'true'
Expand All @@ -174,14 +149,10 @@ jobs:
}
}
- name: Configure CMake
- name: Check loser homework
if: needs.get-changed-files.outputs.any_changed == 'true'
run: cmake -B ${{github.workspace}}/build

- name: Build
if: needs.get-changed-files.outputs.any_changed == 'true'
run: cmake --build ${{github.workspace}}/build --target build_loser_homework

- name: Check run
if: needs.get-changed-files.outputs.any_changed == 'true'
run: cmake --build ${{github.workspace}}/build --target run_loser_homework
uses: ./.github/actions/check-loser-homework
with:
build-path: ${{github.workspace}}/build
shell: pwsh
use-ninja: false
18 changes: 16 additions & 2 deletions src/群友提交/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,33 @@ include(CheckLoserHomeWork)
set(CMAKE_CXX_STANDARD 26)

if (MSVC)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "aa1f7df0-828a-4fcd-9afc-2dc80491aca7") #for cmake 3.27
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /permissive-")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /permissive-")
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
enable_msvc_build_stl_modules()

string(REGEX REPLACE "\/bin\/Host(x|X)(64|86)\/x(64|86)\/cl\.exe" "" MSVC_ROOT "${CMAKE_CXX_COMPILER}")
# std.ixx
configure_file("${MSVC_ROOT}/modules/std.ixx" "${PROJECT_BINARY_DIR}/std.ixx" COPYONLY)
# std.compat.ixx
configure_file("${MSVC_ROOT}/modules/std.compat.ixx" "${PROJECT_BINARY_DIR}/std.compat.ixx" COPYONLY)
endif ()

set(homework_dir ${CMAKE_CURRENT_SOURCE_DIR})
handle_homework(${homework_dir} targets_list run_targets_list)

if (MSVC)
foreach (target ${targets_list})
target_compile_options(${target} PRIVATE "/experimental:module")
get_target_property(use_modules ${target} use_modules)
if(use_modules)
target_compile_options(${target} PRIVATE "/experimental:module")
target_sources(${target}
PUBLIC
FILE_SET cxx_modules TYPE CXX_MODULES BASE_DIRS ${PROJECT_BINARY_DIR}
FILES ${PROJECT_BINARY_DIR}/std.ixx ${PROJECT_BINARY_DIR}/std.compat.ixx
)
endif(use_modules)
endforeach ()
endif ()

Expand Down
36 changes: 15 additions & 21 deletions src/群友提交/cmake/CheckLoserHomeWork.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ function(is_target_compiler cpp_file _ret)
set(${_ret} true PARENT_SCOPE)
endfunction()

function(is_use_modules cpp_file _ret)
set(${_ret} false PARENT_SCOPE)
file(STRINGS ${cpp_file} lines)
if("${lines}" MATCHES "import")
set(${_ret} true PARENT_SCOPE)
endif ()
endfunction()

function(add_homework_target target question_index cpp_file)
message(STATUS "add executable: ${target_name} ${cpp_file}")
add_executable (${target} ${cpp_file})
Expand Down Expand Up @@ -146,6 +154,13 @@ function(handle_homework homework_dir _targets_list _run_targets_list)
get_homework_target_name(${index} ${cpp_file} target)
add_homework_target(${target} ${index} ${cpp_file})
list(APPEND targets_list ${target})
is_use_modules(${cpp_file} use_modules)
set_target_properties(${target}
PROPERTIES use_modules ${use_modules})
if(use_modules)
message(NOTICE "${target}: use modules")
endif()

is_check_run(${cpp_file} check_run)
if(check_run)
add_run_homework_target("run_${target}" ${target} add_run_ret)
Expand All @@ -159,25 +174,4 @@ function(handle_homework homework_dir _targets_list _run_targets_list)
endforeach()
set(${_targets_list} ${targets_list} PARENT_SCOPE)
set(${_run_targets_list} ${run_targets_list} PARENT_SCOPE)
endfunction()

function(enable_msvc_build_stl_modules)
if( MSVC_VERSION GREATER_EQUAL 1936 AND MSVC_IDE ) # 17.6+
# When using /std:c++latest, "Build ISO C++23 Standard Library Modules" defaults to "Yes".
# Default to "No" instead.
#
# As of CMake 3.26.4, there isn't a way to control this property
# (https://gitlab.kitware.com/cmake/cmake/-/issues/24922),
# We'll use the MSBuild project system instead
# (https://learn.microsoft.com/en-us/cpp/build/reference/vcxproj-file-structure)
file( CONFIGURE OUTPUT "${CMAKE_BINARY_DIR}/Directory.Build.props" CONTENT [==[
<Project>
<ItemDefinitionGroup>
<ClCompile>
<BuildStlModules>true</BuildStlModules>
</ClCompile>
</ItemDefinitionGroup>
</Project>
]==] @ONLY )
endif()
endfunction()

0 comments on commit aeb37b4

Please sign in to comment.