diff --git a/.github/actions/check-loser-homework/action.yml b/.github/actions/check-loser-homework/action.yml new file mode 100644 index 00000000..d21d7a85 --- /dev/null +++ b/.github/actions/check-loser-homework/action.yml @@ -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 }} \ No newline at end of file diff --git a/.github/workflows/check-all-loser-homework.yml b/.github/workflows/check-all-loser-homework.yml index 8e28650f..82879dc6 100644 --- a/.github/workflows/check-all-loser-homework.yml +++ b/.github/workflows/check-all-loser-homework.yml @@ -6,6 +6,7 @@ on: branches: [ "main" ] paths: - '.github/workflows/**' + - '.github/actions/**' - '**.cmake' - 'CMakeLists.txt' - 'src/群友提交/**' @@ -16,6 +17,7 @@ on: branches: [ "main" ] paths: - '.github/workflows/**' + - '.github/actions/**' - '**.cmake' - 'CMakeLists.txt' - 'src/群友提交/**' @@ -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 @@ -53,11 +47,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: Install clang 17 run: | @@ -65,31 +54,21 @@ jobs: 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 diff --git a/.github/workflows/check-changed-loser-homework.yml b/.github/workflows/check-changed-loser-homework.yml index ed3bbc70..2fdffcad 100644 --- a/.github/workflows/check-changed-loser-homework.yml +++ b/.github/workflows/check-changed-loser-homework.yml @@ -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' @@ -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 @@ -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' @@ -128,17 +113,12 @@ 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 @@ -146,11 +126,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' @@ -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 \ No newline at end of file + uses: ./.github/actions/check-loser-homework + with: + build-path: ${{github.workspace}}/build + shell: pwsh + use-ninja: false \ No newline at end of file diff --git "a/src/\347\276\244\345\217\213\346\217\220\344\272\244/CMakeLists.txt" "b/src/\347\276\244\345\217\213\346\217\220\344\272\244/CMakeLists.txt" index 0643df9c..4d76ee5c 100644 --- "a/src/\347\276\244\345\217\213\346\217\220\344\272\244/CMakeLists.txt" +++ "b/src/\347\276\244\345\217\213\346\217\220\344\272\244/CMakeLists.txt" @@ -7,11 +7,17 @@ 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("$<$:/utf-8>") add_compile_options("$<$:/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}) @@ -19,7 +25,15 @@ 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 () diff --git "a/src/\347\276\244\345\217\213\346\217\220\344\272\244/cmake/CheckLoserHomeWork.cmake" "b/src/\347\276\244\345\217\213\346\217\220\344\272\244/cmake/CheckLoserHomeWork.cmake" index be02a08f..532555d4 100644 --- "a/src/\347\276\244\345\217\213\346\217\220\344\272\244/cmake/CheckLoserHomeWork.cmake" +++ "b/src/\347\276\244\345\217\213\346\217\220\344\272\244/cmake/CheckLoserHomeWork.cmake" @@ -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}) @@ -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) @@ -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 [==[ - - - - true - - - -]==] @ONLY ) - endif() endfunction() \ No newline at end of file